Completed
Push — master ( 1de9b7...830752 )
by Kristof
38:46 queued 24:09
created

Tests/unit/Helper/Security/Acl/AclHelperTest.php (25 issues)

Upgrade to new PHP Analysis Engine

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 Codeception\Stub;
6
use Doctrine\DBAL\Connection;
7
use Doctrine\DBAL\Driver\Statement;
8
use Doctrine\DBAL\Platforms\AbstractPlatform;
9
use Doctrine\ORM\Configuration;
10
use Doctrine\ORM\EntityManager;
11
use Doctrine\ORM\Mapping\ClassMetadata;
12
use Doctrine\ORM\Mapping\QuoteStrategy;
13
use Doctrine\ORM\NativeQuery;
14
use Doctrine\ORM\Query;
15
use Doctrine\ORM\QueryBuilder;
16
use FOS\UserBundle\Model\UserInterface;
17
use Kunstmaan\AdminBundle\Helper\Security\Acl\AclHelper;
18
use Kunstmaan\AdminBundle\Helper\Security\Acl\Permission\MaskBuilder;
19
use Kunstmaan\AdminBundle\Helper\Security\Acl\Permission\PermissionDefinition;
20
use PHPUnit\Framework\TestCase;
21
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
22
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
23
use Symfony\Component\Security\Core\Role\Role;
24
use Symfony\Component\Security\Core\Role\RoleHierarchyInterface;
25
26
class AclHelperTest extends TestCase
27
{
28
    /**
29
     * @var EntityManager
30
     */
31
    protected $em;
32
33
    /**
34
     * @var TokenStorageInterface
35
     */
36
    protected $tokenStorage;
37
38
    /**
39
     * @var RoleHierarchyInterface
40
     */
41
    protected $rh;
42
43
    /**
44
     * @var TokenInterface
45
     */
46
    protected $token;
47
48
    /**
49
     * @var UserInterface
50
     */
51
    protected $user;
52
53
    /**
54
     * @var AclHelper
55
     */
56
    protected $object;
57
58
    /**
59
     * Sets up the fixture, for example, opens a network connection.
60
     * This method is called before a test is executed.
61
     */
62
    protected function setUp()
63
    {
64
        $this->em = $this->getMockBuilder('Doctrine\ORM\EntityManager')
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->getMockBuilder('D...onstructor()->getMock() of type object<PHPUnit\Framework\MockObject\MockObject> is incompatible with the declared type object<Doctrine\ORM\EntityManager> of property $em.

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...
65
            ->disableOriginalConstructor()
66
            ->getMock();
67
68
        /* @var $conn Connection */
69
        $conn = $this->getMockBuilder('Doctrine\DBAL\Connection')
70
            ->disableOriginalConstructor()
71
            ->getMock();
72
73
        $conn->expects($this->any())
0 ignored issues
show
The method expects() does not seem to exist on object<Doctrine\DBAL\Connection>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
74
            ->method('getDatabase')
75
            ->will($this->returnValue('myDatabase'));
76
77
        /* @var $platform AbstractPlatform */
78
        $platform = $this->getMockForAbstractClass('Doctrine\DBAL\Platforms\AbstractPlatform');
79
80
        $conn->expects($this->any())
0 ignored issues
show
The method expects() does not seem to exist on object<Doctrine\DBAL\Connection>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
81
            ->method('getDatabasePlatform')
82
            ->will($this->returnValue($platform));
83
84
        /* @var $stmt Statement */
85
        $stmt = Stub::makeEmpty(Statement::class);
86
87
        $conn->expects($this->any())
0 ignored issues
show
The method expects() does not seem to exist on object<Doctrine\DBAL\Connection>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
88
            ->method('executeQuery')
89
            ->will($this->returnValue($stmt));
90
91
        $this->em->expects($this->any())
92
            ->method('getConnection')
93
            ->will($this->returnValue($conn));
94
95
        /* @var $conf Configuration */
96
        $conf = $this->getMockBuilder('Doctrine\ORM\Configuration')
97
            ->disableOriginalConstructor()
98
            ->getMock();
99
100
        /* @var $strat QuoteStrategy */
101
        $strat = $this->getMockBuilder('Doctrine\ORM\Mapping\QuoteStrategy')
102
            ->disableOriginalConstructor()
103
            ->getMock();
104
105
        $strat->expects($this->any())
0 ignored issues
show
The method expects() does not seem to exist on object<Doctrine\ORM\Mapping\QuoteStrategy>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
106
            ->method('getTableName')
107
            ->will($this->returnValue('rootTable'));
108
109
        $conf->expects($this->any())
0 ignored issues
show
The method expects() does not seem to exist on object<Doctrine\ORM\Configuration>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
110
            ->method('getQuoteStrategy')
111
            ->will($this->returnValue($strat));
112
113
        $conf->expects($this->any())
114
            ->method('getDefaultQueryHints')
115
            ->willReturn(array());
116
117
        $conf->expects($this->any())
118
            ->method('isSecondLevelCacheEnabled')
119
            ->willReturn(false);
120
121
        $this->em->expects($this->any())
122
            ->method('getConfiguration')
123
            ->will($this->returnValue($conf));
124
125
        /* @var $meta ClassMetadata */
126
        $meta = $this->getMockBuilder('Doctrine\ORM\Mapping\ClassMetadata')
127
            ->disableOriginalConstructor()
128
            ->getMock();
129
130
        $this->em->expects($this->any())
131
            ->method('getClassMetadata')
132
            ->will($this->returnValue($meta));
133
134
        $this->tokenStorage = $this->getMockBuilder('Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface')
135
            ->getMock();
136
137
        $this->token = $this->getMockBuilder('Symfony\Component\Security\Core\Authentication\Token\TokenInterface')
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->getMockBuilder('S...nInterface')->getMock() of type object<PHPUnit\Framework\MockObject\MockObject> is incompatible with the declared type object<Symfony\Component...n\Token\TokenInterface> of property $token.

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...
138
            ->getMock();
139
140
        $this->tokenStorage->expects($this->any())
141
            ->method('getToken')
142
            ->will($this->returnValue($this->token));
143
144
        $this->rh = $this->getMockBuilder('Symfony\Component\Security\Core\Role\RoleHierarchyInterface')
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->getMockBuilder('S...yInterface')->getMock() 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...
145
            ->getMock();
146
147
        $this->object = new AclHelper($this->em, $this->tokenStorage, $this->rh);
148
    }
149
150
    public function testConstructor()
151
    {
152
        new AclHelper($this->em, $this->tokenStorage, $this->rh);
153
    }
154
155
    public function testApply()
156
    {
157
        /* @var $queryBuilder QueryBuilder */
158
        $queryBuilder = $this->getMockBuilder('Doctrine\ORM\QueryBuilder')
159
            ->disableOriginalConstructor()
160
            ->getMock();
161
162
        $query = new Query($this->em);
163
        $query->setParameter('paramName', 'paramValue', 'paramType');
164
        $queryBuilder->expects($this->any())
0 ignored issues
show
The method expects() does not seem to exist on object<Doctrine\ORM\QueryBuilder>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
165
            ->method('getQuery')
166
            ->will($this->returnValue($query));
167
168
        $queryBuilder->expects($this->once())
0 ignored issues
show
The method expects() does not seem to exist on object<Doctrine\ORM\QueryBuilder>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
169
            ->method('getRootEntities')
170
            ->will($this->returnValue(array('Kunstmaan\NodeBundle\Entity\Node')));
171
172
        $queryBuilder->expects($this->once())
0 ignored issues
show
The method expects() does not seem to exist on object<Doctrine\ORM\QueryBuilder>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
173
            ->method('getRootAliases')
174
            ->will($this->returnValue(array('n')));
175
176
        $user = $this->getMockBuilder('FOS\UserBundle\Model\UserInterface')
177
            ->getMock();
178
179
        $user->expects($this->any())
180
            ->method('getUsername')
181
            ->will($this->returnValue('MyUser'));
182
183
        $this->token->expects($this->any())
0 ignored issues
show
The method expects() does not seem to exist on object<Symfony\Component...n\Token\TokenInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
184
            ->method('getUser')
185
            ->will($this->returnValue($user));
186
187
        $roles = array(new Role('ROLE_KING'));
188
        $allRoles = array($roles[0], new Role('ROLE_SUBJECT'));
189
190
        $this->token->expects($this->once())
0 ignored issues
show
The method expects() does not seem to exist on object<Symfony\Component...n\Token\TokenInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
191
            ->method('getRoles')
192
            ->will($this->returnValue($roles));
193
194
        $this->rh->expects($this->once())
0 ignored issues
show
The method expects() does not seem to exist on object<Symfony\Component...RoleHierarchyInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
195
            ->method('getReachableRoles')
196
            ->with($roles)
197
            ->will($this->returnValue($allRoles));
198
199
        $permissionDef = new PermissionDefinition(array('view'), 'Kunstmaan\NodeBundle\Entity\Node');
200
201
        /* @var $query Query */
202
        $query = $this->object->apply($queryBuilder, $permissionDef);
203
204
        $this->assertEquals(MaskBuilder::MASK_VIEW, $query->getHint('acl.mask'));
205
        $this->assertEquals($permissionDef->getEntity(), $query->getHint('acl.root.entity'));
206
        $this->assertEquals('rootTable', $query->getHint('acl.entityRootTableName'));
207
        $this->assertEquals('n', $query->getHint('acl.entityRootTableDqlAlias'));
208
209
        $aclQuery = $query->getHint('acl.extra.query');
210
        $this->assertContains('"ROLE_SUBJECT"', $aclQuery);
211
        $this->assertContains('"ROLE_KING"', $aclQuery);
212
        $this->assertContains('"IS_AUTHENTICATED_ANONYMOUSLY"', $aclQuery);
213
        $this->assertContains('MyUser', $aclQuery);
214
    }
215
216
    public function testApplyAnonymous()
217
    {
218
        /* @var $queryBuilder QueryBuilder */
219
        $queryBuilder = $this->getMockBuilder('Doctrine\ORM\QueryBuilder')
220
            ->disableOriginalConstructor()
221
            ->getMock();
222
223
        $query = new Query($this->em);
224
        $query->setParameter('paramName', 'paramValue', 'paramType');
225
        $queryBuilder->expects($this->any())
0 ignored issues
show
The method expects() does not seem to exist on object<Doctrine\ORM\QueryBuilder>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
226
            ->method('getQuery')
227
            ->will($this->returnValue($query));
228
229
        $queryBuilder->expects($this->once())
0 ignored issues
show
The method expects() does not seem to exist on object<Doctrine\ORM\QueryBuilder>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
230
            ->method('getRootEntities')
231
            ->will($this->returnValue(array('Kunstmaan\NodeBundle\Entity\Node')));
232
233
        $queryBuilder->expects($this->once())
0 ignored issues
show
The method expects() does not seem to exist on object<Doctrine\ORM\QueryBuilder>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
234
            ->method('getRootAliases')
235
            ->will($this->returnValue(array('n')));
236
237
        $roles = array();
238
239
        $this->token->expects($this->once())
0 ignored issues
show
The method expects() does not seem to exist on object<Symfony\Component...n\Token\TokenInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
240
            ->method('getRoles')
241
            ->will($this->returnValue($roles));
242
243
        $this->rh->expects($this->once())
0 ignored issues
show
The method expects() does not seem to exist on object<Symfony\Component...RoleHierarchyInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
244
            ->method('getReachableRoles')
245
            ->with($roles)
246
            ->will($this->returnValue($roles));
247
248
        $this->token->expects($this->any())
0 ignored issues
show
The method expects() does not seem to exist on object<Symfony\Component...n\Token\TokenInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
249
            ->method('getUser')
250
            ->will($this->returnValue('anon.'));
251
252
        $permissionDef = new PermissionDefinition(array('view'), 'Kunstmaan\NodeBundle\Entity\Node');
253
254
        /* @var $query Query */
255
        $query = $this->object->apply($queryBuilder, $permissionDef);
256
257
        $this->assertEquals(MaskBuilder::MASK_VIEW, $query->getHint('acl.mask'));
258
        $this->assertEquals($permissionDef->getEntity(), $query->getHint('acl.root.entity'));
259
        $this->assertEquals('rootTable', $query->getHint('acl.entityRootTableName'));
260
        $this->assertEquals('n', $query->getHint('acl.entityRootTableDqlAlias'));
261
262
        $aclQuery = $query->getHint('acl.extra.query');
263
        $this->assertContains('"IS_AUTHENTICATED_ANONYMOUSLY"', $aclQuery);
264
    }
265
266
    public function testGetAllowedEntityIds()
267
    {
268
        $roles = array(new Role('ROLE_KING'));
269
        $allRoles = array($roles[0], new Role('ROLE_SUBJECT'));
270
271
        $this->token->expects($this->once())
0 ignored issues
show
The method expects() does not seem to exist on object<Symfony\Component...n\Token\TokenInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
272
            ->method('getRoles')
273
            ->will($this->returnValue($roles));
274
275
        $this->rh->expects($this->once())
0 ignored issues
show
The method expects() does not seem to exist on object<Symfony\Component...RoleHierarchyInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
276
            ->method('getReachableRoles')
277
            ->with($roles)
278
            ->will($this->returnValue($allRoles));
279
280
        $user = $this->getMockBuilder('FOS\UserBundle\Model\UserInterface')
281
            ->getMock();
282
283
        $user->expects($this->any())
284
            ->method('getUsername')
285
            ->will($this->returnValue('MyUser'));
286
287
        $this->token->expects($this->any())
0 ignored issues
show
The method expects() does not seem to exist on object<Symfony\Component...n\Token\TokenInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
288
            ->method('getUser')
289
            ->will($this->returnValue($user));
290
291
        $hydrator = $this->getMockBuilder('Doctrine\ORM\Internal\Hydration\ScalarHydrator')
292
            ->disableOriginalConstructor()
293
            ->getMock();
294
295
        $rows = array(
296
            array('id' => 1),
297
            array('id' => 9),
298
        );
299
300
        $hydrator->expects($this->once())
301
            ->method('hydrateAll')
302
            ->will($this->returnValue($rows));
303
304
        $this->em->expects($this->any())
0 ignored issues
show
The method expects() does not seem to exist on object<Doctrine\ORM\EntityManager>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
305
          ->method('newHydrator') // was ->method('getHydrator')
306
          ->will($this->returnValue($hydrator));
307
308
        /* @var $query NativeQuery */
309
        $query = new NativeQuery($this->em);
310
311
        $this->em->expects($this->once())
0 ignored issues
show
The method expects() does not seem to exist on object<Doctrine\ORM\EntityManager>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
312
            ->method('createNativeQuery')
313
            ->will($this->returnValue($query));
314
315
        $permissionDef = new PermissionDefinition(array('view'), 'Kunstmaan\NodeBundle\Entity\Node', 'n');
316
317
        /* @var $result array */
318
        $result = $this->object->getAllowedEntityIds($permissionDef);
319
320
        $this->assertEquals(array(1, 9), $result);
321
    }
322
323
    public function testGetAllowedEntityIdsNoEntity()
324
    {
325
        $this->expectException('InvalidArgumentException');
326
327
        $this->object->getAllowedEntityIds(new PermissionDefinition(array('view')));
328
    }
329
330
    public function testGetTokenStorage()
331
    {
332
        $this->assertSame($this->tokenStorage, $this->object->getTokenStorage());
333
    }
334
}
335