AclNodeTest.php 8.5 KB

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