mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2024-11-15 03:18:26 +00:00
Refactoring out Model.fk parsing. Updating create(). Adding tests.
This commit is contained in:
parent
3aa6d22c2c
commit
449c390d48
2 changed files with 74 additions and 27 deletions
|
@ -137,25 +137,16 @@ class AclShell extends Shell {
|
|||
* @access public
|
||||
*/
|
||||
function create() {
|
||||
|
||||
$this->_checkArgs(3, 'create');
|
||||
$this->checkNodeType();
|
||||
extract($this->__dataVars());
|
||||
|
||||
$class = ucfirst($this->args[0]);
|
||||
$object = new $class();
|
||||
|
||||
if (preg_match('/^([\w]+)\.(.*)$/', $this->args[1], $matches) && count($matches) == 3) {
|
||||
$parent = array(
|
||||
'model' => $matches[1],
|
||||
'foreign_key' => $matches[2],
|
||||
);
|
||||
} else {
|
||||
$parent = $this->args[1];
|
||||
}
|
||||
$object = ClassRegistry::init($class);
|
||||
$parent = $this->parseIdentifier($this->args[1]);
|
||||
|
||||
if (!empty($parent) && $parent != '/' && $parent != 'root') {
|
||||
@$parent = $object->node($parent);
|
||||
$parent = $object->node($parent);
|
||||
if (empty($parent)) {
|
||||
$this->err(sprintf(__('Could not find parent node using reference "%s"', true), $this->args[1]));
|
||||
return;
|
||||
|
@ -166,22 +157,15 @@ class AclShell extends Shell {
|
|||
$parent = null;
|
||||
}
|
||||
|
||||
if (preg_match('/^([\w]+)\.(.*)$/', $this->args[2], $matches) && count($matches) == 3) {
|
||||
$data = array(
|
||||
'model' => $matches[1],
|
||||
'foreign_key' => $matches[2],
|
||||
);
|
||||
} else {
|
||||
if (!($this->args[2] == '/')) {
|
||||
$data = array('alias' => $this->args[2]);
|
||||
} else {
|
||||
$this->error(__('/ can not be used as an alias!', true), __('\t/ is the root, please supply a sub alias', true));
|
||||
}
|
||||
$data = $this->parseIdentifier($this->args[2]);
|
||||
if (is_string($data) && !$data == '/') {
|
||||
$data = array('alias' => $data);
|
||||
} else if (is_string($data)) {
|
||||
$this->error(__('/ can not be used as an alias!', true), __('\t/ is the root, please supply a sub alias', true));
|
||||
}
|
||||
|
||||
$data['parent_id'] = $parent;
|
||||
$object->create();
|
||||
|
||||
if ($object->save($data)) {
|
||||
$this->out(sprintf(__("New %s '%s' created.\n", true), $class, $this->args[2]), true);
|
||||
} else {
|
||||
|
@ -486,6 +470,23 @@ class AclShell extends Shell {
|
|||
return $possibility;
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse an identifier into Model.foriegnKey or an alias.
|
||||
* Takes an identifier determines its type and returns the result as used by other methods.
|
||||
*
|
||||
* @param string $identifier Identifier to parse
|
||||
* @return mixed a string for aliases, and an array for model.foreignKey
|
||||
**/
|
||||
function parseIdentifier($identifier) {
|
||||
if (preg_match('/^([\w]+)\.(.*)$/', $identifier, $matches)) {
|
||||
return array(
|
||||
'model' => $matches[1],
|
||||
'foreign_key' => $matches[2],
|
||||
);
|
||||
}
|
||||
return $identifier;
|
||||
}
|
||||
|
||||
/**
|
||||
* get params for standard Acl methods
|
||||
*
|
||||
|
@ -533,7 +534,7 @@ class AclShell extends Shell {
|
|||
}
|
||||
$vars = array();
|
||||
$class = ucwords($type);
|
||||
$vars['secondary_id'] = ife(strtolower($class) == 'aro', 'foreign_key', 'object_id');
|
||||
$vars['secondary_id'] = (strtolower($class) == 'aro') ? 'foreign_key' : 'object_id';
|
||||
$vars['data_name'] = $type;
|
||||
$vars['table_name'] = $type . 's';
|
||||
$vars['class'] = $class;
|
||||
|
|
|
@ -49,6 +49,8 @@ Mock::generatePartial(
|
|||
array('in', 'out', 'hr', 'createFile')
|
||||
);
|
||||
|
||||
Mock::generate('AclComponent', 'MockAclShellAclComponent');
|
||||
|
||||
/**
|
||||
* AclShellTest class
|
||||
*
|
||||
|
@ -82,7 +84,7 @@ class AclShellTest extends CakeTestCase {
|
|||
}
|
||||
|
||||
/**
|
||||
* setUp method
|
||||
* startTest method
|
||||
*
|
||||
* @return void
|
||||
* @access public
|
||||
|
@ -95,7 +97,7 @@ class AclShellTest extends CakeTestCase {
|
|||
}
|
||||
|
||||
/**
|
||||
* tearDown method
|
||||
* endTest method
|
||||
*
|
||||
* @return void
|
||||
* @access public
|
||||
|
@ -128,5 +130,49 @@ class AclShellTest extends CakeTestCase {
|
|||
|
||||
$this->Task->view();
|
||||
}
|
||||
/**
|
||||
* test the method that splits model.foreign key. and that it returns an array.
|
||||
*
|
||||
* @return void
|
||||
**/
|
||||
function testParsingModelAndForeignKey() {
|
||||
$result = $this->Task->parseIdentifier('Model.foreignKey');
|
||||
$expected = array('model' => 'Model', 'foreign_key' => 'foreignKey');
|
||||
|
||||
$result = $this->Task->parseIdentifier('mySuperUser');
|
||||
$this->assertEqual($result, 'mySuperUser');
|
||||
|
||||
$result = $this->Task->parseIdentifier('111234');
|
||||
$this->assertEqual($result, '111234');
|
||||
}
|
||||
|
||||
/**
|
||||
* test creating aro/aco nodes
|
||||
*
|
||||
* @return void
|
||||
**/
|
||||
function testCreate() {
|
||||
$this->Task->args = array('aro', 'root', 'User.1');
|
||||
$this->Task->expectAt(0, 'out', array(new PatternExpectation('/created/'), '*'));
|
||||
$this->Task->create();
|
||||
|
||||
$Aro =& ClassRegistry::init('Aro');
|
||||
$Aro->cacheQueries = false;
|
||||
$result = $Aro->read();
|
||||
$this->assertEqual($result['Aro']['model'], 'User');
|
||||
$this->assertEqual($result['Aro']['foreign_key'], 1);
|
||||
$this->assertEqual($result['Aro']['parent_id'], null);
|
||||
$id = $result['Aro']['id'];
|
||||
|
||||
$this->Task->args = array('aro', 'User.1', 'User.3');
|
||||
$this->Task->expectAt(1, 'out', array(new PatternExpectation('/created/'), '*'));
|
||||
$this->Task->create();
|
||||
|
||||
$Aro =& ClassRegistry::init('Aro');
|
||||
$result = $Aro->read();
|
||||
$this->assertEqual($result['Aro']['model'], 'User');
|
||||
$this->assertEqual($result['Aro']['foreign_key'], 3);
|
||||
$this->assertEqual($result['Aro']['parent_id'], $id);
|
||||
}
|
||||
}
|
||||
?>
|
Loading…
Reference in a new issue