AclNodeTest.php 8.1 KB

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