AclBehaviorTest.php 9.8 KB

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