PageRenderTime 48ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

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

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