1 | <?php |
||
2 | /* |
||
3 | * This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony). |
||
4 | * |
||
5 | * Copyright (C) 2019 - 2022 Jan Böhmer (https://github.com/jbtronics) |
||
6 | * |
||
7 | * This program is free software: you can redistribute it and/or modify |
||
8 | * it under the terms of the GNU Affero General Public License as published |
||
9 | * by the Free Software Foundation, either version 3 of the License, or |
||
10 | * (at your option) any later version. |
||
11 | * |
||
12 | * This program is distributed in the hope that it will be useful, |
||
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
||
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||
15 | * GNU Affero General Public License for more details. |
||
16 | * |
||
17 | * You should have received a copy of the GNU Affero General Public License |
||
18 | * along with this program. If not, see <https://www.gnu.org/licenses/>. |
||
19 | */ |
||
20 | |||
21 | namespace App\Tests\Entity\UserSystem; |
||
22 | |||
23 | use App\Entity\UserSystem\PermissionData; |
||
24 | use PHPUnit\Framework\TestCase; |
||
0 ignored issues
–
show
|
|||
25 | |||
26 | class PermissionDataTest extends TestCase |
||
27 | { |
||
28 | |||
29 | public function testGetSetIs() |
||
30 | { |
||
31 | $perm_data = new PermissionData(); |
||
32 | |||
33 | //Empty object should have all permissions set to inherit |
||
34 | $this->assertNull($perm_data->getPermissionValue('not_existing', 'not_existing')); |
||
35 | $this->assertFalse($perm_data->isPermissionSet('not_existing', 'not_existing')); |
||
36 | |||
37 | $this->assertNull($perm_data->getPermissionValue('p1', 'op1')); |
||
38 | $this->assertNull($perm_data->getPermissionValue('p1', 'op2')); |
||
39 | $this->assertNull($perm_data->getPermissionValue('p2', 'op1')); |
||
40 | |||
41 | //Set values |
||
42 | $perm_data->setPermissionValue('p1', 'op1', PermissionData::ALLOW); |
||
43 | $perm_data->setPermissionValue('p1', 'op2', PermissionData::DISALLOW); |
||
44 | $perm_data->setPermissionValue('p2', 'op1', PermissionData::ALLOW); |
||
45 | |||
46 | //Check that values were set |
||
47 | $this->assertTrue($perm_data->isPermissionSet('p1', 'op1')); |
||
48 | $this->assertTrue($perm_data->isPermissionSet('p1', 'op2')); |
||
49 | $this->assertTrue($perm_data->isPermissionSet('p2', 'op1')); |
||
50 | |||
51 | //Check that values are correct |
||
52 | $this->assertTrue($perm_data->getPermissionValue('p1', 'op1')); |
||
53 | $this->assertFalse($perm_data->getPermissionValue('p1', 'op2')); |
||
54 | $this->assertTrue($perm_data->getPermissionValue('p2', 'op1')); |
||
55 | |||
56 | //Set values to null |
||
57 | $perm_data->setPermissionValue('p1', 'op1', null); |
||
58 | $this->assertNull($perm_data->getPermissionValue('p1', 'op1')); |
||
59 | //Values should be unset now |
||
60 | $this->assertFalse($perm_data->isPermissionSet('p1', 'op1')); |
||
61 | } |
||
62 | |||
63 | public function testJSONSerialization() |
||
64 | { |
||
65 | $perm_data = new PermissionData(); |
||
66 | |||
67 | $perm_data->setPermissionValue('perm1', 'op1', PermissionData::ALLOW); |
||
68 | $perm_data->setPermissionValue('perm1', 'op2', PermissionData::DISALLOW); |
||
69 | $perm_data->setPermissionValue('perm1', 'op3', PermissionData::ALLOW); |
||
70 | |||
71 | $perm_data->setPermissionValue('perm2', 'op1', PermissionData::ALLOW); |
||
72 | $perm_data->setPermissionValue('perm2', 'op2', PermissionData::DISALLOW); |
||
73 | |||
74 | //Ensure that JSON serialization works |
||
75 | $this->assertJsonStringEqualsJsonString(json_encode([ |
||
76 | 'perm1' => [ |
||
77 | 'op1' => true, |
||
78 | 'op2' => false, |
||
79 | 'op3' => true, |
||
80 | ], |
||
81 | 'perm2' => [ |
||
82 | 'op1' => true, |
||
83 | 'op2' => false, |
||
84 | ], |
||
85 | ], JSON_THROW_ON_ERROR), json_encode($perm_data, JSON_THROW_ON_ERROR)); |
||
86 | |||
87 | //Set values to inherit to ensure they do not show up in the json |
||
88 | $perm_data->setPermissionValue('perm1', 'op3', null); |
||
89 | $perm_data->setPermissionValue('perm2', 'op1', null); |
||
90 | $perm_data->setPermissionValue('perm2', 'op2', null); |
||
91 | |||
92 | //Ensure that JSON serialization works |
||
93 | $this->assertJsonStringEqualsJsonString(json_encode([ |
||
94 | 'perm1' => [ |
||
95 | 'op1' => true, |
||
96 | 'op2' => false, |
||
97 | ], |
||
98 | ], JSON_THROW_ON_ERROR), json_encode($perm_data, JSON_THROW_ON_ERROR)); |
||
99 | |||
100 | } |
||
101 | |||
102 | public function testFromJSON() |
||
103 | { |
||
104 | $json = json_encode([ |
||
105 | 'perm1' => [ |
||
106 | 'op1' => true, |
||
107 | 'op2' => false, |
||
108 | 'op3' => true, |
||
109 | ], |
||
110 | 'perm2' => [ |
||
111 | 'op1' => true, |
||
112 | 'op2' => false, |
||
113 | ], |
||
114 | ], JSON_THROW_ON_ERROR); |
||
115 | |||
116 | $perm_data = PermissionData::fromJSON($json); |
||
117 | |||
118 | //Ensure that values were set correctly |
||
119 | $this->assertTrue($perm_data->getPermissionValue('perm1', 'op1')); |
||
120 | $this->assertFalse($perm_data->getPermissionValue('perm2', 'op2')); |
||
121 | } |
||
122 | |||
123 | public function testResetPermissions() |
||
124 | { |
||
125 | $data = new PermissionData(); |
||
126 | |||
127 | $data->setPermissionValue('perm1', 'op1', PermissionData::ALLOW); |
||
128 | $data->setPermissionValue('perm1', 'op2', PermissionData::DISALLOW); |
||
129 | $data->setPermissionValue('perm1', 'op3', PermissionData::INHERIT); |
||
130 | |||
131 | //Ensure that values were set correctly |
||
132 | $this->assertTrue($data->isPermissionSet('perm1', 'op1')); |
||
133 | $this->assertTrue($data->isPermissionSet('perm1', 'op2')); |
||
134 | $this->assertFalse($data->isPermissionSet('perm1', 'op3')); |
||
135 | |||
136 | //Reset the permissions |
||
137 | $data->resetPermissions(); |
||
138 | |||
139 | //Afterwards all values must be set to inherit (null) |
||
140 | $this->assertNull($data->getPermissionValue('perm1', 'op1')); |
||
141 | $this->assertNull($data->getPermissionValue('perm1', 'op2')); |
||
142 | $this->assertNull($data->getPermissionValue('perm1', 'op3')); |
||
143 | |||
144 | //And be undefined |
||
145 | $this->assertFalse($data->isPermissionSet('perm1', 'op1')); |
||
146 | $this->assertFalse($data->isPermissionSet('perm1', 'op2')); |
||
147 | $this->assertFalse($data->isPermissionSet('perm1', 'op3')); |
||
148 | } |
||
149 | |||
150 | public function testGetSchemaVersion() |
||
151 | { |
||
152 | $data = new PermissionData(); |
||
153 | |||
154 | //By default the schema version must be the CURRENT_SCHEMA_VERSION |
||
155 | $this->assertEquals(PermissionData::CURRENT_SCHEMA_VERSION, $data->getSchemaVersion()); |
||
156 | |||
157 | //Ensure that the schema version can be set |
||
158 | $data->setSchemaVersion(12345); |
||
159 | $this->assertEquals(12345, $data->getSchemaVersion()); |
||
160 | } |
||
161 | |||
162 | public function testIsAnyOperationOfPermissionSet() |
||
163 | { |
||
164 | $data = new PermissionData(); |
||
165 | |||
166 | //Initially no operation of any permission is set |
||
167 | $this->assertFalse($data->isAnyOperationOfPermissionSet('perm1')); |
||
168 | |||
169 | $data->setPermissionValue('perm1', 'op1', PermissionData::ALLOW); |
||
170 | $this->assertTrue($data->isAnyOperationOfPermissionSet('perm1')); |
||
171 | } |
||
172 | |||
173 | public function testGetAllDefinedOperationsOfPermission() |
||
174 | { |
||
175 | $data = new PermissionData(); |
||
176 | |||
177 | $this->assertEmpty($data->getAllDefinedOperationsOfPermission('perm1')); |
||
178 | |||
179 | $data->setPermissionValue('perm1', 'op1', PermissionData::ALLOW); |
||
180 | $data->setPermissionValue('perm1', 'op2', PermissionData::DISALLOW); |
||
181 | |||
182 | $this->assertEquals([ |
||
183 | 'op1' => PermissionData::ALLOW, 'op2' => PermissionData::DISALLOW, |
||
184 | ], |
||
185 | $data->getAllDefinedOperationsOfPermission('perm1')); |
||
186 | } |
||
187 | |||
188 | public function testSetAllOperationsOfPermission() |
||
189 | { |
||
190 | $data = new PermissionData(); |
||
191 | |||
192 | $data->setAllOperationsOfPermission('perm1', [ |
||
193 | 'op1' => PermissionData::ALLOW, |
||
194 | 'op2' => PermissionData::DISALLOW, |
||
195 | ]); |
||
196 | |||
197 | $this->assertEquals([ |
||
198 | 'op1' => PermissionData::ALLOW, 'op2' => PermissionData::DISALLOW, |
||
199 | ], |
||
200 | $data->getAllDefinedOperationsOfPermission('perm1')); |
||
201 | } |
||
202 | |||
203 | public function testRemovePermission() |
||
204 | { |
||
205 | $data = new PermissionData(); |
||
206 | |||
207 | $data->setPermissionValue('perm1', 'op1', PermissionData::ALLOW); |
||
208 | $data->setPermissionValue('perm1', 'op2', PermissionData::DISALLOW); |
||
209 | |||
210 | $this->assertTrue($data->isPermissionSet('perm1', 'op1')); |
||
211 | $this->assertTrue($data->isPermissionSet('perm1', 'op2')); |
||
212 | |||
213 | $data->removePermission('perm1'); |
||
214 | |||
215 | $this->assertFalse($data->isPermissionSet('perm1', 'op1')); |
||
216 | $this->assertFalse($data->isPermissionSet('perm1', 'op2')); |
||
217 | } |
||
218 | } |
||
219 |
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:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths