1010 * @author Ibrahim
1111 */
1212class AccessTest extends TestCase {
13+ public function setUp (): void {
14+ Access::clear ();
15+ }
1316 /**
1417 * @test
1518 */
@@ -31,7 +34,6 @@ public function test01() {
3134 public function test02 () {
3235 $ this ->assertTrue (Access::newGroup ('ADMINS_Group ' ));
3336 $ this ->assertFalse (Access::newGroup ('ADMINS_Group ' ));
34- Access::clear ();
3537 }
3638 /**
3739 * @test
@@ -40,7 +42,6 @@ public function test03() {
4042 $ this ->assertFalse (Access::newGroup ('ADMINS_Group; ' ));
4143 $ this ->assertFalse (Access::newGroup ('ADMINS_Group, ' ));
4244 $ this ->assertFalse (Access::newGroup ('ADMINS-Group, ' ));
43- Access::clear ();
4445 }
4546 /**
4647 * @test
@@ -50,7 +51,6 @@ public function test04() {
5051 $ this ->assertTrue (Access::newGroup ('USERS_Group ' ));
5152 $ this ->assertEquals (2 , count (Access::groups ()));
5253 $ this ->assertEquals (0 , count (Access::privileges ()));
53- Access::clear ();
5454 }
5555 /**
5656 * @test
@@ -61,7 +61,6 @@ public function test05() {
6161 $ this ->assertTrue (Access::newGroup ('USERS_Group ' ));
6262 $ this ->assertEquals (2 , count (Access::groups ()));
6363 $ this ->assertEquals (0 , count (Access::privileges ()));
64- Access::clear ();
6564 }
6665 /**
6766 * @test
@@ -75,7 +74,6 @@ public function test06() {
7574 $ this ->assertTrue (Access::hasGroup ('USERS_MANAGEMENT_GROUP ' ));
7675 $ this ->assertTrue (Access::hasGroup ('USERS_Group ' ));
7776 $ this ->assertEquals (0 , count (Access::privileges ()));
78- Access::clear ();
7977 }
8078 /**
8179 * @test
@@ -104,7 +102,6 @@ public function test07() {
104102 $ this ->assertFalse (Access::hasPrivilege ('new_pr ' ,'USERS_Group ' ));
105103 $ this ->assertFalse (Access::hasPrivilege ('new_pr ' ,'ADMINS_Group ' ));
106104 $ this ->assertTrue (Access::hasPrivilege ('new_pr ' ,'USERS_MANAGEMENT_GROUP ' ));
107- Access::clear ();
108105 }
109106 /**
110107 * @test
@@ -115,50 +112,12 @@ public function test08() {
115112 Access::newGroup ('SUB_SUB_SUPER ' , 'SUB_SUPER ' );
116113 $ this ->assertFalse (Access::newGroup ('SUPER ' , 'SUB_SUB_SUPER ' ));
117114 $ this ->assertFalse (Access::newPrivilege ('SUB_SUB_SUPER ' , 'SUPER ' ));
118- Access::clear ();
119115 }
120116 /**
121117 * @test
122118 */
123119 public function test09 () {
124- $ this ->assertTrue (Access::newGroup ('ADMINS ' ));
125- $ r = Access::newPrivileges ('ADMINS ' , [
126- 'MODIFY_SYS_SETTINGS '
127- ]);
128-
129- foreach ($ r as $ bool ) {
130- $ this ->assertTrue ($ bool );
131- }
132- Access::newGroup ('SYS_USER ' );
133- Access::newPrivilege ('SYS_USER ' , 'RESET_PASSWORD_SELF ' );
134- $ this ->assertTrue (Access::newGroup ('USERS_MANAGERS ' , 'ADMINS ' ));
135- Access::newPrivileges ('USERS_MANAGERS ' , [
136- 'RESET_ANY_USER_PASSWORD ' ,
137- 'CREATE_USER_ACCESS ' ,'UPDATE_USER_ACCESS ' ,
138- 'BLOCK_USER '
139- ]);
140- $ this ->assertTrue (Access::newGroup ('FINANCE ' ,'SYS_USER ' ));
141- Access::newPrivileges ('FINANCE ' , [
142- 'CREATE_INVOICE ' ,
143- 'REVERSE_INVOICE ' ,'VIEW_SALES_REPORT ' ,
144- 'RESET_PASSWORD_SELF ' ,
145- 'DEPOSIT ' ,'WITHDRAW ' ,
146- ]);
147- $ this ->assertTrue (Access::newGroup ('HR ' ,'SYS_USER ' ));
148- Access::newPrivileges ('HR ' , [
149- 'VIEW_ATTENDANCE ' ,
150- 'UPDATE_SALARY ' ,
151- 'UPDATE_POSITION ' ,'RESET_PASSWORD_SELF ' ,
152- 'DO_EMP_EVAL ' ,'WITHDRAW '
153- ]);
154- $ this ->assertTrue (Access::newGroup ('EMPLOYER ' , 'HR ' ));
155- $ this ->assertEquals ([
156- 'DO_INTERVIEW ' => true ,
157- 'EVALUATE_APLICANT ' => true
158- ], Access::newPrivileges ('EMPLOYER ' , [
159- 'DO_INTERVIEW ' ,'EVALUATE_APLICANT '
160- ]));
161- $ this ->assertFalse (Access::newGroup ('SYS_USER ' ,'HR ' ));
120+ $ this ->setupComplexGroups ();
162121 $ this ->assertEquals (2 ,count (Access::groups ()));
163122 $ this ->assertEquals (1 ,count (Access::privileges ('ADMINS ' )));
164123 $ this ->assertEquals (1 ,count (Access::privileges ('SYS_USER ' )));
@@ -170,9 +129,9 @@ public function test09() {
170129 }
171130 /**
172131 * @test
173- * @depends test09
174132 */
175133 public function testAsArray00 () {
134+ $ this ->setupComplexGroups ();
176135 $ asArr = Access::asArray ();
177136 $ this ->assertEquals (2 ,count ($ asArr ));
178137 $ this ->assertEquals ('ADMINS ' ,$ asArr [0 ]['group-id ' ]);
@@ -184,9 +143,9 @@ public function testAsArray00() {
184143 }
185144 /**
186145 * @test
187- * @depends test09
188146 */
189147 public function testCreatePrivilegesStr00 () {
148+ $ this ->setupComplexGroups ();
190149 $ user = new User ();
191150 $ user ->addPrivilege ('DO_INTERVIEW ' );
192151 $ user ->addPrivilege ('EVALUATE_APLICANT ' );
@@ -195,19 +154,19 @@ public function testCreatePrivilegesStr00() {
195154 }
196155 /**
197156 * @test
198- * @depends test09
199157 */
200158 public function testCreatePrivilegesStr01 () {
159+ $ this ->setupComplexGroups ();
201160 $ user = new User ();
202161 $ user ->addToGroup ('EMPLOYER ' );
203162 $ str = Access::createPermissionsStr ($ user );
204163 $ this ->assertEquals ('G-EMPLOYER ' ,$ str );
205164 }
206165 /**
207166 * @test
208- * @depends test09
209167 */
210168 public function testCreatePrivilegesStr02 () {
169+ $ this ->setupComplexGroups ();
211170 $ user = new User ();
212171 $ user ->addToGroup ('EMPLOYER ' );
213172 $ user ->addPrivilege ('REVERSE_INVOICE ' );
@@ -216,34 +175,34 @@ public function testCreatePrivilegesStr02() {
216175 }
217176 /**
218177 * @test
219- * @depends test09
220178 */
221179 public function testCreatePrivilegesStr03 () {
180+ $ this ->setupComplexGroups ();
222181 $ user = new User ();
223182 $ user ->addToGroup ('EMPLOYER ' );
224183 $ user ->addToGroup ('ADMINS ' );
225184 $ user ->addPrivilege ('REVERSE_INVOICE ' );
226185 $ user ->addPrivilege ('RESET_PASSWORD_SELF ' );
227186 $ str = Access::createPermissionsStr ($ user );
228187 $ this ->assertEquals ('G-ADMINS;G-EMPLOYER;REVERSE_INVOICE-1;RESET_PASSWORD_SELF-1 ' ,$ str );
229-
230- return $ str ;
231188 }
232189 /**
233- *
234- * @param User $user
235- * @depends testResolvePrivilegesStr01
190+ * @test
236191 */
237- public function testCreatePrivilegesStr04 ($ user ) {
192+ public function testCreatePrivilegesStr04 () {
193+ $ this ->setupSubGroups ();
194+ $ user = new User ();
195+ $ user ->addToGroup ('SUB_OF_SUB ' );
196+ $ user ->addPrivilege ('SUB_PR_3 ' );
197+ $ user ->addPrivilege ('TOP_PR_2 ' );
238198 $ privilegesStr = Access::createPermissionsStr ($ user );
239199 $ this ->assertEquals ('G-SUB_OF_SUB;SUB_PR_3-1;TOP_PR_2-1 ' ,$ privilegesStr );
240200 }
241201 /**
242- *
243- * @depends testResolvePrivilegesStr01
244202 * @test
245203 */
246204 public function testCreatePrivilegesStr06 () {
205+ $ this ->setupSubGroups ();
247206 $ user = new User ();
248207 $ user ->addPrivilege ('SUB_PR_3 ' );
249208 $ user ->addPrivilege ('SUB_PR_2 ' );
@@ -252,10 +211,11 @@ public function testCreatePrivilegesStr06() {
252211 $ this ->assertEquals ('SUB_PR_3-1;SUB_PR_2-1;SUB_OF_PR_2-1 ' ,$ privilegesStr );
253212 }
254213 /**
255- * @depends testCreatePrivilegesStr03
256- * @param type $str
214+ * @test
257215 */
258- public function testResolvePrivilegesStr00 ($ str ) {
216+ public function testResolvePrivilegesStr00 () {
217+ $ this ->setupComplexGroups ();
218+ $ str = 'G-ADMINS;G-EMPLOYER;REVERSE_INVOICE-1;RESET_PASSWORD_SELF-1 ' ;
259219 $ user = new User ();
260220 Access::resolvePrivileges ($ str , $ user );
261221 $ this ->assertTrue ($ user ->hasPrivilege ('REVERSE_INVOICE ' ));
@@ -269,23 +229,8 @@ public function testResolvePrivilegesStr00($str) {
269229 * @test
270230 */
271231 public function testResolvePrivilegesStr01 () {
272- Access::newGroup ('TOP_GROUP ' );
273- Access::newGroup ('SUB_GROUP ' , 'TOP_GROUP ' );
274- Access::newGroup ('SUB_OF_SUB ' , 'SUB_GROUP ' );
275- Access::newPrivileges ('TOP_GROUP ' , [
276- 'TOP_PR_1 ' ,'TOP_PR_2 ' ,'TOP_PR_3 '
277- ]);
278- Access::newPrivileges ('SUB_GROUP ' , [
279- 'SUB_PR_1 ' ,'SUB_PR_2 ' ,'SUB_PR_3 '
280- ]);
281- Access::newPrivileges ('SUB_OF_SUB ' , [
282- 'SUB_OF_PR_1 ' ,'SUB_OF_PR_2 ' ,'SUB_OF_PR_3 '
283- ]);
284- Access::newGroup ('SUB_OF_SUB2 ' , 'SUB_GROUP ' );
232+ $ this ->setupSubGroups ();
285233 $ user = new User ();
286- Access::newPrivileges ('SUB_OF_SUB ' , [
287- 'SUB2_OF_PR_1 ' ,'SUB2_OF_PR_2 ' ,'SUB2_OF_PR_3 '
288- ]);
289234 Access::resolvePrivileges ('G-SUB_OF_SUB;SUB_PR_1-0;SUB_PR_3-1;TOP_PR_2-1;SUB2_OF_PR_1-1 ' , $ user );
290235
291236 $ this ->assertTrue ($ user ->hasPrivilege ('SUB_OF_PR_1 ' ));
@@ -303,7 +248,40 @@ public function testResolvePrivilegesStr01() {
303248 $ this ->assertTrue ($ user ->hasPrivilege ('SUB2_OF_PR_1 ' ));
304249 $ this ->assertFalse ($ user ->hasPrivilege ('TOP_PR_3 ' ));
305250 $ this ->assertFalse ($ user ->inGroup ('TOP_GROUP ' ));
251+ }
306252
307- return $ user ;
253+ private function setupComplexGroups (): void {
254+ Access::newGroup ('ADMINS ' );
255+ Access::newPrivileges ('ADMINS ' , ['MODIFY_SYS_SETTINGS ' ]);
256+ Access::newGroup ('SYS_USER ' );
257+ Access::newPrivilege ('SYS_USER ' , 'RESET_PASSWORD_SELF ' );
258+ Access::newGroup ('USERS_MANAGERS ' , 'ADMINS ' );
259+ Access::newPrivileges ('USERS_MANAGERS ' , [
260+ 'RESET_ANY_USER_PASSWORD ' ,'CREATE_USER_ACCESS ' ,'UPDATE_USER_ACCESS ' ,'BLOCK_USER '
261+ ]);
262+ Access::newGroup ('FINANCE ' ,'SYS_USER ' );
263+ Access::newPrivileges ('FINANCE ' , [
264+ 'CREATE_INVOICE ' ,'REVERSE_INVOICE ' ,'VIEW_SALES_REPORT ' ,
265+ 'RESET_PASSWORD_SELF ' ,'DEPOSIT ' ,'WITHDRAW ' ,
266+ ]);
267+ Access::newGroup ('HR ' ,'SYS_USER ' );
268+ Access::newPrivileges ('HR ' , [
269+ 'VIEW_ATTENDANCE ' ,'UPDATE_SALARY ' ,'UPDATE_POSITION ' ,
270+ 'RESET_PASSWORD_SELF ' ,'DO_EMP_EVAL ' ,'WITHDRAW '
271+ ]);
272+ Access::newGroup ('EMPLOYER ' , 'HR ' );
273+ Access::newPrivileges ('EMPLOYER ' , ['DO_INTERVIEW ' ,'EVALUATE_APLICANT ' ]);
274+ Access::newGroup ('SYS_USER ' ,'HR ' );
275+ }
276+
277+ private function setupSubGroups (): void {
278+ Access::newGroup ('TOP_GROUP ' );
279+ Access::newGroup ('SUB_GROUP ' , 'TOP_GROUP ' );
280+ Access::newGroup ('SUB_OF_SUB ' , 'SUB_GROUP ' );
281+ Access::newPrivileges ('TOP_GROUP ' , ['TOP_PR_1 ' ,'TOP_PR_2 ' ,'TOP_PR_3 ' ]);
282+ Access::newPrivileges ('SUB_GROUP ' , ['SUB_PR_1 ' ,'SUB_PR_2 ' ,'SUB_PR_3 ' ]);
283+ Access::newPrivileges ('SUB_OF_SUB ' , ['SUB_OF_PR_1 ' ,'SUB_OF_PR_2 ' ,'SUB_OF_PR_3 ' ]);
284+ Access::newGroup ('SUB_OF_SUB2 ' , 'SUB_GROUP ' );
285+ Access::newPrivileges ('SUB_OF_SUB ' , ['SUB2_OF_PR_1 ' ,'SUB2_OF_PR_2 ' ,'SUB2_OF_PR_3 ' ]);
308286 }
309287}
0 commit comments