-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
/
UITest.php
203 lines (165 loc) · 6.69 KB
/
UITest.php
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
<?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\Date;
use Piwik\Piwik;
use Piwik\Access;
use Piwik\AssetManager;
use Piwik\Plugins\VisitsSummary\API;
abstract class UITest extends IntegrationTestCase
{
const IMAGE_TYPE = 'png';
const CAPTURE_PROGRAM = 'phantomjs';
private static $recursiveProxyLinkNames = array('libs', 'plugins', 'tests');
public static function createAccessInstance()
{
Access::setSingletonInstance($access = new Test_Access_OverrideLogin());
Piwik_PostEvent('FrontController.initAuthenticationObject');
}
public static function setUpBeforeClass()
{
if (!self::isXvfbAvailable()) {
self::markTestSkipped("xvfb is not available, skipping UI integration tests. (install with 'sudo apt-get install xvfb')");
} else 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();
AssetManager::removeMergedAssets();
// launch archiving so tests don't run out of time
$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
$urls = array();
foreach (static::getUrlsForTesting() as $testInfo) {
list($name, $urlQuery) = $testInfo;
list($processedScreenshotPath, $expectedScreenshotPath) = self::getProcessedAndExpectedScreenshotPaths($name);
$urls[] = array($processedScreenshotPath, self::getProxyUrl() . $urlQuery);
}
echo "Generating screenshots...\n";
self::runCaptureProgram($urls);
}
public static function tearDownAfterClass()
{
if (file_exists("C:\\nppdf32Log\\debuglog.txt")) { // remove slimerjs oddity
unlink("C:\\nppdf32Log\\debuglog.txt");
}
self::removeRecursiveLinks();
if (!Zend_Registry::get('db')) {
Piwik::createDatabaseObject();
}
parent::tearDownAfterClass();
}
public function setUp()
{
parent::setUp();
if (!Zend_Registry::get('db')) {
Piwik::createDatabaseObject();
}
}
public function tearDown()
{
parent::tearDown();
\Zend_Registry::get('db')->closeConnection();
\Zend_Registry::set('db', false);
}
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";
$cmd = 'xvfb-run --server-args="-screen 0, 1024x768x24" ' . $cmd;
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";
throw new Exception("phantomjs failed");
}
return $output;
}
protected function compareScreenshot($name, $urlQuery)
{
list($expectedPath, $processedPath) = self::getProcessedAndExpectedScreenshotPaths($name);
$processed = file_get_contents($processedPath);
if (!file_exists($expectedPath)) {
$this->markTestIncomplete("expected screenshot for processed '$processedPath' is missing");
}
$expected = file_get_contents($expectedPath);
if ($expected != $processed) {
echo "\nFail: '$processedPath' for '$urlQuery'\n";
}
$this->assertTrue($expected == $processed, "screenshot compare failed for '$processedPath'");
}
private static function isSlimerJsAvailable()
{
return self::isProgramAvailable('slimerjs');
}
private static function isPhantomJsAvailable()
{
return self::isProgramAvailable('phantomjs');
}
private static function isXvfbAvailable()
{
return self::isProgramAvailable('xvfb-run');
}
private static function isProgramAvailable($name)
{
exec($name . ' --help 2>&1', $output, $result);
return $result === 0 || $result === 1;
}
private static function getProcessedAndExpectedScreenshotPaths($name)
{
list($processedDir, $expectedDir) = self::getProcessedAndExpectedDirs();
$outputPrefix = static::getOutputPrefix();
$processedScreenshotPath = $processedDir . $outputPrefix . '_' . "$name." . self::IMAGE_TYPE;
$expectedScreenshotPath = $expectedDir . $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 = self::getProcessedAndExpectedDirs();
foreach ($dirs as $dir) {
if (!is_dir($dir)) {
mkdir($dir);
}
}
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);
}
}
}
}