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

FakeAccess.php « Mock « Framework « PHPUnit « tests - github.com/matomo-org/matomo.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: e23ceb21484f2ca64524677dc7675896c0d4a122 (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
198
199
<?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\Tests\Framework\Mock;

use Piwik\Access;
use Piwik\Auth;
use Piwik\NoAccessException;
use Piwik\Plugins\SitesManager\API;
use Piwik\Site as PiwikSite;
use Exception;

/**
 * FakeAccess for UnitTests
 * @since 2.8.0
 */
class FakeAccess extends Access
{
    public static $superUser = false;
    public static $idSitesAdmin = array();
    public static $idSitesView = array();
    public static $identity = 'superUserLogin';
    public static $superUserLogin = 'superUserLogin';

    public static function clearAccess($superUser = false, $idSitesAdmin = array(), $idSitesView = array(), $identity = 'superUserLogin')
    {
        self::$superUser    = $superUser;
        self::$idSitesAdmin = $idSitesAdmin;
        self::$idSitesView  = $idSitesView;
        self::$identity     = $identity;
    }

    public function getTokenAuth()
    {
        return false;
    }

    public function __construct($superUser = false, $idSitesAdmin = array(), $idSitesView = array(), $identity = 'superUserLogin')
    {
        parent::__construct();

        self::clearAccess($superUser, $idSitesAdmin, $idSitesView, $identity);
    }

    public static function setIdSitesAdmin($ids)
    {
        self::$superUser    = false;
        self::$idSitesAdmin = $ids;
    }

    public static function setIdSitesView($ids)
    {
        self::$superUser   = false;
        self::$idSitesView = $ids;
    }

    public function hasSuperUserAccess()
    {
        return self::$superUser;
    }

    public function checkUserHasSuperUserAccess()
    {
        if (!self::$superUser) {
            throw new NoAccessException("checkUserHasSuperUserAccess Fake exception // string not to be tested");
        }
    }

    public function setSuperUserAccess($bool = true)
    {
        self::$superUser = $bool;
    }

    public function reloadAccess(Auth $auth = null)
    {
        return true;
    }

    public function checkUserHasAdminAccess($idSites)
    {
        if (!self::$superUser) {
            $websitesAccess = self::$idSitesAdmin;
        } else {
            $websitesAccess = API::getInstance()->getAllSitesId();
        }

        $idSites = PiwikSite::getIdSitesFromIdSitesString($idSites);

        foreach ($idSites as $idsite) {
            if (!in_array($idsite, $websitesAccess)) {
                throw new NoAccessException("checkUserHasAdminAccess Fake exception // string not to be tested");
            }
        }
    }

    //means at least view access
    public function checkUserHasViewAccess($idSites)
    {
        if (self::$superUser) {
            return;
        }

        $websitesAccess = array_merge(self::$idSitesView, self::$idSitesAdmin);

        if (!is_array($idSites)) {
            if ($idSites == 'all') {
                $idSites = API::getInstance()->getAllSitesId();
            } else {
                $idSites = explode(',', $idSites);
            }
        }

        if (empty($websitesAccess)) {
            throw new NoAccessException("checkUserHasViewAccess Fake exception // string not to be tested");
        }

        foreach ($idSites as $idsite) {
            if (!in_array($idsite, $websitesAccess)) {
                throw new NoAccessException("checkUserHasViewAccess Fake exception // string not to be tested");
            }
        }
    }

    public function checkUserHasSomeViewAccess()
    {
        if (!self::$superUser) {
            if (count(array_merge(self::$idSitesView, self::$idSitesAdmin)) == 0) {
                throw new NoAccessException("checkUserHasSomeViewAccess Fake exception // string not to be tested");
            }
        } else {
            return;
        }
    }

    //means at least view access
    public function isUserHasSomeAdminAccess()
    {
        if (self::$superUser) {
            return true;
        }

        return count(self::$idSitesAdmin) > 0;
    }

    //means at least view access
    public function checkUserHasSomeAdminAccess()
    {
        if (!$this->isUserHasSomeAdminAccess()) {
            throw new NoAccessException("checkUserHasSomeAdminAccess Fake exception // string not to be tested");
        }
    }

    public function getLogin()
    {
        return self::$identity;
    }

    public function getSitesIdWithAdminAccess()
    {
        if (self::$superUser) {
            return API::getInstance()->getAllSitesId();
        }

        return self::$idSitesAdmin;
    }

    public function getSitesIdWithViewAccess()
    {
        if (self::$superUser) {
            return API::getInstance()->getAllSitesId();
        }

        return self::$idSitesView;
    }

    public function getSitesIdWithAtLeastViewAccess()
    {
        if (self::$superUser) {
            return API::getInstance()->getAllSitesId();
        }

        return array_merge(self::$idSitesView, self::$idSitesAdmin);
    }

    public function getRawSitesWithSomeViewAccess($login)
    {
        $result = array();

        foreach (array_merge(self::$idSitesView, self::$idSitesAdmin) as $idSite) {
            $result[] = array('idsite' => $idSite);
        }

        return $result;
    }
}