1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
/* |
3
|
|
|
* This file is part of FlexPHP. |
4
|
|
|
* |
5
|
|
|
* (c) Freddie Gar <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
namespace FlexPHP\GRBAC\Tests\Unit; |
11
|
|
|
|
12
|
|
|
use FlexPHP\GRBAC\Permission; |
13
|
|
|
use FlexPHP\GRBAC\Role; |
14
|
|
|
use FlexPHP\GRBAC\RoleInterface; |
15
|
|
|
use FlexPHP\GRBAC\Tests\TestCase; |
16
|
|
|
|
17
|
|
|
final class RoleTest extends TestCase |
18
|
|
|
{ |
19
|
|
|
public function testItCreate(): void |
20
|
|
|
{ |
21
|
|
|
$name = 'ROL'; |
22
|
|
|
$role = new Role($name); |
23
|
|
|
|
24
|
|
|
$this->assertInstanceOf(RoleInterface::class, $role); |
25
|
|
|
$this->assertEquals($name, $role->name()); |
26
|
|
|
$this->assertEquals(null, $role->description()); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
public function testItCreateWithDescription(): void |
30
|
|
|
{ |
31
|
|
|
$name = 'ROL'; |
32
|
|
|
$description = 'My cool role!'; |
33
|
|
|
$role = new Role($name, $description); |
34
|
|
|
|
35
|
|
|
$this->assertInstanceOf(RoleInterface::class, $role); |
36
|
|
|
$this->assertEquals($name, $role->name()); |
37
|
|
|
$this->assertEquals($description, $role->description()); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public function testItGrantPermission(): void |
41
|
|
|
{ |
42
|
|
|
$permission = new Permission('user.read'); |
43
|
|
|
|
44
|
|
|
$role = new Role('ROL' . __LINE__); |
45
|
|
|
$role->grant($permission); |
46
|
|
|
|
47
|
|
|
$this->assertTrue($role->allow($permission->slug())); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public function testItRevokePermission(): void |
51
|
|
|
{ |
52
|
|
|
$permission = new Permission('user.read'); |
53
|
|
|
|
54
|
|
|
$role = new Role('ROL' . __LINE__); |
55
|
|
|
$role->revoke($permission); |
56
|
|
|
|
57
|
|
|
$this->assertFalse($role->allow($permission->slug())); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public function testItDenyPermission(): void |
61
|
|
|
{ |
62
|
|
|
$permission = new Permission('user.read'); |
63
|
|
|
|
64
|
|
|
$role = new Role('ROL' . __LINE__); |
65
|
|
|
$role->deny($permission); |
66
|
|
|
|
67
|
|
|
$this->assertFalse($role->allow($permission->slug())); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public function testItAllowAnyPermission(): void |
71
|
|
|
{ |
72
|
|
|
$userRead = new Permission('user.read'); |
73
|
|
|
$userCreate = new Permission('user.create'); |
74
|
|
|
|
75
|
|
|
$role = new Role('ROL' . __LINE__); |
76
|
|
|
$role->grant($userRead); |
77
|
|
|
$role->grant($userCreate); |
78
|
|
|
|
79
|
|
|
$this->assertTrue($role->allowAny([$userRead->slug()])); |
80
|
|
|
$this->assertTrue($role->allowAny([$userCreate->slug()])); |
81
|
|
|
$this->assertTrue($role->allowAny([$userRead->slug(), $userCreate->slug()])); |
82
|
|
|
|
83
|
|
|
$role->deny($userRead); |
84
|
|
|
|
85
|
|
|
$this->assertFalse($role->allowAny([$userRead->slug()])); |
86
|
|
|
$this->assertTrue($role->allowAny([$userCreate->slug()])); |
87
|
|
|
$this->assertTrue($role->allowAny([$userRead->slug(), $userCreate->slug()])); |
88
|
|
|
|
89
|
|
|
$role->deny($userCreate); |
90
|
|
|
|
91
|
|
|
$this->assertFalse($role->allowAny([$userRead->slug()])); |
92
|
|
|
$this->assertFalse($role->allowAny([$userCreate->slug()])); |
93
|
|
|
$this->assertFalse($role->allowAny([$userRead->slug(), $userCreate->slug()])); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
public function testItAllowAllPermission(): void |
97
|
|
|
{ |
98
|
|
|
$userRead = new Permission('user.read'); |
99
|
|
|
$userCreate = new Permission('user.create'); |
100
|
|
|
|
101
|
|
|
$role = new Role('ROL' . __LINE__); |
102
|
|
|
$role->deny($userRead); |
103
|
|
|
$role->deny($userCreate); |
104
|
|
|
|
105
|
|
|
$this->assertFalse($role->allowAll([$userRead->slug()])); |
106
|
|
|
$this->assertFalse($role->allowAll([$userCreate->slug()])); |
107
|
|
|
$this->assertFalse($role->allowAll([$userRead->slug(), $userCreate->slug()])); |
108
|
|
|
|
109
|
|
|
$role->grant($userRead); |
110
|
|
|
|
111
|
|
|
$this->assertTrue($role->allowAll([$userRead->slug()])); |
112
|
|
|
$this->assertFalse($role->allowAll([$userCreate->slug()])); |
113
|
|
|
$this->assertFalse($role->allowAll([$userRead->slug(), $userCreate->slug()])); |
114
|
|
|
|
115
|
|
|
$role->grant($userCreate); |
116
|
|
|
|
117
|
|
|
$this->assertTrue($role->allowAll([$userRead->slug()])); |
118
|
|
|
$this->assertTrue($role->allowAll([$userCreate->slug()])); |
119
|
|
|
$this->assertTrue($role->allowAll([$userRead->slug(), $userCreate->slug()])); |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|