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

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

use Piwik\ArchiveProcessor\Rules;
use Piwik\CronArchive\ReArchiveList;
use Piwik\Date;
use Piwik\Piwik;
use Piwik\Plugins\SegmentEditor\API;
use Piwik\Plugins\SegmentEditor\Model;
use Piwik\Plugins\SitesManager\API as APISitesManager;
use Piwik\Tests\Framework\Mock\FakeAccess;
use Piwik\Tests\Framework\TestCase\IntegrationTestCase;
use Exception;
use Piwik\Plugins\UsersManager\UserUpdater;
use Piwik\Plugins\SegmentEditor\SegmentEditor;
use Piwik\Plugins\UsersManager\API as UsersManagerAPI;

/**
 * Class Plugins_SegmentEditorTest
 *
 * @group Plugins
 */
class SegmentEditorTest extends IntegrationTestCase
{
    public function setUp(): void
    {
        parent::setUp();

        Date::$now = strtotime('2020-03-01 00:00:00');

        \Piwik\Plugin\Manager::getInstance()->loadPlugin('SegmentEditor');
        \Piwik\Plugin\Manager::getInstance()->installLoadedPlugins();

        // setup the access layer
        FakeAccess::setIdSitesView(array(1, 2));
        FakeAccess::setIdSitesAdmin(array(3, 4));

        //finally we set the user as a Super User by default
        FakeAccess::$superUser = true;
        FakeAccess::$superUserLogin = 'superusertest';

        APISitesManager::getInstance()->addSite('test', 'http://example.org');
    }

    public function testAddInvalidSegment_ShouldThrow()
    {
        $this->expectException(\Exception::class);

        API::getInstance()->add('name', 'test==test2');
        $this->fail("Exception not raised.");

        API::getInstance()->add('name', 'test');
        $this->fail("Exception not raised.");
    }

    public function test_AddAndGet_SimpleSegment()
    {
        Rules::setBrowserTriggerArchiving(false);

        $name = 'name';
        $definition = 'searches>1,visitIp!=127.0.0.1';
        $idSegment = API::getInstance()->add($name, $definition);
        $this->assertEquals($idSegment, 1);
        $this->assertReArchivesQueued([]); // none since not auto archive
        $segment = API::getInstance()->get($idSegment);
        unset($segment['ts_created']);
        $expected = array(
            'idsegment' => '1',
            'name' => $name,
            'definition' => $definition,
            'hash' => md5($definition),
            'login' => 'superUserLogin',
            'enable_all_users' => '0',
            'enable_only_idsite' => '0',
            'auto_archive' => '0',
            'ts_last_edit' => null,
            'deleted' => '0',
        );

        $this->assertEquals($segment, $expected);
    }

    public function test_AddAndGet_AnotherSegment()
    {
        Rules::setBrowserTriggerArchiving(false);

        $name = 'name';
        $definition = 'searches>1,visitIp!=127.0.0.1';
        $idSegment = API::getInstance()->add($name, $definition, $idSite = 1, $autoArchive = 1, $enabledAllUsers = 1);
        $this->assertEquals($idSegment, 1);
        $this->assertReArchivesQueued([
            ['idSites' => [1], 'pluginName' => null, 'report' => null, 'segment' => $definition, 'startDate' => Date::factory('2020-03-01 00:00:00')->getTimestamp()],
        ]);

        // Testing get()
        $segment = API::getInstance()->get($idSegment);
        $expected = array(
            'idsegment' => '1',
            'name' => $name,
            'definition' => $definition,
            'hash' => md5($definition),
            'login' => 'superUserLogin',
            'enable_all_users' => '1',
            'enable_only_idsite' => '1',
            'auto_archive' => '1',
            'ts_last_edit' => null,
            'deleted' => '0',
        );
        unset($segment['ts_created']);
        $this->assertEquals($segment, $expected);

        // There is a segment to process for this particular site
        $model = new Model();
        $segments = $model->getSegmentsToAutoArchive($idSite);
        unset($segments[0]['ts_created']);
        $this->assertEquals($segments, array($expected));

        // There is no segment to process for a non existing site
        $segments = $model->getSegmentsToAutoArchive(33);
        $this->assertEquals($segments, array());

        // There is no segment to process across all sites
        $segments = $model->getSegmentsToAutoArchive($idSite = false);
        $this->assertEquals($segments, array());
    }

    public function test_UpdateSegment()
    {
        Rules::setBrowserTriggerArchiving(false);

        $name = 'name"';
        $definition = 'searches>1,visitIp!=127.0.0.1';
        $nameSegment1 = 'hello';
        $idSegment1 = API::getInstance()->add($nameSegment1, 'searches==0', $idSite = 1, $autoArchive = 1, $enabledAllUsers = 1);
        $idSegment2 = API::getInstance()->add($name, $definition, $idSite = 1, $autoArchive = 1, $enabledAllUsers = 1);

        $this->clearReArchiveList();

        $updatedSegment = array(
            'idsegment' => $idSegment2,
            'name' =>   'NEW name',
            'definition' =>  'searches==0',
            'hash' => md5('searches==0'),
            'enable_only_idsite' => '0',
            'enable_all_users' => '0',
            'auto_archive' => '1',
            'ts_last_edit' => Date::now()->getDatetime(),
            'ts_created' => Date::now()->getDatetime(),
            'login' => Piwik::getCurrentUserLogin(),
            'deleted' => '0',
        );
        API::getInstance()->update($idSegment2,
            $updatedSegment['name'],
            $updatedSegment['definition'],
            $updatedSegment['enable_only_idsite'],
            $updatedSegment['auto_archive'],
            $updatedSegment['enable_all_users']
        );

        $this->assertReArchivesQueued([
            ['idSites' => [1], 'pluginName' => null, 'report' => null, 'segment' => $updatedSegment['definition'], 'startDate' => null],
        ]);

        $newSegment = API::getInstance()->get($idSegment2);

        // avoid test failures for when ts_created/ts_last_edit are different by between 1/2 secs
        $this->removeSecondsFromSegmentInfo($updatedSegment);
        $this->removeSecondsFromSegmentInfo($newSegment);

        $this->assertEquals($newSegment, $updatedSegment);

        // Check the other segmenet was not updated
        $newSegment = API::getInstance()->get($idSegment1);
        $this->assertEquals($newSegment['name'], $nameSegment1);
    }

    public function test_deleteSegment()
    {
        Rules::setBrowserTriggerArchiving(false);

        $idSegment1 = API::getInstance()->add('name 1', 'searches==0', $idSite = 1, $autoArchive = 1, $enabledAllUsers = 1);
        $idSegment2 = API::getInstance()->add('name 2', 'searches>1,visitIp!=127.0.0.1', $idSite = 1, $autoArchive = 1, $enabledAllUsers = 1);

        $deleted = API::getInstance()->delete($idSegment2);
        $this->assertTrue($deleted);
        try {
            API::getInstance()->get($idSegment2);
            $this->fail("getting deleted segment should have failed");
        } catch(Exception $e) {
            // expected
        }

        // and this should work
        API::getInstance()->get($idSegment1);
    }

    public function test_transferAllUserSegmentsToSuperUser()
    {
        Rules::setBrowserTriggerArchiving(false);

        $this->addUser('user1');
        $this->addUser('super', true);

        FakeAccess::$identity = 'user1';

        $idSegment = API::getInstance()->add('name 1', 'searches==0', $idSite = 1, $autoArchive = 1, $enabledAllUsers = 0);
        $segment = API::getInstance()->get($idSegment);

        $this->assertEquals('user1', $segment['login']);

        $segmentEditor = new SegmentEditor();
        $segmentEditor->transferAllUserSegmentsToSuperUser('user1');

        $segment = API::getInstance()->get($idSegment);

        $this->assertEquals('super', $segment['login']);
    }

    public function test_deletedUserLostTheSegments()
    {
        Rules::setBrowserTriggerArchiving(false);
        $model = new Model();

        $this->addUser('user1');
        $this->addUser('super', true);

        FakeAccess::$identity = 'user1';
        API::getInstance()->add('name 1', 'searches==0', $idSite = 1, $autoArchive = 1, $enabledAllUsers = 0);

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

        FakeAccess::$identity = 'super';
        $this->deleteUser('user1');

        $segments = $model->getAllSegments('user1');
        $this->assertEmpty($segments);
    }

    private function removeSecondsFromSegmentInfo(&$segmentInfo)
    {
        $timestampProperties = array('ts_last_edit', 'ts_created');
        foreach ($timestampProperties as $propertyName) {
            if (isset($segmentInfo[$propertyName])) {
                $segmentInfo[$propertyName] = substr($segmentInfo[$propertyName], 0, strlen($segmentInfo[$propertyName]) - 2);
            }
        }
    }

    public function provideContainerConfig()
    {
        return array(
            'Piwik\Access' => new FakeAccess()
        );
    }

    private function addUser($login, $isSuper = false)
    {
        UsersManagerAPI::getInstance()->addUser($login, 'password', "{$login}@test.com");

        if ($isSuper) {
            $userUpdater = new UserUpdater();
            $userUpdater->setSuperUserAccessWithoutCurrentPassword($login, true);
        }
    }

    private function deleteUser($login)
    {
        UsersManagerAPI::getInstance()->deleteUser($login);
    }

    private function clearReArchiveList()
    {
        $list = new ReArchiveList();
        $list->setAll([]);
    }

    private function assertReArchivesQueued($expected)
    {
        $list = new ReArchiveList();
        $items = $list->getAll();
        $items = array_map(function ($s) { return json_decode($s, true); }, $items);

        $this->assertEquals($expected, $items);
    }
}