Skip to content

Commit

Permalink
Refactoring out Model.fk parsing. Updating create(). Adding tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
markstory committed Aug 1, 2009
1 parent 3aa6d22 commit 449c390
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 27 deletions.
51 changes: 26 additions & 25 deletions cake/console/libs/acl.php
Expand Up @@ -137,25 +137,16 @@ function main() {
* @access public * @access public
*/ */
function create() { function create() {

$this->_checkArgs(3, 'create'); $this->_checkArgs(3, 'create');
$this->checkNodeType(); $this->checkNodeType();
extract($this->__dataVars()); extract($this->__dataVars());


$class = ucfirst($this->args[0]); $class = ucfirst($this->args[0]);
$object = new $class(); $object = ClassRegistry::init($class);

$parent = $this->parseIdentifier($this->args[1]);
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];
}


if (!empty($parent) && $parent != '/' && $parent != 'root') { if (!empty($parent) && $parent != '/' && $parent != 'root') {
@$parent = $object->node($parent); $parent = $object->node($parent);
if (empty($parent)) { if (empty($parent)) {
$this->err(sprintf(__('Could not find parent node using reference "%s"', true), $this->args[1])); $this->err(sprintf(__('Could not find parent node using reference "%s"', true), $this->args[1]));
return; return;
Expand All @@ -166,22 +157,15 @@ function create() {
$parent = null; $parent = null;
} }


if (preg_match('/^([\w]+)\.(.*)$/', $this->args[2], $matches) && count($matches) == 3) { $data = $this->parseIdentifier($this->args[2]);
$data = array( if (is_string($data) && !$data == '/') {
'model' => $matches[1], $data = array('alias' => $data);
'foreign_key' => $matches[2], } 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));
} 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['parent_id'] = $parent; $data['parent_id'] = $parent;
$object->create(); $object->create();

if ($object->save($data)) { if ($object->save($data)) {
$this->out(sprintf(__("New %s '%s' created.\n", true), $class, $this->args[2]), true); $this->out(sprintf(__("New %s '%s' created.\n", true), $class, $this->args[2]), true);
} else { } else {
Expand Down Expand Up @@ -486,6 +470,23 @@ function nodeExists() {
return $possibility; 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 * get params for standard Acl methods
* *
Expand Down Expand Up @@ -533,7 +534,7 @@ function __dataVars($type = null) {
} }
$vars = array(); $vars = array();
$class = ucwords($type); $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['data_name'] = $type;
$vars['table_name'] = $type . 's'; $vars['table_name'] = $type . 's';
$vars['class'] = $class; $vars['class'] = $class;
Expand Down
50 changes: 48 additions & 2 deletions cake/tests/cases/console/libs/acl.test.php
Expand Up @@ -49,6 +49,8 @@
array('in', 'out', 'hr', 'createFile') array('in', 'out', 'hr', 'createFile')
); );


Mock::generate('AclComponent', 'MockAclShellAclComponent');

/** /**
* AclShellTest class * AclShellTest class
* *
Expand Down Expand Up @@ -82,7 +84,7 @@ function endCase() {
} }


/** /**
* setUp method * startTest method
* *
* @return void * @return void
* @access public * @access public
Expand All @@ -95,7 +97,7 @@ function startTest() {
} }


/** /**
* tearDown method * endTest method
* *
* @return void * @return void
* @access public * @access public
Expand Down Expand Up @@ -128,5 +130,49 @@ function testViewWithModelForeignKeyOutput() {


$this->Task->view(); $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);
}
} }
?> ?>

0 comments on commit 449c390

Please sign in to comment.