1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of the Sonata Project package. |
7
|
|
|
* |
8
|
|
|
* (c) Thomas Rabaix <[email protected]> |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
11
|
|
|
* file that was distributed with this source code. |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Sonata\AdminBundle\Tests\Security\Handler; |
15
|
|
|
|
16
|
|
|
use PHPUnit\Framework\TestCase; |
17
|
|
|
use Sonata\AdminBundle\Admin\AdminInterface; |
18
|
|
|
use Sonata\AdminBundle\Security\Handler\RoleSecurityHandler; |
19
|
|
|
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface; |
20
|
|
|
use Symfony\Component\Security\Core\Exception\AuthenticationCredentialsNotFoundException; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Test for RoleSecurityHandler. |
24
|
|
|
* |
25
|
|
|
* @author Andrej Hudec <[email protected]> |
26
|
|
|
*/ |
27
|
|
|
class RoleSecurityHandlerTest extends TestCase |
28
|
|
|
{ |
29
|
|
|
/** |
30
|
|
|
* @var AdminInterface |
31
|
|
|
*/ |
32
|
|
|
private $admin; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var AuthorizationCheckerInterface |
36
|
|
|
*/ |
37
|
|
|
private $authorizationChecker; |
38
|
|
|
|
39
|
|
|
protected function setUp(): void |
40
|
|
|
{ |
41
|
|
|
$this->authorizationChecker = $this->createMock(AuthorizationCheckerInterface::class); |
42
|
|
|
$this->admin = $this->createMock(AdminInterface::class); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @dataProvider getBaseRoleTests |
47
|
|
|
*/ |
48
|
|
|
public function testGetBaseRole(string $expected, string $code): void |
49
|
|
|
{ |
50
|
|
|
$handler = new RoleSecurityHandler($this->authorizationChecker, ['ROLE_BATMAN', 'ROLE_IRONMAN']); |
51
|
|
|
|
52
|
|
|
$this->admin->expects($this->once()) |
|
|
|
|
53
|
|
|
->method('getCode') |
54
|
|
|
->willReturn($code); |
55
|
|
|
|
56
|
|
|
$this->assertSame($expected, $handler->getBaseRole($this->admin)); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function getBaseRoleTests(): array |
60
|
|
|
{ |
61
|
|
|
return [ |
62
|
|
|
['ROLE_FOO_BAR_%s', 'foo.bar'], |
63
|
|
|
['ROLE_FOO_BAR_%s', 'Foo.Bar'], |
64
|
|
|
['ROLE_FOO_BAR_BAZ_%s', 'foo.bar_baz'], |
65
|
|
|
['ROLE_FOO_BAR_%s', 'FOO.BAR'], |
66
|
|
|
]; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @dataProvider getIsGrantedTests |
71
|
|
|
*/ |
72
|
|
|
public function testIsGranted(bool $expected, array $superAdminRoles, string $adminCode, $operation, $object = null): void |
73
|
|
|
{ |
74
|
|
|
$handler = $this->getRoleSecurityHandler($superAdminRoles); |
75
|
|
|
|
76
|
|
|
$this->admin |
|
|
|
|
77
|
|
|
->method('getCode') |
78
|
|
|
->willReturn($adminCode); |
79
|
|
|
|
80
|
|
|
$this->authorizationChecker |
|
|
|
|
81
|
|
|
->method('isGranted') |
82
|
|
|
->willReturnCallback(static function (string $attribute, $object) { |
83
|
|
|
switch ($attribute) { |
84
|
|
|
case 'ROLE_BATMAN': |
85
|
|
|
case 'ROLE_IRONMAN': |
86
|
|
|
case 'ROLE_FOO_BAR_ABC': |
87
|
|
|
case 'ROLE_FOO_BAR_BAZ_ALL': |
88
|
|
|
return true; |
89
|
|
|
case 'ROLE_AUTH_EXCEPTION': |
90
|
|
|
throw new AuthenticationCredentialsNotFoundException(); |
91
|
|
|
case 'ROLE_FOO_BAR_DEF': |
92
|
|
|
return $object instanceof \stdClass; |
93
|
|
|
default: |
94
|
|
|
return false; |
95
|
|
|
} |
96
|
|
|
}); |
97
|
|
|
|
98
|
|
|
$this->assertSame($expected, $handler->isGranted($this->admin, $operation, $object)); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
public function getIsGrantedTests(): array |
102
|
|
|
{ |
103
|
|
|
return [ |
104
|
|
|
//empty |
105
|
|
|
[false, [''], 'foo.bar', ''], |
106
|
|
|
[false, [''], 'foo.bar', ['']], |
107
|
|
|
[false, [''], 'foo.bar.abc', ['']], |
108
|
|
|
[false, [''], 'foo.bar.def', ['']], |
109
|
|
|
[false, [''], 'foo.bar.baz.xyz', ''], |
110
|
|
|
[false, [''], 'foo.bar.baz.xyz', ['']], |
111
|
|
|
|
112
|
|
|
//superadmins |
113
|
|
|
[true, ['ROLE_BATMAN', 'ROLE_IRONMAN'], 'foo.bar', 'BAZ'], |
114
|
|
|
[true, ['ROLE_BATMAN', 'ROLE_IRONMAN'], 'foo.bar', 'ANYTHING'], |
115
|
|
|
[true, ['ROLE_BATMAN', 'ROLE_IRONMAN'], 'foo.bar', ['BAZ', 'ANYTHING']], |
116
|
|
|
[true, ['ROLE_IRONMAN'], 'foo.bar', 'BAZ'], |
117
|
|
|
[true, ['ROLE_IRONMAN'], 'foo.bar', 'ANYTHING'], |
118
|
|
|
[true, ['ROLE_IRONMAN'], 'foo.bar.baz.xyz', 'ANYTHING'], |
119
|
|
|
[true, ['ROLE_IRONMAN'], 'foo.bar', ''], |
120
|
|
|
[true, ['ROLE_IRONMAN'], 'foo.bar', ['']], |
121
|
|
|
|
122
|
|
|
//operations |
123
|
|
|
[true, ['ROLE_SPIDERMAN'], 'foo.bar', 'ABC'], |
124
|
|
|
[true, ['ROLE_SPIDERMAN'], 'foo.bar', ['ABC']], |
125
|
|
|
[true, ['ROLE_SPIDERMAN'], 'foo.bar', ['ABC', 'DEF']], |
126
|
|
|
[true, ['ROLE_SPIDERMAN'], 'foo.bar', ['BAZ', 'ABC']], |
127
|
|
|
[false, ['ROLE_SPIDERMAN'], 'foo.bar', 'DEF'], |
128
|
|
|
[false, ['ROLE_SPIDERMAN'], 'foo.bar', ['DEF']], |
129
|
|
|
[false, ['ROLE_SPIDERMAN'], 'foo.bar', 'BAZ'], |
130
|
|
|
[false, ['ROLE_SPIDERMAN'], 'foo.bar', ['BAZ']], |
131
|
|
|
[true, [], 'foo.bar', 'ABC'], |
132
|
|
|
[true, [], 'foo.bar', ['ABC']], |
133
|
|
|
[false, [], 'foo.bar', 'DEF'], |
134
|
|
|
[false, [], 'foo.bar', ['DEF']], |
135
|
|
|
[false, [], 'foo.bar', 'BAZ'], |
136
|
|
|
[false, [], 'foo.bar', ['BAZ']], |
137
|
|
|
[false, [], 'foo.bar.baz.xyz', 'ABC'], |
138
|
|
|
[false, [], 'foo.bar.baz.xyz', ['ABC']], |
139
|
|
|
[false, [], 'foo.bar.baz.xyz', ['ABC', 'DEF']], |
140
|
|
|
[false, [], 'foo.bar.baz.xyz', 'DEF'], |
141
|
|
|
[false, [], 'foo.bar.baz.xyz', ['DEF']], |
142
|
|
|
[false, [], 'foo.bar.baz.xyz', 'BAZ'], |
143
|
|
|
[false, [], 'foo.bar.baz.xyz', ['BAZ']], |
144
|
|
|
|
145
|
|
|
//objects |
146
|
|
|
[true, ['ROLE_SPIDERMAN'], 'foo.bar', ['DEF'], new \stdClass()], |
147
|
|
|
[true, ['ROLE_SPIDERMAN'], 'foo.bar', ['ABC'], new \stdClass()], |
148
|
|
|
[true, ['ROLE_SPIDERMAN'], 'foo.bar', ['ABC', 'DEF'], new \stdClass()], |
149
|
|
|
[true, ['ROLE_SPIDERMAN'], 'foo.bar', ['BAZ', 'DEF'], new \stdClass()], |
150
|
|
|
[true, ['ROLE_SPIDERMAN'], 'foo.bar', 'DEF', new \stdClass()], |
151
|
|
|
[true, ['ROLE_SPIDERMAN'], 'foo.bar', 'ABC', new \stdClass()], |
152
|
|
|
[false, ['ROLE_SPIDERMAN'], 'foo.bar', 'BAZ', new \stdClass()], |
153
|
|
|
[false, ['ROLE_SPIDERMAN'], 'foo.bar.baz.xyz', 'DEF', new \stdClass()], |
154
|
|
|
[false, ['ROLE_SPIDERMAN'], 'foo.bar.baz.xyz', 'ABC', new \stdClass()], |
155
|
|
|
[true, [], 'foo.bar', ['ABC'], new \stdClass()], |
156
|
|
|
[true, [], 'foo.bar', 'ABC', new \stdClass()], |
157
|
|
|
[true, [], 'foo.bar', ['DEF'], new \stdClass()], |
158
|
|
|
[true, [], 'foo.bar', 'DEF', new \stdClass()], |
159
|
|
|
[false, [], 'foo.bar', ['BAZ'], new \stdClass()], |
160
|
|
|
[false, [], 'foo.bar', 'BAZ', new \stdClass()], |
161
|
|
|
[false, [], 'foo.bar.baz.xyz', 'BAZ', new \stdClass()], |
162
|
|
|
[false, [], 'foo.bar.baz.xyz', ['BAZ'], new \stdClass()], |
163
|
|
|
[false, ['ROLE_AUTH_EXCEPTION'], 'foo.bar.baz.xyz', ['BAZ'], new \stdClass()], |
164
|
|
|
|
165
|
|
|
// ALL role |
166
|
|
|
[true, [], 'foo.bar.baz', 'LIST'], |
167
|
|
|
[true, [], 'foo.bar.baz', ['LIST', 'EDIT']], |
168
|
|
|
]; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
public function testIsGrantedWithException(): void |
172
|
|
|
{ |
173
|
|
|
$this->expectException(\RuntimeException::class); |
174
|
|
|
$this->expectExceptionMessage('Something is wrong'); |
175
|
|
|
|
176
|
|
|
$this->admin |
|
|
|
|
177
|
|
|
->method('getCode') |
178
|
|
|
->willReturn('foo.bar'); |
179
|
|
|
|
180
|
|
|
$this->authorizationChecker |
|
|
|
|
181
|
|
|
->method('isGranted') |
182
|
|
|
->willReturnCallback(static function (): void { |
183
|
|
|
throw new \RuntimeException('Something is wrong'); |
184
|
|
|
}); |
185
|
|
|
|
186
|
|
|
$handler = $this->getRoleSecurityHandler(['ROLE_BATMAN']); |
187
|
|
|
$handler->isGranted($this->admin, 'BAZ'); |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* @doesNotPerformAssertions |
192
|
|
|
*/ |
193
|
|
|
public function testCreateObjectSecurity(): void |
194
|
|
|
{ |
195
|
|
|
$handler = $this->getRoleSecurityHandler(['ROLE_FOO']); |
196
|
|
|
$handler->createObjectSecurity($this->getSonataAdminObject(), new \stdClass()); |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* @doesNotPerformAssertions |
201
|
|
|
*/ |
202
|
|
|
public function testDeleteObjectSecurity(): void |
203
|
|
|
{ |
204
|
|
|
$handler = $this->getRoleSecurityHandler(['ROLE_FOO']); |
205
|
|
|
$handler->deleteObjectSecurity($this->getSonataAdminObject(), new \stdClass()); |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
public function testBuildSecurityInformation(): void |
209
|
|
|
{ |
210
|
|
|
$handler = $this->getRoleSecurityHandler(['ROLE_FOO']); |
211
|
|
|
$this->assertSame([], $handler->buildSecurityInformation($this->getSonataAdminObject())); |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
private function getRoleSecurityHandler(array $superAdminRoles): RoleSecurityHandler |
215
|
|
|
{ |
216
|
|
|
return new RoleSecurityHandler($this->authorizationChecker, $superAdminRoles); |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
private function getSonataAdminObject(): AdminInterface |
220
|
|
|
{ |
221
|
|
|
return $this->getMockForAbstractClass(AdminInterface::class); |
222
|
|
|
} |
223
|
|
|
} |
224
|
|
|
|
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.