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

CustomDimensionsTest.php « Integration « tests « CustomDimensions « plugins - github.com/matomo-org/matomo.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: df738bf500fed1c697341c3a03a5a77a7a396869 (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
<?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;

use Piwik\Plugins\CustomDimensions\CustomDimensions;
use Piwik\Plugins\CustomDimensions\Dao\Configuration;
use Piwik\Plugins\CustomDimensions\Dao\LogTable;
use Piwik\Plugins\CustomDimensions\VisitorDetails;
use Piwik\Tests\Framework\Fixture;
use Piwik\Tests\Framework\TestCase\IntegrationTestCase;
use Piwik\Tracker\Cache;
use Piwik\Tracker\Request;

/**
 * @group CustomDimensions
 * @group CustomDimensionsTest
 * @group Plugins
 */
class CustomDimensionsTest extends IntegrationTestCase
{
    /**
     * @var CustomDimensions
     */
    private $plugin;

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

        foreach (array(1, 2) as $idSite) {
            if (!Fixture::siteCreated($idSite)) {
                Fixture::createWebsite('2012-01-01 00:00:00');
            }
        }

        $this->plugin = new CustomDimensions();
    }

    public function test_install_shouldCreate5IndexesPerScopeAndCreateConfigurationTable()
    {
        foreach (CustomDimensions::getScopes() as $scope) {
            $logTable = new LogTable($scope);
            $this->assertSame(range(1, 5), $logTable->getInstalledIndexes());
        }

        // should succeed as table configured
        $this->configureSomeDimensions();
    }

    public function test_install_multipleTimes_ShouldNotChangeAnythingAndNotFail()
    {
        $this->plugin->install();
        $this->plugin->install();
        $this->plugin->install();
        $this->plugin->install();

        foreach (CustomDimensions::getScopes() as $scope) {
            $logTable = new LogTable($scope);
            $this->assertSame(range(1, 5), $logTable->getInstalledIndexes());
        }

        // should succeed as table configured
        $this->configureSomeDimensions();
    }

    public function test_uninstall_shouldRemoveAllColumnsFromLogTablesAndUninstallConfigTable()
    {
        $this->plugin->uninstall();
        foreach (CustomDimensions::getScopes() as $scope) {
            $logTable = new LogTable($scope);
            $this->assertSame(array(), $logTable->getInstalledIndexes());
        }

        try {
            $this->configureSomeDimensions();
        } catch (\Exception $e) {
            // should fail as table was removed
        }

        $this->plugin->install();
    }

    public function test_addVisitFieldsToPersist()
    {
        $fields = array('existingField');

        $this->plugin->addVisitFieldsToPersist($fields);

        $expected = array(
            'existingField',
            'last_idlink_va',
            'custom_dimension_1',
            'custom_dimension_2',
            'custom_dimension_3',
            'custom_dimension_4',
            'custom_dimension_5'
        );
        $this->assertSame($expected, $fields);
    }

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

        $conversion = array();

        $request = new Request(array('idsite' => 1));
        $visit = array(
            'existingField' => 'any existing',
            'custom_dimension_' => 'value',
            'custom_dimension_1' => 'value 1',
            'custom_dimension_2' => 'value 2', // should be ignored as inactive
            'custom_dimension_3' => 'value 3', // should be ignored as does not exist
            'custom_dimension_4' => 'value 4',
        );
        $this->plugin->addConversionInformation($conversion, $visit, $request);

        $this->assertSame(array(
            'custom_dimension_1' => 'value 1',
            'custom_dimension_4' => 'value 4',
        ), $conversion);
    }

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

        $logTable = new LogTable(CustomDimensions::SCOPE_CONVERSION);
        $logTable->removeCustomDimension(1);

        $conversion = array();

        $request = new Request(array('idsite' => 1));
        $visit = array(
            'custom_dimension_1' => 'value 1',
            'custom_dimension_4' => 'value 4',
        );
        $this->plugin->addConversionInformation($conversion, $visit, $request);

        $this->assertSame(array(
            'custom_dimension_4' => 'value 4',
        ), $conversion);
    }

    public function test_getCachedInstalledIndexesForScope_shouldIgnoreAnIndexIfTheIndexIsMissingInConversionTable()
    {
        $indexes = $this->plugin->getCachedInstalledIndexesForScope(CustomDimensions::SCOPE_CONVERSION);
        $this->assertSame(range(2, 5), $indexes);

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

        $this->plugin->uninstall();
        $this->plugin->install();

        $indexes = $this->plugin->getCachedInstalledIndexesForScope(CustomDimensions::SCOPE_CONVERSION);
        $this->assertSame(range(1, 5), $indexes);
    }

    public function test_shouldCacheInstalledIndexes()
    {
        Cache::clearCacheGeneral();
        $cache = Cache::getCacheGeneral();

        $test = array(
            CustomDimensions::SCOPE_VISIT => range(1, 5),
            CustomDimensions::SCOPE_ACTION => range(1, 5),
            CustomDimensions::SCOPE_CONVERSION => range(2, 5),
        );

        foreach (CustomDimensions::getScopes() as $scope) {
            $key = 'custom_dimension_indexes_installed_' . $scope;
            $this->assertArrayHasKey($key, $cache);
            $this->assertSame(range(1, 5), $cache[$key]);
        }
    }

    public function test_shouldCacheDimensinsViaWebsiteAttributes_ButOnlyActiveOnes()
    {
        $this->configureSomeDimensions();
        $cache = Cache::getCacheWebsiteAttributes($idSite = 1);
        $this->assertCount(4, $cache['custom_dimensions']);

        foreach ($cache['custom_dimensions'] as $dimension) {
            $this->assertTrue($dimension['active']);
        }

        $cache = Cache::getCacheWebsiteAttributes($idSite = 2);
        $this->assertCount(1, $cache['custom_dimensions']);

        foreach ($cache['custom_dimensions'] as $dimension) {
            $this->assertTrue($dimension['active']);
        }
    }

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

        $visitor = array('idSite' => 1);
        $details = array(
            'custom_dimension_1' => 'my value 1',
            'custom_dimension_2' => 'my value 2', // should be ignored as not active
            'custom_dimension_3' => 'my value 3', // should be ignored as does not exist in visit scope
            'custom_dimension_4' => 'my value 4',
            'custom_dimension_5' => 'my value 5', // index is not used
            'custom_dimension_6' => 'my value 6', // index does not exist
        );

        $visitorDetails = new VisitorDetails();
        $visitorDetails->setDetails($details);
        $visitorDetails->extendVisitorDetails($visitor);

        $expected = array(
            'idSite' => 1,
            'dimension1' => 'my value 1',
            'dimension6' => 'my value 4'
        );
        $this->assertSame($expected, $visitor);
    }

    public function test_deleteCustomDimensionDefinitionsForSite_shouldRemoveConfigurationsForOneSiteWhenSiteIsDeleted()
    {
        $this->configureSomeDimensions();
        $config = new Configuration();
        $this->assertNotEmpty($config->getCustomDimensionsForSite($idSite = 1));

        \Piwik\API\Request::processRequest('SitesManager.deleteSite', array('idSite' => $idSite));

        // verify removed
        $this->assertSame(array(), $config->getCustomDimensionsForSite($idSite));

        // verify entries for other site still exists
        $this->assertNotEmpty($config->getCustomDimensionsForSite($idSite = 2));
    }

    /**
     * @dataProvider getScopesSupportExtractions
     */
    public function test_doesScopeSupportExtractions($expectedSupportsExtractions, $scope)
    {
        $this->assertSame($expectedSupportsExtractions, CustomDimensions::doesScopeSupportExtractions($scope));
    }

    public function getScopesSupportExtractions()
    {
        return array(
            array($support = true, CustomDimensions::SCOPE_ACTION),
            array($support = false, CustomDimensions::SCOPE_VISIT),
            array($support = false, CustomDimensions::SCOPE_CONVERSION),
            array($support = false, 'anyRanDOm'),
        );
    }

    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, false, $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);

        Cache::deleteCacheWebsiteAttributes(1);
        Cache::deleteCacheWebsiteAttributes(2);
    }
}