Browse Source

Finished implementation for re-paretning a node

Jose Lorenzo Rodriguez 12 years ago
parent
commit
7ec8cd888f

+ 5 - 5
src/Model/Behavior/TreeBehavior.php

@@ -126,22 +126,22 @@ class TreeBehavior extends Behavior {
 		$right = $entity->get($config['right']);
 		$right = $entity->get($config['right']);
 		$left = $entity->get($config['left']);
 		$left = $entity->get($config['left']);
 
 
-		$diff = $right - $left + 1;
-		$targetLeft = $parentRight - $diff;
-		$targetRight = $parentRight - 1;
-
 		// Values for moving to the left
 		// Values for moving to the left
+		$diff = $right - $left + 1;
+		$targetLeft = $parentRight;
+		$targetRight = $diff + $parentRight - 1;
 		$min = $parentRight;
 		$min = $parentRight;
 		$max = $left - 1;
 		$max = $left - 1;
 
 
 		if ($left < $targetLeft) {
 		if ($left < $targetLeft) {
 			//Moving to the right
 			//Moving to the right
+			$targetLeft = $parentRight - $diff;
+			$targetRight = $parentRight - 1;
 			$min = $right + 1;
 			$min = $right + 1;
 			$max = $parentRight - 1;
 			$max = $parentRight - 1;
 			$diff *= -1;
 			$diff *= -1;
 		}
 		}
 
 
-
 		if ($right - $left > 1) {
 		if ($right - $left > 1) {
 			//Correcting internal subtree
 			//Correcting internal subtree
 			$internalLeft = $left + 1;
 			$internalLeft = $left + 1;

+ 66 - 0
tests/TestCase/Model/Behavior/TreeBehaviorTest.php

@@ -394,4 +394,70 @@ class TreeBehaviorTest extends TestCase {
 		$this->assertEquals(range(1, 22), $numbers);
 		$this->assertEquals(range(1, 22), $numbers);
 	}
 	}
 
 
+/**
+ * Tests moving a subtree to the left
+ *
+ * @return void
+ */
+	public function testReParentSubTreeLeft() {
+		$table = TableRegistry::get('NumberTrees');
+		$table->addBehavior('Tree');
+		$entity = $table->get(6);
+		$entity->parent_id = 2;
+		$this->assertSame($entity, $table->save($entity));
+		$this->assertEquals(9, $entity->lft);
+		$this->assertEquals(18, $entity->rght);
+
+		$result = $table->find()->order('lft')->hydrate(false)->toArray();
+		$table->recover();
+		$expected = $table->find()->order('lft')->hydrate(false)->toArray();
+		$this->assertEquals($expected, $result);
+	}
+
+/**
+ * Test moving a leaft to the left
+ *
+ * @return void
+ */
+	public function testReParentLeafLeft() {
+		$table = TableRegistry::get('NumberTrees');
+		$table->addBehavior('Tree');
+		$entity = $table->get(10);
+		$entity->parent_id = 2;
+		$this->assertSame($entity, $table->save($entity));
+		$this->assertEquals(9, $entity->lft);
+		$this->assertEquals(10, $entity->rght);
+
+		$result = $table->find()->order('lft')->hydrate(false)->toArray();
+		$table->recover();
+		$expected = $table->find()->order('lft')->hydrate(false)->toArray();
+		$this->assertEquals($expected, $result);
+	}
+
+/**
+ * Test moving a leaft to the left
+ *
+ * @return void
+ */
+	public function testReParentLeafRight() {
+		$table = TableRegistry::get('NumberTrees');
+		$table->addBehavior('Tree');
+		$entity = $table->get(5);
+		$entity->parent_id = 6;
+		$this->assertSame($entity, $table->save($entity));
+		$this->assertEquals(17, $entity->lft);
+		$this->assertEquals(18, $entity->rght);
+
+		$result = $table->find()->order('lft')->hydrate(false);
+		$expected = [1, 2, 3, 4, 6, 7, 8, 9, 10, 5, 11];
+		$this->assertEquals($expected, $result->extract('id')->toArray());
+		$numbers = [];
+		$result->each(function($v) use (&$numbers) {
+			$numbers[] = $v['lft'];
+			$numbers[] = $v['rght'];
+		});
+		sort($numbers);
+		$this->assertEquals(range(1, 22), $numbers);
+	}
+
 }
 }