UserTest   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 177
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 7
eloc 110
dl 0
loc 177
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A tearDown() 0 3 1
A testSetRole() 0 15 3
B testGetGlobalPermissions() 0 113 1
A testSetPassword() 0 18 1
A providerSetRole() 0 15 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace ApplicationTest\Model;
6
7
use Application\Enum\Site;
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
    public function testGetGlobalPermissions(): void
19
    {
20
        $user = new User();
21
        $user->setSite(Site::Dilps);
22
        $actual = $user->getGlobalPermissions();
23
        $expected = [
24
            'artist' => [
25
                'create' => true,
26
            ],
27
            'card' => [
28
                'create' => true,
29
            ],
30
            'change' => [
31
                'create' => true,
32
            ],
33
            'collection' => [
34
                'create' => true,
35
            ],
36
            'country' => [
37
                'create' => false,
38
            ],
39
            'dating' => [
40
                'create' => false,
41
            ],
42
            'institution' => [
43
                'create' => true,
44
            ],
45
            'tag' => [
46
                'create' => true,
47
            ],
48
            'user' => [
49
                'create' => false,
50
            ],
51
            'domain' => [
52
                'create' => false,
53
            ],
54
            'documentType' => [
55
                'create' => false,
56
            ],
57
            'news' => [
58
                'create' => false,
59
            ],
60
            'period' => [
61
                'create' => false,
62
            ],
63
            'material' => [
64
                'create' => false,
65
            ],
66
            'antiqueName' => [
67
                'create' => false,
68
            ],
69
        ];
70
71
        self::assertEquals($expected, $actual);
72
73
        $expectedForAdmin = [
74
            'artist' => [
75
                'create' => true,
76
            ],
77
            'card' => [
78
                'create' => true,
79
            ],
80
            'change' => [
81
                'create' => true,
82
            ],
83
            'collection' => [
84
                'create' => true,
85
            ],
86
            'country' => [
87
                'create' => false,
88
            ],
89
            'dating' => [
90
                'create' => false,
91
            ],
92
            'institution' => [
93
                'create' => true,
94
            ],
95
            'tag' => [
96
                'create' => true,
97
            ],
98
            'user' => [
99
                'create' => true,
100
            ],
101
            'domain' => [
102
                'create' => true,
103
            ],
104
            'documentType' => [
105
                'create' => true,
106
            ],
107
            'news' => [
108
                'create' => true,
109
            ],
110
            'period' => [
111
                'create' => true,
112
            ],
113
            'material' => [
114
                'create' => true,
115
            ],
116
            'antiqueName' => [
117
                'create' => true,
118
            ],
119
        ];
120
121
        User::setCurrent($user);
122
        self::assertSame($user, User::getCurrent());
123
124
        $admin = new User(User::ROLE_ADMINISTRATOR);
125
        $admin->setSite(Site::Dilps);
126
        $actualForAdmin = $admin->getGlobalPermissions();
127
128
        self::assertEquals($expectedForAdmin, $actualForAdmin);
129
        self::assertSame($user, User::getCurrent());
130
        self::assertNotEquals($expectedForAdmin, $expected);
131
    }
132
133
    /**
134
     * @dataProvider providerSetRole
135
     */
136
    public function testSetRole(string $currentRole, string $oldRole, string $newRole, ?string $exception): void
137
    {
138
        if ($currentRole !== User::ROLE_ANONYMOUS) {
139
            $currentUser = new User($currentRole);
140
            User::setCurrent($currentUser);
141
        }
142
143
        $user2 = new User($oldRole);
144
145
        if ($exception) {
146
            $this->expectExceptionMessage($exception);
147
        }
148
149
        $user2->setRole($newRole);
150
        self::assertSame($newRole, $user2->getRole());
151
    }
152
153
    public function providerSetRole(): iterable
154
    {
155
        yield [User::ROLE_ANONYMOUS, User::ROLE_STUDENT, User::ROLE_JUNIOR, 'anonymous is not allowed to change role from student to junior'];
156
        yield [User::ROLE_STUDENT, User::ROLE_STUDENT, User::ROLE_STUDENT, null];
157
        yield [User::ROLE_STUDENT, User::ROLE_STUDENT, User::ROLE_JUNIOR, 'student is not allowed to change role from student to junior'];
158
        yield [User::ROLE_JUNIOR, User::ROLE_STUDENT, User::ROLE_JUNIOR, null];
159
        yield [User::ROLE_JUNIOR, User::ROLE_JUNIOR, User::ROLE_JUNIOR, null];
160
        yield 'cannot promote higher than us' => [User::ROLE_JUNIOR, User::ROLE_JUNIOR, User::ROLE_SENIOR, 'junior is not allowed to change role from junior to senior'];
161
        yield 'cannot demote' => [User::ROLE_JUNIOR, User::ROLE_SENIOR, User::ROLE_JUNIOR, 'junior is not allowed to change role from senior to junior'];
162
        yield [User::ROLE_MAJOR, User::ROLE_JUNIOR, User::ROLE_SENIOR, null];
163
        yield [User::ROLE_MAJOR, User::ROLE_JUNIOR, User::ROLE_MAJOR, null];
164
        yield [User::ROLE_ADMINISTRATOR, User::ROLE_JUNIOR, User::ROLE_SENIOR, null];
165
        yield [User::ROLE_ADMINISTRATOR, User::ROLE_JUNIOR, User::ROLE_MAJOR, null];
166
        yield [User::ROLE_ADMINISTRATOR, User::ROLE_JUNIOR, User::ROLE_ADMINISTRATOR, null];
167
        yield [User::ROLE_ADMINISTRATOR, User::ROLE_ADMINISTRATOR, User::ROLE_STUDENT, null];
168
    }
169
170
    public function testSetPassword(): void
171
    {
172
        $user = new User();
173
        self::assertSame('', $user->getPassword(), 'should have no password at first');
174
175
        $user->setPassword('12345');
176
        $actual1 = $user->getPassword();
177
        self::assertNotSame('', $actual1, 'should be able to change password ');
178
        self::assertTrue(password_verify('12345', $actual1), 'password must have been hashed');
179
180
        $user->setPassword('');
181
        $actual2 = $user->getPassword();
182
        self::assertSame($actual1, $actual2, 'should ignore empty password');
183
184
        $user->setPassword('money');
185
        $actual3 = $user->getPassword();
186
        self::assertNotSame($actual1, $actual3, 'should be able to change to something else');
187
        self::assertTrue(password_verify('money', $actual3), 'password must have been hashed again');
188
    }
189
}
190