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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
|
<?php
/**
* Piwik - Open source web analytics
*
* @link http://piwik.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*/
use Piwik\Access;
use Piwik\AssetManager;
use Piwik\Date;
use Piwik\Db;
use Piwik\DbHelper;
use Piwik\Plugins\VisitsSummary\API;
use Piwik\ArchiveProcessor\Rules;
abstract class UITest extends IntegrationTestCase
{
const IMAGE_TYPE = 'png';
const CAPTURE_PROGRAM = 'phantomjs';
const SCREENSHOT_GROUP_SIZE = 12;
const DEBUG_IMAGE_MAGICK_COMPARE = true;
private static $recursiveProxyLinkNames = array('libs', 'plugins', 'tests');
private static $imageMagickAvailable = false;
public static $failureScreenshotNames = array();
public static function createAccessInstance()
{
Access::setSingletonInstance($access = new Test_Access_OverrideLogin());
\Piwik\Piwik::postEvent('Request.initAuthenticationObject');
}
public static function setUpBeforeClass()
{
if (self::CAPTURE_PROGRAM == 'slimerjs'
&& !self::isSlimerJsAvailable()
) {
self::markTestSkipped("slimerjs is not available, skipping UI integration tests. "
. "(install by downloading http://slimerjs.org/download.html)");
} else if (self::CAPTURE_PROGRAM == 'phantomjs'
&& !self::isPhantomJsAvailable()
) {
self::markTestSkipped("phantomjs is not available, skipping UI integration tests. "
. "(install by downloading http://phantomjs.org/download.html)");
}
parent::setUpBeforeClass();
DbHelper::createAnonymousUser();
AssetManager::removeMergedAssets();
// launch archiving so tests don't run out of time
Rules::$purgeDisabledByTests = true;
$date = Date::factory(static::$fixture->dateTime)->toString();
API::getInstance()->get(static::$fixture->idSite, 'year', $date);
// make sure processed & expected dirs exist
self::makeDirsAndLinks();
// run slimerjs/phantomjs w/ all urls so we only invoke it once per 25 entries (travis needs
// there to be output)
$urlsToTest = static::getUrlsForTesting();
reset($urlsToTest);
for ($i = 0; $i < count($urlsToTest); $i += self::SCREENSHOT_GROUP_SIZE) {
$urls = array();
echo "Generating screenshots...";
for ($j = $i; $j != $i + self::SCREENSHOT_GROUP_SIZE && $j < count($urlsToTest); ++$j) {
$currentTest = current($urlsToTest);
if (count($currentTest) == 2) {
list($name, $urlQuery) = $currentTest;
$jsToTest = false;
} else {
list($name, $urlQuery, $jsToTest) = $currentTest;
}
$testUrl = self::getProxyUrl() . $urlQuery;
// Screenshot Zeitgeist
list($processedScreenshotPath, $expectedScreenshotPath) = self::getProcessedAndExpectedScreenshotPaths($name);
$urls[] = array($processedScreenshotPath, $testUrl, $jsToTest);
// Screenshot Morpheus
list($processedScreenshotPath, $expectedScreenshotPath) = self::getProcessedAndExpectedScreenshotPaths($name, "Morpheus/");
$enableMorpheus = "&morpheus=1";
// Add the parameter to the query string, not the hash
if(($hash = strpos($testUrl, '#')) !== false) {
$testUrl = substr($testUrl, 0, $hash) . $enableMorpheus . substr($testUrl, $hash);
} else {
$testUrl .= $enableMorpheus;
}
$urls[] = array($processedScreenshotPath, $testUrl, $jsToTest);
next($urlsToTest);
echo ".";
}
echo "\n";
self::runCaptureProgram($urls);
}
// check if image magick available
self::$imageMagickAvailable = self::checkImageMagickAvailable();
// remove existing diffs
self::removeExistingDiffs();
}
public static function removeExistingDiffs()
{
$files = glob(dirname(__FILE__) . '/UI/screenshot-diffs/*.png');
foreach ($files as $file) {
unlink($file);
}
}
public static function tearDownAfterClass()
{
if (file_exists("C:\\nppdf32Log\\debuglog.txt")) { // remove slimerjs oddity
unlink("C:\\nppdf32Log\\debuglog.txt");
}
self::removeRecursiveLinks();
Db::createDatabaseObject();
self::outputDiffViewerHtmlFile();
parent::tearDownAfterClass();
}
public function setUp()
{
parent::setUp();
if (!Db::get()) {
DbHelper::createDatabaseObject();
}
}
public function tearDown()
{
parent::tearDown();
Db::get()->closeConnection();
}
private static function runCaptureProgram($urlInfo)
{
file_put_contents(PIWIK_INCLUDE_PATH . '/tmp/urls.txt', json_encode($urlInfo));
$cmd = self::CAPTURE_PROGRAM . " \"" . PIWIK_INCLUDE_PATH . "/tests/resources/screenshot-capture/capture.js\" 2>&1";
exec($cmd, $output, $result);
$output = implode("\n", $output);
if ($result !== 0
|| strpos($output, "ERROR") !== false
) {
echo self::CAPTURE_PROGRAM . " failed: " . $output . "\n\ncommand used: $cmd\n";
}
return $output;
}
protected function compareScreenshots($testsInfo)
{
$failures = array();
foreach ($testsInfo as $info) {
list($name, $urlQuery) = $info;
// compare processed w/ expected
try {
$this->compareScreenshot($name, $urlQuery);
} catch (Exception $ex) {
$failures[] = $ex;
}
}
}
protected function compareScreenshot($name, $urlQuery)
{
list($processedPath, $expectedPath) = self::getProcessedAndExpectedScreenshotPaths($name);
$this->compareScreenshotAgainstExpected($name, $urlQuery, $processedPath, $expectedPath);
}
private function saveImageDiff($expectedPath, $processedPath, $diffPath)
{
$cmd = "compare \"$expectedPath\" \"$processedPath\" \"$diffPath\" 2>&1";
exec($cmd, $output, $result);
if (self::DEBUG_IMAGE_MAGICK_COMPARE
&& $result !== 0
) {
echo "Could not save image diff: " . implode("\n", $output) . "\n";
}
}
private static function checkImageMagickAvailable()
{
return self::isProgramAvailable('compare');
}
private static function isSlimerJsAvailable()
{
return self::isProgramAvailable('slimerjs');
}
public static function isPhantomJsAvailable()
{
return self::isProgramAvailable('phantomjs');
}
private static function isProgramAvailable($name)
{
exec($name . ' --help 2>&1', $output, $result);
return $result === 0 || $result === 1;
}
private static function getProcessedAndExpectedScreenshotPaths($name, $pathSuffix = '')
{
list($processedDir, $expectedDir) = self::getProcessedAndExpectedDirs();
$outputPrefix = static::getOutputPrefix();
$processedScreenshotPath = $processedDir . $pathSuffix . $outputPrefix . '_' . "$name." . self::IMAGE_TYPE;
$expectedScreenshotPath = $expectedDir . $pathSuffix . $outputPrefix . '_' . "$name." . self::IMAGE_TYPE;
return array($processedScreenshotPath, $expectedScreenshotPath);
}
protected static function getProcessedAndExpectedDirs()
{
$path = self::getPathToTestDirectory() . '/../UI';
return array($path . '/processed-ui-screenshots/', $path . '/expected-ui-screenshots/');
}
public static function getProxyUrl()
{
return Test_Piwik_BaseFixture::getRootUrl() . 'tests/PHPUnit/proxy/index.php';
}
private static function makeDirsAndLinks()
{
$dirs = array_merge(self::getProcessedAndExpectedDirs(), array(
PIWIK_INCLUDE_PATH . '/tmp/sessions', self::getScreenshotDiffDir()
));
foreach ($dirs as $dir) {
if (!is_dir($dir)) {
mkdir($dir);
}
}
self::createProxySymlinks();
}
private static function createProxySymlinks()
{
foreach (self::$recursiveProxyLinkNames as $linkName) {
$linkPath = PIWIK_INCLUDE_PATH . '/tests/PHPUnit/proxy/' . $linkName;
if (!file_exists($linkPath)) {
symlink(PIWIK_INCLUDE_PATH . '/' . $linkName, $linkPath);
}
}
}
private static function removeRecursiveLinks()
{
foreach (self::$recursiveProxyLinkNames as $linkName) {
$wholePath = PIWIK_INCLUDE_PATH . '/tests/PHPUnit/proxy/' . $linkName;
if (file_exists($wholePath)) {
unlink($wholePath);
}
}
}
private static function getScreenshotDiffPath($name)
{
$outputPrefix = static::getOutputPrefix();
$diffDir = self::getScreenshotDiffDir();
return $diffDir . "/" . $outputPrefix . '_' . $name . '.' . self::IMAGE_TYPE;
}
private static function getScreenshotDiffDir()
{
return dirname(__FILE__) . "/UI/screenshot-diffs";
}
private static function outputDiffViewerHtmlFile()
{
if (!empty(self::$failureScreenshotNames)) {
$diffViewerPath = self::getScreenshotDiffDir() . '/diffviewer.html';
echo "\nFailures encountered. View all diffs at:
$diffViewerPath
If processed screenshots are correct, you can copy the generated screenshots to the expected screenshot folder.
*** IMPORTANT *** In your commit message, explain the cause of the difference in rendering so other Piwik developers will be aware of it.";
}
list($processedDir, $expectedDir) = self::getProcessedAndExpectedDirs();
$diffDir = self::getScreenshotDiffDir();
$diffViewerEntries = array();
foreach (self::$failureScreenshotNames as $name) {
$name = static::getOutputPrefix() . '_' . $name;
$file = $name . '.png';
$diffFileOrError = $file;
if (!is_file($diffDir . '/' . $file)) {
$diffFileOrError = false;
}
$diffViewerEntries[] = array(
'name' => $name,
'expectedUrl' => 'https://raw.github.com/piwik/piwik-ui-tests/master/expected-ui-screenshots/' . $file,
'processedUrl' => '../processed-ui-screenshots/' . $file,
'diffUrl' => $diffFileOrError
);
}
$diffViewerHtml = '<html>
<head></head>
<body>
<h1>Screenshot Test Failures</h1>
<table>
<tr>
<th>Name</th>
<th>Expected</th>
<th>Processed</th>
<th>Difference</th>
</tr>';
foreach ($diffViewerEntries as $fileInfo) {
$diffViewerHtml .= '
<tr>
<td>' . $fileInfo['name'] . '</td>
<td><a href="' . $fileInfo['expectedUrl'] . '">Expected</a></td>
<td><a href="' . $fileInfo['processedUrl'] . '">Processed</a></td>
<td>';
if (!empty($fileInfo['diffUrl'])) {
$diffViewerHtml .= '<a href="' . $fileInfo['diffUrl'] . '">Difference</a>';
} else {
$diffViewerHtml .= "<em>Could not create diff.</em>";
}
$diffViewerHtml .= '
</td>
</tr>';
}
$diffViewerHtml .= '
</table>
</body>
</html>';
file_put_contents($diffDir . '/diffviewer.html', $diffViewerHtml);
}
/**
* @param $name
* @param $urlQuery
* @param $processedPath
* @param $expectedPath
*/
protected function compareScreenshotAgainstExpected($name, $urlQuery, $processedPath, $expectedPath)
{
if (!file_exists($processedPath)) {
$this->fail("failed to generate screenshot for '$name'.");
return;
}
$processed = file_get_contents($processedPath);
if (!file_exists($expectedPath)) {
$this->fail("expected screenshot for '$name' test is missing.
Generated screenshot: $processedPath");
return;
}
$expected = file_get_contents($expectedPath);
if ($expected != $processed) {
self::$failureScreenshotNames[] = $name;
$diffPath = self::getScreenshotDiffPath($name);
echo "\nFail: generated screenshot does not match expected for '$name'.
Url to reproduce: $urlQuery
Generated screenshot: $processedPath
Expected screenshot: $expectedPath
Screenshot diff: $diffPath\n";
$this->saveImageDiff($expectedPath, $processedPath, $diffPath);
}
$this->assertTrue($expected == $processed, "screenshot compare failed for '$processedPath'");
}
}
|