Skip to content

Commit

Permalink
adding tests for recover by parent
Browse files Browse the repository at this point in the history
  • Loading branch information
dogmatic69 committed Aug 24, 2012
1 parent 17320b8 commit 4e865f5
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
4 changes: 4 additions & 0 deletions Core/Libs/Model/Behavior/InfiniTreeBehavior.php
Expand Up @@ -315,13 +315,17 @@ public function moveup(Model $Model, $id = null, $number = 1) {
* parameter only applies to "parent" mode and determines what to do if the parent field contains an id that is not present.
*
* @todo Could be written to be faster, *maybe*. Ideally using a subquery and putting all the logic burden on the DB.
*
* @param AppModel $Model Model instance
* @param string $mode parent or tree
* @param mixed $missingParentAction 'return' to do nothing and return, 'delete' to
* delete, or the id of the parent to set as the parent_id
* @param string $scope The scopeField to select which tree to recover
*
* @return boolean true on success, false on failure
*
* @access public
*
* @link http://book.cakephp.org/view/1628/Recover
*/
public function recover(Model $Model, $mode = 'parent', $missingParentAction = null, $scope = null) {
Expand Down
48 changes: 48 additions & 0 deletions Core/Libs/Test/Case/Model/Behavior/InfiniTreeTest.php
Expand Up @@ -33,6 +33,54 @@ public function testFixtureIntegrity() {
$this->assertIdentical($validTree, true);
}

public function testRecoverFromParent() {
$this->ScopedNumberTree->Behaviors->disable('InfiniTree');
$this->ScopedNumberTree->deleteAll(array('ScopedNumberTree.id !=' => 0));
$data = array(
array(
'id' => '1',
'name' => '1',
'category_id' => 'cat-1',
'parent_id' => null
),
array(
'id' => '1-1',
'name' => '1-1',
'category_id' => 'cat-1',
'parent_id' => '1'
),
array(
'id' => '1-1-1',
'name' => '1-1-1',
'category_id' => 'cat-1',
'parent_id' => '1-1'
)
);
$this->ScopedNumberTree->create();
$this->assertTrue((bool)$this->ScopedNumberTree->saveAll($data));

$results = $this->ScopedNumberTree->find('all');
$parent = array_unique(Set::extract($results, '/ScopedNumberTree/parent_id'));
$lft = current(array_unique(Set::extract($results, '/ScopedNumberTree/lft')));
$rght = current(array_unique(Set::extract($results, '/ScopedNumberTree/rght')));

$this->assertEquals(array(null, '1', '1-1'), $parent);
$this->assertEquals(0, $lft);
$this->assertEquals(0, $rght);

$this->ScopedNumberTree->Behaviors->enable('InfiniTree');
$this->assertTrue($this->ScopedNumberTree->recover('parent', null, 'cat-1'));

$results = $this->ScopedNumberTree->find('all');
$parent = array_unique(Set::extract($results, '/ScopedNumberTree/parent_id'));
$lft = array_unique(Set::extract($results, '/ScopedNumberTree/lft'));
$rght = array_unique(Set::extract($results, '/ScopedNumberTree/rght'));

$this->assertEquals(array(null, '1', '1-1'), $parent);
$this->assertEquals(array(1, 2, 3), $lft);
$this->assertEquals(array(6, 5, 4), $rght);
}

public function testTreeSave() {
//Test invalid values
$this->assertFalse($this->ScopedNumberTree->treeSave());
Expand Down

0 comments on commit 4e865f5

Please sign in to comment.