Completed
Push — master ( 0df9d8...6593b9 )
by Jeroen
12:03 queued 05:32
created

unit/Helper/Security/Acl/AclNativeHelperTest.php (2 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 Doctrine\DBAL\Connection;
6
use Doctrine\DBAL\Platforms\AbstractPlatform;
7
use Doctrine\DBAL\Query\QueryBuilder;
8
use Doctrine\ORM\EntityManager;
9
use Doctrine\ORM\Mapping\ClassMetadata;
10
use FOS\UserBundle\Model\UserInterface;
11
use Kunstmaan\AdminBundle\Helper\Security\Acl\AclNativeHelper;
12
use Kunstmaan\AdminBundle\Helper\Security\Acl\Permission\PermissionDefinition;
13
use PHPUnit\Framework\TestCase;
14
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
15
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
16
use Symfony\Component\Security\Core\Role\RoleHierarchyInterface;
17
18
class AclNativeHelperTest extends TestCase
19
{
20
    /**
21
     * @var EntityManager
22
     */
23
    protected $em;
24
25
    /**
26
     * @var TokenStorageInterface
27
     */
28
    protected $tokenStorage;
29
30
    /**
31
     * @var RoleHierarchyInterface
32
     */
33
    protected $rh;
34
35
    /**
36
     * @var TokenInterface
37
     */
38
    protected $token;
39
40
    /**
41
     * @var UserInterface
42
     */
43
    protected $user;
44
45
    /**
46
     * @var Connection
47
     */
48
    protected $conn;
49
50
    /**
51
     * @var AclNativeHelper
52
     */
53
    protected $object;
54
55
    /**
56
     * Sets up the fixture, for example, opens a network connection.
57
     * This method is called before a test is executed.
58
     */
59
    protected function setUp()
60
    {
61
        $this->em = $this->getMockBuilder('Doctrine\ORM\EntityManager')
62
            ->disableOriginalConstructor()
63
            ->getMock();
64
65
        $this->conn = $this->getMockBuilder('Doctrine\DBAL\Connection')
66
            ->disableOriginalConstructor()
67
            ->getMock();
68
69
        $this->conn->expects($this->any())
70
            ->method('getDatabase')
71
            ->will($this->returnValue('myDatabase'));
72
73
        /* @var $platform AbstractPlatform */
74
        $platform = $this->getMockForAbstractClass('Doctrine\DBAL\Platforms\AbstractPlatform');
75
76
        $this->conn->expects($this->any())
77
            ->method('getDatabasePlatform')
78
            ->will($this->returnValue($platform));
79
80
        $this->em->expects($this->any())
81
            ->method('getConnection')
82
            ->will($this->returnValue($this->conn));
83
84
        /* @var $meta ClassMetadata */
85
        $meta = $this->getMockBuilder('Doctrine\ORM\Mapping\ClassMetadata')
86
            ->disableOriginalConstructor()
87
            ->getMock();
88
89
        $this->em->expects($this->any())
90
            ->method('getClassMetadata')
91
            ->will($this->returnValue($meta));
92
93
        $this->tokenStorage = $this->getMockBuilder('Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface')
94
            ->getMock();
95
96
        $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...tMockForAbstractClass() 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...
97
            ->setMethods(['getRoleNames'])
98
            ->getMockForAbstractClass();
99
100
        $this->tokenStorage->expects($this->any())
101
            ->method('getToken')
102
            ->will($this->returnValue($this->token));
103
104
        $this->rh = $this->getMockBuilder('Symfony\Component\Security\Core\Role\RoleHierarchyInterface')
0 ignored issues
show
Documentation Bug introduced by
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...
105
            ->setMethods(['getReachableRoleNames'])
106
            ->getMockForAbstractClass();
107
108
        $this->object = new AclNativeHelper($this->em, $this->tokenStorage, $this->rh);
109
    }
110
111
    public function testApply()
112
    {
113
        $queryBuilder = new QueryBuilder($this->conn);
114
        $queryBuilder->add(
115
            'from',
116
            array(
117
                array(
118
                    'table' => 'myTable',
119
                    'alias' => 'n',
120
                ),
121
            )
122
        );
123
124
        $roles = array('ROLE_KING');
125
        $allRoles = array($roles[0], 'ROLE_SUBJECT');
126
127
        $this->token->expects($this->once())
128
            ->method('getRoleNames')
129
            ->will($this->returnValue($roles));
130
131
        $this->rh->expects($this->once())
132
            ->method('getReachableRoleNames')
133
            ->with($roles)
134
            ->will($this->returnValue($allRoles));
135
136
        $user = $this->getMockBuilder('FOS\UserBundle\Model\UserInterface')
137
            ->getMock();
138
139
        $user->expects($this->any())
140
            ->method('getUsername')
141
            ->will($this->returnValue('MyUser'));
142
143
        $this->token->expects($this->any())
144
            ->method('getUser')
145
            ->will($this->returnValue($user));
146
147
        $permissionDef = new PermissionDefinition(array('view'), 'Kunstmaan\NodeBundle\Entity\Node', 'n');
148
149
        /* @var $qb QueryBuilder */
150
        $qb = $this->object->apply($queryBuilder, $permissionDef);
151
        $query = $qb->getSQL();
152
153
        $this->assertContains('"ROLE_SUBJECT"', $query);
154
        $this->assertContains('"ROLE_KING"', $query);
155
        $this->assertContains('"IS_AUTHENTICATED_ANONYMOUSLY"', $query);
156
        $this->assertContains('MyUser', $query);
157
    }
158
159
    public function testApplyAnonymous()
160
    {
161
        $queryBuilder = new QueryBuilder($this->conn);
162
        $queryBuilder->add(
163
            'from',
164
            array(
165
                array(
166
                    'table' => 'myTable',
167
                    'alias' => 'n',
168
                ),
169
            )
170
        );
171
172
        $roles = array();
173
174
        $this->token->expects($this->once())
175
            ->method('getRoleNames')
176
            ->will($this->returnValue($roles));
177
178
        $this->rh->expects($this->once())
179
            ->method('getReachableRoleNames')
180
            ->with($roles)
181
            ->will($this->returnValue($roles));
182
183
        $this->token->expects($this->any())
184
            ->method('getUser')
185
            ->will($this->returnValue('anon.'));
186
187
        $permissionDef = new PermissionDefinition(array('view'), 'Kunstmaan\NodeBundle\Entity\Node', 'n');
188
189
        /* @var $qb QueryBuilder */
190
        $qb = $this->object->apply($queryBuilder, $permissionDef);
191
        $query = $qb->getSQL();
192
193
        $this->assertContains('"IS_AUTHENTICATED_ANONYMOUSLY"', $query);
194
    }
195
196
    public function testGetTokenStorage()
197
    {
198
        $this->assertSame($this->tokenStorage, $this->object->getTokenStorage());
199
    }
200
}
201