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
|
|
|
public function testGetGlobalPermissions(): void |
19
|
|
|
{ |
20
|
|
|
$user = new User(); |
21
|
|
|
$actual = $user->getGlobalPermissions(); |
22
|
|
|
$expected = [ |
23
|
|
|
'country' => [ |
24
|
|
|
'create' => false, |
25
|
|
|
], |
26
|
|
|
'license' => [ |
27
|
|
|
'create' => false, |
28
|
|
|
], |
29
|
|
|
'user' => [ |
30
|
|
|
'create' => true, |
31
|
|
|
], |
32
|
|
|
]; |
33
|
|
|
|
34
|
|
|
self::assertEquals($expected, $actual); |
35
|
|
|
|
36
|
|
|
$expectedForAdmin = [ |
37
|
|
|
'country' => [ |
38
|
|
|
'create' => false, |
39
|
|
|
], |
40
|
|
|
'license' => [ |
41
|
|
|
'create' => true, |
42
|
|
|
], |
43
|
|
|
'user' => [ |
44
|
|
|
'create' => true, |
45
|
|
|
], |
46
|
|
|
]; |
47
|
|
|
|
48
|
|
|
User::setCurrent($user); |
49
|
|
|
self::assertSame($user, User::getCurrent()); |
50
|
|
|
|
51
|
|
|
$admin = new User(User::ROLE_ADMINISTRATOR); |
52
|
|
|
$actualForAdmin = $admin->getGlobalPermissions(); |
53
|
|
|
|
54
|
|
|
self::assertEquals($expectedForAdmin, $actualForAdmin); |
55
|
|
|
self::assertSame($user, User::getCurrent()); |
56
|
|
|
self::assertNotEquals($expectedForAdmin, $expected); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @dataProvider providerSetRole |
61
|
|
|
* |
62
|
|
|
* @param string $currentRole |
63
|
|
|
* @param string $oldRole |
64
|
|
|
* @param string $newRole |
65
|
|
|
* @param null|string $exception |
66
|
|
|
*/ |
67
|
|
|
public function testSetRole(string $currentRole, string $oldRole, string $newRole, ?string $exception): void |
68
|
|
|
{ |
69
|
|
|
if ($currentRole !== User::ROLE_ANONYMOUS) { |
70
|
|
|
$currentUser = new User($currentRole); |
71
|
|
|
User::setCurrent($currentUser); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
$user2 = new User($oldRole); |
75
|
|
|
|
76
|
|
|
if ($exception) { |
77
|
|
|
$this->expectExceptionMessage($exception); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
$user2->setRole($newRole); |
81
|
|
|
self::assertSame($newRole, $user2->getRole()); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
public function providerSetRole(): array |
85
|
|
|
{ |
86
|
|
|
return [ |
87
|
|
|
[User::ROLE_ANONYMOUS, User::ROLE_ADMINISTRATOR, User::ROLE_MEMBER, 'anonymous is not allowed to change role to member'], |
88
|
|
|
[User::ROLE_ANONYMOUS, User::ROLE_MEMBER, User::ROLE_ADMINISTRATOR, 'anonymous is not allowed to change role to administrator'], |
89
|
|
|
|
90
|
|
|
[User::ROLE_MEMBER, User::ROLE_MEMBER, User::ROLE_MEMBER, null], |
91
|
|
|
[User::ROLE_MEMBER, User::ROLE_MEMBER, User::ROLE_ADMINISTRATOR, 'member is not allowed to change role to administrator'], |
92
|
|
|
|
93
|
|
|
[User::ROLE_ADMINISTRATOR, User::ROLE_MEMBER, User::ROLE_ADMINISTRATOR, null], |
94
|
|
|
[User::ROLE_ADMINISTRATOR, User::ROLE_ADMINISTRATOR, User::ROLE_MEMBER, null], |
95
|
|
|
]; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
public function testSetPassword(): void |
99
|
|
|
{ |
100
|
|
|
$user = new User(); |
101
|
|
|
self::assertNull($user->getPassword(), 'should have no password at first'); |
102
|
|
|
|
103
|
|
|
$user->setPassword('12345'); |
104
|
|
|
$actual1 = $user->getPassword(); |
105
|
|
|
self::assertNotSame('', $actual1, 'should be able to change password '); |
106
|
|
|
self::assertTrue(password_verify('12345', $actual1), 'password must have been hashed'); |
107
|
|
|
|
108
|
|
|
$user->setPassword(''); |
109
|
|
|
$actual2 = $user->getPassword(); |
110
|
|
|
self::assertSame($actual1, $actual2, 'should ignore empty password'); |
111
|
|
|
|
112
|
|
|
$user->setPassword('money'); |
113
|
|
|
$actual3 = $user->getPassword(); |
114
|
|
|
self::assertNotSame($actual1, $actual3, 'should be able to change to something else'); |
115
|
|
|
self::assertTrue(password_verify('money', $actual3), 'password must have been hashed again'); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @dataProvider providerSetOwner |
120
|
|
|
*/ |
121
|
|
|
public function testSetOwner(?User $currentUser, ?User $originalOwner, ?User $newOwner, ?string $exception = null): void |
122
|
|
|
{ |
123
|
|
|
User::setCurrent($currentUser); |
124
|
|
|
|
125
|
|
|
$subject = new Booking(); |
126
|
|
|
self::assertNull($subject->getOwner()); |
127
|
|
|
|
128
|
|
|
$subject->setOwner($originalOwner); |
129
|
|
|
self::assertSame($originalOwner, $subject->getOwner()); |
130
|
|
|
|
131
|
|
|
if ($exception) { |
132
|
|
|
$this->expectExceptionMessage($exception); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
$subject->setOwner($newOwner); |
136
|
|
|
self::assertSame($newOwner, $subject->getOwner()); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
public function providerSetOwner(): array |
140
|
|
|
{ |
141
|
|
|
$u1 = new User(); |
142
|
|
|
$u1->setLogin('u1'); |
143
|
|
|
$u2 = new User(); |
144
|
|
|
$u2->setLogin('u2'); |
145
|
|
|
$u3 = new User(); |
146
|
|
|
$u3->setLogin('u3'); |
147
|
|
|
$admin = new User(User::ROLE_ADMINISTRATOR); |
148
|
|
|
$admin->setLogin('admin'); |
149
|
|
|
|
150
|
|
|
return [ |
151
|
|
|
'can change nothing' => [null, null, null], |
152
|
|
|
'can set owner for first time' => [null, null, $u3], |
153
|
|
|
'can set owner for first time to myself' => [$u1, null, $u1], |
154
|
|
|
'can set owner for first time even if it is not myself' => [$u1, null, $u3], |
155
|
|
|
'can donate my stuff' => [$u1, $u1, $u3], |
156
|
|
|
'cannot donate stuff that are not mine' => [$u1, $u2, $u3, 'u1 is not allowed to change owner to u3 because it belongs to u2'], |
157
|
|
|
'admin cannot donate stuff that are not mine' => [$admin, $u2, $u3], |
158
|
|
|
]; |
159
|
|
|
} |
160
|
|
|
} |
161
|
|
|
|