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

tests/Util/ObjectAclManipulatorTest.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 Prophecy\Argument;
18
use Sonata\AdminBundle\Admin\AdminInterface;
19
use Sonata\AdminBundle\Security\Handler\AclSecurityHandlerInterface;
20
use Sonata\AdminBundle\Tests\Fixtures\Util\DummyObjectAclManipulator;
21
use Symfony\Component\Console\Output\OutputInterface;
22
use Symfony\Component\Security\Acl\Domain\UserSecurityIdentity;
23
use Symfony\Component\Security\Acl\Model\AclInterface;
24
use Symfony\Component\Security\Acl\Model\ObjectIdentityInterface;
25
26
/**
27
 * @author Grégoire Paris <[email protected]>
28
 */
29
class ObjectAclManipulatorTest extends TestCase
30
{
31
    protected function setUp(): void
32
    {
33
        $this->output = $this->prophesize(OutputInterface::class);
34
        $this->admin = $this->prophesize(AdminInterface::class);
35
        $this->oids = new \ArrayIterator([
0 ignored issues
show
The property oids does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
36
            $this->prophesize(ObjectIdentityInterface::class)->reveal(),
37
            $this->prophesize(ObjectIdentityInterface::class)->reveal(),
38
        ]);
39
        $this->securityIdentity = new UserSecurityIdentity('Michael', 'stdClass');
40
    }
41
42
    public function testConfigureAclsIgnoresNonAclSecurityHandlers(): void
43
    {
44
        $this->admin->getSecurityHandler()->shouldBeCalled();
45
        $this->admin->getCode()->shouldBeCalled()->willReturn('test');
46
        $this->output->writeln(Argument::allof(
47
            Argument::containingString('ignoring'),
48
            Argument::containingString('test')
49
        ))->shouldBeCalled();
50
        $manipulator = new DummyObjectAclManipulator();
51
        $this->assertSame(
52
            [0, 0],
53
            $manipulator->configureAcls(
54
                $this->output->reveal(),
55
                $this->admin->reveal(),
56
                $this->oids,
57
                $this->securityIdentity
58
            )
59
        );
60
    }
61
62
    public function testConfigureAcls(): void
63
    {
64
        $securityHandler = $this->prophesize(AclSecurityHandlerInterface::class);
65
        $acls = $this->prophesize('SplObjectStorage');
66
        $acls->contains(Argument::type(ObjectIdentityInterface::class))
67
            ->shouldBeCalled()
68
            ->willReturn(false, true);
69
        $acl = $this->prophesize(AclInterface::class)->reveal();
70
        $acls->offsetGet(Argument::Type(ObjectIdentityInterface::class))
71
            ->shouldBeCalled()
72
            ->willReturn($acl);
73
        $securityHandler->findObjectAcls($this->oids)->shouldBeCalled()->willReturn($acls->reveal());
74
        $securityHandler->createAcl(Argument::type(ObjectIdentityInterface::class))->shouldBeCalled()->willReturn($acl);
75
        $securityHandler->addObjectOwner($acl, Argument::type(UserSecurityIdentity::class))->shouldBeCalled();
76
        $securityHandler->buildSecurityInformation($this->admin)->shouldBeCalled()->willReturn([]);
77
        $securityHandler->addObjectClassAces($acl, [])->shouldBeCalled();
78
        $securityHandler->updateAcl($acl)->shouldBeCalled()->willThrow(new \Exception('test exception'));
79
        $this->output->writeln(Argument::allof(
80
            Argument::containingString('ignoring'),
81
            Argument::containingString('test exception')
82
        ))->shouldBeCalled();
83
84
        $this->admin->getSecurityHandler()->shouldBeCalled()->willReturn($securityHandler->reveal());
85
86
        $manipulator = new DummyObjectAclManipulator();
87
88
        $this->assertSame(
89
            [1, 1],
90
            $manipulator->configureAcls(
91
                $this->output->reveal(),
92
                $this->admin->reveal(),
93
                $this->oids,
94
                $this->securityIdentity
95
            )
96
        );
97
    }
98
}
99