AclBehaviorTest.php 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  1. <?php
  2. /**
  3. * AclBehaviorTest file
  4. *
  5. * Test the Acl Behavior
  6. *
  7. * CakePHP : Rapid Development Framework (http://cakephp.org)
  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://cakephp.org CakePHP Project
  16. * @package Cake.Test.Case.Model.Behavior
  17. * @since CakePHP v 1.2.0.4487
  18. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  19. */
  20. App::uses('AclBehavior', 'Model/Behavior');
  21. App::uses('Aco', 'Model');
  22. App::uses('Aro', 'Model');
  23. App::uses('AclNode', 'Model');
  24. App::uses('DbAcl', 'Model');
  25. /**
  26. * Test Person class - self joined model
  27. *
  28. * @package Cake.Test.Case.Model.Behavior
  29. */
  30. class AclPerson extends CakeTestModel {
  31. /**
  32. * useTable property
  33. *
  34. * @var string
  35. */
  36. public $useTable = 'people';
  37. /**
  38. * actsAs property
  39. *
  40. * @var array
  41. */
  42. public $actsAs = array('Acl' => 'both');
  43. /**
  44. * belongsTo property
  45. *
  46. * @var array
  47. */
  48. public $belongsTo = array(
  49. 'Mother' => array(
  50. 'className' => 'AclPerson',
  51. 'foreignKey' => 'mother_id',
  52. )
  53. );
  54. /**
  55. * hasMany property
  56. *
  57. * @var array
  58. */
  59. public $hasMany = array(
  60. 'Child' => array(
  61. 'className' => 'AclPerson',
  62. 'foreignKey' => 'mother_id'
  63. )
  64. );
  65. /**
  66. * parentNode method
  67. *
  68. * @return void
  69. */
  70. public function parentNode() {
  71. if (!$this->id && empty($this->data)) {
  72. return null;
  73. }
  74. if (isset($this->data['AclPerson']['mother_id'])) {
  75. $motherId = $this->data['AclPerson']['mother_id'];
  76. } else {
  77. $motherId = $this->field('mother_id');
  78. }
  79. if (!$motherId) {
  80. return null;
  81. }
  82. return array('AclPerson' => array('id' => $motherId));
  83. }
  84. }
  85. /**
  86. * AclUser class
  87. *
  88. * @package Cake.Test.Case.Model.Behavior
  89. */
  90. class AclUser extends CakeTestModel {
  91. /**
  92. * name property
  93. *
  94. * @var string
  95. */
  96. public $name = 'User';
  97. /**
  98. * useTable property
  99. *
  100. * @var string
  101. */
  102. public $useTable = 'users';
  103. /**
  104. * actsAs property
  105. *
  106. * @var array
  107. */
  108. public $actsAs = array('Acl' => array('type' => 'requester'));
  109. /**
  110. * parentNode
  111. *
  112. */
  113. public function parentNode() {
  114. return null;
  115. }
  116. }
  117. /**
  118. * AclPost class
  119. *
  120. * @package Cake.Test.Case.Model.Behavior
  121. */
  122. class AclPost extends CakeTestModel {
  123. /**
  124. * name property
  125. *
  126. * @var string
  127. */
  128. public $name = 'Post';
  129. /**
  130. * useTable property
  131. *
  132. * @var string
  133. */
  134. public $useTable = 'posts';
  135. /**
  136. * actsAs property
  137. *
  138. * @var array
  139. */
  140. public $actsAs = array('Acl' => array('type' => 'Controlled'));
  141. /**
  142. * parentNode
  143. *
  144. */
  145. public function parentNode() {
  146. return null;
  147. }
  148. }
  149. /**
  150. * AclBehaviorTest class
  151. *
  152. * @package Cake.Test.Case.Model.Behavior
  153. */
  154. class AclBehaviorTest extends CakeTestCase {
  155. /**
  156. * Aco property
  157. *
  158. * @var Aco
  159. */
  160. public $Aco;
  161. /**
  162. * Aro property
  163. *
  164. * @var Aro
  165. */
  166. public $Aro;
  167. /**
  168. * fixtures property
  169. *
  170. * @var array
  171. */
  172. public $fixtures = array('core.person', 'core.user', 'core.post', 'core.aco', 'core.aro', 'core.aros_aco');
  173. /**
  174. * Set up the test
  175. *
  176. * @return void
  177. */
  178. public function setUp() {
  179. parent::setUp();
  180. Configure::write('Acl.database', 'test');
  181. $this->Aco = new Aco();
  182. $this->Aro = new Aro();
  183. }
  184. /**
  185. * tearDown method
  186. *
  187. * @return void
  188. */
  189. public function tearDown() {
  190. parent::tearDown();
  191. unset($this->Aro, $this->Aco);
  192. }
  193. /**
  194. * Test Setup of AclBehavior
  195. *
  196. * @return void
  197. */
  198. public function testSetup() {
  199. parent::setUp();
  200. $User = new AclUser();
  201. $this->assertTrue(isset($User->Behaviors->Acl->settings['User']));
  202. $this->assertEquals('requester', $User->Behaviors->Acl->settings['User']['type']);
  203. $this->assertTrue(is_object($User->Aro));
  204. $Post = new AclPost();
  205. $this->assertTrue(isset($Post->Behaviors->Acl->settings['Post']));
  206. $this->assertEquals('controlled', $Post->Behaviors->Acl->settings['Post']['type']);
  207. $this->assertTrue(is_object($Post->Aco));
  208. }
  209. /**
  210. * Test Setup of AclBehavior as both requester and controlled
  211. *
  212. * @return void
  213. */
  214. public function testSetupMulti() {
  215. $User = new AclPerson();
  216. $this->assertTrue(isset($User->Behaviors->Acl->settings['AclPerson']));
  217. $this->assertEquals('both', $User->Behaviors->Acl->settings['AclPerson']['type']);
  218. $this->assertTrue(is_object($User->Aro));
  219. $this->assertTrue(is_object($User->Aco));
  220. }
  221. /**
  222. * test After Save
  223. *
  224. * @return void
  225. */
  226. public function testAfterSave() {
  227. $Post = new AclPost();
  228. $data = array(
  229. 'Post' => array(
  230. 'author_id' => 1,
  231. 'title' => 'Acl Post',
  232. 'body' => 'post body',
  233. 'published' => 1
  234. ),
  235. );
  236. $Post->save($data);
  237. $result = $this->Aco->find('first', array(
  238. 'conditions' => array('Aco.model' => 'Post', 'Aco.foreign_key' => $Post->id)
  239. ));
  240. $this->assertTrue(is_array($result));
  241. $this->assertEquals('Post', $result['Aco']['model']);
  242. $this->assertEquals($Post->id, $result['Aco']['foreign_key']);
  243. $aroData = array(
  244. 'Aro' => array(
  245. 'model' => 'AclPerson',
  246. 'foreign_key' => 2,
  247. 'parent_id' => null
  248. )
  249. );
  250. $this->Aro->save($aroData);
  251. $acoData = array(
  252. 'Aco' => array(
  253. 'model' => 'AclPerson',
  254. 'foreign_key' => 2,
  255. 'parent_id' => null
  256. )
  257. );
  258. $this->Aco->save($acoData);
  259. $Person = new AclPerson();
  260. $data = array(
  261. 'AclPerson' => array(
  262. 'name' => 'Trent',
  263. 'mother_id' => 2,
  264. 'father_id' => 3,
  265. ),
  266. );
  267. $Person->save($data);
  268. $result = $this->Aro->find('first', array(
  269. 'conditions' => array('Aro.model' => 'AclPerson', 'Aro.foreign_key' => $Person->id)
  270. ));
  271. $this->assertTrue(is_array($result));
  272. $this->assertEquals(5, $result['Aro']['parent_id']);
  273. $node = $Person->node(array('model' => 'AclPerson', 'foreign_key' => 8), 'Aro');
  274. $this->assertEquals(2, count($node));
  275. $this->assertEquals(5, $node[0]['Aro']['parent_id']);
  276. $this->assertEquals(null, $node[1]['Aro']['parent_id']);
  277. $aroData = array(
  278. 'Aro' => array(
  279. 'model' => 'AclPerson',
  280. 'foreign_key' => 1,
  281. 'parent_id' => null
  282. )
  283. );
  284. $this->Aro->create();
  285. $this->Aro->save($aroData);
  286. $acoData = array(
  287. 'Aco' => array(
  288. 'model' => 'AclPerson',
  289. 'foreign_key' => 1,
  290. 'parent_id' => null
  291. ));
  292. $this->Aco->create();
  293. $this->Aco->save($acoData);
  294. $Person->read(null, 8);
  295. $Person->set('mother_id', 1);
  296. $Person->save();
  297. $result = $this->Aro->find('first', array(
  298. 'conditions' => array('Aro.model' => 'AclPerson', 'Aro.foreign_key' => $Person->id)
  299. ));
  300. $this->assertTrue(is_array($result));
  301. $this->assertEquals(7, $result['Aro']['parent_id']);
  302. $node = $Person->node(array('model' => 'AclPerson', 'foreign_key' => 8), 'Aro');
  303. $this->assertEquals(2, count($node));
  304. $this->assertEquals(7, $node[0]['Aro']['parent_id']);
  305. $this->assertEquals(null, $node[1]['Aro']['parent_id']);
  306. }
  307. /**
  308. * test that an afterSave on an update does not cause parent_id to become null.
  309. *
  310. * @return void
  311. */
  312. public function testAfterSaveUpdateParentIdNotNull() {
  313. $aroData = array(
  314. 'Aro' => array(
  315. 'model' => 'AclPerson',
  316. 'foreign_key' => 2,
  317. 'parent_id' => null
  318. )
  319. );
  320. $this->Aro->save($aroData);
  321. $acoData = array(
  322. 'Aco' => array(
  323. 'model' => 'AclPerson',
  324. 'foreign_key' => 2,
  325. 'parent_id' => null
  326. )
  327. );
  328. $this->Aco->save($acoData);
  329. $Person = new AclPerson();
  330. $data = array(
  331. 'AclPerson' => array(
  332. 'name' => 'Trent',
  333. 'mother_id' => 2,
  334. 'father_id' => 3,
  335. ),
  336. );
  337. $Person->save($data);
  338. $result = $this->Aro->find('first', array(
  339. 'conditions' => array('Aro.model' => 'AclPerson', 'Aro.foreign_key' => $Person->id)
  340. ));
  341. $this->assertTrue(is_array($result));
  342. $this->assertEquals(5, $result['Aro']['parent_id']);
  343. $Person->save(array('id' => $Person->id, 'name' => 'Bruce'));
  344. $result = $this->Aro->find('first', array(
  345. 'conditions' => array('Aro.model' => 'AclPerson', 'Aro.foreign_key' => $Person->id)
  346. ));
  347. $this->assertEquals(5, $result['Aro']['parent_id']);
  348. }
  349. /**
  350. * Test After Delete
  351. *
  352. * @return void
  353. */
  354. public function testAfterDelete() {
  355. $aroData = array(
  356. 'Aro' => array(
  357. 'model' => 'AclPerson',
  358. 'foreign_key' => 2,
  359. 'parent_id' => null
  360. )
  361. );
  362. $this->Aro->save($aroData);
  363. $acoData = array(
  364. 'Aco' => array(
  365. 'model' => 'AclPerson',
  366. 'foreign_key' => 2,
  367. 'parent_id' => null
  368. )
  369. );
  370. $this->Aco->save($acoData);
  371. $Person = new AclPerson();
  372. $data = array(
  373. 'AclPerson' => array(
  374. 'name' => 'Trent',
  375. 'mother_id' => 2,
  376. 'father_id' => 3,
  377. ),
  378. );
  379. $Person->save($data);
  380. $id = $Person->id;
  381. $node = $Person->node(null, 'Aro');
  382. $this->assertEquals(2, count($node));
  383. $this->assertEquals(5, $node[0]['Aro']['parent_id']);
  384. $this->assertEquals(null, $node[1]['Aro']['parent_id']);
  385. $Person->delete($id);
  386. $result = $this->Aro->find('first', array(
  387. 'conditions' => array('Aro.model' => 'AclPerson', 'Aro.foreign_key' => $id)
  388. ));
  389. $this->assertTrue(empty($result));
  390. $result = $this->Aro->find('first', array(
  391. 'conditions' => array('Aro.model' => 'AclPerson', 'Aro.foreign_key' => 2)
  392. ));
  393. $this->assertFalse(empty($result));
  394. $data = array(
  395. 'AclPerson' => array(
  396. 'name' => 'Trent',
  397. 'mother_id' => 2,
  398. 'father_id' => 3,
  399. ),
  400. );
  401. $Person->save($data);
  402. $id = $Person->id;
  403. $Person->delete(2);
  404. $result = $this->Aro->find('first', array(
  405. 'conditions' => array('Aro.model' => 'AclPerson', 'Aro.foreign_key' => $id)
  406. ));
  407. $this->assertTrue(empty($result));
  408. $result = $this->Aro->find('first', array(
  409. 'conditions' => array('Aro.model' => 'AclPerson', 'Aro.foreign_key' => 2)
  410. ));
  411. $this->assertTrue(empty($result));
  412. }
  413. /**
  414. * Test Node()
  415. *
  416. * @return void
  417. */
  418. public function testNode() {
  419. $Person = new AclPerson();
  420. $aroData = array(
  421. 'Aro' => array(
  422. 'model' => 'AclPerson',
  423. 'foreign_key' => 2,
  424. 'parent_id' => null
  425. )
  426. );
  427. $this->Aro->save($aroData);
  428. $Person->id = 2;
  429. $result = $Person->node(null, 'Aro');
  430. $this->assertTrue(is_array($result));
  431. $this->assertEquals(1, count($result));
  432. }
  433. }