Skip to content

Commit

Permalink
Finished implementation for re-paretning a node
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzo committed Mar 29, 2014
1 parent ec77160 commit 7ec8cd8
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 5 deletions.
10 changes: 5 additions & 5 deletions src/Model/Behavior/TreeBehavior.php
Expand Up @@ -126,22 +126,22 @@ protected function _setParent($entity, $parent) {
$right = $entity->get($config['right']);
$left = $entity->get($config['left']);

$diff = $right - $left + 1;
$targetLeft = $parentRight - $diff;
$targetRight = $parentRight - 1;

// Values for moving to the left
$diff = $right - $left + 1;
$targetLeft = $parentRight;
$targetRight = $diff + $parentRight - 1;
$min = $parentRight;
$max = $left - 1;

if ($left < $targetLeft) {
//Moving to the right
$targetLeft = $parentRight - $diff;
$targetRight = $parentRight - 1;
$min = $right + 1;
$max = $parentRight - 1;
$diff *= -1;
}


if ($right - $left > 1) {
//Correcting internal subtree
$internalLeft = $left + 1;
Expand Down
66 changes: 66 additions & 0 deletions tests/TestCase/Model/Behavior/TreeBehaviorTest.php
Expand Up @@ -394,4 +394,70 @@ public function testReParentSubTreeRight() {
$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);
}

}

0 comments on commit 7ec8cd8

Please sign in to comment.