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

CustomDimensionsRequestProcessorTest.php « Tracker « Integration « tests « CustomDimensions « plugins - github.com/matomo-org/matomo.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 0da15e9f1674bbe5ca99333ac8adfc6f0bb6d546 (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
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
<?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\Plugins\CustomDimensions\tests\Integration\Dao;

use Piwik\Plugins\CustomDimensions\CustomDimensions;
use Piwik\Plugins\CustomDimensions\Dao\Configuration;
use Piwik\Plugins\CustomDimensions\Dao\LogTable;
use Piwik\Plugins\CustomDimensions\Tracker\CustomDimensionsRequestProcessor as Processor;
use Piwik\Plugins\CustomDimensions\Tracker\CustomDimensionsRequestProcessor;
use Piwik\Tests\Framework\Fixture;
use Piwik\Tests\Framework\TestCase\IntegrationTestCase;
use Piwik\Tracker\ActionPageview;
use Piwik\Tracker\Cache;
use Piwik\Tracker\Request;
use Piwik\Tracker\Visit\VisitProperties;

/**
 * @group CustomDimensions
 * @group CustomDimensionsRequestProcessorTest
 * @group CustomDimensionsRequestProcessor
 * @group Dao
 * @group Plugins
 */
class CustomDimensionsRequestProcessorTest extends IntegrationTestCase
{
    /**
     * @var Processor
     */
    private $processor;

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

        if (!Fixture::siteCreated(1)) {
            Fixture::createWebsite('2012-01-01 00:00:00');
        }
        if (!Fixture::siteCreated(2)) {
            Fixture::createWebsite('2012-01-01 00:00:00');
        }

        Cache::clearCacheGeneral();
        Cache::deleteCacheWebsiteAttributes($idSite = 1);
        Cache::deleteCacheWebsiteAttributes($idSite = 2);

        $this->processor = new Processor();
    }

    public function tearDown(): void
    {
        parent::tearDown();
    }

    public function test_buildCustomDimensionTrackingApiName()
    {
        $this->assertNull(Processor::buildCustomDimensionTrackingApiName(''));
        $this->assertNull(Processor::buildCustomDimensionTrackingApiName('0'));
        $this->assertNull(Processor::buildCustomDimensionTrackingApiName(null));
        $this->assertNull(Processor::buildCustomDimensionTrackingApiName(array()));
        $this->assertNull(Processor::buildCustomDimensionTrackingApiName(array('idcustomdimension' => '')));

        $this->assertSame('dimension1', Processor::buildCustomDimensionTrackingApiName(1));
        $this->assertSame('dimension3', Processor::buildCustomDimensionTrackingApiName('3'));
        $this->assertSame('dimension4', Processor::buildCustomDimensionTrackingApiName(array('idcustomdimension' => '4')));
    }

    public function test_getCachedCustomDimensionIndexes()
    {
        $logTable = new LogTable(CustomDimensions::SCOPE_ACTION);
        $logTable->removeCustomDimension(1);

        $indexes = Processor::getCachedCustomDimensionIndexes(CustomDimensions::SCOPE_VISIT);
        $this->assertSame(range(1,5), $indexes);

        $indexes = Processor::getCachedCustomDimensionIndexes(CustomDimensions::SCOPE_ACTION);
        $this->assertSame(range(2,5), $indexes);
    }

    public function test_getCachedCustomDimensions_shouldReturnDimensionsForSiteButOnlyActiveOnes()
    {
        $this->configureSomeDimensions();

        $request = new Request(array('idsite' => 1));
        $dimensions = Processor::getCachedCustomDimensions($request);

        $this->assertCount(5, $dimensions);

        foreach ($dimensions as $dimension) {
            $this->assertSame('1', $dimension['idsite']);
            $this->assertTrue($dimension['active']);
            $this->assertStringStartsWith('MyName', $dimension['name']);
        }

        $request = new Request(array('idsite' => 2));
        $dimensions = Processor::getCachedCustomDimensions($request);
        $this->assertCount(1, $dimensions);
    }

    public function test_hasActionCustomDimensionConfiguredInSite_whenHasActionDimensionConfigured()
    {
        $this->configureSomeDimensions();

        $request = new Request(array('idsite' => 1));
        $this->assertTrue(Processor::hasActionCustomDimensionConfiguredInSite($request));
    }

    public function test_hasActionCustomDimensionConfiguredInSite_whenHasOnlyVisitDimensions()
    {
        $request = new Request(array('idsite' => 2));
        $this->assertFalse(Processor::hasActionCustomDimensionConfiguredInSite($request));
    }

    public function test_hasActionCustomDimensionConfiguredInSite_WhenNoDimensionsAreConfgigured()
    {
        $request = new Request(array('idsite' => 1));
        $this->assertFalse(Processor::hasActionCustomDimensionConfiguredInSite($request));
    }

    public function test_onExistingVisit_ShouldOnlyAddColumnsOfCustomDimensionsInScopeVisit()
    {
        $this->configureSomeDimensions();

        $valuesToUpdate = array();
        $visitProperties = new VisitProperties();

        $request = new Request(array(
            'idsite' => 1,
            'something' => 5,
            Processor::buildCustomDimensionTrackingApiName(2) => '2 value',
            Processor::buildCustomDimensionTrackingApiName(6) => '6 value',
            'dimension_' => 'not an actual dimension',
            'dimension99' => 'not an actual dimension2',
            Processor::buildCustomDimensionTrackingApiName(3) => '3 value', // should be ignored as it is an action dimension
            Processor::buildCustomDimensionTrackingApiName(9) => '9 value', // should be ignored as 9 dimensions are not installed
        ));

        $this->processor->onExistingVisit($valuesToUpdate, $visitProperties, $request);

        $expected = array(
            'custom_dimension_2' => '2 value',
            'custom_dimension_4' => '6 value',
        );
        $this->assertSame($expected, $valuesToUpdate);
    }

    public function test_onNewVisit_afterRequestProcessed_ShouldSaveManuallySetDimensionValues_ForActivatedDimensions()
    {
        $this->configureSomeDimensions();

        $visitProperties = new VisitProperties();
        $request = new Request(array(
            'idsite' => 1,
            'dimension1' => 'value1',
            'dimension3' => 'value3',
            'dimension4' => 'value4',
            'dimension5' => 'value5',
            'dimension6' => 'value6',
            'dimension7' => 'value7', // this one should be ignored as it is not configured to be used
        ));

        $action = new ActionPageview($request);
        $request->setMetadata('Actions', 'action', $action);

        // should only add visit scope dimensions
        $this->processor->onNewVisit($visitProperties, $request);

        $expected1 = array(
            'custom_dimension_1' => 'value1',
            // there should be no value for dimension2 as nothing was set
            // there should be no value for dimension 3, 4 and 5 as they are in scope action
            'custom_dimension_4' => 'value6'
        );
        $this->assertSame($expected1, $visitProperties->getProperties());
        $this->assertSame(array(), $action->getCustomFields());

        $this->processor->afterRequestProcessed($visitProperties, $request);

        $expected2 = array(
            // the value for dimension4 should be ignored as it is not active
            'custom_dimension_1' => 'value3', // dimension 3 maps to index 1
            'custom_dimension_3' => 'value5', // dimension 4 maps to index 3 in db
        );
        $this->assertSame($expected2, $action->getCustomFields());

        // should not have changed visit properties
        $this->assertSame($expected1, $visitProperties->getProperties());
    }

    public function test_afterRequestProcessed_ShouldSaveManuallySetDimensionValues_ForActivatedDimensions()
    {
        $this->configureSomeDimensions();

        $visitProperties = new VisitProperties();
        $request = new Request(array(
            'idsite' => 1,
            'dimension1' => 'value1',
            'dimension3' => 'value3',
            'dimension4' => 'value4',
            'dimension5' => 'value5',
            'dimension6' => 'value6',
            'dimension7' => 'value7', // this one should be ignored as it is not configured to be used
        ));

        $action = new ActionPageview($request);
        $request->setMetadata('Actions', 'action', $action);

        $this->processor->onNewVisit($visitProperties, $request);

        $expected1 = array(
            'custom_dimension_1' => 'value1',
            // there should be no value for dimension2 as nothing was set
            // there should be no value for dimension 3, 4 and 5 as they are in scope action
            'custom_dimension_4' => 'value6'
        );
        $this->assertSame($expected1, $visitProperties->getProperties());
        $this->assertSame(array(), $action->getCustomFields()); // should not set any action dimensions

        $this->processor->afterRequestProcessed($visitProperties, $request);

        $expected2 = array(
            // the value for dimension4 should be ignored as it is not active
            'custom_dimension_1' => 'value3', // dimension 3 maps to index 1
            'custom_dimension_3' => 'value5', // dimension 4 maps to index 3 in db
        );
        $this->assertSame($expected2, $action->getCustomFields());
        $this->assertSame($expected1, $visitProperties->getProperties()); // should not have added visit dimensions
    }

    public function test_onNewVisit_afterRequestProcessed_NoDimensionsConfigured_ShouldSaveNothing()
    {
        $visitProperties = new VisitProperties();
        $request = new Request(array(
            'idsite' => 1,
            'dimension1' => 'value1',
            'dimension3' => 'value3',
            'dimension4' => 'value4',
            'dimension5' => 'value5',
            'dimension6' => 'value6',
        ));

        $this->processor->onNewVisit($visitProperties, $request);
        $this->processor->afterRequestProcessed($visitProperties, $request);

        $this->assertSame(array(), $visitProperties->getProperties());
    }

    public function test_onNewVisit_afterRequestProcessed_NoActionSet_ShouldNotFailIfThereIsNoActionSet()
    {
        $this->configureSomeDimensions();

        $visitProperties = new VisitProperties();
        $request = new Request(array(
            'idsite' => 1,
            'dimension4' => 'value4',
            'dimension5' => 'value5',
            'dimension6' => 'value6',
        ));

        $this->processor->onNewVisit($visitProperties, $request);

        $expected = array(
            'custom_dimension_4' => 'value6'
        );
        $this->assertSame($expected, $visitProperties->getProperties());

        $this->processor->afterRequestProcessed($visitProperties, $request);

        $this->assertSame($expected, $visitProperties->getProperties());
    }

    public function test_afterRequestProcessed_NoActionSet_ShouldNotSaveAnEmptyValue()
    {
        $this->configureSomeDimensions();

        $visitProperties = new VisitProperties();
        $request = new Request(array(
            'idsite' => 1,
            'dimension4' => '',
            'dimension5' => '',
            'dimension6' => '',
        ));

        $this->processor->onNewVisit($visitProperties, $request);
        $this->assertSame(array('custom_dimension_4' => ''), $visitProperties->getProperties());

        $this->processor->afterRequestProcessed($visitProperties, $request);

        $this->assertSame(array('custom_dimension_4' => ''), $visitProperties->getProperties());
    }

    public function test_afterRequestProcessed_NoActionSet_ShouldBeAbleToExtractAValue()
    {
        $configuration = new Configuration();
        $extractions = array(
            array('dimension' => 'url', 'pattern' => 'www(.+).com')
        );
        $configuration->configureNewDimension($idSite = 1, 'MyName1', CustomDimensions::SCOPE_VISIT, 1, true, $extractions, $caseSensitive = true);

        $extractions = array(
            array('dimension' => 'url', 'pattern' => 'www.piwik(.+).com'), // first one doesn't match
            array('dimension' => 'url', 'pattern' => 'www.ex(.+).com'), // but second matches
            array('dimension' => 'url', 'pattern' => 'www(.+).com'), // third one matches too but should use the one that matches first
        );
        $configuration->configureNewDimension($idSite = 1, 'MyName2', CustomDimensions::SCOPE_VISIT, 2, true, $extractions, $caseSensitive = true);

        $extractions = array(
            array('dimension' => 'urlparam', 'pattern' => 'id'), // first one doesn't match
        );
        $configuration->configureNewDimension($idSite = 1, 'MyName3', CustomDimensions::SCOPE_VISIT, 3, true, $extractions, $caseSensitive = true);
        $configuration->configureNewDimension($idSite = 1, 'MyName4', CustomDimensions::SCOPE_ACTION, 1, true, $extractions, $caseSensitive = true);

        $request = new Request(array('idsite' => 1, 'url' => 'http://www.example.com/test?id=11&module=test'));
        $action = new ActionPageview($request);
        $request->setMetadata('Actions', 'action', $action);

        $visitProperties = new VisitProperties();

        $this->processor->onNewVisit($visitProperties, $request);

        $this->assertSame(array(
            'custom_dimension_1' => '.example',
            'custom_dimension_2' => 'ample',
            'custom_dimension_3' => '11',
        ), $visitProperties->getProperties());

        $this->processor->afterRequestProcessed($visitProperties, $request);

        $this->assertSame(array('custom_dimension_1' => '11'), $action->getCustomFields());
    }

    public function test_afterRequestProcessed_NoActionSet_ShouldBeAbleToHandleCaseSensitive()
    {
        $configuration = new Configuration();
        $extractions = array(
            array('dimension' => 'url', 'pattern' => 'wwW(.+).com')
        );
        $configuration->configureNewDimension($idSite = 1, 'MyName1', CustomDimensions::SCOPE_ACTION, 1, true, $extractions, $caseSensitive = true);
        $configuration->configureNewDimension($idSite = 1, 'MyName2', CustomDimensions::SCOPE_ACTION, 2, true, $extractions, $caseSensitive = false);

        $request = new Request(array('idsite' => 1, 'url' => 'http://www.exAmple.com/test?id=11&module=test'));
        $action = new ActionPageview($request);
        $request->setMetadata('Actions', 'action', $action);

        $this->processor->afterRequestProcessed(new VisitProperties(), $request);

        $this->assertSame(array(
            'custom_dimension_2' => '.exAmple',
        ), $action->getCustomFields());
    }

    public function test_valueWithWhitespace_isTrimmed()
    {
        $this->configureSomeDimensions();

        $visitProperties = new VisitProperties();
        $request = new Request(array(
            'idsite' => 1,
            'dimension1' => 'value1 ',
        ));

        $action = new ActionPageview($request);
        $request->setMetadata('Actions', 'action', $action);

        // should only add visit scope dimensions
        $this->processor->onNewVisit($visitProperties, $request);

        $expected1 = array(
            'custom_dimension_1' => 'value1',
        );
        $this->assertSame($expected1, $visitProperties->getProperties());
        $this->assertSame(array(), $action->getCustomFields());
    }

    public function test_veryLongValue_isTruncated()
    {
        $this->configureSomeDimensions();

        $veryLongStr =  '12345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890';
        $trimmedStr =   '1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890';

        $visitProperties = new VisitProperties();
        $request = new Request(array(
            'idsite' => 1,
            'dimension1' => $veryLongStr,
        ));

        $action = new ActionPageview($request);
        $request->setMetadata('Actions', 'action', $action);

        // should only add visit scope dimensions
        $this->processor->onNewVisit($visitProperties, $request);

        $expected1 = array(
            'custom_dimension_1' => $trimmedStr,
        );
        $this->assertSame($expected1, $visitProperties->getProperties());
        $this->assertSame(array(), $action->getCustomFields());
    }

    public function test_valueWithWhitespace_isTrimmed_extractedFromUrl()
    {
        $configuration = new Configuration();
        $extractions = array(
            array('dimension' => 'urlparam', 'pattern' => 'id'),
        );
        $configuration->configureNewDimension($idSite = 1, 'Dimension1', CustomDimensions::SCOPE_VISIT, 1, true, $extractions, $caseSensitive = true);

        $extractions = array(
            array('dimension' => 'urlparam', 'pattern' => 'module'),
        );
        $configuration->configureNewDimension($idSite = 1, 'Dimension2', CustomDimensions::SCOPE_VISIT, 2, true, $extractions, $caseSensitive = true);

        $request = new Request(array('idsite' => 1, 'url' => 'http://www.example.com/test?id=11 &module=test%20'));
        $action = new ActionPageview($request);
        $request->setMetadata('Actions', 'action', $action);

        $visitProperties = new VisitProperties();

        $this->processor->onNewVisit($visitProperties, $request);

        $this->assertSame(array(
            'custom_dimension_1' => '11',
            'custom_dimension_2' => 'test'
        ), $visitProperties->getProperties());
    }

    public function test_veryLongValue_isTrimmed_extractedFromUrl()
    {
        $veryLongStr =  '12345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890';
        $trimmedStr =   '1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890';

        $configuration = new Configuration();
        $extractions = array(
            array('dimension' => 'urlparam', 'pattern' => 'module'),
        );
        $configuration->configureNewDimension($idSite = 1, 'Dimension1', CustomDimensions::SCOPE_VISIT, 1, true, $extractions, $caseSensitive = true);

        $request = new Request(array('idsite' => 1, 'url' => 'http://www.example.com/test?id=11&module=' . $veryLongStr));
        $action = new ActionPageview($request);
        $request->setMetadata('Actions', 'action', $action);

        $visitProperties = new VisitProperties();

        $this->processor->onNewVisit($visitProperties, $request);

        $this->assertSame(array(
            'custom_dimension_1' => $trimmedStr,
        ), $visitProperties->getProperties());
    }

    private function configureSomeDimensions()
    {
        $configuration = new Configuration();
        $configuration->configureNewDimension($idSite = 1, 'MyName1', CustomDimensions::SCOPE_VISIT, 1, true, $extractions = array(), $caseSensitive = true);
        $configuration->configureNewDimension($idSite = 1, 'MyName2', CustomDimensions::SCOPE_VISIT, 2, true, $extractions = array(), $caseSensitive = true);
        $configuration->configureNewDimension($idSite = 2, 'MyName1', CustomDimensions::SCOPE_VISIT, 1, true, $extractions = array(), $caseSensitive = true);
        $configuration->configureNewDimension($idSite = 1, 'MyName3', CustomDimensions::SCOPE_ACTION, 1, true, $extractions = array(), $caseSensitive = true);
        $configuration->configureNewDimension($idSite = 1, 'MyName4', CustomDimensions::SCOPE_ACTION, 2, $active = false, $extractions = array(), $caseSensitive = true);
        $configuration->configureNewDimension($idSite = 1, 'MyName5', CustomDimensions::SCOPE_ACTION, 3, $active = true, $extractions = array(), $caseSensitive = true);
        $configuration->configureNewDimension($idSite = 1, 'MyName6', CustomDimensions::SCOPE_VISIT, 4, $active = true, $extractions = array(), $caseSensitive = true);
    }

}