1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Tests\Unit; |
4
|
|
|
|
5
|
|
|
use Potievdev\SlimRbac\Component\AuthManager; |
6
|
|
|
use Potievdev\SlimRbac\Models\Entity\Permission; |
7
|
|
|
use Potievdev\SlimRbac\Models\Entity\Role; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class AuthManagerTest |
11
|
|
|
* @package Tests\Unit |
12
|
|
|
*/ |
13
|
|
|
class AuthManagerTest extends BaseTestCase |
14
|
|
|
{ |
15
|
|
|
|
16
|
|
|
/** @var AuthManager $authManager */ |
17
|
|
|
protected $authManager; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @throws \Potievdev\SlimRbac\Exception\CyclicException |
21
|
|
|
* @throws \Potievdev\SlimRbac\Exception\DatabaseException |
22
|
|
|
* @throws \Potievdev\SlimRbac\Exception\NotUniqueException |
23
|
|
|
* @throws \Doctrine\ORM\Query\QueryException |
24
|
|
|
*/ |
25
|
|
|
public function setUp() |
26
|
|
|
{ |
27
|
|
|
parent::setUp(); |
28
|
|
|
|
29
|
|
|
$this->authManager = new AuthManager($this->authOptions); |
30
|
|
|
$this->authManager->removeAll(); |
31
|
|
|
|
32
|
|
|
$edit = $this->authManager->createPermission('edit'); |
33
|
|
|
$this->authManager->addPermission($edit); |
34
|
|
|
|
35
|
|
|
$write = $this->authManager->createPermission('write'); |
36
|
|
|
$this->authManager->addPermission($write); |
37
|
|
|
|
38
|
|
|
$moderator = $this->authManager->createRole('moderator'); |
39
|
|
|
$this->authManager->addRole($moderator); |
40
|
|
|
|
41
|
|
|
$admin = $this->authManager->createRole('admin'); |
42
|
|
|
$this->authManager->addRole($admin); |
43
|
|
|
|
44
|
|
|
$this->authManager->addChildPermission($moderator, $edit); |
45
|
|
|
$this->authManager->addChildPermission($admin, $write); |
46
|
|
|
$this->authManager->addChildRole($admin, $moderator); |
47
|
|
|
|
48
|
|
|
$this->authManager->assign($moderator, self::MODERATOR_USER_ID); |
49
|
|
|
$this->authManager->assign($admin, self::ADMIN_USER_ID); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Testing has permission cases |
54
|
|
|
* @throws \Doctrine\ORM\Query\QueryException |
55
|
|
|
* @throws \Potievdev\SlimRbac\Exception\InvalidArgumentException |
56
|
|
|
*/ |
57
|
|
|
public function testAccessTrue() |
58
|
|
|
{ |
59
|
|
|
$this->assertTrue($this->authManager->checkAccess(self::MODERATOR_USER_ID, 'edit')); |
60
|
|
|
$this->assertTrue($this->authManager->checkAccess(self::ADMIN_USER_ID, 'edit')); |
61
|
|
|
$this->assertTrue($this->authManager->checkAccess(self::ADMIN_USER_ID, 'write')); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @return array |
66
|
|
|
*/ |
67
|
|
|
public function failCasesProvider() |
68
|
|
|
{ |
69
|
|
|
return [ |
70
|
|
|
[self::MODERATOR_USER_ID, 'write'], |
71
|
|
|
[self::ADMIN_USER_ID, 'none_permission'], |
72
|
|
|
[self::NOT_USER_ID, 'edit'], |
73
|
|
|
[self::NOT_USER_ID, 'admin'], |
74
|
|
|
[self::NOT_USER_ID, 'moderator'], |
75
|
|
|
]; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Testing not have permission cases |
80
|
|
|
* @param integer $userId user id |
81
|
|
|
* @param string $roleOrPermission role or permission name |
82
|
|
|
* @throws \Doctrine\ORM\Query\QueryException |
83
|
|
|
* @throws \Potievdev\SlimRbac\Exception\InvalidArgumentException |
84
|
|
|
* @dataProvider failCasesProvider |
85
|
|
|
*/ |
86
|
|
|
public function testAccessFalse($userId, $roleOrPermission) |
87
|
|
|
{ |
88
|
|
|
$this->assertFalse($this->authManager->checkAccess($userId, $roleOrPermission)); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Testing adding not unique permission |
93
|
|
|
* @expectedException \Potievdev\SlimRbac\Exception\NotUniqueException |
94
|
|
|
* @throws \Potievdev\SlimRbac\Exception\DatabaseException |
95
|
|
|
* @throws \Potievdev\SlimRbac\Exception\NotUniqueException |
96
|
|
|
*/ |
97
|
|
|
public function testNotUniquePermission() |
98
|
|
|
{ |
99
|
|
|
$edit = $this->authManager->createPermission('edit'); |
100
|
|
|
$this->authManager->addPermission($edit); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Testing adding not unique role |
105
|
|
|
* @expectedException \Potievdev\SlimRbac\Exception\NotUniqueException |
106
|
|
|
* @throws \Potievdev\SlimRbac\Exception\DatabaseException |
107
|
|
|
* @throws \Potievdev\SlimRbac\Exception\NotUniqueException |
108
|
|
|
*/ |
109
|
|
|
public function testNonUniqueRole() |
110
|
|
|
{ |
111
|
|
|
$moderator = $this->authManager->createRole('moderator'); |
112
|
|
|
$this->authManager->addRole($moderator); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @expectedException \Potievdev\SlimRbac\Exception\CyclicException |
117
|
|
|
* @throws \Potievdev\SlimRbac\Exception\CyclicException |
118
|
|
|
* @throws \Potievdev\SlimRbac\Exception\DatabaseException |
119
|
|
|
* @throws \Potievdev\SlimRbac\Exception\NotUniqueException |
120
|
|
|
* @throws \Doctrine\ORM\Query\QueryException |
121
|
|
|
*/ |
122
|
|
|
public function testCyclicException() |
123
|
|
|
{ |
124
|
|
|
$a = $this->authManager->createRole('a'); |
125
|
|
|
$b = $this->authManager->createRole('b'); |
126
|
|
|
|
127
|
|
|
$this->authManager->addRole($a); |
128
|
|
|
$this->authManager->addRole($b); |
129
|
|
|
|
130
|
|
|
$this->authManager->addChildRole($a, $b); |
131
|
|
|
$this->authManager->addChildRole($b, $a); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Testing creating permission |
136
|
|
|
*/ |
137
|
|
|
public function testCreatingPermission() |
138
|
|
|
{ |
139
|
|
|
$repositoryRegistry = $this->createRepositoryRegistry(); |
140
|
|
|
$permission = $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 testCreatingRole() |
151
|
|
|
{ |
152
|
|
|
$repositoryRegistry = $this->createRepositoryRegistry(); |
153
|
|
|
|
154
|
|
|
$role = $repositoryRegistry |
155
|
|
|
->getRoleRepository() |
156
|
|
|
->findOneBy(['name' => 'admin']); |
157
|
|
|
|
158
|
|
|
$this->assertTrue($role instanceof Role); |
159
|
|
|
} |
160
|
|
|
} |
161
|
|
|
|