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: e7a0f1f44ebecbf6ed327702e376fca2ba61df29 (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
/*!
 * Piwik - Web Analytics
 *
 * 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];
    }

    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));
};

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) {
                done(new Error("Unable to parse JSON response: " + response));
                return;
            }

            if (response.result == "error") {
                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);
};

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;
    }

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

        console.log();

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

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 || "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();
        }
    })
};

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

exports.TestingEnvironment = new TestingEnvironment();