/*!
 * Matomo - free/libre analytics platform
 *
 * CoreUpdater screenshot tests.
 *
 * @link    https://matomo.org
 * @license https://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
 */

describe("CoreUpdaterDb", function () {
    this.fixture = "Piwik\\Plugins\\CoreUpdater\\tests\\Fixtures\\DbUpdaterTestFixture";

    const updateDetailsToken = 'uiTestUpdateDetailsToken'

    before(function () {
        testEnvironment.tablesPrefix = 'piwik_';
        testEnvironment.configOverride.General = {
            update_details_token: updateDetailsToken
        };
        testEnvironment.save();
    });

    after(function () {
        if (testEnvironment.configOverride.General) {
            delete testEnvironment.configOverride.General;
            testEnvironment.save();
        }
    });

    function apiUpgradeTest(format) {
        it("should start the updater when an old version of Piwik is detected in the DB with format " + format, async function() {
            await page.goto(""); // page.downloadUrl needs jQuery loaded, so load a page
            const url = '?module=API&method=API.getPiwikVersion&format=' + format;
            var pageContents = await page.downloadUrl(url);

            expect.file('CoreUpdater.API.ErrorMessage' + format + '.txt').to.equal(pageContents);
        });
    }

    var formats = ['CSV', 'TSV', 'XML', 'JSON'];
    formats.forEach(apiUpgradeTest);

    it("should start the updater when an old version of Piwik is detected in the DB", async function() {
        await page.goto("");
        await page.evaluate(function () {
            $('p').each(function () {
                var replace = $(this).html().replace(/(?!1\.0)\d+\.\d+(\.\d+)?([\-a-z]*\d+|-alpha)?(\.[0-9]{14})?/g, '');
                $(this).html(replace);
            });
        });

        expect(await page.screenshot({ fullPage: true })).to.matchImage('main_no_update_token');
    });

    it("should show update details if correct token is provided", async function () {
        const currentUrl = await page.getWholeCurrentUrl();

        await page.goto(currentUrl + '&updateDetailsToken=' + updateDetailsToken);
        await page.evaluate(function () {
            $('p').each(function () {
                var replace = $(this).html().replace(/(?!1\.0)\d+\.\d+(\.\d+)?([\-a-z]*\d+|-alpha)?(\.[0-9]{14})?/g, '');
                $(this).html(replace);
            });
        });

        expect(await page.screenshot({ fullPage: true })).to.matchImage('main');
    });

    it("should show the file integrity dialog", async function() {
        await page.click('#more-results');
        await page.waitForTimeout(50);

        expect(await page.screenshot({ fullPage: true })).to.matchImage('file-integrity');
    });

    it("should show instance id in updating screen", async function() {
        testEnvironment.configOverride.General.instance_id = 'custom.instance';
        testEnvironment.save();

        const currentUrl = await page.getWholeCurrentUrl();

        await page.goto(currentUrl + '&updateDetailsToken=' + updateDetailsToken);
        await page.evaluate(function () {
            $('p').each(function () {
                var replace = $(this).html().replace(/(?!1\.0)\d+\.\d+(\.\d+)?([\-a-z]*\d+|-alpha)?(\.[0-9]{14})?/g, '');
                $(this).html(replace);
            });
        });

        expect(await page.screenshot({ fullPage: true })).to.matchImage('main_instance');
    });

    it("should show the donation form when the update process is complete", async function() {
        await page.click('.btn');
        await page.waitForNetworkIdle();

        expect(await page.screenshot({ fullPage: true })).to.matchImage('updated');
    });
});
