File size: 6,443 Bytes
bf8c0ce
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
<?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\Tests\System;

use Piwik\Date;
use Piwik\Tests\Framework\Fixture;
use Piwik\Tests\Framework\TestCase\SystemTestCase;

class ComplicatedSegmentTest extends SystemTestCase
{
    private static $dateTime = '2020-10-01 06:03:45';
    private static $idSite = 1;

    public static function setUpBeforeClass(): void
    {
        parent::setUpBeforeClass();

        Fixture::createWebsite(self::$dateTime);
        self::trackVisitThatMatches();
        self::trackVisitThatDoesNotMatch();
    }

    public static function getOutputPrefix()
    {
        return "ComplicatedSegment";
    }

    /**
     * @dataProvider getApiForTesting
     */
    public function testApi($api, $params)
    {
        $this->runApiTests($api, $params);
    }

    public function getApiForTesting()
    {
        $idSite = self::$idSite;
        $dateTime = self::$dateTime;

        // Note: Creating a segment in the UI would generate double url encoded values
        // But as segments might also be created through API, or provided as URL parameter, this might not always be the case
        // Therefor some values are urlencoded twice, some aren't
        $segmentWithManyActions = 'contentTarget=@' . urlencode(urlencode('place.com'))
            . ',contentName==my\,video' // the backslash should escape the "," as part of the value
            . ';contentInteraction!@maximise'
            . ';contentPiece=@' . urlencode(urlencode('/to/'))
            . ',entryPageTitle!@exit'
            . ';entryPageTitle==' . urlencode(urlencode('entry page'))
            . ';pageTitle=@page'
            . ',pageUrl!@=home'
            . ';entryPageUrl!=' . urlencode(urlencode('https://piwik.net/some/other/page'))
            . ';exitPageUrl==' . urlencode(urlencode('https://piwik.net/outro'))
            . ',downloadUrl==' . urlencode(urlencode('http://piwik.net/fileout.zip'))
            . ';actionUrl!@absent'
            . ';productViewSku!@DEF'
            . ';productCategory==product\category'
            . ';productSku=@ABC'
            . ',productName=@plugin'
            . ';productViewCategory!@thing'
            . ';productViewName!@thing'
            . ',eventAction==action3'
            . ';eventAction!@blah'
            . ',eventName!=blah2'
        ;

        return [
            [
                // This should use a live query (without temporary segment table)
                'Live.getLastVisitsDetails',
                [
                    'idSite'                 => $idSite,
                    'date'                   => $dateTime,
                    'periods'                => ['day'],
                    'segment'                => $segmentWithManyActions,
                    'testSuffix'             => '_SegmentWithManyActions',
                ],
            ], [
                // This should trigger archiving (using temporary segment table)
                'VisitsSummary.get',
                [
                    'idSite'                 => $idSite,
                    'date'                   => $dateTime,
                    'periods'                => ['day'],
                    'segment'                => $segmentWithManyActions,
                    'testSuffix'             => '_SegmentWithManyActions',
                ],
            ],
        ];
    }

    private static function trackVisitThatMatches()
    {
        $t = Fixture::getTracker(self::$idSite, self::$dateTime);
        $t->setIp('203.46.67.91');
        $t->setUrl('https://piwik.net/home/page');
        Fixture::checkResponse($t->doTrackPageView('entry page'));

        // content tracking
        Fixture::checkResponse($t->doTrackContentImpression('test content name', '/path/to/image.png', 'http://place.com/landingpage'));
        Fixture::checkResponse($t->doTrackContentInteraction('expand', 'my,video', '/path/to/myvideo.mp3'));

        // download
        $t->setForceVisitDateTime(Date::factory(self::$dateTime)->addHour(0.15));
        Fixture::checkResponse($t->doTrackAction('http://piwik.net/fileout.zip', 'download'));

        // site search
        Fixture::checkResponse($t->doTrackSiteSearch('site search keyword', 'internal', 40));

        // ecommerce
        $t->setForceVisitDateTime(Date::factory(self::$dateTime)->addHour(0.2)->getTimestamp());
        $t->setEcommerceView($sku = 'ABCSKU123', $name = 'special plugin', $cat = 'product\category', $price = 888);
        $t->setUrl('http://piwik.net/product');
        Fixture::checkResponse($t->doTrackPageView('product page')); // view

        $t->setForceVisitDateTime(Date::factory(self::$dateTime)->addHour(0.22)->getTimestamp());
        $t->addEcommerceItem($sku = 'ABCSKU123', $name = 'special plugin', $cat = 'product\category', $price = 888, $quantity = 2);
        $t->setUrl('http://piwik.net/order');
        Fixture::checkResponse($t->doTrackPageView('order page'));
        $discount = 50;
        $tax = 100;
        $shipping = 35;
        Fixture::checkResponse($t->doTrackEcommerceOrder('937nsjusu 3894', $grandTotal = 888 * 2 - $discount + $shipping + $tax, 888 * 2, $tax, $shipping, $discount));

        // events
        $t->setForceVisitDateTime(Date::factory(self::$dateTime)->addHour(0.3)->getTimestamp());
        $t->setUrl('http://piwik.net/page/with/events');
        Fixture::checkResponse($t->doTrackEvent($category = 'event cat1', $action = 'action3', $name = 'somemovie', $value = 1));
        Fixture::checkResponse($t->doTrackEvent($category = 'event cat2', $action = 'action4', $name = 'someimage', $value = 2));

        // outlink
        $t->setForceVisitDateTime(Date::factory(self::$dateTime)->addHour(0.4));
        Fixture::checkResponse($t->doTrackAction('https://www.anothersite.com/somewhere/else', 'link'));

        $t->setForceVisitDateTime(Date::factory(self::$dateTime)->addHour(0.5)->getTimestamp());
        $t->setUrl('https://piwik.net/outro');
        Fixture::checkResponse($t->doTrackPageView('exit page'));
    }

    private static function trackVisitThatDoesNotMatch()
    {
        $t = Fixture::getTracker(self::$idSite, self::$dateTime);
        $t->setIp('203.45.66.90');
        $t->setUrl('https://piwik.net/some/other/page');
        Fixture::checkResponse($t->doTrackPageView('other page'));
    }
}