AclBehaviorTest.php 10.0 KB

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