|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace ApplicationTest\Model; |
|
6
|
|
|
|
|
7
|
|
|
use Application\DBAL\Types\BookingStatusType; |
|
8
|
|
|
use Application\Model\Account; |
|
9
|
|
|
use Application\Model\Booking; |
|
10
|
|
|
use Application\Model\User; |
|
11
|
|
|
use Cake\Chronos\Chronos; |
|
12
|
|
|
use PHPUnit\Framework\TestCase; |
|
13
|
|
|
|
|
14
|
|
|
class UserTest extends TestCase |
|
15
|
|
|
{ |
|
16
|
|
|
protected function tearDown(): void |
|
17
|
|
|
{ |
|
18
|
|
|
User::setCurrent(null); |
|
19
|
|
|
} |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @dataProvider providerSetRole |
|
23
|
|
|
*/ |
|
24
|
|
|
public function testSetRole(string $currentRole, string $oldRole, string $newRole, ?string $exception): void |
|
25
|
|
|
{ |
|
26
|
|
|
User::setCurrent(null); |
|
27
|
|
|
if ($currentRole !== User::ROLE_ANONYMOUS) { |
|
28
|
|
|
$currentUser = new User($currentRole); |
|
29
|
|
|
User::setCurrent($currentUser); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
$user2 = new User($oldRole); |
|
33
|
|
|
|
|
34
|
|
|
if ($exception) { |
|
35
|
|
|
$this->expectExceptionMessage($exception); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
$user2->setRole($newRole); |
|
39
|
|
|
self::assertSame($newRole, $user2->getRole()); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
public function providerSetRole(): array |
|
43
|
|
|
{ |
|
44
|
|
|
return [ |
|
45
|
|
|
[User::ROLE_ANONYMOUS, User::ROLE_ADMINISTRATOR, User::ROLE_MEMBER, 'anonymous is not allowed to change role from administrator to member'], |
|
46
|
|
|
[User::ROLE_ANONYMOUS, User::ROLE_MEMBER, User::ROLE_ADMINISTRATOR, 'anonymous is not allowed to change role from member to administrator'], |
|
47
|
|
|
|
|
48
|
|
|
[User::ROLE_MEMBER, User::ROLE_MEMBER, User::ROLE_MEMBER, null], |
|
49
|
|
|
[User::ROLE_MEMBER, User::ROLE_MEMBER, User::ROLE_ADMINISTRATOR, 'member is not allowed to change role from member to administrator'], |
|
50
|
|
|
|
|
51
|
|
|
[User::ROLE_ADMINISTRATOR, User::ROLE_MEMBER, User::ROLE_ADMINISTRATOR, null], |
|
52
|
|
|
[User::ROLE_ADMINISTRATOR, User::ROLE_ADMINISTRATOR, User::ROLE_MEMBER, null], |
|
53
|
|
|
]; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* @dataProvider providerSetOwner |
|
58
|
|
|
*/ |
|
59
|
|
|
public function testSetOwner(?User $currentUser, ?User $originalOwner, ?User $newOwner, ?string $exception = null): void |
|
60
|
|
|
{ |
|
61
|
|
|
User::setCurrent($currentUser); |
|
62
|
|
|
|
|
63
|
|
|
$subject = new Booking(); |
|
64
|
|
|
self::assertNull($subject->getOwner()); |
|
65
|
|
|
|
|
66
|
|
|
$subject->setOwner($originalOwner); |
|
67
|
|
|
self::assertSame($originalOwner, $subject->getOwner()); |
|
68
|
|
|
|
|
69
|
|
|
if ($exception) { |
|
70
|
|
|
$this->expectExceptionMessage($exception); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
$subject->setOwner($newOwner); |
|
74
|
|
|
self::assertSame($newOwner, $subject->getOwner()); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
public function providerSetOwner(): array |
|
78
|
|
|
{ |
|
79
|
|
|
$u1 = new User(); |
|
80
|
|
|
$u1->setLogin('u1'); |
|
81
|
|
|
$u2 = new User(); |
|
82
|
|
|
$u2->setLogin('u2'); |
|
83
|
|
|
$u3 = new User(); |
|
84
|
|
|
$u3->setLogin('u3'); |
|
85
|
|
|
$admin = new User(User::ROLE_ADMINISTRATOR); |
|
86
|
|
|
$admin->setLogin('admin'); |
|
87
|
|
|
|
|
88
|
|
|
return [ |
|
89
|
|
|
'can change nothing' => [null, null, null], |
|
90
|
|
|
'can set owner for first time' => [null, null, $u3], |
|
91
|
|
|
'can set owner for first time to myself' => [$u1, null, $u1], |
|
92
|
|
|
'can set owner for first time even if it is not myself' => [$u1, null, $u3], |
|
93
|
|
|
'can donate my stuff' => [$u1, $u1, $u3], |
|
94
|
|
|
'cannot donate stuff that are not mine' => [$u1, $u2, $u3, 'u1 is not allowed to change owner to u3 because it belongs to u2'], |
|
95
|
|
|
'admin cannot donate stuff that are not mine' => [$admin, $u2, $u3], |
|
96
|
|
|
]; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
public function providerCanOpenDoor(): array |
|
100
|
|
|
{ |
|
101
|
|
|
return [ |
|
102
|
|
|
'anonymous cannot open' => [ |
|
103
|
|
|
User::ROLE_ANONYMOUS, |
|
104
|
|
|
User::STATUS_ACTIVE, |
|
105
|
|
|
['door1' => true, 'door2' => true, 'door3' => true, 'door4' => true], |
|
106
|
|
|
['door1' => false, 'door2' => false, 'door3' => false, 'door4' => false], |
|
107
|
|
|
], |
|
108
|
|
|
'individual member can open' => [ |
|
109
|
|
|
User::ROLE_INDIVIDUAL, |
|
110
|
|
|
User::STATUS_ACTIVE, |
|
111
|
|
|
['door1' => true, 'door2' => true, 'door3' => true, 'door4' => false], |
|
112
|
|
|
['door1' => true, 'door2' => true, 'door3' => true, 'door4' => false], |
|
113
|
|
|
], |
|
114
|
|
|
'active member can open' => [ |
|
115
|
|
|
User::ROLE_MEMBER, |
|
116
|
|
|
User::STATUS_ACTIVE, |
|
117
|
|
|
['door1' => true, 'door2' => true, 'door3' => true, 'door4' => false], |
|
118
|
|
|
['door1' => true, 'door2' => true, 'door3' => true, 'door4' => false], |
|
119
|
|
|
], |
|
120
|
|
|
'inactive member cannot open' => [ |
|
121
|
|
|
User::ROLE_MEMBER, |
|
122
|
|
|
User::STATUS_INACTIVE, |
|
123
|
|
|
['door1' => true, 'door2' => true, 'door3' => true, 'door4' => false], |
|
124
|
|
|
['door1' => false, 'door2' => false, 'door3' => false, 'door4' => false], |
|
125
|
|
|
], |
|
126
|
|
|
'responsible can open' => [ |
|
127
|
|
|
User::ROLE_RESPONSIBLE, |
|
128
|
|
|
User::STATUS_ACTIVE, |
|
129
|
|
|
['door1' => true, 'door2' => true, 'door3' => true, 'door4' => true], |
|
130
|
|
|
['door1' => true, 'door2' => true, 'door3' => true, 'door4' => true], |
|
131
|
|
|
], |
|
132
|
|
|
'administrator can open' => [ |
|
133
|
|
|
User::ROLE_ADMINISTRATOR, |
|
134
|
|
|
User::STATUS_ACTIVE, |
|
135
|
|
|
['door1' => true, 'door2' => true, 'door3' => true, 'door4' => true], |
|
136
|
|
|
['door1' => true, 'door2' => true, 'door3' => true, 'door4' => true], |
|
137
|
|
|
], |
|
138
|
|
|
]; |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
/** |
|
142
|
|
|
* @dataProvider providerCanOpenDoor, |
|
143
|
|
|
*/ |
|
144
|
|
|
public function testCanOpenDoor(string $role, string $status, array $doors, array $result): void |
|
145
|
|
|
{ |
|
146
|
|
|
$user = new User($role); |
|
147
|
|
|
$user->setStatus($status); |
|
148
|
|
|
foreach ($doors as $door => $value) { |
|
149
|
|
|
$setter = 'set' . ucfirst($door); |
|
150
|
|
|
$user->$setter($value); |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
foreach ($result as $door => $canOpen) { |
|
154
|
|
|
self::assertSame($canOpen, $user->getCanOpenDoor($door)); |
|
155
|
|
|
} |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
public function testGetAccount(): void |
|
159
|
|
|
{ |
|
160
|
|
|
$user1 = new User(); |
|
161
|
|
|
$user2 = new User(); |
|
162
|
|
|
|
|
163
|
|
|
self::assertNull($user1->getAccount()); |
|
164
|
|
|
self::assertNull($user2->getAccount()); |
|
165
|
|
|
|
|
166
|
|
|
$account1 = new Account(); |
|
167
|
|
|
$account2 = new Account(); |
|
168
|
|
|
$account1->setOwner($user1); |
|
169
|
|
|
$account2->setOwner($user2); |
|
170
|
|
|
|
|
171
|
|
|
self::assertSame($account1, $user1->getAccount()); |
|
172
|
|
|
self::assertSame($account2, $user2->getAccount()); |
|
173
|
|
|
|
|
174
|
|
|
$user2->setOwner($user1); |
|
175
|
|
|
|
|
176
|
|
|
self::assertSame($account1, $user1->getAccount()); |
|
177
|
|
|
self::assertSame($account1, $user2->getAccount(), 'user2 should now use user1 account'); |
|
178
|
|
|
|
|
179
|
|
|
User::setCurrent($user1); |
|
180
|
|
|
$user2->setOwner($user2); |
|
181
|
|
|
|
|
182
|
|
|
self::assertSame($account1, $user1->getAccount()); |
|
183
|
|
|
self::assertSame($account2, $user2->getAccount(), 'user2 should be use his own account again'); |
|
184
|
|
|
|
|
185
|
|
|
User::setCurrent($user2); |
|
186
|
|
|
$user2->setOwner(null); |
|
187
|
|
|
|
|
188
|
|
|
self::assertSame($account1, $user1->getAccount()); |
|
189
|
|
|
self::assertSame($account2, $user2->getAccount(), 'user2 should be use his own account again'); |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
|
|
public function testGetRunningBookings(): void |
|
193
|
|
|
{ |
|
194
|
|
|
$user = new User(); |
|
195
|
|
|
|
|
196
|
|
|
$unapproved = new Booking(); |
|
197
|
|
|
$unapproved->setOwner($user); |
|
198
|
|
|
$unapproved->setStatus(BookingStatusType::APPLICATION); |
|
199
|
|
|
|
|
200
|
|
|
$completed = new Booking(); |
|
201
|
|
|
$completed->setOwner($user); |
|
202
|
|
|
$completed->setStatus(BookingStatusType::BOOKED); |
|
203
|
|
|
$completed->setEndDate(Chronos::yesterday()); |
|
204
|
|
|
|
|
205
|
|
|
$running = new Booking(); |
|
206
|
|
|
$running->setOwner($user); |
|
207
|
|
|
$running->setStatus(BookingStatusType::BOOKED); |
|
208
|
|
|
|
|
209
|
|
|
$runnings = $user->getRunningBookings(); |
|
210
|
|
|
|
|
211
|
|
|
self::assertCount(1, $runnings); |
|
212
|
|
|
} |
|
213
|
|
|
} |
|
214
|
|
|
|