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

RequestTest.php « Tracker « Unit « PHPUnit « tests - github.com/matomo-org/matomo.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 62b285247dbf143ecff070eaf6caf8d750e72f64 (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
<?php
/**
 * Matomo - free/libre analytics platform
 *
 * @link https://matomo.org
 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
 */

namespace Piwik\Tests\Unit\Tracker;

use Piwik\Cookie;
use Piwik\Exception\InvalidRequestParameterException;
use Matomo\Network\IPUtils;
use Piwik\Piwik;
use Piwik\Plugins\CustomVariables\CustomVariables;
use Piwik\Tests\Framework\TestCase\UnitTestCase;
use Piwik\Tracker\Request;
use Piwik\Tracker\TrackerConfig;

/**
 * @group RequestSetTest
 * @group RequestSet
 * @group Tracker
 */
class RequestTest extends UnitTestCase
{
    /**
     * @var TestRequest
     */
    private $request;
    private $time;

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

        $this->time = 1416795617;
        $this->request = $this->buildRequest(array('idsite' => '1'));
    }

    public function test_getCurrentTimestamp_ShouldReturnTheSetTimestamp_IfNoCustomValueGiven()
    {
        $this->assertSame($this->time, $this->request->getCurrentTimestamp());
    }

    public function test_getCurrentTimestamp_ShouldReturnTheCurrentTimestamp_IfTimestampIsInvalid()
    {
        $request = $this->buildRequest(array('cdt' => '' . 5));
        $request->setIsAuthenticated();
        $this->assertSame($this->time, $request->getCurrentTimestamp());
    }

    public function test_isEmptyRequest_ShouldReturnTrue_InCaseNoParamsSet()
    {
        $request = $this->buildRequest(array());
        $this->assertTrue($request->isEmptyRequest());
    }

    public function test_isEmptyRequest_ShouldReturnTrue_InCaseNullIsSet()
    {
        $request = $this->buildRequest(null);
        $this->assertTrue($request->isEmptyRequest());
    }

    public function test_isEmptyRequest_ShouldRecognizeEmptyRequest_EvenIfConstructorAddsAParam()
    {
        $_SERVER['HTTP_REFERER'] = 'http://www.example.com';

        $request = $this->buildRequest(array());
        $this->assertCount(1, $request->getParams());

        $this->assertTrue($request->isEmptyRequest());

        unset($_SERVER['HTTP_REFERER']);
    }

    public function test_isEmptyRequest_ShouldReturnFalse_InCaseAtLEastOneParamIssSet()
    {
        $request = $this->buildRequest(array('idsite' => 1));
        $this->assertFalse($request->isEmptyRequest());
    }

    public function test_getTokenAuth_shouldReturnDefaultValue_IfNoneSet()
    {
        $request = $this->buildRequest(array('idsite' => 1));
        $this->assertFalse($request->getTokenAuth());
    }

    public function test_getTokenAuth_shouldReturnSetTokenAuth()
    {
        $request = $this->buildRequestWithToken(array('idsite' => 1), 'myToken');
        $this->assertEquals('myToken', $request->getTokenAuth());
    }

    public function test_getForcedUserId_shouldReturnFalseByDefault()
    {
        $this->assertFalse($this->request->getForcedUserId());
    }

    public function test_getForcedUserId_shouldReturnCustomUserId_IfSet()
    {
        $request = $this->buildRequest(array('uid' => 'mytest'));
        $this->assertEquals('mytest', $request->getForcedUserId());
    }

    public function test_getForcedUserId_shouldReturnFalse_IfCustomUserIdIsEmpty()
    {
        $request = $this->buildRequest(array('uid' => ''));
        $this->assertFalse($request->getForcedUserId());
    }

    public function test_getGoalRevenue_ShouldReturnDefaultValue_IfNothingSet()
    {
        $this->assertFalse($this->request->getGoalRevenue(false));
    }

    public function test_getGoalRevenue_ShouldReturnParam_IfSet()
    {
        $request = $this->buildRequest(array('revenue' => '5.51'));
        $this->assertSame(5.51, $request->getGoalRevenue(false));
    }

    public function test_getUserIdHashed_shouldReturnSetTokenAuth()
    {
        $hash = $this->request->getUserIdHashed(1);

        $this->assertEquals('356a192b7913b04c', $hash);
        $this->assertSame(16, strlen($hash));
        $this->assertTrue(ctype_alnum($hash));

        $this->assertEquals('da4b9237bacccdf1', $this->request->getUserIdHashed(2));
    }

    public function test_getLocalTime_shouldFallbackToCurrentDate_IfNoParamIsSet()
    {
        $this->assertEquals('02:20:17', $this->request->getLocalTime());
    }

    public function test_getLocalTime_shouldReturnAtLEastOneEvenIfLowerValueIsSet()
    {
        $request = $this->buildRequest(array('h' => 15, 'm' => 3, 's' => 4));
        $this->assertEquals('15:03:04', $request->getLocalTime());
    }

    public function test_getLocalTime_shouldFallbackToPartsOfCurrentDate()
    {
        $request = $this->buildRequest(array('h' => 5));
        $this->assertEquals('05:20:17', $request->getLocalTime());
    }

    public function test_getParam_shouldThrowException_IfTryingToAccessInvalidParam()
    {
        $this->expectException(\Exception::class);
        $this->expectExceptionMessage('Requested parameter myCustomFaKeParaM is not a known Tracking API Parameter');

        $this->request->getParam('myCustomFaKeParaM');
    }

    public function test_getParam_aString()
    {
        $request = $this->buildRequest(array('url' => 'test'));
        $this->assertEquals('test', $request->getParam('url'));
    }

    public function test_getParam_aInt()
    {
        $request = $this->buildRequest(array('new_visit' => '12'));
        $this->assertSame(12, $request->getParam('new_visit'));
    }

    public function test_getPlugins_shouldReturnZeroForAllIfNothingGiven()
    {
        $expected = array_fill(0, 8, 0);

        $this->assertEquals($expected, $this->request->getPlugins());
    }

    public function test_getPlugins_shouldReturnAllOneIfAllGiven()
    {
        $plugins = array('fla', 'java', 'qt', 'realp', 'pdf', 'wma', 'ag', 'cookie');
        $request = $this->buildRequest(array_fill_keys($plugins, '1'));

        $this->assertEquals(array_fill(0, 8, 1), $request->getPlugins());
    }

    public function test_getPlugins_shouldDetectSome()
    {
        $plugins = array('fla' => 1, 'java', 'qt' => '0', 'realp' => 0, 'ag' => 1, 'cookie');
        $request = $this->buildRequest($plugins);

        $expected = array(1, 0, 0, 0, 0, 0, 1, 0);
        $this->assertEquals($expected, $request->getPlugins());
    }

    public function test_truncateCustomVariable_shouldNotTruncateAnything_IfValueIsShortEnough()
    {
        $len = CustomVariables::getMaxLengthCustomVariables();
        $input = str_pad('test', $len - 2, 't');

        $result = Request::truncateCustomVariable($input);

        $this->assertSame($result, $input);
    }

    public function test_truncateCustomVariable_shouldActuallyTruncateTheValue()
    {
        $len = CustomVariables::getMaxLengthCustomVariables();
        $input = str_pad('test', $len + 2, 't');

        $this->assertGreaterThan(100, $len);

        $truncated = Request::truncateCustomVariable($input);

        $this->assertEquals(str_pad('test', $len, 't'), $truncated);
    }

    public function test_getUserAgent_ShouldReturnEmptyString_IfNoneIsSet()
    {
        $this->assertEquals('', $this->request->getUserAgent());
    }

    public function test_getUserAgent_ShouldDefaultToServerUa_IfPossibleAndNoneIsSet()
    {
        $_SERVER['HTTP_USER_AGENT'] = 'MyUserAgent';
        $this->assertSame('MyUserAgent', $this->request->getUserAgent());
        unset($_SERVER['HTTP_USER_AGENT']);
    }

    public function test_getUserAgent_ShouldReturnTheUaFromParams_IfOneIsSet()
    {
        $request = $this->buildRequest(array('idsite' => '14', 'ua' => 'My Custom UA'));
        $this->assertSame('My Custom UA', $request->getUserAgent());
    }

    public function test_getBrowserLanguage_ShouldReturnACustomSetLangParam_IfOneIsSet()
    {
        $request = $this->buildRequest(array('lang' => 'CusToMLang'));
        $this->assertSame('CusToMLang', $request->getBrowserLanguage());
    }

    public function test_getBrowserLanguage_ShouldReturnADefaultLanguageInCaseNoneIsSet()
    {
        $envLanguage = getenv('LANG');
        putenv('LANG=en');

        $lang = $this->request->getBrowserLanguage();
        $this->assertNotEmpty($lang);
        $this->assertTrue(2 <= strlen($lang) && strlen($lang) <= 10);

        if ($envLanguage !== false) {
            putenv('LANG=' . $envLanguage);
        }
    }

    public function test_makeThirdPartyCookie_ShouldReturnAnInstanceOfCookie()
    {
        $cookie = $this->request->makeThirdPartyCookieUID();

        $this->assertTrue($cookie instanceof Cookie);
    }

    public function test_makeThirdPartyCookie_ShouldPreconfigureTheCookieInstance()
    {
        $cookie = $this->request->makeThirdPartyCookieUID();

        $this->assertCookieContains('COOKIE _pk_uid', $cookie);
        $this->assertCookieContains('expire: 1450750817', $cookie);
        $this->assertCookieContains('path: ,', $cookie);
    }

    private function assertCookieContains($needle, Cookie $cookie)
    {
        self::assertStringContainsString($needle, $cookie . '');
    }

    public function test_getLocalTime()
    {
        $request = $this->buildRequest(array('h' => '12', 'm' => '34', 's' => '3'));
        $this->assertSame('12:34:03', $request->getLocalTime());


        $request = $this->buildRequest(array('h' => '23', 'm' => '59', 's' => '59'));
        $this->assertSame('23:59:59', $request->getLocalTime());
    }

    public function test_getLocalTime_shouldReturnValidTime_whenTimeWasInvalid()
    {
        $request = $this->buildRequest(array('h' => '26', 'm' => '60', 's' => '333'));
        $this->assertSame('00:00:00', $request->getLocalTime());

        $request = $this->buildRequest(array('h' => '-26', 'm' => '-60', 's' => '-333'));
        $this->assertSame('00:00:00', $request->getLocalTime());
    }

    public function test_getIpString_ShouldDefaultToServerAddress()
    {
        $this->assertEquals($_SERVER['REMOTE_ADDR'], $this->request->getIpString());
    }

    public function test_getIpString_ShouldDefaultToServerAddress_IfCustomIpIsSetButNotAuthenticated()
    {
        $this->expectException(InvalidRequestParameterException::class);
        $this->expectExceptionMessage('requires valid token_auth');
        $request = $this->buildRequest(array('cip' => '192.192.192.192'));
        $this->assertEquals($_SERVER['REMOTE_ADDR'], $request->getIpString());
    }

    public function test_getIpString_ShouldReturnCustomIp_IfAuthenticated()
    {
        $request = $this->buildRequest(array('cip' => '192.192.192.192'));
        $request->setIsAuthenticated();
        $this->assertEquals('192.192.192.192', $request->getIpString());
    }

    public function test_getIp()
    {
        $ip = $_SERVER['REMOTE_ADDR'];
        $this->assertEquals(IPUtils::stringToBinaryIP($ip), $this->request->getIp());
    }

    public function test_getCookieName_ShouldReturnConfigValue()
    {
        $this->assertEquals('_pk_uid', $this->request->getCookieName());
    }

    public function test_getCookieExpire_ShouldReturnConfigValue()
    {
        $this->assertEquals($this->time + (60 * 60 * 24 * 393), $this->request->getCookieExpire());
    }

    public function test_getCookiePath_ShouldBeEmptyByDefault()
    {
        $this->assertEquals('', $this->request->getCookiePath());
    }

    public function test_getCookiePath_ShouldReturnConfigValue()
    {
        $oldPath = TrackerConfig::getConfigValue('cookie_path');
        TrackerConfig::setConfigValue('cookie_path', 'test');

        $this->assertEquals('test', $this->request->getCookiePath());

        TrackerConfig::setConfigValue('cookie_path', $oldPath);
    }

    private function buildRequest($params)
    {
        $request = new TestRequest($params);
        $request->setCurrentTimestamp($this->time);

        return $request;
    }

    private function buildRequestWithToken($params, $token)
    {
        return new TestRequest($params, $token);
    }
}

class TestRequest extends Request
{
    public function getCookieName()
    {
        return parent::getCookieName();
    }

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

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

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

    public function setIsAuthenticated()
    {
        $this->isAuthenticated = true;
    }
}