Failed Conditions
Pull Request — master (#13)
by Adrien
14:06
created

DebugAclTest::testNamedAssertionsWithDeny()

Size

Total Lines 45
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 28
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 45
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 Exception;
12
use Laminas\Permissions\Acl\Acl;
13
use Laminas\Permissions\Acl\Resource\ResourceInterface;
14
use Laminas\Permissions\Acl\Role\RoleInterface;
15
use PHPUnit\Framework\TestCase;
16
17
class DebugAclTest extends TestCase
18
{
19
    private DebugAcl $acl;
20
21
    private NamedAssertion $adminAssertion;
22
23
    protected function setUp(): void
24
    {
25
        $this->acl = new DebugAcl();
26
27
        $this->acl->addRole('member');
28
        $this->acl->addRole('admin');
29
30
        $this->acl->addResource('user');
31
        $this->acl->addResource('post');
32
33
        $this->adminAssertion = new class() implements NamedAssertion {
34
            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...
35
            {
36
                throw new Exception('Assertion should never be run in debug version of ACL');
37
            }
38
39
            public function getName(): string
40
            {
41
                return 'admin assertion';
42
            }
43
        };
44
    }
45
46
    public function testPrivileges(): void
47
    {
48
        self::assertSame([], $this->acl->getPrivileges());
49
50
        $this->acl->allow('member', 'user', 'read');
51
        self::assertSame(['read'], $this->acl->getPrivileges());
52
53
        $this->acl->allow('member', 'post', 'read');
54
        self::assertSame(['read'], $this->acl->getPrivileges());
55
56
        $this->acl->allow('admin', 'post', ['create', 'unusual-privilege']);
57
        $this->acl->deny('admin', 'post', ['denied-privilege']);
58
        self::assertSame(['create', 'read', 'denied-privilege', 'unusual-privilege'], $this->acl->getPrivileges());
59
60
        $this->acl->allow('admin', 'post', null);
61
        self::assertSame([null, 'create', 'read', 'denied-privilege', 'unusual-privilege'], $this->acl->getPrivileges());
62
63
        self::assertSame(
64
            [
65
                'privilege' => 'create',
66
                'allowed' => false,
67
                'allowIf' => [],
68
                'denyIf' => [],
69
            ],
70
            $this->acl->show('member', 'user', 'create')
71
        );
72
    }
73
74
    public function testNamedAssertionsWithAllow(): void
75
    {
76
        $this->acl->allow('member', 'user', 'read', new IsMyself());
77
        $this->acl->allow('admin', 'user', 'read', $this->adminAssertion);
78
79
        self::assertSame(
80
            [
81
                'privilege' => 'read',
82
                'allowed' => false,
83
                'allowIf' => ["c'est moi-même"],
84
                'denyIf' => [],
85
            ],
86
            $this->acl->show('member', 'user', 'read')
87
        );
88
89
        self::assertSame(
90
            [
91
                'privilege' => 'read',
92
                'allowed' => false,
93
                'allowIf' => ['admin assertion'],
94
                'denyIf' => [],
95
            ],
96
            $this->acl->show('admin', 'user', 'read')
97
        );
98
99
        self::assertSame(
100
            [
101
                'privilege' => 'read',
102
                'allowed' => false,
103
                'allowIf' => ['admin assertion', "c'est moi-même"],
104
                'denyIf' => [],
105
            ],
106
            $this->acl->show(new MultipleRoles(['member', 'admin']), 'user', 'read')
107
        );
108
109
        self::assertSame(
110
            [
111
                'privilege' => 'non-existing-privilege',
112
                'allowed' => false,
113
                'allowIf' => [],
114
                'denyIf' => [],
115
            ],
116
            $this->acl->show('member', 'user', 'non-existing-privilege')
117
        );
118
119
        self::assertSame(
120
            [
121
                'privilege' => null,
122
                'allowed' => false,
123
                'allowIf' => ["c'est moi-même"],
124
                'denyIf' => [],
125
            ],
126
            $this->acl->show('member', 'user', null)
127
        );
128
    }
129
130
    public function testNamedAssertionsWithDeny(): void
131
    {
132
        $this->acl->allow('member', 'user', null);
133
        $this->acl->allow('admin', 'user', null);
134
        $this->acl->deny('member', 'user', 'read', new IsMyself());
135
        $this->acl->deny('admin', 'user', 'read', $this->adminAssertion);
136
137
        self::assertSame(
138
            [
139
                'privilege' => 'read',
140
                'allowed' => false,
141
                'allowIf' => [],
142
                'denyIf' => ["c'est moi-même"],
143
            ],
144
            $this->acl->show('member', 'user', 'read')
145
        );
146
147
        self::assertSame(
148
            [
149
                'privilege' => 'read',
150
                'allowed' => false,
151
                'allowIf' => [],
152
                'denyIf' => ['admin assertion'],
153
            ],
154
            $this->acl->show('admin', 'user', 'read')
155
        );
156
157
        self::assertSame(
158
            [
159
                'privilege' => 'read',
160
                'allowed' => false,
161
                'allowIf' => [],
162
                'denyIf' => ['admin assertion', "c'est moi-même"],
163
            ],
164
            $this->acl->show(new MultipleRoles(['member', 'admin']), 'user', 'read')
165
        );
166
167
        self::assertSame(
168
            [
169
                'privilege' => 'non-existing-privilege',
170
                'allowed' => true,  // True because allowed via the `null` wildcard
171
                'allowIf' => [],
172
                'denyIf' => [],
173
            ],
174
            $this->acl->show('member', 'user', 'non-existing-privilege')
175
        );
176
    }
177
}
178