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