Skip to content

Commit

Permalink
reverting the Table::get() thing, new test cases added
Browse files Browse the repository at this point in the history
  • Loading branch information
quickapps authored and lorenzo committed Mar 29, 2014
1 parent f5f5450 commit d111a14
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 6 deletions.
36 changes: 31 additions & 5 deletions src/Model/Behavior/TreeBehavior.php
Expand Up @@ -118,6 +118,7 @@ public function childCount($id, $direct = false) {
public function findChildren($query, $options) {
extract($this->config());
extract($options);
$primaryKey = $this->_table->primaryKey();
$direct = !isset($direct) ? false : $direct;

if (empty($for)) {
Expand All @@ -132,7 +133,14 @@ public function findChildren($query, $options) {
return $this->_scope($query)->where([$parent => $for]);
}

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

if (!$node) {
throw new \Cake\ORM\Error\RecordNotFoundException("Node \"{$for}\ was not found in the tree.");
}

return $this->_scope($query)
->where([
Expand All @@ -147,17 +155,26 @@ public function findChildren($query, $options) {
* If the node is the first child, or is a top level node with no previous node this method will return false
*
* @param integer|string $id The ID of the record to move
* @param integer|boolean $number how many places to move the node, or true to move to first position
* @param integer|boolean $number How many places to move the node, or true to move to first position
* @throws \Cake\ORM\Error\RecordNotFoundException When node was not found
* @return boolean true on success, false on failure
*/
public function moveUp($id, $number = 1) {
extract($this->config());
$primaryKey = $this->_table->primaryKey();

if (!$number) {
return false;
}

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

if (!$node) {
throw new \Cake\ORM\Error\RecordNotFoundException("Node \"{$id}\" was not found in the tree.");
}

if ($node->{$parent}) {
$parentNode = $this->_table->get($node->{$parent}, ['fields' => [$left, $right]]);
Expand Down Expand Up @@ -198,17 +215,26 @@ public function moveUp($id, $number = 1) {
* If the node is the last child, or is a top level node with no subsequent node this method will return false
*
* @param integer|string $id The ID of the record to move
* @param integer|boolean $number how many places to move the node or true to move to last position
* @param integer|boolean $number How many places to move the node or true to move to last position
* @throws \Cake\ORM\Error\RecordNotFoundException When node was not found
* @return boolean true on success, false on failure
*/
public function moveDown($id, $number = 1) {
extract($this->config());
$primaryKey = $this->_table->primaryKey();

if (!$number) {
return false;
}

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

if (!$node) {
throw new \Cake\ORM\Error\RecordNotFoundException("Node \"{$id}\" was not found in the tree.");
}

if ($node->{$parent}) {
$parentNode = $this->_table->get($node->{$parent}, ['fields' => [$left, $right]]);
Expand Down
25 changes: 24 additions & 1 deletion tests/TestCase/Model/Behavior/TreeBehaviorTest.php
Expand Up @@ -148,7 +148,6 @@ public function testFindChildren() {
* 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() {
Expand Down Expand Up @@ -193,6 +192,18 @@ public function testMoveUp() {
$this->assertEquals([8, 1, 6], $nodes->extract('id')->toArray());
}

/**
* Tests that moveUp() will throw an exception if the node was not found
*
* @expectedException \Cake\ORM\Error\RecordNotFoundException
* @return void
*/
public function testMoveUpException() {
$table = TableRegistry::get('MenuLinkTrees');
$table->addBehavior('Tree', ['scope' => ['menu' => 'main-menu']]);
$table->moveUp(500, 1);
}

/**
* Tests the moveDown() method
*
Expand Down Expand Up @@ -228,4 +239,16 @@ public function testMoveDown() {
->all();
$this->assertEquals([6, 8, 1], $nodes->extract('id')->toArray());
}

/**
* Tests that moveDown() will throw an exception if the node was not found
*
* @expectedException \Cake\ORM\Error\RecordNotFoundException
* @return void
*/
public function testMoveDownException() {
$table = TableRegistry::get('MenuLinkTrees');
$table->addBehavior('Tree', ['scope' => ['menu' => 'main-menu']]);
$table->moveDown(500, 1);
}
}

0 comments on commit d111a14

Please sign in to comment.