Welcome to mirror list, hosted at ThFree Co, Russian Federation.

test-environment.js « support « screenshot-testing « lib « tests - github.com/matomo-org/matomo.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: ffe43cb431b0cdd9ba397ddb5c28847059a705ef (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
/*!
 * Piwik - free/libre analytics platform
 *
 * Test environment overriding
 *
 * @link http://piwik.org
 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
 */

var fs = require('fs'),
    testingEnvironmentOverridePath = path.join(PIWIK_INCLUDE_PATH, '/tmp/testingPathOverride.json');

var DEFAULT_UI_TEST_FIXTURE_NAME = "Piwik\\Tests\\Fixtures\\UITestFixture";

var TestingEnvironment = function () {
    this.reload();
};

TestingEnvironment.prototype.reload = function () {
    for (var key in this) {
        delete this[key];
    }

    this['useOverrideCss'] = true;
    this['useOverrideJs'] = true;
    this['loadRealTranslations'] = true; // UI tests should test w/ real translations, not translation keys
    this['testUseMockAuth'] = true;
    this['configOverride'] = {};

    if (fs.exists(testingEnvironmentOverridePath)) {
        var data = JSON.parse(fs.read(testingEnvironmentOverridePath));
        for (var key in data) {
            this[key] = data[key];
        }
    }
};

/**
 * Overrides a config entry.
 *
 * You can use this method either to set one specific config value `overrideConfig(group, name, value)`
 * or you can set a whole group of values `overrideConfig(group, valueObject)`.
 */
TestingEnvironment.prototype.overrideConfig = function (group, name, value) {
    if (!name) {
        return;
    }

    if (!this['configOverride']) {
        this['configOverride'] = {};
    }

    if ((typeof value) === 'undefined') {
        this['configOverride'][group] = name;
        return;
    }

    if (!this['configOverride'][group]) {
        this['configOverride'][group] = {};
    }

    this['configOverride'][group][name] = value;
};

TestingEnvironment.prototype.save = function () {
    var copy = {};
    for (var key in this) {
        copy[key] = this[key];
    }

    fs.write(testingEnvironmentOverridePath, JSON.stringify(copy));
};

TestingEnvironment.prototype.callApi = function (method, params, done) {
    params.module = "API";
    params.method = method;
    params.format = 'json';

    this._call(params, done);
};

TestingEnvironment.prototype.callController = function (method, params, done) {
    var parts = method.split('.');

    params.module = parts[0];
    params.action = parts[1];
    params.idSite = params.idSite || 1;

    this._call(params, done);
};

TestingEnvironment.prototype._call = function (params, done) {
    var url = path.join(config.piwikUrl, "tests/PHPUnit/proxy/index.php?");
    for (var key in params) {
        var value = params[key];
        if (value instanceof Array) {
            for (var i = 0; i != value.length; ++i) {
                url += key + "[]=" + encodeURIComponent(value[i]) + "&";
            }
        } else {
            url += key + "=" + encodeURIComponent(value) + "&";
        }
    }
    url = url.substring(0, url.length - 1);

    var page = require('webpage').create();
    page.open(url, function () {
        var response = page.plainText;
        if (response.replace(/\s*/g, "")) {
            try {
                response = JSON.parse(response);
            } catch (e) {
                page.close();

                done(new Error("Unable to parse JSON response: " + response));
                return;
            }

            if (response.result == "error") {
                page.close();

                done(new Error("API returned error: " + response.message));
                return;
            }
        }

        page.close();

        done(null, response);
    });
};

TestingEnvironment.prototype.executeConsoleCommand = function (command, args, callback) {
    var consoleFile = path.join(PIWIK_INCLUDE_PATH, 'console'),
        commandArgs = [consoleFile, command].concat(args),
        child = require('child_process').spawn(config.php, commandArgs);

    var firstLine = true;
    child.stdout.on("data", function (data) {
        if (firstLine) {
            data = "    " + data;
            firstLine = false;
        }

        fs.write("/dev/stdout", data.replace(/\n/g, "\n    "), "w");
    });

    child.stderr.on("data", function (data) {
        if (firstLine) {
            data = "    " + data;
            firstLine = false;
        }

        fs.write("/dev/stderr", data, "w");
    });

    child.on("exit", callback);
};

TestingEnvironment.prototype.addPluginOnCmdLineToTestEnv = function () {
    if (options.plugin) {
        this.pluginsToLoad = [options.plugin];
        this.save();
    }
};

var droppedOnce = false;
TestingEnvironment.prototype.setupFixture = function (fixtureClass, done) {
    console.log("    Setting up fixture " + fixtureClass + "...");

    this.deleteAndSave();

    var args = [
        fixtureClass || DEFAULT_UI_TEST_FIXTURE_NAME,
        '--set-phantomjs-symlinks',
        '--server-global=' + JSON.stringify(config.phpServer)
    ];

    if (options['persist-fixture-data']) {
        args.push('--persist-fixture-data');
    }

    if (options['drop']
        && !droppedOnce
    ) {
        args.push('--drop');
        droppedOnce = true;
    }

    if (options['plugin']) {
        args.push('--plugins=' + options['plugin']);
    }

    if (options['piwik-domain']) {
        args.push('--piwik-domain=' + options['piwik-domain']);
    }

    var self = this;
    this.executeConsoleCommand('tests:setup-fixture', args, function (code) {
        self.reload();
        self.addPluginOnCmdLineToTestEnv();

        self.fixtureClass = fixtureClass;
        self.save();

        console.log();

        if (code) {
            done(new Error("Failed to setup fixture " + fixtureClass + " (error code = " + code + ")"));
        } else {
            done();
        }
    });
};

TestingEnvironment.prototype.readDbInfoFromConfig = function () {

    var username = 'root';
    var password = '';

    var pathConfigIni = path.join(PIWIK_INCLUDE_PATH, "/config/config.ini.php");

    var configFile = fs.read(pathConfigIni);

    if (configFile) {
        var match = ('' + configFile).match(/password\s?=\s?"(.*)"/);

        if (match && match.length) {
            password = match[1];
        }

        match = ('' + configFile).match(/username\s?=\s?"(.*)"/);

        if (match && match.length) {
            username = match[1];
        }
    }

    return {
        username: username,
        password: password
    }
};

TestingEnvironment.prototype.teardownFixture = function (fixtureClass, done) {
    if (options['persist-fixture-data']
        || !fixtureClass
    ) {
        done();
        return;
    }

    console.log();
    console.log("    Tearing down fixture " + fixtureClass + "...");

    var args = [fixtureClass || DEFAULT_UI_TEST_FIXTURE_NAME, "--teardown", '--server-global=' + JSON.stringify(config.phpServer)];

    if (options['piwik-domain']) {
        args.push('--piwik-domain=' + options['piwik-domain']);
    }

    this.executeConsoleCommand('tests:setup-fixture', args, function (code) {
        if (code) {
            done(new Error("Failed to teardown fixture " + fixtureClass + " (error code = " + code + ")"));
        } else {
            done();
        }
    })
};

TestingEnvironment.prototype.deleteAndSave = function () {
    fs.write(testingEnvironmentOverridePath, "{}");
    this.reload();
};

exports.TestingEnvironment = new TestingEnvironment();