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 PHPUnit\Framework\TestCase; |
||
20 | use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface; |
||
21 | use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; |
||
22 | use Symfony\Component\Security\Core\Role\RoleHierarchyInterface; |
||
23 | |||
24 | class AclHelperTest extends 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->createMock(Statement::class); |
||
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') |
||
0 ignored issues
–
show
|
|||
136 | ->setMethods(['getRoleNames']) |
||
137 | ->getMockForAbstractClass(); |
||
138 | |||
139 | $this->tokenStorage->expects($this->any()) |
||
140 | ->method('getToken') |
||
141 | ->will($this->returnValue($this->token)); |
||
142 | |||
143 | $this->rh = $this->getMockBuilder('Symfony\Component\Security\Core\Role\RoleHierarchyInterface') |
||
0 ignored issues
–
show
It seems like
$this->getMockBuilder('S...tMockForAbstractClass() of type object<PHPUnit\Framework\MockObject\MockObject> is incompatible with the declared type object<Symfony\Component...RoleHierarchyInterface> of property $rh .
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property. Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..
Loading history...
|
|||
144 | ->setMethods(['getReachableRoleNames']) |
||
145 | ->getMockForAbstractClass(); |
||
146 | |||
147 | $this->object = new AclHelper($this->em, $this->tokenStorage, $this->rh); |
||
148 | } |
||
149 | |||
150 | public function testApply() |
||
151 | { |
||
152 | /* @var $queryBuilder QueryBuilder */ |
||
153 | $queryBuilder = $this->getMockBuilder('Doctrine\ORM\QueryBuilder') |
||
154 | ->disableOriginalConstructor() |
||
155 | ->getMock(); |
||
156 | |||
157 | $query = new Query($this->em); |
||
158 | $query->setParameter('paramName', 'paramValue', 'paramType'); |
||
159 | $queryBuilder->expects($this->any()) |
||
160 | ->method('getQuery') |
||
161 | ->will($this->returnValue($query)); |
||
162 | |||
163 | $queryBuilder->expects($this->once()) |
||
164 | ->method('getRootEntities') |
||
165 | ->will($this->returnValue(array('Kunstmaan\NodeBundle\Entity\Node'))); |
||
166 | |||
167 | $queryBuilder->expects($this->once()) |
||
168 | ->method('getRootAliases') |
||
169 | ->will($this->returnValue(array('n'))); |
||
170 | |||
171 | $user = $this->getMockBuilder('FOS\UserBundle\Model\UserInterface') |
||
172 | ->getMock(); |
||
173 | |||
174 | $user->expects($this->any()) |
||
175 | ->method('getUsername') |
||
176 | ->will($this->returnValue('MyUser')); |
||
177 | |||
178 | $this->token->expects($this->any()) |
||
179 | ->method('getUser') |
||
180 | ->will($this->returnValue($user)); |
||
181 | |||
182 | $roles = array('ROLE_KING'); |
||
183 | $allRoles = array($roles[0], 'ROLE_SUBJECT'); |
||
184 | |||
185 | $this->token->expects($this->once()) |
||
186 | ->method('getRoleNames') |
||
187 | ->will($this->returnValue($roles)); |
||
188 | |||
189 | $this->rh->expects($this->once()) |
||
190 | ->method('getReachableRoleNames') |
||
191 | ->with($roles) |
||
192 | ->will($this->returnValue($allRoles)); |
||
193 | |||
194 | $permissionDef = new PermissionDefinition(array('view'), 'Kunstmaan\NodeBundle\Entity\Node'); |
||
195 | |||
196 | /* @var $query Query */ |
||
197 | $query = $this->object->apply($queryBuilder, $permissionDef); |
||
198 | |||
199 | $this->assertEquals(MaskBuilder::MASK_VIEW, $query->getHint('acl.mask')); |
||
200 | $this->assertEquals($permissionDef->getEntity(), $query->getHint('acl.root.entity')); |
||
201 | $this->assertEquals('rootTable', $query->getHint('acl.entityRootTableName')); |
||
202 | $this->assertEquals('n', $query->getHint('acl.entityRootTableDqlAlias')); |
||
203 | |||
204 | $aclQuery = $query->getHint('acl.extra.query'); |
||
205 | $this->assertContains('"ROLE_SUBJECT"', $aclQuery); |
||
206 | $this->assertContains('"ROLE_KING"', $aclQuery); |
||
207 | $this->assertContains('"IS_AUTHENTICATED_ANONYMOUSLY"', $aclQuery); |
||
208 | $this->assertContains('MyUser', $aclQuery); |
||
209 | } |
||
210 | |||
211 | public function testApplyAnonymous() |
||
212 | { |
||
213 | /* @var $queryBuilder QueryBuilder */ |
||
214 | $queryBuilder = $this->getMockBuilder('Doctrine\ORM\QueryBuilder') |
||
215 | ->disableOriginalConstructor() |
||
216 | ->getMock(); |
||
217 | |||
218 | $query = new Query($this->em); |
||
219 | $query->setParameter('paramName', 'paramValue', 'paramType'); |
||
220 | $queryBuilder->expects($this->any()) |
||
221 | ->method('getQuery') |
||
222 | ->will($this->returnValue($query)); |
||
223 | |||
224 | $queryBuilder->expects($this->once()) |
||
225 | ->method('getRootEntities') |
||
226 | ->will($this->returnValue(array('Kunstmaan\NodeBundle\Entity\Node'))); |
||
227 | |||
228 | $queryBuilder->expects($this->once()) |
||
229 | ->method('getRootAliases') |
||
230 | ->will($this->returnValue(array('n'))); |
||
231 | |||
232 | $roles = array(); |
||
233 | |||
234 | $this->token->expects($this->once()) |
||
235 | ->method('getRoleNames') |
||
236 | ->will($this->returnValue($roles)); |
||
237 | |||
238 | $this->rh->expects($this->once()) |
||
239 | ->method('getReachableRoleNames') |
||
240 | ->with($roles) |
||
241 | ->willReturn($roles); |
||
242 | |||
243 | $this->token->expects($this->any()) |
||
244 | ->method('getUser') |
||
245 | ->will($this->returnValue('anon.')); |
||
246 | |||
247 | $permissionDef = new PermissionDefinition(array('view'), 'Kunstmaan\NodeBundle\Entity\Node'); |
||
248 | |||
249 | /* @var $query Query */ |
||
250 | $query = $this->object->apply($queryBuilder, $permissionDef); |
||
251 | |||
252 | $this->assertEquals(MaskBuilder::MASK_VIEW, $query->getHint('acl.mask')); |
||
253 | $this->assertEquals($permissionDef->getEntity(), $query->getHint('acl.root.entity')); |
||
254 | $this->assertEquals('rootTable', $query->getHint('acl.entityRootTableName')); |
||
255 | $this->assertEquals('n', $query->getHint('acl.entityRootTableDqlAlias')); |
||
256 | |||
257 | $aclQuery = $query->getHint('acl.extra.query'); |
||
258 | $this->assertContains('"IS_AUTHENTICATED_ANONYMOUSLY"', $aclQuery); |
||
259 | } |
||
260 | |||
261 | public function testGetAllowedEntityIds() |
||
262 | { |
||
263 | $roles = array('ROLE_KING'); |
||
264 | $allRoles = array($roles[0], 'ROLE_SUBJECT'); |
||
265 | |||
266 | $this->token->expects($this->once()) |
||
267 | ->method('getRoleNames') |
||
268 | ->will($this->returnValue($roles)); |
||
269 | |||
270 | $this->rh->expects($this->once()) |
||
271 | ->method('getReachableRoleNames') |
||
272 | ->with($roles) |
||
273 | ->will($this->returnValue($allRoles)); |
||
274 | |||
275 | $user = $this->getMockBuilder('FOS\UserBundle\Model\UserInterface') |
||
276 | ->getMock(); |
||
277 | |||
278 | $user->expects($this->any()) |
||
279 | ->method('getUsername') |
||
280 | ->will($this->returnValue('MyUser')); |
||
281 | |||
282 | $this->token->expects($this->any()) |
||
283 | ->method('getUser') |
||
284 | ->will($this->returnValue($user)); |
||
285 | |||
286 | $hydrator = $this->getMockBuilder('Doctrine\ORM\Internal\Hydration\ScalarHydrator') |
||
287 | ->disableOriginalConstructor() |
||
288 | ->getMock(); |
||
289 | |||
290 | $rows = array( |
||
291 | array('id' => 1), |
||
292 | array('id' => 9), |
||
293 | ); |
||
294 | |||
295 | $hydrator->expects($this->once()) |
||
296 | ->method('hydrateAll') |
||
297 | ->will($this->returnValue($rows)); |
||
298 | |||
299 | $this->em->expects($this->any()) |
||
300 | ->method('newHydrator') // was ->method('getHydrator') |
||
301 | ->will($this->returnValue($hydrator)); |
||
302 | |||
303 | /* @var $query NativeQuery */ |
||
304 | $query = new NativeQuery($this->em); |
||
305 | |||
306 | $this->em->expects($this->once()) |
||
307 | ->method('createNativeQuery') |
||
308 | ->will($this->returnValue($query)); |
||
309 | |||
310 | $permissionDef = new PermissionDefinition(array('view'), 'Kunstmaan\NodeBundle\Entity\Node', 'n'); |
||
311 | |||
312 | /* @var $result array */ |
||
313 | $result = $this->object->getAllowedEntityIds($permissionDef); |
||
314 | |||
315 | $this->assertEquals(array(1, 9), $result); |
||
316 | } |
||
317 | |||
318 | public function testGetAllowedEntityIdsNoEntity() |
||
319 | { |
||
320 | $this->expectException('InvalidArgumentException'); |
||
321 | |||
322 | $this->object->getAllowedEntityIds(new PermissionDefinition(array('view'))); |
||
323 | } |
||
324 | |||
325 | public function testGetTokenStorage() |
||
326 | { |
||
327 | $this->assertSame($this->tokenStorage, $this->object->getTokenStorage()); |
||
328 | } |
||
329 | } |
||
330 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..