1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace ApplicationTest\Model; |
6
|
|
|
|
7
|
|
|
use Application\Model\Booking; |
8
|
|
|
use Application\Model\User; |
9
|
|
|
use PHPUnit\Framework\TestCase; |
10
|
|
|
|
11
|
|
|
class UserTest extends TestCase |
12
|
|
|
{ |
13
|
|
|
public function tearDown(): void |
14
|
|
|
{ |
15
|
|
|
User::setCurrent(null); |
16
|
|
|
} |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @dataProvider providerSetRole |
20
|
|
|
* |
21
|
|
|
* @param string $currentRole |
22
|
|
|
* @param string $oldRole |
23
|
|
|
* @param string $newRole |
24
|
|
|
* @param null|string $exception |
25
|
|
|
*/ |
26
|
|
|
public function testSetRole(string $currentRole, string $oldRole, string $newRole, ?string $exception): void |
27
|
|
|
{ |
28
|
|
|
User::setCurrent(null); |
29
|
|
|
if ($currentRole !== User::ROLE_ANONYMOUS) { |
30
|
|
|
$currentUser = new User($currentRole); |
31
|
|
|
User::setCurrent($currentUser); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
$user2 = new User($oldRole); |
35
|
|
|
|
36
|
|
|
if ($exception) { |
37
|
|
|
$this->expectExceptionMessage($exception); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
$user2->setRole($newRole); |
41
|
|
|
self::assertSame($newRole, $user2->getRole()); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public function providerSetRole(): array |
45
|
|
|
{ |
46
|
|
|
return [ |
47
|
|
|
[User::ROLE_ANONYMOUS, User::ROLE_ADMINISTRATOR, User::ROLE_MEMBER, 'anonymous is not allowed to change role from administrator to member'], |
48
|
|
|
[User::ROLE_ANONYMOUS, User::ROLE_MEMBER, User::ROLE_ADMINISTRATOR, 'anonymous is not allowed to change role from member to administrator'], |
49
|
|
|
|
50
|
|
|
[User::ROLE_MEMBER, User::ROLE_MEMBER, User::ROLE_MEMBER, null], |
51
|
|
|
[User::ROLE_MEMBER, User::ROLE_MEMBER, User::ROLE_ADMINISTRATOR, 'member is not allowed to change role from member to administrator'], |
52
|
|
|
|
53
|
|
|
[User::ROLE_ADMINISTRATOR, User::ROLE_MEMBER, User::ROLE_ADMINISTRATOR, null], |
54
|
|
|
[User::ROLE_ADMINISTRATOR, User::ROLE_ADMINISTRATOR, User::ROLE_MEMBER, null], |
55
|
|
|
]; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @dataProvider providerSetOwner |
60
|
|
|
*/ |
61
|
|
|
public function testSetOwner(?User $currentUser, ?User $originalOwner, ?User $newOwner, ?string $exception = null): void |
62
|
|
|
{ |
63
|
|
|
User::setCurrent($currentUser); |
64
|
|
|
|
65
|
|
|
$subject = new Booking(); |
66
|
|
|
self::assertNull($subject->getOwner()); |
67
|
|
|
|
68
|
|
|
$subject->setOwner($originalOwner); |
69
|
|
|
self::assertSame($originalOwner, $subject->getOwner()); |
70
|
|
|
|
71
|
|
|
if ($exception) { |
72
|
|
|
$this->expectExceptionMessage($exception); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
$subject->setOwner($newOwner); |
76
|
|
|
self::assertSame($newOwner, $subject->getOwner()); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
public function providerSetOwner(): array |
80
|
|
|
{ |
81
|
|
|
$u1 = new User(); |
82
|
|
|
$u1->setLogin('u1'); |
83
|
|
|
$u2 = new User(); |
84
|
|
|
$u2->setLogin('u2'); |
85
|
|
|
$u3 = new User(); |
86
|
|
|
$u3->setLogin('u3'); |
87
|
|
|
$admin = new User(User::ROLE_ADMINISTRATOR); |
88
|
|
|
$admin->setLogin('admin'); |
89
|
|
|
|
90
|
|
|
return [ |
91
|
|
|
'can change nothing' => [null, null, null], |
92
|
|
|
'can set owner for first time' => [null, null, $u3], |
93
|
|
|
'can set owner for first time to myself' => [$u1, null, $u1], |
94
|
|
|
'can set owner for first time even if it is not myself' => [$u1, null, $u3], |
95
|
|
|
'can donate my stuff' => [$u1, $u1, $u3], |
96
|
|
|
'cannot donate stuff that are not mine' => [$u1, $u2, $u3, 'u1 is not allowed to change owner to u3 because it belongs to u2'], |
97
|
|
|
'admin cannot donate stuff that are not mine' => [$admin, $u2, $u3], |
98
|
|
|
]; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
public function providerCanOpenDoor(): array |
102
|
|
|
{ |
103
|
|
|
return [ |
104
|
|
|
'anonymous cannot open' => [ |
105
|
|
|
User::ROLE_ANONYMOUS, |
106
|
|
|
User::STATUS_ACTIVE, |
107
|
|
|
['door1' => true, 'door2' => true, 'door3' => true, 'door4' => true], |
108
|
|
|
['door1' => false, 'door2' => false, 'door3' => false, 'door4' => false], |
109
|
|
|
], |
110
|
|
|
'individual member can open' => [ |
111
|
|
|
User::ROLE_INDIVIDUAL, |
112
|
|
|
User::STATUS_ACTIVE, |
113
|
|
|
['door1' => true, 'door2' => true, 'door3' => true, 'door4' => false], |
114
|
|
|
['door1' => true, 'door2' => true, 'door3' => true, 'door4' => false], |
115
|
|
|
], |
116
|
|
|
'active member can open' => [ |
117
|
|
|
User::ROLE_MEMBER, |
118
|
|
|
User::STATUS_ACTIVE, |
119
|
|
|
['door1' => true, 'door2' => true, 'door3' => true, 'door4' => false], |
120
|
|
|
['door1' => true, 'door2' => true, 'door3' => true, 'door4' => false], |
121
|
|
|
], |
122
|
|
|
'inactive member cannot open' => [ |
123
|
|
|
User::ROLE_MEMBER, |
124
|
|
|
User::STATUS_INACTIVE, |
125
|
|
|
['door1' => true, 'door2' => true, 'door3' => true, 'door4' => false], |
126
|
|
|
['door1' => false, 'door2' => false, 'door3' => false, 'door4' => false], |
127
|
|
|
], |
128
|
|
|
'responsible can open' => [ |
129
|
|
|
User::ROLE_RESPONSIBLE, |
130
|
|
|
User::STATUS_ACTIVE, |
131
|
|
|
['door1' => true, 'door2' => true, 'door3' => true, 'door4' => true], |
132
|
|
|
['door1' => true, 'door2' => true, 'door3' => true, 'door4' => true], |
133
|
|
|
], |
134
|
|
|
'administrator can open' => [ |
135
|
|
|
User::ROLE_ADMINISTRATOR, |
136
|
|
|
User::STATUS_ACTIVE, |
137
|
|
|
['door1' => true, 'door2' => true, 'door3' => true, 'door4' => true], |
138
|
|
|
['door1' => true, 'door2' => true, 'door3' => true, 'door4' => true], |
139
|
|
|
], |
140
|
|
|
]; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* @dataProvider providerCanOpenDoor, |
145
|
|
|
* |
146
|
|
|
* @param string $role |
147
|
|
|
* @param string $status |
148
|
|
|
* @param array $doors |
149
|
|
|
* @param array $result |
150
|
|
|
*/ |
151
|
|
|
public function testCanOpenDoor(string $role, string $status, array $doors, array $result): void |
152
|
|
|
{ |
153
|
|
|
$user = new User($role); |
154
|
|
|
$user->setStatus($status); |
155
|
|
|
foreach ($doors as $door => $value) { |
156
|
|
|
$setter = 'set' . ucfirst($door); |
157
|
|
|
$user->$setter($value); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
foreach ($result as $door => $canOpen) { |
161
|
|
|
self::assertSame($canOpen, $user->getCanOpenDoor($door)); |
162
|
|
|
} |
163
|
|
|
} |
164
|
|
|
} |
165
|
|
|
|