DbAclTest.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. <?php
  2. /**
  3. * DbAclTest file
  4. *
  5. * PHP 5
  6. *
  7. * CakePHP(tm) Tests <http://book.cakephp.org/view/1196/Testing>
  8. * Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
  9. *
  10. * Licensed under The MIT License
  11. * Redistributions of files must retain the above copyright notice
  12. *
  13. * @copyright Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
  14. * @link http://book.cakephp.org/view/1196/Testing CakePHP(tm) Tests
  15. * @package Cake.Test.Case.Model
  16. * @since CakePHP(tm) v 1.2.0.4206
  17. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  18. */
  19. App::uses('AclComponent', 'Controller/Component');
  20. App::uses('AclNode', 'Model');
  21. class_exists('AclComponent');
  22. /**
  23. * DB ACL wrapper test class
  24. *
  25. * @package Cake.Test.Case.Model
  26. */
  27. class DbAclNodeTestBase extends AclNode {
  28. /**
  29. * useDbConfig property
  30. *
  31. * @var string 'test'
  32. */
  33. public $useDbConfig = 'test';
  34. /**
  35. * cacheSources property
  36. *
  37. * @var bool false
  38. */
  39. public $cacheSources = false;
  40. }
  41. /**
  42. * Aro Test Wrapper
  43. *
  44. * @package Cake.Test.Case.Model
  45. */
  46. class DbAroTest extends DbAclNodeTestBase {
  47. /**
  48. * name property
  49. *
  50. * @var string 'DbAroTest'
  51. */
  52. public $name = 'DbAroTest';
  53. /**
  54. * useTable property
  55. *
  56. * @var string 'aros'
  57. */
  58. public $useTable = 'aros';
  59. /**
  60. * hasAndBelongsToMany property
  61. *
  62. * @var array
  63. */
  64. public $hasAndBelongsToMany = array('DbAcoTest' => array('with' => 'DbPermissionTest'));
  65. }
  66. /**
  67. * Aco Test Wrapper
  68. *
  69. * @package Cake.Test.Case.Model
  70. */
  71. class DbAcoTest extends DbAclNodeTestBase {
  72. /**
  73. * name property
  74. *
  75. * @var string 'DbAcoTest'
  76. */
  77. public $name = 'DbAcoTest';
  78. /**
  79. * useTable property
  80. *
  81. * @var string 'acos'
  82. */
  83. public $useTable = 'acos';
  84. /**
  85. * hasAndBelongsToMany property
  86. *
  87. * @var array
  88. */
  89. public $hasAndBelongsToMany = array('DbAroTest' => array('with' => 'DbPermissionTest'));
  90. }
  91. /**
  92. * Permission Test Wrapper
  93. *
  94. * @package Cake.Test.Case.Model
  95. */
  96. class DbPermissionTest extends CakeTestModel {
  97. /**
  98. * name property
  99. *
  100. * @var string 'DbPermissionTest'
  101. */
  102. public $name = 'DbPermissionTest';
  103. /**
  104. * useTable property
  105. *
  106. * @var string 'aros_acos'
  107. */
  108. public $useTable = 'aros_acos';
  109. /**
  110. * cacheQueries property
  111. *
  112. * @var bool false
  113. */
  114. public $cacheQueries = false;
  115. /**
  116. * belongsTo property
  117. *
  118. * @var array
  119. */
  120. public $belongsTo = array('DbAroTest' => array('foreignKey' => 'aro_id'), 'DbAcoTest' => array('foreignKey' => 'aco_id'));
  121. }
  122. /**
  123. * DboActionTest class
  124. *
  125. * @package Cake.Test.Case.Model
  126. */
  127. class DbAcoActionTest extends CakeTestModel {
  128. /**
  129. * name property
  130. *
  131. * @var string 'DbAcoActionTest'
  132. */
  133. public $name = 'DbAcoActionTest';
  134. /**
  135. * useTable property
  136. *
  137. * @var string 'aco_actions'
  138. */
  139. public $useTable = 'aco_actions';
  140. /**
  141. * belongsTo property
  142. *
  143. * @var array
  144. */
  145. public $belongsTo = array('DbAcoTest' => array('foreignKey' => 'aco_id'));
  146. }
  147. /**
  148. * DbAroUserTest class
  149. *
  150. * @package Cake.Test.Case.Model
  151. */
  152. class DbAroUserTest extends CakeTestModel {
  153. /**
  154. * name property
  155. *
  156. * @var string 'AuthUser'
  157. */
  158. public $name = 'AuthUser';
  159. /**
  160. * useTable property
  161. *
  162. * @var string 'auth_users'
  163. */
  164. public $useTable = 'auth_users';
  165. /**
  166. * bindNode method
  167. *
  168. * @param mixed $ref
  169. * @return void
  170. */
  171. public function bindNode($ref = null) {
  172. if (Configure::read('DbAclbindMode') == 'string') {
  173. return 'ROOT/admins/Gandalf';
  174. } elseif (Configure::read('DbAclbindMode') == 'array') {
  175. return array('DbAroTest' => array('DbAroTest.model' => 'AuthUser', 'DbAroTest.foreign_key' => 2));
  176. }
  177. }
  178. }
  179. /**
  180. * TestDbAcl class
  181. *
  182. * @package Cake.Test.Case.Model
  183. */
  184. class TestDbAcl extends DbAcl {
  185. /**
  186. * construct method
  187. *
  188. * @return void
  189. */
  190. function __construct() {
  191. $this->Aro = new DbAroTest();
  192. $this->Aro->Permission = new DbPermissionTest();
  193. $this->Aco = new DbAcoTest();
  194. $this->Aro->Permission = new DbPermissionTest();
  195. }
  196. }
  197. /**
  198. * AclNodeTest class
  199. *
  200. * @package Cake.Test.Case.Model
  201. */
  202. class AclNodeTest extends CakeTestCase {
  203. /**
  204. * fixtures property
  205. *
  206. * @var array
  207. */
  208. public $fixtures = array('core.aro', 'core.aco', 'core.aros_aco', 'core.aco_action', 'core.auth_user');
  209. /**
  210. * setUp method
  211. *
  212. * @return void
  213. */
  214. public function setUp() {
  215. Configure::write('Acl.classname', 'TestDbAcl');
  216. Configure::write('Acl.database', 'test');
  217. }
  218. /**
  219. * testNode method
  220. *
  221. * @return void
  222. */
  223. public function testNode() {
  224. $Aco = new DbAcoTest();
  225. $result = Set::extract($Aco->node('Controller1'), '{n}.DbAcoTest.id');
  226. $expected = array(2, 1);
  227. $this->assertEqual($expected, $result);
  228. $result = Set::extract($Aco->node('Controller1/action1'), '{n}.DbAcoTest.id');
  229. $expected = array(3, 2, 1);
  230. $this->assertEqual($expected, $result);
  231. $result = Set::extract($Aco->node('Controller2/action1'), '{n}.DbAcoTest.id');
  232. $expected = array(7, 6, 1);
  233. $this->assertEqual($expected, $result);
  234. $result = Set::extract($Aco->node('Controller1/action2'), '{n}.DbAcoTest.id');
  235. $expected = array(5, 2, 1);
  236. $this->assertEqual($expected, $result);
  237. $result = Set::extract($Aco->node('Controller1/action1/record1'), '{n}.DbAcoTest.id');
  238. $expected = array(4, 3, 2, 1);
  239. $this->assertEqual($expected, $result);
  240. $result = Set::extract($Aco->node('Controller2/action1/record1'), '{n}.DbAcoTest.id');
  241. $expected = array(8, 7, 6, 1);
  242. $this->assertEqual($expected, $result);
  243. $result = Set::extract($Aco->node('Controller2/action3'), '{n}.DbAcoTest.id');
  244. $this->assertNull($result);
  245. $result = Set::extract($Aco->node('Controller2/action3/record5'), '{n}.DbAcoTest.id');
  246. $this->assertNull($result);
  247. $result = $Aco->node('');
  248. $this->assertEqual($result, null);
  249. }
  250. /**
  251. * test that node() doesn't dig deeper than it should.
  252. *
  253. * @return void
  254. */
  255. public function testNodeWithDuplicatePathSegments() {
  256. $Aco = new DbAcoTest();
  257. $nodes = $Aco->node('ROOT/Users');
  258. $this->assertEqual($nodes[0]['DbAcoTest']['parent_id'], 1, 'Parent id does not point at ROOT. %s');
  259. }
  260. /**
  261. * testNodeArrayFind method
  262. *
  263. * @return void
  264. */
  265. public function testNodeArrayFind() {
  266. $Aro = new DbAroTest();
  267. Configure::write('DbAclbindMode', 'string');
  268. $result = Set::extract($Aro->node(array('DbAroUserTest' => array('id' => '1', 'foreign_key' => '1'))), '{n}.DbAroTest.id');
  269. $expected = array(3, 2, 1);
  270. $this->assertEqual($expected, $result);
  271. Configure::write('DbAclbindMode', 'array');
  272. $result = Set::extract($Aro->node(array('DbAroUserTest' => array('id' => 4, 'foreign_key' => 2))), '{n}.DbAroTest.id');
  273. $expected = array(4);
  274. $this->assertEqual($expected, $result);
  275. }
  276. /**
  277. * testNodeObjectFind method
  278. *
  279. * @return void
  280. */
  281. public function testNodeObjectFind() {
  282. $Aro = new DbAroTest();
  283. $Model = new DbAroUserTest();
  284. $Model->id = 1;
  285. $result = Set::extract($Aro->node($Model), '{n}.DbAroTest.id');
  286. $expected = array(3, 2, 1);
  287. $this->assertEqual($expected, $result);
  288. $Model->id = 2;
  289. $result = Set::extract($Aro->node($Model), '{n}.DbAroTest.id');
  290. $expected = array(4, 2, 1);
  291. $this->assertEqual($expected, $result);
  292. }
  293. /**
  294. * testNodeAliasParenting method
  295. *
  296. * @return void
  297. */
  298. public function testNodeAliasParenting() {
  299. $Aco = ClassRegistry::init('DbAcoTest');
  300. $db = $Aco->getDataSource();
  301. $db->truncate($Aco);
  302. $Aco->create(array('model' => null, 'foreign_key' => null, 'parent_id' => null, 'alias' => 'Application'));
  303. $Aco->save();
  304. $Aco->create(array('model' => null, 'foreign_key' => null, 'parent_id' => $Aco->id, 'alias' => 'Pages'));
  305. $Aco->save();
  306. $result = $Aco->find('all');
  307. $expected = array(
  308. array('DbAcoTest' => array('id' => '1', 'parent_id' => null, 'model' => null, 'foreign_key' => null, 'alias' => 'Application', 'lft' => '1', 'rght' => '4'), 'DbAroTest' => array()),
  309. array('DbAcoTest' => array('id' => '2', 'parent_id' => '1', 'model' => null, 'foreign_key' => null, 'alias' => 'Pages', 'lft' => '2', 'rght' => '3', ), 'DbAroTest' => array())
  310. );
  311. $this->assertEqual($expected, $result);
  312. }
  313. }