/lib/Cake/Test/TestCase/Model/Behavior/AclBehaviorTest.php

https://github.com/shama/cakephp · PHP · 493 lines · 279 code · 51 blank · 163 comment · 6 complexity · a5f20984f3a4ccc523a2d5d62516b885 MD5 · raw file

  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. $this->markTestIncomplete('Not runnable until Models are fixed.');
  190. Configure::write('Acl.database', 'test');
  191. $this->Aco = new Aco();
  192. $this->Aro = new Aro();
  193. }
  194. /**
  195. * tearDown method
  196. *
  197. * @return void
  198. */
  199. public function tearDown() {
  200. parent::tearDown();
  201. unset($this->Aro, $this->Aco);
  202. }
  203. /**
  204. * Test Setup of AclBehavior
  205. *
  206. * @return void
  207. */
  208. public function testSetup() {
  209. $User = new AclUser();
  210. $this->assertTrue(isset($User->Behaviors->Acl->settings['User']));
  211. $this->assertEquals('requester', $User->Behaviors->Acl->settings['User']['type']);
  212. $this->assertTrue(is_object($User->Aro));
  213. $Post = new AclPost();
  214. $this->assertTrue(isset($Post->Behaviors->Acl->settings['Post']));
  215. $this->assertEquals('controlled', $Post->Behaviors->Acl->settings['Post']['type']);
  216. $this->assertTrue(is_object($Post->Aco));
  217. }
  218. /**
  219. * Test Setup of AclBehavior as both requester and controlled
  220. *
  221. * @return void
  222. */
  223. public function testSetupMulti() {
  224. $User = new AclPerson();
  225. $this->assertTrue(isset($User->Behaviors->Acl->settings['AclPerson']));
  226. $this->assertEquals('both', $User->Behaviors->Acl->settings['AclPerson']['type']);
  227. $this->assertTrue(is_object($User->Aro));
  228. $this->assertTrue(is_object($User->Aco));
  229. }
  230. /**
  231. * test After Save
  232. *
  233. * @return void
  234. */
  235. public function testAfterSave() {
  236. $Post = new AclPost();
  237. $data = array(
  238. 'Post' => array(
  239. 'author_id' => 1,
  240. 'title' => 'Acl Post',
  241. 'body' => 'post body',
  242. 'published' => 1
  243. ),
  244. );
  245. $Post->save($data);
  246. $result = $this->Aco->find('first', array(
  247. 'conditions' => array('Aco.model' => 'Post', 'Aco.foreign_key' => $Post->id)
  248. ));
  249. $this->assertTrue(is_array($result));
  250. $this->assertEquals('Post', $result['Aco']['model']);
  251. $this->assertEquals($Post->id, $result['Aco']['foreign_key']);
  252. $aroData = array(
  253. 'Aro' => array(
  254. 'model' => 'AclPerson',
  255. 'foreign_key' => 2,
  256. 'parent_id' => null
  257. )
  258. );
  259. $this->Aro->save($aroData);
  260. $acoData = array(
  261. 'Aco' => array(
  262. 'model' => 'AclPerson',
  263. 'foreign_key' => 2,
  264. 'parent_id' => null
  265. )
  266. );
  267. $this->Aco->save($acoData);
  268. $Person = new AclPerson();
  269. $data = array(
  270. 'AclPerson' => array(
  271. 'name' => 'Trent',
  272. 'mother_id' => 2,
  273. 'father_id' => 3,
  274. ),
  275. );
  276. $Person->save($data);
  277. $result = $this->Aro->find('first', array(
  278. 'conditions' => array('Aro.model' => 'AclPerson', 'Aro.foreign_key' => $Person->id)
  279. ));
  280. $this->assertTrue(is_array($result));
  281. $this->assertEquals(5, $result['Aro']['parent_id']);
  282. $node = $Person->node(array('model' => 'AclPerson', 'foreign_key' => 8), 'Aro');
  283. $this->assertEquals(2, count($node));
  284. $this->assertEquals(5, $node[0]['Aro']['parent_id']);
  285. $this->assertEquals(null, $node[1]['Aro']['parent_id']);
  286. $aroData = array(
  287. 'Aro' => array(
  288. 'model' => 'AclPerson',
  289. 'foreign_key' => 1,
  290. 'parent_id' => null
  291. )
  292. );
  293. $this->Aro->create();
  294. $this->Aro->save($aroData);
  295. $acoData = array(
  296. 'Aco' => array(
  297. 'model' => 'AclPerson',
  298. 'foreign_key' => 1,
  299. 'parent_id' => null
  300. ));
  301. $this->Aco->create();
  302. $this->Aco->save($acoData);
  303. $Person->read(null, 8);
  304. $Person->set('mother_id', 1);
  305. $Person->save();
  306. $result = $this->Aro->find('first', array(
  307. 'conditions' => array('Aro.model' => 'AclPerson', 'Aro.foreign_key' => $Person->id)
  308. ));
  309. $this->assertTrue(is_array($result));
  310. $this->assertEquals(7, $result['Aro']['parent_id']);
  311. $node = $Person->node(array('model' => 'AclPerson', 'foreign_key' => 8), 'Aro');
  312. $this->assertEquals(2, count($node));
  313. $this->assertEquals(7, $node[0]['Aro']['parent_id']);
  314. $this->assertEquals(null, $node[1]['Aro']['parent_id']);
  315. }
  316. /**
  317. * test that an afterSave on an update does not cause parent_id to become null.
  318. *
  319. * @return void
  320. */
  321. public function testAfterSaveUpdateParentIdNotNull() {
  322. $aroData = array(
  323. 'Aro' => array(
  324. 'model' => 'AclPerson',
  325. 'foreign_key' => 2,
  326. 'parent_id' => null
  327. )
  328. );
  329. $this->Aro->save($aroData);
  330. $acoData = array(
  331. 'Aco' => array(
  332. 'model' => 'AclPerson',
  333. 'foreign_key' => 2,
  334. 'parent_id' => null
  335. )
  336. );
  337. $this->Aco->save($acoData);
  338. $Person = new AclPerson();
  339. $data = array(
  340. 'AclPerson' => array(
  341. 'name' => 'Trent',
  342. 'mother_id' => 2,
  343. 'father_id' => 3,
  344. ),
  345. );
  346. $Person->save($data);
  347. $result = $this->Aro->find('first', array(
  348. 'conditions' => array('Aro.model' => 'AclPerson', 'Aro.foreign_key' => $Person->id)
  349. ));
  350. $this->assertTrue(is_array($result));
  351. $this->assertEquals(5, $result['Aro']['parent_id']);
  352. $Person->save(array('id' => $Person->id, 'name' => 'Bruce'));
  353. $result = $this->Aro->find('first', array(
  354. 'conditions' => array('Aro.model' => 'AclPerson', 'Aro.foreign_key' => $Person->id)
  355. ));
  356. $this->assertEquals(5, $result['Aro']['parent_id']);
  357. }
  358. /**
  359. * Test After Delete
  360. *
  361. * @return void
  362. */
  363. public function testAfterDelete() {
  364. $aroData = array(
  365. 'Aro' => array(
  366. 'model' => 'AclPerson',
  367. 'foreign_key' => 2,
  368. 'parent_id' => null
  369. )
  370. );
  371. $this->Aro->save($aroData);
  372. $acoData = array(
  373. 'Aco' => array(
  374. 'model' => 'AclPerson',
  375. 'foreign_key' => 2,
  376. 'parent_id' => null
  377. )
  378. );
  379. $this->Aco->save($acoData);
  380. $Person = new AclPerson();
  381. $data = array(
  382. 'AclPerson' => array(
  383. 'name' => 'Trent',
  384. 'mother_id' => 2,
  385. 'father_id' => 3,
  386. ),
  387. );
  388. $Person->save($data);
  389. $id = $Person->id;
  390. $node = $Person->node(null, 'Aro');
  391. $this->assertEquals(2, count($node));
  392. $this->assertEquals(5, $node[0]['Aro']['parent_id']);
  393. $this->assertEquals(null, $node[1]['Aro']['parent_id']);
  394. $Person->delete($id);
  395. $result = $this->Aro->find('first', array(
  396. 'conditions' => array('Aro.model' => 'AclPerson', 'Aro.foreign_key' => $id)
  397. ));
  398. $this->assertTrue(empty($result));
  399. $result = $this->Aro->find('first', array(
  400. 'conditions' => array('Aro.model' => 'AclPerson', 'Aro.foreign_key' => 2)
  401. ));
  402. $this->assertFalse(empty($result));
  403. $data = array(
  404. 'AclPerson' => array(
  405. 'name' => 'Trent',
  406. 'mother_id' => 2,
  407. 'father_id' => 3,
  408. ),
  409. );
  410. $Person->save($data);
  411. $id = $Person->id;
  412. $Person->delete(2);
  413. $result = $this->Aro->find('first', array(
  414. 'conditions' => array('Aro.model' => 'AclPerson', 'Aro.foreign_key' => $id)
  415. ));
  416. $this->assertTrue(empty($result));
  417. $result = $this->Aro->find('first', array(
  418. 'conditions' => array('Aro.model' => 'AclPerson', 'Aro.foreign_key' => 2)
  419. ));
  420. $this->assertTrue(empty($result));
  421. }
  422. /**
  423. * Test Node()
  424. *
  425. * @return void
  426. */
  427. public function testNode() {
  428. $Person = new AclPerson();
  429. $aroData = array(
  430. 'Aro' => array(
  431. 'model' => 'AclPerson',
  432. 'foreign_key' => 2,
  433. 'parent_id' => null
  434. )
  435. );
  436. $this->Aro->save($aroData);
  437. $Person->id = 2;
  438. $result = $Person->node(null, 'Aro');
  439. $this->assertTrue(is_array($result));
  440. $this->assertEquals(1, count($result));
  441. }
  442. }