5
* Test the Acl Behavior
9
* CakePHP : Rapid Development Framework (http://cakephp.org)
10
* Copyright 2006-2010, Cake Software Foundation, Inc.
12
* Licensed under The MIT License
13
* Redistributions of files must retain the above copyright notice.
15
* @copyright Copyright 2006-2010, Cake Software Foundation, Inc.
16
* @link http://cakephp.org CakePHP Project
18
* @subpackage cake.cake.libs.tests.model.behaviors.acl
19
* @since CakePHP v 1.2.0.4487
20
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
22
App::import('Behavior', 'Acl');
23
App::import('Core', 'db_acl');
26
* Test Person class - self joined model
29
* @subpackage cake.tests.cases.libs.model.behaviors
31
class AclPerson extends CakeTestModel {
39
var $name = 'AclPerson';
47
var $useTable = 'people';
55
var $actsAs = array('Acl' => 'requester');
63
var $belongsTo = array(
65
'className' => 'AclPerson',
66
'foreignKey' => 'mother_id',
78
'className' => 'AclPerson',
79
'foreignKey' => 'mother_id'
89
function parentNode() {
90
if (!$this->id && empty($this->data)) {
93
if (isset($this->data['AclPerson']['mother_id'])) {
94
$motherId = $this->data['AclPerson']['mother_id'];
96
$motherId = $this->field('mother_id');
101
return array('AclPerson' => array('id' => $motherId));
110
* @subpackage cake.tests.cases.libs.model.behaviors
112
class AclUser extends CakeTestModel {
128
var $useTable = 'users';
136
var $actsAs = array('Acl');
143
function parentNode() {
152
* @subpackage cake.tests.cases.libs.model.behaviors
154
class AclPost extends CakeTestModel {
170
var $useTable = 'posts';
178
var $actsAs = array('Acl' => 'Controlled');
185
function parentNode() {
191
* AclBehaviorTest class
194
* @subpackage cake.tests.cases.libs.controller.components
196
class AclBehaviorTestCase extends CakeTestCase {
220
var $fixtures = array('core.person', 'core.user', 'core.post', 'core.aco', 'core.aro', 'core.aros_aco');
228
function startTest() {
229
Configure::write('Acl.database', 'test_suite');
231
$this->Aco =& new Aco();
232
$this->Aro =& new Aro();
241
function tearDown() {
242
ClassRegistry::flush();
243
unset($this->Aro, $this->Aco);
247
* Test Setup of AclBehavior
252
function testSetup() {
253
$User =& new AclUser();
254
$this->assertTrue(isset($User->Behaviors->Acl->settings['User']));
255
$this->assertEqual($User->Behaviors->Acl->settings['User']['type'], 'requester');
256
$this->assertTrue(is_object($User->Aro));
258
$Post =& new AclPost();
259
$this->assertTrue(isset($Post->Behaviors->Acl->settings['Post']));
260
$this->assertEqual($Post->Behaviors->Acl->settings['Post']['type'], 'controlled');
261
$this->assertTrue(is_object($Post->Aco));
270
function testAfterSave() {
271
$Post =& new AclPost();
275
'title' => 'Acl Post',
276
'body' => 'post body',
281
$result = $this->Aco->find('first', array(
282
'conditions' => array('Aco.model' => 'Post', 'Aco.foreign_key' => $Post->id)
284
$this->assertTrue(is_array($result));
285
$this->assertEqual($result['Aco']['model'], 'Post');
286
$this->assertEqual($result['Aco']['foreign_key'], $Post->id);
290
'model' => 'AclPerson',
295
$this->Aro->save($aroData);
297
$Person =& new AclPerson();
299
'AclPerson' => array(
305
$Person->save($data);
306
$result = $this->Aro->find('first', array(
307
'conditions' => array('Aro.model' => 'AclPerson', 'Aro.foreign_key' => $Person->id)
309
$this->assertTrue(is_array($result));
310
$this->assertEqual($result['Aro']['parent_id'], 5);
312
$node = $Person->node(array('model' => 'AclPerson', 'foreign_key' => 8));
313
$this->assertEqual(count($node), 2);
314
$this->assertEqual($node[0]['Aro']['parent_id'], 5);
315
$this->assertEqual($node[1]['Aro']['parent_id'], null);
319
'model' => 'AclPerson',
324
$this->Aro->create();
325
$this->Aro->save($aroData);
327
$Person->read(null, 8);
328
$Person->set('mother_id', 1);
330
$result = $this->Aro->find('first', array(
331
'conditions' => array('Aro.model' => 'AclPerson', 'Aro.foreign_key' => $Person->id)
333
$this->assertTrue(is_array($result));
334
$this->assertEqual($result['Aro']['parent_id'], 7);
336
$node = $Person->node(array('model' => 'AclPerson', 'foreign_key' => 8));
337
$this->assertEqual(sizeof($node), 2);
338
$this->assertEqual($node[0]['Aro']['parent_id'], 7);
339
$this->assertEqual($node[1]['Aro']['parent_id'], null);
343
* test that an afterSave on an update does not cause parent_id to become null.
347
function testAfterSaveUpdateParentIdNotNull() {
350
'model' => 'AclPerson',
355
$this->Aro->save($aroData);
357
$Person =& new AclPerson();
359
'AclPerson' => array(
365
$Person->save($data);
366
$result = $this->Aro->find('first', array(
367
'conditions' => array('Aro.model' => 'AclPerson', 'Aro.foreign_key' => $Person->id)
369
$this->assertTrue(is_array($result));
370
$this->assertEqual($result['Aro']['parent_id'], 5);
372
$Person->save(array('id' => $Person->id, 'name' => 'Bruce'));
373
$result = $this->Aro->find('first', array(
374
'conditions' => array('Aro.model' => 'AclPerson', 'Aro.foreign_key' => $Person->id)
376
$this->assertEqual($result['Aro']['parent_id'], 5);
385
function testAfterDelete() {
388
'model' => 'AclPerson',
393
$this->Aro->save($aroData);
394
$Person =& new AclPerson();
396
'AclPerson' => array(
402
$Person->save($data);
404
$node = $Person->node();
405
$this->assertEqual(count($node), 2);
406
$this->assertEqual($node[0]['Aro']['parent_id'], 5);
407
$this->assertEqual($node[1]['Aro']['parent_id'], null);
409
$Person->delete($id);
410
$result = $this->Aro->find('first', array(
411
'conditions' => array('Aro.model' => 'AclPerson', 'Aro.foreign_key' => $id)
413
$this->assertTrue(empty($result));
414
$result = $this->Aro->find('first', array(
415
'conditions' => array('Aro.model' => 'AclPerson', 'Aro.foreign_key' => 2)
417
$this->assertFalse(empty($result));
420
'AclPerson' => array(
426
$Person->save($data);
429
$result = $this->Aro->find('first', array(
430
'conditions' => array('Aro.model' => 'AclPerson', 'Aro.foreign_key' => $id)
432
$this->assertTrue(empty($result));
434
$result = $this->Aro->find('first', array(
435
'conditions' => array('Aro.model' => 'AclPerson', 'Aro.foreign_key' => 2)
437
$this->assertTrue(empty($result));
446
function testNode() {
447
$Person =& new AclPerson();
450
'model' => 'AclPerson',
455
$this->Aro->save($aroData);
458
$result = $Person->node();
459
$this->assertTrue(is_array($result));
460
$this->assertEqual(count($result), 1);