Permalink
Browse files

Fixed TreeBehavior::recover() when using scope. Fixes #4062.

  • Loading branch information...
1 parent e690c93 commit 1d89ee1c6df0781e72e769ba1c829c897e22b8c3 @ADmad ADmad committed Sep 18, 2013
@@ -676,7 +676,7 @@ protected function _recoverByParentId(Model $Model, $counter = 1, $parentId = nu
$scope = $this->settings[$Model->alias]['scope'];
if ($scope && ($scope !== '1 = 1' && $scope !== true)) {
- $conditions[] = $scope;
+ $params['conditions'][] = $scope;
}
$children = $Model->find('all', $params);
@@ -380,4 +380,185 @@ public function testGenerateTreeListWithScope() {
$this->assertEquals($expected, $result);
}
+/**
+ * testRecoverUsingParentMode method
+ *
+ * @return void
+ */
+ public function testRecoverUsingParentMode() {
+ extract($this->settings);
+ $this->Tree = new $modelClass();
+ $this->Tree->order = null;
+ $this->Tree->initialize(2, 3);
+
+ $this->Tree->Behaviors->attach('Tree', array('scope' => 'FlagTree.flag = 1'));
+ $this->Tree->Behaviors->disable('Tree');
+
+ $this->Tree->save(array('name' => 'Main', $parentField => null, $leftField => 0, $rightField => 0, 'flag' => 1));
+ $node1 = $this->Tree->id;
+
+ $this->Tree->create();
+ $this->Tree->save(array('name' => 'About Us', $parentField => $node1, $leftField => 0, $rightField => 0, 'flag' => 1));
+ $node11 = $this->Tree->id;
+ $this->Tree->create();
+ $this->Tree->save(array('name' => 'Programs', $parentField => $node1, $leftField => 0, $rightField => 0, 'flag' => 1));
+ $node12 = $this->Tree->id;
+ $this->Tree->create();
+ $this->Tree->save(array('name' => 'Mission and History', $parentField => $node11, $leftField => 0, $rightField => 0, 'flag' => 1));
+ $this->Tree->create();
+ $this->Tree->save(array('name' => 'Overview', $parentField => $node12, $leftField => 0, $rightField => 0, 'flag' => 1));
+
+ $this->Tree->Behaviors->enable('Tree');
+
+ $result = $this->Tree->verify();
+ $this->assertNotSame($result, true);
+
+ $result = $this->Tree->recover();
+ $this->assertTrue($result);
+
+ $result = $this->Tree->verify();
+ $this->assertTrue($result);
+
+ $result = $this->Tree->find('first', array(
+ 'fields' => array('name', $parentField, $leftField, $rightField, 'flag'),
+ 'conditions' => array('name' => 'Main'),
+ 'recursive' => -1
+ ));
+ $expected = array(
+ $modelClass => array(
+ 'name' => 'Main',
+ $parentField => null,
+ $leftField => 1,
+ $rightField => 10,
+ 'flag' => 1
+ )
+ );
+ $this->assertEquals($expected, $result);
+ }
+
+/**
+ * testRecoverFromMissingParent method
+ *
+ * @return void
+ */
+ public function testRecoverFromMissingParent() {
+ extract($this->settings);
+ $this->Tree = new $modelClass();
+ $this->Tree->order = null;
+ $this->Tree->initialize(2, 2);
+
+ $this->Tree->id = 1;
+ $this->Tree->saveField('flag', 1);
+ $this->Tree->id = 2;
+ $this->Tree->saveField('flag', 1);
+
+ $this->Tree->Behaviors->attach('Tree', array('scope' => array('FlagTree.flag' => 1)));
+
+ $result = $this->Tree->findByName('1.1');
+ $this->Tree->updateAll(array($parentField => 999999), array('id' => $result[$modelClass]['id']));
+
+ $result = $this->Tree->verify();
+ $this->assertNotSame($result, true);
+
+ $result = $this->Tree->recover();
+ $this->assertSame($result, true);
+
+ $result = $this->Tree->verify();
+ $this->assertSame($result, true);
+ }
+
+/**
+ * testDetectInvalidParents method
+ *
+ * @return void
+ */
+ public function testDetectInvalidParents() {
+ extract($this->settings);
+ $this->Tree = new $modelClass();
+ $this->Tree->order = null;
+ $this->Tree->initialize(2, 2);
+
+ $this->Tree->id = 1;
+ $this->Tree->saveField('flag', 1);
+ $this->Tree->id = 2;
+ $this->Tree->saveField('flag', 1);
+
+ $this->Tree->Behaviors->attach('Tree', array('scope' => array('FlagTree.flag' => 1)));
+
+ $this->Tree->updateAll(array($parentField => null));
+
+ $result = $this->Tree->verify();
+ $this->assertNotSame($result, true);
+
+ $result = $this->Tree->recover();
+ $this->assertSame($result, true);
+
+ $result = $this->Tree->verify();
+ $this->assertSame($result, true);
+ }
+
+/**
+ * testDetectInvalidLftsRghts method
+ *
+ * @return void
+ */
+ public function testDetectInvalidLftsRghts() {
+ extract($this->settings);
+ $this->Tree = new $modelClass();
+ $this->Tree->order = null;
+ $this->Tree->initialize(2, 2);
+
+ $this->Tree->id = 1;
+ $this->Tree->saveField('flag', 1);
+ $this->Tree->id = 2;
+ $this->Tree->saveField('flag', 1);
+
+ $this->Tree->Behaviors->attach('Tree', array('scope' => array('FlagTree.flag' => 1)));
+
+ $this->Tree->updateAll(array($leftField => 0, $rightField => 0));
+
+ $result = $this->Tree->verify();
+ $this->assertNotSame($result, true);
+
+ $this->Tree->recover();
+
+ $result = $this->Tree->verify();
+ $this->assertSame($result, true);
+ }
+
+/**
+ * Reproduces a situation where a single node has lft= rght, and all other lft and rght fields follow sequentially
+ *
+ * @return void
+ */
+ public function testDetectEqualLftsRghts() {
+ extract($this->settings);
+ $this->Tree = new $modelClass();
+ $this->Tree->order = null;
+ $this->Tree->initialize(1, 3);
+
+ $this->Tree->id = 1;
+ $this->Tree->saveField('flag', 1);
+ $this->Tree->id = 2;
+ $this->Tree->saveField('flag', 1);
+
+ $this->Tree->Behaviors->attach('Tree', array('scope' => array('FlagTree.flag' => 1)));
+
+ $result = $this->Tree->findByName('1.1');
+ $this->Tree->updateAll(array($rightField => $result[$modelClass][$leftField]), array('id' => $result[$modelClass]['id']));
+ $this->Tree->updateAll(array($leftField => $this->Tree->escapeField($leftField) . ' -1'),
+ array($leftField . ' >' => $result[$modelClass][$leftField]));
+ $this->Tree->updateAll(array($rightField => $this->Tree->escapeField($rightField) . ' -1'),
+ array($rightField . ' >' => $result[$modelClass][$leftField]));
+
+ $result = $this->Tree->verify();
+ $this->assertNotSame($result, true);
+
+ $result = $this->Tree->recover();
+ $this->assertTrue($result);
+
+ $result = $this->Tree->verify();
+ $this->assertTrue($result);
+ }
+
}

0 comments on commit 1d89ee1

Please sign in to comment.