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

ApiCounterTest.php « System « tests « Live « plugins - github.com/matomo-org/matomo.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 5c81d0260e71fd67a961bdf55088c8764b7db4c8 (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
<?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\Live\tests\System;

use Piwik\Date;
use Piwik\Plugins\Goals\API as GoalsApi;
use Piwik\Plugins\Live\API;
use Piwik\Tests\Framework\Fixture;
use Piwik\Tests\Framework\Mock\FakeAccess;
use Piwik\Tests\Framework\TestCase\SystemTestCase;

/**
 * @group Live
 * @group ApiTest
 * @group Api
 * @group Plugins
 */
class ApiCounterTest extends SystemTestCase
{
    /**
     * @var int
     */
    private static $testNow;

    /**
     * @var API
     */
    private $api;
    private $idSite = 1;

    public static function setUpBeforeClass(): void
    {
        self::$testNow = strtotime('2018-02-03 04:45:40');

        parent::setUpBeforeClass();
    }

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

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

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

        $this->setAnonymous();
        $this->api->getCounters($this->idSite, 5);
    }

    public function test_GetCounters_ShouldReturnZeroForAllCounters_IfThereAreNoVisitsEtc()
    {
        $counters = $this->api->getCounters($this->idSite, 5);

        $this->assertEquals($this->buildCounter(0, 0, 0, 0), $counters);
    }

    public function test_GetCounters_ShouldOnlyReturnResultsOfLastMinutes()
    {
        $this->trackSomeVisits();

        $counters = $this->api->getCounters($this->idSite, 5);
        $this->assertEquals($this->buildCounter(19, 32, 16, 16), $counters);

        $counters = $this->api->getCounters($this->idSite, 20);
        $this->assertEquals($this->buildCounter(24, 60, 20, 40), $counters);

        $counters = $this->api->getCounters($this->idSite, 0);
        $this->assertEquals($this->buildCounter(0, 0, 0, 0), $counters);
    }

    public function test_GetCounters_ShouldHideAllColumnsIfRequested()
    {
        $exampleCounter = $this->buildCounter(0, 0, 0, 0);
        $counters = $this->api->getCounters($this->idSite, 5, false, array(), array_keys($exampleCounter[0]));
        $this->assertEquals(array(array()), $counters);
    }

    public function test_GetCounters_ShouldHideSomeColumnsIfRequested()
    {
        $counters = $this->api->getCounters($this->idSite, 20, false, array(), array('visitsConverted', 'visitors'));
        $this->assertEquals(array(array('visits' => 24, 'actions' => 60)), $counters);
    }

    public function test_GetCounters_ShouldShowAllColumnsIfRequested()
    {
        $counter = $this->buildCounter(24, 60, 20, 40);
        $counters = $this->api->getCounters($this->idSite, 20, false, array_keys($counter[0]));
        $this->assertEquals($counter, $counters);
    }

    public function test_GetCounters_ShouldShowSomeColumnsIfRequested()
    {
        $counters = $this->api->getCounters($this->idSite, 20, false, array('visits', 'actions'));
        $this->assertEquals(array(array('visits' => 24, 'actions' => 60)), $counters);
    }

    public function test_GetCounters_ShouldHideColumnIfGivenInShowAndHide()
    {
        $counters = $this->api->getCounters($this->idSite, 20, false, array('visits', 'actions'), array('actions'));
        $this->assertEquals(array(array('visits' => 24)), $counters);
    }

    private function trackSomeVisits()
    {
        $nowTimestamp = self::$testNow;

        // use local tracker so mock location provider can be used
        $t = Fixture::getTracker($this->idSite, $nowTimestamp, $defaultInit = true, $useLocal = false);
        $t->enableBulkTracking();

        for ($i = 0; $i != 20; ++$i) {
            $t->setForceNewVisit();
            $t->setVisitorId( substr(md5($i * 1000), 0, $t::LENGTH_VISITOR_ID));

            $factor = 10;
            if ($i > 15) {
                $factor = 30; // make sure first 15 visits are always within 5 minutes to prevent any random fails
            }
            $time = $nowTimestamp - ($i * $factor);

            // first visit -> this one is > 5 minutes and should be ignored in one test
            $date = Date::factory($time - 600);
            $t->setForceVisitDateTime($date->getDatetime());
            $t->setUrl("http://piwik.net/space/quest/iv");
            $t->doTrackPageView("Space Quest XV");

            $t->doTrackGoal(1); // this one is > 5 minutes and should be ignored in one test

            // second visit
            $date = Date::factory($time - 1);
            $t->setForceVisitDateTime($date->getDatetime());
            $t->setUrl("http://piwik.net/space/quest/iv");
            $t->doTrackPageView("Space Quest XII");

            if ($i % 6 == 0) {
                $t->setForceNewVisit(); // to test visitors vs visits
            }

            // third visit
            $date = Date::factory($time);
            $t->setForceVisitDateTime($date->getDatetime());
            $t->setUrl("http://piwik.net/grue/$i");
            $t->doTrackPageView('It is pitch black...');

            $t->doTrackGoal(2);
        }

        Fixture::checkBulkTrackingResponse($t->doBulkTrack());
    }

    private function buildCounter($visits, $actions, $visitors, $visitsConverted)
    {
        return array(array(
            'visits'   => $visits,
            'actions'  => $actions,
            'visitors' => $visitors,
            'visitsConverted' => $visitsConverted,
        ));
    }

    private function createSite()
    {
        Fixture::createWebsite('2013-01-23 01:23:45');
        GoalsApi::getInstance()->addGoal(1, 'MyName', 'manually', '', 'contains');
        GoalsApi::getInstance()->addGoal(1, 'MyGoal', 'manually', '', 'contains');
    }

    private function setSuperUser()
    {
        FakeAccess::$superUser = true;
    }

    private function setAnonymous()
    {
        FakeAccess::clearAccess();
    }

    public static function provideContainerConfigBeforeClass()
    {
        return array(
            'Piwik\Access' => new FakeAccess(),
            'Tests.now' => self::$testNow,
        );
    }
}