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
|
<?php
/**
* Piwik - free/libre analytics platform
*
* @link http://piwik.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*/
namespace Piwik\Tests\System;
use Piwik\Tests\Framework\Fixture;
use Piwik\Tests\Framework\TestCase\SystemTestCase;
use Piwik\Version;
/**
* @group System
*/
class EnvironmentValidationTest extends SystemTestCase
{
public function getEntryPointsToTest()
{
return array(
array('tracker'),
array('web'),
array('console'),
array('archive_web')
);
}
public function setUp()
{
parent::setUp();
$testingEnvironment = new \Piwik\Tests\Framework\TestingEnvironmentVariables();
$testingEnvironment->configFileGlobal = null;
$testingEnvironment->configFileLocal = null;
$testingEnvironment->configFileCommon = null;
$testingEnvironment->loadRealTranslations = true;
$testingEnvironment->save();
}
/**
* @dataProvider getEntryPointsToTest
*/
public function test_NoGlobalConfigFile_TriggersError($entryPoint)
{
$this->simulateAbsentConfigFile('global.ini.php');
$output = $this->triggerPiwikFrom($entryPoint);
$this->assertOutputContainsConfigFileMissingError('global.ini.php', $output);
}
public function test_NoLocalConfigFile_TriggersError_inTracker()
{
$this->simulateAbsentConfigFile('config.ini.php');
$output = $this->triggerPiwikFrom('tracker');
$this->assertContains('As Matomo is not installed yet, the Tracking API cannot proceed and will exit without error.', $output);
}
public function test_NoLocalConfigFile_TriggersError_inConsole()
{
$this->simulateAbsentConfigFile('config.ini.php');
$output = $this->triggerPiwikFrom('console');
$this->assertOutputContainsConfigFileMissingError('config.ini.php', $output);
}
public function test_NoLocalConfigFile_StartsInstallation_PiwikAccessedThroughWeb()
{
$this->simulateAbsentConfigFile('config.ini.php');
$output = $this->triggerPiwikFrom('web');
$this->assertInstallationProcessStarted($output);
}
public function getEntryPointsAndConfigFilesToTest()
{
return array(
array('global.ini.php', 'tracker'),
array('global.ini.php', 'web'),
array('global.ini.php', 'console'),
array('global.ini.php', 'archive_web'),
array('config.ini.php', 'tracker'),
array('config.ini.php', 'web'),
array('config.ini.php', 'console'),
array('config.ini.php', 'archive_web'),
array('common.config.ini.php', 'tracker'),
array('common.config.ini.php', 'web'),
array('common.config.ini.php', 'console'),
array('common.config.ini.php', 'archive_web'),
);
}
/**
* @dataProvider getEntryPointsAndConfigFilesToTest
*/
public function test_BadConfigFile_TriggersError($configFile, $entryPoint)
{
$this->simulateBadConfigFile($configFile);
$output = $this->triggerPiwikFrom($entryPoint);
$this->assertOutputContainsBadConfigFileError($output);
}
/**
* @dataProvider getEntryPointsToTest
*/
public function test_BadDomainSpecificLocalConfigFile_TriggersError($entryPoint)
{
$this->simulateHost('piwik.kobra.org');
$configFile = 'piwik.kobra.org.config.ini.php';
$this->simulateBadConfigFile($configFile);
$output = $this->triggerPiwikFrom($entryPoint);
$this->assertOutputContainsBadConfigFileError($output);
}
private function assertOutputContainsConfigFileMissingError($fileName, $output)
{
$this->assertRegExp("/.*The configuration file \\{.*\\/" . preg_quote($fileName) . "\\} has not been found or could not be read\\..*/", $output, "Output did not contain the expected exception for $fileName --- Output was --- $output");
}
private function assertOutputContainsBadConfigFileError($output)
{
$this->assertRegExp("/Unable to read INI file \\{.*\\/piwik.php\\}:/", $output);
$this->assertRegExp("/Your host may have disabled parse_ini_file\\(\\)/", $output);
}
private function assertInstallationProcessStarted($output)
{
$this->assertContains('<title>Matomo '. Version::VERSION .' › Installation</title>', $output);
}
private function simulateAbsentConfigFile($fileName)
{
$testingEnvironment = new \Piwik\Tests\Framework\TestingEnvironmentVariables();
if ($fileName == 'global.ini.php') {
$testingEnvironment->configFileGlobal = PIWIK_INCLUDE_PATH . '/tmp/nonexistant/global.ini.php';
} else if ($fileName == 'common.config.ini.php') {
$testingEnvironment->configFileCommon = PIWIK_INCLUDE_PATH . '/tmp/nonexistant/common.config.ini.php';
} else {
$testingEnvironment->configFileLocal = PIWIK_INCLUDE_PATH . '/tmp/nonexistant/' . $fileName;
}
$testingEnvironment->save();
}
private function simulateBadConfigFile($fileName)
{
$testingEnvironment = new \Piwik\Tests\Framework\TestingEnvironmentVariables();
if ($fileName == 'global.ini.php') {
$testingEnvironment->configFileGlobal = PIWIK_INCLUDE_PATH . '/piwik.php';
} else if ($fileName == 'common.config.ini.php') {
$testingEnvironment->configFileCommon = PIWIK_INCLUDE_PATH . '/piwik.php';
} else {
$testingEnvironment->configFileLocal = PIWIK_INCLUDE_PATH . '/piwik.php';
}
$testingEnvironment->save();
}
private function simulateHost($host)
{
$testingEnvironment = new \Piwik\Tests\Framework\TestingEnvironmentVariables();
$testingEnvironment->hostOverride = $host;
$testingEnvironment->save();
}
private function triggerPiwikFrom($entryPoint)
{
if ($entryPoint == 'tracker') {
return $this->sendRequestToTracker();
} else if ($entryPoint == 'web') {
return $this->sendRequestToWeb();
} else if ($entryPoint == 'console') {
return $this->startConsoleProcess();
} else if ($entryPoint == 'archive_web') {
return $this->sendArchiveWebRequest();
} else {
throw new \Exception("Don't know how to access '$entryPoint'.");
}
}
private function sendRequestToTracker()
{
list($response, $info) = $this->curl(Fixture::getRootUrl() . 'tests/PHPUnit/proxy/piwik.php?idsite=1&rec=1&action_name=something');
// Check Tracker requests return 200
$this->assertEquals(200, $info["http_code"], 'Ok response');
return $response;
}
private function sendRequestToWeb()
{
list($response, $info) = $this->curl(Fixture::getRootUrl() . 'tests/PHPUnit/proxy/index.php');
return $response;
}
private function sendArchiveWebRequest()
{
list($response, $info) = $this->curl(Fixture::getRootUrl() . 'tests/PHPUnit/proxy/archive.php?token_auth=' . Fixture::getTokenAuth());
return $response;
}
private function startConsoleProcess()
{
$pathToProxyConsole = PIWIK_INCLUDE_PATH . '/tests/PHPUnit/proxy/console';
return shell_exec("php '$pathToProxyConsole' list 2>&1");
}
private function curl($url)
{
if (!function_exists('curl_init')) {
$this->markTestSkipped('Curl is not installed');
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
$response = curl_exec($ch);
$headerSize = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
$response = substr($response, $headerSize);
$responseInfo = curl_getinfo($ch);
curl_close($ch);
return array($response, $responseInfo);
}
}
|