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

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

use Piwik\Common;
use Piwik\Plugins\BulkTracking\tests\Framework\Mock\Tracker\Response;
use Piwik\Plugins\BulkTracking\tests\Framework\TestCase\BulkTrackingTestCase;
use Piwik\Plugins\BulkTracking\Tracker\Handler;
use Piwik\Tests\Framework\Fixture;
use Piwik\Tracker;
use Piwik\Tests\Framework\Mock\Tracker\RequestSet;

class TestIntegrationTracker extends Tracker {

    protected function loadTrackerPlugins()
    {
        // if we reload the plugins we would lose the injected data :(
    }
}

/**
 * @group TrackerTest
 * @group Tracker
 */
class TrackerTest extends BulkTrackingTestCase
{
    /**
     * @var TestIntegrationTracker
     */
    private $tracker;

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

        $this->tracker = new TestIntegrationTracker();

        Fixture::createWebsite('2014-01-01 00:00:00');
        Fixture::createWebsite('2014-01-01 00:00:00');

        $this->injectRawDataToBulk($this->getDummyRequest());
    }

    public function test_main_shouldIncreaseLoggedRequestsCounter()
    {
        $this->tracker->main($this->getHandler(), $this->getEmptyRequestSet());

        $this->assertSame(2, $this->tracker->getCountOfLoggedRequests());
    }

    public function test_main_shouldUseBulkHandler()
    {
        $handler = $this->getHandler();
        $this->assertTrue($handler instanceof Handler);
    }

    public function test_main_shouldReturnBulkTrackingResponse()
    {
        $response = $this->tracker->main($this->getHandler(), $this->getEmptyRequestSet());

        $this->assertSame('{"status":"success","tracked":2,"invalid":0}', $response);
    }

    public function test_main_shouldReturnErrorResponse_InCaseOfAnyError()
    {
        $requestSet = new RequestSet();
        $requestSet->enableThrowExceptionOnInit();

        $handler = $this->getHandler();
        $handler->setResponse(new Response());

        $response = $this->tracker->main($handler, $requestSet);

        $this->assertSame('{"status":"error","tracked":0,"invalid":0}', $response);
    }

    public function test_main_shouldReturnErrorResponse_IfNotAuthorized()
    {
        $this->injectRawDataToBulk($this->getDummyRequest(), true);

        $handler = $this->getHandler();
        $handler->setResponse(new Response());

        $response = $this->tracker->main($handler, $this->getEmptyRequestSet());

        $this->assertSame('{"status":"error","tracked":0,"invalid":0}', $response);
    }

    public function test_main_shouldActuallyTrack()
    {
        $this->assertEmpty($this->getIdVisit(1));
        $this->assertEmpty($this->getIdVisit(2));

        $requestSet = $this->getEmptyRequestSet();
        $this->tracker->main($this->getHandler(), $requestSet);

        $this->assertCount(2, $requestSet->getRequests(), 'Nothing tracked because it could not find 2 requests');

        $visit1 = $this->getIdVisit(1);
        $visit2 = $this->getIdVisit(2);

        $this->assertNotEmpty($visit1);
        $this->assertEquals(1, $visit1['idsite']);
        $this->assertNotEmpty($visit2);
        $this->assertEquals(2, $visit2['idsite']);

        $this->assertEmpty($this->getIdVisit(3));
    }

    public function test_main_shouldReportInvalidIndices_IfInvalidRequestsIncluded_AndRequestAuthenticated()
    {
        $this->injectRawDataToBulk($this->getDummyRequest($token = Fixture::getTokenAuth(), $idSite = array(1, -100)));

        $handler = $this->getHandler();
        $handler->setResponse(new Response());

        $response = $this->tracker->main($handler, $this->getEmptyRequestSet());

        $this->assertEquals('{"status":"success","tracked":1,"invalid":1,"invalid_indices":[1]}', $response);
    }

    public function test_main_shouldReportInvalidCount_IfInvalidRequestsIncluded_AndRequestNotAuthenticated()
    {
        $this->injectRawDataToBulk($this->getDummyRequest($token = null, $idSite = array(1, -100)));

        $handler = $this->getHandler();
        $handler->setResponse(new Response());

        $response = $this->tracker->main($handler, $this->getEmptyRequestSet());

        $this->assertEquals('{"status":"success","tracked":1,"invalid":1}', $response);
    }

    private function getHandler()
    {
        return Tracker\Handler\Factory::make();
    }

    private function getEmptyRequestSet()
    {
        return new Tracker\RequestSet();
    }

    private function getIdVisit($idVisit)
    {
        return Tracker::getDatabase()->fetchRow("SELECT * FROM " . Common::prefixTable('log_visit') . " WHERE idvisit = ?", array($idVisit));
    }

    protected static function configureFixture($fixture)
    {
        parent::configureFixture($fixture);

        $fixture->createSuperUser = true;
    }
}