Completed
Push — master ( 91fdab...75a7b9 )
by
unknown
13:37
created

Tests/unit/Helper/Security/Acl/AclHelperTest.php (23 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 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 \PHPUnit_Framework_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())
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...
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())
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...
80
            ->method('getDatabasePlatform')
81
            ->will($this->returnValue($platform));
82
83
        /* @var $stmt Statement */
84
        $stmt = Stub::makeEmpty(Statement::class);
85
86
        $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...
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())
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...
105
            ->method('getTableName')
106
            ->will($this->returnValue('rootTable'));
107
108
        $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...
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 testConstructor()
150
    {
151
        new AclHelper($this->em, $this->tokenStorage, $this->rh);
152
    }
153
154
    public function testApply()
155
    {
156
        /* @var $queryBuilder QueryBuilder */
157
        $queryBuilder = $this->getMockBuilder('Doctrine\ORM\QueryBuilder')
158
            ->disableOriginalConstructor()
159
            ->getMock();
160
161
        $query = new Query($this->em);
162
        $query->setParameter('paramName', 'paramValue', 'paramType');
163
        $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...
164
            ->method('getQuery')
165
            ->will($this->returnValue($query));
166
167
        $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...
168
            ->method('getRootEntities')
169
            ->will($this->returnValue(array('Kunstmaan\NodeBundle\Entity\Node')));
170
171
        $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...
172
            ->method('getRootAliases')
173
            ->will($this->returnValue(array('n')));
174
175
        $user = $this->getMockBuilder('FOS\UserBundle\Model\UserInterface')
176
            ->getMock();
177
178
        $user->expects($this->any())
179
            ->method('getUsername')
180
            ->will($this->returnValue('MyUser'));
181
182
        $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...
183
            ->method('getUser')
184
            ->will($this->returnValue($user));
185
186
        $roles = array(new Role('ROLE_KING'));
187
        $allRoles = array($roles[0], new Role('ROLE_SUBJECT'));
188
189
        $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...
190
            ->method('getRoles')
191
            ->will($this->returnValue($roles));
192
193
        $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...
194
            ->method('getReachableRoles')
195
            ->with($roles)
196
            ->will($this->returnValue($allRoles));
197
198
        $permissionDef = new PermissionDefinition(array('view'), 'Kunstmaan\NodeBundle\Entity\Node');
199
200
        /* @var $query Query */
201
        $query = $this->object->apply($queryBuilder, $permissionDef);
202
203
        $this->assertEquals(MaskBuilder::MASK_VIEW, $query->getHint('acl.mask'));
204
        $this->assertEquals($permissionDef->getEntity(), $query->getHint('acl.root.entity'));
205
        $this->assertEquals('rootTable', $query->getHint('acl.entityRootTableName'));
206
        $this->assertEquals('n', $query->getHint('acl.entityRootTableDqlAlias'));
207
208
        $aclQuery = $query->getHint('acl.extra.query');
209
        $this->assertContains('"ROLE_SUBJECT"', $aclQuery);
210
        $this->assertContains('"ROLE_KING"', $aclQuery);
211
        $this->assertContains('"IS_AUTHENTICATED_ANONYMOUSLY"', $aclQuery);
212
        $this->assertContains('MyUser', $aclQuery);
213
    }
214
215
    public function testApplyAnonymous()
216
    {
217
        /* @var $queryBuilder QueryBuilder */
218
        $queryBuilder = $this->getMockBuilder('Doctrine\ORM\QueryBuilder')
219
            ->disableOriginalConstructor()
220
            ->getMock();
221
222
        $query = new Query($this->em);
223
        $query->setParameter('paramName', 'paramValue', 'paramType');
224
        $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...
225
            ->method('getQuery')
226
            ->will($this->returnValue($query));
227
228
        $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...
229
            ->method('getRootEntities')
230
            ->will($this->returnValue(array('Kunstmaan\NodeBundle\Entity\Node')));
231
232
        $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...
233
            ->method('getRootAliases')
234
            ->will($this->returnValue(array('n')));
235
236
        $roles = array();
237
238
        $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...
239
            ->method('getRoles')
240
            ->will($this->returnValue($roles));
241
242
        $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...
243
            ->method('getReachableRoles')
244
            ->with($roles)
245
            ->will($this->returnValue($roles));
246
247
        $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...
248
            ->method('getUser')
249
            ->will($this->returnValue('anon.'));
250
251
        $permissionDef = new PermissionDefinition(array('view'), 'Kunstmaan\NodeBundle\Entity\Node');
252
253
        /* @var $query Query */
254
        $query = $this->object->apply($queryBuilder, $permissionDef);
255
256
        $this->assertEquals(MaskBuilder::MASK_VIEW, $query->getHint('acl.mask'));
257
        $this->assertEquals($permissionDef->getEntity(), $query->getHint('acl.root.entity'));
258
        $this->assertEquals('rootTable', $query->getHint('acl.entityRootTableName'));
259
        $this->assertEquals('n', $query->getHint('acl.entityRootTableDqlAlias'));
260
261
        $aclQuery = $query->getHint('acl.extra.query');
262
        $this->assertContains('"IS_AUTHENTICATED_ANONYMOUSLY"', $aclQuery);
263
    }
264
265
    public function testGetAllowedEntityIds()
266
    {
267
        $roles = array(new Role('ROLE_KING'));
268
        $allRoles = array($roles[0], new Role('ROLE_SUBJECT'));
269
270
        $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...
271
            ->method('getRoles')
272
            ->will($this->returnValue($roles));
273
274
        $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...
275
            ->method('getReachableRoles')
276
            ->with($roles)
277
            ->will($this->returnValue($allRoles));
278
279
        $user = $this->getMockBuilder('FOS\UserBundle\Model\UserInterface')
280
            ->getMock();
281
282
        $user->expects($this->any())
283
            ->method('getUsername')
284
            ->will($this->returnValue('MyUser'));
285
286
        $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...
287
            ->method('getUser')
288
            ->will($this->returnValue($user));
289
290
        $hydrator = $this->getMockBuilder('Doctrine\ORM\Internal\Hydration\ScalarHydrator')
291
            ->disableOriginalConstructor()
292
            ->getMock();
293
294
        $rows = array(
295
            array('id' => 1),
296
            array('id' => 9)
297
        );
298
299
        $hydrator->expects($this->once())
300
            ->method('hydrateAll')
301
            ->will($this->returnValue($rows));
302
303
        $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...
304
          ->method('newHydrator') // was ->method('getHydrator')
305
          ->will($this->returnValue($hydrator));
306
307
        /* @var $query NativeQuery */
308
        $query = new NativeQuery($this->em);
309
310
        $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...
311
            ->method('createNativeQuery')
312
            ->will($this->returnValue($query));
313
314
        $permissionDef = new PermissionDefinition(array('view'), 'Kunstmaan\NodeBundle\Entity\Node', 'n');
315
316
        /* @var $result array */
317
        $result = $this->object->getAllowedEntityIds($permissionDef);
318
319
        $this->assertEquals(array(1, 9), $result);
320
    }
321
322
    public function testGetAllowedEntityIdsNoEntity()
323
    {
324
        $this->setExpectedException('InvalidArgumentException');
0 ignored issues
show
Deprecated Code introduced by
The method PHPUnit_Framework_TestCase::setExpectedException() has been deprecated with message: Method deprecated since Release 5.2.0; use expectException() instead

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.

Loading history...
325
326
        $this->object->getAllowedEntityIds(new PermissionDefinition(array('view')));
327
    }
328
329
    public function testGetTokenStorage()
330
    {
331
        $this->assertSame($this->tokenStorage, $this->object->getTokenStorage());
332
    }
333
}
334