Failed Conditions
Pull Request — master (#13)
by Adrien
05:33 queued 02:11
created

DebugAclTest.php$0 ➔ assert()   A

Complexity

Conditions 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
c 1
b 0
f 0
dl 0
loc 3
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace EcodevTests\Felix\Acl;
6
7
use Ecodev\Felix\Acl\Assertion\IsMyself;
8
use Ecodev\Felix\Acl\Assertion\NamedAssertion;
9
use Ecodev\Felix\Acl\DebugAcl;
10
use Ecodev\Felix\Acl\MultipleRoles;
11
use EcodevTests\Felix\Traits\TestWithContainer;
12
use Exception;
13
use Laminas\Permissions\Acl\Acl;
14
use Laminas\Permissions\Acl\Resource\ResourceInterface;
15
use Laminas\Permissions\Acl\Role\RoleInterface;
16
use PHPUnit\Framework\TestCase;
17
18
class DebugAclTest extends TestCase
19
{
20
    use TestWithContainer;
21
22
    private DebugAcl $acl;
23
24
    private NamedAssertion $adminAssertion;
25
26
    protected function setUp(): void
27
    {
28
        $this->createDefaultFelixContainer();
29
        $this->acl = new DebugAcl();
30
31
        $this->acl->addRole('member');
32
        $this->acl->addRole('admin');
33
34
        $this->acl->addResource('user');
35
        $this->acl->addResource('post');
36
37
        $this->adminAssertion = new class() implements NamedAssertion {
38
            public function assert(Acl $acl, ?RoleInterface $role = null, ?ResourceInterface $resource = null, $privilege = null): never
1 ignored issue
show
Bug introduced by
The type EcodevTests\Felix\Acl\never was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
39
            {
40
                throw new Exception('Assertion should never be run in debug version of ACL');
41
            }
42
43
            public function getName(): string
44
            {
45
                return 'admin assertion';
46
            }
47
        };
48
    }
49
50
    public function testPrivileges(): void
51
    {
52
        self::assertSame([], $this->acl->getPrivileges());
53
54
        $this->acl->allow('member', 'user', 'read');
55
        self::assertSame(['read'], $this->acl->getPrivileges());
56
57
        $this->acl->allow('member', 'post', 'read');
58
        self::assertSame(['read'], $this->acl->getPrivileges());
59
60
        $this->acl->allow('admin', 'post', ['create', 'unusual-privilege']);
61
        $this->acl->deny('admin', 'post', ['denied-privilege']);
62
        self::assertSame(['create', 'read', 'denied-privilege', 'unusual-privilege'], $this->acl->getPrivileges());
63
64
        $this->acl->allow('admin', 'post', null);
65
        self::assertSame([null, 'create', 'read', 'denied-privilege', 'unusual-privilege'], $this->acl->getPrivileges());
66
67
        self::assertSame(
68
            [
69
                'privilege' => 'create',
70
                'allowed' => false,
71
                'allowIf' => [],
72
                'denyIf' => [],
73
            ],
74
            $this->acl->show('member', 'user', 'create')
75
        );
76
    }
77
78
    public function testNamedAssertionsWithAllow(): void
79
    {
80
        $this->acl->allow('member', 'user', 'read', new IsMyself());
81
        $this->acl->allow('admin', 'user', 'read', $this->adminAssertion);
82
83
        self::assertSame(
84
            [
85
                'privilege' => 'read',
86
                'allowed' => false,
87
                'allowIf' => ["c'est moi-même"],
88
                'denyIf' => [],
89
            ],
90
            $this->acl->show('member', 'user', 'read')
91
        );
92
93
        self::assertSame(
94
            [
95
                'privilege' => 'read',
96
                'allowed' => false,
97
                'allowIf' => ['admin assertion'],
98
                'denyIf' => [],
99
            ],
100
            $this->acl->show('admin', 'user', 'read')
101
        );
102
103
        self::assertSame(
104
            [
105
                'privilege' => 'read',
106
                'allowed' => false,
107
                'allowIf' => ['admin assertion', "c'est moi-même"],
108
                'denyIf' => [],
109
            ],
110
            $this->acl->show(new MultipleRoles(['member', 'admin']), 'user', 'read')
111
        );
112
113
        self::assertSame(
114
            [
115
                'privilege' => 'non-existing-privilege',
116
                'allowed' => false,
117
                'allowIf' => [],
118
                'denyIf' => [],
119
            ],
120
            $this->acl->show('member', 'user', 'non-existing-privilege')
121
        );
122
123
        self::assertSame(
124
            [
125
                'privilege' => null,
126
                'allowed' => false,
127
                'allowIf' => ["c'est moi-même"],
128
                'denyIf' => [],
129
            ],
130
            $this->acl->show('member', 'user', null)
131
        );
132
    }
133
134
    public function testNamedAssertionsWithDeny(): void
135
    {
136
        $this->acl->allow('member', 'user', null);
137
        $this->acl->allow('admin', 'user', null);
138
        $this->acl->deny('member', 'user', 'read', new IsMyself());
139
        $this->acl->deny('admin', 'user', 'read', $this->adminAssertion);
140
141
        self::assertSame(
142
            [
143
                'privilege' => 'read',
144
                'allowed' => false,
145
                'allowIf' => [],
146
                'denyIf' => ["c'est moi-même"],
147
            ],
148
            $this->acl->show('member', 'user', 'read')
149
        );
150
151
        self::assertSame(
152
            [
153
                'privilege' => 'read',
154
                'allowed' => false,
155
                'allowIf' => [],
156
                'denyIf' => ['admin assertion'],
157
            ],
158
            $this->acl->show('admin', 'user', 'read')
159
        );
160
161
        self::assertSame(
162
            [
163
                'privilege' => 'read',
164
                'allowed' => false,
165
                'allowIf' => [],
166
                'denyIf' => ['admin assertion', "c'est moi-même"],
167
            ],
168
            $this->acl->show(new MultipleRoles(['member', 'admin']), 'user', 'read')
169
        );
170
171
        self::assertSame(
172
            [
173
                'privilege' => 'non-existing-privilege',
174
                'allowed' => true,  // True because allowed via the `null` wildcard
175
                'allowIf' => [],
176
                'denyIf' => [],
177
            ],
178
            $this->acl->show('member', 'user', 'non-existing-privilege')
179
        );
180
    }
181
}
182