Completed
Push — master ( 56af9f...386ab4 )
by Adrien
13:07
created

UserTest::testGetGlobalPermissions()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 106
Code Lines 68

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 68
nc 1
nop 0
dl 0
loc 106
rs 8.6981
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
            'account' => [
24
                'create' => true,
25
            ],
26
            'accountingDocument' => [
27
                'create' => true,
28
            ],
29
            'bookable' => [
30
                'create' => false,
31
            ],
32
            'bookableMetadata' => [
33
                'create' => false,
34
            ],
35
            'bookableTag' => [
36
                'create' => false,
37
            ],
38
            'booking' => [
39
                'create' => true,
40
            ],
41
            'category' => [
42
                'create' => false,
43
            ],
44
            'country' => [
45
                'create' => false,
46
            ],
47
            'expenseClaim' => [
48
                'create' => true,
49
            ],
50
            'image' => [
51
                'create' => false,
52
            ],
53
            'license' => [
54
                'create' => false,
55
            ],
56
            'message' => [
57
                'create' => false,
58
            ],
59
            'user' => [
60
                'create' => true,
61
            ],
62
            'userTag' => [
63
                'create' => false,
64
            ],
65
66
        ];
67
        self::assertEquals($expected, $actual);
68
69
        $expectedForAdmin = [
70
            'account' => [
71
                'create' => true,
72
            ],
73
            'accountingDocument' => [
74
                'create' => true,
75
            ],
76
            'bookable' => [
77
                'create' => true,
78
            ],
79
            'bookableMetadata' => [
80
                'create' => true,
81
            ],
82
            'bookableTag' => [
83
                'create' => true,
84
            ],
85
            'booking' => [
86
                'create' => true,
87
            ],
88
            'category' => [
89
                'create' => true,
90
            ],
91
            'country' => [
92
                'create' => false,
93
            ],
94
            'expenseClaim' => [
95
                'create' => true,
96
            ],
97
            'image' => [
98
                'create' => true,
99
            ],
100
            'license' => [
101
                'create' => true,
102
            ],
103
            'message' => [
104
                'create' => false,
105
            ],
106
            'user' => [
107
                'create' => true,
108
            ],
109
            'userTag' => [
110
                'create' => true,
111
            ],
112
113
        ];
114
115
        User::setCurrent($user);
116
        self::assertSame($user, User::getCurrent());
117
118
        $admin = new User(User::ROLE_ADMINISTRATOR);
119
        $actualForAdmin = $admin->getGlobalPermissions();
120
121
        self::assertEquals($expectedForAdmin, $actualForAdmin);
122
        self::assertSame($user, User::getCurrent());
123
        self::assertNotEquals($expectedForAdmin, $expected);
124
    }
125
126
    /**
127
     * @dataProvider providerSetRole
128
     *
129
     * @param string $currentRole
130
     * @param string $oldRole
131
     * @param string $newRole
132
     * @param null|string $exception
133
     */
134
    public function testSetRole(string $currentRole, string $oldRole, string $newRole, ?string $exception): void
135
    {
136
        if ($currentRole !== User::ROLE_ANONYMOUS) {
137
            $currentUser = new User($currentRole);
138
            User::setCurrent($currentUser);
139
        }
140
141
        $user2 = new User($oldRole);
142
143
        if ($exception) {
144
            $this->expectExceptionMessage($exception);
145
        }
146
147
        $user2->setRole($newRole);
148
        self::assertSame($newRole, $user2->getRole());
149
    }
150
151
    public function providerSetRole(): array
152
    {
153
        return [
154
            [User::ROLE_ANONYMOUS, User::ROLE_ADMINISTRATOR, User::ROLE_MEMBER, 'anonymous is not allowed to change role to member'],
155
            [User::ROLE_ANONYMOUS, User::ROLE_MEMBER, User::ROLE_ADMINISTRATOR, 'anonymous is not allowed to change role to administrator'],
156
157
            [User::ROLE_MEMBER, User::ROLE_MEMBER, User::ROLE_MEMBER, null],
158
            [User::ROLE_MEMBER, User::ROLE_MEMBER, User::ROLE_ADMINISTRATOR, 'member is not allowed to change role to administrator'],
159
160
            [User::ROLE_ADMINISTRATOR, User::ROLE_MEMBER, User::ROLE_ADMINISTRATOR, null],
161
            [User::ROLE_ADMINISTRATOR, User::ROLE_ADMINISTRATOR, User::ROLE_MEMBER, null],
162
        ];
163
    }
164
165
    public function testSetPassword(): void
166
    {
167
        $user = new User();
168
        self::assertNull($user->getPassword(), 'should have no password at first');
169
170
        $user->setPassword('12345');
171
        $actual1 = $user->getPassword();
172
        self::assertNotSame('', $actual1, 'should be able to change password ');
173
        self::assertTrue(password_verify('12345', $actual1), 'password must have been hashed');
174
175
        $user->setPassword('');
176
        $actual2 = $user->getPassword();
177
        self::assertSame($actual1, $actual2, 'should ignore empty password');
178
179
        $user->setPassword('money');
180
        $actual3 = $user->getPassword();
181
        self::assertNotSame($actual1, $actual3, 'should be able to change to something else');
182
        self::assertTrue(password_verify('money', $actual3), 'password must have been hashed again');
183
    }
184
185
    /**
186
     * @dataProvider providerSetOwner
187
     */
188
    public function testSetOwner(?User $currentUser, ?User $originalOwner, ?User $newOwner, ?string $exception = null): void
189
    {
190
        User::setCurrent($currentUser);
191
192
        $subject = new Booking();
193
        self::assertNull($subject->getOwner());
194
195
        $subject->setOwner($originalOwner);
196
        self::assertSame($originalOwner, $subject->getOwner());
197
198
        if ($exception) {
199
            $this->expectExceptionMessage($exception);
200
        }
201
202
        $subject->setOwner($newOwner);
203
        self::assertSame($newOwner, $subject->getOwner());
204
    }
205
206
    public function providerSetOwner(): array
207
    {
208
        $u1 = new User();
209
        $u1->setLogin('u1');
210
        $u2 = new User();
211
        $u2->setLogin('u2');
212
        $u3 = new User();
213
        $u3->setLogin('u3');
214
        $admin = new User(User::ROLE_ADMINISTRATOR);
215
        $admin->setLogin('admin');
216
217
        return [
218
            'can change nothing' => [null, null, null],
219
            'can set owner for first time' => [null, null, $u3],
220
            'can set owner for first time to myself' => [$u1, null, $u1],
221
            'can set owner for first time even if it is not myself' => [$u1, null, $u3],
222
            'can donate my stuff' => [$u1, $u1, $u3],
223
            'cannot donate stuff that are not mine' => [$u1, $u2, $u3, 'u1 is not allowed to change owner to u3 because it belongs to u2'],
224
            'admin cannot donate stuff that are not mine' => [$admin, $u2, $u3],
225
        ];
226
    }
227
}
228