File: ModelTest.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 (342 lines) | stat: -rw-r--r-- 12,702 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
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
<?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\SegmentEditor\tests\Integration;

use Piwik\Common;
use Piwik\Date;
use Piwik\Db;
use Piwik\Plugins\SegmentEditor\Model;
use Piwik\Tests\Framework\TestCase\IntegrationTestCase;

class ModelTest extends IntegrationTestCase
{
    private $model;

    private $idSegment1;

    private $idSegment2;

    private $idSegment3;

    private $idSegment4;

    public function setUp(): void
    {
        parent::setUp();
        $this->model = new Model();
        $this->idSegment1 = $this->model->createSegment(array(
            'name' => 'Narnia',
            'definition' => 'country==Narnia',
            'login' => 'user1',
            'enable_only_idsite' => 0,
        ));
        $this->idSegment2 = $this->model->createSegment(array(
            'name' => 'Genovia',
            'definition' => 'country==Genovia',
            'auto_archive' => 1,
            'enable_only_idsite' => 0,
            'enable_all_users' => 1,
        ));
        $this->idSegment3 = $this->model->createSegment(array(
            'name' => 'Hobbiton',
            'definition' => 'country==Hobbiton',
            'auto_archive' => 0,
            'login' => 'user2',
            'enable_only_idsite' => 1,
        ));
    }

    public function tearDown(): void
    {
        parent::tearDown();
        // Force a hard delete of segment
        $idsToDelete = array($this->idSegment1, $this->idSegment2, $this->idSegment3);
        if ($this->idSegment4) {
            $idsToDelete[] = $this->idSegment4;
        }
        $idsToDelete = implode(',', $idsToDelete);
        Db::query(
            "DELETE FROM " . Common::prefixTable('segment') . " WHERE idsegment IN ($idsToDelete)"
        );
    }

    public function testDeleteSegmentDoesSoftDelete()
    {
        $preDeleteTimestamp = Date::getNowTimestamp();
        $this->model->deleteSegment($this->idSegment1);

        // None of the model methods should return it as it's deleted - so we need to get it manually from DB
        $result = Db::query(
            'SELECT * FROM ' . Common::prefixTable('segment') . ' WHERE idsegment = ' . $this->idSegment1
        );
        $row = $result->fetch();

        $this->assertNotEmpty($row);
        $this->assertEquals(1, $row['deleted']);
        $deletedTimestamp = Date::factory($row['ts_last_edit'])->getTimestamp();
        $this->assertGreaterThanOrEqual($deletedTimestamp, $preDeleteTimestamp);
    }

    public function testGetAllSegmentsAndIgnoreVisibilityWithDeletedSegment()
    {
        $segments = $this->model->getAllSegmentsAndIgnoreVisibility();
        $this->assertEquals(3, count($segments));

        $this->model->deleteSegment($this->idSegment2);
        $segments = $this->model->getAllSegmentsAndIgnoreVisibility();

        $this->assertReturnedIdsMatch(array($this->idSegment1, $this->idSegment3), $segments);
    }

    public function testGetSegmentsToAutoArchiveWithDeletedSegment()
    {
        $segments = $this->model->getSegmentsToAutoArchive();
        $this->assertEquals(1, count($segments));
        $this->assertReturnedIdsMatch(array($this->idSegment2), $segments);

        $this->model->deleteSegment($this->idSegment2);
        $segments = $this->model->getSegmentsToAutoArchive();

        $this->assertEmpty($segments);
    }

    public function testGetAllSegmentsWithDeletedSegment()
    {
        $segments = $this->model->getAllSegments('user1');
        $this->assertEquals(2, count($segments));

        $this->model->deleteSegment($this->idSegment1);
        $segments = $this->model->getAllSegments('user1');

        $this->assertReturnedIdsMatch(array($this->idSegment2), $segments);
    }

    public function testGetAllSegmentsForSiteWithDeletedSegment()
    {
        $segments = $this->model->getAllSegmentsForSite(1, 'user1');
        $this->assertEquals(2, count($segments));
        $this->assertReturnedIdsMatch(array($this->idSegment1, $this->idSegment2), $segments);

        $this->model->deleteSegment($this->idSegment2);
        $segments = $this->model->getAllSegmentsForSite(1, 'user1');

        $this->assertReturnedIdsMatch(array($this->idSegment1), $segments);
    }

    public function testGetAllSegmentsForAllUsersWithDeletedSegment()
    {
        $segments = $this->model->getAllSegmentsForAllUsers();
        $this->assertEquals(3, count($segments));

        $this->model->deleteSegment($this->idSegment3);
        $segments = $this->model->getAllSegmentsForAllUsers();

        $this->assertReturnedIdsMatch(array($this->idSegment1, $this->idSegment2), $segments);
    }

    public function testGetSegmentByDefinitionWithDeletedSegment()
    {
        $segment = $this->model->getSegmentByDefinition('Country==Genovia');
        $this->assertNotEmpty($segment);

        $this->model->deleteSegment($this->idSegment2);
        $segment = $this->model->getSegmentByDefinition('Country==Genovia');

        $this->assertEmpty($segment);
    }

    public function testGetSegmentsDeletedSinceNoDeletedSegments()
    {
        $date = Date::factory('now');
        $segments = $this->model->getSegmentsDeletedSince($date);
        $this->assertEmpty($segments);
    }

    public function testGetSegmentsDeletedSinceOneDeletedSegment()
    {
        $this->model->deleteSegment($this->idSegment3);

        $date = Date::factory('now')->subDay(1);
        $segments = $this->model->getSegmentsDeletedSince($date);

        $this->assertCount(1, $segments);
        $this->assertEquals('country==Hobbiton', $segments[0]['definition']);
    }

    public function testGetSegmentsDeletedSinceSegmentDeletedTooLongAgo()
    {
        // Manually delete it to set timestamp 9 days in past
        $deletedAt = Date::factory('now')->subDay(9)->toString('Y-m-d H:i:s');
        $this->model->updateSegment($this->idSegment1, array(
            'deleted' => 1,
            'ts_last_edit' => $deletedAt,
        ));

        // The segment deleted above should not be included as it was more than 8 days ago
        $date = Date::factory('now')->subDay(8);
        $segments = $this->model->getSegmentsDeletedSince($date);

        $this->assertEmpty($segments);
    }

    public function testGetSegmentsDeletedSinceDuplicateSegment()
    {
        // Turn segment1 into a duplicate of segment2, except it's also deleted
        $this->model->updateSegment($this->idSegment1, array(
            'definition' => 'country==Genovia',
            'deleted' => 1,
            'ts_last_edit' => Date::factory('now')->toString('Y-m-d H:i:s'),
        ));

        $date = Date::factory('now')->subDay(8);
        $segments = $this->model->getSegmentsDeletedSince($date);

        $this->assertEmpty($segments);
    }

    public function testGetSegmentsDeletedSinceDuplicateSegmentDifferentIdSite()
    {
        // Turn segment2 into a duplicate of segment3, except for a different idsite and also deleted
        $this->model->updateSegment($this->idSegment2, array(
            'definition' => 'country==Hobbiton',
            'enable_only_idsite' => 2,
            'deleted' => 1,
            'ts_last_edit' => Date::factory('now')->toString('Y-m-d H:i:s'),
        ));

        $date = Date::factory('now')->subDay(8);
        $segments = $this->model->getSegmentsDeletedSince($date);

        $this->assertCount(1, $segments);
        $this->assertEquals('country==Hobbiton', $segments[0]['definition']);
        $this->assertEquals(2, $segments[0]['enable_only_idsite']);
    }

    public function testGetSegmentsDeletedSinceDuplicateSegmentAllSitesAndSingleSite()
    {
        // Turn segment2 into a duplicate of segment3, except for all sites and also deleted
        $this->model->updateSegment($this->idSegment2, array(
            'definition' => 'country==Hobbiton',
            'enable_only_idsite' => 0,
            'deleted' => 1,
            'ts_last_edit' => Date::factory('now')->toString('Y-m-d H:i:s'),
        ));

        $date = Date::factory('now')->subDay(8);
        $segments = $this->model->getSegmentsDeletedSince($date);

        $this->assertCount(1, $segments);
        $this->assertEquals('country==Hobbiton', $segments[0]['definition']);
        $this->assertEquals(0, $segments[0]['enable_only_idsite']);
        $this->assertEquals(array(1), $segments[0]['idsites_to_preserve']);
    }

    public function testGetSegmentsDeletedSinceDuplicateSegmentSingleSiteAndAllSites()
    {
        // Turn segment3 into a duplicate of segment1, except for a single site and deleted
        $this->model->updateSegment($this->idSegment3, array(
            'definition' => 'country==Narnia',
            'enable_only_idsite' => 1,
            'deleted' => 1,
            'ts_last_edit' => Date::factory('now')->toString('Y-m-d H:i:s'),
        ));

        $date = Date::factory('now')->subDay(8);
        $segments = $this->model->getSegmentsDeletedSince($date);

        // There is still a live segment for all sites, so the deleted site-specific one is ignored
        $this->assertEmpty($segments);
    }

    public function testGetSegmentsDeletedSinceExistingSiteSpecificAndAllSitesMatch()
    {
        // A deleted all-sites segment, with both an all-sites and a site-specific segment still present
        $this->model->updateSegment($this->idSegment1, array(
            'definition' => 'actions >= 1',
            'enable_only_idsite' => 0,
            'deleted' => 0,
            'ts_last_edit' => Date::factory('now')->toString('Y-m-d H:i:s'),
        ));
        $this->model->updateSegment($this->idSegment2, array(
            'definition' => 'actions >= 1',
            'enable_only_idsite' => 0,
            'deleted' => 1,
            'ts_last_edit' => Date::factory('now')->toString('Y-m-d H:i:s'),
        ));
        $this->model->updateSegment($this->idSegment3, array(
            'definition' => 'actions >= 1',
            'enable_only_idsite' => 3,
            'deleted' => 0,
            'ts_last_edit' => Date::factory('now')->toString('Y-m-d H:i:s'),
        ));
        $this->idSegment4 = $this->model->createSegment(array(
            'definition' => 'actions >= 1',
            'enable_only_idsite' => 1,
            'deleted' => 1,
            'ts_last_edit' => Date::factory('now')->toString('Y-m-d H:i:s'),
        ));

        $date = Date::factory('now')->subDay(8);
        $segments = $this->model->getSegmentsDeletedSince($date);

        $this->assertEmpty($segments);
    }

    public function testGetSegmentsDeletedSinceUrlDecodedVersionOfSegment()
    {
        // Turn segment2 into a duplicate of segment3, except a urlencoded version
        $this->model->updateSegment($this->idSegment2, array(
            'definition' => 'country%3D%3DHobbiton',
            'enable_only_idsite' => 1,
            'deleted' => 1,
            'ts_last_edit' => Date::factory('now')->toString('Y-m-d H:i:s'),
        ));

        $date = Date::factory('now')->subDay(8);
        $segments = $this->model->getSegmentsDeletedSince($date);

        // The two encoded and decoded version of the segments should be treated as duplicates
        // This means there segment has a non-deleted version so it's not returned
        $this->assertEmpty($segments);
    }

    public function testGetSegmentsDeletedSinceUrlEncodedVersionOfSegment()
    {
        // segment1 => url decoded version, deleted
        $this->model->updateSegment($this->idSegment1, array(
            'definition' => 'country==Narnia',
            'deleted' => 1,
            'ts_last_edit' => Date::factory('now')->toString('Y-m-d H:i:s'),
        ));
        // segment2 => url encoded version, not deleted
        $this->model->updateSegment($this->idSegment2, array(
            'definition' => 'country%3D%3DNarnia',
            'deleted' => 0,
            'ts_last_edit' => Date::factory('now')->toString('Y-m-d H:i:s'),
        ));

        $date = Date::factory('now')->subDay(8);
        $segments = $this->model->getSegmentsDeletedSince($date);

        // The two encoded and decoded version of the segments should be treated as duplicates
        // This means there segment has a non-deleted version so it's not returned
        $this->assertEmpty($segments);
    }

    private function assertReturnedIdsMatch(array $expectedIds, array $resultSet)
    {
        $this->assertEquals(count($expectedIds), count($resultSet));

        $returnedIds = array_column($resultSet, 'idsegment');
        sort($returnedIds);

        $this->assertEquals(array_values($expectedIds), array_values($returnedIds));
    }
}