Completed
Pull Request — 5.0 (#2104)
by Kevin
13:45 queued 04:21
created

Security/Acl/Permission/PermissionAdminTest.php (1 issue)

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\Permission;
4
5
use Doctrine\ORM\EntityManager;
6
use Kunstmaan\AdminBundle\Entity\AbstractEntity;
7
use Kunstmaan\AdminBundle\Entity\User;
8
use Kunstmaan\AdminBundle\Helper\Security\Acl\Permission\MaskBuilder;
9
use Kunstmaan\AdminBundle\Helper\Security\Acl\Permission\PermissionAdmin;
10
use Kunstmaan\AdminBundle\Helper\Security\Acl\Permission\PermissionMapInterface;
11
use Kunstmaan\UtilitiesBundle\Helper\Shell\Shell;
12
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
13
use Symfony\Component\HttpKernel\KernelInterface;
14
use Symfony\Component\Security\Acl\Domain\RoleSecurityIdentity;
15
use Symfony\Component\Security\Acl\Model\AclProviderInterface;
16
use Symfony\Component\Security\Acl\Model\ObjectIdentityRetrievalStrategyInterface;
17
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
18
19
class PermissionAdminTest extends \PHPUnit_Framework_TestCase
20
{
21
    /**
22
     * @var PermissionAdmin $object
23
     */
24
    protected $object;
25
26
    /**
27
     * Sets up the fixture, for example, opens a network connection.
28
     * This method is called before a test is executed.
29
     */
30
    protected function setUp()
31
    {
32
    }
33
34
    /**
35
     * Tears down the fixture, for example, closes a network connection.
36
     * This method is called after a test is executed.
37
     */
38
    protected function tearDown()
39
    {
40
    }
41
42
    /**
43
     * @covers Kunstmaan\AdminBundle\Helper\Security\Acl\Permission\PermissionAdmin::initialize
44
     * @covers Kunstmaan\AdminBundle\Helper\Security\Acl\Permission\PermissionAdmin::getPermissions
45
     */
46
    public function testInitialize()
47
    {
48
        $object = $this->getInitializedPermissionAdmin();
49
50
        $this->assertEquals(array('ROLE_TEST' => new MaskBuilder(1)), $object->getPermissions());
51
    }
52
53
    /**
54
     * @covers Kunstmaan\AdminBundle\Helper\Security\Acl\Permission\PermissionAdmin::initialize
55
     * @covers Kunstmaan\AdminBundle\Helper\Security\Acl\Permission\PermissionAdmin::getPermission
56
     */
57
    public function testGetPermissionWithString()
58
    {
59
        $object = $this->getInitializedPermissionAdmin();
60
61
        $this->assertEquals(new MaskBuilder(1), $object->getPermission('ROLE_TEST'));
62
    }
63
64
    /**
65
     * @covers Kunstmaan\AdminBundle\Helper\Security\Acl\Permission\PermissionAdmin::initialize
66
     * @covers Kunstmaan\AdminBundle\Helper\Security\Acl\Permission\PermissionAdmin::getPermission
67
     */
68
    public function testGetPermissionWithRoleObject()
69
    {
70
        $object = $this->getInitializedPermissionAdmin();
71
72
        $role = $this->getMock('Symfony\Component\Security\Core\Role\RoleInterface');
73
        $role->expects($this->once())
74
            ->method('getRole')
75
            ->will($this->returnValue('ROLE_TEST'));
76
        $this->assertEquals(new MaskBuilder(1), $object->getPermission($role));
77
    }
78
79
    /**
80
     * @covers Kunstmaan\AdminBundle\Helper\Security\Acl\Permission\PermissionAdmin::initialize
81
     * @covers Kunstmaan\AdminBundle\Helper\Security\Acl\Permission\PermissionAdmin::getPermission
82
     */
83
    public function testGetPermissionWithUnknownRole()
84
    {
85
        $object = $this->getInitializedPermissionAdmin();
86
87
        $this->assertNull($object->getPermission('ROLE_UNKNOWN'));
88
    }
89
90
    /**
91
     * @covers Kunstmaan\AdminBundle\Helper\Security\Acl\Permission\PermissionAdmin::__construct
92
     * @covers Kunstmaan\AdminBundle\Helper\Security\Acl\Permission\PermissionAdmin::getAllRoles
93
     */
94
    public function testGetAllRoles()
95
    {
96
        $roleRepo = $this->getMockBuilder('Doctrine\ORM\EntityRepository')
97
            ->disableOriginalConstructor()
98
            ->getMock();
99
        $roleRepo->expects($this->once())
100
            ->method('findAll')
101
            ->will($this->returnValue(null));
102
103
        $em = $this->getEntityManager();
104
        $em->expects($this->once())
105
            ->method('getRepository')
106
            ->with('KunstmaanAdminBundle:Role')
107
            ->will($this->returnValue($roleRepo));
108
        $context = $this->getTokenStorage();
109
        $aclProvider = $this->getAclProvider();
110
        $retrievalStrategy = $this->getOidRetrievalStrategy();
111
        $dispatcher = $this->getEventDispatcher();
112
        $shell = $this->getShell();
113
        $kernel = $this->getKernel();
114
        $object = new PermissionAdmin($em, $context, $aclProvider, $retrievalStrategy, $dispatcher, $shell, $kernel);
115
116
        $this->assertNull($object->getAllRoles());
117
    }
118
119
    /**
120
     * @covers Kunstmaan\AdminBundle\Helper\Security\Acl\Permission\PermissionAdmin::__construct
121
     * @covers Kunstmaan\AdminBundle\Helper\Security\Acl\Permission\PermissionAdmin::getPossiblePermissions
122
     * @covers Kunstmaan\AdminBundle\Helper\Security\Acl\Permission\PermissionAdmin::initialize
123
     */
124
    public function testGetPossiblePermissions()
125
    {
126
        $em = $this->getEntityManager();
127
        $context = $this->getTokenStorage();
128
        $aclProvider = $this->getAclProvider();
129
        $retrievalStrategy = $this->getOidRetrievalStrategy();
130
        $retrievalStrategy
131
            ->expects($this->once())
132
            ->method('getObjectIdentity')
133
            ->will($this->throwException(new \Symfony\Component\Security\Acl\Exception\AclNotFoundException()));
134
        $dispatcher = $this->getEventDispatcher();
135
        $shell = $this->getShell();
136
        $kernel = $this->getKernel();
137
        $object = new PermissionAdmin($em, $context, $aclProvider, $retrievalStrategy, $dispatcher, $shell, $kernel);
138
139
        $permissions = array('PERMISSION1', 'PERMISSION2');
140
        $permissionMap = $this->getMock('Kunstmaan\AdminBundle\Helper\Security\Acl\Permission\PermissionMapInterface');
141
        $permissionMap
142
            ->expects($this->any())
143
            ->method('getPossiblePermissions')
144
            ->will($this->returnValue($permissions));
145
        $entity = $this->getEntity();
146
        /* @var $permissionMap PermissionMapInterface */
147
        $object->initialize($entity, $permissionMap);
148
        $this->assertEquals($permissions, $object->getPossiblePermissions());
149
    }
150
151
    /**
152
     * @covers Kunstmaan\AdminBundle\Helper\Security\Acl\Permission\PermissionAdmin::createAclChangeset
153
     */
154
    public function testCreateAclChangeset()
155
    {
156
        $em = $this->getEntityManager();
157
        $em->expects($this->once())
158
            ->method('persist')
159
            ->with($this->isInstanceOf('Kunstmaan\AdminBundle\Entity\AclChangeset'));
160
        $em->expects($this->once())
161
            ->method('flush');
162
        $context = $this->getTokenStorage();
163
        $aclProvider = $this->getAclProvider();
164
        $retrievalStrategy = $this->getOidRetrievalStrategy();
165
        $dispatcher = $this->getEventDispatcher();
166
        $shell = $this->getShell();
167
        $kernel = $this->getKernel();
168
        $object = new PermissionAdmin($em, $context, $aclProvider, $retrievalStrategy, $dispatcher, $shell, $kernel);
169
170
        $entity = $this->getEntity();
171
        /* @var $user User */
172
        $user = $this->getMockBuilder('Kunstmaan\AdminBundle\Entity\User')
173
            ->disableOriginalConstructor()
174
            ->getMock();
175
176
        $object->createAclChangeSet($entity, array(), $user);
177
    }
178
179
    /**
180
     * Return entity manager mock
181
     *
182
     * @return EntityManager
183
     */
184
    public function getEntityManager()
185
    {
186
        return $this->getMockBuilder('Doctrine\ORM\EntityManager')
187
            ->disableOriginalConstructor()
188
            ->getMock();
189
    }
190
191
    /**
192
     * Return alc provider mock
193
     *
194
     * @return AclProviderInterface
195
     */
196
    public function getAclProvider()
197
    {
198
        return $this->getMock('Symfony\Component\Security\Acl\Model\AclProviderInterface');
199
    }
200
201
    /**
202
     * Return security token storage
203
     *
204
     * @return TokenStorageInterface
205
     */
206
    public function getTokenStorage()
207
    {
208
        return $this->getMock('Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface');
0 ignored issues
show
Deprecated Code introduced by
The method PHPUnit_Framework_TestCase::getMock() has been deprecated with message: Method deprecated since Release 5.4.0; use createMock() or getMockBuilder() 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...
209
    }
210
211
    /**
212
     * Return oid retrieval strategy mock
213
     *
214
     * @return ObjectIdentityRetrievalStrategyInterface
215
     */
216
    public function getOidRetrievalStrategy()
217
    {
218
        return $this->getMock('Symfony\Component\Security\Acl\Model\ObjectIdentityRetrievalStrategyInterface');
219
    }
220
221
    /**
222
     * Return event dispatcher mock
223
     *
224
     * @return EventDispatcherInterface
225
     */
226
    public function getEventDispatcher()
227
    {
228
        return $this->getMock('Symfony\Component\EventDispatcher\EventDispatcher');
229
    }
230
231
    /**
232
     * @return Shell
233
     */
234
    public function getShell()
235
    {
236
        return new Shell();
237
    }
238
239
    /**
240
     * @return KernelInterface
241
     */
242
    public function getKernel()
243
    {
244
        return $this->getMock('Symfony\Component\HttpKernel\KernelInterface');
245
    }
246
247
    /**
248
     * Return permission admin mock
249
     *
250
     * @return PermissionAdmin
251
     */
252
    public function getPermissionAdmin()
253
    {
254
        $em = $this->getEntityManager();
255
        $context = $this->getTokenStorage();
256
257
        $securityIdentity = new RoleSecurityIdentity('ROLE_TEST');
258
259
        $entity = $this->getMockBuilder('Symfony\Component\Security\Acl\Domain\Entry')
260
            ->disableOriginalConstructor()
261
            ->getMock();
262
        $entity
263
            ->expects($this->any())
264
            ->method('getSecurityIdentity')
265
            ->will($this->returnValue($securityIdentity));
266
        $entity
267
            ->expects($this->any())
268
            ->method('getMask')
269
            ->will($this->returnValue(1));
270
271
        $acl = $this->getMockBuilder('Symfony\Component\Security\Acl\Domain\Acl')
272
            ->disableOriginalConstructor()
273
            ->getMock();
274
        $acl->expects($this->once())
275
            ->method('getObjectAces')
276
            ->will($this->returnValue(array($entity)));
277
278
        $aclProvider = $this->getAclProvider();
279
        $aclProvider
280
            ->expects($this->once())
281
            ->method('findAcl')
282
            ->with($this->anything())
283
            ->will($this->returnValue($acl));
284
285
        $retrievalStrategy = $this->getOidRetrievalStrategy();
286
        $objectIdentity = $this->getMock('Symfony\Component\Security\Acl\Model\ObjectIdentityInterface');
287
        $retrievalStrategy
288
            ->expects($this->once())
289
            ->method('getObjectIdentity')
290
            ->will($this->returnValue($objectIdentity));
291
        $dispatcher = $this->getEventDispatcher();
292
        $shell = $this->getShell();
293
        $kernel = $this->getKernel();
294
        $object = new PermissionAdmin($em, $context, $aclProvider, $retrievalStrategy, $dispatcher, $shell, $kernel);
295
296
        return $object;
297
    }
298
299
    /**
300
     * Return entity mock
301
     *
302
     * @return AbstractEntity
303
     */
304
    public function getEntity()
305
    {
306
        return $this->getMockForAbstractClass('Kunstmaan\AdminBundle\Entity\AbstractEntity');
307
    }
308
309
    /**
310
     * Return permission admin mock
311
     *
312
     * @return PermissionAdmin
313
     */
314
    public function getInitializedPermissionAdmin()
315
    {
316
        $object = $this->getPermissionAdmin();
317
        $entity = $this->getEntity();
318
        /* @var $permissionMap PermissionMapInterface */
319
        $permissionMap = $this->getMock('Kunstmaan\AdminBundle\Helper\Security\Acl\Permission\PermissionMapInterface');
320
        $object->initialize($entity, $permissionMap);
321
322
        return $object;
323
    }
324
}
325