File: LogTableTest.php

package info (click to toggle)
matomo 5.8.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 95,068 kB
  • sloc: php: 289,425; xml: 127,249; javascript: 112,130; python: 202; sh: 178; makefile: 20; sql: 10
file content (239 lines) | stat: -rw-r--r-- 8,929 bytes parent folder | download
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
<?php

/**
 * Matomo - free/libre analytics platform
 *
 * @link    https://matomo.org
 * @license https://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
 */

namespace Piwik\Plugins\CustomDimensions\tests\Integration\Dao;

use Piwik\Common;
use Piwik\DbHelper;
use Piwik\Plugins\CustomDimensions\CustomDimensions;
use Piwik\Plugins\CustomDimensions\Dao\LogTable;
use Piwik\Tests\Framework\TestCase\IntegrationTestCase;

/**
 * @group CustomDimensions
 * @group LogTableTest
 * @group LogTable
 * @group Dao
 * @group Plugins
 */
class LogTableTest extends IntegrationTestCase
{
    /**
     * @var LogTable
     */
    private $logVisit;

    /**
     * @var LogTable
     */
    private $logAction;

    /**
     * @var LogTable
     */
    private $logConverison;

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

        $this->logVisit = new LogTable(CustomDimensions::SCOPE_VISIT);
        $this->logAction = new LogTable(CustomDimensions::SCOPE_ACTION);
        $this->logConverison = new LogTable(CustomDimensions::SCOPE_CONVERSION);

        $this->logVisit->install();
        $this->logAction->install();
        $this->logConverison->install();
    }

    public function tearDown(): void
    {
        $this->logVisit->uninstall();
        $this->logAction->uninstall();
        $this->logConverison->uninstall();

        parent::tearDown();
    }

    public function testShouldInstall5IndexesByDefault()
    {
        $this->assertSame(5, $this->logVisit->getNumInstalledIndexes());
        $this->assertSame(5, $this->logAction->getNumInstalledIndexes());
        $this->assertSame(5, $this->logConverison->getNumInstalledIndexes());
    }

    public function testInstallShouldInstallColumnLogLinkVisitActionTimeSpent()
    {
        $columnn = DbHelper::getTableColumns(Common::prefixTable('log_link_visit_action'));
        $this->assertArrayHasKey('time_spent', $columnn);
    }

    public function testInstallShouldInstallColumnLogVisitLastIdlinkVa()
    {
        $columnn = DbHelper::getTableColumns(Common::prefixTable('log_visit'));
        $this->assertArrayHasKey('last_idlink_va', $columnn);
    }

    public function testUninstallShouldRemoveAllInstalledColumns()
    {
        $this->logVisit->uninstall();
        $this->logAction->uninstall();
        $this->logConverison->uninstall();

        $this->assertSame(0, $this->logVisit->getNumInstalledIndexes());
        $this->assertSame(0, $this->logAction->getNumInstalledIndexes());
        $this->assertSame(0, $this->logConverison->getNumInstalledIndexes());
    }

    public function testUninstallShouldInstallColumnLogLinkVisitActionTimeSpent()
    {
        $this->logAction->uninstall();

        $columnn = DbHelper::getTableColumns(Common::prefixTable('log_link_visit_action'));
        $this->assertArrayNotHasKey('time_spent', $columnn);
    }

    public function testUninstallShouldInstallColumnLogVisitLastIdlinkVa()
    {
        $this->logVisit->uninstall();

        $columnn = DbHelper::getTableColumns(Common::prefixTable('log_visit'));
        $this->assertArrayNotHasKey('last_idlink_va', $columnn);
    }

    public function testInstallShouldMakeSureThereAreAtLeast5Installed()
    {
        $this->logVisit->removeCustomDimension(3);
        $this->logVisit->removeCustomDimension(4);
        $this->logVisit->removeCustomDimension(1);

        $this->assertSame(2, $this->logVisit->getNumInstalledIndexes());

        // should automatically detect to install 3
        $this->logVisit->install();

        $this->assertSame(5, $this->logVisit->getNumInstalledIndexes());
        $this->assertSame(array(2,5,6,7,8), $this->logVisit->getInstalledIndexes());
    }

    public function testGetInstalledIndexesShouldReturnInstalledIndexes()
    {
        $expected = range(1, 5);
        $this->assertSame($expected, $this->logVisit->getInstalledIndexes());
        $this->assertSame($expected, $this->logAction->getInstalledIndexes());
        $this->assertSame($expected, $this->logConverison->getInstalledIndexes());

        $this->logAction->addManyCustomDimensions(1);

        $this->assertSame($expected, $this->logVisit->getInstalledIndexes());
        $this->assertSame(range(1, 6), $this->logAction->getInstalledIndexes());
        $this->assertSame($expected, $this->logConverison->getInstalledIndexes());

        $this->logVisit->removeCustomDimension(2);

        $this->assertSame(array(1,3,4,5), $this->logVisit->getInstalledIndexes());
        $this->assertSame(range(1, 6), $this->logAction->getInstalledIndexes());
        $this->assertSame($expected, $this->logConverison->getInstalledIndexes());
    }

    public function testGetNumInstalledIndexesShouldReturnInstalledIndexes()
    {
        $expected = 5;
        $this->assertSame($expected, $this->logVisit->getNumInstalledIndexes());
        $this->assertSame($expected, $this->logAction->getNumInstalledIndexes());
        $this->assertSame($expected, $this->logConverison->getNumInstalledIndexes());

        $this->logAction->addManyCustomDimensions(1);

        $this->assertSame($expected, $this->logVisit->getNumInstalledIndexes());
        $this->assertSame(6, $this->logAction->getNumInstalledIndexes());
        $this->assertSame($expected, $this->logConverison->getNumInstalledIndexes());

        $this->logVisit->removeCustomDimension(2);

        $this->assertSame(4, $this->logVisit->getNumInstalledIndexes());
        $this->assertSame(6, $this->logAction->getNumInstalledIndexes());
        $this->assertSame($expected, $this->logConverison->getNumInstalledIndexes());
    }

    public function testBuildCustomDimensionColumnName()
    {
        $this->assertNull(LogTable::buildCustomDimensionColumnName('0'));
        $this->assertNull(LogTable::buildCustomDimensionColumnName(''));
        $this->assertNull(LogTable::buildCustomDimensionColumnName(null));
        $this->assertNull(LogTable::buildCustomDimensionColumnName(array()));
        $this->assertNull(LogTable::buildCustomDimensionColumnName(array('index' => '')));

        $this->assertSame('custom_dimension_1', LogTable::buildCustomDimensionColumnName('1'));
        $this->assertSame('custom_dimension_1', LogTable::buildCustomDimensionColumnName('1'));
        $this->assertSame('custom_dimension_99', LogTable::buildCustomDimensionColumnName('99'));
        $this->assertSame('custom_dimension_94', LogTable::buildCustomDimensionColumnName('94te'));
        $this->assertSame('custom_dimension_95', LogTable::buildCustomDimensionColumnName(array('index' => '95')));
    }

    public function testRemoveCustomDimensionShouldRemoveASpecificIndex()
    {
        // should remove nothing as not a valid index
        $this->logVisit->removeCustomDimension(0);
        $this->logVisit->removeCustomDimension(null);

        $this->assertSame(range(1, 5), $this->logVisit->getInstalledIndexes());

        $this->logVisit->removeCustomDimension(3);
        $this->assertSame(array(1,2,4,5), $this->logVisit->getInstalledIndexes());

        $this->logVisit->removeCustomDimension('1');
        $this->assertSame(array(2,4,5), $this->logVisit->getInstalledIndexes());
    }

    public function testAddManyCustomDimensionsShouldAddNewColumns()
    {
        // should add nothing as not a valid index
        $this->logVisit->addManyCustomDimensions(0);
        $this->logVisit->addManyCustomDimensions(null);

        $this->assertSame(range(1, 5), $this->logVisit->getInstalledIndexes());

        $this->logVisit->addManyCustomDimensions(1);
        $this->assertSame(range(1, 6), $this->logVisit->getInstalledIndexes());

        $this->logVisit->addManyCustomDimensions(4);
        $this->assertSame(range(1, 10), $this->logVisit->getInstalledIndexes());

        // should automatically add after highest index if some indexes are missing in between
        $this->logVisit->removeCustomDimension('8');
        $this->logVisit->removeCustomDimension('2');
        $this->logVisit->removeCustomDimension('1');
        $this->logVisit->addManyCustomDimensions(2);

        $this->assertSame(array(3,4,5,6,7,9,10,11,12), $this->logVisit->getInstalledIndexes());
    }

    /**
     * @dataProvider getDimensionColumnTestNames
     */
    public function testIsCustomDimensionColumn($expected, $name)
    {
        $this->assertSame($expected, LogTable::isCustomDimensionColumn($name));
    }

    public function getDimensionColumnTestNames()
    {
        return array(
            array(true, 'custom_dimension_5'),
            array(true, 'custom_dimension_99'),
            array(true, 'custom_dimension_0'), // this one should be in theory false, but to keep things simple we return true, logic will be handled somewhere else
            array(false, 'custom_dimension_'),
            array(false, 'dimension_5'),
            array(false, 'anything_6'),
            array(false, ''),
            array(false, 5),
        );
    }
}