Browse Source

Merge branch 'master' into 2.3

mark_story 13 years ago
parent
commit
a7f192c606

+ 8 - 8
lib/Cake/Model/Behavior/TreeBehavior.php

@@ -108,7 +108,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,15 +616,15 @@ 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->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;
@@ -985,14 +985,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) {

+ 5 - 4
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;
 	}

+ 76 - 8
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));
-		$node1	= $this->Tree->id;
+		$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));
-		$node11	= $this->Tree->id;
+		$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));
-		$node12	= $this->Tree->id;
+		$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');
 
@@ -225,6 +225,74 @@ class TreeBehaviorNumberTest extends CakeTestCase {
 	}
 
 /**
+ * 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
  *
  * @return void

+ 12 - 10
lib/Cake/Test/Case/Model/BehaviorCollectionTest.php

@@ -766,21 +766,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(
@@ -796,22 +797,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));
 	}
 
 /**

+ 12 - 0
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);
 	}
 
 /**

+ 17 - 0
lib/Cake/Test/Case/View/Helper/FormHelperTest.php

@@ -2172,6 +2172,23 @@ class FormHelperTest extends CakeTestCase {
 	}
 
 /**
+ * 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
  *
  * @return void

+ 2 - 2
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());
 		}