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: 7f6236d82c0ef088a4343c3d83d800491ed3a3e5 (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
<?php
/**
 * Piwik - free/libre analytics platform
 *
 * @link http://piwik.org
 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
 */

namespace Piwik\Plugins\BulkTracking\tests\Integration;

use Piwik\Plugins\BulkTracking\Tracker\Requests;
use Piwik\Plugins\UsersManager\API;
use Piwik\Plugins\UsersManager\UsersManager;
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()
    {
        parent::setUp();

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

    public function tearDown()
    {
        // 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);
    }

    /**
     * @expectedException \Exception
     * @expectedExceptionMessage token_auth must be specified when using Bulk Tracking Import
     */
    public function test_authenticateRequests_shouldThrowAnException_IfTokenAuthIsEmpty()
    {
        $requests = array($this->buildDummyRequest());
        $this->requests->authenticateRequests($requests);
    }

    /**
     * @expectedException \Exception
     * @expectedExceptionMessage token_auth must be specified when using Bulk Tracking Import
     */
    public function test_authenticateRequests_shouldThrowAnException_IfAnyTokenAuthIsEmpty()
    {
        $requests = array($this->buildDummyRequest($this->getSuperUserToken()), $this->buildDummyRequest());
        $this->requests->authenticateRequests($requests);
    }

    /**
     * @expectedException \Exception
     * @expectedExceptionMessage token_auth specified does not have Admin permission for idsite=1
     */
    public function test_authenticateRequests_shouldThrowAnException_IfTokenIsNotValid()
    {
        $dummyToken = API::getInstance()->createTokenAuth('test');
        $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);
    }

}