Completed
Push — master ( a97f84...b5df1f )
by
unknown
11:58
created

tests/Util/AdminObjectAclDataTest.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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\Util;
15
16
use PHPUnit\Framework\TestCase;
17
use Sonata\AdminBundle\Admin\AdminInterface;
18
use Sonata\AdminBundle\Security\Handler\AclSecurityHandlerInterface;
19
use Sonata\AdminBundle\Util\AdminObjectAclData;
20
use Symfony\Component\Form\Form;
21
use Symfony\Component\Security\Acl\Domain\Acl;
22
use Symfony\Component\Security\Acl\Permission\MaskBuilder;
23
24
/**
25
 * @author Kévin Dunglas <[email protected]>
26
 */
27
class AdminObjectAclDataTest extends TestCase
28
{
29
    public function testGetAdmin(): void
30
    {
31
        $adminObjectAclData = $this->createAdminObjectAclData();
32
        $this->assertInstanceOf(AdminInterface::class, $adminObjectAclData->getAdmin());
33
    }
34
35
    public function testGetObject(): void
36
    {
37
        $adminObjectAclData = $this->createAdminObjectAclData();
38
        $this->assertInstanceOf(\stdClass::class, $adminObjectAclData->getObject());
39
    }
40
41
    public function testGetAclUsers(): void
42
    {
43
        $adminObjectAclData = $this->createAdminObjectAclData();
44
        $this->assertInstanceOf(\ArrayIterator::class, $adminObjectAclData->getAclUsers());
45
    }
46
47
    public function testGetAclRoles(): void
48
    {
49
        $adminObjectAclData = $this->createAdminObjectAclData();
50
        $this->assertInstanceOf(\ArrayIterator::class, $adminObjectAclData->getAclRoles());
51
    }
52
53
    public function testSetAcl()
54
    {
55
        $acl = $this->getMockBuilder(Acl::class)
56
            ->disableOriginalConstructor()
57
            ->getMock();
58
        $adminObjectAclData = $this->createAdminObjectAclData();
59
        $ret = $adminObjectAclData->setAcl($acl);
60
61
        $this->assertSame($adminObjectAclData, $ret);
62
63
        return $adminObjectAclData;
64
    }
65
66
    /**
67
     * @depends testSetAcl
68
     */
69
    public function testGetAcl($adminObjectAclData): void
70
    {
71
        $this->assertInstanceOf(Acl::class, $adminObjectAclData->getAcl());
72
    }
73
74
    public function testGetMasks(): void
75
    {
76
        $adminObjectAclData = $this->createAdminObjectAclData();
77
        $this->assertInternalType('array', $adminObjectAclData->getMasks());
78
79
        foreach ($adminObjectAclData->getMasks() as $key => $mask) {
80
            $this->assertInternalType('string', $key);
81
            $this->assertInternalType('int', $mask);
82
        }
83
    }
84
85
    /**
86
     * @group legacy
87
     */
88
    public function testSetForm()
89
    {
90
        $form = $this->getMockBuilder(Form::class)
91
            ->disableOriginalConstructor()
92
            ->getMock();
93
        $adminObjectAclData = $this->createAdminObjectAclData();
94
        $ret = $adminObjectAclData->setAclUsersForm($form);
0 ignored issues
show
$form is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Symfony\Component\Form\Form>.

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:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
95
96
        $this->assertSame($adminObjectAclData, $ret);
97
98
        return $adminObjectAclData;
99
    }
100
101
    /**
102
     * @depends testSetForm
103
     *
104
     * @group legacy
105
     */
106
    public function testGetForm($adminObjectAclData): void
107
    {
108
        $this->assertInstanceOf(Form::class, $adminObjectAclData->getAclUsersForm());
109
    }
110
111
    public function testSetAclUsersForm()
112
    {
113
        $form = $this->getMockBuilder(Form::class)
114
            ->disableOriginalConstructor()
115
            ->getMock();
116
        $adminObjectAclData = $this->createAdminObjectAclData();
117
        $ret = $adminObjectAclData->setAclUsersForm($form);
118
119
        $this->assertSame($adminObjectAclData, $ret);
120
121
        return $adminObjectAclData;
122
    }
123
124
    /**
125
     * @depends testSetAclUsersForm
126
     */
127
    public function testGetAclUsersForm($adminObjectAclData): void
128
    {
129
        $this->assertInstanceOf(Form::class, $adminObjectAclData->getAclUsersForm());
130
    }
131
132
    public function testSetAclRolesForm()
133
    {
134
        $form = $this->getMockBuilder(Form::class)
135
            ->disableOriginalConstructor()
136
            ->getMock();
137
        $adminObjectAclData = $this->createAdminObjectAclData();
138
        $ret = $adminObjectAclData->setAclRolesForm($form);
139
140
        $this->assertSame($adminObjectAclData, $ret);
141
142
        return $adminObjectAclData;
143
    }
144
145
    /**
146
     * @depends testSetAclRolesForm
147
     */
148
    public function testGetAclRolesForm($adminObjectAclData): void
149
    {
150
        $this->assertInstanceOf(Form::class, $adminObjectAclData->getAclRolesForm());
151
    }
152
153
    public function testGetPermissions(): void
154
    {
155
        $adminObjectAclData = $this->createAdminObjectAclData();
156
        $this->assertInternalType('array', $adminObjectAclData->getPermissions());
157
158
        foreach ($adminObjectAclData->getPermissions() as $permission) {
159
            $this->assertInternalType('string', $permission);
160
        }
161
    }
162
163
    public function testGetUserPermissions(): void
164
    {
165
        $adminObjectAclDataOwner = $this->createAdminObjectAclData();
166
        $this->assertInternalType('array', $adminObjectAclDataOwner->getUserPermissions());
167
168
        foreach ($adminObjectAclDataOwner->getUserPermissions() as $permission) {
169
            $this->assertInternalType('string', $permission);
170
        }
171
172
        $this->assertTrue(false !== array_search('OWNER', $adminObjectAclDataOwner->getUserPermissions(), true));
173
        $this->assertTrue(false !== array_search('MASTER', $adminObjectAclDataOwner->getUserPermissions(), true));
174
175
        $adminObjectAclData = $this->createAdminObjectAclData(false);
176
        $this->assertInternalType('array', $adminObjectAclData->getUserPermissions());
177
178
        foreach ($adminObjectAclData->getUserPermissions() as $permission) {
179
            $this->assertInternalType('string', $permission);
180
        }
181
182
        $this->assertFalse(array_search('OWNER', $adminObjectAclData->getUserPermissions(), true));
183
        $this->assertFalse(array_search('MASTER', $adminObjectAclData->getUserPermissions(), true));
184
    }
185
186
    public function testIsOwner(): void
187
    {
188
        $adminObjectAclDataOwner = $this->createAdminObjectAclData();
189
        $this->assertTrue($adminObjectAclDataOwner->isOwner());
190
191
        $adminObjectAclData = $this->createAdminObjectAclData(false);
192
        $this->assertFalse($adminObjectAclData->isOwner());
193
    }
194
195
    public function testGetSecurityHandler(): void
196
    {
197
        $adminObjectAclData = $this->createAdminObjectAclData();
198
199
        $this->assertInstanceOf(AclSecurityHandlerInterface::class, $adminObjectAclData->getSecurityHandler());
200
    }
201
202
    public function testGetSecurityInformation(): void
203
    {
204
        $adminObjectAclData = $this->createAdminObjectAclData();
205
206
        $this->assertSame([], $adminObjectAclData->getSecurityInformation());
207
    }
208
209
    protected static function createAclUsers()
210
    {
211
        return new \ArrayIterator();
212
    }
213
214
    protected static function createAclRoles()
215
    {
216
        return new \ArrayIterator();
217
    }
218
219
    protected function createAdminObjectAclData($isOwner = true)
220
    {
221
        return new AdminObjectAclData($this->createAdmin($isOwner), new \stdClass(), self::createAclUsers(), MaskBuilder::class, self::createAclRoles());
222
    }
223
224
    protected function createAdmin($isOwner = true)
225
    {
226
        $securityHandler = $this->getMockForAbstractClass(AclSecurityHandlerInterface::class);
227
228
        $securityHandler->expects($this->any())
229
            ->method('getObjectPermissions')
230
            ->will($this->returnValue(['VIEW', 'EDIT', 'DELETE', 'UNDELETE', 'OPERATOR', 'MASTER', 'OWNER']))
231
        ;
232
233
        $securityHandler->expects($this->any())
234
            ->method('buildSecurityInformation')
235
            ->with($this->isInstanceOf(AdminInterface::class))
236
            ->will($this->returnValue([]))
237
        ;
238
239
        $admin = $this->getMockForAbstractClass(AdminInterface::class);
240
241
        $admin->expects($this->any())
242
            ->method('isGranted')
243
            ->will($this->returnValue($isOwner))
244
        ;
245
246
        $admin->expects($this->any())
247
            ->method('getSecurityHandler')
248
            ->will($this->returnValue($securityHandler))
249
        ;
250
251
        return $admin;
252
    }
253
}
254