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

ApiTest.php « Integration « tests « CustomDimensions « plugins - github.com/matomo-org/matomo.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: f66fad436ea17922998e147efc44564985692714 (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
<?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\API;
use Piwik\Plugins\CustomDimensions\CustomDimensions;
use Piwik\Plugins\CustomDimensions\Dao\Configuration;
use Piwik\Plugins\CustomDimensions\tests\Integration\Dao\ConfigurationTest;
use Piwik\Tests\Framework\Fixture;
use Piwik\Tests\Framework\Mock\FakeAccess;
use Piwik\Tests\Framework\TestCase\IntegrationTestCase;
use Exception;

/**
 * @group CustomDimensions
 * @group ApiTest
 * @group Plugins
 */
class ApiTest extends IntegrationTestCase
{
    /**
     * @var API
     */
    private $api;

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

        $this->api = API::getInstance();

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

        $this->setSuperUser();
    }

    /**
     * @dataProvider getInvalidConfigForNewDimensions
     */
    public function test_configureNewDimension_shouldFailWhenThereIsAnError($dimension)
    {
        try {
            $this->api->configureNewCustomDimension($idSite = 1, $dimension['name'], $dimension['scope'], $dimension['active'], $dimension['extractions'], $dimension['case_sensitive']);
        } catch (Exception $e) {
            self::assertStringContainsString($dimension['message'], $e->getMessage());
            return;
        }

        $this->fail('An expected exception has not been thrown');
    }

    public function getInvalidConfigForNewDimensions()
    {
        return array(
            array(array('message' => 'CustomDimensions_NameAllowedCharacters',          'name' => 'Inval/\\nam&<b>e</b>', 'scope' => CustomDimensions::SCOPE_ACTION, 'active' => '1', 'extractions' => array(), 'case_sensitive' => '1')),
            array(array('message' => "Invalid value 'anyScOPe' for 'scope'",            'name' => 'Valid Name äöü',       'scope' => 'anyScOPe',                     'active' => '1', 'extractions' => array(), 'case_sensitive' => '1')),
            array(array('message' => "Invalid value '2' for 'active' specified",        'name' => 'Valid Name äöü',       'scope' => CustomDimensions::SCOPE_ACTION, 'active' => '2', 'extractions' => array(), 'case_sensitive' => '1')),
            array(array('message' => 'extractions has to be an array',                  'name' => 'Valid Name äöü',       'scope' => CustomDimensions::SCOPE_ACTION, 'active' => '1', 'extractions' => 5, 'case_sensitive' => '1')),
            array(array('message' => "Extractions can be used only in scope 'action'",  'name' => 'Valid Name äöü',       'scope' => CustomDimensions::SCOPE_VISIT,  'active' => '1', 'extractions' => array(array('dimension' => 'url', 'pattern' => 'i(.*)')), 'case_sensitive' => '1')),
            array(array('message' => "Invalid value '4' for 'caseSensitive' specified", 'name' => 'Valid Name äöü',       'scope' => CustomDimensions::SCOPE_ACTION, 'active' => '1', 'extractions' => array(), 'case_sensitive' => '4')),
        );
    }

    public function test_configureNewDimension_shouldReturnCreatedIdOnSuccess()
    {
        $id = $this->api->configureNewCustomDimension($idSite = 1, 'Valid Name äöü', CustomDimensions::SCOPE_ACTION, '1', array(array('dimension' => 'urlparam', 'pattern' => 'test')), '0');

        $this->assertSame(1, $id);

        // verify created
        $dimensions = $this->api->getConfiguredCustomDimensions(1);

        $expectedDimension = array(
            'idcustomdimension' => '1',
            'idsite' => '1',
            'name' => 'Valid Name äöü',
            'index' => '1',
            'scope' => 'action',
            'active' => true,
            'extractions' => array(
                array ('dimension' => 'urlparam', 'pattern' => 'test')
            ),
            'case_sensitive' => false
        );
        $this->assertSame(array($expectedDimension), $dimensions);
    }

    public function test_configureNewDimension_shouldFailWhenNotHavingAdminPermissions()
    {
        $this->expectException(\Exception::class);
        $this->expectExceptionMessage('checkUserHasWriteAccess');

        $this->setUser();
        $this->api->configureNewCustomDimension($idSite = 1, 'Valid Name äöü', CustomDimensions::SCOPE_VISIT, '1', array(array('dimension' => 'urlparam', 'pattern' => 'test')));
    }

    /**
     * @dataProvider getInvalidConfigForExistingDimensions
     */
    public function test_configureExistingCustomDimension_shouldFailWhenThereIsAnError($dimension)
    {
        try {
            $this->test_configureNewDimension_shouldReturnCreatedIdOnSuccess();
            $this->api->configureExistingCustomDimension($dimension['id'], $idSite = 1, $dimension['name'], $dimension['active'], $dimension['extractions']);
        } catch (Exception $e) {
            self::assertStringContainsString($dimension['message'], $e->getMessage());
            return;
        }

        $this->fail('An expected exception has not been thrown');
    }

    public function getInvalidConfigForExistingDimensions()
    {
        return array(
            array(array('message' => "CustomDimensions_ExceptionDimensionDoesNotExist", 'id' => '999', 'name' => 'Valid Name äöü', 'active' => '1', 'extractions' => array())),
            array(array('message' => 'CustomDimensions_NameAllowedCharacters',          'id' => '1',   'name' => 'Inval/\\nam&<b>e</b>', 'active' => '1', 'extractions' => array())),
            array(array('message' => "Invalid value '2' for 'active' specified",        'id' => '1',   'name' => 'Valid Name äöü', 'active' => '2', 'extractions' => array())),
            array(array('message' => 'extractions has to be an array',                  'id' => '1',   'name' => 'Valid Name äöü', 'active' => '1', 'extractions' => 5)),
        );
    }

    public function test_configureExistingCustomDimension_shouldReturnNothingOnSuccess()
    {
        $this->test_configureNewDimension_shouldReturnCreatedIdOnSuccess();
        $return = $this->api->configureExistingCustomDimension($id = 1, $idSite = 1, 'New Valid Name äöü', '0', array(array('dimension' => 'urlparam', 'pattern' => 'newtest')), $caseSensitive = true);

        $this->assertNull($return);

        // verify updated
        $dimensions = $this->api->getConfiguredCustomDimensions(1);
        $this->assertCount(1, $dimensions);
        $this->assertSame('New Valid Name äöü', $dimensions[0]['name']);
        $this->assertFalse($dimensions[0]['active']);
        $this->assertTrue($dimensions[0]['case_sensitive']);
        $this->assertSame('newtest', $dimensions[0]['extractions'][0]['pattern']);
    }

    public function test_configureExistingCustomDimension_shouldNotChangeCaseSensitive_IfNoValuePassed()
    {
        $this->test_configureNewDimension_shouldReturnCreatedIdOnSuccess();

        // verify created with false
        $dimensions = $this->api->getConfiguredCustomDimensions(1);
        $this->assertFalse($dimensions[0]['case_sensitive']);

        $return = $this->api->configureExistingCustomDimension($id = 1, $idSite = 1, 'New Valid Name äöü', '0', array(array('dimension' => 'urlparam', 'pattern' => 'newtest')));

        $this->assertNull($return);

        // verify after update still false
        $dimensions = $this->api->getConfiguredCustomDimensions(1);
        $this->assertFalse($dimensions[0]['case_sensitive']);
    }

    public function test_configureExistingCustomDimension_shouldThrowException_WhenTryingToSetExtractionsForNonActionScope()
    {
        $this->expectException(\Exception::class);
        $this->expectExceptionMessage('Extractions can be used only in scope \'action\'');

        $id = $this->api->configureNewCustomDimension($idSite = 1, 'Name', CustomDimensions::SCOPE_VISIT, '1');
        $this->api->configureExistingCustomDimension($id, $idSite, 'Name', '0', array(array('dimension' => 'urlparam', 'pattern' => 'newtest')));
    }

    public function test_configureExistingCustomDimension_shouldFailWhenNotHavingAdminPermissions()
    {
        $this->expectException(\Exception::class);
        $this->expectExceptionMessage('checkUserHasWriteAccess');

        $this->setUser();
        $this->api->configureExistingCustomDimension($id = 1, $idSite = 1, 'New Valid Name äöü', '0', array(array('dimension' => 'urlparam', 'pattern' => 'newtest')));
    }

    public function test_getConfiguredCustomDimensions_shouldFailWhenNotHavingAdminPermissions()
    {
        $this->expectException(\Exception::class);
        $this->expectExceptionMessage('checkUserHasViewAccess');

        $this->setAnonymousUser();
        $this->api->getConfiguredCustomDimensions($idSite = 1);
    }

    public function test_getAvailableScopes_shouldFailWhenNotHavingAdminPermissions()
    {
        $this->expectException(\Exception::class);
        $this->expectExceptionMessage('checkUserHasViewAccess');

        $this->setAnonymousUser();
        $this->api->getAvailableScopes($idSite = 1);
    }

    public function test_getAvailableExtractionDimensions_shouldFailWhenNotHavingAdminPermissions()
    {
        $this->expectException(\Exception::class);
        $this->expectExceptionMessage('checkUserHasSomeWriteAccess');

        $this->setUser();
        $this->api->getAvailableExtractionDimensions();
    }

    public function test_getCustomDimension_shouldFailWhenNotHavingViewPermissions()
    {
        $this->expectException(\Exception::class);
        $this->expectExceptionMessage('checkUserHasViewAccess');

        $this->setAnonymousUser();
        $this->api->getCustomDimension($idDimension = 1, $idSite = 1, $period = 'day', $date = 'today');
    }

    public function test_getConfiguredCustomDimensionsHavingScope_shouldFindEntriesHavingScopeAndSite()
    {
        ConfigurationTest::createManyCustomDimensionCasesFor(new Configuration());

        $dimensions = $this->api->getConfiguredCustomDimensionsHavingScope($idSite = 1, $scope = 'action');

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

        foreach ($dimensions as $dimension) {
            $this->assertSame('1', $dimension['idsite']);
            $this->assertSame('action', $dimension['scope']);
            $this->assertTrue(is_bool($dimension['active']));
        }

        $dimensions = $this->api->getConfiguredCustomDimensionsHavingScope($idSite = 1, $scope = 'visit');

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

        foreach ($dimensions as $dimension) {
            $this->assertSame('1', $dimension['idsite']);
            $this->assertSame('visit', $dimension['scope']);
            $this->assertTrue(is_bool($dimension['active']));
        }

        // nothing matches
        $dimensions = $this->api->getConfiguredCustomDimensionsHavingScope($idSite = 1, $scope = 'nothing');
        $this->assertSame(array(), $dimensions);
    }

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

    protected function setSuperUser()
    {
        FakeAccess::clearAccess(true);
    }

    protected function setUser()
    {
        FakeAccess::clearAccess(false);
        FakeAccess::$idSitesView = array(1);
        FakeAccess::$idSitesAdmin = array();
        FakeAccess::$identity = 'aUser';
    }

    protected function setAnonymousUser()
    {
        FakeAccess::clearAccess();
        FakeAccess::$identity = 'anonymous';
    }

}