/cake/tests/cases/libs/model/behaviors/acl.test.php

https://github.com/damanlovett/NCAIE · PHP · 368 lines · 173 code · 10 blank · 185 comment · 5 complexity · 961916c6e35256276732fe90b7a5dd04 MD5 · raw file

  1. <?php
  2. /* SVN FILE: $Id: acl.test.php 8120 2009-03-19 20:25:10Z gwoo $ */
  3. /**
  4. * AclBehaviorTest file
  5. *
  6. * Test the Acl Behavior
  7. *
  8. * PHP versions 4 and 5
  9. *
  10. * CakePHP : Rapid Development Framework (http://www.cakephp.org)
  11. * Copyright 2006-2008, Cake Software Foundation, Inc.
  12. *
  13. * Licensed under The MIT License
  14. * Redistributions of files must retain the above copyright notice.
  15. *
  16. * @filesource
  17. * @copyright Copyright 2006-2008, Cake Software Foundation, Inc.
  18. * @link http://www.cakefoundation.org/projects/info/cakephp CakePHP Project
  19. * @package cake
  20. * @subpackage cake.cake.libs.tests.model.behaviors.acl
  21. * @since CakePHP v 1.2.0.4487
  22. * @version $Revision: 8120 $
  23. * @modifiedby $LastChangedBy: gwoo $
  24. * @lastmodified $Date: 2009-03-19 16:25:10 -0400 (Thu, 19 Mar 2009) $
  25. * @license http://www.opensource.org/licenses/mit-license.php The MIT License
  26. */
  27. App::import('Behavior', 'Acl');
  28. App::import('Core', 'db_acl');
  29. /**
  30. * Test Person class - self joined model
  31. *
  32. * @package cake
  33. * @subpackage cake.tests.cases.libs.model.behaviors
  34. */
  35. class AclPerson extends CakeTestModel {
  36. /**
  37. * name property
  38. *
  39. * @var string
  40. * @access public
  41. */
  42. var $name = 'AclPerson';
  43. /**
  44. * useTable property
  45. *
  46. * @var string
  47. * @access public
  48. */
  49. var $useTable = 'people';
  50. /**
  51. * actsAs property
  52. *
  53. * @var array
  54. * @access public
  55. */
  56. var $actsAs = array('Acl' => 'requester');
  57. /**
  58. * belongsTo property
  59. *
  60. * @var array
  61. * @access public
  62. */
  63. var $belongsTo = array(
  64. 'Mother' => array(
  65. 'className' => 'AclPerson',
  66. 'foreignKey' => 'mother_id',
  67. )
  68. );
  69. /**
  70. * hasMany property
  71. *
  72. * @var array
  73. * @access public
  74. */
  75. var $hasMany = array(
  76. 'Child' => array(
  77. 'className' => 'AclPerson',
  78. 'foreignKey' => 'mother_id'
  79. )
  80. );
  81. /**
  82. * parentNode method
  83. *
  84. * @return void
  85. * @access public
  86. */
  87. function parentNode() {
  88. if (!$this->id && empty($this->data)) {
  89. return null;
  90. }
  91. $data = $this->data;
  92. if (empty($this->data)) {
  93. $data = $this->read();
  94. }
  95. if (!$data['AclPerson']['mother_id']) {
  96. return null;
  97. } else {
  98. return array('AclPerson' => array('id' => $data['AclPerson']['mother_id']));
  99. }
  100. }
  101. }
  102. /**
  103. * AclUser class
  104. *
  105. * @package cake
  106. * @subpackage cake.tests.cases.libs.model.behaviors
  107. */
  108. class AclUser extends CakeTestModel {
  109. /**
  110. * name property
  111. *
  112. * @var string
  113. * @access public
  114. */
  115. var $name = 'User';
  116. /**
  117. * useTable property
  118. *
  119. * @var string
  120. * @access public
  121. */
  122. var $useTable = 'users';
  123. /**
  124. * actsAs property
  125. *
  126. * @var array
  127. * @access public
  128. */
  129. var $actsAs = array('Acl');
  130. /**
  131. * parentNode
  132. *
  133. * @access public
  134. */
  135. function parentNode() {
  136. return null;
  137. }
  138. }
  139. /**
  140. * AclPost class
  141. *
  142. * @package cake
  143. * @subpackage cake.tests.cases.libs.model.behaviors
  144. */
  145. class AclPost extends CakeTestModel {
  146. /**
  147. * name property
  148. *
  149. * @var string
  150. * @access public
  151. */
  152. var $name = 'Post';
  153. /**
  154. * useTable property
  155. *
  156. * @var string
  157. * @access public
  158. */
  159. var $useTable = 'posts';
  160. /**
  161. * actsAs property
  162. *
  163. * @var array
  164. * @access public
  165. */
  166. var $actsAs = array('Acl' => 'controlled');
  167. /**
  168. * parentNode
  169. *
  170. * @access public
  171. */
  172. function parentNode() {
  173. return null;
  174. }
  175. }
  176. /**
  177. * AclBehaviorTest class
  178. *
  179. * @package cake
  180. * @subpackage cake.tests.cases.libs.controller.components
  181. */
  182. class AclBehaviorTestCase extends CakeTestCase {
  183. /**
  184. * Aco property
  185. *
  186. * @var Aco
  187. * @access public
  188. */
  189. var $Aco;
  190. /**
  191. * Aro property
  192. *
  193. * @var Aro
  194. * @access public
  195. */
  196. var $Aro;
  197. /**
  198. * fixtures property
  199. *
  200. * @var array
  201. * @access public
  202. */
  203. var $fixtures = array('core.person', 'core.user', 'core.post', 'core.aco', 'core.aro', 'core.aros_aco');
  204. /**
  205. * Set up the test
  206. *
  207. * @return void
  208. * @access public
  209. */
  210. function startTest() {
  211. Configure::write('Acl.database', 'test_suite');
  212. $this->Aco =& new Aco();
  213. $this->Aro =& new Aro();
  214. }
  215. /**
  216. * tearDown method
  217. *
  218. * @return void
  219. * @access public
  220. */
  221. function tearDown() {
  222. ClassRegistry::flush();
  223. unset($this->Aro, $this->Aco);
  224. }
  225. /**
  226. * Test Setup of AclBehavior
  227. *
  228. * @return void
  229. * @access public
  230. */
  231. function testSetup() {
  232. $User =& new AclUser();
  233. $this->assertTrue(isset($User->Behaviors->Acl->settings['User']));
  234. $this->assertEqual($User->Behaviors->Acl->settings['User']['type'], 'requester');
  235. $this->assertTrue(is_object($User->Aro));
  236. $Post =& new AclPost();
  237. $this->assertTrue(isset($Post->Behaviors->Acl->settings['Post']));
  238. $this->assertEqual($Post->Behaviors->Acl->settings['Post']['type'], 'controlled');
  239. $this->assertTrue(is_object($Post->Aco));
  240. }
  241. /**
  242. * test After Save
  243. *
  244. * @return void
  245. * @access public
  246. */
  247. function testAfterSave() {
  248. $Post =& new AclPost();
  249. $data = array(
  250. 'Post' => array(
  251. 'author_id' => 1,
  252. 'title' => 'Acl Post',
  253. 'body' => 'post body',
  254. 'published' => 1
  255. ),
  256. );
  257. $Post->save($data);
  258. $result = $this->Aco->find('first', array('conditions' => array('Aco.model' => 'Post', 'Aco.foreign_key' => $Post->id)));
  259. $this->assertTrue(is_array($result));
  260. $this->assertEqual($result['Aco']['model'], 'Post');
  261. $this->assertEqual($result['Aco']['foreign_key'], $Post->id);
  262. $aroData = array(
  263. 'Aro' => array(
  264. 'model' => 'AclPerson',
  265. 'foreign_key' => 2,
  266. 'parent_id' => null
  267. )
  268. );
  269. $this->Aro->save($aroData);
  270. $Person =& new AclPerson();
  271. $data = array(
  272. 'AclPerson' => array(
  273. 'name' => 'Trent',
  274. 'mother_id' => 2,
  275. 'father_id' => 3,
  276. ),
  277. );
  278. $Person->save($data);
  279. $result = $this->Aro->find('first', array('conditions' => array('Aro.model' => 'AclPerson', 'Aro.foreign_key' => $Person->id)));
  280. $this->assertTrue(is_array($result));
  281. $this->assertEqual($result['Aro']['parent_id'], 5);
  282. $node = $Person->node(array('model' => 'AclPerson', 'foreign_key' => 8));
  283. $this->assertEqual(sizeof($node), 2);
  284. $this->assertEqual($node[0]['Aro']['parent_id'], 5);
  285. $this->assertEqual($node[1]['Aro']['parent_id'], null);
  286. }
  287. /**
  288. * Test After Delete
  289. *
  290. * @return void
  291. * @access public
  292. */
  293. function testAfterDelete() {
  294. $aroData = array(
  295. 'Aro' => array(
  296. 'model' => 'AclPerson',
  297. 'foreign_key' => 2,
  298. 'parent_id' => null
  299. )
  300. );
  301. $this->Aro->save($aroData);
  302. $Person =& new AclPerson();
  303. $data = array(
  304. 'AclPerson' => array(
  305. 'name' => 'Trent',
  306. 'mother_id' => 2,
  307. 'father_id' => 3,
  308. ),
  309. );
  310. $Person->save($data);
  311. $id = $Person->id;
  312. $node = $Person->node();
  313. $this->assertEqual(sizeof($node), 2);
  314. $this->assertEqual($node[0]['Aro']['parent_id'], 5);
  315. $this->assertEqual($node[1]['Aro']['parent_id'], null);
  316. $Person->delete($id);
  317. $result = $this->Aro->find('first', array('conditions' => array('Aro.model' => 'AclPerson', 'Aro.foreign_key' => $id)));
  318. $this->assertTrue(empty($result));
  319. $result = $this->Aro->find('first', array('conditions' => array('Aro.model' => 'AclPerson', 'Aro.foreign_key' => 2)));
  320. $this->assertFalse(empty($result));
  321. $data = array(
  322. 'AclPerson' => array(
  323. 'name' => 'Trent',
  324. 'mother_id' => 2,
  325. 'father_id' => 3,
  326. ),
  327. );
  328. $Person->save($data);
  329. $id = $Person->id;
  330. $Person->delete(2);
  331. $result = $this->Aro->find('first', array('conditions' => array('Aro.model' => 'AclPerson', 'Aro.foreign_key' => $id)));
  332. $this->assertTrue(empty($result));
  333. $result = $this->Aro->find('first', array('conditions' => array('Aro.model' => 'AclPerson', 'Aro.foreign_key' => 2)));
  334. $this->assertTrue(empty($result));
  335. }
  336. /**
  337. * Test Node()
  338. *
  339. * @return void
  340. * @access public
  341. */
  342. function testNode() {
  343. $Person =& new AclPerson();
  344. $aroData = array(
  345. 'Aro' => array(
  346. 'model' => 'AclPerson',
  347. 'foreign_key' => 2,
  348. 'parent_id' => null
  349. )
  350. );
  351. $this->Aro->save($aroData);
  352. $Person->id = 2;
  353. $result = $Person->node();
  354. $this->assertTrue(is_array($result));
  355. $this->assertEqual(sizeof($result), 1);
  356. }
  357. }
  358. ?>