Welcome to mirror list, hosted at ThFree Co, Russian Federation.

RequestSetTest.php « Tracker « Unit « PHPUnit « tests - github.com/matomo-org/matomo.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 871582c91577bb00caa07783d1e869535d787990 (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
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
<?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\Unit\Tracker;

use Piwik\Tracker\Request;
use Piwik\Tracker\RequestSet;

/**
 * @group RequestSetTest
 * @group Tracker
 */
class RequestSetTest extends \PHPUnit_Framework_TestCase
{
    /**
     * @var TestRequestSet
     */
    private $requestSet;
    private $time;

    public function setUp()
    {
        parent::setUp();

        $this->requestSet = $this->createRequestSet();
        $this->time = time();
    }

    private function createRequestSet()
    {
        return new TestRequestSet();
    }

    public function test_internalBuildRequest_ShoulBuildOneRequest()
    {
        $request = new Request(array('idsite' => '2'));
        $request->setCurrentTimestamp($this->time);

        $this->assertEquals($request, $this->buildRequest(2));
    }

    public function test_internalBuildRequests_ShoulBuildASetOfRequests()
    {
        $this->assertEquals(array(), $this->buildRequests(0));

        $this->assertEquals(array($this->buildRequest(1)), $this->buildRequests(1));

        $this->assertEquals(array(
            $this->buildRequest(1),
            $this->buildRequest(2),
            $this->buildRequest(3)
        ), $this->buildRequests(3));
    }

    public function test_getRequests_shouldReturnEmptyArray_IfThereAreNoRequestsInitializedYet()
    {
        $this->assertEquals(array(), $this->requestSet->getRequests());
    }

    public function test_setRequests_shouldNotFail_IfEmptyArrayGiven()
    {
        $this->requestSet->setRequests(array());
        $this->assertEquals(array(), $this->requestSet->getRequests());
    }

    public function test_setRequests_shouldSetAndOverwriteRequests()
    {
        $this->requestSet->setRequests($this->buildRequests(3));
        $this->assertEquals($this->buildRequests(3), $this->requestSet->getRequests());

        // overwrite
        $this->requestSet->setRequests($this->buildRequests(5));
        $this->assertEquals($this->buildRequests(5), $this->requestSet->getRequests());

        // overwrite
        $this->requestSet->setRequests($this->buildRequests(1));
        $this->assertEquals($this->buildRequests(1), $this->requestSet->getRequests());

        // clear
        $this->requestSet->setRequests(array());
        $this->assertEquals(array(), $this->requestSet->getRequests());
    }

    public function test_setRequests_shouldConvertNonRequestInstancesToARequestInstance()
    {
        $requests = array(
            $this->buildRequest(5),
            array('idsite' => 9),
            $this->buildRequest(2),
            array('idsite' => 3),
            $this->buildRequest(6)
        );

        $this->requestSet->setRequests($requests);

        $setRequests = $this->requestSet->getRequests();
        $this->assertEquals($this->buildRequest(5), $setRequests[0]);
        $this->assertEquals($this->buildRequest(2), $setRequests[2]);
        $this->assertEquals($this->buildRequest(6), $setRequests[4]);

        $this->assertTrue($setRequests[1] instanceof Request);
        $this->assertEquals(array('idsite' => 9), $setRequests[1]->getParams());

        $this->assertTrue($setRequests[3] instanceof Request);
        $this->assertEquals(array('idsite' => 3), $setRequests[3]->getParams());

        $this->assertCount(5, $setRequests);
    }

    public function test_setRequests_shouldIgnoreEmptyRequestsButNotArrays()
    {
        $requests = array(
            $this->buildRequest(5),
            null,
            $this->buildRequest(2),
            0,
            $this->buildRequest(6),
            array()
        );

        $this->requestSet->setRequests($requests);

        $expected = array($this->buildRequest(5), $this->buildRequest(2), $this->buildRequest(6), new Request(array()));
        $this->assertEquals($expected, $this->requestSet->getRequests());
    }

    public function test_getNumberOfRequests_shouldReturnZeroIfNothingSet()
    {
        $this->assertEquals(0, $this->requestSet->getNumberOfRequests());
    }

    public function test_getNumberOfRequests_shouldReturnNumberOfRequests()
    {
        $this->requestSet->setRequests($this->buildRequests(3));
        $this->assertSame(3, $this->requestSet->getNumberOfRequests());

        $this->requestSet->setRequests($this->buildRequests(5));
        $this->assertSame(5, $this->requestSet->getNumberOfRequests());

        $this->requestSet->setRequests($this->buildRequests(1));
        $this->assertSame(1, $this->requestSet->getNumberOfRequests());
    }

    public function test_hasRequests_shouldReturnFalse_IfNotInitializedYetOrNoDataSet()
    {
        $this->assertFalse($this->requestSet->hasRequests());

        $this->requestSet->setRequests(array());
        $this->assertFalse($this->requestSet->hasRequests());
    }

    public function test_hasRequests_shouldReturnTrue_IfAtLeastOneRequestIsSet()
    {
        $this->assertFalse($this->requestSet->hasRequests());

        $this->requestSet->setRequests($this->buildRequests(1));
        $this->assertTrue($this->requestSet->hasRequests());

        $this->requestSet->setRequests($this->buildRequests(5));
        $this->assertTrue($this->requestSet->hasRequests());

        $this->requestSet->setRequests(array(null, 0));
        $this->assertFalse($this->requestSet->hasRequests());
    }

    public function test_getTokenAuth_ShouldReturnFalse_IfNoTokenIsSetAndNoRequestParam()
    {
        $this->assertFalse($this->requestSet->getTokenAuth());
    }

    public function test_getTokenAuth_setTokenAuth_shouldOverwriteTheToken()
    {
        $this->requestSet->setTokenAuth('MKyKTokenTestIn');

        $this->assertEquals('MKyKTokenTestIn', $this->requestSet->getTokenAuth());
    }

    public function test_getTokenAuth_setTokenAuth_shouldBePossibleToClearASetToken()
    {
        $this->requestSet->setTokenAuth('MKyKTokenTestIn');
        $this->assertNotEmpty($this->requestSet->getTokenAuth());

        $this->requestSet->setTokenAuth(null);
        $this->assertFalse($this->requestSet->getTokenAuth()); // does now fallback to get param
    }

    public function test_getTokenAuth_ShouldFallbackToRequestParam_IfNoTokenSet()
    {
        $_GET['token_auth'] = 'MyTokenAuthTest';

        $this->assertSame('MyTokenAuthTest', $this->requestSet->getTokenAuth());

        unset($_GET['token_auth']);
    }

    public function test_getEnvironment_shouldReturnCurrentServerVar()
    {
        $this->assertEquals(array(
            'server' => $_SERVER,
            'cookie' => $_COOKIE
        ), $this->requestSet->getEnvironment());
    }

    public function test_intertnalFakeEnvironment_shouldActuallyReturnAValue()
    {
        $myEnv = $this->getFakeEnvironment();
        $this->assertInternalType('array', $myEnv);
        $this->assertNotEmpty($myEnv);
    }

    public function test_setEnvironment_shouldOverwriteAnEnvironment()
    {
        $this->requestSet->setEnvironment($this->getFakeEnvironment());

        $this->assertEquals($this->getFakeEnvironment(), $this->requestSet->getEnvironment());
    }

    public function test_restoreEnvironment_shouldRestoreAPreviouslySetEnvironment()
    {
        $serverBackup = $_SERVER;
        $cookieBackup = $_COOKIE;
        
        $this->requestSet->setEnvironment($this->getFakeEnvironment());
        $this->requestSet->restoreEnvironment();

        $this->assertEquals(array('mytest' => 'test'), $_SERVER);
        $this->assertEquals(array('mytest2' => 'test2'), $_COOKIE);

        $_SERVER = $serverBackup;
        $_COOKIE = $cookieBackup;
    }

    public function test_rememberEnvironment_shouldSaveCurrentEnvironment()
    {
        $expected = array('server' => $_SERVER, 'cookie' => $_COOKIE);

        $this->requestSet->rememberEnvironment();

        $this->assertEquals($expected, $this->requestSet->getEnvironment());

        // should not change anything
        $this->requestSet->restoreEnvironment();
        $this->assertEquals($expected['server'], $_SERVER);
        $this->assertEquals($expected['cookie'], $_COOKIE);
    }

    public function test_getState_shouldReturnCurrentStateOfRequestSet()
    {
        $this->requestSet->setRequests($this->buildRequests(2));
        $this->requestSet->setTokenAuth('mytoken');

        $state = $this->requestSet->getState();

        $expectedKeys = array('requests', 'env', 'tokenAuth', 'time');
        $this->assertEquals($expectedKeys, array_keys($state));

        $expectedRequests = array(
            array('idsite' => 1),
            array('idsite' => 2)
        );

        $this->assertEquals($expectedRequests, $state['requests']);
        $this->assertEquals('mytoken', $state['tokenAuth']);
        $this->assertTrue(is_numeric($state['time']));
        $this->assertEquals(array('server' => $_SERVER, 'cookie' => $_COOKIE), $state['env']);
    }

    public function test_getState_shouldRememberAnyAddedParamsFromRequestConstructor()
    {
        $_SERVER['HTTP_REFERER'] = 'test';

        $requests = $this->buildRequests(1);

        $this->requestSet->setRequests($requests);
        $this->requestSet->setTokenAuth('mytoken');

        $state = $this->requestSet->getState();

        unset($_SERVER['HTTP_REFERER']);

        $expectedRequests = array(
            array('idsite' => 1)
        );

        $this->assertEquals($expectedRequests, $state['requests']);

        // the actual params include an added urlref param which should NOT be in the state. otherwise we cannot detect empty requests etc
        $this->assertEquals(array('idsite' => 1, 'url' => 'test'), $requests[0]->getParams());
    }

    public function test_restoreState_shouldRestoreRequestSet()
    {
        $serverBackup = $_SERVER;

        $state = array(
            'requests' => array(array('idsite' => 1), array('idsite' => 2), array('idsite' => 3)),
            'time' => $this->time,
            'tokenAuth' => 'tokenAuthRestored',
            'env' => $this->getFakeEnvironment()
        );

        $this->requestSet->restoreState($state);

        $this->assertEquals($this->getFakeEnvironment(), $this->requestSet->getEnvironment());
        $this->assertEquals('tokenAuthRestored', $this->requestSet->getTokenAuth());

        $expectedRequests = array(
            new Request(array('idsite' => 1), 'tokenAuthRestored'),
            new Request(array('idsite' => 2), 'tokenAuthRestored'),
            new Request(array('idsite' => 3), 'tokenAuthRestored'),
        );
        $expectedRequests[0]->setCurrentTimestamp($this->time);
        $expectedRequests[1]->setCurrentTimestamp($this->time);
        $expectedRequests[2]->setCurrentTimestamp($this->time);

        $requests = $this->requestSet->getRequests();
        $this->assertEquals($expectedRequests, $requests);

        // verify again just to be sure (only first one)
        $this->assertEquals('tokenAuthRestored', $requests[0]->getTokenAuth());
        $this->assertEquals($this->time, $requests[0]->getCurrentTimestamp());

        // should not restoreEnvironment, only set the environment
        $this->assertSame($serverBackup, $_SERVER);
    }

    public function test_restoreState_ifRequestWasEmpty_ShouldBeStillEmptyWhenRestored()
    {
        $_SERVER['HTTP_REFERER'] = 'test';

        $this->requestSet->setRequests(array(new Request(array())));
        $state = $this->requestSet->getState();

        $requestSet = $this->createRequestSet();
        $requestSet->restoreState($state);

        unset($_SERVER['HTTP_REFERER']);

        $requests = $requestSet->getRequests();
        $this->assertTrue($requests[0]->isEmptyRequest());
    }

    public function test_restoreState_shouldResetTheStoredEnvironmentBeforeRestoringRequests()
    {
        $this->requestSet->setRequests(array(new Request(array())));
        $state = $this->requestSet->getState();
        $state['env']['server']['HTTP_REFERER'] = 'mytesturl';

        $requestSet = $this->createRequestSet();
        $requestSet->restoreState($state);

        $requests = $requestSet->getRequests();
        $this->assertTrue($requests[0]->isEmptyRequest());
        $this->assertEquals(array('url' => 'mytesturl'), $requests[0]->getParams());
        $this->assertTrue(empty($_SERVER['HTTP_REFERER']));
    }

    public function test_getRedirectUrl_ShouldReturnEmptyString_IfNoUrlSet()
    {
        $this->assertEquals('', $this->requestSet->getRedirectUrl());
    }

    public function test_getRedirectUrl_ShouldReturnTrue_IfAUrlSetIsSetViaGET()
    {
        $_GET['redirecturl'] = 'whatsoever';
        $this->assertEquals('whatsoever', $this->requestSet->getRedirectUrl());
        unset($_GET['redirecturl']);
    }

    public function test_getRedirectUrl_ShouldReturnTrue_IfAUrlSetIsSetViaPOST()
    {
        $_POST['redirecturl'] = 'whatsoeverPOST';
        $this->assertEquals('whatsoeverPOST', $this->requestSet->getRedirectUrl());
        unset($_POST['redirecturl']);
    }

    public function test_hasRedirectUrl_ShouldReturnFalse_IfNoUrlSet()
    {
        $this->assertFalse($this->requestSet->hasRedirectUrl());
    }

    public function test_hasRedirectUrl_ShouldReturnTrue_IfAUrlSetIsSetViaGET()
    {
        $_GET['redirecturl'] = 'whatsoever';
        $this->assertTrue($this->requestSet->hasRedirectUrl());
        unset($_GET['redirecturl']);
    }

    public function test_hasRedirectUrl_ShouldReturnTrue_IfAUrlSetIsSetViaPOST()
    {
        $_POST['redirecturl'] = 'whatsoever';
        $this->assertTrue($this->requestSet->hasRedirectUrl());
        unset($_POST['redirecturl']);
    }

    /**
     * @param int $numRequests
     * @return Request[]
     */
    private function buildRequests($numRequests)
    {
        $requests = array();
        for ($index = 1; $index <= $numRequests; $index++) {
            $requests[] = $this->buildRequest($index);
        }
        return $requests;
    }

    private function buildRequest($idsite)
    {
        $request = new Request(array('idsite' => ('' . $idsite)));
        $request->setCurrentTimestamp($this->time);

        return $request;
    }

    private function getFakeEnvironment()
    {
        return array('server' => array('mytest' => 'test'), 'cookie' => array('mytest2' => 'test2'));
    }
}

class TestRequestSet extends RequestSet
{
    public function getRedirectUrl()
    {
        return parent::getRedirectUrl();
    }

    public function hasRedirectUrl()
    {
        return parent::hasRedirectUrl();
    }

    public function getAllSiteIdsWithinRequest()
    {
        return parent::getAllSiteIdsWithinRequest();
    }

    public function getEnvironment()
    {
        return parent::getEnvironment();
    }
}