From e0586da808431811dc2d0f0738032b7a5838f8c0 Mon Sep 17 00:00:00 2001 From: mark_story Date: Sun, 11 Nov 2012 22:14:59 -0500 Subject: [PATCH 1/6] Fix tests that occasionally fail on postgres. --- .../Case/Model/BehaviorCollectionTest.php | 22 ++++++++++--------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/lib/Cake/Test/Case/Model/BehaviorCollectionTest.php b/lib/Cake/Test/Case/Model/BehaviorCollectionTest.php index 19497f7a6..21104eda5 100644 --- a/lib/Cake/Test/Case/Model/BehaviorCollectionTest.php +++ b/lib/Cake/Test/Case/Model/BehaviorCollectionTest.php @@ -786,21 +786,22 @@ class BehaviorCollectionTest extends CakeTestCase { public function testBehaviorBelongsToFindCallbacks() { $this->skipIf($this->db instanceof Sqlserver, 'This test is not compatible with SQL Server.'); + $conditions = array('order' => 'Apple.id ASC'); $Apple = new Apple(); $Apple->unbindModel(array('hasMany' => array('Child'), 'hasOne' => array('Sample')), false); - $expected = $Apple->find('all'); + $expected = $Apple->find('all', $conditions); $Apple->unbindModel(array('belongsTo' => array('Parent'))); - $wellBehaved = $Apple->find('all'); + $wellBehaved = $Apple->find('all', $conditions); $Apple->Parent->Behaviors->attach('Test'); $Apple->unbindModel(array('belongsTo' => array('Parent'))); - $this->assertSame($Apple->find('all'), $wellBehaved); + $this->assertSame($Apple->find('all', $conditions), $wellBehaved); $Apple->Parent->Behaviors->attach('Test', array('before' => 'off')); - $this->assertSame($expected, $Apple->find('all')); + $this->assertSame($expected, $Apple->find('all', $conditions)); $Apple->Parent->Behaviors->attach('Test', array('before' => 'test')); - $this->assertSame($expected, $Apple->find('all')); + $this->assertSame($expected, $Apple->find('all', $conditions)); $Apple->Parent->Behaviors->attach('Test', array('before' => 'modify')); $expected2 = array( @@ -816,22 +817,23 @@ class BehaviorCollectionTest extends CakeTestCase { ); $result2 = $Apple->find('all', array( 'fields' => array('Apple.id', 'Parent.id', 'Parent.name', 'Parent.mytime'), - 'conditions' => array('Apple.id <' => '4') + 'conditions' => array('Apple.id <' => '4'), + 'order' => 'Apple.id ASC', )); $this->assertEquals($expected2, $result2); $Apple->Parent->Behaviors->disable('Test'); - $result = $Apple->find('all'); + $result = $Apple->find('all', $conditions); $this->assertEquals($expected, $result); $Apple->Parent->Behaviors->attach('Test', array('after' => 'off')); - $this->assertEquals($expected, $Apple->find('all')); + $this->assertEquals($expected, $Apple->find('all', $conditions)); $Apple->Parent->Behaviors->attach('Test', array('after' => 'test')); - $this->assertEquals($expected, $Apple->find('all')); + $this->assertEquals($expected, $Apple->find('all', $conditions)); $Apple->Parent->Behaviors->attach('Test', array('after' => 'test2')); - $this->assertEquals($expected, $Apple->find('all')); + $this->assertEquals($expected, $Apple->find('all', $conditions)); } /** From 22f65f7cb5c7e9e4ff7702d770486facb6fef707 Mon Sep 17 00:00:00 2001 From: euromark Date: Tue, 13 Nov 2012 11:18:17 +0100 Subject: [PATCH 2/6] asserting that tree behavior generates valid sql in recover --- lib/Cake/Model/Behavior/TreeBehavior.php | 14 ++--- .../Model/Behavior/TreeBehaviorNumberTest.php | 61 ++++++++++++++++++- 2 files changed, 67 insertions(+), 8 deletions(-) diff --git a/lib/Cake/Model/Behavior/TreeBehavior.php b/lib/Cake/Model/Behavior/TreeBehavior.php index 6895fe9d0..04357d544 100644 --- a/lib/Cake/Model/Behavior/TreeBehavior.php +++ b/lib/Cake/Model/Behavior/TreeBehavior.php @@ -107,7 +107,7 @@ class TreeBehavior extends ModelBehavior { * @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 @@ class TreeBehavior extends ModelBehavior { } 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 @@ class TreeBehavior extends ModelBehavior { )); $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 @@ class TreeBehavior extends ModelBehavior { 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) { diff --git a/lib/Cake/Test/Case/Model/Behavior/TreeBehaviorNumberTest.php b/lib/Cake/Test/Case/Model/Behavior/TreeBehaviorNumberTest.php index adcbf4c58..a4a8e58d8 100644 --- a/lib/Cake/Test/Case/Model/Behavior/TreeBehaviorNumberTest.php +++ b/lib/Cake/Test/Case/Model/Behavior/TreeBehaviorNumberTest.php @@ -184,7 +184,7 @@ class TreeBehaviorNumberTest extends CakeTestCase { $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)); @@ -224,6 +224,65 @@ class TreeBehaviorNumberTest extends CakeTestCase { $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('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 * From ac087ec9385899447d5bb0773d37601f0984b24b Mon Sep 17 00:00:00 2001 From: ADmad Date: Tue, 13 Nov 2012 22:13:15 +0530 Subject: [PATCH 3/6] Fix rules set being updated with array instead of CakeValidationRule objects. Closes #3367 --- lib/Cake/Model/Validator/CakeValidationSet.php | 9 +++++---- .../Case/Model/Validator/CakeValidationSetTest.php | 12 ++++++++++++ 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/lib/Cake/Model/Validator/CakeValidationSet.php b/lib/Cake/Model/Validator/CakeValidationSet.php index 60d41c6fe..4ada8bd27 100644 --- a/lib/Cake/Model/Validator/CakeValidationSet.php +++ b/lib/Cake/Model/Validator/CakeValidationSet.php @@ -192,7 +192,7 @@ class CakeValidationSet implements ArrayAccess, IteratorAggregate, Countable { * @return CakeValidationSet this instance */ public function setRule($name, $rule) { - if (!$rule instanceof CakeValidationRule) { + if (!($rule instanceof CakeValidationRule)) { $rule = new CakeValidationRule($rule); } $this->_rules[$name] = $rule; @@ -236,9 +236,10 @@ class CakeValidationSet implements ArrayAccess, IteratorAggregate, Countable { */ public function setRules($rules = array(), $mergeVars = true) { if ($mergeVars === false) { - $this->_rules = $rules; - } else { - $this->_rules = array_merge($this->_rules, $rules); + $this->_rules = array(); + } + foreach ($rules as $name => $rule) { + $this->setRule($name, $rule); } return $this; } diff --git a/lib/Cake/Test/Case/Model/Validator/CakeValidationSetTest.php b/lib/Cake/Test/Case/Model/Validator/CakeValidationSetTest.php index 10fa6fdf6..aa604fac9 100644 --- a/lib/Cake/Test/Case/Model/Validator/CakeValidationSetTest.php +++ b/lib/Cake/Test/Case/Model/Validator/CakeValidationSetTest.php @@ -156,10 +156,22 @@ class CakeValidationSetTest extends CakeTestCase { $result = $Field->getRules(); $this->assertEquals(array('validEmail'), array_keys($result)); + $Field->setRules(array('validEmail' => $rule), false); + $result = $Field->getRules(); + $this->assertEquals(array('validEmail'), array_keys($result)); + $this->assertTrue(array_pop($result) instanceof CakeValidationRule); + $rules = array('notEmpty' => $RuleEmpty); $Field->setRules($rules, true); $result = $Field->getRules(); $this->assertEquals(array('validEmail', 'notEmpty'), array_keys($result)); + + $rules = array('notEmpty' => array('rule' => 'notEmpty')); + $Field->setRules($rules, true); + $result = $Field->getRules(); + $this->assertEquals(array('validEmail', 'notEmpty'), array_keys($result)); + $this->assertTrue(array_pop($result) instanceof CakeValidationRule); + $this->assertTrue(array_pop($result) instanceof CakeValidationRule); } /** From 354069d22995ded6564f9c7ff94c4bf8dd978e97 Mon Sep 17 00:00:00 2001 From: euromark Date: Tue, 13 Nov 2012 21:12:46 +0100 Subject: [PATCH 4/6] cascade false for recover --- lib/Cake/Model/Behavior/TreeBehavior.php | 4 +-- .../Model/Behavior/TreeBehaviorNumberTest.php | 31 ++++++++++++------- 2 files changed, 22 insertions(+), 13 deletions(-) diff --git a/lib/Cake/Model/Behavior/TreeBehavior.php b/lib/Cake/Model/Behavior/TreeBehavior.php index 04357d544..920ce4a81 100644 --- a/lib/Cake/Model/Behavior/TreeBehavior.php +++ b/lib/Cake/Model/Behavior/TreeBehavior.php @@ -622,9 +622,9 @@ class TreeBehavior extends ModelBehavior { } return false; } elseif ($missingParentAction === 'delete') { - $Model->deleteAll(array($Model->alias . '.' . $Model->primaryKey => array_flip($missingParents))); + $Model->deleteAll(array($Model->escapeField($Model->primaryKey) => array_flip($missingParents)), false); } 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; diff --git a/lib/Cake/Test/Case/Model/Behavior/TreeBehaviorNumberTest.php b/lib/Cake/Test/Case/Model/Behavior/TreeBehaviorNumberTest.php index a4a8e58d8..82ecbcad3 100644 --- a/lib/Cake/Test/Case/Model/Behavior/TreeBehaviorNumberTest.php +++ b/lib/Cake/Test/Case/Model/Behavior/TreeBehaviorNumberTest.php @@ -183,19 +183,19 @@ class TreeBehaviorNumberTest extends CakeTestCase { $this->Tree = new $modelClass(); $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; $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; $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; $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->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'); @@ -234,24 +234,33 @@ class TreeBehaviorNumberTest extends CakeTestCase { $this->Tree = new $modelClass(); $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; $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; $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; $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->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->create(); - $this->Tree->save(array('parent_id' => null, 'name' => 'Lost', $parentField => 9, $leftField => 0, $rightField => 0)); + $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); From b48f105b3fc1dbc5a145965e5513f7d48a657827 Mon Sep 17 00:00:00 2001 From: euromark Date: Wed, 14 Nov 2012 09:55:33 +0100 Subject: [PATCH 5/6] fix tabs to spaces --- .../Test/Case/Model/Behavior/TreeBehaviorNumberTest.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/Cake/Test/Case/Model/Behavior/TreeBehaviorNumberTest.php b/lib/Cake/Test/Case/Model/Behavior/TreeBehaviorNumberTest.php index 82ecbcad3..4b316e967 100644 --- a/lib/Cake/Test/Case/Model/Behavior/TreeBehaviorNumberTest.php +++ b/lib/Cake/Test/Case/Model/Behavior/TreeBehaviorNumberTest.php @@ -188,10 +188,10 @@ class TreeBehaviorNumberTest extends CakeTestCase { $this->Tree->create(); $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->save(array('name' => 'Programs', $parentField => $node1, $leftField => 0, $rightField => 0)); - $node12 = $this->Tree->id; + $node12 = $this->Tree->id; $this->Tree->create(); $this->Tree->save(array('name' => 'Mission and History', $parentField => $node11, $leftField => 0, $rightField => 0)); $this->Tree->create(); @@ -239,10 +239,10 @@ class TreeBehaviorNumberTest extends CakeTestCase { $this->Tree->create(); $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->save(array('name' => 'Programs', $parentField => $node1, $leftField => 0, $rightField => 0)); - $node12 = $this->Tree->id; + $node12 = $this->Tree->id; $this->Tree->create(); $this->Tree->save(array('name' => 'Mission and History', $parentField => $node11, $leftField => 0, $rightField => 0)); $this->Tree->create(); From 58de6702bc02aa395997bdf51099adf7a9a1bafe Mon Sep 17 00:00:00 2001 From: mark_story Date: Wed, 14 Nov 2012 21:27:12 -0500 Subject: [PATCH 6/6] Fix notice errors when creating fields named 0. Fixes #3371 --- .../Test/Case/View/Helper/FormHelperTest.php | 17 +++++++++++++++++ lib/Cake/View/Helper/FormHelper.php | 4 ++-- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/lib/Cake/Test/Case/View/Helper/FormHelperTest.php b/lib/Cake/Test/Case/View/Helper/FormHelperTest.php index 4549e48a0..118f62a40 100644 --- a/lib/Cake/Test/Case/View/Helper/FormHelperTest.php +++ b/lib/Cake/Test/Case/View/Helper/FormHelperTest.php @@ -2153,6 +2153,23 @@ class FormHelperTest extends CakeTestCase { $this->assertTags($result, $expected); } +/** + * Test that inputs with 0 can be created. + * + * @return void + */ + public function testInputZero() { + $this->Form->create('User'); + $result = $this->Form->input('0'); + $expected = array( + 'div' => array('class' => 'input text'), + 'label' => array('for' => 'User0'), '/label', + 'input' => array('type' => 'text', 'name' => 'data[User][0]', 'id' => 'User0'), + '/div' + ); + $this->assertTags($result, $expected); + } + /** * test input() with checkbox creation * diff --git a/lib/Cake/View/Helper/FormHelper.php b/lib/Cake/View/Helper/FormHelper.php index 34c344c3d..71eb1a788 100644 --- a/lib/Cake/View/Helper/FormHelper.php +++ b/lib/Cake/View/Helper/FormHelper.php @@ -199,7 +199,7 @@ class FormHelper extends AppHelper { $this->fieldset[$object->alias]['fields'][$alias] = array('type' => 'multiple'); } } - if (empty($field)) { + if ($field === null || $field === false) { return $this->fieldset[$model]['fields']; } elseif (isset($this->fieldset[$model]['fields'][$field])) { return $this->fieldset[$model]['fields'][$field]; @@ -773,7 +773,7 @@ class FormHelper extends AppHelper { * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/form.html#FormHelper::label */ public function label($fieldName = null, $text = null, $options = array()) { - if (empty($fieldName)) { + if ($fieldName === null) { $fieldName = implode('.', $this->entity()); }