AclNodeTest.php 7.8 KB

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