1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Tests\Unit; |
4
|
|
|
|
5
|
|
|
use Doctrine\ORM\Query\QueryException; |
6
|
|
|
use Potievdev\SlimRbac\Exception\CyclicException; |
7
|
|
|
use Potievdev\SlimRbac\Exception\DatabaseException; |
8
|
|
|
use Potievdev\SlimRbac\Exception\InvalidArgumentException; |
9
|
|
|
use Potievdev\SlimRbac\Exception\NotUniqueException; |
10
|
|
|
use Potievdev\SlimRbac\Models\Entity\Permission; |
11
|
|
|
use Potievdev\SlimRbac\Models\Entity\Role; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Class RbacManagerTest |
15
|
|
|
* @package Tests\Unit |
16
|
|
|
*/ |
17
|
|
|
class RbacManagerTest extends BaseTestCase |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @throws CyclicException |
21
|
|
|
* @throws DatabaseException |
22
|
|
|
* @throws NotUniqueException |
23
|
|
|
* @throws QueryException |
24
|
|
|
*/ |
25
|
|
|
public function setUp(): void |
26
|
|
|
{ |
27
|
|
|
parent::setUp(); |
28
|
|
|
|
29
|
|
|
$this->rbacManager->removeAll(); |
30
|
|
|
|
31
|
|
|
$edit = $this->rbacManager->createPermission('edit'); |
32
|
|
|
$write = $this->rbacManager->createPermission('write'); |
33
|
|
|
|
34
|
|
|
$moderator = $this->rbacManager->createRole('moderator'); |
35
|
|
|
$admin = $this->rbacManager->createRole('admin'); |
36
|
|
|
|
37
|
|
|
$this->rbacManager->attachPermission($moderator, $edit); |
38
|
|
|
$this->rbacManager->attachPermission($admin, $write); |
39
|
|
|
|
40
|
|
|
$this->rbacManager->attachChildRole($admin, $moderator); |
41
|
|
|
|
42
|
|
|
$this->rbacManager->assign($moderator, self::MODERATOR_USER_ID); |
43
|
|
|
$this->rbacManager->assign($admin, self::ADMIN_USER_ID); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function successCasesProvider(): array |
47
|
|
|
{ |
48
|
|
|
return [ |
49
|
|
|
'moderator can edit' => [self::MODERATOR_USER_ID, 'edit'], |
50
|
|
|
'admin can edit' => [self::ADMIN_USER_ID, 'edit'], |
51
|
|
|
'admin can write' => [self::ADMIN_USER_ID, 'write'], |
52
|
|
|
]; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Testing has permission cases. |
57
|
|
|
* @param integer $userId user id |
58
|
|
|
* @param string $roleOrPermission role or permission name |
59
|
|
|
* @throws QueryException |
60
|
|
|
* @throws InvalidArgumentException |
61
|
|
|
* @dataProvider successCasesProvider |
62
|
|
|
*/ |
63
|
|
|
public function testCheckAccessSuccessCases(int $userId, string $roleOrPermission): void |
64
|
|
|
{ |
65
|
|
|
$this->assertTrue($this->rbacManager->checkAccess($userId, $roleOrPermission)); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @return array |
70
|
|
|
*/ |
71
|
|
|
public function failCasesProvider(): array |
72
|
|
|
{ |
73
|
|
|
return [ |
74
|
|
|
'moderator has no write permission' => [self::MODERATOR_USER_ID, 'write'], |
75
|
|
|
'not existing permission' => [self::ADMIN_USER_ID, 'none_permission'], |
76
|
|
|
'not existing user id not has permission' => [self::NOT_USER_ID, 'edit'], |
77
|
|
|
'not existing user id not has role' => [self::NOT_USER_ID, 'admin'] |
78
|
|
|
]; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Testing not have permission cases |
83
|
|
|
* @param integer $userId user id |
84
|
|
|
* @param string $roleOrPermission role or permission name |
85
|
|
|
* @throws QueryException |
86
|
|
|
* @throws InvalidArgumentException |
87
|
|
|
* @dataProvider failCasesProvider |
88
|
|
|
*/ |
89
|
|
|
public function testCheckAccessFailureCases(int $userId, string $roleOrPermission): void |
90
|
|
|
{ |
91
|
|
|
$this->assertFalse($this->rbacManager->checkAccess($userId, $roleOrPermission)); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Testing adding not unique permission |
96
|
|
|
* |
97
|
|
|
* @throws DatabaseException |
98
|
|
|
* @throws NotUniqueException |
99
|
|
|
*/ |
100
|
|
|
public function testCheckAddingNotUniquePermission() |
101
|
|
|
{ |
102
|
|
|
$this->expectException(NotUniqueException::class); |
103
|
|
|
$this->rbacManager->createPermission('edit'); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Testing adding not unique role |
108
|
|
|
* |
109
|
|
|
* @throws DatabaseException |
110
|
|
|
* @throws NotUniqueException |
111
|
|
|
*/ |
112
|
|
|
public function testCheckAddingNonUniqueRole() |
113
|
|
|
{ |
114
|
|
|
$this->expectException(NotUniqueException::class); |
115
|
|
|
$this->rbacManager->createRole('moderator'); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* |
120
|
|
|
* @throws CyclicException |
121
|
|
|
* @throws DatabaseException |
122
|
|
|
* @throws NotUniqueException |
123
|
|
|
* @throws QueryException |
124
|
|
|
*/ |
125
|
|
|
public function testCheckCyclicException() |
126
|
|
|
{ |
127
|
|
|
$this->expectException(CyclicException::class); |
128
|
|
|
$a = $this->rbacManager->createRole('a'); |
129
|
|
|
$b = $this->rbacManager->createRole('b'); |
130
|
|
|
|
131
|
|
|
$this->rbacManager->attachChildRole($a, $b); |
132
|
|
|
$this->rbacManager->attachChildRole($b, $a); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Testing creating permission |
137
|
|
|
*/ |
138
|
|
|
public function testCheckCreatingPermission() |
139
|
|
|
{ |
140
|
|
|
$permission = $this->repositoryRegistry |
141
|
|
|
->getPermissionRepository() |
|
|
|
|
142
|
|
|
->findOneBy(['name' => 'edit']); |
143
|
|
|
|
144
|
|
|
$this->assertTrue($permission instanceof Permission); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Testing creating role |
149
|
|
|
*/ |
150
|
|
|
public function testCheckCreatingRole() |
151
|
|
|
{ |
152
|
|
|
$role = $this->repositoryRegistry |
153
|
|
|
->getRoleRepository() |
|
|
|
|
154
|
|
|
->findOneBy(['name' => 'admin']); |
155
|
|
|
|
156
|
|
|
$this->assertTrue($role instanceof Role); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* @throws DatabaseException |
161
|
|
|
*/ |
162
|
|
|
public function testCheckDoubleAssigningPermissionToSameRole() |
163
|
|
|
{ |
164
|
|
|
$this->expectException(NotUniqueException::class); |
165
|
|
|
|
166
|
|
|
/** @var Role $role */ |
167
|
|
|
$role = $this->repositoryRegistry |
168
|
|
|
->getRoleRepository() |
169
|
|
|
->findOneBy(['name' => 'admin']); |
170
|
|
|
|
171
|
|
|
/** @var Permission $permission */ |
172
|
|
|
$permission = $this->repositoryRegistry |
173
|
|
|
->getPermissionRepository() |
174
|
|
|
->findOneBy(['name' => 'write']); |
175
|
|
|
|
176
|
|
|
$this->rbacManager->attachPermission($role, $permission); |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* @throws QueryException |
181
|
|
|
* @throws CyclicException |
182
|
|
|
* @throws DatabaseException |
183
|
|
|
* @throws NotUniqueException |
184
|
|
|
* |
185
|
|
|
*/ |
186
|
|
|
public function testCheckAddingSameChildRoleDoubleTime() |
187
|
|
|
{ |
188
|
|
|
$this->expectException(NotUniqueException::class); |
189
|
|
|
|
190
|
|
|
/** @var Role $parent */ |
191
|
|
|
$parent = $this->repositoryRegistry |
192
|
|
|
->getRoleRepository() |
193
|
|
|
->findOneBy(['name' => 'admin']); |
194
|
|
|
|
195
|
|
|
/** @var Role $child */ |
196
|
|
|
$child = $this->repositoryRegistry |
197
|
|
|
->getRoleRepository() |
198
|
|
|
->findOneBy(['name' => 'moderator']); |
199
|
|
|
|
200
|
|
|
$this->rbacManager->attachChildRole($parent, $child); |
201
|
|
|
} |
202
|
|
|
} |
203
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.