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

RequestsTest.php « Integration « tests « BulkTracking « plugins - github.com/matomo-org/matomo.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: e141f1db84a0d0d19bb3add82ebec55a2db26cb4 (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
<?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\Container\StaticContainer;
use Piwik\Plugins\BulkTracking\Tracker\Requests;
use Piwik\Plugins\UsersManager\Model;
use Piwik\Tests\Framework\Fixture;
use Piwik\Tests\Framework\TestCase\IntegrationTestCase;
use Piwik\Tracker\Request;
use Piwik\Tracker\TrackerConfig;

/**
 * @group BulkTracking
 * @group RequestsTest
 * @group Plugins
 * @group Tracker
 */
class RequestsTest extends IntegrationTestCase
{
    /**
     * @var Requests
     */
    private $requests;

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

        Fixture::createWebsite('2014-01-02 03:04:05');

        $this->requests = new Requests();
    }

    public function tearDown(): void
    {
        // clean up your test here if needed

        parent::tearDown();
    }

    public function test_requiresAuthentication_shouldReturnTrue_IfEnabled()
    {
        $oldConfig = TrackerConfig::getConfigValue('bulk_requests_require_authentication');
        TrackerConfig::setConfigValue('bulk_requests_require_authentication', 1);

        $this->assertTrue($this->requests->requiresAuthentication());

        TrackerConfig::setConfigValue('bulk_requests_require_authentication', $oldConfig);
    }

    public function test_requiresAuthentication_shouldReturnFalse_IfDisabled()
    {
        $oldConfig = TrackerConfig::getConfigValue('bulk_requests_require_authentication');
        TrackerConfig::setConfigValue('bulk_requests_require_authentication', 0);

        $this->assertFalse($this->requests->requiresAuthentication());

        TrackerConfig::setConfigValue('bulk_requests_require_authentication', $oldConfig);
    }

    public function test_authenticateRequests_shouldThrowAnException_IfTokenAuthIsEmpty()
    {
        $this->expectException(\Exception::class);
        $this->expectExceptionMessage('token_auth must be specified when using Bulk Tracking Import');

        $requests = array($this->buildDummyRequest());
        $this->requests->authenticateRequests($requests);
    }

    public function test_authenticateRequests_shouldThrowAnException_IfAnyTokenAuthIsEmpty()
    {
        $this->expectException(\Exception::class);
        $this->expectExceptionMessage('token_auth must be specified when using Bulk Tracking Import');

        $requests = array($this->buildDummyRequest($this->getSuperUserToken()), $this->buildDummyRequest());
        $this->requests->authenticateRequests($requests);
    }

    public function test_authenticateRequests_shouldThrowAnException_IfTokenIsNotValid()
    {
        $this->expectException(\Exception::class);
        $this->expectExceptionMessage('token_auth specified does not have Admin permission for idsite=1');

        $dummyToken = StaticContainer::get(Model::class)->generateRandomTokenAuth();
        $superUserToken = $this->getSuperUserToken();

        $requests = array($this->buildDummyRequest($superUserToken), $this->buildDummyRequest($dummyToken));
        $this->requests->authenticateRequests($requests);
    }

    public function test_authenticateRequests_shouldNotFail_IfAllTokensAreValid()
    {
        $superUserToken = $this->getSuperUserToken();

        $requests = array($this->buildDummyRequest($superUserToken), $this->buildDummyRequest($superUserToken));
        $this->requests->authenticateRequests($requests);

        $this->assertTrue(true);
    }

    public function test_authenticateRequests_shouldNotFail_IfEmptyRequestSetGiven()
    {
        $this->requests->authenticateRequests(array());

        $this->assertTrue(true);
    }

    private function getSuperUserToken()
    {
        Fixture::createSuperUser(false);
        return Fixture::getTokenAuth();
    }

    private function buildDummyRequest($token = false)
    {
        return new Request(array('idsite' => '1'), $token);
    }

}