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 Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface; |
14
|
|
|
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; |
15
|
|
|
use Symfony\Component\Security\Core\Role\Role; |
16
|
|
|
use Symfony\Component\Security\Core\Role\RoleHierarchyInterface; |
17
|
|
|
|
18
|
|
|
class AclNativeHelperTest extends \PHPUnit_Framework_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') |
97
|
|
|
->getMock(); |
98
|
|
|
|
99
|
|
|
$this->tokenStorage->expects($this->any()) |
100
|
|
|
->method('getToken') |
101
|
|
|
->will($this->returnValue($this->token)); |
102
|
|
|
|
103
|
|
|
$this->rh = $this->getMockBuilder('Symfony\Component\Security\Core\Role\RoleHierarchyInterface') |
104
|
|
|
->getMock(); |
105
|
|
|
|
106
|
|
|
$this->object = new AclNativeHelper($this->em, $this->tokenStorage, $this->rh); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
public function testConstructor() |
110
|
|
|
{ |
111
|
|
|
new AclNativeHelper($this->em, $this->tokenStorage, $this->rh); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
public function testApply() |
115
|
|
|
{ |
116
|
|
|
$queryBuilder = new QueryBuilder($this->conn); |
117
|
|
|
$queryBuilder->add( |
118
|
|
|
'from', |
119
|
|
|
array( |
|
|
|
|
120
|
|
|
array( |
121
|
|
|
'table' => 'myTable', |
122
|
|
|
'alias' => 'n' |
123
|
|
|
) |
124
|
|
|
) |
125
|
|
|
); |
126
|
|
|
|
127
|
|
|
$roles = array(new Role('ROLE_KING')); |
128
|
|
|
$allRoles = array($roles[0], new Role('ROLE_SUBJECT')); |
129
|
|
|
|
130
|
|
|
$this->token->expects($this->once()) |
|
|
|
|
131
|
|
|
->method('getRoles') |
132
|
|
|
->will($this->returnValue($roles)); |
133
|
|
|
|
134
|
|
|
$this->rh->expects($this->once()) |
|
|
|
|
135
|
|
|
->method('getReachableRoles') |
136
|
|
|
->with($roles) |
137
|
|
|
->will($this->returnValue($allRoles)); |
138
|
|
|
|
139
|
|
|
$user = $this->getMockBuilder('FOS\UserBundle\Model\UserInterface') |
140
|
|
|
->getMock(); |
141
|
|
|
|
142
|
|
|
$user->expects($this->any()) |
143
|
|
|
->method('getUsername') |
144
|
|
|
->will($this->returnValue('MyUser')); |
145
|
|
|
|
146
|
|
|
$this->token->expects($this->any()) |
|
|
|
|
147
|
|
|
->method('getUser') |
148
|
|
|
->will($this->returnValue($user)); |
149
|
|
|
|
150
|
|
|
$permissionDef = new PermissionDefinition(array('view'), 'Kunstmaan\NodeBundle\Entity\Node', 'n'); |
151
|
|
|
|
152
|
|
|
/* @var $qb QueryBuilder */ |
153
|
|
|
$qb = $this->object->apply($queryBuilder, $permissionDef); |
154
|
|
|
$query = $qb->getSQL(); |
155
|
|
|
|
156
|
|
|
$this->assertContains('"ROLE_SUBJECT"', $query); |
157
|
|
|
$this->assertContains('"ROLE_KING"', $query); |
158
|
|
|
$this->assertContains('"IS_AUTHENTICATED_ANONYMOUSLY"', $query); |
159
|
|
|
$this->assertContains('MyUser', $query); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
public function testApplyAnonymous() |
163
|
|
|
{ |
164
|
|
|
$queryBuilder = new QueryBuilder($this->conn); |
165
|
|
|
$queryBuilder->add( |
166
|
|
|
'from', |
167
|
|
|
array( |
|
|
|
|
168
|
|
|
array( |
169
|
|
|
'table' => 'myTable', |
170
|
|
|
'alias' => 'n' |
171
|
|
|
) |
172
|
|
|
) |
173
|
|
|
); |
174
|
|
|
|
175
|
|
|
$roles = array(); |
176
|
|
|
|
177
|
|
|
$this->token->expects($this->once()) |
|
|
|
|
178
|
|
|
->method('getRoles') |
179
|
|
|
->will($this->returnValue($roles)); |
180
|
|
|
|
181
|
|
|
$this->rh->expects($this->once()) |
|
|
|
|
182
|
|
|
->method('getReachableRoles') |
183
|
|
|
->with($roles) |
184
|
|
|
->will($this->returnValue($roles)); |
185
|
|
|
|
186
|
|
|
$this->token->expects($this->any()) |
|
|
|
|
187
|
|
|
->method('getUser') |
188
|
|
|
->will($this->returnValue('anon.')); |
189
|
|
|
|
190
|
|
|
$permissionDef = new PermissionDefinition(array('view'), 'Kunstmaan\NodeBundle\Entity\Node', 'n'); |
191
|
|
|
|
192
|
|
|
/* @var $qb QueryBuilder */ |
193
|
|
|
$qb = $this->object->apply($queryBuilder, $permissionDef); |
194
|
|
|
$query = $qb->getSQL(); |
195
|
|
|
|
196
|
|
|
$this->assertContains('"IS_AUTHENTICATED_ANONYMOUSLY"', $query); |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
public function testGetTokenStorage() |
200
|
|
|
{ |
201
|
|
|
$this->assertSame($this->tokenStorage, $this->object->getTokenStorage()); |
202
|
|
|
} |
203
|
|
|
} |
204
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: