1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace ApplicationTest\Model; |
6
|
|
|
|
7
|
|
|
use Application\Model\Product; |
8
|
|
|
use Application\Model\User; |
9
|
|
|
use PHPUnit\Framework\TestCase; |
10
|
|
|
|
11
|
|
|
class UserTest extends TestCase |
12
|
|
|
{ |
13
|
|
|
protected function tearDown(): void |
14
|
|
|
{ |
15
|
|
|
User::setCurrent(null); |
16
|
|
|
} |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @dataProvider providerSetRole |
20
|
|
|
*/ |
21
|
|
|
public function testSetRole(string $currentRole, string $oldRole, string $newRole, ?string $exception): void |
22
|
|
|
{ |
23
|
|
|
User::setCurrent(null); |
24
|
|
|
if ($currentRole !== User::ROLE_ANONYMOUS) { |
25
|
|
|
$currentUser = new User($currentRole); |
26
|
|
|
User::setCurrent($currentUser); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
$user2 = new User($oldRole); |
30
|
|
|
|
31
|
|
|
if ($exception) { |
32
|
|
|
$this->expectExceptionMessage($exception); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
$user2->setRole($newRole); |
36
|
|
|
self::assertSame($newRole, $user2->getRole()); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function providerSetRole(): array |
40
|
|
|
{ |
41
|
|
|
return [ |
42
|
|
|
[User::ROLE_ANONYMOUS, User::ROLE_ADMINISTRATOR, User::ROLE_MEMBER, 'anonymous is not allowed to change role from administrator to member'], |
43
|
|
|
[User::ROLE_ANONYMOUS, User::ROLE_MEMBER, User::ROLE_ADMINISTRATOR, 'anonymous is not allowed to change role from member to administrator'], |
44
|
|
|
|
45
|
|
|
[User::ROLE_MEMBER, User::ROLE_MEMBER, User::ROLE_MEMBER, null], |
46
|
|
|
[User::ROLE_MEMBER, User::ROLE_MEMBER, User::ROLE_ADMINISTRATOR, 'member is not allowed to change role from member to administrator'], |
47
|
|
|
|
48
|
|
|
[User::ROLE_ADMINISTRATOR, User::ROLE_MEMBER, User::ROLE_ADMINISTRATOR, null], |
49
|
|
|
[User::ROLE_ADMINISTRATOR, User::ROLE_ADMINISTRATOR, User::ROLE_MEMBER, null], |
50
|
|
|
]; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @dataProvider providerSetOwner |
55
|
|
|
*/ |
56
|
|
|
public function testSetOwner(?User $currentUser, ?User $originalOwner, ?User $newOwner, ?string $exception = null): void |
57
|
|
|
{ |
58
|
|
|
User::setCurrent($currentUser); |
59
|
|
|
|
60
|
|
|
$subject = new Product(); |
61
|
|
|
self::assertNull($subject->getOwner()); |
62
|
|
|
|
63
|
|
|
$subject->setOwner($originalOwner); |
64
|
|
|
self::assertSame($originalOwner, $subject->getOwner()); |
65
|
|
|
|
66
|
|
|
if ($exception) { |
67
|
|
|
$this->expectExceptionMessage($exception); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
$subject->setOwner($newOwner); |
71
|
|
|
self::assertSame($newOwner, $subject->getOwner()); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
public function providerSetOwner(): array |
75
|
|
|
{ |
76
|
|
|
$u1 = new User(); |
77
|
|
|
$u1->setFirstName('u1'); |
78
|
|
|
$u2 = new User(); |
79
|
|
|
$u2->setFirstName('u2'); |
80
|
|
|
$u3 = new User(); |
81
|
|
|
$u3->setFirstName('u3'); |
82
|
|
|
$admin = new User(User::ROLE_ADMINISTRATOR); |
83
|
|
|
$admin->setFirstName('admin'); |
84
|
|
|
|
85
|
|
|
return [ |
86
|
|
|
'can change nothing' => [null, null, null], |
87
|
|
|
'can set owner for first time' => [null, null, $u3], |
88
|
|
|
'can set owner for first time to myself' => [$u1, null, $u1], |
89
|
|
|
'can set owner for first time even if it is not myself' => [$u1, null, $u3], |
90
|
|
|
'can donate my stuff' => [$u1, $u1, $u3], |
91
|
|
|
'cannot donate stuff that are not mine' => [$u1, $u2, $u3, 'u1 is not allowed to change owner to u3 because it belongs to u2'], |
92
|
|
|
'admin cannot donate stuff that are not mine' => [$admin, $u2, $u3], |
93
|
|
|
]; |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|