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
|
|
|
|
31
|
|
|
$edit = $this->authManager->createPermission('edit'); |
32
|
|
|
$this->authManager->addPermission($edit); |
33
|
|
|
|
34
|
|
|
$write = $this->authManager->createPermission('write'); |
35
|
|
|
$this->authManager->addPermission($write); |
36
|
|
|
|
37
|
|
|
$moderator = $this->authManager->createRole('moderator'); |
38
|
|
|
$this->authManager->addRole($moderator); |
39
|
|
|
|
40
|
|
|
$admin = $this->authManager->createRole('admin'); |
41
|
|
|
$this->authManager->addRole($admin); |
42
|
|
|
|
43
|
|
|
$this->authManager->addChildPermission($moderator, $edit); |
44
|
|
|
$this->authManager->addChildPermission($admin, $write); |
45
|
|
|
$this->authManager->addChildRole($admin, $moderator); |
46
|
|
|
|
47
|
|
|
$this->authManager->assign($moderator, self::MODERATOR_USER_ID); |
48
|
|
|
$this->authManager->assign($admin, self::ADMIN_USER_ID); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Testing has permission cases |
53
|
|
|
* @throws \Doctrine\ORM\Query\QueryException |
54
|
|
|
* @throws \Potievdev\SlimRbac\Exception\InvalidArgumentException |
55
|
|
|
*/ |
56
|
|
|
public function testAccessTrue() |
57
|
|
|
{ |
58
|
|
|
$this->assertTrue($this->authManager->checkAccess(self::MODERATOR_USER_ID, 'edit')); |
59
|
|
|
$this->assertTrue($this->authManager->checkAccess(self::ADMIN_USER_ID, 'edit')); |
60
|
|
|
$this->assertTrue($this->authManager->checkAccess(self::ADMIN_USER_ID, 'write')); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @return array |
65
|
|
|
*/ |
66
|
|
|
public function failCasesProvider() |
67
|
|
|
{ |
68
|
|
|
return [ |
69
|
|
|
[self::MODERATOR_USER_ID, 'write'], |
70
|
|
|
[self::ADMIN_USER_ID, 'none_permission'], |
71
|
|
|
[self::NOT_USER_ID, 'edit'], |
72
|
|
|
[self::NOT_USER_ID, 'admin'], |
73
|
|
|
[self::NOT_USER_ID, 'moderator'], |
74
|
|
|
]; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Testing not have permission cases |
79
|
|
|
* @param integer $userId user id |
80
|
|
|
* @param string $roleOrPermission role or permission name |
81
|
|
|
* @throws \Doctrine\ORM\Query\QueryException |
82
|
|
|
* @throws \Potievdev\SlimRbac\Exception\InvalidArgumentException |
83
|
|
|
* @dataProvider failCasesProvider |
84
|
|
|
*/ |
85
|
|
|
public function testAccessFalse($userId, $roleOrPermission) |
86
|
|
|
{ |
87
|
|
|
$this->assertFalse($this->authManager->checkAccess($userId, $roleOrPermission)); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Testing adding not unique permission |
92
|
|
|
* @expectedException \Potievdev\SlimRbac\Exception\NotUniqueException |
93
|
|
|
* @throws \Potievdev\SlimRbac\Exception\DatabaseException |
94
|
|
|
* @throws \Potievdev\SlimRbac\Exception\NotUniqueException |
95
|
|
|
*/ |
96
|
|
|
public function testNotUniquePermission() |
97
|
|
|
{ |
98
|
|
|
$edit = $this->authManager->createPermission('edit'); |
99
|
|
|
$this->authManager->addPermission($edit); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Testing adding not unique role |
104
|
|
|
* @expectedException \Potievdev\SlimRbac\Exception\NotUniqueException |
105
|
|
|
* @throws \Potievdev\SlimRbac\Exception\DatabaseException |
106
|
|
|
* @throws \Potievdev\SlimRbac\Exception\NotUniqueException |
107
|
|
|
*/ |
108
|
|
|
public function testNonUniqueRole() |
109
|
|
|
{ |
110
|
|
|
$moderator = $this->authManager->createRole('moderator'); |
111
|
|
|
$this->authManager->addRole($moderator); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @expectedException \Potievdev\SlimRbac\Exception\CyclicException |
116
|
|
|
* @throws \Potievdev\SlimRbac\Exception\CyclicException |
117
|
|
|
* @throws \Potievdev\SlimRbac\Exception\DatabaseException |
118
|
|
|
* @throws \Potievdev\SlimRbac\Exception\NotUniqueException |
119
|
|
|
* @throws \Doctrine\ORM\Query\QueryException |
120
|
|
|
*/ |
121
|
|
|
public function testCyclicException() |
122
|
|
|
{ |
123
|
|
|
$a = $this->authManager->createRole('a'); |
124
|
|
|
$b = $this->authManager->createRole('b'); |
125
|
|
|
|
126
|
|
|
$this->authManager->addRole($a); |
127
|
|
|
$this->authManager->addRole($b); |
128
|
|
|
|
129
|
|
|
$this->authManager->addChildRole($a, $b); |
130
|
|
|
$this->authManager->addChildRole($b, $a); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Testing creating permission |
135
|
|
|
*/ |
136
|
|
|
public function testCreatingPermission() |
137
|
|
|
{ |
138
|
|
|
$repositoryRegistry = $this->createRepositoryRegistry(); |
139
|
|
|
$permission = $repositoryRegistry |
140
|
|
|
->getPermissionRepository() |
141
|
|
|
->findOneBy(['name' => 'edit']); |
142
|
|
|
|
143
|
|
|
$this->assertTrue($permission instanceof Permission); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* Testing creating role |
148
|
|
|
*/ |
149
|
|
|
public function testCreatingRole() |
150
|
|
|
{ |
151
|
|
|
$repositoryRegistry = $this->createRepositoryRegistry(); |
152
|
|
|
|
153
|
|
|
$role = $repositoryRegistry |
154
|
|
|
->getRoleRepository() |
155
|
|
|
->findOneBy(['name' => 'admin']); |
156
|
|
|
|
157
|
|
|
$this->assertTrue($role instanceof Role); |
158
|
|
|
} |
159
|
|
|
} |
160
|
|
|
|