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
This commit is contained in:
AD7six 2008-08-11 15:54:54 +00:00
parent 8cb73ead3c
commit 460223b61a
2 changed files with 69 additions and 4 deletions

View file

@ -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;
}
}
?>
?>

View file

@ -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
*