Skip to content
Browse files

asserting that tree behavior generates valid sql in recover

  • Loading branch information...
1 parent e0586da commit 22f65f7cb5c7e9e4ff7702d770486facb6fef707 @dereuromark dereuromark committed Nov 13, 2012
View
14 lib/Cake/Model/Behavior/TreeBehavior.php
@@ -107,7 +107,7 @@ public function afterSave(Model $Model, $created) {
* @return array
*/
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'];
}
return $query;
@@ -602,7 +602,7 @@ public function recover(Model $Model, $mode = 'parent', $missingParentAction = n
}
extract($this->settings[$Model->alias]);
$Model->recursive = $recursive;
- if ($mode == 'parent') {
+ if ($mode === 'parent') {
$Model->bindModel(array('belongsTo' => array('VerifyParent' => array(
'className' => $Model->name,
'foreignKey' => $parent,
@@ -616,13 +616,13 @@ public function recover(Model $Model, $mode = 'parent', $missingParentAction = n
));
$Model->unbindModel(array('belongsTo' => array('VerifyParent')));
if ($missingParents) {
- if ($missingParentAction == 'return') {
+ if ($missingParentAction === 'return') {
foreach ($missingParents as $id => $display) {
$this->errors[] = 'cannot find the parent for ' . $Model->alias . ' with id ' . $id . '(' . $display . ')';
}
return false;
- } elseif ($missingParentAction == 'delete') {
- $Model->deleteAll(array($Model->primaryKey => array_flip($missingParents)));
+ } elseif ($missingParentAction === 'delete') {
+ $Model->deleteAll(array($Model->alias . '.' . $Model->primaryKey => array_flip($missingParents)));
} else {
$Model->updateAll(array($parent => $missingParentAction), array($Model->escapeField($Model->primaryKey) => array_flip($missingParents)));
}
@@ -986,14 +986,14 @@ protected function _sync(Model $Model, $shift, $dir = '+', $conditions = array()
extract($this->settings[$Model->alias]);
$Model->recursive = $recursive;
- if ($field == 'both') {
+ if ($field === 'both') {
$this->_sync($Model, $shift, $dir, $conditions, $created, $left);
$field = $right;
}
if (is_string($conditions)) {
$conditions = array($Model->escapeField($field) . " {$conditions}");
}
- if (($scope != '1 = 1' && $scope !== true) && $scope) {
+ if (($scope !== '1 = 1' && $scope !== true) && $scope) {
$conditions[] = $scope;
}
if ($created) {
View
61 lib/Cake/Test/Case/Model/Behavior/TreeBehaviorNumberTest.php
@@ -184,7 +184,7 @@ public function testRecoverUsingParentMode() {
$this->Tree->Behaviors->disable('Tree');
$this->Tree->save(array('parent_id' => null, 'name' => 'Main', $parentField => null, $leftField => 0, $rightField => 0));
- $node1 = $this->Tree->id;
+ $node1 = $this->Tree->id;
$this->Tree->create();
$this->Tree->save(array('parent_id' => null, 'name' => 'About Us', $parentField => $node1, $leftField => 0, $rightField => 0));
@@ -225,6 +225,65 @@ public function testRecoverUsingParentMode() {
}
/**
+ * testRecoverUsingParentModeAndDelete method
+ *
+ * @return void
+ */
+ public function testRecoverUsingParentModeAndDelete() {
+ extract($this->settings);
+ $this->Tree = new $modelClass();
+ $this->Tree->Behaviors->disable('Tree');
+
+ $this->Tree->save(array('parent_id' => null, 'name' => 'Main', $parentField => null, $leftField => 0, $rightField => 0));
+ $node1 = $this->Tree->id;
+
+ $this->Tree->create();
+ $this->Tree->save(array('parent_id' => null, 'name' => 'About Us', $parentField => $node1, $leftField => 0, $rightField => 0));
+ $node11 = $this->Tree->id;
+ $this->Tree->create();
+ $this->Tree->save(array('parent_id' => null, 'name' => 'Programs', $parentField => $node1, $leftField => 0, $rightField => 0));
+ $node12 = $this->Tree->id;
+ $this->Tree->create();
+ $this->Tree->save(array('parent_id' => null, 'name' => 'Mission and History', $parentField => $node11, $leftField => 0, $rightField => 0));
+ $this->Tree->create();
+ $this->Tree->save(array('parent_id' => null, 'name' => 'Overview', $parentField => $node12, $leftField => 0, $rightField => 0));
+ $this->Tree->create();
+ $this->Tree->save(array('parent_id' => null, 'name' => 'Lost', $parentField => 9, $leftField => 0, $rightField => 0));
+
+ $this->Tree->Behaviors->enable('Tree');
+
+ $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
*
* @return void

0 comments on commit 22f65f7

Please sign in to comment.
Something went wrong with that request. Please try again.