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

ActionTest.php « Tracker « Core « PHPUnit « tests - github.com/matomo-org/matomo.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 95a8fee7b46fed572ab2dfbc7550bd8bf620aefd (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
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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
<?php
use Piwik\Config;
use Piwik\Access;
use Piwik\Translate;

/**
 * Piwik - Open source web analytics
 *
 * @link http://piwik.org
 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
 */
class Tracker_ActionTest extends DatabaseTestCase
{
    public function setUp()
    {
        parent::setUp();
        $userFile = dirname(__FILE__) . '/../../../resources/Tracker/Action.config.ini.php';
        $config = Config::getInstance();
        $config->clear();
        $config->setTestEnvironment($userFile, false);

        \Piwik\PluginsManager::getInstance()->loadPlugins(array('SitesManager'));
        
        Translate::getInstance()->loadEnglishTranslation();
    }

    protected function setUpRootAccess()
    {
        $pseudoMockAccess = new FakeAccess;
        FakeAccess::$superUser = true;
        Access::setSingletonInstance($pseudoMockAccess);
    }

    public function getTestUrls()
    {
        $campaignNameParam = 'test_campaign_name';
        $campaignKwdParam = 'test_piwik_kwd';

        $urls = array(
            // a wrongly formatted url (parse_url returns false)
            array('http:////wrongurl',
                  array(false,
                        false)),

            // a URL with all components
            array('http://username:password@hostname:80/path?phpSESSID=value#anchor',
                  array('http://username:password@hostname:80/path#anchor',
                        'http://username:password@hostname:80/path#anchor')),

            // a standard url with excluded campaign parameters
            array('http://a.com/index?p1=v1&' . $campaignNameParam . '=Adwords-CPC&' . $campaignKwdParam . '=My killer keyword',
                  array('http://a.com/index?p1=v1',
                        'http://a.com/index?p1=v1')),

            // a standard url with excluded campaign parameters, GA style
            array('http://a.com/index?p1=v1&utm_campaign=Adwords-CPC&utm_term=My killer keyword',
                  array('http://a.com/index?p1=v1',
                        'http://a.com/index?p1=v1')),

            // testing with capital parameter
            array('http://a.com/index?p1=v1&P2=v2&p3=v3',
                  array('http://a.com/index?p1=v1&P2=v2&p3=v3',
                        'http://a.com/index?p1=v1&p3=v3')),

            // testing with array []
            array('http://a.com/index?p1=v1&p2[]=v;2a&p2[]=v2b&p2[]=v2c&p3=v3&p4=v4',
                  array('http://a.com/index?p1=v1&p2[]=v;2a&p2[]=v2b&p2[]=v2c&p3=v3&p4=v4',
                        'http://a.com/index?p1=v1&p3=v3')),

            // testing with missing value
            array('http://a.com/index?p1=v1&p2=&p3=v3&p4',
                  array('http://a.com/index?p1=v1&p2=&p3=v3&p4',
                        'http://a.com/index?p1=v1&p3=v3')),
            array('http://a.com/index?p1&p2=v2&p3=v3&p4',
                  array('http://a.com/index?p1&p2=v2&p3=v3&p4',
                        'http://a.com/index?p1&p3=v3')),

            // testing with extra &&
            array('http://a.com/index?p1=v1&&p2=v;2&p3=v%3b3&p4=v4&&',
                  array('http://a.com/index?p1=v1&p2=v;2&p3=v%3b3&p4=v4',
                        'http://a.com/index?p1=v1&p3=v%3b3')),

            // encode entities
            array('http://a.com/index?p1=v1&p2%5B%5D=v2&p3=v3&p4=v4',
                  array('http://a.com/index?p1=v1&p2[]=v2&p3=v3&p4=v4',
                        'http://a.com/index?p1=v1&p3=v3')),
            array('http://a.com/index?var%5Bvalue%5D%5Bdate%5D=01.01.2012',
                  array('http://a.com/index?var[value][date]=01.01.2012',
                        'http://a.com/index')),

            // matrix parameters
            array('http://a.com/index;jsessionid=value;p1=v1;p2=v2',
                  array('http://a.com/index?p1=v1&p2=v2',
                        'http://a.com/index?p1=v1')),
            array('http://a.com/index;jsessionid=value?p1=v1&p2=v2',
                  array('http://a.com/index?p1=v1&p2=v2',
                        'http://a.com/index?p1=v1')),
        );

        return $urls;
    }

    /**
     * No excluded query parameters specified, apart from the standard "session" parameters, always excluded
     *
     * @group Core
     * @group Tracker
     * @group Tracker_Action
     * @dataProvider getTestUrls
     */
    public function testExcludeQueryParametersNone($url, $filteredUrl)
    {
        $this->setUpRootAccess();
        $idSite = Piwik_SitesManager_API::getInstance()->addSite("site1", array('http://example.org'), $ecommerce = 0,
            $siteSearch = 1, $searchKeywordParameters = null, $searchCategoryParameters = null,
            $excludedIps = '', $excludedQueryParameters = '', $timezone = null, $currency = null,
            $group = null, $startDate = null, $excludedUserAgents = null, $keepURLFragments = 1);
        $this->assertEquals($filteredUrl[0], Piwik_Tracker_Action::excludeQueryParametersFromUrl($url, $idSite));
    }

    public function getTestUrlsHashtag()
    {
        $urls = array(
            // URL, Expected URL
            array('wrongurl/#', 'http://wrongurl/'),
            array('wrongurl/#t', 'http://wrongurl/#t'),
            array('wrongurl/#test', 'http://wrongurl/#test'),
            array('wrongurl/#test=1', 'http://wrongurl/#test=1'),
            array('wrongurl/#test=1#', 'http://wrongurl/#test=1'),
        );
        return $urls;
    }

    /**
     * Test removing hash tag
     * @group Core
     * @group Tracker
     * @group Tracker_Action
     * @dataProvider getTestUrlsHashtag
     */
    public function testRemoveTrailingHashtag($url, $expectedUrl)
    {
        $this->assertEquals(Piwik_Tracker_Action::reconstructNormalizedUrl($url, Piwik_Tracker_Action::$urlPrefixMap['http://']), $expectedUrl);
    }


    /**
     * Testing with some website specific parameters excluded
     * @group Core
     * @group Tracker
     * @group Tracker_Action
     * @dataProvider getTestUrls
     */
    public function testExcludeQueryParametersSiteExcluded($url, $filteredUrl)
    {
        $excludedQueryParameters = 'p4, p2, var[value][date]';
        $this->setUpRootAccess();
        $idSite = Piwik_SitesManager_API::getInstance()->addSite("site1", array('http://example.org'), $ecommerce = 0,
            $siteSearch = 1, $searchKeywordParameters = null, $searchCategoryParameters = null,
            $excludedIps = '', $excludedQueryParameters, $timezone = null, $currency = null,
            $group = null, $startDate = null, $excludedUserAgents = null, $keepURLFragments = 1);
        $this->assertEquals($filteredUrl[1], Piwik_Tracker_Action::excludeQueryParametersFromUrl($url, $idSite));
    }

    /**
     * Testing with some website specific and some global excluded query parameters
     * @group Core
     * @group Tracker
     * @group Tracker_Action
     * @dataProvider getTestUrls
     */
    public function testExcludeQueryParametersSiteAndGlobalExcluded($url, $filteredUrl)
    {
        // testing also that query parameters are case insensitive 
        $excludedQueryParameters = 'P2,var[value][date]';
        $excludedGlobalParameters = 'blabla, P4';
        $this->setUpRootAccess();
        $idSite = Piwik_SitesManager_API::getInstance()->addSite("site1", array('http://example.org'), $ecommerce = 0,
            $siteSearch = 1, $searchKeywordParameters = null, $searchCategoryParameters = null,
            $excludedIps = '', $excludedQueryParameters, $timezone = null, $currency = null,
            $group = null, $startDate = null, $excludedUserAgents = null, $keepURLFragments = 1);
        Piwik_SitesManager_API::getInstance()->setGlobalExcludedQueryParameters($excludedGlobalParameters);
        $this->assertEquals($filteredUrl[1], Piwik_Tracker_Action::excludeQueryParametersFromUrl($url, $idSite));
    }


    public function getExtractUrlData()
    {
        return array(
            // outlinks
            array(
                'request'  => array('link' => 'http://example.org'),
                'expected' => array('name' => null,
                                    'url'  => 'http://example.org',
                                    'type' => Piwik_Tracker_Action::TYPE_OUTLINK),
            ),
            // outlinks with custom name
            array(
                'request'  => array('link' => 'http://example.org', 'action_name' => 'Example.org'),
                'expected' => array('name' => 'Example.org',
                                    'url'  => 'http://example.org',
                                    'type' => Piwik_Tracker_Action::TYPE_OUTLINK),
            ),
            // keep the case in urls, but trim
            array(
                'request'  => array('link' => '    http://example.org/Category/Test/      '),
                'expected' => array('name' => null,
                                    'url'  => 'http://example.org/Category/Test/',
                                    'type' => Piwik_Tracker_Action::TYPE_OUTLINK),
            ),

            // trim the custom name
            array(
                'request'  => array('link' => '    http://example.org/Category/Test/      ', 'action_name' => '  Example dot org '),
                'expected' => array('name' => 'Example dot org',
                                    'url'  => 'http://example.org/Category/Test/',
                                    'type' => Piwik_Tracker_Action::TYPE_OUTLINK),
            ),

            // downloads
            array(
                'request'  => array('download' => 'http://example.org/*$test.zip'),
                'expected' => array('name' => null,
                                    'url'  => 'http://example.org/*$test.zip',
                                    'type' => Piwik_Tracker_Action::TYPE_DOWNLOAD),
            ),

            // downloads with custom name
            array(
                'request'  => array('download' => 'http://example.org/*$test.zip', 'action_name' => 'Download test.zip'),
                'expected' => array('name' => 'Download test.zip',
                                    'url'  => 'http://example.org/*$test.zip',
                                    'type' => Piwik_Tracker_Action::TYPE_DOWNLOAD),
            ),

            // keep the case and multiple / in urls
            array(
                'request'  => array('download' => 'http://example.org/CATEGORY/test///test.pdf'),
                'expected' => array('name' => null,
                                    'url'  => 'http://example.org/CATEGORY/test///test.pdf',
                                    'type' => Piwik_Tracker_Action::TYPE_DOWNLOAD),
            ),

            // page view
            array(
                'request'  => array('url' => 'http://example.org/'),
                'expected' => array('name' => null,
                                    'url'  => 'http://example.org/',
                                    'type' => Piwik_Tracker_Action::TYPE_ACTION_URL),
            ),
            array(
                'request'  => array('url' => 'http://example.org/', 'action_name' => 'Example.org Website'),
                'expected' => array('name' => 'Example.org Website',
                                    'url'  => 'http://example.org/',
                                    'type' => Piwik_Tracker_Action::TYPE_ACTION_URL),
            ),
            array(
                'request'  => array('url' => 'http://example.org/CATEGORY/'),
                'expected' => array('name' => null,
                                    'url'  => 'http://example.org/CATEGORY/',
                                    'type' => Piwik_Tracker_Action::TYPE_ACTION_URL),
            ),
            array(
                'request'  => array('url' => 'http://example.org/CATEGORY/TEST', 'action_name' => 'Example.org / Category / test /'),
                'expected' => array('name' => 'Example.org/Category/test',
                                    'url'  => 'http://example.org/CATEGORY/TEST',
                                    'type' => Piwik_Tracker_Action::TYPE_ACTION_URL),
            ),
            array(
                'request'  => array('url' => 'http://example.org/?2,123'),
                'expected' => array('name' => null,
                                    'url'  => 'http://example.org/?2,123',
                                    'type' => Piwik_Tracker_Action::TYPE_ACTION_URL),
            ),

            // empty request
            array(
                'request'  => array(),
                'expected' => array('name' => null, 'url' => '',
                                    'type' => Piwik_Tracker_Action::TYPE_ACTION_URL),
            ),
            array(
                'request'  => array('name' => null, 'url' => "\n"),
                'expected' => array('name' => null, 'url' => '',
                                    'type' => Piwik_Tracker_Action::TYPE_ACTION_URL),
            ),
            array(
                'request'  => array('url'         => 'http://example.org/category/',
                                    'action_name' => 'custom name with/one delimiter/two delimiters/'),
                'expected' => array('name' => 'custom name with/one delimiter/two delimiters',
                                    'url'  => 'http://example.org/category/',
                                    'type' => Piwik_Tracker_Action::TYPE_ACTION_URL),
            ),
            array(
                'request'  => array('url'         => 'http://example.org/category/',
                                    'action_name' => 'http://custom action name look like url/'),
                'expected' => array('name' => 'http:/custom action name look like url',
                                    'url'  => 'http://example.org/category/',
                                    'type' => Piwik_Tracker_Action::TYPE_ACTION_URL),
            ),
            // testing: delete tab, trimmed, not strtolowered
            array(
                'request'  => array('url' => "http://example.org/category/test///test  wOw      "),
                'expected' => array('name' => null,
                                    'url'  => 'http://example.org/category/test///test  wOw',
                                    'type' => Piwik_Tracker_Action::TYPE_ACTION_URL),
            ),
            // testing: inclusion of zero values in action name
            array(
                'request'  => array('url' => "http://example.org/category/1/0/t/test"),
                'expected' => array('name' => null,
                                    'url'  => 'http://example.org/category/1/0/t/test',
                                    'type' => Piwik_Tracker_Action::TYPE_ACTION_URL),
            ),
            // testing: action name ("Test &hellip;") - expect decoding of some html entities
            array(
                'request'  => array('url'         => 'http://example.org/ACTION/URL',
                                    'action_name' => "Test &hellip;"),
                'expected' => array('name' => 'Test …',
                                    'url'  => 'http://example.org/ACTION/URL',
                                    'type' => Piwik_Tracker_Action::TYPE_ACTION_URL),
            ),
            // testing: action name ("Special &amp; chars") - expect no conversion of html special chars
            array(
                'request'  => array('url'         => 'http://example.org/ACTION/URL',
                                    'action_name' => "Special &amp; chars"),
                'expected' => array('name' => 'Special &amp; chars',
                                    'url'  => 'http://example.org/ACTION/URL',
                                    'type' => Piwik_Tracker_Action::TYPE_ACTION_URL),
            ),
            // testing: action name ("Tést") - handle wide character
            array(
                'request'  => array('url'         => 'http://example.org/ACTION/URL',
                                    'action_name' => "Tést"),
                'expected' => array('name' => 'Tést',
                                    'url'  => 'http://example.org/ACTION/URL',
                                    'type' => Piwik_Tracker_Action::TYPE_ACTION_URL),
            ),
            // testing: action name ("Tést") - handle UTF-8 byte sequence
            array(
                'request'  => array('url'         => 'http://example.org/ACTION/URL',
                                    'action_name' => "T\xc3\xa9st"),
                'expected' => array('name' => 'Tést',
                                    'url'  => 'http://example.org/ACTION/URL',
                                    'type' => Piwik_Tracker_Action::TYPE_ACTION_URL),
            ),
            // testing: action name ("Tést") - invalid UTF-8 (e.g., ISO-8859-1) is not handled
            array(
                'request'  => array('url'         => 'http://example.org/ACTION/URL',
                                    'action_name' => "T\xe9st"),
                'expected' => array('name' => version_compare(PHP_VERSION, '5.2.5') === -1 ? 'T\xe9st' : 'Tést',
                                    'url'  => 'http://example.org/ACTION/URL',
                                    'type' => Piwik_Tracker_Action::TYPE_ACTION_URL),
            ),
        );
    }

    /**
     * @dataProvider getExtractUrlData
     * @group Core
     * @group Tracker
     * @group Tracker_Action
     */
    public function testExtractUrlAndActionNameFromRequest($request, $expected)
    {
        $this->setUpRootAccess();
        $idSite = Piwik_SitesManager_API::getInstance()->addSite("site1", array('http://example.org'));
        $request['idsite'] = $idSite;
        $request = new Piwik_Tracker_Request($request);
        $action = new Test_Piwik_TrackerAction_extractUrlAndActionNameFromRequest($request);

        $this->assertEquals($action->public_extractUrlAndActionNameFromRequest(), $expected);
    }
}

class Test_Piwik_TrackerAction_extractUrlAndActionNameFromRequest extends Piwik_Tracker_Action
{
    public function public_extractUrlAndActionNameFromRequest()
    {
        return $this->extractUrlAndActionNameFromRequest();
    }
}