AclBehaviorTest.php 9.9 KB

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