Skip to content

Commit

Permalink
update fo test cases. and some code refactoring
Browse files Browse the repository at this point in the history
find('children') will throw when node was not found
  • Loading branch information
quickapps authored and lorenzo committed Mar 29, 2014
1 parent b6e93e0 commit 7bdcb0a
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 34 deletions.
37 changes: 8 additions & 29 deletions src/Model/Behavior/TreeBehavior.php
Expand Up @@ -112,12 +112,12 @@ public function childCount($id, $direct = false) {
* If false is passed for the id parameter, top level, or all (depending on direct parameter appropriate) are counted.
*
* @param array $options Array of options as described above
* @throws \Cake\ORM\Error\RecordNotFoundException When node was not found
* @return \Cake\ORM\Query
*/
public function findChildren($query, $options) {
extract($this->config());
extract($options);
$primaryKey = $this->_table->primaryKey();
$direct = !isset($direct) ? false : $direct;

if (empty($for)) {
Expand All @@ -129,18 +129,10 @@ public function findChildren($query, $options) {
}

if ($direct) {
return $this->_scope($query)
->where([$parent => $for]);
return $this->_scope($query)->where([$parent => $for]);
}

$node = $this->_scope($this->_table->find())
->select([$right, $left])
->where([$primaryKey => $for])
->first();

if (!$node) {
return $query;
}
$node = $this->_table->get($for, ['fields' => [$right, $left]]);

return $this->_scope($query)
->where([
Expand All @@ -159,32 +151,25 @@ public function findChildren($query, $options) {
* @return boolean true on success, false on failure
*/
public function moveUp($id, $number = 1) {
$primaryKey = $this->_table->primaryKey();
$config = $this->config();
extract($config);

if (!$number) {
return false;
}

$node = $this->_scope($this->_table->find())
->select([$primaryKey, $parent, $left, $right])
->where([$primaryKey => $id])
->first();
$node = $this->_table->get($id, ['fields' => [$parent, $left, $right]]);

if ($node->{$parent}) {
$parentNode = $this->_scope($this->_table->find())
->select([$primaryKey, $left, $right])
->where([$primaryKey => $node->{$parent}])
->first();
$parentNode = $this->_table->get($node->{$parent}, ['fields' => [$left, $right]]);

if (($node->{$left} - 1) == $parentNode->{$left}) {
return false;
}
}

$previousNode = $this->_scope($this->_table->find())
->select([$primaryKey, $left, $right])
->select([$left, $right])
->where([$right => ($node->{$left} - 1)])
->first();

Expand Down Expand Up @@ -226,16 +211,10 @@ public function moveDown($id, $number = 1) {
return false;
}

$node = $this->_scope($this->_table->find())
->select([$primaryKey, $parent, $left, $right])
->where([$primaryKey => $id])
->first();
$node = $this->_table->get($id, ['fields' => [$left, $right]]);

if ($node->{$parent}) {
$parentNode = $this->_scope($this->_table->find())
->select([$primaryKey, $left, $right])
->where([$primaryKey => $node->{$parent}])
->first();
$parentNode = $this->_table->get($node->{$parent}, ['fields' => [$left, $right]]);

if (($node->{$right} + 1) == $parentNode->{$right}) {
return false;
Expand Down
19 changes: 14 additions & 5 deletions tests/TestCase/Model/Behavior/TreeBehaviorTest.php
Expand Up @@ -121,7 +121,7 @@ public function testCallableScoping() {
}

/**
* Tests the children() method
* Tests the find('children') method
*
* @return void
*/
Expand All @@ -134,10 +134,6 @@ public function testFindChildren() {
$nodes = $table->find('children', ['for' => 1])->all();
$this->assertEquals([2, 3, 4, 5], $nodes->extract('id')->toArray());

// unexisting node
$query = $table->find('children', ['for' => 500]);
$this->assertEquals($query, $table->find('children', ['for' => 500]));

// leaf
$nodeIds = [];
$nodes = $table->find('children', ['for' => 5])->all();
Expand All @@ -148,6 +144,19 @@ public function testFindChildren() {
$this->assertEquals([2, 3], $nodes->extract('id')->toArray());
}

/**
* Tests that find('children') will throw an exception if the node was not found
*
* @expectedException \Cake\ORM\Error\RecordNotFoundException
* @expectedExceptionMessage Record "500" not found in table "menu_link_trees"
* @return void
*/
public function testFindChildrenException() {
$table = TableRegistry::get('MenuLinkTrees');
$table->addBehavior('Tree', ['scope' => ['menu' => 'main-menu']]);
$query = $table->find('children', ['for' => 500]);
}

/**
* Tests the moveUp() method
*
Expand Down

0 comments on commit 7bdcb0a

Please sign in to comment.