Merge branch 'master' of ssh://openemr.git.sourceforge.net/gitroot/openemr/openemr
[openemr.git] / gacl / test_suite / unit_tests.php
blob16b115b07c7c03df609b9924b838d77521c2c600
1 <?php
2 class phpgacl_api_test extends gacl_test_case {
4 /** VERSION **/
6 function get_version() {
7 $result = $this->gacl_api->get_version();
8 //$expected = '/^[0-9]{1,2}.[0-9]{1,2}.[0-9]{1,2}$/i';
9 $expected = '/^[0-9]{1,2}.[0-9]{1,2}.[0-9]{1,2}[a-zA-Z]{0,1}[0-9]{0,1}$/i';
11 $this->assertRegexp($expected, $result, 'Version incorrect.');
13 function get_schema_version() {
14 $result = $this->gacl_api->get_schema_version();
15 $expected = '/^[0-9]{1,2}.[0-9]{1,2}$/i';
17 $this->assertRegexp($expected, $result, 'Schema Version incorrect.');
20 /** GENERAL **/
22 function count_all() {
23 //Create array
24 $arr = array(
25 'Level1a' => array(
26 'Level2a' => array(
27 'Level3a' => 1,
28 'Level3b' => 2
30 'Level2b' => 3,
32 'Level1b' => 4,
33 'Level1c' => array(
34 'Level2c' => array(
35 'Level3c' => 5,
36 'Level3d' => 6
38 'Level2d' => 7,
40 'Level1d' => 8
43 //Keep in mind count_all only counts actual values. So array()'s don't count as +1
44 $result = $this->gacl_api->count_all($arr);
46 $this->assertEquals(8, $result, 'Incorrect array count, Should be 8.');
49 /** ACO SECTION **/
51 function get_object_section_section_id_aco() {
52 $result = $this->gacl_api->get_object_section_section_id('unit_test', 'unit_test', 'ACO');
53 $message = 'get_object_section_section_id failed';
55 $this->assert($result, $message);
57 return $result;
60 function add_object_section_aco() {
61 $result = $this->gacl_api->add_object_section('unit_test', 'unit_test', 999, 0, 'ACO');
62 $message = 'add_object_section failed';
64 $this->assert($result, $message);
67 function del_object_section_aco() {
68 $result = $this->gacl_api->del_object_section($this->get_object_section_section_id_aco(), 'ACO');
69 $message = 'del_object_section failed';
70 $this->assert($result, $message);
73 /** ACO **/
75 function get_object_id_aco() {
76 $result = $this->gacl_api->get_object_id('unit_test','enable_tests', 'ACO');
77 $message = 'get_object_id failed';
78 $this->assert($result, $message);
80 return $result;
83 function add_object_aco() {
84 $result = $this->gacl_api->add_object('unit_test', 'Enable - Tests', 'enable_tests', 999, 0, 'ACO');
85 $message = 'add_object failed';
86 $this->assert($result, $message);
89 function del_object_aco() {
90 $result = $this->gacl_api->del_object($this->get_object_id_aco(), 'ACO');
91 $message = 'del_object failed';
92 $this->assert($result, $message);
95 /** ARO SECTION **/
97 function get_object_section_section_id_aro() {
98 $result = $this->gacl_api->get_object_section_section_id('unit_test', 'unit_test', 'ARO');
99 $this->_aco_section_id = $result;
100 $message = 'get_object_section_section_id failed';
101 $this->assert($result >= 0, $message);
103 return $result;
106 function add_object_section_aro() {
107 $result = $this->gacl_api->add_object_section('unit_test', 'unit_test', 999, 0, 'ARO');
108 $message = 'add_object_section failed';
109 $this->assert($result, $message);
112 function edit_object_section_aro() {
113 $object_id = $this->get_object_section_section_id_aro();
115 $rename_result = $this->gacl_api->edit_object_section($object_id, 'unit_test_tmp', 'unit_test_tmp', 999, 0, 'ARO');
116 $rename2_result = $this->gacl_api->edit_object_section($object_id, 'unit_test', 'unit_test', 999, 0, 'ARO');
118 if ($rename_result === TRUE AND $rename2_result === TRUE) {
119 $result = TRUE;
120 } else {
121 $result = FALSE;
124 $message = 'edit_object_section failed';
125 $this->assert($result, $message);
128 function del_object_section_aro() {
129 $result = $this->gacl_api->del_object_section($this->get_object_section_section_id_aro(), 'ARO');
130 $message = 'del_object_section failed';
131 $this->assert($result, $message);
134 /** ARO **/
136 function get_object_id_aro() {
137 $result = $this->gacl_api->get_object_id('unit_test','john_doe', 'ARO');
138 $message = 'get_object_id failed';
139 $this->assert($result, $message);
141 return $result;
144 function get_object2_id_aro() {
145 $result = $this->gacl_api->get_object_id('unit_test','jane_doe', 'ARO');
146 $message = 'get_object2_id failed';
147 $this->assert($result, $message);
149 return $result;
152 function add_object_aro() {
153 $result = $this->gacl_api->add_object('unit_test', 'John Doe', 'john_doe', 999, 0, 'ARO');
154 $message = 'add_object failed';
155 $this->assert($result, $message);
157 function del_object_aro() {
158 $result = $this->gacl_api->del_object($this->get_object_id_aro(), 'ARO');
159 $message = 'del_object failed';
160 $this->assert($result, $message);
163 function add_object2_aro() {
164 $result = $this->gacl_api->add_object('unit_test', 'Jane Doe', 'jane_doe', 998, 0, 'ARO');
165 $message = 'add_object2 failed';
166 $this->assert($result, $message);
168 function del_object2_aro() {
169 $result = $this->gacl_api->del_object($this->get_object2_id_aro(), 'ARO');
170 $message = 'del_object2 failed';
171 $this->assert($result, $message);
174 /** AXO SECTION **/
176 function get_object_section_section_id_axo() {
177 $result = $this->gacl_api->get_object_section_section_id('unit_test', 'unit_test', 'AXO');
178 $message = 'get_object_section_section_id failed';
179 $this->assert($result, $message);
181 return $result;
184 function add_object_section_axo() {
185 $result = $this->gacl_api->add_object_section('unit_test', 'unit_test', 999, 0, 'AXO');
186 $this->_aco_section_id = $result;
187 $message = 'add_object_section failed';
188 $this->assert($result, $message);
191 function del_object_section_axo() {
192 $result = $this->gacl_api->del_object_section($this->get_object_section_section_id_axo(), 'AXO');
193 $message = 'del_object_section failed';
194 $this->assert($result, $message);
197 /** AXO **/
199 function get_object_id_axo() {
200 $result = $this->gacl_api->get_object_id('unit_test','object_1', 'AXO');
201 $message = 'get_object_id failed';
202 $this->assert($result, $message);
204 return $result;
207 function add_object_axo() {
208 $result = $this->gacl_api->add_object('unit_test', 'Object 1', 'object_1', 999, 0, 'AXO');
209 $message = 'add_object failed';
210 $this->assert($result, $message);
213 function del_object_axo() {
214 $result = $this->gacl_api->del_object($this->get_object_id_axo(), 'AXO');
215 $message = 'del_object failed';
216 $this->assert($result, $message);
219 /** ARO GROUP **/
221 function get_group_id_parent_aro() {
222 $result = $this->gacl_api->get_group_id(NULL, 'ARO Group 1', 'ARO');
223 $message = 'get_group_id_parent_aro failed';
224 $this->assert($result, $message);
226 return $result;
229 function get_group_id_child_aro() {
230 $result = $this->gacl_api->get_group_id(NULL, 'ARO Group 2', 'ARO');
231 $message = 'get_group_id_child_aro failed';
232 $this->assert($result, $message);
234 return $result;
237 function get_group_parent_id_aro() {
238 $parent_id = $this->gacl_api->get_group_parent_id($this->get_group_id_child_aro(), 'ARO');
239 //Make sure it matches with the actual parent.
240 if ($parent_id === $this->get_group_id_parent_aro() ) {
241 $result = TRUE;
242 } else {
243 $result = FALSE;
245 $message = 'get_group_parent_id_aro failed';
246 $this->assert($result, $message);
248 return $result;
251 function get_group_data_aro() {
252 list($id, $parent_id, $value, $name, $lft, $rgt) = $this->gacl_api->get_group_data($this->get_group_id_parent_aro(), 'ARO');
253 //Check all values in the resulting array.
254 if ( $id > 0 AND $parent_id >= 0 AND strlen($name) > 0 AND $lft >= 1 AND $rgt > 1) {
255 $result = TRUE;
256 } else {
257 $result = FALSE;
259 $message = 'get_group_data_aro failed';
260 $this->assert($result, $message);
262 return $result;
265 function get_parent_group_objects_aro() {
266 $group_objects = $this->gacl_api->get_group_objects($this->get_group_id_parent_aro(), 'ARO');
267 if (count($group_objects, COUNT_RECURSIVE) == 2 AND $group_objects['unit_test'][0] == 'john_doe') {
268 $result = TRUE;
269 } else {
270 $result = FALSE;
272 $message = 'get_parent_group_objects_aro failed';
273 $this->assert($result, $message);
275 return $result;
278 function get_parent_group_objects_recurse_aro() {
279 $group_objects = $this->gacl_api->get_group_objects($this->get_group_id_parent_aro(), 'ARO', 'RECURSE');
281 switch (TRUE) {
282 case count($group_objects) != 1:
283 case !isset($group_objects['unit_test']):
284 case count($group_objects['unit_test']) != 2:
285 case !in_array('john_doe', $group_objects['unit_test']):
286 case !in_array('jane_doe', $group_objects['unit_test']):
287 $result = FALSE;
288 break;
289 default:
290 $result = TRUE;
293 $message = 'get_parent_group_objects_recurse_aro failed';
294 $this->assert($result, $message);
296 return $result;
299 function add_group_parent_aro() {
300 $result = $this->gacl_api->add_group('group_1', 'ARO Group 1', 0, 'ARO');
301 $message = 'add_group_parent_aro failed';
302 $this->assert($result, $message);
305 function edit_group_parent_aro() {
306 $group_id = $this->get_group_id_parent_aro();
308 $first_rename = $this->gacl_api->edit_group($group_id, 'group_1_tmp', 'ARO Group 1 - tmp', 0, 'ARO');
309 $second_rename = $this->gacl_api->edit_group($group_id,'group_1', 'ARO Group 1', 0, 'ARO');
310 $reparent_to_self = $this->gacl_api->edit_group($group_id,'group_1', 'ARO Group 1', $group_id, 'ARO');
312 if ($first_rename === TRUE AND $second_rename === TRUE AND $reparent_to_self === FALSE) {
313 $result = TRUE;
314 } else {
315 $result = FALSE;
317 $message = 'edit_group_parent_aro failed';
318 $this->assert($result, $message);
321 function del_group_parent_reparent_aro() {
322 $this->add_group_parent_aro();
323 $this->add_group_child_aro();
324 $this->add_parent_group_object_aro();
325 $this->add_child_group_object_aro();
327 $result = $this->gacl_api->del_group($this->get_group_id_parent_aro(), TRUE, 'ARO');
329 $this->del_child_group_object_aro();
330 $this->del_group_child_aro();
332 $message = 'del_group_parent_no_reparent_aro failed';
333 $this->assert($result, $message);
336 function del_group_parent_no_reparent_aro() {
337 $this->add_group_parent_aro();
338 $this->add_group_child_aro();
339 $this->add_parent_group_object_aro();
340 $this->add_child_group_object_aro();
342 $result = $this->gacl_api->del_group($this->get_group_id_parent_aro(), FALSE, 'ARO');
344 $message = 'del_group_parent_reparent_aro failed';
345 $this->assert($result, $message);
349 function del_group_parent_aro() {
350 $result = $this->gacl_api->del_group($this->get_group_id_parent_aro(), TRUE, 'ARO');
351 $message = 'del_group_parent_aro failed';
352 $this->assert($result, $message);
355 function add_group_child_aro() {
356 $result = $this->gacl_api->add_group('group_2', 'ARO Group 2', $this->get_group_id_parent_aro(), 'ARO');
357 $message = 'add_group_child failed';
358 $this->assert($result, $message);
361 function del_group_child_aro() {
362 $result = $this->gacl_api->del_group($this->get_group_id_child_aro(), TRUE, 'ARO');
363 $message = 'del_group failed';
364 $this->assert($result, $message);
367 function add_parent_group_object_aro() {
368 $result = $this->gacl_api->add_group_object($this->get_group_id_parent_aro(), 'unit_test', 'john_doe', 'ARO');
369 $message = 'add_parent_group_object failed';
370 $this->assert($result, $message);
372 function del_parent_group_object_aro() {
373 $result = $this->gacl_api->del_group_object($this->get_group_id_parent_aro(), 'unit_test', 'john_doe', 'ARO');
374 $message = 'del_group_object failed';
375 $this->assert($result, $message);
378 function add_child_group_object_aro() {
379 $result = $this->gacl_api->add_group_object($this->get_group_id_child_aro(), 'unit_test', 'jane_doe', 'ARO');
380 $message = 'add_child_group_object failed';
381 $this->assert($result, $message);
383 function del_child_group_object_aro() {
384 $result = $this->gacl_api->del_group_object($this->get_group_id_child_aro(), 'unit_test', 'jane_doe', 'ARO');
385 $message = 'del_child_group_object failed';
386 $this->assert($result, $message);
389 /** AXO GROUP **/
391 function get_group_id_parent_axo() {
392 $result = $this->gacl_api->get_group_id(NULL, 'AXO Group 1', 'AXO');
393 $message = 'get_group_id_parent_aro failed';
394 $this->assert($result, $message);
396 return $result;
399 function get_group_id_child_axo() {
400 $result = $this->gacl_api->get_group_id(NULL, 'AXO Group 2', 'AXO');
401 $message = 'get_group_id_child_axo failed';
402 $this->assert($result, $message);
404 return $result;
407 function get_group_parent_id_axo() {
408 $parent_id = $this->gacl_api->get_group_parent_id($this->get_group_id_child_axo(), 'AXO');
409 //Make sure it matches with the actual parent.
410 if ($parent_id === $this->get_group_id_parent_axo() ) {
411 $result = TRUE;
412 } else {
413 $result = FALSE;
415 $message = 'get_group_parent_id_aro failed';
416 $this->assert($result, $message);
418 return $result;
421 function get_group_data_axo() {
422 list($id, $parent_id, $value, $name, $lft, $rgt) = $this->gacl_api->get_group_data($this->get_group_id_parent_axo(), 'AXO');
423 //Check all values in the resulting array.
424 if ( $id > 0 AND $parent_id >= 0 AND strlen($name) > 0 AND $lft >= 1 AND $rgt > 1) {
425 $result = TRUE;
426 } else {
427 $result = FALSE;
429 $message = 'get_group_data_axo failed';
430 $this->assert($result, $message);
432 return $result;
435 function add_group_parent_axo() {
436 $result = $this->gacl_api->add_group('group_1', 'AXO Group 1', 0, 'AXO');
437 $message = 'add_group failed';
438 $this->assert($result, $message);
441 function del_group_parent_axo() {
442 $result = $this->gacl_api->del_group($this->get_group_id_parent_axo(), TRUE, 'AXO');
443 $message = 'del_group failed';
444 $this->assert($result, $message);
447 function add_group_child_axo() {
448 $result = $this->gacl_api->add_group('group_2', 'AXO Group 2', $this->get_group_id_parent_axo(), 'AXO');
449 $message = 'add_group failed';
450 $this->assert($result, $message);
453 function del_group_child_axo() {
454 $result = $this->gacl_api->del_group($this->get_group_id_child_axo(), TRUE, 'AXO');
455 $message = 'del_group failed';
456 $this->assert($result, $message);
459 function add_group_object_axo() {
460 $result = $this->gacl_api->add_group_object($this->get_group_id_parent_axo(), 'unit_test', 'object_1', 'AXO');
461 $message = 'add_group_object failed';
462 $this->assert($result, $message);
465 function del_group_object_axo() {
466 $result = $this->gacl_api->del_group_object($this->get_group_id_parent_axo(), 'unit_test', 'object_1', 'AXO');
467 $message = 'del_group_object failed';
468 $this->assert($result, $message);
472 // initialise test suite
473 $suite = new gacl_test_suite;
475 //This comes in handy.
476 //$suite->gacl_api->db->debug=TRUE;
478 // general
479 $suite->addTest(new phpgacl_api_test('get_version'));
480 $suite->addTest(new phpgacl_api_test('get_schema_version'));
482 $suite->addTest(new phpgacl_api_test('count_all'));
484 // build structure
485 $suite->addTest(new phpgacl_api_test('add_object_section_aco'));
486 $suite->addTest(new phpgacl_api_test('get_object_section_section_id_aco'));
487 $suite->addTest(new phpgacl_api_test('add_object_aco'));
488 $suite->addTest(new phpgacl_api_test('get_object_id_aco'));
490 $suite->addTest(new phpgacl_api_test('add_object_section_aro'));
491 $suite->addTest(new phpgacl_api_test('get_object_section_section_id_aco'));
492 $suite->addTest(new phpgacl_api_test('add_object_aro'));
493 //Test the below with ACLs as well... I haven't gotten around to that just yet.
494 $suite->addTest(new phpgacl_api_test('edit_object_section_aro'));
495 $suite->addTest(new phpgacl_api_test('get_object_id_aro'));
496 $suite->addTest(new phpgacl_api_test('add_object2_aro'));
497 $suite->addTest(new phpgacl_api_test('get_object2_id_aro'));
499 $suite->addTest(new phpgacl_api_test('add_object_section_axo'));
500 $suite->addTest(new phpgacl_api_test('get_object_section_section_id_axo'));
501 $suite->addTest(new phpgacl_api_test('add_object_axo'));
502 $suite->addTest(new phpgacl_api_test('get_object_id_axo'));
504 $suite->addTest(new phpgacl_api_test('add_group_parent_aro'));
505 $suite->addTest(new phpgacl_api_test('edit_group_parent_aro'));
506 $suite->addTest(new phpgacl_api_test('get_group_id_parent_aro'));
507 $suite->addTest(new phpgacl_api_test('get_group_data_aro'));
508 $suite->addTest(new phpgacl_api_test('add_group_child_aro'));
509 $suite->addTest(new phpgacl_api_test('get_group_id_child_aro'));
510 $suite->addTest(new phpgacl_api_test('get_group_parent_id_aro'));
512 $suite->addTest(new phpgacl_api_test('add_parent_group_object_aro'));
513 //Try adding twice. Both times should return true.
514 $suite->addTest(new phpgacl_api_test('add_parent_group_object_aro'));
515 $suite->addTest(new phpgacl_api_test('add_child_group_object_aro'));
517 $suite->addTest(new phpgacl_api_test('get_parent_group_objects_aro'));
518 $suite->addTest(new phpgacl_api_test('get_parent_group_objects_recurse_aro'));
521 $suite->addTest(new phpgacl_api_test('add_group_parent_axo'));
522 $suite->addTest(new phpgacl_api_test('get_group_id_parent_axo'));
523 $suite->addTest(new phpgacl_api_test('get_group_data_axo'));
524 $suite->addTest(new phpgacl_api_test('add_group_child_axo'));
525 $suite->addTest(new phpgacl_api_test('get_group_id_child_axo'));
526 $suite->addTest(new phpgacl_api_test('add_group_object_axo'));
527 $suite->addTest(new phpgacl_api_test('get_group_parent_id_axo'));
530 // clean up...
531 $suite->addTest(new phpgacl_api_test('del_parent_group_object_aro'));
532 $suite->addTest(new phpgacl_api_test('del_child_group_object_aro'));
533 $suite->addTest(new phpgacl_api_test('del_group_child_aro'));
534 $suite->addTest(new phpgacl_api_test('del_group_parent_aro'));
536 $suite->addTest(new phpgacl_api_test('del_group_object_axo'));
537 $suite->addTest(new phpgacl_api_test('del_group_child_axo'));
538 $suite->addTest(new phpgacl_api_test('del_group_parent_axo'));
540 $suite->addTest(new phpgacl_api_test('del_object_aco'));
541 $suite->addTest(new phpgacl_api_test('del_object_section_aco'));
543 //Test group reparenting - Order of this test is important.
544 $suite->addTest(new phpgacl_api_test('del_group_parent_no_reparent_aro'));
545 $suite->addTest(new phpgacl_api_test('del_group_parent_reparent_aro'));
547 $suite->addTest(new phpgacl_api_test('del_object_aro'));
548 $suite->addTest(new phpgacl_api_test('del_object2_aro'));
549 $suite->addTest(new phpgacl_api_test('del_object_section_aro'));
551 $suite->addTest(new phpgacl_api_test('del_object_axo'));
552 $suite->addTest(new phpgacl_api_test('del_object_section_axo'));
554 // run tests
555 echo '<p>Running API tests... ';
556 $suite->run($result);
557 echo '<b>Done</b></p>';
559 unset ($suite);
561 // done.