Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Refactoring out Model.fk parsing. Updating create(). Adding tests.

  • Loading branch information...
commit 449c390d48e9d3a85a84d4f192850f95222f7188 1 parent 3aa6d22
@markstory markstory authored
Showing with 74 additions and 27 deletions.
  1. +26 −25 cake/console/libs/acl.php
  2. +48 −2 cake/tests/cases/console/libs/acl.test.php
View
51 cake/console/libs/acl.php
@@ -137,25 +137,16 @@ function main() {
* @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 @@ function create() {
$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 {
@@ -487,6 +471,23 @@ function nodeExists() {
}
/**
+ * 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
*
* @return array aro, aco, action
@@ -533,7 +534,7 @@ function __dataVars($type = null) {
}
$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;
View
50 cake/tests/cases/console/libs/acl.test.php
@@ -49,6 +49,8 @@
array('in', 'out', 'hr', 'createFile')
);
+Mock::generate('AclComponent', 'MockAclShellAclComponent');
+
/**
* AclShellTest class
*
@@ -82,7 +84,7 @@ function endCase() {
}
/**
- * setUp method
+ * startTest method
*
* @return void
* @access public
@@ -95,7 +97,7 @@ function startTest() {
}
/**
- * tearDown method
+ * endTest method
*
* @return void
* @access public
@@ -128,5 +130,49 @@ function testViewWithModelForeignKeyOutput() {
$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);
+ }
}
?>
Please sign in to comment.
Something went wrong with that request. Please try again.