|
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\Acl\Permission\MaskBuilder; |
|
19
|
|
|
use Sonata\AdminBundle\Security\Handler\AclSecurityHandler; |
|
20
|
|
|
use Symfony\Component\Security\Acl\Model\MutableAclInterface; |
|
21
|
|
|
use Symfony\Component\Security\Acl\Model\MutableAclProviderInterface; |
|
22
|
|
|
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface; |
|
23
|
|
|
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface; |
|
24
|
|
|
use Symfony\Component\Security\Core\Exception\AuthenticationCredentialsNotFoundException; |
|
25
|
|
|
|
|
26
|
|
|
class AclSecurityHandlerTest extends TestCase |
|
27
|
|
|
{ |
|
28
|
|
|
public function getTokenStorageMock() |
|
29
|
|
|
{ |
|
30
|
|
|
return $this->getMockForAbstractClass(TokenStorageInterface::class); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
public function getAuthorizationCheckerMock() |
|
34
|
|
|
{ |
|
35
|
|
|
return $this->getMockForAbstractClass(AuthorizationCheckerInterface::class); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
public function testAcl(): void |
|
39
|
|
|
{ |
|
40
|
|
|
$admin = $this->getMockForAbstractClass(AdminInterface::class); |
|
41
|
|
|
$admin |
|
42
|
|
|
->method('getCode') |
|
43
|
|
|
->willReturn('test'); |
|
44
|
|
|
|
|
45
|
|
|
$authorizationChecker = $this->getAuthorizationCheckerMock(); |
|
46
|
|
|
$authorizationChecker |
|
47
|
|
|
->method('isGranted') |
|
48
|
|
|
->willReturn(true); |
|
49
|
|
|
|
|
50
|
|
|
$aclProvider = $this->getMockForAbstractClass(MutableAclProviderInterface::class); |
|
51
|
|
|
|
|
52
|
|
|
$handler = new AclSecurityHandler($this->getTokenStorageMock(), $authorizationChecker, $aclProvider, MaskBuilder::class, []); |
|
53
|
|
|
|
|
54
|
|
|
$this->assertTrue($handler->isGranted($admin, ['TOTO'])); |
|
55
|
|
|
$this->assertTrue($handler->isGranted($admin, 'TOTO')); |
|
56
|
|
|
|
|
57
|
|
|
$authorizationChecker = $this->getAuthorizationCheckerMock(); |
|
58
|
|
|
$authorizationChecker |
|
59
|
|
|
->method('isGranted') |
|
60
|
|
|
->willReturn(false); |
|
61
|
|
|
|
|
62
|
|
|
$handler = new AclSecurityHandler($this->getTokenStorageMock(), $authorizationChecker, $aclProvider, MaskBuilder::class, []); |
|
63
|
|
|
|
|
64
|
|
|
$this->assertFalse($handler->isGranted($admin, ['TOTO'])); |
|
65
|
|
|
$this->assertFalse($handler->isGranted($admin, 'TOTO')); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
public function testBuildInformation(): void |
|
69
|
|
|
{ |
|
70
|
|
|
$informations = [ |
|
71
|
|
|
'EDIT' => ['EDIT'], |
|
72
|
|
|
]; |
|
73
|
|
|
|
|
74
|
|
|
$authorizationChecker = $this->getAuthorizationCheckerMock(); |
|
75
|
|
|
$admin = $this->getMockForAbstractClass(AdminInterface::class); |
|
76
|
|
|
$admin->expects($this->once()) |
|
77
|
|
|
->method('getCode') |
|
78
|
|
|
->willReturn('test'); |
|
79
|
|
|
|
|
80
|
|
|
$admin->expects($this->once()) |
|
81
|
|
|
->method('getSecurityInformation') |
|
82
|
|
|
->willReturn($informations); |
|
83
|
|
|
|
|
84
|
|
|
$aclProvider = $this->getMockForAbstractClass(MutableAclProviderInterface::class); |
|
85
|
|
|
|
|
86
|
|
|
$handler = new AclSecurityHandler($this->getTokenStorageMock(), $authorizationChecker, $aclProvider, MaskBuilder::class, []); |
|
87
|
|
|
|
|
88
|
|
|
$results = $handler->buildSecurityInformation($admin); |
|
89
|
|
|
|
|
90
|
|
|
$this->assertArrayHasKey('ROLE_TEST_EDIT', $results); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
public function testWithAuthenticationCredentialsNotFoundException(): void |
|
94
|
|
|
{ |
|
95
|
|
|
$admin = $this->getMockForAbstractClass(AdminInterface::class); |
|
96
|
|
|
|
|
97
|
|
|
$authorizationChecker = $this->getAuthorizationCheckerMock(); |
|
98
|
|
|
$authorizationChecker |
|
99
|
|
|
->method('isGranted') |
|
100
|
|
|
->will($this->throwException(new AuthenticationCredentialsNotFoundException('FAIL'))); |
|
101
|
|
|
|
|
102
|
|
|
$aclProvider = $this->getMockForAbstractClass(MutableAclProviderInterface::class); |
|
103
|
|
|
|
|
104
|
|
|
$handler = new AclSecurityHandler($this->getTokenStorageMock(), $authorizationChecker, $aclProvider, MaskBuilder::class, []); |
|
105
|
|
|
|
|
106
|
|
|
$this->assertFalse($handler->isGranted($admin, 'raise exception', $admin)); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
public function testWithNonAuthenticationCredentialsNotFoundException(): void |
|
110
|
|
|
{ |
|
111
|
|
|
$this->expectException(\RuntimeException::class); |
|
112
|
|
|
|
|
113
|
|
|
$admin = $this->getMockForAbstractClass(AdminInterface::class); |
|
114
|
|
|
|
|
115
|
|
|
$authorizationChecker = $this->getAuthorizationCheckerMock(); |
|
116
|
|
|
$authorizationChecker |
|
117
|
|
|
->method('isGranted') |
|
118
|
|
|
->will($this->throwException(new \RuntimeException('FAIL'))); |
|
119
|
|
|
|
|
120
|
|
|
$aclProvider = $this->getMockForAbstractClass(MutableAclProviderInterface::class); |
|
121
|
|
|
|
|
122
|
|
|
$handler = new AclSecurityHandler($this->getTokenStorageMock(), $authorizationChecker, $aclProvider, MaskBuilder::class, []); |
|
123
|
|
|
|
|
124
|
|
|
$this->assertFalse($handler->isGranted($admin, 'raise exception', $admin)); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
public function testSuccessfulUpdateAcl(): void |
|
128
|
|
|
{ |
|
129
|
|
|
$acl = $this->createStub(MutableAclInterface::class); |
|
130
|
|
|
$aclProvider = $this->getMockForAbstractClass(MutableAclProviderInterface::class); |
|
131
|
|
|
|
|
132
|
|
|
$aclProvider |
|
133
|
|
|
->expects($this->once()) |
|
134
|
|
|
->method('updateAcl') |
|
135
|
|
|
->with($acl) |
|
136
|
|
|
; |
|
137
|
|
|
|
|
138
|
|
|
$handler = new AclSecurityHandler( |
|
139
|
|
|
$this->getTokenStorageMock(), |
|
140
|
|
|
$this->getAuthorizationCheckerMock(), |
|
141
|
|
|
$aclProvider, |
|
142
|
|
|
MaskBuilder::class, |
|
143
|
|
|
[] |
|
144
|
|
|
); |
|
145
|
|
|
$handler->updateAcl($acl); |
|
146
|
|
|
} |
|
147
|
|
|
} |
|
148
|
|
|
|