From d111a14bbb1d973d715c9c4575e94e2bb7905d0c Mon Sep 17 00:00:00 2001 From: QuickApps Date: Sun, 23 Mar 2014 01:49:00 +0100 Subject: [PATCH] reverting the` Table::get()` thing, new test cases added --- src/Model/Behavior/TreeBehavior.php | 36 ++++++++++++++++--- .../Model/Behavior/TreeBehaviorTest.php | 25 ++++++++++++- 2 files changed, 55 insertions(+), 6 deletions(-) diff --git a/src/Model/Behavior/TreeBehavior.php b/src/Model/Behavior/TreeBehavior.php index 6f6b9f8425d..89c97b911a6 100644 --- a/src/Model/Behavior/TreeBehavior.php +++ b/src/Model/Behavior/TreeBehavior.php @@ -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)) { @@ -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([ @@ -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]]); @@ -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]]); diff --git a/tests/TestCase/Model/Behavior/TreeBehaviorTest.php b/tests/TestCase/Model/Behavior/TreeBehaviorTest.php index a1d05477646..91b553d42e6 100644 --- a/tests/TestCase/Model/Behavior/TreeBehaviorTest.php +++ b/tests/TestCase/Model/Behavior/TreeBehaviorTest.php @@ -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() { @@ -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 * @@ -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); + } }