/bk/lib/Cake/Test/Case/Model/Behavior/AclBehaviorTest.php

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