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

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

use Piwik\Plugins\CustomDimensions\Dimension\Extraction;
use Piwik\Tests\Framework\Fixture;
use Piwik\Tests\Framework\TestCase\IntegrationTestCase;
use Piwik\Tracker\ActionPageview;
use Piwik\Tracker\Request;

/**
 * @group CustomDimensions
 * @group ExtractionTest
 * @group Extraction
 * @group Dao
 * @group Plugins
 */
class ExtractionTest extends IntegrationTestCase
{

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

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

    public function test_toArray()
    {
        $extraction = $this->buildExtraction('url', '.com/(.+)/index');
        $value = $extraction->toArray();

        $this->assertSame(array('dimension' => 'url', 'pattern' => '.com/(.+)/index'), $value);
    }

    public function test_extract_url_withMatch()
    {
        $extraction = $this->buildExtraction('url', '.com/(.+)/index');

        $request = $this->buildRequest();
        $value   = $extraction->extract($request);

        $this->assertSame('test', $value);
    }

    public function test_extract_url_withNoPattern()
    {
        $extraction = $this->buildExtraction('url', 'example');

        $request = $this->buildRequest();
        $value   = $extraction->extract($request);

        $this->assertNull($value);
    }

    public function test_extract_url_withPatternButNoMatch()
    {
        $extraction = $this->buildExtraction('url', 'examplePiwik(.+)');

        $request = $this->buildRequest();
        $value   = $extraction->extract($request);

        $this->assertNull($value);
    }

    public function test_actionName_match()
    {
        $extraction = $this->buildExtraction('action_name', 'My(.+)Title');

        $request = $this->buildRequest();
        $value   = $extraction->extract($request);

        $this->assertSame(' Test ', $value);
    }

    public function test_extract_urlparam()
    {
        $request = $this->buildRequest();

        $value = $this->buildExtraction('urlparam', 'module')->extract($request);
        $this->assertSame('CoreHome', $value);

        $value = $this->buildExtraction('urlparam', 'action')->extract($request);
        $this->assertSame('test', $value);

        $value = $this->buildExtraction('urlparam', 'notExist')->extract($request);
        $this->assertNull($value);
    }

    public function test_extract_withAction_shouldReadValueFromAction_NotFromPassedRequest()
    {
        $request = $this->buildRequest();
        $action = new ActionPageview($request);

        // we create a new empty request here to make sure it actually reads the value from $action and not from $request
        $request = new Request(array());
        $request->setMetadata('Actions', 'action', $action);

        $value = $this->buildExtraction('urlparam', 'module')->extract($request);
        $this->assertSame('CoreHome', $value);

        $value = $this->buildExtraction('action_name', 'My(.+)Title')->extract($request);
        $this->assertSame(' Test ', $value);
    }

    /**
     * @dataProvider getCaseSensitiveTestProvider
     */
    public function test_extract_shouldBeCaseSensitiveByDefault($dimension, $pattern, $expectedExtracted)
    {
        $request = $this->buildRequest();

        // Title is "My Test Title"
        $value = $this->buildExtraction($dimension, $pattern)->extract($request);
        $this->assertSame($expectedExtracted, $value);
    }

    public function getCaseSensitiveTestProvider()
    {
        return array(
            array('action_name', 'My(.+)Title', ' Test '),
            array('action_name', 'my(.+)Title', null),
            array('action_name', 'My(.+)title', null),
            array('urlparam',    'camelCase',   'fooBarBaz'),
            array('urlparam',    'camelcase',   null),
            array('urlparam',    'Camelcase',   null),
        );
    }

    /**
     * @dataProvider getCaseInsensitiveTestProvider
     */
    public function test_extract_WhenCaseInsensitiveIsEnabled($dimension, $pattern, $expectedExtracted)
    {
        $request = $this->buildRequest();

        $extraction = $this->buildExtraction($dimension, $pattern);
        $extraction->setCaseSensitive(false);
        // Title is "My Test Title"
        $value = $extraction->extract($request);
        $this->assertSame($expectedExtracted, $value);
    }

    public function getCaseInsensitiveTestProvider()
    {
        return array(
            array('action_name', 'My(.+)Title', ' Test '),
            array('action_name', 'my(.+)Title', ' Test '),
            array('action_name', 'My(.+)title', ' Test '),
            array('urlparam',    'camelCase',   'fooBarBaz'),
            array('urlparam',    'camelcase',   'fooBarBaz'),
            array('urlparam',    'Camelcase',   'fooBarBaz'),
        );
    }

    public function test_extract_anyRandomTrackingApiParameter()
    {
        $request = $this->buildRequest();

        $value = $this->buildExtraction('urlref', '/ref(.+)')->extract($request);
        $this->assertSame('errer', $value);
    }

    public function test_extract_whenOnlyPatternGiven()
    {
        $request = $this->buildRequest();

        $value = $this->buildExtraction('url', '(.+)')->extract($request);
        $this->assertSame('http://www.example.com/test/index.php?idsite=54&module=CoreHome&action=test&camelCase=fooBarBaz', $value);
    }

    public function test_check_shouldFailWhenInvalidDimensionGiven()
    {
        $this->expectException(\Exception::class);
        $this->expectExceptionMessage('Invald dimension \'anyInvalid\' used in an extraction. Available dimensions are: url, urlparam, action_name');

        $this->buildExtraction('anyInvalid', '/ref(.+)')->check();
    }

    /**
     * @dataProvider getInvalidPatterns
     */
    public function test_check_shouldFailWhenInvalidPatternGiven($pattern)
    {
        $this->expectException(\Exception::class);
        $this->expectExceptionMessage('You need to group exactly one part of the regular expression inside round brackets, eg \'index_(.+).html\'');

        $this->buildExtraction('url', $pattern)->check();
    }

    public function test_check_shouldNotFailWhenValidCombinationsAreGiven()
    {
        $this->buildExtraction('url', 'index_(+).html')->check();
        $this->buildExtraction('action_name', 'index_(+).html')->check();
        $this->buildExtraction('url', '')->check(); // empty value is allowed
        $this->buildExtraction('urlparam', 'index')->check(); // does not have to contain brackets
    }

    public function getInvalidPatterns()
    {
        return array(
            array('index.html'),
            array('index.(html'),
            array('index.)html'),
            array('index.)(html'),
            array('index.)(.+)html'),
        );
    }

    private function buildRequest()
    {
        $url = 'http://www.example.com/test/index.php?idsite=54&module=CoreHome&action=test&camelCase=fooBarBaz';
        $referrer = 'http://www.example.com/referrer';
        $actionName = 'My Test Title';

        return new Request(array('idsite' => 1, 'url' => $url, 'action_name' => $actionName, 'urlref' => $referrer));
    }

    private function buildExtraction($dimension, $pattern)
    {
        return new Extraction($dimension, $pattern);
    }
}