Skip to content

Commit

Permalink
Merge pull request #955 from dereuromark/master-tree-fixes
Browse files Browse the repository at this point in the history
Asserting that tree behavior generates valid sql in recover

Fixes #3366
  • Loading branch information
markstory committed Nov 15, 2012
2 parents ac087ec + b48f105 commit 7206254
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 16 deletions.
16 changes: 8 additions & 8 deletions lib/Cake/Model/Behavior/TreeBehavior.php
Expand Up @@ -107,7 +107,7 @@ public function afterSave(Model $Model, $created) {
* @return array * @return array
*/ */
public function beforeFind(Model $Model, $query) { public function beforeFind(Model $Model, $query) {
if ($Model->findQueryType == 'threaded' && !isset($query['parent'])) { if ($Model->findQueryType === 'threaded' && !isset($query['parent'])) {
$query['parent'] = $this->settings[$Model->alias]['parent']; $query['parent'] = $this->settings[$Model->alias]['parent'];
} }
return $query; return $query;
Expand Down Expand Up @@ -602,7 +602,7 @@ public function recover(Model $Model, $mode = 'parent', $missingParentAction = n
} }
extract($this->settings[$Model->alias]); extract($this->settings[$Model->alias]);
$Model->recursive = $recursive; $Model->recursive = $recursive;
if ($mode == 'parent') { if ($mode === 'parent') {
$Model->bindModel(array('belongsTo' => array('VerifyParent' => array( $Model->bindModel(array('belongsTo' => array('VerifyParent' => array(
'className' => $Model->name, 'className' => $Model->name,
'foreignKey' => $parent, 'foreignKey' => $parent,
Expand All @@ -616,15 +616,15 @@ public function recover(Model $Model, $mode = 'parent', $missingParentAction = n
)); ));
$Model->unbindModel(array('belongsTo' => array('VerifyParent'))); $Model->unbindModel(array('belongsTo' => array('VerifyParent')));
if ($missingParents) { if ($missingParents) {
if ($missingParentAction == 'return') { if ($missingParentAction === 'return') {
foreach ($missingParents as $id => $display) { foreach ($missingParents as $id => $display) {
$this->errors[] = 'cannot find the parent for ' . $Model->alias . ' with id ' . $id . '(' . $display . ')'; $this->errors[] = 'cannot find the parent for ' . $Model->alias . ' with id ' . $id . '(' . $display . ')';
} }
return false; return false;
} elseif ($missingParentAction == 'delete') { } elseif ($missingParentAction === 'delete') {
$Model->deleteAll(array($Model->primaryKey => array_flip($missingParents))); $Model->deleteAll(array($Model->escapeField($Model->primaryKey) => array_flip($missingParents)), false);
} else { } else {
$Model->updateAll(array($parent => $missingParentAction), array($Model->escapeField($Model->primaryKey) => array_flip($missingParents))); $Model->updateAll(array($Model->escapeField($parent) => $missingParentAction), array($Model->escapeField($Model->primaryKey) => array_flip($missingParents)));
} }
} }
$count = 1; $count = 1;
Expand Down Expand Up @@ -986,14 +986,14 @@ protected function _sync(Model $Model, $shift, $dir = '+', $conditions = array()
extract($this->settings[$Model->alias]); extract($this->settings[$Model->alias]);
$Model->recursive = $recursive; $Model->recursive = $recursive;


if ($field == 'both') { if ($field === 'both') {
$this->_sync($Model, $shift, $dir, $conditions, $created, $left); $this->_sync($Model, $shift, $dir, $conditions, $created, $left);
$field = $right; $field = $right;
} }
if (is_string($conditions)) { if (is_string($conditions)) {
$conditions = array($Model->escapeField($field) . " {$conditions}"); $conditions = array($Model->escapeField($field) . " {$conditions}");
} }
if (($scope != '1 = 1' && $scope !== true) && $scope) { if (($scope !== '1 = 1' && $scope !== true) && $scope) {
$conditions[] = $scope; $conditions[] = $scope;
} }
if ($created) { if ($created) {
Expand Down
84 changes: 76 additions & 8 deletions lib/Cake/Test/Case/Model/Behavior/TreeBehaviorNumberTest.php
Expand Up @@ -183,19 +183,19 @@ public function testRecoverUsingParentMode() {
$this->Tree = new $modelClass(); $this->Tree = new $modelClass();
$this->Tree->Behaviors->disable('Tree'); $this->Tree->Behaviors->disable('Tree');


$this->Tree->save(array('parent_id' => null, 'name' => 'Main', $parentField => null, $leftField => 0, $rightField => 0)); $this->Tree->save(array('name' => 'Main', $parentField => null, $leftField => 0, $rightField => 0));
$node1 = $this->Tree->id; $node1 = $this->Tree->id;


$this->Tree->create(); $this->Tree->create();
$this->Tree->save(array('parent_id' => null, 'name' => 'About Us', $parentField => $node1, $leftField => 0, $rightField => 0)); $this->Tree->save(array('name' => 'About Us', $parentField => $node1, $leftField => 0, $rightField => 0));
$node11 = $this->Tree->id; $node11 = $this->Tree->id;
$this->Tree->create(); $this->Tree->create();
$this->Tree->save(array('parent_id' => null, 'name' => 'Programs', $parentField => $node1, $leftField => 0, $rightField => 0)); $this->Tree->save(array('name' => 'Programs', $parentField => $node1, $leftField => 0, $rightField => 0));
$node12 = $this->Tree->id; $node12 = $this->Tree->id;
$this->Tree->create(); $this->Tree->create();
$this->Tree->save(array('parent_id' => null, 'name' => 'Mission and History', $parentField => $node11, $leftField => 0, $rightField => 0)); $this->Tree->save(array('name' => 'Mission and History', $parentField => $node11, $leftField => 0, $rightField => 0));
$this->Tree->create(); $this->Tree->create();
$this->Tree->save(array('parent_id' => null, 'name' => 'Overview', $parentField => $node12, $leftField => 0, $rightField => 0)); $this->Tree->save(array('name' => 'Overview', $parentField => $node12, $leftField => 0, $rightField => 0));


$this->Tree->Behaviors->enable('Tree'); $this->Tree->Behaviors->enable('Tree');


Expand Down Expand Up @@ -224,6 +224,74 @@ public function testRecoverUsingParentMode() {
$this->assertEquals($expected, $result); $this->assertEquals($expected, $result);
} }


/**
* testRecoverUsingParentModeAndDelete method
*
* @return void
*/
public function testRecoverUsingParentModeAndDelete() {
extract($this->settings);
$this->Tree = new $modelClass();
$this->Tree->Behaviors->disable('Tree');

$this->Tree->save(array('name' => 'Main', $parentField => null, $leftField => 0, $rightField => 0));
$node1 = $this->Tree->id;

$this->Tree->create();
$this->Tree->save(array('name' => 'About Us', $parentField => $node1, $leftField => 0, $rightField => 0));
$node11 = $this->Tree->id;
$this->Tree->create();
$this->Tree->save(array('name' => 'Programs', $parentField => $node1, $leftField => 0, $rightField => 0));
$node12 = $this->Tree->id;
$this->Tree->create();
$this->Tree->save(array('name' => 'Mission and History', $parentField => $node11, $leftField => 0, $rightField => 0));
$this->Tree->create();
$this->Tree->save(array('name' => 'Overview', $parentField => $node12, $leftField => 0, $rightField => 0));
$this->Tree->create();
$this->Tree->save(array('name' => 'Lost', $parentField => 9, $leftField => 0, $rightField => 0));

$this->Tree->Behaviors->enable('Tree');

$this->Tree->bindModel(array('belongsTo' => array('Parent' => array(
'className' => $this->Tree->name,
'foreignKey' => $parentField
))));
$this->Tree->bindModel(array('hasMany' => array('Child' => array(
'className' => $this->Tree->name,
'foreignKey' => $parentField
))));

$result = $this->Tree->verify();
$this->assertNotSame($result, true);

$count = $this->Tree->find('count');
$this->assertEquals(6, $count);

$result = $this->Tree->recover('parent', 'delete');
$this->assertTrue($result);

$result = $this->Tree->verify();
$this->assertTrue($result);

$count = $this->Tree->find('count');
$this->assertEquals(5, $count);

$result = $this->Tree->find('first', array(
'fields' => array('name', $parentField, $leftField, $rightField),
'conditions' => array('name' => 'Main'),
'recursive' => -1
));
$expected = array(
$modelClass => array(
'name' => 'Main',
$parentField => null,
$leftField => 1,
$rightField => 10
)
);
$this->assertEquals($expected, $result);
}

/** /**
* testRecoverFromMissingParent method * testRecoverFromMissingParent method
* *
Expand Down

0 comments on commit 7206254

Please sign in to comment.