1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Containers\Authorization\UI\API\Tests\Functional; |
4
|
|
|
|
5
|
|
|
use App\Containers\Authorization\Models\Permission; |
6
|
|
|
use App\Containers\Authorization\Models\Role; |
7
|
|
|
use App\Port\Test\PHPUnit\Abstracts\TestCase; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class DetachPermissionsFromRoleTest. |
11
|
|
|
* |
12
|
|
|
* @author Mahmoud Zalt <[email protected]> |
13
|
|
|
*/ |
14
|
|
|
class DetachPermissionsFromRoleTest extends TestCase |
15
|
|
|
{ |
16
|
|
|
|
17
|
|
|
protected $endpoint = '/permissions/detach'; |
18
|
|
|
|
19
|
|
|
protected $access = [ |
20
|
|
|
'roles' => 'admin', |
21
|
|
|
'permissions' => '', |
22
|
|
|
]; |
23
|
|
|
|
24
|
|
View Code Duplication |
public function testDetachSinglePermissionFromRole_() |
|
|
|
|
25
|
|
|
{ |
26
|
|
|
$this->getTestingAdmin(); |
27
|
|
|
|
28
|
|
|
$permissionA = Permission::create([ |
29
|
|
|
'name' => 'permission-A', |
30
|
|
|
'description' => 'AA', |
31
|
|
|
'display_name' => 'A', |
32
|
|
|
]); |
33
|
|
|
|
34
|
|
|
$roleA = Role::create([ |
35
|
|
|
'name' => 'role-A', |
36
|
|
|
'description' => 'AA', |
37
|
|
|
'display_name' => 'A', |
38
|
|
|
]); |
39
|
|
|
|
40
|
|
|
$roleA->givePermissionTo($permissionA); |
41
|
|
|
|
42
|
|
|
$data = [ |
43
|
|
|
'role_name' => $roleA['name'], |
44
|
|
|
'permission_name' => $permissionA['name'], |
45
|
|
|
]; |
46
|
|
|
|
47
|
|
|
// send the HTTP request |
48
|
|
|
$response = $this->apiCall($this->endpoint, 'post', $data, true); |
49
|
|
|
|
50
|
|
|
// assert response status is correct |
51
|
|
|
$this->assertEquals('200', $response->getStatusCode()); |
52
|
|
|
|
53
|
|
|
$responseObject = $this->getResponseObject($response); |
54
|
|
|
|
55
|
|
|
$this->assertEquals($roleA['name'], $responseObject->data->name); |
56
|
|
|
|
57
|
|
|
$this->missingFromDatabase('role_has_permissions', [ |
58
|
|
|
'permission_id' => $permissionA->id, |
59
|
|
|
'role_id' => $roleA->id |
60
|
|
|
]); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public function testDetachMultiplePermissionFromRole_() |
64
|
|
|
{ |
65
|
|
|
$this->getTestingAdmin(); |
66
|
|
|
|
67
|
|
|
$permissionA = Permission::create([ |
68
|
|
|
'name' => 'permission-A', |
69
|
|
|
'description' => 'AA', |
70
|
|
|
'display_name' => 'A', |
71
|
|
|
]); |
72
|
|
|
|
73
|
|
|
$permissionB = Permission::create([ |
74
|
|
|
'name' => 'permission-B', |
75
|
|
|
'description' => 'BB', |
76
|
|
|
'display_name' => 'B', |
77
|
|
|
]); |
78
|
|
|
|
79
|
|
|
$roleA = Role::create([ |
80
|
|
|
'name' => 'role-A', |
81
|
|
|
'description' => 'AA', |
82
|
|
|
'display_name' => 'A', |
83
|
|
|
]); |
84
|
|
|
|
85
|
|
|
$roleA->givePermissionTo($permissionA); |
86
|
|
|
$roleA->givePermissionTo($permissionB); |
87
|
|
|
|
88
|
|
|
$data = [ |
89
|
|
|
'role_name' => $roleA['name'], |
90
|
|
|
'permission_name' => [$permissionA['name'], $permissionB['name']] |
91
|
|
|
]; |
92
|
|
|
|
93
|
|
|
// send the HTTP request |
94
|
|
|
$response = $this->apiCall($this->endpoint, 'post', $data, true); |
95
|
|
|
|
96
|
|
|
// assert response status is correct |
97
|
|
|
$this->assertEquals('200', $response->getStatusCode()); |
98
|
|
|
|
99
|
|
|
$responseObject = $this->getResponseObject($response); |
|
|
|
|
100
|
|
|
|
101
|
|
|
$responseObject = $this->getResponseObject($response); |
102
|
|
|
|
103
|
|
|
$this->assertEquals($roleA['name'], $responseObject->data->name); |
104
|
|
|
|
105
|
|
|
$this->missingFromDatabase('role_has_permissions', [ |
106
|
|
|
'permission_id' => $permissionA->id, |
107
|
|
|
'permission_id' => $permissionB->id, |
108
|
|
|
'role_id' => $roleA->id |
109
|
|
|
]); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
|
113
|
|
|
} |
114
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.