From 460223b61adad04bd03ec13ec24a9bf6faa6f31a Mon Sep 17 00:00:00 2001 From: AD7six Date: Mon, 11 Aug 2008 15:54:54 +0000 Subject: [PATCH] fixes #5203 - removeFromTree should return after deleting a node with no children. Also modified removeFromTree to set the parent_id to null if removeFromTree is called on a node with no children and delete is set to false git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@7453 3807eeeb-6ff5-0310-8944-8be069107fe0 --- cake/libs/model/behaviors/tree.php | 8 +-- .../cases/libs/model/behaviors/tree.test.php | 65 +++++++++++++++++++ 2 files changed, 69 insertions(+), 4 deletions(-) diff --git a/cake/libs/model/behaviors/tree.php b/cake/libs/model/behaviors/tree.php index e008ff288..a1c06b52a 100644 --- a/cake/libs/model/behaviors/tree.php +++ b/cake/libs/model/behaviors/tree.php @@ -649,9 +649,10 @@ class TreeBehavior extends ModelBehavior { if ($node[$right] == $node[$left] + 1) { if ($delete) { - $model->delete(); + return $model->delete($id); } else { - return false; + $model->id = $id; + return $model->saveField($parent, null); } } elseif ($node[$parent]) { list($parentNode) = array_values($model->find('first', array( @@ -907,5 +908,4 @@ class TreeBehavior extends ModelBehavior { $model->recursive = $modelRecursive; } } - -?> +?> \ No newline at end of file diff --git a/cake/tests/cases/libs/model/behaviors/tree.test.php b/cake/tests/cases/libs/model/behaviors/tree.test.php index d419b915b..51f0020db 100644 --- a/cake/tests/cases/libs/model/behaviors/tree.test.php +++ b/cake/tests/cases/libs/model/behaviors/tree.test.php @@ -948,6 +948,39 @@ class NumberTreeCase extends CakeTestCase { $this->assertIdentical($validTree, true); } /** + * testRemoveNoChildren method + * + * @return void + * @access public + */ + function testRemoveNoChildren() { + $this->NumberTree =& new NumberTree(); + $this->NumberTree->initialize(2, 2); + $initialCount = $this->NumberTree->find('count'); + + $result = $this->NumberTree->findByName('1.1.1'); + $this->NumberTree->removeFromTree($result['NumberTree']['id']); + + $laterCount = $this->NumberTree->find('count'); + $this->assertEqual($initialCount, $laterCount); + + $nodes = $this->NumberTree->find('list', array('order' => 'lft')); + $expects = array( + 1 => '1. Root', + 2 => '1.1', + 4 => '1.1.2', + 5 => '1.2', + 6 => '1.2.1', + 7 => '1.2.2', + 3 => '1.1.1', + ); + + $this->assertEqual($nodes, $expects); + + $validTree = $this->NumberTree->verify(); + $this->assertIdentical($validTree, true); + } +/** * testRemoveAndDelete method * * @access public @@ -978,6 +1011,38 @@ class NumberTreeCase extends CakeTestCase { $validTree = $this->NumberTree->verify(); $this->assertIdentical($validTree, true); } +/** + * testRemoveAndDeleteNoChildren method + * + * @return void + * @access public + */ + function testRemoveAndDeleteNoChildren() { + $this->NumberTree =& new NumberTree(); + $this->NumberTree->initialize(2, 2); + $initialCount = $this->NumberTree->find('count'); + + $result = $this->NumberTree->findByName('1.1.1'); + $this->NumberTree->removeFromTree($result['NumberTree']['id'], true); + + $laterCount = $this->NumberTree->find('count'); + $this->assertEqual($initialCount - 1, $laterCount); + + $nodes = $this->NumberTree->find('list', array('order' => 'lft')); + $expects = array( + 1 => '1. Root', + 2 => '1.1', + 4 => '1.1.2', + 5 => '1.2', + 6 => '1.2.1', + 7 => '1.2.2', + ); + $this->assertEqual($nodes, $expects); + + $validTree = $this->NumberTree->verify(); + $this->assertIdentical($validTree, true); + } + /** * testChildren method *