Skip to content

Commit

Permalink
Also ensuring columns for delete, unit testing
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzo committed Apr 9, 2014
1 parent 6d5a870 commit e926c22
Show file tree
Hide file tree
Showing 2 changed files with 112 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/Model/Behavior/TreeBehavior.php
Expand Up @@ -130,6 +130,7 @@ public function beforeSave(Event $event, Entity $entity) {
*/
public function beforeDelete(Event $event, Entity $entity) {
$config = $this->config();
$this->_ensureFields($entity);
$left = $entity->get($config['left']);
$right = $entity->get($config['right']);
$diff = $right - $left + 1;
Expand Down
112 changes: 111 additions & 1 deletion tests/TestCase/Model/Behavior/TreeBehaviorTest.php
Expand Up @@ -276,6 +276,30 @@ public function testMoveTop() {
$this->assertEquals([8, 1, 6], $nodes->extract('id')->toArray());
}

/**
* Tests moving a node with no lft and rght
*
* @return void
*/
public function testMoveNoTreeColumns() {
$table = TableRegistry::get('MenuLinkTrees');
$table->addBehavior('Tree', ['scope' => ['menu' => 'main-menu']]);
$node = $table->get(8);
$node->unsetProperty('lft');
$node->unsetProperty('rght');
$node = $table->moveUp($node, true);
$this->assertEquals(['lft' => 1, 'rght' => 2], $node->extract(['lft', 'rght']));
$nodes = $table->find()
->select(['id'])
->where(function($exp) {
return $exp->isNull('parent_id');
})
->where(['menu' => 'main-menu'])
->order(['lft' => 'ASC'])
->all();
$this->assertEquals([8, 1, 6], $nodes->extract('id')->toArray());
}

/**
* Tests the moveDown() method
*
Expand Down Expand Up @@ -331,6 +355,30 @@ public function testMoveToBottom() {
$this->assertEquals([6, 8, 1], $nodes->extract('id')->toArray());
}

/**
* Tests moving a node with no lft and rght columns
*
* @return void
*/
public function testMoveDownNoTreeColumns() {
$table = TableRegistry::get('MenuLinkTrees');
$table->addBehavior('Tree', ['scope' => ['menu' => 'main-menu']]);
$node = $table->get(1);
$node->unsetProperty('lft');
$node->unsetProperty('rght');
$node = $table->moveDown($node, true);
$this->assertEquals(['lft' => 7, 'rght' => 16], $node->extract(['lft', 'rght']));
$nodes = $table->find()
->select(['id'])
->where(function($exp) {
return $exp->isNull('parent_id');
})
->where(['menu' => 'main-menu'])
->order(['lft' => 'ASC'])
->all();
$this->assertEquals([6, 8, 1], $nodes->extract('id')->toArray());
}

/**
* Tests the recover function
*
Expand Down Expand Up @@ -509,7 +557,7 @@ public function testReParentLeafLeft() {
}

/**
* Test moving a leaft to the left
* Test moving a leaf to the left
*
* @return void
*/
Expand All @@ -527,6 +575,28 @@ public function testReParentLeafRight() {
$this->assertTreeNumbers($expected, $table);
}

/**
* Tests moving a subtree with a node having no lft and rght columns
*
* @return void
*/
public function testReParentNoTreeColumns() {
$table = TableRegistry::get('NumberTrees');
$table->addBehavior('Tree');
$entity = $table->get(6);
$entity->unsetProperty('lft');
$entity->unsetProperty('rght');
$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);
}

/**
* Tests moving a subtree as a new root
*
Expand All @@ -546,6 +616,27 @@ public function testRootingSubTree() {
$this->assertTreeNumbers($expected, $table);
}

/**
* Tests moving a subtree with no tree columns
*
* @return void
*/
public function testRootingNoTreeColumns() {
$table = TableRegistry::get('NumberTrees');
$table->addBehavior('Tree');
$entity = $table->get(2);
$entity->unsetProperty('lft');
$entity->unsetProperty('rght');
$entity->parent_id = null;
$this->assertSame($entity, $table->save($entity));
$this->assertEquals(15, $entity->lft);
$this->assertEquals(22, $entity->rght);

$result = $table->find()->order('lft')->hydrate(false);
$expected = [1, 6, 7, 8, 9, 10, 11, 2, 3, 4, 5];
$this->assertTreeNumbers($expected, $table);
}

/**
* Tests that trying to create a cycle throws an exception
*
Expand Down Expand Up @@ -609,6 +700,25 @@ public function testDeleteRoot() {
$this->assertEquals($expected, $result);
}


/**
* Test deleting a node with no tree columns
*
* @return void
*/
public function testDeleteRootNoTreeColumns() {
$table = TableRegistry::get('NumberTrees');
$table->addBehavior('Tree');
$entity = $table->get(1);
$entity->unsetProperty('lft');
$entity->unsetProperty('rght');
$this->assertTrue($table->delete($entity));
$result = $table->find()->order('lft')->hydrate(false)->toArray();
$table->recover();
$expected = $table->find()->order('lft')->hydrate(false)->toArray();
$this->assertEquals($expected, $result);
}

/**
* Tests that a leaf can be taken out of the tree and put in as a root
*
Expand Down

0 comments on commit e926c22

Please sign in to comment.