1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Containers\Order\UI\API\Tests\Functional; |
4
|
|
|
|
5
|
|
|
use App\Containers\User\Models\User; |
6
|
|
|
use App\Port\Test\PHPUnit\Abstracts\TestCase; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Class AssignUserToRoleTest. |
10
|
|
|
* |
11
|
|
|
* @author Mahmoud Zalt <[email protected]> |
12
|
|
|
*/ |
13
|
|
|
class AssignUserToRoleTest extends TestCase |
14
|
|
|
{ |
15
|
|
|
|
16
|
|
|
protected $endpoint = '/roles/assign'; |
17
|
|
|
|
18
|
|
|
protected $permissions = [ |
19
|
|
|
'admin-access' // no need to set `admin-access` since it's given to the admins by default while seeding. |
20
|
|
|
]; |
21
|
|
|
|
22
|
|
|
public function testAssignUserToRole_() |
23
|
|
|
{ |
24
|
|
|
$this->getLoggedInTestingAdmin(); |
25
|
|
|
|
26
|
|
|
$randomUser = factory(User::class)->create(); |
27
|
|
|
|
28
|
|
|
$data = [ |
29
|
|
|
'roles_names' => 'admin', |
30
|
|
|
'user_id' => $randomUser->getHashedKey(), |
31
|
|
|
]; |
32
|
|
|
|
33
|
|
|
// send the HTTP request |
34
|
|
|
$response = $this->apiCall($this->endpoint, 'post', $data, true); |
35
|
|
|
|
36
|
|
|
// assert response status is correct |
37
|
|
|
$this->assertEquals($response->getStatusCode(), '200'); |
38
|
|
|
|
39
|
|
|
$responseObject = $this->getResponseObject($response); |
40
|
|
|
|
41
|
|
|
$this->assertEquals($data['user_id'], $responseObject->data->id); |
42
|
|
|
|
43
|
|
|
$this->assertEquals($data['roles_names'], $responseObject->data->roles->data[0]->name); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function testAssignUserToRoleWithRealId_() |
47
|
|
|
{ |
48
|
|
|
$this->getLoggedInTestingAdmin(); |
49
|
|
|
|
50
|
|
|
$randomUser = factory(User::class)->create(); |
51
|
|
|
|
52
|
|
|
$data = [ |
53
|
|
|
'roles_names' => 'admin', |
54
|
|
|
'user_id' => $randomUser->id, |
55
|
|
|
]; |
56
|
|
|
|
57
|
|
|
// send the HTTP request |
58
|
|
|
$response = $this->apiCall($this->endpoint, 'post', $data, true); |
59
|
|
|
|
60
|
|
|
// assert response status is correct |
61
|
|
|
$this->assertEquals($response->getStatusCode(), '400'); |
62
|
|
|
|
63
|
|
|
$this->assertResponseContainKeyValue([ |
64
|
|
|
'message' => 'Only Hashed ID\'s allowed to be passed.', |
65
|
|
|
], $response); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
|
69
|
|
|
public function testAssignUserToManyRoles_() |
70
|
|
|
{ |
71
|
|
|
$admin = $this->getLoggedInTestingAdmin(); |
|
|
|
|
72
|
|
|
|
73
|
|
|
$randomUser = factory(User::class)->create(); |
74
|
|
|
|
75
|
|
|
$data = [ |
76
|
|
|
'roles_names' => ['admin', 'client'], |
77
|
|
|
'user_id' => $randomUser->getHashedKey(), |
78
|
|
|
]; |
79
|
|
|
|
80
|
|
|
// send the HTTP request |
81
|
|
|
$response = $this->apiCall($this->endpoint, 'post', $data, true); |
82
|
|
|
|
83
|
|
|
// assert response status is correct |
84
|
|
|
$this->assertEquals($response->getStatusCode(), '200'); |
85
|
|
|
|
86
|
|
|
$responseObject = $this->getResponseObject($response); |
87
|
|
|
|
88
|
|
|
$this->assertTrue(count($responseObject->data->roles->data) > 1); |
89
|
|
|
|
90
|
|
|
$this->assertEquals($data['roles_names'][0], $responseObject->data->roles->data[0]->name); |
91
|
|
|
|
92
|
|
|
$this->assertEquals($data['roles_names'][1], $responseObject->data->roles->data[1]->name); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
} |
96
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.