Completed
Pull Request — 3.x (#6161)
by
unknown
02:56
created

testAddObjectOwnerParamMustBeMutableAclInterface()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
rs 9.8333
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(\InvalidArgumentException::class);
131
        $this->expectExceptionMessage('$acl must implement Symfony\Component\Security\Acl\Model\MutableAclInterface');
132
        $handler = new AclSecurityHandler(
133
            $this->getTokenStorageMock(),
134
            $this->getAuthorizationCheckerMock(),
135
            $this->getMockForAbstractClass(MutableAclProviderInterface::class),
136
            MaskBuilder::class,
137
            []
138
        );
139
        $handler->addObjectOwner($this->createMock(AclInterface::class));
140
    }
141
142
    public function testUpdateAclMustOnlyAcceptMutableAclInterface()
143
    {
144
        $this->expectWarning();
145
        $this->expectWarningMessage('assert(): assert($acl instanceof MutableAclInterface) failed');
146
        $handler = new AclSecurityHandler(
147
            $this->getTokenStorageMock(),
148
            $this->getAuthorizationCheckerMock(),
149
            $this->getMockForAbstractClass(MutableAclProviderInterface::class),
150
            MaskBuilder::class,
151
            []
152
        );
153
        $acl = $this->createMock(AclInterface::class);
154
        $handler->updateAcl($acl);
155
    }
156
157
    public function testSuccerfulUpdateAcl()
158
    {
159
        $acl = $this->createMock(MutableAclInterface::class);
160
        $aclProvider = $this->getMockForAbstractClass(MutableAclProviderInterface::class);
161
162
        $aclProvider
163
            ->expects($this->once())
164
            ->method('updateAcl')
165
            ->with($acl)
166
        ;
167
168
        $handler = new AclSecurityHandler(
169
            $this->getTokenStorageMock(),
170
            $this->getAuthorizationCheckerMock(),
171
            $aclProvider,
172
            MaskBuilder::class,
173
            []
174
        );
175
        $handler->updateAcl($acl);
176
    }
177
}
178