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
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
|
<?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\Integration;
use Piwik\Common;
use Piwik\Config;
use Piwik\Piwik;
use Piwik\SettingsServer;
use Piwik\Tests\Framework\Fixture;
use Piwik\Tests\Framework\TestCase\IntegrationTestCase;
use Piwik\Tests\Framework\Mock\Tracker\Handler;
use Piwik\Tests\Framework\Mock\Tracker\RequestSet;
use Piwik\Tracker;
use Piwik\Tracker\Request;
/**
* @group TrackerTest
* @group Tracker
*/
class TrackerTest extends IntegrationTestCase
{
/**
* @var TestTracker
*/
private $tracker;
/**
* @var Request
*/
private $request;
public function setUp()
{
parent::setUp();
Fixture::createWebsite('2014-01-01 00:00:00');
$this->tracker = new TestTracker();
$this->request = $this->buildRequest(array('idsite' => 1));
}
public function tearDown()
{
$this->restoreConfigFile();
if($this->tracker) {
$this->tracker->disconnectDatabase();
}
if (array_key_exists('PIWIK_TRACKER_DEBUG', $GLOBALS)) {
unset($GLOBALS['PIWIK_TRACKER_DEBUG']);
}
parent::tearDown();
}
public function test_isInstalled_shouldReturnTrue_AsPiwikIsInstalled()
{
$this->assertTrue($this->tracker->isInstalled());
}
public function test_shouldRecordStatistics_shouldReturnTrue_IfEnabled_WhichItIsByDefault()
{
$this->assertTrue($this->tracker->shouldRecordStatistics());
}
public function test_shouldRecordStatistics_shouldReturnFalse_IfEnabledButNotInstalled()
{
$this->tracker->setIsNotInstalled();
$this->assertFalse($this->tracker->shouldRecordStatistics());
}
public function test_shouldRecordStatistics_shouldReturnFalse_IfDisabledButInstalled()
{
$oldConfig = Tracker\TrackerConfig::getConfigValue('record_statistics');
Tracker\TrackerConfig::setConfigValue('record_statistics', 0);
$this->assertFalse($this->tracker->shouldRecordStatistics());
Tracker\TrackerConfig::setConfigValue('record_statistics', $oldConfig); // reset
}
public function test_loadTrackerEnvironment_shouldSetGlobalsDebugVar_WhichShouldBeDisabledByDefault()
{
$this->assertTrue(!array_key_exists('PIWIK_TRACKER_DEBUG', $GLOBALS));
Tracker::loadTrackerEnvironment();
$this->assertFalse($GLOBALS['PIWIK_TRACKER_DEBUG']);
}
public function test_loadTrackerEnvironment_shouldSetGlobalsDebugVar()
{
$this->assertTrue(!array_key_exists('PIWIK_TRACKER_DEBUG', $GLOBALS));
$oldConfig = Tracker\TrackerConfig::getConfigValue('debug');
Tracker\TrackerConfig::setConfigValue('debug', 1);
Tracker::loadTrackerEnvironment();
$this->assertTrue($this->tracker->isDebugModeEnabled());
Tracker\TrackerConfig::setConfigValue('debug', $oldConfig); // reset
$this->assertTrue($GLOBALS['PIWIK_TRACKER_DEBUG']);
}
public function test_loadTrackerEnvironment_shouldEnableTrackerMode()
{
$this->assertTrue(!array_key_exists('PIWIK_TRACKER_DEBUG', $GLOBALS));
$this->assertFalse(SettingsServer::isTrackerApiRequest());
Tracker::loadTrackerEnvironment();
$this->assertTrue(SettingsServer::isTrackerApiRequest());
}
public function test_loadTrackerEnvironment_shouldNotThrow_whenConfigNotFound()
{
$this->assertTrue(!array_key_exists('PIWIK_TRACKER_DEBUG', $GLOBALS));
$this->assertFalse(SettingsServer::isTrackerApiRequest());
$this->assertTrue(is_readable(Config::getInstance()->getLocalPath()));
$this->removeConfigFile();
$this->assertFalse(is_readable(Config::getInstance()->getLocalPath()));
Tracker::loadTrackerEnvironment();
$this->assertTrue(SettingsServer::isTrackerApiRequest());
//always reset on the test itself
$this->restoreConfigFile();
}
public function test_isDatabaseConnected_shouldReturnFalse_IfNotConnected()
{
$this->tracker->disconnectDatabase();
$this->assertFalse($this->tracker->isDatabaseConnected());
}
public function test_getDatabase_shouldReturnDbInstance()
{
$db = $this->tracker->getDatabase();
$this->assertInstanceOf('Piwik\\Tracker\\Db', $db);
}
public function test_isDatabaseConnected_shouldReturnTrue_WhenDbIsConnected()
{
$db = $this->tracker->getDatabase(); // make sure connected
$this->assertNotEmpty($db);
$this->assertTrue($this->tracker->isDatabaseConnected());
}
public function test_disconnectDatabase_shouldDisconnectDb()
{
$this->tracker->getDatabase(); // make sure connected
$this->assertTrue($this->tracker->isDatabaseConnected());
$this->tracker->disconnectDatabase();
$this->assertFalse($this->tracker->isDatabaseConnected());
}
public function test_trackRequest_shouldNotTrackAnything_IfRequestIsEmpty()
{
$called = false;
Piwik::addAction('Tracker.makeNewVisitObject', function () use (&$called) {
$called = true;
});
$this->tracker->trackRequest(new Request(array()));
$this->assertFalse($called);
}
public function test_trackRequest_shouldTrack_IfRequestIsNotEmpty()
{
$called = false;
Piwik::addAction('Tracker.makeNewVisitObject', function () use (&$called) {
$called = true;
});
$this->tracker->trackRequest($this->request);
$this->assertTrue($called);
}
public function test_trackRequest_shouldIncreaseLoggedRequestsCounter()
{
$this->tracker->trackRequest($this->request);
$this->assertSame(1, $this->tracker->getCountOfLoggedRequests());
$this->tracker->trackRequest($this->request);
$this->assertSame(2, $this->tracker->getCountOfLoggedRequests());
}
public function test_trackRequest_shouldIncreaseLoggedRequestsCounter_EvenIfRequestIsEmpty()
{
$request = $this->buildRequest(array());
$this->assertTrue($request->isEmptyRequest());
$this->tracker->trackRequest($request);
$this->assertSame(1, $this->tracker->getCountOfLoggedRequests());
$this->tracker->trackRequest($request);
$this->assertSame(2, $this->tracker->getCountOfLoggedRequests());
}
public function test_trackRequest_shouldActuallyTrack()
{
$request = $this->buildRequest(array('idsite' => 1, 'url' => 'http://www.example.com', 'action_name' => 'test', 'rec' => 1));
$this->tracker->trackRequest($request);
$this->assertActionEquals('test', 1);
$this->assertActionEquals('example.com', 2);
}
public function test_trackRequest_shouldTrackOutlinkWithFragment()
{
$request = $this->buildRequest(array('idsite' => 1, 'link' => 'http://example.com/outlink#fragment-here', 'rec' => 1));
$this->tracker->trackRequest($request);
$this->assertActionEquals('http://example.com/outlink#fragment-here', 1);
}
public function test_trackRequest_shouldTrackDownloadWithFragment()
{
$request = $this->buildRequest(array('idsite' => 1, 'download' => 'http://example.com/file.zip#fragment-here&pk_campaign=Campaign param accepted here', 'rec' => 1));
$this->tracker->trackRequest($request);
$this->assertActionEquals('http://example.com/file.zip#fragment-here&pk_campaign=Campaign param accepted here', 1);
}
public function test_main_shouldReturnEmptyPiwikResponse_IfNoRequestsAreGiven()
{
$requestSet = $this->getEmptyRequestSet();
$requestSet->setRequests(array());
$response = $this->tracker->main($this->getDefaultHandler(), $requestSet);
$expected = "This resource is part of Matomo. Keep full control of your data with the leading free and open source <a href='https://matomo.org' target='_blank' rel='noopener noreferrer'>digital analytics platform</a> for web and mobile.";
$this->assertEquals($expected, $response);
}
public function test_main_shouldReturnApiResponse_IfRequestsAreGiven()
{
$response = $this->tracker->main($this->getDefaultHandler(), $this->getRequestSetWithRequests());
Fixture::checkResponse($response);
}
public function test_main_shouldReturnNotReturnAnyApiResponse_IfImageIsDisabled()
{
$_GET['send_image'] = '0';
$response = $this->tracker->main($this->getDefaultHandler(), $this->getRequestSetWithRequests());
unset($_GET['send_image']);
$this->assertEquals('', $response);
}
public function test_main_shouldActuallyTrackNumberOfTrackedRequests()
{
$this->assertSame(0, $this->tracker->getCountOfLoggedRequests());
$this->tracker->main($this->getDefaultHandler(), $this->getRequestSetWithRequests());
$this->assertSame(2, $this->tracker->getCountOfLoggedRequests());
}
public function test_main_shouldNotTrackAnythingButStillReturnApiResponse_IfNotInstalledOrShouldNotRecordStats()
{
$this->tracker->setIsNotInstalled();
$response = $this->tracker->main($this->getDefaultHandler(), $this->getRequestSetWithRequests());
Fixture::checkResponse($response);
$this->assertSame(0, $this->tracker->getCountOfLoggedRequests());
}
public function test_main_shouldReadValuesFromGETandPOSTifNoRequestSet()
{
$_GET = array('idsite' => '1');
$_POST = array('url' => 'http://localhost/post');
$requestSet = $this->getEmptyRequestSet();
$response = $this->tracker->main($this->getDefaultHandler(), $requestSet);
$_GET = array();
$_POST = array();
Fixture::checkResponse($response);
$this->assertSame(1, $this->tracker->getCountOfLoggedRequests());
$identifiedRequests = $requestSet->getRequests();
$this->assertCount(1, $identifiedRequests);
$this->assertEquals(array('idsite' => '1', 'url' => 'http://localhost/post'),
$identifiedRequests[0]->getParams());
}
public function test_main_shouldPostEndEvent()
{
$called = false;
Piwik::addAction('Tracker.end', function () use (&$called) {
$called = true;
});
$this->tracker->main(new Handler(), new RequestSet());
$this->assertTrue($called);
}
public function test_main_shouldPostEndEvent_EvenIfShouldNotRecordStats()
{
$called = false;
Piwik::addAction('Tracker.end', function () use (&$called) {
$called = true;
});
$handler = new Handler();
$this->tracker->disableRecordStatistics();
$this->tracker->main($handler, new RequestSet());
$this->assertFalse($handler->isProcessed);
$this->assertTrue($called);
}
public function test_main_shouldPostEndEvent_EvenIfThereIsAnException()
{
$called = false;
Piwik::addAction('Tracker.end', function () use (&$called) {
$called = true;
});
$handler = new Handler();
$handler->enableTriggerExceptionInProcess();
$requestSet = new RequestSet();
$requestSet->setRequests(array($this->buildRequest(1), $this->buildRequest(1)));
$this->tracker->main($handler, $requestSet);
$this->assertTrue($handler->isOnException);
$this->assertTrue($called);
}
private function getDefaultHandler()
{
return new Tracker\Handler();
}
private function getEmptyRequestSet()
{
return new RequestSet();
}
private function getRequestSetWithRequests()
{
$requestSet = $this->getEmptyRequestSet();
$requestSet->setRequests(array(
$this->buildRequest(array('idsite' => '1', 'url' => 'http://localhost')),
$this->buildRequest(array('idsite' => '1', 'url' => 'http://localhost/test'))
));
return $requestSet;
}
private function assertActionEquals($expected, $idaction)
{
$actionName = Tracker::getDatabase()->fetchOne("SELECT name FROM " . Common::prefixTable('log_action') . " WHERE idaction = ?", array($idaction));
$this->assertEquals($expected, $actionName);
}
private function buildRequest($params)
{
return new Request($params);
}
/**
* @return string
*/
protected function getLocalConfigPath()
{
return PIWIK_USER_PATH . '/config/config.ini.php';
}
/**
* @return string
*/
protected function getLocalConfigPathMoved()
{
return PIWIK_USER_PATH . '/config/tmp-config.ini.php';
}
protected function removeConfigFile()
{
rename($this->getLocalConfigPath(), $this->getLocalConfigPathMoved());
}
protected function restoreConfigFile()
{
if(file_exists($this->getLocalConfigPathMoved())){
rename($this->getLocalConfigPathMoved(), $this->getLocalConfigPath());
}
}
}
class TestTracker extends Tracker
{
public function __construct()
{
$this->isInstalled = true;
$this->record = true;
}
public function setIsNotInstalled()
{
$this->isInstalled = false;
}
public function disconnectDatabase()
{
parent::disconnectDatabase();
}
public function shouldRecordStatistics()
{
if (! $this->record) {
return false;
}
return parent::shouldRecordStatistics();
}
public function disableRecordStatistics()
{
$this->record = false;
}
}
|