These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | namespace Kunstmaan\AdminBundle\Tests\Helper\Security\Acl\Permission; |
||
4 | |||
5 | use Doctrine\Common\Collections\ArrayCollection; |
||
6 | use Doctrine\ORM\EntityManager; |
||
7 | use Doctrine\ORM\EntityRepository; |
||
8 | use Kunstmaan\AdminBundle\Entity\AbstractEntity; |
||
9 | use Kunstmaan\AdminBundle\Entity\User; |
||
10 | use Kunstmaan\AdminBundle\Helper\Security\Acl\Permission\MaskBuilder; |
||
11 | use Kunstmaan\AdminBundle\Helper\Security\Acl\Permission\PermissionAdmin; |
||
12 | use Kunstmaan\AdminBundle\Helper\Security\Acl\Permission\PermissionMapInterface; |
||
13 | use Kunstmaan\NodeBundle\Entity\Node; |
||
14 | use Kunstmaan\UtilitiesBundle\Helper\Shell\Shell; |
||
15 | use PHPUnit\Framework\TestCase; |
||
16 | use ReflectionClass; |
||
17 | use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
||
18 | use Symfony\Component\HttpFoundation\Request; |
||
19 | use Symfony\Component\HttpKernel\KernelInterface; |
||
20 | use Symfony\Component\Security\Acl\Domain\RoleSecurityIdentity; |
||
21 | use Symfony\Component\Security\Acl\Exception\AclNotFoundException; |
||
22 | use Symfony\Component\Security\Acl\Model\AclInterface; |
||
23 | use Symfony\Component\Security\Acl\Model\AclProviderInterface; |
||
24 | use Symfony\Component\Security\Acl\Model\AuditableEntryInterface; |
||
25 | use Symfony\Component\Security\Acl\Model\MutableAclInterface; |
||
26 | use Symfony\Component\Security\Acl\Model\MutableAclProviderInterface; |
||
27 | use Symfony\Component\Security\Acl\Model\ObjectIdentityRetrievalStrategyInterface; |
||
28 | use Symfony\Component\Security\Acl\Model\SecurityIdentityInterface; |
||
29 | use Symfony\Component\Security\Core\Authentication\Token\PreAuthenticatedToken; |
||
30 | use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage; |
||
31 | use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface; |
||
32 | |||
33 | class PermissionAdminTest extends TestCase |
||
34 | { |
||
35 | /** |
||
36 | * @var PermissionAdmin |
||
37 | */ |
||
38 | protected $object; |
||
39 | |||
40 | public function testInitialize() |
||
41 | { |
||
42 | $object = $this->getInitializedPermissionAdmin(); |
||
43 | $this->assertEquals(array('ROLE_TEST' => new MaskBuilder(1)), $object->getPermissions()); |
||
44 | } |
||
45 | |||
46 | public function testGetPermissionWithString() |
||
47 | { |
||
48 | $object = $this->getInitializedPermissionAdmin(); |
||
49 | $this->assertEquals(new MaskBuilder(1), $object->getPermission('ROLE_TEST')); |
||
50 | } |
||
51 | |||
52 | public function testGetPermissionWithRoleObject() |
||
53 | { |
||
54 | $object = $this->getInitializedPermissionAdmin(); |
||
55 | |||
56 | $role = $this->createMock('Symfony\Component\Security\Core\Role\Role'); |
||
57 | $role->expects($this->once()) |
||
58 | ->method('getRole') |
||
59 | ->will($this->returnValue('ROLE_TEST')); |
||
60 | $this->assertEquals(new MaskBuilder(1), $object->getPermission($role)); |
||
61 | } |
||
62 | |||
63 | public function testGetPermissionWithUnknownRole() |
||
64 | { |
||
65 | $object = $this->getInitializedPermissionAdmin(); |
||
66 | $this->assertNull($object->getPermission('ROLE_UNKNOWN')); |
||
67 | } |
||
68 | |||
69 | public function testGetAllRoles() |
||
70 | { |
||
71 | $roleRepo = $this->getMockBuilder('Doctrine\ORM\EntityRepository') |
||
72 | ->disableOriginalConstructor() |
||
73 | ->getMock(); |
||
74 | $roleRepo->expects($this->once()) |
||
75 | ->method('findAll') |
||
76 | ->will($this->returnValue(null)); |
||
77 | |||
78 | $em = $this->getEntityManager(); |
||
79 | $em->expects($this->once()) |
||
80 | ->method('getRepository') |
||
81 | ->with('KunstmaanAdminBundle:Role') |
||
82 | ->will($this->returnValue($roleRepo)); |
||
83 | $context = $this->getTokenStorage(); |
||
84 | $aclProvider = $this->getAclProvider(); |
||
85 | $retrievalStrategy = $this->getOidRetrievalStrategy(); |
||
86 | $dispatcher = $this->getEventDispatcher(); |
||
87 | $shell = $this->getShell(); |
||
88 | $kernel = $this->getKernel(); |
||
89 | $object = new PermissionAdmin($em, $context, $aclProvider, $retrievalStrategy, $dispatcher, $shell, $kernel); |
||
90 | |||
91 | $this->assertNull($object->getAllRoles()); |
||
92 | } |
||
93 | |||
94 | public function testGetPossiblePermissions() |
||
95 | { |
||
96 | $em = $this->getEntityManager(); |
||
97 | $context = $this->getTokenStorage(); |
||
98 | $aclProvider = $this->getAclProvider(); |
||
99 | $retrievalStrategy = $this->getOidRetrievalStrategy(); |
||
100 | $retrievalStrategy |
||
101 | ->expects($this->once()) |
||
102 | ->method('getObjectIdentity') |
||
103 | ->will($this->throwException(new \Symfony\Component\Security\Acl\Exception\AclNotFoundException())); |
||
104 | $dispatcher = $this->getEventDispatcher(); |
||
105 | $shell = $this->getShell(); |
||
106 | $kernel = $this->getKernel(); |
||
107 | $object = new PermissionAdmin($em, $context, $aclProvider, $retrievalStrategy, $dispatcher, $shell, $kernel); |
||
108 | |||
109 | $permissions = array('PERMISSION1', 'PERMISSION2'); |
||
110 | $permissionMap = $this->createMock('Kunstmaan\AdminBundle\Helper\Security\Acl\Permission\PermissionMapInterface'); |
||
111 | $permissionMap |
||
112 | ->expects($this->any()) |
||
113 | ->method('getPossiblePermissions') |
||
114 | ->will($this->returnValue($permissions)); |
||
115 | $entity = $this->getEntity(); |
||
116 | /* @var $permissionMap PermissionMapInterface */ |
||
117 | $object->initialize($entity, $permissionMap); |
||
118 | $this->assertEquals($permissions, $object->getPossiblePermissions()); |
||
119 | } |
||
120 | |||
121 | public function testCreateAclChangeset() |
||
122 | { |
||
123 | $em = $this->getEntityManager(); |
||
124 | $em->expects($this->once()) |
||
125 | ->method('persist') |
||
126 | ->with($this->isInstanceOf('Kunstmaan\AdminBundle\Entity\AclChangeset')); |
||
127 | $em->expects($this->once()) |
||
128 | ->method('flush'); |
||
129 | $context = $this->getTokenStorage(); |
||
130 | $aclProvider = $this->getAclProvider(); |
||
131 | $retrievalStrategy = $this->getOidRetrievalStrategy(); |
||
132 | $dispatcher = $this->getEventDispatcher(); |
||
133 | $shell = $this->getShell(); |
||
134 | $kernel = $this->getKernel(); |
||
135 | $object = new PermissionAdmin($em, $context, $aclProvider, $retrievalStrategy, $dispatcher, $shell, $kernel); |
||
136 | |||
137 | $entity = $this->getEntity(); |
||
138 | /* @var $user User */ |
||
139 | $user = $this->getMockBuilder('Kunstmaan\AdminBundle\Entity\User') |
||
140 | ->disableOriginalConstructor() |
||
141 | ->getMock(); |
||
142 | |||
143 | $object->createAclChangeSet($entity, array(), $user); |
||
144 | } |
||
145 | |||
146 | /** |
||
147 | * Return entity manager mock |
||
148 | * |
||
149 | * @return EntityManager |
||
150 | */ |
||
151 | public function getEntityManager() |
||
152 | { |
||
153 | return $this->getMockBuilder('Doctrine\ORM\EntityManager') |
||
154 | ->disableOriginalConstructor() |
||
155 | ->getMock(); |
||
156 | } |
||
157 | |||
158 | /** |
||
159 | * Return alc provider mock |
||
160 | * |
||
161 | * @return AclProviderInterface |
||
162 | */ |
||
163 | public function getAclProvider() |
||
164 | { |
||
165 | return $this->createMock('Symfony\Component\Security\Acl\Model\MutableAclProviderInterface'); |
||
166 | } |
||
167 | |||
168 | /** |
||
169 | * Return security token storage |
||
170 | * |
||
171 | * @return TokenStorageInterface |
||
172 | */ |
||
173 | public function getTokenStorage() |
||
174 | { |
||
175 | return $this->createMock('Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface'); |
||
176 | } |
||
177 | |||
178 | /** |
||
179 | * Return oid retrieval strategy mock |
||
180 | * |
||
181 | * @return ObjectIdentityRetrievalStrategyInterface |
||
182 | */ |
||
183 | public function getOidRetrievalStrategy() |
||
184 | { |
||
185 | return $this->createMock('Symfony\Component\Security\Acl\Model\ObjectIdentityRetrievalStrategyInterface'); |
||
186 | } |
||
187 | |||
188 | /** |
||
189 | * Return event dispatcher mock |
||
190 | * |
||
191 | * @return EventDispatcherInterface |
||
192 | */ |
||
193 | public function getEventDispatcher() |
||
194 | { |
||
195 | return $this->createMock('Symfony\Component\EventDispatcher\EventDispatcher'); |
||
196 | } |
||
197 | |||
198 | /** |
||
199 | * @return Shell |
||
200 | */ |
||
201 | public function getShell() |
||
202 | { |
||
203 | return new Shell(); |
||
204 | } |
||
205 | |||
206 | /** |
||
207 | * @return KernelInterface |
||
208 | */ |
||
209 | public function getKernel() |
||
210 | { |
||
211 | return $this->createMock('Symfony\Component\HttpKernel\KernelInterface'); |
||
212 | } |
||
213 | |||
214 | /** |
||
215 | * Return permission admin mock |
||
216 | * |
||
217 | * @return PermissionAdmin |
||
218 | */ |
||
219 | public function getPermissionAdmin() |
||
220 | { |
||
221 | $em = $this->getEntityManager(); |
||
222 | $context = $this->getTokenStorage(); |
||
223 | |||
224 | $securityIdentity = new RoleSecurityIdentity('ROLE_TEST'); |
||
225 | |||
226 | $entity = $this->getMockBuilder('Symfony\Component\Security\Acl\Domain\Entry') |
||
227 | ->disableOriginalConstructor() |
||
228 | ->getMock(); |
||
229 | $entity |
||
230 | ->expects($this->any()) |
||
231 | ->method('getSecurityIdentity') |
||
232 | ->will($this->returnValue($securityIdentity)); |
||
233 | $entity |
||
234 | ->expects($this->any()) |
||
235 | ->method('getMask') |
||
236 | ->will($this->returnValue(1)); |
||
237 | |||
238 | $acl = $this->getMockBuilder('Symfony\Component\Security\Acl\Domain\Acl') |
||
239 | ->disableOriginalConstructor() |
||
240 | ->getMock(); |
||
241 | $acl->expects($this->atLeastOnce()) |
||
242 | ->method('getObjectAces') |
||
243 | ->will($this->returnValue(array($entity))); |
||
244 | |||
245 | $aclProvider = $this->getAclProvider(); |
||
246 | $aclProvider |
||
247 | ->expects($this->atLeastOnce()) |
||
248 | ->method('findAcl') |
||
249 | ->with($this->anything()) |
||
250 | ->will($this->returnValue($acl)); |
||
251 | |||
252 | $retrievalStrategy = $this->getOidRetrievalStrategy(); |
||
253 | $objectIdentity = $this->createMock('Symfony\Component\Security\Acl\Model\ObjectIdentityInterface'); |
||
254 | $retrievalStrategy |
||
255 | ->expects($this->atLeastOnce()) |
||
256 | ->method('getObjectIdentity') |
||
257 | ->will($this->returnValue($objectIdentity)); |
||
258 | $dispatcher = $this->getEventDispatcher(); |
||
259 | $shell = $this->getShell(); |
||
260 | $kernel = $this->getKernel(); |
||
261 | $object = new PermissionAdmin($em, $context, $aclProvider, $retrievalStrategy, $dispatcher, $shell, $kernel); |
||
262 | |||
263 | return $object; |
||
264 | } |
||
265 | |||
266 | /** |
||
267 | * Return entity mock |
||
268 | * |
||
269 | * @return AbstractEntity |
||
270 | */ |
||
271 | public function getEntity() |
||
272 | { |
||
273 | return $this->getMockForAbstractClass('Kunstmaan\AdminBundle\Entity\AbstractEntity'); |
||
274 | } |
||
275 | |||
276 | /** |
||
277 | * Return permission admin mock |
||
278 | * |
||
279 | * @return PermissionAdmin |
||
280 | */ |
||
281 | public function getInitializedPermissionAdmin() |
||
282 | { |
||
283 | $object = $this->getPermissionAdmin(); |
||
284 | $entity = $this->getEntity(); |
||
285 | /* @var $permissionMap PermissionMapInterface */ |
||
286 | $permissionMap = $this->createMock('Kunstmaan\AdminBundle\Helper\Security\Acl\Permission\PermissionMapInterface'); |
||
287 | $object->initialize($entity, $permissionMap); |
||
288 | |||
289 | return $object; |
||
290 | } |
||
291 | |||
292 | public function testBindRequestReturnsTrueWhenNoChanges() |
||
293 | { |
||
294 | $object = $this->getInitializedPermissionAdmin(); |
||
295 | $request = $this->createMock(Request::class); |
||
296 | $request->request = $this->createMock(Request::class); |
||
0 ignored issues
–
show
|
|||
297 | $request->request->expects($this->once())->method('get')->willReturn(''); |
||
0 ignored issues
–
show
Accessing
request on the interface PHPUnit\Framework\MockObject\MockObject suggest that you code against a concrete implementation. How about adding an instanceof check?
If you access a property on an interface, you most likely code against a concrete implementation of the interface. Available Fixes
Loading history...
|
|||
298 | $object->bindRequest($request); |
||
299 | } |
||
300 | |||
301 | /** |
||
302 | * @throws \ReflectionException |
||
303 | */ |
||
304 | public function testBindRequest() |
||
305 | { |
||
306 | $object = $this->getInitializedPermissionAdmin(); |
||
307 | $token = $this->createMock(PreAuthenticatedToken::class); |
||
308 | $token->expects($this->once())->method('getUser')->willReturn(new User()); |
||
309 | $request = $this->createMock(Request::class); |
||
310 | $request->request = $this->createMock(Request::class); |
||
0 ignored issues
–
show
Accessing
request on the interface PHPUnit\Framework\MockObject\MockObject suggest that you code against a concrete implementation. How about adding an instanceof check?
If you access a property on an interface, you most likely code against a concrete implementation of the interface. Available Fixes
Loading history...
|
|||
311 | $request->request->expects($this->any())->method('get')->will($this->onConsecutiveCalls(['ADMIN' => ['ADD' => ['VIEW']]], true)); |
||
312 | |||
313 | $mirror = new ReflectionClass(PermissionAdmin::class); |
||
314 | $property = $mirror->getProperty('tokenStorage'); |
||
315 | $property->setAccessible(true); |
||
316 | $val = $property->getValue($object); |
||
317 | $val->expects($this->once())->method('getToken')->willReturn($token); |
||
318 | |||
319 | $object->bindRequest($request); |
||
320 | } |
||
321 | |||
322 | /** |
||
323 | * @throws \ReflectionException |
||
324 | */ |
||
325 | public function testMaskAtIndex() |
||
326 | { |
||
327 | $object = $this->getInitializedPermissionAdmin(); |
||
328 | |||
329 | $id = $this->createMock(SecurityIdentityInterface::class); |
||
330 | $ace = $this->createMock(AuditableEntryInterface::class); |
||
331 | $ace->expects($this->once())->method('getSecurityIdentity')->willReturn($id); |
||
332 | $acl = $this->createMock(AclInterface::class); |
||
333 | $acl->expects($this->once())->method('getObjectAces')->willReturn([1 => $ace]); |
||
334 | |||
335 | $mirror = new ReflectionClass(PermissionAdmin::class); |
||
336 | $method = $mirror->getMethod('getMaskAtIndex'); |
||
337 | $method->setAccessible(true); |
||
338 | |||
339 | $this->assertFalse($method->invoke($object, $acl, 1)); |
||
340 | |||
341 | $id = new RoleSecurityIdentity('ADMIN'); |
||
342 | $ace = $this->createMock(AuditableEntryInterface::class); |
||
343 | $ace->expects($this->once())->method('getSecurityIdentity')->willReturn($id); |
||
344 | $ace->expects($this->once())->method('getMask')->willReturn(true); |
||
345 | $acl = $this->createMock(AclInterface::class); |
||
346 | $acl->expects($this->once())->method('getObjectAces')->willReturn([1 => $ace]); |
||
347 | |||
348 | $this->assertTrue($method->invoke($object, $acl, 1)); |
||
349 | } |
||
350 | |||
351 | /** |
||
352 | * @throws \ReflectionException |
||
353 | */ |
||
354 | public function testGetManageableRolesForPages() |
||
355 | { |
||
356 | $object = $this->getInitializedPermissionAdmin(); |
||
357 | |||
358 | $repo = $this->createMock(EntityRepository::class); |
||
359 | $repo->expects($this->once())->method('findAll')->willReturn(['ROLE_SUPER_ADMIN', 'USER']); |
||
360 | |||
361 | $em = $this->getEntityManager(); |
||
362 | $em->expects($this->once())->method('getRepository')->willReturn($repo); |
||
363 | |||
364 | $token = $this->createMock(PreAuthenticatedToken::class); |
||
365 | $token->expects($this->once())->method('getUser')->willReturn(new User()); |
||
366 | |||
367 | $storage = $this->createMock(TokenStorage::class); |
||
368 | $storage->expects($this->once())->method('getToken')->willReturn($token); |
||
369 | |||
370 | $mirror = new ReflectionClass(PermissionAdmin::class); |
||
371 | $property = $mirror->getProperty('tokenStorage'); |
||
372 | $property->setAccessible(true); |
||
373 | $property->setValue($object, $storage); |
||
374 | $property = $mirror->getProperty('em'); |
||
375 | $property->setAccessible(true); |
||
376 | $property->setValue($object, $em); |
||
377 | |||
378 | $roles = $object->getManageableRolesForPages(); |
||
379 | $this->assertCount(1, $roles); |
||
380 | $this->assertTrue(\in_array('USER', $roles)); |
||
381 | } |
||
382 | |||
383 | /** |
||
384 | * @throws \ReflectionException |
||
385 | */ |
||
386 | public function testApplyAclChangesetReturnsNull() |
||
387 | { |
||
388 | $object = $this->getInitializedPermissionAdmin(); |
||
389 | $entity = $this->createMock(AbstractEntity::class); |
||
390 | $entity->method('getId')->willReturn(666); |
||
391 | $entity->method('setId')->willReturn(null); |
||
392 | $entity->method('__toString')->willReturn('666'); |
||
393 | |||
394 | $this->assertNull($object->applyAclChangeset($entity, [], true)); |
||
395 | } |
||
396 | |||
397 | /** |
||
398 | * @throws \ReflectionException |
||
399 | */ |
||
400 | public function testApplyAclAppliesChangesetRecursive() |
||
401 | { |
||
402 | $object = $this->getInitializedPermissionAdmin(); |
||
403 | $acl = $this->createMock(MutableAclInterface::class); |
||
404 | $provider = $this->createMock(MutableAclProviderInterface::class); |
||
405 | $provider->expects($this->atLeastOnce())->method('findAcl')->willThrowException(new AclNotFoundException()); |
||
406 | $provider->expects($this->atLeastOnce())->method('createAcl')->willReturn($acl); |
||
407 | |||
408 | $mirror = new ReflectionClass(PermissionAdmin::class); |
||
409 | $property = $mirror->getProperty('aclProvider'); |
||
410 | $property->setAccessible(true); |
||
411 | $property->setValue($object, $provider); |
||
412 | |||
413 | $entity = new Node(); |
||
414 | $entity->setChildren(new ArrayCollection([new Node()])); |
||
415 | $this->assertNull($object->applyAclChangeset($entity, [], true)); |
||
416 | } |
||
417 | |||
418 | /** |
||
419 | * @throws \ReflectionException |
||
420 | */ |
||
421 | public function testApplyAclAppliesChangeset() |
||
422 | { |
||
423 | $object = $this->getInitializedPermissionAdmin(); |
||
424 | |||
425 | $entity = new Node(); |
||
426 | $this->assertNull($object->applyAclChangeset($entity, ['ROLE_TEST' => ['DEL' => ['VIEW']]], false)); |
||
427 | } |
||
428 | } |
||
429 |
If you access a property on an interface, you most likely code against a concrete implementation of the interface.
Available Fixes
Adding an additional type check:
Changing the type hint: