1 | <?php |
||
2 | /** |
||
3 | * This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony). |
||
4 | * |
||
5 | * Copyright (C) 2019 - 2020 Jan Böhmer (https://github.com/jbtronics) |
||
6 | * |
||
7 | * This program is free software: you can redistribute it and/or modify |
||
8 | * it under the terms of the GNU Affero General Public License as published |
||
9 | * by the Free Software Foundation, either version 3 of the License, or |
||
10 | * (at your option) any later version. |
||
11 | * |
||
12 | * This program is distributed in the hope that it will be useful, |
||
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
||
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||
15 | * GNU Affero General Public License for more details. |
||
16 | * |
||
17 | * You should have received a copy of the GNU Affero General Public License |
||
18 | * along with this program. If not, see <https://www.gnu.org/licenses/>. |
||
19 | */ |
||
20 | |||
21 | declare(strict_types=1); |
||
22 | |||
23 | namespace App\Tests\Services\UserSystem; |
||
24 | |||
25 | use App\Entity\UserSystem\Group; |
||
26 | use App\Entity\UserSystem\PermissionData; |
||
27 | use App\Entity\UserSystem\PermissionsEmbed; |
||
0 ignored issues
–
show
|
|||
28 | use App\Entity\UserSystem\User; |
||
29 | use App\Services\UserSystem\PermissionManager; |
||
30 | use InvalidArgumentException; |
||
31 | use Symfony\Bundle\FrameworkBundle\Test\WebTestCase; |
||
32 | |||
33 | class PermissionManagerTest extends WebTestCase |
||
34 | { |
||
35 | protected $user_withoutGroup; |
||
36 | |||
37 | protected $user; |
||
38 | protected $group; |
||
39 | |||
40 | /** |
||
41 | * @var PermissionManager |
||
42 | */ |
||
43 | protected $service; |
||
44 | |||
45 | protected function setUp(): void |
||
46 | { |
||
47 | parent::setUp(); // TODO: Change the autogenerated stub |
||
48 | |||
49 | //Get an service instance. |
||
50 | self::bootKernel(); |
||
51 | $this->service = self::getContainer()->get(PermissionManager::class); |
||
52 | |||
53 | //Set up a mocked user |
||
54 | $user_perms = new PermissionData(); |
||
55 | $user_perms->setPermissionValue('parts', 'read', true) //read |
||
56 | ->setPermissionValue('parts', 'edit', false) //edit |
||
57 | ->setPermissionValue('parts', 'create', null) //create |
||
58 | ->setPermissionValue('parts', 'move', null) //move |
||
59 | ->setPermissionValue('parts', 'delete', null); //delete |
||
60 | |||
61 | $this->user = $this->createMock(User::class); |
||
62 | $this->user->method('getPermissions')->willReturn($user_perms); |
||
63 | |||
64 | $this->user_withoutGroup = $this->createMock(User::class); |
||
65 | $this->user_withoutGroup->method('getPermissions')->willReturn($user_perms); |
||
66 | $this->user_withoutGroup->method('getGroup')->willReturn(null); |
||
67 | |||
68 | //Set up a faked group |
||
69 | $group1_perms = new PermissionData(); |
||
70 | $group1_perms |
||
71 | ->setPermissionValue('parts', 'delete', false) |
||
72 | ->setPermissionValue('parts', 'search', null) |
||
73 | ->setPermissionValue('parts', 'read', false) |
||
74 | ->setPermissionValue('parts', 'show_history', true) |
||
75 | ->setPermissionValue('parts', 'edit', true); |
||
76 | |||
77 | $this->group = $this->createMock(Group::class); |
||
78 | $this->group->method('getPermissions')->willReturn($group1_perms); |
||
79 | |||
80 | //Set this group for the user |
||
81 | $this->user->method('getGroup')->willReturn($this->group); |
||
82 | |||
83 | //parent group |
||
84 | $parent_group_perms = new PermissionData(); |
||
85 | $parent_group_perms->setPermissionValue('parts', 'all_parts', true) |
||
86 | ->setPermissionValue('parts', 'no_price_parts', false) |
||
87 | ->setPermissionValue('parts', 'obsolete_parts', null); |
||
88 | $parent_group = $this->createMock(Group::class); |
||
89 | $parent_group->method('getPermissions')->willReturn($parent_group_perms); |
||
90 | |||
91 | $this->group->method('getParent')->willReturn($parent_group); |
||
92 | } |
||
93 | |||
94 | public function getPermissionNames(): array |
||
95 | { |
||
96 | //List some permission names |
||
97 | return [ |
||
98 | ['parts'], |
||
99 | ['system'], |
||
100 | ['footprints'], |
||
101 | ['suppliers'], |
||
102 | ['tools'] |
||
103 | ]; |
||
104 | } |
||
105 | |||
106 | /** |
||
107 | * @dataProvider getPermissionNames |
||
108 | */ |
||
109 | public function testListOperationsForPermission($perm_name): void |
||
110 | { |
||
111 | $arr = $this->service->listOperationsForPermission($perm_name); |
||
112 | |||
113 | //Every entry should not be empty. |
||
114 | $this->assertNotEmpty($arr); |
||
115 | } |
||
116 | |||
117 | public function testInvalidListOperationsForPermission(): void |
||
118 | { |
||
119 | $this->expectException(InvalidArgumentException::class); |
||
120 | //Must throw an exception |
||
121 | $this->service->listOperationsForPermission('invalid'); |
||
122 | } |
||
123 | |||
124 | public function testisValidPermission(): void |
||
125 | { |
||
126 | $this->assertTrue($this->service->isValidPermission('parts')); |
||
127 | $this->assertFalse($this->service->isValidPermission('invalid')); |
||
128 | } |
||
129 | |||
130 | public function testIsValidOperation(): void |
||
131 | { |
||
132 | $this->assertTrue($this->service->isValidOperation('parts', 'read')); |
||
133 | |||
134 | //Must return false if either the permission or the operation is not existing |
||
135 | $this->assertFalse($this->service->isValidOperation('parts', 'invalid')); |
||
136 | $this->assertFalse($this->service->isValidOperation('invalid', 'read')); |
||
137 | $this->assertFalse($this->service->isValidOperation('invalid', 'invalid')); |
||
138 | } |
||
139 | |||
140 | public function testDontInherit(): void |
||
141 | { |
||
142 | //Check with faked object |
||
143 | $this->assertTrue($this->service->dontInherit($this->user, 'parts', 'read')); |
||
144 | $this->assertFalse($this->service->dontInherit($this->user, 'parts', 'edit')); |
||
145 | $this->assertNull($this->service->dontInherit($this->user, 'parts', 'create')); |
||
146 | $this->assertNull($this->service->dontInherit($this->user, 'parts', 'show_history')); |
||
147 | $this->assertNull($this->service->dontInherit($this->user, 'parts', 'delete')); |
||
148 | |||
149 | //Test for user without group |
||
150 | $this->assertTrue($this->service->dontInherit($this->user_withoutGroup, 'parts', 'read')); |
||
151 | $this->assertFalse($this->service->dontInherit($this->user_withoutGroup, 'parts', 'edit')); |
||
152 | $this->assertNull($this->service->dontInherit($this->user_withoutGroup, 'parts', 'create')); |
||
153 | $this->assertNull($this->service->dontInherit($this->user_withoutGroup, 'parts', 'show_history')); |
||
154 | $this->assertNull($this->service->dontInherit($this->user_withoutGroup, 'parts', 'delete')); |
||
155 | } |
||
156 | |||
157 | public function testInherit(): void |
||
158 | { |
||
159 | //Not inherited values should be same as dont inherit: |
||
160 | $this->assertTrue($this->service->inherit($this->user, 'parts', 'read')); |
||
161 | $this->assertFalse($this->service->inherit($this->user, 'parts', 'edit')); |
||
162 | //When thing can not be resolved null should be returned |
||
163 | $this->assertNull($this->service->inherit($this->user, 'parts', 'create')); |
||
164 | |||
165 | //Check for inherit from group |
||
166 | $this->assertTrue($this->service->inherit($this->user, 'parts', 'show_history')); |
||
167 | $this->assertFalse($this->service->inherit($this->user, 'parts', 'delete')); |
||
168 | |||
169 | //Test for user without group |
||
170 | $this->assertTrue($this->service->inherit($this->user_withoutGroup, 'parts', 'read')); |
||
171 | $this->assertFalse($this->service->inherit($this->user_withoutGroup, 'parts', 'edit')); |
||
172 | $this->assertNull($this->service->inherit($this->user_withoutGroup, 'parts', 'create')); |
||
173 | $this->assertNull($this->service->inherit($this->user_withoutGroup, 'parts', 'show_history')); |
||
174 | $this->assertNull($this->service->inherit($this->user_withoutGroup, 'parts', 'delete')); |
||
175 | } |
||
176 | |||
177 | public function testSetPermission(): void |
||
178 | { |
||
179 | $user = new User(); |
||
180 | |||
181 | //Set permission to true |
||
182 | $this->service->setPermission($user, 'parts', 'read', true); |
||
183 | $this->assertTrue($this->service->dontInherit($user, 'parts', 'read')); |
||
184 | $this->assertTrue($this->service->inherit($user, 'parts', 'read')); |
||
185 | |||
186 | //Set permission to false |
||
187 | $this->service->setPermission($user, 'parts', 'read', false); |
||
188 | $this->assertFalse($this->service->dontInherit($user, 'parts', 'read')); |
||
189 | $this->assertFalse($this->service->inherit($user, 'parts', 'read')); |
||
190 | |||
191 | //Set permission to null |
||
192 | $this->service->setPermission($user, 'parts', 'read', null); |
||
193 | $this->assertNull($this->service->dontInherit($user, 'parts', 'read')); |
||
194 | $this->assertNull($this->service->inherit($user, 'parts', 'read')); |
||
195 | } |
||
196 | |||
197 | public function testSetAllPermissions(): void |
||
198 | { |
||
199 | $user = new User(); |
||
200 | |||
201 | //Set all permissions to true |
||
202 | $this->service->setAllPermissions($user, true); |
||
203 | $this->assertTrue($this->service->dontInherit($user, 'parts', 'read')); |
||
204 | $this->assertTrue($this->service->dontInherit($user, 'parts', 'create')); |
||
205 | $this->assertTrue($this->service->dontInherit($user, 'categories', 'edit')); |
||
206 | |||
207 | //Set all permissions to false |
||
208 | $this->service->setAllPermissions($user, false); |
||
209 | $this->assertFalse($this->service->dontInherit($user, 'parts', 'read')); |
||
210 | $this->assertFalse($this->service->dontInherit($user, 'parts', 'create')); |
||
211 | $this->assertFalse($this->service->dontInherit($user, 'categories', 'edit')); |
||
212 | |||
213 | //Set all permissions to null |
||
214 | $this->service->setAllPermissions($user, null); |
||
215 | $this->assertNull($this->service->dontInherit($user, 'parts', 'read')); |
||
216 | $this->assertNull($this->service->dontInherit($user, 'parts', 'create')); |
||
217 | $this->assertNull($this->service->dontInherit($user, 'categories', 'edit')); |
||
218 | } |
||
219 | |||
220 | public function testSetAllOperationsOfPermission(): void |
||
221 | { |
||
222 | $user = new User(); |
||
223 | |||
224 | //Set all operations of permission to true |
||
225 | $this->service->setAllOperationsOfPermission($user, 'parts', true); |
||
226 | $this->assertTrue($this->service->dontInherit($user, 'parts', 'read')); |
||
227 | $this->assertTrue($this->service->dontInherit($user, 'parts', 'create')); |
||
228 | $this->assertTrue($this->service->dontInherit($user, 'parts', 'edit')); |
||
229 | |||
230 | //Set all operations of permission to false |
||
231 | $this->service->setAllOperationsOfPermission($user, 'parts', false); |
||
232 | $this->assertFalse($this->service->dontInherit($user, 'parts', 'read')); |
||
233 | $this->assertFalse($this->service->dontInherit($user, 'parts', 'create')); |
||
234 | $this->assertFalse($this->service->dontInherit($user, 'parts', 'edit')); |
||
235 | |||
236 | //Set all operations of permission to null |
||
237 | $this->service->setAllOperationsOfPermission($user, 'parts', null); |
||
238 | $this->assertNull($this->service->dontInherit($user, 'parts', 'read')); |
||
239 | $this->assertNull($this->service->dontInherit($user, 'parts', 'create')); |
||
240 | $this->assertNull($this->service->dontInherit($user, 'parts', 'edit')); |
||
241 | } |
||
242 | |||
243 | public function testEnsureCorrectSetOperations(): void |
||
244 | { |
||
245 | //Create an empty user (all permissions are inherit) |
||
246 | $user = new User(); |
||
247 | |||
248 | //ensure that all permissions are inherit |
||
249 | $this->assertNull($this->service->inherit($user, 'parts', 'read')); |
||
250 | $this->assertNull($this->service->inherit($user, 'parts', 'edit')); |
||
251 | $this->assertNull($this->service->inherit($user, 'categories', 'read')); |
||
252 | |||
253 | //Set some permissions |
||
254 | $this->service->setPermission($user, 'parts', 'create', true); |
||
255 | //Until now only the create permission should be set |
||
256 | $this->assertTrue($this->service->dontInherit($user, 'parts', 'create')); |
||
257 | $this->assertNull($this->service->dontInherit($user, 'parts', 'read')); |
||
258 | |||
259 | //Now we call the ensureCorrectSetOperations method |
||
260 | $this->service->ensureCorrectSetOperations($user); |
||
261 | |||
262 | //Now all permissions should be set |
||
263 | $this->assertTrue($this->service->dontInherit($user, 'parts', 'create')); |
||
264 | $this->assertTrue($this->service->dontInherit($user, 'parts', 'read')); |
||
265 | $this->assertTrue($this->service->dontInherit($user, 'parts', 'edit')); |
||
266 | $this->assertTrue($this->service->dontInherit($user, 'categories', 'read')); |
||
267 | } |
||
268 | } |
||
269 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths