-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathUserServiceTest.php
More file actions
228 lines (185 loc) · 8.15 KB
/
Copy pathUserServiceTest.php
File metadata and controls
228 lines (185 loc) · 8.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
<?php
declare(strict_types=1);
/**
* This source file is available under the terms of the
* Pimcore Open Core License (POCL)
* Full copyright and license information is available in
* LICENSE.md which is distributed with this source code.
*
* @copyright Copyright (c) Pimcore GmbH (https://www.pimcore.com)
* @license Pimcore Open Core License (POCL)
*/
namespace Pimcore\Bundle\StudioBackendBundle\Tests\Unit\User\Service;
use Codeception\Stub\Expected;
use Codeception\Test\Unit;
use Exception;
use Pimcore\Bundle\StudioBackendBundle\Exception\Api\DatabaseException;
use Pimcore\Bundle\StudioBackendBundle\Exception\Api\ForbiddenException;
use Pimcore\Bundle\StudioBackendBundle\Exception\Api\NotFoundException;
use Pimcore\Bundle\StudioBackendBundle\Security\Service\SecurityServiceInterface;
use Pimcore\Bundle\StudioBackendBundle\User\Hydrator\SimpleUserHydratorInterface;
use Pimcore\Bundle\StudioBackendBundle\User\Hydrator\UserHydratorInterface;
use Pimcore\Bundle\StudioBackendBundle\User\Hydrator\UserTreeNodeHydratorInterface;
use Pimcore\Bundle\StudioBackendBundle\User\Repository\UserFolderRepositoryInterface;
use Pimcore\Bundle\StudioBackendBundle\User\Repository\UserRepositoryInterface;
use Pimcore\Bundle\StudioBackendBundle\User\Schema\User as UserSchema;
use Pimcore\Bundle\StudioBackendBundle\User\Service\UserService;
use Pimcore\Model\User;
use Pimcore\Model\UserInterface;
use ReflectionClass;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
/**
* @internal
*/
final class UserServiceTest extends Unit
{
public function testDeleteUserWhenUserToDeleteIsAdminButCurrentUserNot()
{
$userToDelete = new User();
$userToDelete->setAdmin(true);
$securityServiceMock = $this->makeEmpty(SecurityServiceInterface::class, [
'getCurrentUser' => $this->makeEmpty(UserInterface::class, ['isAdmin' => false]),
]);
$userRepositoryMock = $this->makeEmpty(UserRepositoryInterface::class, [
'getUserById' => $userToDelete,
]);
$userService = $this->getUserService($securityServiceMock, $userRepositoryMock);
$this->expectExceptionMessage('Only admins can delete other admins');
$this->expectException(ForbiddenException::class);
$userService->deleteUser(1);
}
public function testDeleteUserWithDatabaseException()
{
$userToDelete = new User();
$userToDelete->setAdmin(false);
$securityServiceMock = $this->makeEmpty(SecurityServiceInterface::class, [
'getCurrentUser' => $this->makeEmpty(UserInterface::class, ['isAdmin' => true]),
]);
$userRepositoryMock = $this->makeEmpty(UserRepositoryInterface::class, [
'getUserById' => $userToDelete,
'deleteUser' => function (User $user) {
throw new Exception('Database error');
},
]);
$userService = $this->getUserService($securityServiceMock, $userRepositoryMock);
$this->expectExceptionMessage('Error deleting user with id 1: Database error');
$this->expectException(DatabaseException::class);
$userService->deleteUser(1);
}
public function testDeleteUser()
{
$userToDelete = new User();
$userToDelete->setAdmin(false);
$securityServiceMock = $this->makeEmpty(SecurityServiceInterface::class, [
'getCurrentUser' => $this->makeEmpty(UserInterface::class, ['isAdmin' => true]),
]);
$userRepositoryMock = $this->makeEmpty(UserRepositoryInterface::class, [
'getUserById' => $userToDelete,
'deleteUser' => Expected::once(),
]);
$userService = $this->getUserService($securityServiceMock, $userRepositoryMock);
$userService->deleteUser(1);
}
public function testDeleteUserWhenBothAreAdmins(): void
{
$userToDelete = new User();
$userToDelete->setAdmin(true);
$securityServiceMock = $this->makeEmpty(SecurityServiceInterface::class, [
'getCurrentUser' => $this->makeEmpty(UserInterface::class, ['isAdmin' => true]),
]);
$userRepositoryMock = $this->makeEmpty(UserRepositoryInterface::class, [
'getUserById' => $userToDelete,
'deleteUser' => Expected::once(),
]);
$userService = $this->getUserService($securityServiceMock, $userRepositoryMock);
$userService->deleteUser(1);
}
public function testGetUserByIdThrowsForbiddenWhenNonAdminViewsAdmin(): void
{
$adminUser = new User();
$adminUser->setAdmin(true);
$securityServiceMock = $this->makeEmpty(SecurityServiceInterface::class, [
'getCurrentUser' => $this->makeEmpty(UserInterface::class, ['isAdmin' => false]),
]);
$userRepositoryMock = $this->makeEmpty(UserRepositoryInterface::class, [
'getUserById' => $adminUser,
]);
$userService = $this->getUserService($securityServiceMock, $userRepositoryMock);
$this->expectException(ForbiddenException::class);
$this->expectExceptionMessage('Only admins can view other admins');
$userService->getUserById(1);
}
public function testGetUserByIdSuccess(): void
{
$user = new User();
$user->setAdmin(false);
$userSchema = (new ReflectionClass(UserSchema::class))->newInstanceWithoutConstructor();
$securityServiceMock = $this->makeEmpty(SecurityServiceInterface::class, [
'getCurrentUser' => $this->makeEmpty(UserInterface::class, ['isAdmin' => true]),
]);
$userRepositoryMock = $this->makeEmpty(UserRepositoryInterface::class, [
'getUserById' => $user,
]);
$userHydratorMock = $this->makeEmpty(UserHydratorInterface::class, [
'hydrate' => $userSchema,
]);
$eventDispatcherMock = $this->makeEmpty(EventDispatcherInterface::class, [
'dispatch' => Expected::once(function (object $event) {
return $event;
}),
]);
$userService = $this->getUserService(
$securityServiceMock,
$userRepositoryMock,
$eventDispatcherMock,
$userHydratorMock
);
$result = $userService->getUserById(1);
$this->assertSame($userSchema, $result);
}
public function testGetUserNameByIdReturnsName(): void
{
$user = new User();
$user->setName('johndoe');
$userRepositoryMock = $this->makeEmpty(UserRepositoryInterface::class, [
'getUserById' => $user,
]);
$userService = $this->getUserService(
$this->makeEmpty(SecurityServiceInterface::class),
$userRepositoryMock
);
$this->assertSame('johndoe', $userService->getUserNameById(1));
}
public function testGetUserNameByIdReturnsNullWhenNotFound(): void
{
$userRepositoryMock = $this->makeEmpty(UserRepositoryInterface::class, [
'getUserById' => function () {
throw new NotFoundException('User', 999);
},
]);
$userService = $this->getUserService(
$this->makeEmpty(SecurityServiceInterface::class),
$userRepositoryMock
);
$this->assertNull($userService->getUserNameById(999));
}
private function getUserService(
SecurityServiceInterface $securityServiceMock,
UserRepositoryInterface $userRepositoryMock,
?EventDispatcherInterface $eventDispatcherMock = null,
?UserHydratorInterface $userHydratorMock = null,
): UserService {
$userTreeNodeHydratorMock = $this->makeEmpty(UserTreeNodeHydratorInterface::class);
$userFolderRepositoryMock = $this->makeEmpty(UserFolderRepositoryInterface::class);
$simpleUserHydratorMock = $this->makeEmpty(SimpleUserHydratorInterface::class);
return new UserService(
$userRepositoryMock,
$userTreeNodeHydratorMock,
$eventDispatcherMock ?? $this->makeEmpty(EventDispatcherInterface::class),
$securityServiceMock,
$userFolderRepositoryMock,
$userHydratorMock ?? $this->makeEmpty(UserHydratorInterface::class),
$simpleUserHydratorMock
);
}
}