/gacl/test_suite/acl_tests.php

https://github.com/md-tech/openemr · PHP · 223 lines · 149 code · 52 blank · 22 comment · 1 complexity · 54c81fcec68e968b66a3055c1b531d85 MD5 · raw file

  1. <?php
  2. class acl_setup {
  3. var $gacl_api;
  4. var $aco_section = array();
  5. var $aco = array();
  6. var $aro_section = array();
  7. var $aro = array();
  8. var $aro_group = array();
  9. var $axo_section = array();
  10. var $axo = array();
  11. var $axo_group = array();
  12. var $acl = array();
  13. function acl_setup() {
  14. $this->gacl_api = &$GLOBALS['gacl_api'];
  15. }
  16. function setup() {
  17. // ACO
  18. $this->aco_section[] = $this->gacl_api->add_object_section ('Test','test_aco',0,0,'ACO');
  19. $this->aco[] = $this->gacl_api->add_object ('test_aco','Access','access',0,0,'ACO');
  20. // ARO
  21. $this->aro_section[] = $this->gacl_api->add_object_section ('Human', 'test_human',0,0,'ARO');
  22. $this->aro[] = $this->gacl_api->add_object ('test_human','Han','han',0,0,'ARO');
  23. $this->aro[] = $this->gacl_api->add_object ('test_human','Lando','lando',0,0,'ARO');
  24. $this->aro[] = $this->gacl_api->add_object ('test_human','Obi-wan','obiwan',0,0,'ARO');
  25. $this->aro[] = $this->gacl_api->add_object ('test_human','Luke','luke',0,0,'ARO');
  26. $this->aro_section[] = $this->gacl_api->add_object_section ('Android', 'test_android',0,0,'ARO');
  27. $this->aro[] = $this->gacl_api->add_object ('test_android','R2D2','r2d2',0,0,'ARO');
  28. $this->aro[] = $this->gacl_api->add_object ('test_android','C3PO','c3po',0,0,'ARO');
  29. $this->aro_section[] = $this->gacl_api->add_object_section ('Alien', 'test_alien',0,0,'ARO');
  30. $this->aro[] = $this->gacl_api->add_object ('test_alien','Chewie','chewie',0,0,'ARO');
  31. $this->aro[] = $this->gacl_api->add_object ('test_alien','Hontook','hontook',0,0,'ARO');
  32. // ARO groups
  33. $this->aro_group['root'] = $this->gacl_api->add_group('millennium_falcon_passengers', 'Millennium Falcon Passengers',0,'ARO');
  34. $this->aro_group['crew'] = $this->gacl_api->add_group('crew', 'Crew',$this->aro_group['root'],'ARO');
  35. $this->aro_group['passengers'] = $this->gacl_api->add_group('passengers','Passengers',$this->aro_group['root'],'ARO');
  36. $this->aro_group['jedi'] = $this->gacl_api->add_group('jedi', 'Jedi',$this->aro_group['passengers'],'ARO');
  37. $this->aro_group['engineers'] = $this->gacl_api->add_group('engineers', 'Engineers',$this->aro_group['root'],'ARO');
  38. // add AROs to groups
  39. $this->gacl_api->add_group_object($this->aro_group['crew'],'test_alien','chewie','ARO');
  40. $this->gacl_api->add_group_object($this->aro_group['crew'],'test_human','han','ARO');
  41. $this->gacl_api->add_group_object($this->aro_group['crew'],'test_human','lando','ARO');
  42. $this->gacl_api->add_group_object($this->aro_group['passengers'],'test_android','c3po','ARO');
  43. $this->gacl_api->add_group_object($this->aro_group['passengers'],'test_android','r2d2','ARO');
  44. $this->gacl_api->add_group_object($this->aro_group['jedi'],'test_human','luke','ARO');
  45. $this->gacl_api->add_group_object($this->aro_group['jedi'],'test_human','obiwan','ARO');
  46. $this->gacl_api->add_group_object($this->aro_group['engineers'],'test_alien','hontook','ARO');
  47. $this->gacl_api->add_group_object($this->aro_group['engineers'],'test_android','r2d2','ARO');
  48. $this->gacl_api->add_group_object($this->aro_group['engineers'],'test_human','han','ARO');
  49. // AXO
  50. $this->axo_section[] = $this->gacl_api->add_object_section ('Location', 'test_location',0,0,'AXO');
  51. $this->axo[] = $this->gacl_api->add_object ('test_location','Engines','engines',0,0,'AXO');
  52. $this->axo[] = $this->gacl_api->add_object ('test_location','Lounge','lounge',0,0,'AXO');
  53. $this->axo[] = $this->gacl_api->add_object ('test_location','Cockpit','cockpit',0,0,'AXO');
  54. $this->axo[] = $this->gacl_api->add_object ('test_location','Guns','guns',0,0,'AXO');
  55. // AXO Groups
  56. $this->axo_group['locations'] = $this->gacl_api->add_group('locations', 'Locations',0,'AXO');
  57. // add AXOs to groups
  58. $this->gacl_api->add_group_object($this->axo_group['locations'],'test_location','engines','AXO');
  59. $this->gacl_api->add_group_object($this->axo_group['locations'],'test_location','lounge','AXO');
  60. $this->gacl_api->add_group_object($this->axo_group['locations'],'test_location','cockpit','AXO');
  61. $this->gacl_api->add_group_object($this->axo_group['locations'],'test_location','guns','AXO');
  62. // create ACLs
  63. $this->acl[] = $this->gacl_api->add_acl(array('test_aco'=>array('access')),NULL,array($this->aro_group['crew']),NULL,array($this->axo_group['locations']),1,1,NULL,'Crew can go anywhere');
  64. $this->acl[] = $this->gacl_api->add_acl(array('test_aco'=>array('access')),array('test_alien'=>array('chewie')),NULL,array('test_location'=>array('engines')),NULL,0,1,NULL,'Chewie can\'t access the engines');
  65. $this->acl[] = $this->gacl_api->add_acl(array('test_aco'=>array('access')),NULL,array($this->aro_group['passengers']),array('test_location'=>array('lounge')),NULL,1,1,NULL,'Passengers are allowed in the lounge');
  66. $this->acl[] = $this->gacl_api->add_acl(array('test_aco'=>array('access')),NULL,array($this->aro_group['jedi']),array('test_location'=>array('cockpit')),NULL,1,1,NULL,'Jedi are allowed in the cockpit');
  67. $this->acl[] = $this->gacl_api->add_acl(array('test_aco'=>array('access')),array('test_human'=>array('luke')),NULL,array('test_location'=>array('guns')),NULL,1,1,NULL,'Luke can access the guns');
  68. $this->acl[] = $this->gacl_api->add_acl(array('test_aco'=>array('access')),NULL,array($this->aro_group['engineers']),array('test_location'=>array('engines','guns')),NULL,1,1,NULL,'Engineers can access the engines and guns');
  69. }
  70. function teardown() {
  71. // delete ACLs
  72. foreach ($this->acl as $id) {
  73. $this->gacl_api->del_acl($id);
  74. }
  75. // delete AXO groups
  76. foreach (array_reverse($this->axo_group) as $id) {
  77. $this->gacl_api->del_group($id,TRUE,'AXO');
  78. }
  79. // delete AXOs
  80. foreach ($this->axo as $id) {
  81. $this->gacl_api->del_object($id,'AXO');
  82. }
  83. // delete AXO sections
  84. foreach ($this->axo_section as $id) {
  85. $this->gacl_api->del_object_section($id,'AXO');
  86. }
  87. // delete ARO groups
  88. foreach (array_reverse($this->aro_group) as $id) {
  89. $this->gacl_api->del_group($id,TRUE,'ARO');
  90. }
  91. // delete AROs
  92. foreach ($this->aro as $id) {
  93. $this->gacl_api->del_object($id,'ARO');
  94. }
  95. // delete ARO sections
  96. foreach ($this->aro_section as $id) {
  97. $this->gacl_api->del_object_section($id,'ARO');
  98. }
  99. // delete ACOs
  100. foreach ($this->aco as $id) {
  101. $this->gacl_api->del_object($id,'ACO');
  102. }
  103. // delete ACO sections
  104. foreach ($this->aco_section as $id) {
  105. $this->gacl_api->del_object_section($id,'ACO');
  106. }
  107. }
  108. }
  109. class acl_test extends gacl_test_case {
  110. var $acl_setup;
  111. function acl_test($name) {
  112. $this->gacl_test_case($name);
  113. $this->acl_setup = &$GLOBALS['acl_setup'];
  114. }
  115. function test_check_luke_lounge() {
  116. $result = $this->gacl_api->acl_check('test_aco','access','test_human','luke','test_location','lounge');
  117. $message = 'Luke should have access to the Lounge';
  118. $this->assertEquals(TRUE, $result, $message);
  119. }
  120. function test_check_luke_engines() {
  121. $result = $this->gacl_api->acl_check('test_aco','access','test_human','luke','test_location','engines');
  122. $message = 'Luke shouldn\'t have access to the Engines';
  123. $this->assertEquals(FALSE, $result, $message);
  124. }
  125. function test_check_chewie_guns() {
  126. $result = $this->gacl_api->acl_check('test_aco','access','test_alien','chewie','test_location','guns');
  127. $message = 'Chewie should have access to the Guns';
  128. $this->assertEquals(TRUE, $result, $message);
  129. }
  130. function test_check_chewie_engines() {
  131. $result = $this->gacl_api->acl_check('test_aco','access','test_alien','chewie','test_location','engines');
  132. $message = 'Chewie shouldn\'t have access to the Engines';
  133. $this->assertEquals(FALSE, $result, $message);
  134. }
  135. function test_query_luke_lounge() {
  136. $result = $this->gacl_api->acl_query('test_aco','access','test_human','luke','test_location','lounge');
  137. $expected = array(
  138. 'acl_id' => $this->acl_setup->acl[2],
  139. 'return_value' => '',
  140. 'allow' => TRUE
  141. );
  142. $message = 'Luke should have access to the Lounge';
  143. $this->assertEquals($expected, $result, $message);
  144. }
  145. }
  146. // check no previous tests failed
  147. if ( $result->failureCount() > 0 )
  148. {
  149. echo '<p>Previous test failed, not running ACL check tests.</p>';
  150. return;
  151. }
  152. echo '<p>Running ACL tests...<br />';
  153. // set up test environment
  154. $acl_setup = new acl_setup;
  155. echo '&nbsp;&nbsp;Setting Up... ';
  156. $acl_setup->setup();
  157. echo 'Done<br/>';
  158. // run tests & destroy suite
  159. $suite = new gacl_test_suite('acl_test');
  160. echo '&nbsp;&nbsp;Running Tests... ';
  161. $suite->run($result);
  162. echo 'Done<br />';
  163. unset ($suite);
  164. // tear down test environment;
  165. echo '&nbsp;&nbsp;Cleaning Up... ';
  166. $acl_setup->teardown();
  167. unset ($acl_setup);
  168. echo 'Done<br />';
  169. echo '<b>Done</b></p>';
  170. // done.
  171. ?>