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; |
||
4 | |||
5 | use Doctrine\DBAL\Connection; |
||
6 | use Doctrine\DBAL\Driver\Statement; |
||
7 | use Doctrine\DBAL\Platforms\AbstractPlatform; |
||
8 | use Doctrine\ORM\Configuration; |
||
9 | use Doctrine\ORM\EntityManager; |
||
10 | use Doctrine\ORM\Mapping\ClassMetadata; |
||
11 | use Doctrine\ORM\Mapping\QuoteStrategy; |
||
12 | use Doctrine\ORM\NativeQuery; |
||
13 | use Doctrine\ORM\Query; |
||
14 | use Doctrine\ORM\QueryBuilder; |
||
15 | use FOS\UserBundle\Model\UserInterface; |
||
16 | use Kunstmaan\AdminBundle\Helper\Security\Acl\AclHelper; |
||
17 | use Kunstmaan\AdminBundle\Helper\Security\Acl\Permission\MaskBuilder; |
||
18 | use Kunstmaan\AdminBundle\Helper\Security\Acl\Permission\PermissionDefinition; |
||
19 | use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface; |
||
20 | use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; |
||
21 | use Symfony\Component\Security\Core\Role\Role; |
||
22 | use Symfony\Component\Security\Core\Role\RoleHierarchyInterface; |
||
23 | |||
24 | class AclHelperTest extends \PHPUnit_Framework_TestCase |
||
25 | { |
||
26 | /** |
||
27 | * @var EntityManager |
||
28 | */ |
||
29 | protected $em; |
||
30 | |||
31 | /** |
||
32 | * @var TokenStorageInterface |
||
33 | */ |
||
34 | protected $tokenStorage; |
||
35 | |||
36 | /** |
||
37 | * @var RoleHierarchyInterface |
||
38 | */ |
||
39 | protected $rh; |
||
40 | |||
41 | /** |
||
42 | * @var TokenInterface |
||
43 | */ |
||
44 | protected $token; |
||
45 | |||
46 | /** |
||
47 | * @var UserInterface |
||
48 | */ |
||
49 | protected $user; |
||
50 | |||
51 | /** |
||
52 | * @var AclHelper |
||
53 | */ |
||
54 | protected $object; |
||
55 | |||
56 | /** |
||
57 | * Sets up the fixture, for example, opens a network connection. |
||
58 | * This method is called before a test is executed. |
||
59 | */ |
||
60 | protected function setUp() |
||
61 | { |
||
62 | $this->em = $this->getMockBuilder('Doctrine\ORM\EntityManager') |
||
63 | ->disableOriginalConstructor() |
||
64 | ->getMock(); |
||
65 | |||
66 | /* @var $conn Connection */ |
||
67 | $conn = $this->getMockBuilder('Doctrine\DBAL\Connection') |
||
68 | ->disableOriginalConstructor() |
||
69 | ->getMock(); |
||
70 | |||
71 | $conn->expects($this->any()) |
||
72 | ->method('getDatabase') |
||
73 | ->will($this->returnValue('myDatabase')); |
||
74 | |||
75 | /* @var $platform AbstractPlatform */ |
||
76 | $platform = $this->getMockForAbstractClass('Doctrine\DBAL\Platforms\AbstractPlatform'); |
||
77 | |||
78 | $conn->expects($this->any()) |
||
79 | ->method('getDatabasePlatform') |
||
80 | ->will($this->returnValue($platform)); |
||
81 | |||
82 | /* @var $stmt Statement */ |
||
83 | $stmt = $this->getMockForAbstractClass('Kunstmaan\AdminBundle\Tests\Mocks\StatementMock'); |
||
84 | |||
85 | $conn->expects($this->any()) |
||
86 | ->method('executeQuery') |
||
87 | ->will($this->returnValue($stmt)); |
||
88 | |||
89 | $this->em->expects($this->any()) |
||
90 | ->method('getConnection') |
||
91 | ->will($this->returnValue($conn)); |
||
92 | |||
93 | /* @var $conf Configuration */ |
||
94 | $conf = $this->getMockBuilder('Doctrine\ORM\Configuration') |
||
95 | ->disableOriginalConstructor() |
||
96 | ->getMock(); |
||
97 | |||
98 | /* @var $strat QuoteStrategy */ |
||
99 | $strat = $this->getMockBuilder('Doctrine\ORM\Mapping\QuoteStrategy') |
||
100 | ->disableOriginalConstructor() |
||
101 | ->getMock(); |
||
102 | |||
103 | $strat->expects($this->any()) |
||
104 | ->method('getTableName') |
||
105 | ->will($this->returnValue('rootTable')); |
||
106 | |||
107 | $conf->expects($this->any()) |
||
108 | ->method('getQuoteStrategy') |
||
109 | ->will($this->returnValue($strat)); |
||
110 | |||
111 | $conf->expects($this->any()) |
||
112 | ->method('getDefaultQueryHints') |
||
113 | ->willReturn(array()); |
||
114 | |||
115 | $conf->expects($this->any()) |
||
116 | ->method('isSecondLevelCacheEnabled') |
||
117 | ->willReturn(false); |
||
118 | |||
119 | $this->em->expects($this->any()) |
||
120 | ->method('getConfiguration') |
||
121 | ->will($this->returnValue($conf)); |
||
122 | |||
123 | /* @var $meta ClassMetadata */ |
||
124 | $meta = $this->getMockBuilder('Doctrine\ORM\Mapping\ClassMetadata') |
||
125 | ->disableOriginalConstructor() |
||
126 | ->getMock(); |
||
127 | |||
128 | $this->em->expects($this->any()) |
||
129 | ->method('getClassMetadata') |
||
130 | ->will($this->returnValue($meta)); |
||
131 | |||
132 | $this->tokenStorage = $this->getMockBuilder('Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface') |
||
133 | ->getMock(); |
||
134 | |||
135 | $this->token = $this->getMockBuilder('Symfony\Component\Security\Core\Authentication\Token\TokenInterface') |
||
136 | ->getMock(); |
||
137 | |||
138 | $this->tokenStorage->expects($this->any()) |
||
139 | ->method('getToken') |
||
140 | ->will($this->returnValue($this->token)); |
||
141 | |||
142 | $this->rh = $this->getMockBuilder('Symfony\Component\Security\Core\Role\RoleHierarchyInterface') |
||
143 | ->getMock(); |
||
144 | |||
145 | $this->object = new AclHelper($this->em, $this->tokenStorage, $this->rh); |
||
146 | } |
||
147 | |||
148 | /** |
||
149 | * Tears down the fixture, for example, closes a network connection. |
||
150 | * This method is called after a test is executed. |
||
151 | */ |
||
152 | protected function tearDown() |
||
153 | { |
||
154 | } |
||
155 | |||
156 | /** |
||
157 | * @covers Kunstmaan\AdminBundle\Helper\Security\Acl\AclHelper::__construct |
||
158 | */ |
||
159 | public function testConstructor() |
||
160 | { |
||
161 | new AclHelper($this->em, $this->tokenStorage, $this->rh); |
||
162 | } |
||
163 | |||
164 | /** |
||
165 | * @covers Kunstmaan\AdminBundle\Helper\Security\Acl\AclHelper::apply |
||
166 | * @covers Kunstmaan\AdminBundle\Helper\Security\Acl\AclHelper::cloneQuery |
||
167 | */ |
||
168 | public function testApply() |
||
169 | { |
||
170 | /* @var $queryBuilder QueryBuilder */ |
||
171 | $queryBuilder = $this->getMockBuilder('Doctrine\ORM\QueryBuilder') |
||
172 | ->disableOriginalConstructor() |
||
173 | ->getMock(); |
||
174 | |||
175 | $query = new Query($this->em); |
||
176 | $query->setParameter('paramName', 'paramValue', 'paramType'); |
||
177 | $queryBuilder->expects($this->any()) |
||
178 | ->method('getQuery') |
||
179 | ->will($this->returnValue($query)); |
||
180 | |||
181 | $queryBuilder->expects($this->once()) |
||
182 | ->method('getRootEntities') |
||
183 | ->will($this->returnValue(array('Kunstmaan\NodeBundle\Entity\Node'))); |
||
184 | |||
185 | $queryBuilder->expects($this->once()) |
||
186 | ->method('getRootAliases') |
||
187 | ->will($this->returnValue(array('n'))); |
||
188 | |||
189 | $user = $this->getMockBuilder('FOS\UserBundle\Model\UserInterface') |
||
190 | ->getMock(); |
||
191 | |||
192 | $user->expects($this->any()) |
||
193 | ->method('getUsername') |
||
194 | ->will($this->returnValue('MyUser')); |
||
195 | |||
196 | $this->token->expects($this->any()) |
||
197 | ->method('getUser') |
||
198 | ->will($this->returnValue($user)); |
||
199 | |||
200 | $roles = array(new Role('ROLE_KING')); |
||
201 | $allRoles = array($roles[0], new Role('ROLE_SUBJECT')); |
||
202 | |||
203 | $this->token->expects($this->once()) |
||
204 | ->method('getRoles') |
||
205 | ->will($this->returnValue($roles)); |
||
206 | |||
207 | $this->rh->expects($this->once()) |
||
208 | ->method('getReachableRoles') |
||
209 | ->with($roles) |
||
210 | ->will($this->returnValue($allRoles)); |
||
211 | |||
212 | $permissionDef = new PermissionDefinition(array('view'), 'Kunstmaan\NodeBundle\Entity\Node'); |
||
213 | |||
214 | /* @var $query Query */ |
||
215 | $query = $this->object->apply($queryBuilder, $permissionDef); |
||
216 | |||
217 | $this->assertEquals(MaskBuilder::MASK_VIEW, $query->getHint('acl.mask')); |
||
218 | $this->assertEquals($permissionDef->getEntity(), $query->getHint('acl.root.entity')); |
||
219 | $this->assertEquals('rootTable', $query->getHint('acl.entityRootTableName')); |
||
220 | $this->assertEquals('n', $query->getHint('acl.entityRootTableDqlAlias')); |
||
221 | |||
222 | $aclQuery = $query->getHint('acl.extra.query'); |
||
223 | $this->assertContains('"ROLE_SUBJECT"', $aclQuery); |
||
224 | $this->assertContains('"ROLE_KING"', $aclQuery); |
||
225 | $this->assertContains('"IS_AUTHENTICATED_ANONYMOUSLY"', $aclQuery); |
||
226 | $this->assertContains('MyUser', $aclQuery); |
||
227 | } |
||
228 | |||
229 | /** |
||
230 | * @covers Kunstmaan\AdminBundle\Helper\Security\Acl\AclHelper::apply |
||
231 | * @covers Kunstmaan\AdminBundle\Helper\Security\Acl\AclHelper::cloneQuery |
||
232 | */ |
||
233 | public function testApplyAnonymous() |
||
234 | { |
||
235 | /* @var $queryBuilder QueryBuilder */ |
||
236 | $queryBuilder = $this->getMockBuilder('Doctrine\ORM\QueryBuilder') |
||
237 | ->disableOriginalConstructor() |
||
238 | ->getMock(); |
||
239 | |||
240 | $query = new Query($this->em); |
||
241 | $query->setParameter('paramName', 'paramValue', 'paramType'); |
||
242 | $queryBuilder->expects($this->any()) |
||
243 | ->method('getQuery') |
||
244 | ->will($this->returnValue($query)); |
||
245 | |||
246 | $queryBuilder->expects($this->once()) |
||
247 | ->method('getRootEntities') |
||
248 | ->will($this->returnValue(array('Kunstmaan\NodeBundle\Entity\Node'))); |
||
249 | |||
250 | $queryBuilder->expects($this->once()) |
||
251 | ->method('getRootAliases') |
||
252 | ->will($this->returnValue(array('n'))); |
||
253 | |||
254 | $roles = array(); |
||
255 | |||
256 | $this->token->expects($this->once()) |
||
257 | ->method('getRoles') |
||
258 | ->will($this->returnValue($roles)); |
||
259 | |||
260 | $this->rh->expects($this->once()) |
||
261 | ->method('getReachableRoles') |
||
262 | ->with($roles) |
||
263 | ->will($this->returnValue($roles)); |
||
264 | |||
265 | $this->token->expects($this->any()) |
||
266 | ->method('getUser') |
||
267 | ->will($this->returnValue('anon.')); |
||
268 | |||
269 | $permissionDef = new PermissionDefinition(array('view'), 'Kunstmaan\NodeBundle\Entity\Node'); |
||
270 | |||
271 | /* @var $query Query */ |
||
272 | $query = $this->object->apply($queryBuilder, $permissionDef); |
||
273 | |||
274 | $this->assertEquals(MaskBuilder::MASK_VIEW, $query->getHint('acl.mask')); |
||
275 | $this->assertEquals($permissionDef->getEntity(), $query->getHint('acl.root.entity')); |
||
276 | $this->assertEquals('rootTable', $query->getHint('acl.entityRootTableName')); |
||
277 | $this->assertEquals('n', $query->getHint('acl.entityRootTableDqlAlias')); |
||
278 | |||
279 | $aclQuery = $query->getHint('acl.extra.query'); |
||
280 | $this->assertContains('"IS_AUTHENTICATED_ANONYMOUSLY"', $aclQuery); |
||
281 | } |
||
282 | |||
283 | /** |
||
284 | * @covers Kunstmaan\AdminBundle\Helper\Security\Acl\AclHelper::getAllowedEntityIds |
||
285 | * @covers Kunstmaan\AdminBundle\Helper\Security\Acl\AclHelper::getPermittedAclIdsSQLForUser |
||
286 | */ |
||
287 | public function testGetAllowedEntityIds() |
||
288 | { |
||
289 | $roles = array(new Role('ROLE_KING')); |
||
290 | $allRoles = array($roles[0], new Role('ROLE_SUBJECT')); |
||
291 | |||
292 | $this->token->expects($this->once()) |
||
293 | ->method('getRoles') |
||
294 | ->will($this->returnValue($roles)); |
||
295 | |||
296 | $this->rh->expects($this->once()) |
||
297 | ->method('getReachableRoles') |
||
298 | ->with($roles) |
||
299 | ->will($this->returnValue($allRoles)); |
||
300 | |||
301 | $user = $this->getMockBuilder('FOS\UserBundle\Model\UserInterface') |
||
302 | ->getMock(); |
||
303 | |||
304 | $user->expects($this->any()) |
||
305 | ->method('getUsername') |
||
306 | ->will($this->returnValue('MyUser')); |
||
307 | |||
308 | $this->token->expects($this->any()) |
||
309 | ->method('getUser') |
||
310 | ->will($this->returnValue($user)); |
||
311 | |||
312 | $hydrator = $this->getMockBuilder('Doctrine\ORM\Internal\Hydration\ScalarHydrator') |
||
313 | ->disableOriginalConstructor() |
||
314 | ->getMock(); |
||
315 | |||
316 | $rows = array( |
||
317 | array('id' => 1), |
||
318 | array('id' => 9) |
||
319 | ); |
||
320 | |||
321 | $hydrator->expects($this->once()) |
||
322 | ->method('hydrateAll') |
||
323 | ->will($this->returnValue($rows)); |
||
324 | |||
325 | $this->em->expects($this->any()) |
||
326 | ->method('newHydrator') // was ->method('getHydrator') |
||
327 | ->will($this->returnValue($hydrator)); |
||
328 | |||
329 | /* @var $query NativeQuery */ |
||
330 | $query = new NativeQuery($this->em); |
||
331 | |||
332 | $this->em->expects($this->once()) |
||
333 | ->method('createNativeQuery') |
||
334 | ->will($this->returnValue($query)); |
||
335 | |||
336 | $permissionDef = new PermissionDefinition(array('view'), 'Kunstmaan\NodeBundle\Entity\Node', 'n'); |
||
337 | |||
338 | /* @var $result array */ |
||
339 | $result = $this->object->getAllowedEntityIds($permissionDef); |
||
340 | |||
341 | $this->assertEquals(array(1, 9), $result); |
||
342 | } |
||
343 | |||
344 | /** |
||
345 | * @covers Kunstmaan\AdminBundle\Helper\Security\Acl\AclHelper::getAllowedEntityIds |
||
346 | */ |
||
347 | public function testGetAllowedEntityIdsNoEntity() |
||
348 | { |
||
349 | $this->setExpectedException('InvalidArgumentException'); |
||
0 ignored issues
–
show
|
|||
350 | |||
351 | $this->object->getAllowedEntityIds(new PermissionDefinition(array('view'))); |
||
352 | } |
||
353 | |||
354 | /** |
||
355 | * @covers Kunstmaan\AdminBundle\Helper\Security\Acl\AclHelper::getTokenStorage |
||
356 | */ |
||
357 | public function testGetTokenStorage() |
||
358 | { |
||
359 | $this->assertSame($this->tokenStorage, $this->object->getTokenStorage()); |
||
360 | } |
||
361 | } |
||
362 |
This method has been deprecated. The supplier of the class has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.