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: e1ff3e8ec38a1c43ea4d0ced1270c99e3d9a7045 (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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
<?php
/**
 * Piwik - free/libre analytics platform
 *
 * @link https://matomo.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\Container\StaticContainer;
use Piwik\NoAccessException;
use Piwik\Piwik;
use Piwik\Plugins\SitesManager\API;
use Piwik\Site as PiwikSite;

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

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

    public function getTokenAuth()
    {
        return false;
    }

    public function __construct($superUser = false, $idSitesAdmin = array(), $idSitesView = array(), $identity = 'superUserLogin', $idSitesWrite = array())
    {
        // couldn't use DI here as tests seem to fail cause at this time when it is called eg in
        // plugins/Live/tests/System/ApiCounterTest.php the environment is not set up yet
        $role = new Access\RolesProvider();
        $capabilities = new Access\CapabilitiesProvider();
        parent::__construct($role, $capabilities);

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

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

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

    public static function setIdSitesWrite($ids)
    {
        self::$superUser    = false;
        self::$idSitesWrite = $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 {
            return;
        }

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

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

    public function checkUserHasWriteAccess($idSites)
    {
        if (!self::$superUser) {
            $websitesAccess = array_merge(self::$idSitesWrite, self::$idSitesAdmin);
        } else {
            return;
        }

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

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

    public function checkUserHasCapability($idSites, $capability)
    {
        $cap = $this->capabilityProvider->getCapability($capability);

        if ($cap && Piwik::isUserHasAdminAccess($idSites) && $cap->hasRoleCapability(Access\Role\Admin::ID)) {
            return;
        } elseif ($cap && Piwik::isUserHasWriteAccess($idSites) && $cap->hasRoleCapability(Access\Role\Write::ID)) {
            return;
        } elseif ($cap && Piwik::isUserHasViewAccess($idSites) && $cap->hasRoleCapability(Access\Role\View::ID)) {
            return;
        }

        if (isset(self::$idSitesCapabilities[$capability]) && is_array(self::$idSitesCapabilities[$capability])) {
            if (!is_array($idSites)) {
                $idSites = array($idSites);
            }
            $idSites = array_map('intval', $idSites);
            $idSitesCap = array_map('intval', self::$idSitesCapabilities[$capability]);
            $missingSites = array_diff($idSites, $idSitesCap);
            if (empty($missingSites)) {
                return;
            }
        }

        throw new NoAccessException("checkUserHasCapability " . $capability ." 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::$idSitesWrite, 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::$idSitesWrite, self::$idSitesAdmin)) == 0) {
                throw new NoAccessException("checkUserHasSomeViewAccess Fake exception // string not to be tested");
            }
        } else {
            return;
        }
    }

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

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

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

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

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

    //means at least write access
    public function checkUserHasSomeWriteAccess()
    {
        if (!$this->isUserHasSomeWriteAccess()) {
            throw new NoAccessException("checkUserHasSomeWriteAccess 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 getSitesIdWithWriteAccess()
    {
        if (self::$superUser) {
            return API::getInstance()->getAllSitesId();
        }

        return self::$idSitesWrite;
    }

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

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

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

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

        return $result;
    }
}