mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2025-03-18 23:49:55 +00:00
Adding test case for AclBehavior. Modified AclBehavior to use $model->name for configuration and operation. Closes #5163
git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@7391 3807eeeb-6ff5-0310-8944-8be069107fe0
This commit is contained in:
parent
7f75f74f5b
commit
3402f40061
2 changed files with 349 additions and 8 deletions
|
@ -47,17 +47,19 @@ class AclBehavior extends ModelBehavior {
|
||||||
* Sets up the configuation for the model, and loads ACL models if they haven't been already
|
* Sets up the configuation for the model, and loads ACL models if they haven't been already
|
||||||
*
|
*
|
||||||
* @param mixed $config
|
* @param mixed $config
|
||||||
|
* @return void
|
||||||
*/
|
*/
|
||||||
function setup(&$model, $config = array()) {
|
function setup(&$model, $config = array()) {
|
||||||
if (is_string($config)) {
|
if (is_string($config)) {
|
||||||
$config = array('type' => $config);
|
$config = array('type' => $config);
|
||||||
}
|
}
|
||||||
$this->settings[$model->alias] = array_merge(array('type' => 'requester'), (array)$config);
|
$this->settings[$model->name] = array_merge(array('type' => 'requester'), (array)$config);
|
||||||
$type = $this->__typeMaps[$this->settings[$model->alias]['type']];
|
|
||||||
|
$type = $this->__typeMaps[$this->settings[$model->name]['type']];
|
||||||
if (!class_exists('AclNode')) {
|
if (!class_exists('AclNode')) {
|
||||||
uses('model' . DS . 'db_acl');
|
uses('model' . DS . 'db_acl');
|
||||||
}
|
}
|
||||||
$model->{$type} =& ClassRegistry::init($type);;
|
$model->{$type} =& ClassRegistry::init($type);
|
||||||
if (!method_exists($model, 'parentNode')) {
|
if (!method_exists($model, 'parentNode')) {
|
||||||
trigger_error("Callback parentNode() not defined in {$model->alias}", E_USER_WARNING);
|
trigger_error("Callback parentNode() not defined in {$model->alias}", E_USER_WARNING);
|
||||||
}
|
}
|
||||||
|
@ -69,9 +71,9 @@ class AclBehavior extends ModelBehavior {
|
||||||
* @return array
|
* @return array
|
||||||
*/
|
*/
|
||||||
function node(&$model, $ref = null) {
|
function node(&$model, $ref = null) {
|
||||||
$type = $this->__typeMaps[strtolower($this->settings[$model->alias]['type'])];
|
$type = $this->__typeMaps[strtolower($this->settings[$model->name]['type'])];
|
||||||
if (empty($ref)) {
|
if (empty($ref)) {
|
||||||
$ref = array('model' => $model->alias, 'foreign_key' => $model->id);
|
$ref = array('model' => $model->name, 'foreign_key' => $model->id);
|
||||||
}
|
}
|
||||||
return $model->{$type}->node($ref);
|
return $model->{$type}->node($ref);
|
||||||
}
|
}
|
||||||
|
@ -79,10 +81,11 @@ class AclBehavior extends ModelBehavior {
|
||||||
* Creates a new ARO/ACO node bound to this record
|
* Creates a new ARO/ACO node bound to this record
|
||||||
*
|
*
|
||||||
* @param boolean $created True if this is a new record
|
* @param boolean $created True if this is a new record
|
||||||
|
* @return void
|
||||||
*/
|
*/
|
||||||
function afterSave(&$model, $created) {
|
function afterSave(&$model, $created) {
|
||||||
if ($created) {
|
if ($created) {
|
||||||
$type = $this->__typeMaps[strtolower($this->settings[$model->alias]['type'])];
|
$type = $this->__typeMaps[strtolower($this->settings[$model->name]['type'])];
|
||||||
$parent = $model->parentNode();
|
$parent = $model->parentNode();
|
||||||
if (!empty($parent)) {
|
if (!empty($parent)) {
|
||||||
$parent = $this->node($model, $parent);
|
$parent = $this->node($model, $parent);
|
||||||
|
@ -93,7 +96,7 @@ class AclBehavior extends ModelBehavior {
|
||||||
$model->{$type}->create();
|
$model->{$type}->create();
|
||||||
$model->{$type}->save(array(
|
$model->{$type}->save(array(
|
||||||
'parent_id' => Set::extract($parent, "0.{$type}.id"),
|
'parent_id' => Set::extract($parent, "0.{$type}.id"),
|
||||||
'model' => $model->alias,
|
'model' => $model->name,
|
||||||
'foreign_key' => $model->id
|
'foreign_key' => $model->id
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
@ -101,9 +104,10 @@ class AclBehavior extends ModelBehavior {
|
||||||
/**
|
/**
|
||||||
* Destroys the ARO/ACO node bound to the deleted record
|
* Destroys the ARO/ACO node bound to the deleted record
|
||||||
*
|
*
|
||||||
|
* @return void
|
||||||
*/
|
*/
|
||||||
function afterDelete(&$model) {
|
function afterDelete(&$model) {
|
||||||
$type = $this->__typeMaps[strtolower($this->settings[$model->alias]['type'])];
|
$type = $this->__typeMaps[strtolower($this->settings[$model->name]['type'])];
|
||||||
$node = Set::extract($this->node($model), "0.{$type}.id");
|
$node = Set::extract($this->node($model), "0.{$type}.id");
|
||||||
if (!empty($node)) {
|
if (!empty($node)) {
|
||||||
$model->{$type}->delete($node);
|
$model->{$type}->delete($node);
|
||||||
|
|
337
cake/tests/cases/libs/model/behaviors/acl.test.php
Normal file
337
cake/tests/cases/libs/model/behaviors/acl.test.php
Normal file
|
@ -0,0 +1,337 @@
|
||||||
|
<?php
|
||||||
|
/* SVN FILE: $Id$ */
|
||||||
|
/**
|
||||||
|
* Acl behavior test
|
||||||
|
*
|
||||||
|
* Test the Acl Behavior
|
||||||
|
*
|
||||||
|
* PHP versions 4 and 5
|
||||||
|
*
|
||||||
|
* CakePHP : Rapid Development Framework <http://www.cakephp.org/>
|
||||||
|
* Copyright 2006-2008, Cake Software Foundation, Inc.
|
||||||
|
* 1785 E. Sahara Avenue, Suite 490-204
|
||||||
|
* Las Vegas, Nevada 89104
|
||||||
|
*
|
||||||
|
* Licensed under The MIT License
|
||||||
|
* Redistributions of files must retain the above copyright notice.
|
||||||
|
*
|
||||||
|
* @filesource
|
||||||
|
* @copyright Copyright 2006-2008, Cake Software Foundation, Inc.
|
||||||
|
* @link http://www.cakefoundation.org/projects/info/cakephp CakePHP Project
|
||||||
|
* @package cake
|
||||||
|
* @subpackage cake.cake.libs.tests.model.behaviors.acl
|
||||||
|
* @since CakePHP v 1.2.0.4487
|
||||||
|
* @version $Revision$
|
||||||
|
* @modifiedby $LastChangedBy$
|
||||||
|
* @lastmodified $Date$
|
||||||
|
* @license http://www.opensource.org/licenses/mit-license.php The MIT License
|
||||||
|
*/
|
||||||
|
App::import('Behavior', 'Acl');
|
||||||
|
App::import('Core', 'db_acl');
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test Person Class - self joined model
|
||||||
|
*
|
||||||
|
* @package cake.tests
|
||||||
|
* @subpackage cake.tests.cases.libs.model.behaviors
|
||||||
|
*/
|
||||||
|
class AclPerson extends CakeTestModel {
|
||||||
|
/**
|
||||||
|
* name property
|
||||||
|
*
|
||||||
|
* @var string
|
||||||
|
* @access public
|
||||||
|
*/
|
||||||
|
var $name = 'Person';
|
||||||
|
/**
|
||||||
|
* useTable property
|
||||||
|
*
|
||||||
|
* @var string
|
||||||
|
* @access public
|
||||||
|
*/
|
||||||
|
var $useTable = 'people';
|
||||||
|
/**
|
||||||
|
* actsAs property
|
||||||
|
*
|
||||||
|
* @var array
|
||||||
|
* @access public
|
||||||
|
*/
|
||||||
|
var $actsAs = array('Acl' => 'requester');
|
||||||
|
/**
|
||||||
|
* belongsTo property
|
||||||
|
*
|
||||||
|
* @var array
|
||||||
|
* @access public
|
||||||
|
*/
|
||||||
|
var $belongsTo = array(
|
||||||
|
'Mother' => array(
|
||||||
|
'className' => 'AclPerson',
|
||||||
|
'foreignKey' => 'mother_id',
|
||||||
|
)
|
||||||
|
);
|
||||||
|
/**
|
||||||
|
* hasMany property
|
||||||
|
*
|
||||||
|
* @var array
|
||||||
|
* @access public
|
||||||
|
*/
|
||||||
|
var $hasMany = array(
|
||||||
|
'Child' => array(
|
||||||
|
'className' => 'AclPerson',
|
||||||
|
'foreignKey' => 'mother_id'
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* ParentNode
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
**/
|
||||||
|
function parentNode() {
|
||||||
|
if (!$this->id && empty($this->data)) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
$data = $this->data;
|
||||||
|
if (empty($this->data)) {
|
||||||
|
$data = $this->read();
|
||||||
|
}
|
||||||
|
if (!$data['Person']['mother_id']) {
|
||||||
|
return null;
|
||||||
|
} else {
|
||||||
|
return array('Person' => array('id' => $data['Person']['mother_id']));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Acl Test User
|
||||||
|
*
|
||||||
|
* @package cake.tests
|
||||||
|
* @subpackage cake.tests.cases.libs.model.behaviors
|
||||||
|
*/
|
||||||
|
class AclUser extends CakeTestModel {
|
||||||
|
/**
|
||||||
|
* name property
|
||||||
|
*
|
||||||
|
* @var string
|
||||||
|
* @access public
|
||||||
|
*/
|
||||||
|
var $name = 'User';
|
||||||
|
/**
|
||||||
|
* useTable property
|
||||||
|
*
|
||||||
|
* @var string
|
||||||
|
* @access public
|
||||||
|
*/
|
||||||
|
var $useTable = 'users';
|
||||||
|
/**
|
||||||
|
* actsAs property
|
||||||
|
*
|
||||||
|
* @var array
|
||||||
|
* @access public
|
||||||
|
*/
|
||||||
|
var $actsAs = array('Acl');
|
||||||
|
/**
|
||||||
|
* parentNode
|
||||||
|
*
|
||||||
|
* @access public
|
||||||
|
*/
|
||||||
|
function parentNode() {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Acl Test User
|
||||||
|
*
|
||||||
|
* @package cake.tests
|
||||||
|
* @subpackage cake.tests.cases.libs.model.behaviors
|
||||||
|
*/
|
||||||
|
class AclPost extends CakeTestModel {
|
||||||
|
/**
|
||||||
|
* name property
|
||||||
|
*
|
||||||
|
* @var string
|
||||||
|
* @access public
|
||||||
|
*/
|
||||||
|
var $name = 'Post';
|
||||||
|
/**
|
||||||
|
* useTable property
|
||||||
|
*
|
||||||
|
* @var string
|
||||||
|
* @access public
|
||||||
|
*/
|
||||||
|
var $useTable = 'posts';
|
||||||
|
/**
|
||||||
|
* actsAs property
|
||||||
|
*
|
||||||
|
* @var array
|
||||||
|
* @access public
|
||||||
|
*/
|
||||||
|
var $actsAs = array('Acl' => 'controlled');
|
||||||
|
/**
|
||||||
|
* parentNode
|
||||||
|
*
|
||||||
|
* @access public
|
||||||
|
*/
|
||||||
|
function parentNode() {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* ACL behavior test class
|
||||||
|
*
|
||||||
|
* @package cake.tests
|
||||||
|
* @subpackage cake.tests.cases.libs.controller.components
|
||||||
|
*/
|
||||||
|
class AclBehaviorTestCase extends CakeTestCase {
|
||||||
|
var $fixtures = array('core.person', 'core.user', 'core.post', 'core.aco', 'core.aro', 'core.aros_aco');
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set up the test
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
**/
|
||||||
|
function startTest() {
|
||||||
|
Configure::write('Acl.database', 'test_suite');
|
||||||
|
|
||||||
|
$this->Aco = new Aco();
|
||||||
|
$this->Aro = new Aro();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test Setup of AclBehavior
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
**/
|
||||||
|
function testSetup() {
|
||||||
|
$User =& new AclUser();
|
||||||
|
$this->assertTrue(isset($User->Behaviors->Acl->settings['User']));
|
||||||
|
$this->assertEqual($User->Behaviors->Acl->settings['User']['type'], 'requester');
|
||||||
|
$this->assertTrue(is_object($User->Aro));
|
||||||
|
|
||||||
|
$Post =& new AclPost();
|
||||||
|
$this->assertTrue(isset($Post->Behaviors->Acl->settings['Post']));
|
||||||
|
$this->assertEqual($Post->Behaviors->Acl->settings['Post']['type'], 'controlled');
|
||||||
|
$this->assertTrue(is_object($Post->Aco));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* test After Save
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
**/
|
||||||
|
function testAfterSave() {
|
||||||
|
$Post =& new AclPost();
|
||||||
|
$data = array(
|
||||||
|
'Post' => array(
|
||||||
|
'author_id' => 1,
|
||||||
|
'title' => 'Acl Post',
|
||||||
|
'body' => 'post body',
|
||||||
|
'published' => 1
|
||||||
|
),
|
||||||
|
);
|
||||||
|
$Post->save($data);
|
||||||
|
$result = $this->Aco->find('first', array('conditions' => array('Aco.model' => 'Post', 'Aco.foreign_key' => $Post->id)));
|
||||||
|
$this->assertTrue(is_array($result));
|
||||||
|
$this->assertEqual($result['Aco']['model'], 'Post');
|
||||||
|
$this->assertEqual($result['Aco']['foreign_key'], $Post->id);
|
||||||
|
|
||||||
|
$aroData = array(
|
||||||
|
'Aro' => array(
|
||||||
|
'model' => 'Person',
|
||||||
|
'foreign_key' => 2,
|
||||||
|
'parent_id' => null
|
||||||
|
)
|
||||||
|
);
|
||||||
|
$this->Aro->save($aroData);
|
||||||
|
|
||||||
|
$Person =& new AclPerson();
|
||||||
|
$data = array(
|
||||||
|
'Person' => array(
|
||||||
|
'name' => 'Trent',
|
||||||
|
'mother_id' => 2,
|
||||||
|
'father_id' => 3,
|
||||||
|
),
|
||||||
|
);
|
||||||
|
$Person->save($data);
|
||||||
|
$result = $this->Aro->find('first', array('conditions' => array('Aro.model' => 'Person', 'Aro.foreign_key' => $Person->id)));
|
||||||
|
$this->assertTrue(is_array($result));
|
||||||
|
$this->assertEqual($result['Aro']['parent_id'], 5);
|
||||||
|
|
||||||
|
$node = $Person->node(array('model' => 'Person', 'foreign_key' => 8));
|
||||||
|
$this->assertEqual(sizeof($node), 2);
|
||||||
|
$this->assertEqual($node[0]['Aro']['parent_id'], 5);
|
||||||
|
$this->assertEqual($node[1]['Aro']['parent_id'], null);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test After Delete
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
**/
|
||||||
|
function testAfterDelete() {
|
||||||
|
$aroData = array(
|
||||||
|
'Aro' => array(
|
||||||
|
'model' => 'Person',
|
||||||
|
'foreign_key' => 2,
|
||||||
|
'parent_id' => null
|
||||||
|
)
|
||||||
|
);
|
||||||
|
$this->Aro->save($aroData);
|
||||||
|
|
||||||
|
$Person =& new AclPerson();
|
||||||
|
$data = array(
|
||||||
|
'Person' => array(
|
||||||
|
'name' => 'Trent',
|
||||||
|
'mother_id' => 2,
|
||||||
|
'father_id' => 3,
|
||||||
|
),
|
||||||
|
);
|
||||||
|
$Person->save($data);
|
||||||
|
|
||||||
|
$id = $Person->id;
|
||||||
|
$result = $this->Aro->find('first', array('conditions' => array('Aro.model' => 'Person', 'Aro.foreign_key' => $id)));
|
||||||
|
$this->assertTrue(is_array($result));
|
||||||
|
|
||||||
|
$Person->delete($id);
|
||||||
|
$result = $this->Aro->find('first', array('conditions' => array('Aro.model' => 'Person', 'Aro.foreign_key' => $id)));
|
||||||
|
$this->assertTrue(empty($result));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test Node()
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
**/
|
||||||
|
function testNode() {
|
||||||
|
$Person =& new AclPerson();
|
||||||
|
$aroData = array(
|
||||||
|
'Aro' => array(
|
||||||
|
'model' => 'Person',
|
||||||
|
'foreign_key' => 2,
|
||||||
|
'parent_id' => null
|
||||||
|
)
|
||||||
|
);
|
||||||
|
$this->Aro->save($aroData);
|
||||||
|
|
||||||
|
$Person->id = 2;
|
||||||
|
$result = $Person->node();
|
||||||
|
$this->assertTrue(is_array($result));
|
||||||
|
$this->assertEqual(sizeof($result), 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* tear down test
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
**/
|
||||||
|
function tearDown() {
|
||||||
|
ClassRegistry::flush();
|
||||||
|
unset($this->Aro, $this->Aco);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
?>
|
Loading…
Add table
Reference in a new issue