1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the EloyekunlePermissionsBundle package. |
5
|
|
|
* |
6
|
|
|
* (c) Elijah Oyekunle <https://elijahoyekunle.com/> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Eloyekunle\PermissionsBundle\Tests\Model; |
13
|
|
|
|
14
|
|
|
use Eloyekunle\PermissionsBundle\Model\RoleInterface; |
15
|
|
|
use PHPUnit\Framework\TestCase; |
16
|
|
|
|
17
|
|
|
class RoleTest extends TestCase |
18
|
|
|
{ |
19
|
|
|
public function testName() |
20
|
|
|
{ |
21
|
|
|
$role = $this->getRole(); |
22
|
|
|
$this->assertNull($role->getRole()); |
23
|
|
|
|
24
|
|
|
$role->setRole('System Administrator'); |
25
|
|
|
$this->assertSame('System Administrator', $role->getRole()); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
public function testGrantPermission() |
29
|
|
|
{ |
30
|
|
|
$role = $this->getRole(); |
31
|
|
|
$this->assertFalse($role->hasPermission('Administer Systems')); |
32
|
|
|
|
33
|
|
|
$role->grantPermission('Administer Systems'); |
34
|
|
|
$this->assertTrue($role->hasPermission('Administer Systems')); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public function testRevokePermission() |
38
|
|
|
{ |
39
|
|
|
$role = $this->getRole(); |
40
|
|
|
$role->grantPermission('Administer Systems'); |
41
|
|
|
$this->assertTrue($role->hasPermission('Administer Systems')); |
42
|
|
|
|
43
|
|
|
$role->revokePermission('Administer Systems'); |
44
|
|
|
$this->assertFalse($role->hasPermission('Administer Systems')); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public function testSuperAdminPermissions() |
48
|
|
|
{ |
49
|
|
|
$role = $this->getRole(); |
50
|
|
|
$this->assertFalse($role->hasPermission('Administer Systems')); |
51
|
|
|
|
52
|
|
|
$role->setRole(RoleInterface::ROLE_SUPER_ADMIN); |
53
|
|
|
$this->assertTrue($role->hasPermission('Administer Systems')); |
54
|
|
|
$role->grantPermission('Play with Cats'); |
55
|
|
|
$this->assertSame([], $role->getPermissions()); |
56
|
|
|
$role->revokePermission('Play with Cats'); |
57
|
|
|
$this->assertSame([], $role->getPermissions()); |
58
|
|
|
|
59
|
|
|
$role->setRole('Not Admin'); |
60
|
|
|
$this->assertFalse($role->hasPermission('Administer Systems')); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public function testSetPermissions() |
64
|
|
|
{ |
65
|
|
|
$role = $this->getRole(); |
66
|
|
|
$this->assertFalse($role->hasPermission('Administer Systems')); |
67
|
|
|
$this->assertFalse($role->hasPermission('View Reports')); |
68
|
|
|
|
69
|
|
|
$permissions = ['Administer Systems', 'View Reports', '']; |
70
|
|
|
$role->setPermissions($permissions); |
71
|
|
|
$this->assertTrue($role->hasPermission('Administer Systems')); |
72
|
|
|
$this->assertTrue($role->hasPermission('View Reports')); |
73
|
|
|
$this->assertSame(2, count($role->getPermissions())); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @return RoleInterface |
78
|
|
|
* |
79
|
|
|
* @throws \ReflectionException |
80
|
|
|
*/ |
81
|
|
|
protected function getRole() |
82
|
|
|
{ |
83
|
|
|
return $this->getMockForAbstractClass('Eloyekunle\PermissionsBundle\Model\Role'); |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|