diff --git a/cake/libs/model/behaviors/tree.php b/cake/libs/model/behaviors/tree.php index 844db147e..8cf3799c8 100644 --- a/cake/libs/model/behaviors/tree.php +++ b/cake/libs/model/behaviors/tree.php @@ -769,6 +769,9 @@ class TreeBehavior extends ModelBehavior { if (is_null($instance[$model->alias][$left]) || is_null($instance[$model->alias][$right])) { $errors[] = array('node', $instance[$model->alias][$model->primaryKey], 'has invalid left or right values'); + } elseif ($instance[$model->alias][$left] == $instance[$model->alias][$right]){ + $errors[] = array('node', $instance[$model->alias][$model->primaryKey], + 'left and right values identical'); } elseif ($instance[$model->alias][$parent]) { if (!$instance['VerifyParent'][$model->primaryKey]) { $errors[] = array('node', $instance[$model->alias][$model->primaryKey], diff --git a/cake/tests/cases/libs/model/behaviors/tree.test.php b/cake/tests/cases/libs/model/behaviors/tree.test.php index e33dec56a..6bf12aeb6 100644 --- a/cake/tests/cases/libs/model/behaviors/tree.test.php +++ b/cake/tests/cases/libs/model/behaviors/tree.test.php @@ -415,6 +415,30 @@ class NumberTreeCase extends CakeTestCase { $result = $this->NumberTree->verify(); $this->assertIdentical($result, true); } +/** + * Reproduces a situation where a single node has lft=rght, and all other lft and rght fields follow sequentially + * + * @access public + * @return void + */ + function testDetectEqualLftsRghts() { + $this->NumberTree =& new NumberTree(); + $this->NumberTree->initialize(1, 3); + + $result = $this->NumberTree->findByName('1.1'); + $this->NumberTree->updateAll(array('rght' => $result['NumberTree']['lft']), array('id' => $result['NumberTree']['id'])); + $this->NumberTree->updateAll(array('lft' => 'lft-1'), array('lft >' => $result['NumberTree']['lft'])); + $this->NumberTree->updateAll(array('rght' => 'rght-1'), array('rght >' => $result['NumberTree']['lft'])); + + $result = $this->NumberTree->verify(); + $this->assertNotIdentical($result, true); + + $result = $this->NumberTree->recover(); + $this->assertTrue($result); + + $result = $this->NumberTree->verify(); + $this->assertTrue($result); + } /** * testAddOrphan method *