Skip to content

Commit

Permalink
Updating delete() and adding test cases. Refactoring out common methods.
Browse files Browse the repository at this point in the history
  • Loading branch information
markstory committed Aug 2, 2009
1 parent 449c390 commit acaba02
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 16 deletions.
44 changes: 29 additions & 15 deletions cake/console/libs/acl.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,31 +142,24 @@ function create() {
extract($this->__dataVars());

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

if (!empty($parent) && $parent != '/' && $parent != 'root') {
$parent = $object->node($parent);
if (empty($parent)) {
$this->err(sprintf(__('Could not find parent node using reference "%s"', true), $this->args[1]));
return;
} else {
$parent = Set::extract($parent, "0.{$class}.id");
}
$parent = $this->_getNodeId($class, $parent);
} else {
$parent = null;
}

$data = $this->parseIdentifier($this->args[2]);
if (is_string($data) && !$data == '/') {
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));
} elseif (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->Acl->{$class}->create();
if ($this->Acl->{$class}->save($data)) {
$this->out(sprintf(__("New %s '%s' created.\n", true), $class, $this->args[2]), true);
} else {
$this->err(sprintf(__("There was a problem creating a new %s '%s'.", true), $class, $this->args[2]));
Expand All @@ -182,7 +175,11 @@ function delete() {
$this->_checkArgs(2, 'delete');
$this->checkNodeType();
extract($this->__dataVars());
if (!$this->Acl->{$class}->delete($this->args[1])) {

$identifier = $this->parseIdentifier($this->args[1]);
$nodeId = $this->_getNodeId($class, $identifier);

if (!$this->Acl->{$class}->delete($nodeId)) {
$this->error(__("Node Not Deleted", true), sprintf(__("There was an error deleting the %s. Check that the node exists", true), $class) . ".\n");
}
$this->out(sprintf(__("%s deleted", true), $class) . ".\n", true);
Expand Down Expand Up @@ -461,7 +458,7 @@ function nodeExists() {
return false;
}
extract($this->__dataVars($this->args[0]));
$key = (ife(is_numeric($this->args[1]), $secondary_id, 'alias'));
$key = is_numeric($this->args[1]) ? $secondary_id : 'alias';
$conditions = array($class . '.' . $key => $this->args[1]);
$possibility = $this->Acl->{$class}->find('all', compact('conditions'));
if (empty($possibility)) {
Expand All @@ -487,6 +484,23 @@ function parseIdentifier($identifier) {
return $identifier;
}

/**
* Get the node for a given identifier. $identifier can either be a string alias
* or an array of properties to use in AcoNode::node()
*
* @param string $class Class type you want (Aro/Aco)
* @param mixed $identifier A mixed identifier for finding the node.
* @return int Integer of NodeId. Will trigger an error if nothing is found.
**/
function _getNodeId($class, $identifier) {
$node = $this->Acl->{$class}->node($identifier);
if (empty($node)) {
$this->error(sprintf(__('Could not find node using reference "%s"', true), $identifier));
return;
}
return Set::extract($node, "0.{$class}.id");
}

/**
* get params for standard Acl methods
*
Expand Down
31 changes: 30 additions & 1 deletion cake/tests/cases/console/libs/acl.test.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
);
Mock::generatePartial(
'AclShell', 'MockAclShell',
array('in', 'out', 'hr', 'createFile')
array('in', 'out', 'hr', 'createFile', 'error', 'err')
);

Mock::generate('AclComponent', 'MockAclShellAclComponent');
Expand Down Expand Up @@ -94,6 +94,9 @@ function startTest() {
$this->Task =& new MockAclShell($this->Dispatcher);
$this->Task->Dispatch = new $this->Dispatcher;
$this->Task->params['datasource'] = 'test_suite';
$this->Task->Acl =& new AclComponent();
$controller = null;
$this->Task->Acl->startup($controller);
}

/**
Expand Down Expand Up @@ -173,6 +176,32 @@ function testCreate() {
$this->assertEqual($result['Aro']['model'], 'User');
$this->assertEqual($result['Aro']['foreign_key'], 3);
$this->assertEqual($result['Aro']['parent_id'], $id);

$this->Task->args = array('aro', 'root', 'somealias');
$this->Task->expectAt(2, 'out', array(new PatternExpectation('/created/'), '*'));
$this->Task->create();

$Aro =& ClassRegistry::init('Aro');
$result = $Aro->read();
$this->assertEqual($result['Aro']['alias'], 'somealias');
$this->assertEqual($result['Aro']['model'], null);
$this->assertEqual($result['Aro']['foreign_key'], null);
$this->assertEqual($result['Aro']['parent_id'], null);
}

/**
* test the delete method with different node types.
*
* @return void
**/
function testDelete() {
$this->Task->args = array('aro', 'AuthUser.1');
$this->Task->expectAt(0, 'out', array(new NoPatternExpectation('/not/'), true));
$this->Task->delete();

$Aro =& ClassRegistry::init('Aro');
$result = $Aro->read(null, 3);
$this->assertFalse($result);
}
}
?>

0 comments on commit acaba02

Please sign in to comment.