Completed
Push — 3.x ( 3e834f...38b337 )
by Grégoire
03:36
created

testUpdateAclMustOnlyAcceptMutableAclInterface()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 9.7998
c 0
b 0
f 0
cc 1
nc 1
nop 0
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\AclInterface;
21
use Symfony\Component\Security\Acl\Model\MutableAclInterface;
22
use Symfony\Component\Security\Acl\Model\MutableAclProviderInterface;
23
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
24
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
25
use Symfony\Component\Security\Core\Exception\AuthenticationCredentialsNotFoundException;
26
27
class AclSecurityHandlerTest extends TestCase
28
{
29
    public function getTokenStorageMock()
30
    {
31
        return $this->getMockForAbstractClass(TokenStorageInterface::class);
32
    }
33
34
    public function getAuthorizationCheckerMock()
35
    {
36
        return $this->getMockForAbstractClass(AuthorizationCheckerInterface::class);
37
    }
38
39
    public function testAcl(): void
40
    {
41
        $admin = $this->getMockForAbstractClass(AdminInterface::class);
42
        $admin
43
            ->method('getCode')
44
            ->willReturn('test');
45
46
        $authorizationChecker = $this->getAuthorizationCheckerMock();
47
        $authorizationChecker
48
            ->method('isGranted')
49
            ->willReturn(true);
50
51
        $aclProvider = $this->getMockForAbstractClass(MutableAclProviderInterface::class);
52
53
        $handler = new AclSecurityHandler($this->getTokenStorageMock(), $authorizationChecker, $aclProvider, MaskBuilder::class, []);
54
55
        $this->assertTrue($handler->isGranted($admin, ['TOTO']));
56
        $this->assertTrue($handler->isGranted($admin, 'TOTO'));
57
58
        $authorizationChecker = $this->getAuthorizationCheckerMock();
59
        $authorizationChecker
60
            ->method('isGranted')
61
            ->willReturn(false);
62
63
        $handler = new AclSecurityHandler($this->getTokenStorageMock(), $authorizationChecker, $aclProvider, MaskBuilder::class, []);
64
65
        $this->assertFalse($handler->isGranted($admin, ['TOTO']));
66
        $this->assertFalse($handler->isGranted($admin, 'TOTO'));
67
    }
68
69
    public function testBuildInformation(): void
70
    {
71
        $informations = [
72
            'EDIT' => ['EDIT'],
73
        ];
74
75
        $authorizationChecker = $this->getAuthorizationCheckerMock();
76
        $admin = $this->getMockForAbstractClass(AdminInterface::class);
77
        $admin->expects($this->once())
78
            ->method('getCode')
79
            ->willReturn('test');
80
81
        $admin->expects($this->once())
82
            ->method('getSecurityInformation')
83
            ->willReturn($informations);
84
85
        $aclProvider = $this->getMockForAbstractClass(MutableAclProviderInterface::class);
86
87
        $handler = new AclSecurityHandler($this->getTokenStorageMock(), $authorizationChecker, $aclProvider, MaskBuilder::class, []);
88
89
        $results = $handler->buildSecurityInformation($admin);
90
91
        $this->assertArrayHasKey('ROLE_TEST_EDIT', $results);
92
    }
93
94
    public function testWithAuthenticationCredentialsNotFoundException(): void
95
    {
96
        $admin = $this->getMockForAbstractClass(AdminInterface::class);
97
98
        $authorizationChecker = $this->getAuthorizationCheckerMock();
99
        $authorizationChecker
100
            ->method('isGranted')
101
            ->will($this->throwException(new AuthenticationCredentialsNotFoundException('FAIL')));
102
103
        $aclProvider = $this->getMockForAbstractClass(MutableAclProviderInterface::class);
104
105
        $handler = new AclSecurityHandler($this->getTokenStorageMock(), $authorizationChecker, $aclProvider, MaskBuilder::class, []);
106
107
        $this->assertFalse($handler->isGranted($admin, 'raise exception', $admin));
108
    }
109
110
    public function testWithNonAuthenticationCredentialsNotFoundException(): void
111
    {
112
        $this->expectException(\RuntimeException::class);
113
114
        $admin = $this->getMockForAbstractClass(AdminInterface::class);
115
116
        $authorizationChecker = $this->getAuthorizationCheckerMock();
117
        $authorizationChecker
118
            ->method('isGranted')
119
            ->will($this->throwException(new \RuntimeException('FAIL')));
120
121
        $aclProvider = $this->getMockForAbstractClass(MutableAclProviderInterface::class);
122
123
        $handler = new AclSecurityHandler($this->getTokenStorageMock(), $authorizationChecker, $aclProvider, MaskBuilder::class, []);
124
125
        $this->assertFalse($handler->isGranted($admin, 'raise exception', $admin));
126
    }
127
128
    public function testAddObjectOwnerParamMustBeMutableAclInterface()
129
    {
130
        $this->expectException(\TypeError::class);
131
        $this->expectExceptionMessage(sprintf(
132
            'Argument 1 passed to "%s::addObjectOwner()" must implement "%s".',
133
            AclSecurityHandler::class,
134
            MutableAclInterface::class
135
        ));
136
        $handler = new AclSecurityHandler(
137
            $this->getTokenStorageMock(),
138
            $this->getAuthorizationCheckerMock(),
139
            $this->getMockForAbstractClass(MutableAclProviderInterface::class),
140
            MaskBuilder::class,
141
            []
142
        );
143
        $handler->addObjectOwner($this->createStub(AclInterface::class));
144
    }
145
146
    public function testUpdateAclMustOnlyAcceptMutableAclInterface()
147
    {
148
        $this->expectWarning();
149
        $this->expectWarningMessage('assert(): assert($acl instanceof MutableAclInterface) failed');
150
        $handler = new AclSecurityHandler(
151
            $this->getTokenStorageMock(),
152
            $this->getAuthorizationCheckerMock(),
153
            $this->getMockForAbstractClass(MutableAclProviderInterface::class),
154
            MaskBuilder::class,
155
            []
156
        );
157
        $acl = $this->createStub(AclInterface::class);
158
        $handler->updateAcl($acl);
159
    }
160
161
    public function testSuccerfulUpdateAcl()
162
    {
163
        $acl = $this->createStub(MutableAclInterface::class);
164
        $aclProvider = $this->getMockForAbstractClass(MutableAclProviderInterface::class);
165
166
        $aclProvider
167
            ->expects($this->once())
168
            ->method('updateAcl')
169
            ->with($acl)
170
        ;
171
172
        $handler = new AclSecurityHandler(
173
            $this->getTokenStorageMock(),
174
            $this->getAuthorizationCheckerMock(),
175
            $aclProvider,
176
            MaskBuilder::class,
177
            []
178
        );
179
        $handler->updateAcl($acl);
180
    }
181
}
182