Newer
Older
diosmosis
a validé
/*!
* Piwik - free/libre analytics platform
diosmosis
a validé
*
* 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 TestingEnvironment = function () {
this.reload();
};
TestingEnvironment.prototype.reload = function () {
for (var key in this) {
delete this[key];
}
this['useOverrideCss'] = true;
this['useOverrideJs'] = true;
diosmosis
a validé
this['loadRealTranslations'] = true; // UI tests should test w/ real translations, not translation keys
diosmosis
a validé
this['testUseMockAuth'] = true;
diosmosis
a validé
if (fs.exists(testingEnvironmentOverridePath)) {
var data = JSON.parse(fs.read(testingEnvironmentOverridePath));
for (var key in data) {
this[key] = data[key];
}
}
};
TestingEnvironment.prototype.save = function () {
var copy = {};
for (var key in this) {
copy[key] = this[key];
}
fs.write(testingEnvironmentOverridePath, JSON.stringify(copy));
diosmosis
a validé
};
TestingEnvironment.prototype.callApi = function (method, params, done) {
params.module = "API";
params.method = method;
params.format = 'json';
diosmosis
a validé
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) {
diosmosis
a validé
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) + "&";
}
diosmosis
a validé
}
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) {
diosmosis
a validé
done(new Error("Unable to parse JSON response: " + response));
return;
diosmosis
a validé
done(new Error("API returned error: " + response.message));
return;
diosmosis
a validé
}
page.close();
done(null, response);
});
};
diosmosis
a validé
TestingEnvironment.prototype.executeConsoleCommand = function (command, args, callback) {
var consoleFile = path.join(PIWIK_INCLUDE_PATH, 'console'),
Matthieu Napoli
a validé
commandArgs = [consoleFile, command].concat(args),
diosmosis
a validé
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");
});
diosmosis
a validé
child.on("exit", callback);
};
TestingEnvironment.prototype.addPluginOnCmdLineToTestEnv = function () {
if (options.plugin) {
this.pluginsToLoad = [options.plugin];
this.save();
}
};
diosmosis
a validé
var droppedOnce = false;
TestingEnvironment.prototype.setupFixture = function (fixtureClass, done) {
console.log(" Setting up fixture " + fixtureClass + "...");
this.deleteAndSave();
var args = [fixtureClass || "UITestFixture", '--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']);
}
diosmosis
a validé
var self = this;
this.executeConsoleCommand('tests:setup-fixture', args, function (code) {
self.reload();
self.addPluginOnCmdLineToTestEnv();
self.fixtureClass = fixtureClass;
self.save();
console.log();
diosmosis
a validé
if (code) {
done(new Error("Failed to setup fixture " + fixtureClass + " (error code = " + code + ")"));
} else {
done();
}
});
};
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
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 + "...");
diosmosis
a validé
var args = [fixtureClass || "UITestFixture", "--teardown", '--server-global=' + JSON.stringify(config.phpServer)];
this.executeConsoleCommand('tests:setup-fixture', args, function (code) {
if (code) {
done(new Error("Failed to teardown fixture " + fixtureClass + " (error code = " + code + ")"));
} else {
done();
}
diosmosis
a validé
})
};
TestingEnvironment.prototype.deleteAndSave = function () {
fs.write(testingEnvironmentOverridePath, "{}");
this.reload();
};
diosmosis
a validé
exports.TestingEnvironment = new TestingEnvironment();