Browse Source

Adding tests for Validator

Ceeram 14 years ago
parent
commit
ec9966ec6b

+ 2 - 1
lib/Cake/Model/Model.php

@@ -979,7 +979,8 @@ class Model extends Object implements CakeEventListener {
 
 						if (strpos($assoc, '.') !== false) {
 							list($plugin, $assoc) = pluginSplit($assoc, true);
-							$this->{$type}[$assoc] = array('className' => $plugin . $assoc);						} else {
+							$this->{$type}[$assoc] = array('className' => $plugin . $assoc);
+						} else {
 							$this->{$type}[$assoc] = $value;
 						}
 					}

+ 6 - 4
lib/Cake/Model/ModelValidator.php

@@ -15,7 +15,7 @@
  * @copyright     Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
  * @link          http://cakephp.org CakePHP(tm) Project
  * @package       Cake.Model
- * @since         CakePHP(tm) v 0.10.0.0
+ * @since         CakePHP(tm) v 2.2.0
  * @license       MIT License (http://www.opensource.org/licenses/mit-license.php)
  */
 App::uses('CakeField', 'Model/Validator');
@@ -72,7 +72,7 @@ class ModelValidator {
 	public $options = array();
 
 /**
- * Holds the ModelFields
+ * Holds the CakeField objects array
  *
  * @var array
  */
@@ -345,17 +345,19 @@ class ModelValidator {
  * Gets all fields if $name is null (default), or the field for fieldname $name if it's found.
  *
  * @param string $name [optional] The fieldname to fetch. Defaults to null.
- * @return array|ModelField Either the fields array or the ModelField for fieldname $name
+ * @return mixed Either array of CakeField objects , single object for $name or false when $name not present in fields
  */
 	public function getFields($name = null) {
 		if ($name !== null && !empty($this->_fields[$name])) {
 			return $this->_fields[$name];
+		} elseif ($name !==null) {
+			return false;
 		}
 		return $this->_fields;
 	}
 
 /**
- * Sets the ModelField isntances from the Model::$validate property after processing the fieldList and whiteList.
+ * Sets the CakeField isntances from the Model::$validate property after processing the fieldList and whiteList.
  * If Model::$validate is not set or empty, this method returns false. True otherwise.
  *
  * @param boolean $reset If true will reset the Validator $validate array to the Model's default

+ 18 - 18
lib/Cake/Model/Validator/CakeField.php

@@ -14,62 +14,62 @@
  *
  * @copyright     Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
  * @link          http://cakephp.org CakePHP(tm) Project
- * @package       Cake.Model
- * @since         CakePHP(tm) v 3.0.0
+ * @package       Cake.Model.Validator
+ * @since         CakePHP(tm) v 2.2.0
  * @license       MIT License (http://www.opensource.org/licenses/mit-license.php)
  */
 App::uses('ModelValidator', 'Model');
 App::uses('CakeRule', 'Model/Validator');
 
 /**
- * ModelField object.
+ * CakeField object.
  *
- * @package       Cake.Model
+ * @package       Cake.Model.Validator
  * @link          http://book.cakephp.org/2.0/en/data-validation.html
  */
 class CakeField {
 
 /**
  * Holds the parent Validator instance
- * 
+ *
  * @var ModelValidator
  */
 	protected $_validator = null;
 
 /**
  * Holds the ValidationRule objects
- * 
+ *
  * @var array
  */
 	protected $_rules = array();
 
 /**
  * If the validation is stopped
- * 
+ *
  * @var boolean
  */
 	public $isStopped = false;
 
 /**
  * Holds the fieldname
- * 
+ *
  * @var string
  */
 	public $field = null;
 
 /**
  * Holds the original ruleSet
- * 
+ *
  * @var array
  */
 	public $ruleSet = array();
 
 /**
  * Constructor
- * 
+ *
  * @param ModelValidator $validator The parent ModelValidator
  * @param string $fieldName The fieldname
- * @param 
+ * @param
  */
 	public function __construct(ModelValidator $validator, $fieldName, $ruleSet) {
 		$this->_validator = $validator;
@@ -89,7 +89,7 @@ class CakeField {
 
 /**
  * Validates a ModelField
- * 
+ *
  * @return mixed
  */
 	public function validate() {
@@ -120,7 +120,7 @@ class CakeField {
 
 /**
  * Gets a rule for a certain index
- * 
+ *
  * @param mixed index
  * @return ValidationRule
  */
@@ -132,7 +132,7 @@ class CakeField {
 
 /**
  * Gets all rules for this ModelField
- * 
+ *
  * @return array
  */
 	public function getRules() {
@@ -141,7 +141,7 @@ class CakeField {
 
 /**
  * Sets a ValidationRule $rule for key $key
- * 
+ *
  * @param mixed $key The key under which the rule should be set
  * @param ValidationRule $rule The ValidationRule to be set
  * @return ModelField
@@ -153,7 +153,7 @@ class CakeField {
 
 /**
  * Sets the rules for a given field
- * 
+ *
  * @param array $rules The rules to be set
  * @param bolean $mergeVars [optional] If true, merges vars instead of replace. Defaults to true.
  * @return ModelField
@@ -169,7 +169,7 @@ class CakeField {
 
 /**
  * Gets the validator this field is atached to
- * 
+ *
  * @return ModelValidator The parent ModelValidator instance
  */
 	public function getValidator() {
@@ -178,7 +178,7 @@ class CakeField {
 
 /**
  * Magic isset
- * 
+ *
  * @return true if the field exists in data, false otherwise
  */
 	public function __isset($fieldName) {

+ 52 - 42
lib/Cake/Model/Validator/CakeRule.php

@@ -14,8 +14,8 @@
  *
  * @copyright     Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
  * @link          http://cakephp.org CakePHP(tm) Project
- * @package       Cake.Model
- * @since         CakePHP(tm) v 3.0.0
+ * @package       Cake.Model.Validator
+ * @since         CakePHP(tm) v 2.2.0
  * @license       MIT License (http://www.opensource.org/licenses/mit-license.php)
  */
 App::uses('ModelValidator', 'Model');
@@ -23,129 +23,129 @@ App::uses('CakeField', 'Model/Validator');
 App::uses('Validation', 'Utility');
 
 /**
- * ValidationRule object.
+ * CakeRule object.
  *
- * @package       Cake.Model
+ * @package       Cake.Model.Validator
  * @link          http://book.cakephp.org/2.0/en/data-validation.html
  */
 class CakeRule {
 
 /**
  * Holds a reference to the parent field
- * 
- * @var ModelField
+ *
+ * @var CakeField
  */
 	protected $_field = null;
 
 /**
  * Has the required check failed?
- * 
+ *
  * @var boolean
  */
 	protected $_requiredFail = null;
 
 /**
  * The 'valid' value
- * 
+ *
  * @var mixed
  */
 	protected $_valid = true;
 
 /**
- * Holds the index under which the Vaildator was attached
- * 
+ * Holds the index under which the Validator was attached
+ *
  * @var mixed
  */
 	protected $_index = null;
 
 /**
  * Create or Update transaction?
- * 
+ *
  * @var boolean
  */
 	protected $_modelExists = null;
 
 /**
  * The parsed rule
- * 
+ *
  * @var mixed
  */
 	protected $_rule = null;
 
 /**
  * The parsed rule parameters
- * 
+ *
  * @var array
  */
 	protected $_ruleParams = array();
 
 /**
  * The errorMessage
- * 
+ *
  * @var string
  */
 	protected $_errorMessage = null;
 
 /**
  * Holds passed in options
- * 
+ *
  * @var array
  */
 	protected $_passedOptions = array();
 
 /**
  * Flag indicating wether the allowEmpty check has failed
- * 
- * @var boolean 
+ *
+ * @var boolean
  */
 	protected $_emptyFail = null;
 
 /**
  * The 'rule' key
- * 
+ *
  * @var mixed
  */
 	public $rule = 'blank';
 
 /**
  * The 'required' key
- * 
+ *
  * @var mixed
  */
 	public $required = null;
 
 /**
  * The 'allowEmpty' key
- * 
+ *
  * @var boolean
  */
 	public $allowEmpty = false;
 
 /**
  * The 'on' key
- * 
+ *
  * @var string
  */
 	public $on = null;
 
 /**
  * The 'last' key
- * 
+ *
  * @var boolean
  */
 	public $last = true;
 
 /**
  * The 'message' key
- * 
+ *
  * @var string
  */
 	public $message = null;
 
 /**
  * Constructor
- * 
- * @param ModelField $field
+ *
+ * @param CakeField $field
  * @param array $validator [optional] The validator properties
  * @param mixed $index [optional]
  */
@@ -168,11 +168,11 @@ class CakeRule {
 
 /**
  * Checks if the rule is valid
- * 
+ *
  * @return boolean
  */
 	public function isValid() {
-		if (!$this->_valid || (is_string($this->_valid) && strlen($this->_valid) > 0)) {
+		if (!$this->_valid || (is_string($this->_valid) && !empty($this->_valid))) {
 			return false;
 		}
 
@@ -181,11 +181,11 @@ class CakeRule {
 
 /**
  * Checks if the field is required by the 'required' value
- * 
+ *
  * @return boolean
  */
 	public function isRequired() {
-		if ($this->required === true || $this->required === false) {
+		if (is_bool($this->required)) {
 			return $this->required;
 		}
 
@@ -200,7 +200,7 @@ class CakeRule {
 
 /**
  * Checks if the field failed the required validation
- * 
+ *
  * @return boolean
  */
 	public function checkRequired() {
@@ -219,7 +219,7 @@ class CakeRule {
 
 /**
  * Checks if the allowEmpty key applies
- * 
+ *
  * @return boolean
  */
 	public function checkEmpty() {
@@ -236,8 +236,8 @@ class CakeRule {
 
 /**
  * Checks if the Validation rule can be skipped
- * 
- * @return boolean True if the ValidaitonRule can be skipped
+ *
+ * @return boolean True if the ValidationRule can be skipped
  */
 	public function skip() {
 		if (!empty($this->on)) {
@@ -250,7 +250,7 @@ class CakeRule {
 
 /**
  * Checks if the 'last' key is true
- * 
+ *
  * @return boolean
  */
 	public function isLast() {
@@ -259,7 +259,7 @@ class CakeRule {
 
 /**
  * Gets the validation error message
- * 
+ *
  * @return string
  */
 	public function getMessage() {
@@ -268,8 +268,8 @@ class CakeRule {
 
 /**
  * Gets the parent field
- * 
- * @return ModelField
+ *
+ * @return CakeField
  */
 	public function getField() {
 		return $this->_field;
@@ -277,7 +277,7 @@ class CakeRule {
 
 /**
  * Gets an array with the rule properties
- * 
+ *
  * @return array
  */
 	public function getPropertiesArray() {
@@ -293,7 +293,7 @@ class CakeRule {
 
 /**
  * Dispatches the validation rule to the given validator method
- * 
+ *
  * @return boolean True if the rule could be dispatched, false otherwise
  */
 	public function dispatchValidation() {
@@ -326,7 +326,7 @@ class CakeRule {
 
 /**
  * Fetches the correct error message for a failed validation
- * 
+ *
  * @return string
  */
 	protected function _processValidationResponse() {
@@ -345,10 +345,20 @@ class CakeRule {
 			if (is_array($this->rule) && $args === null) {
 				$args = array_slice($this->getField()->ruleSet[$this->_index]['rule'], 1);
 			}
+			if (!empty($args)) {
+				foreach ($args as $k => $arg) {
+					$args[$k] = __d($validationDomain, $arg);
+				}
+			}
 			$this->_errorMessage = __d($validationDomain, $this->_errorMessage, $args);
 		} elseif (is_string($this->_index)) {
 			if (is_array($this->rule)) {
 				$args = array_slice($this->getField()->ruleSet[$this->_index]['rule'], 1);
+				if (!empty($args)) {
+					foreach ($args as $k => $arg) {
+						$args[$k] = __d($validationDomain, $arg);
+					}
+				}
 				$this->_errorMessage = __d($validationDomain, $this->_index, $args);
 			} else {
 				$this->_errorMessage = __d($validationDomain, $this->_index);
@@ -365,7 +375,7 @@ class CakeRule {
 
 /**
  * Sets the rule properties from the rule entry in validate
- * 
+ *
  * @param array $validator [optional]
  * @return void
  */
@@ -387,7 +397,7 @@ class CakeRule {
 
 /**
  * Parses the rule and sets the rule and ruleParams
- * 
+ *
  * @return void
  */
 	protected function _parseRule() {

+ 2 - 0
lib/Cake/Test/Case/Model/ModelTest.php

@@ -34,6 +34,8 @@ class ModelTest extends PHPUnit_Framework_TestSuite {
 	public static function suite() {
 		$suite = new PHPUnit_Framework_TestSuite('All Model related class tests');
 
+		$suite->addTestFile(CORE_TEST_CASES . DS . 'Model' . DS . 'Validator' . DS .'CakeFieldTest.php');
+		$suite->addTestFile(CORE_TEST_CASES . DS . 'Model' . DS . 'Validator' . DS .'CakeRuleTest.php');
 		$suite->addTestFile(CORE_TEST_CASES . DS . 'Model' . DS . 'ModelReadTest.php');
 		$suite->addTestFile(CORE_TEST_CASES . DS . 'Model' . DS . 'ModelWriteTest.php');
 		$suite->addTestFile(CORE_TEST_CASES . DS . 'Model' . DS . 'ModelDeleteTest.php');

File diff suppressed because it is too large
+ 911 - 7
lib/Cake/Test/Case/Model/ModelValidationTest.php


+ 0 - 712
lib/Cake/Test/Case/Model/ModelWriteTest.php

@@ -2958,16 +2958,6 @@ class ModelWriteTest extends BaseModelTest {
 		);
 		$this->assertEquals($expected['Comment'], $model->validationErrors);
 		$this->assertEquals($expected['Attachment'], $model->Attachment->validationErrors);
-
-		$this->assertFalse($model->saveAll(
-			array(
-				'Comment' => array('comment' => '', 'article_id' => 1, 'user_id' => 1),
-				'Attachment' => array('attachment' => '')
-			),
-			array('validate' => 'only')
-		));
-		$this->assertEquals($expected['Comment'], $model->validationErrors);
-		$this->assertEquals($expected['Attachment'], $model->Attachment->validationErrors);
 	}
 
 /**
@@ -3254,281 +3244,6 @@ class ModelWriteTest extends BaseModelTest {
 		$result = $TestModel->saveAll($data, array('deep' => true));
 		$this->assertTrue($result);
 	}
-/**
- * testSaveAllDeepValidateOnly
- * tests the validate methods with deeper recursive data
- *
- * @return void
- */
-	public function testSaveAllDeepValidateOnly() {
-		$this->loadFixtures('Article', 'Comment', 'User', 'Attachment');
-		$TestModel = new Article();
-		$TestModel->hasMany['Comment']['order'] = array('Comment.created' => 'ASC');
-		$TestModel->hasAndBelongsToMany = array();
-		$TestModel->Comment->Attachment->validate['attachment'] = 'notEmpty';
-		$TestModel->Comment->validate['comment'] = 'notEmpty';
-
-		$result = $TestModel->saveAll(
-			array(
-				'Article' => array('id' => 2),
-				'Comment' => array(
-					array('comment' => 'First new comment', 'published' => 'Y', 'User' => array('user' => 'newuser', 'password' => 'newuserpass')),
-					array('comment' => 'Second new comment', 'published' => 'Y', 'user_id' => 2)
-				)
-			),
-			array('validate' => 'only', 'deep' => true)
-		);
-		$this->assertTrue($result);
-
-		$result = $TestModel->saveAll(
-			array(
-				'Article' => array('id' => 2),
-				'Comment' => array(
-					array('comment' => 'First new comment', 'published' => 'Y', 'User' => array('user' => '', 'password' => 'newuserpass')),
-					array('comment' => 'Second new comment', 'published' => 'Y', 'user_id' => 2)
-				)
-			),
-			array('validate' => 'only', 'deep' => true)
-		);
-		$this->assertFalse($result);
-
-		$result = $TestModel->saveAll(
-			array(
-				'Article' => array('id' => 2),
-				'Comment' => array(
-					array('comment' => 'First new comment', 'published' => 'Y', 'User' => array('user' => 'newuser', 'password' => 'newuserpass')),
-					array('comment' => 'Second new comment', 'published' => 'Y', 'user_id' => 2)
-				)
-			),
-			array('validate' => 'only', 'atomic' => false, 'deep' => true)
-		);
-		$expected = array(
-			'Article' => true,
-			'Comment' => array(
-                true,
-                true
-			)
-		);
-		$this->assertSame($expected, $result);
-
-		$result = $TestModel->saveAll(
-			array(
-				'Article' => array('id' => 2),
-				'Comment' => array(
-					array('comment' => 'First new comment', 'published' => 'Y', 'User' => array('user' => '', 'password' => 'newuserpass')),
-					array('comment' => 'Second new comment', 'published' => 'Y', 'user_id' => 2)
-				)
-			),
-			array('validate' => 'only', 'atomic' => false, 'deep' => true)
-		);
-		$expected = array(
-			'Article' => true,
-			'Comment' => array(
-                false,
-                true
-			)
-		);
-		$this->assertSame($expected, $result);
-
-		$result = $TestModel->saveAll(array(
-			'Article' => array('id' => 2),
-			'Comment' => array(
-				array('comment' => 'Third new comment', 'published' => 'Y', 'user_id' => 5),
-				array('comment' => 'Fourth new comment', 'published' => 'Y', 'user_id' => 2, 'Attachment' => array('attachment' => 'deepsaved'))
-			)
-		),
-		array('validate' => 'only', 'deep' => true)
-		);
-		$this->assertTrue($result);
-
-		$result = $TestModel->saveAll(array(
-			'Article' => array('id' => 2),
-			'Comment' => array(
-				array('comment' => 'Third new comment', 'published' => 'Y', 'user_id' => 5),
-				array('comment' => 'Fourth new comment', 'published' => 'Y', 'user_id' => 2, 'Attachment' => array('attachment' => ''))
-			)
-		),
-		array('validate' => 'only', 'deep' => true)
-		);
-		$this->assertFalse($result);
-
-		$result = $TestModel->saveAll(array(
-			'Article' => array('id' => 2),
-			'Comment' => array(
-				array('comment' => 'Third new comment', 'published' => 'Y', 'user_id' => 5),
-				array('comment' => 'Fourth new comment', 'published' => 'Y', 'user_id' => 2, 'Attachment' => array('attachment' => 'deepsave'))
-			)
-		),
-		array('validate' => 'only', 'atomic' => false, 'deep' => true)
-		);
-		$expected = array(
-			'Article' => true,
-			'Comment' => array(
-                true,
-                true
-			)
-		);
-		$this->assertSame($expected, $result);
-
-		$result = $TestModel->saveAll(array(
-			'Article' => array('id' => 2),
-			'Comment' => array(
-				array('comment' => 'Third new comment', 'published' => 'Y', 'user_id' => 5),
-				array('comment' => 'Fourth new comment', 'published' => 'Y', 'user_id' => 2, 'Attachment' => array('attachment' => ''))
-			)
-		),
-		array('validate' => 'only', 'atomic' => false, 'deep' => true)
-		);
-		$expected = array(
-			'Article' => true,
-			'Comment' => array(
-                true,
-                false
-			)
-		);
-		$this->assertSame($expected, $result);
-
-		$expected = array(
-			'Comment' => array(
-				1 => array(
-					'Attachment' => array(
-						'attachment' => array('This field cannot be left blank')
-					)
-				)
-			)
-		);
-		$result = $TestModel->validationErrors;
-		$this->assertSame($expected, $result);
-
-		$data = array(
-			'Attachment' => array(
-				'attachment' => 'deepsave insert',
-			),
-			'Comment' => array(
-				'comment' => 'First comment deepsave insert',
-				'published' => 'Y',
-				'user_id' => 5,
-				'Article' => array(
-					'title' => 'First Article deepsave insert',
-					'body' => 'First Article Body deepsave insert',
-					'User' => array(
-						'user' => 'deepsave',
-						'password' => 'magic'
-					),
-				),
-			)
-		);
-
-		$result = $TestModel->Comment->Attachment->saveAll($data, array('validate' => 'only', 'deep' => true));
-		$this->assertTrue($result);
-
-		$result = $TestModel->Comment->Attachment->saveAll($data, array('validate' => 'only', 'atomic' => false, 'deep' => true));
-		$expected = array(
-			'Attachment' => true,
-			'Comment' => true
-		);
-		$this->assertSame($expected, $result);
-
-		$data = array(
-			'Attachment' => array(
-				'attachment' => 'deepsave insert',
-			),
-			'Comment' => array(
-				'comment' => 'First comment deepsave insert',
-				'published' => 'Y',
-				'user_id' => 5,
-				'Article' => array(
-					'title' => 'First Article deepsave insert',
-					'body' => 'First Article Body deepsave insert',
-					'User' => array(
-						'user' => '',
-						'password' => 'magic'
-					),
-				),
-			)
-		);
-
-		$result = $TestModel->Comment->Attachment->saveAll($data, array('validate' => 'only', 'deep' => true));
-		$this->assertFalse($result);
-
-		$result = $TestModel->Comment->Attachment->validationErrors;
-		$expected = array(
-			'Comment' => array(
-				'Article' => array(
-					'User' => array(
-						'user' => array('This field cannot be left blank')
-					)
-				)
-			)
-		);
-		$this->assertSame($expected, $result);
-
-		$result = $TestModel->Comment->Attachment->saveAll($data, array('validate' => 'only', 'atomic' => false, 'deep' => true));
-		$expected = array(
-			'Attachment' => true,
-			'Comment' => false
-		);
-		$this->assertEquals($expected, $result);
-
-		$data['Comment']['Article']['body'] = '';
-		$result = $TestModel->Comment->Attachment->saveAll($data, array('validate' => 'only', 'deep' => true));
-		$this->assertFalse($result);
-
-		$result = $TestModel->Comment->Attachment->validationErrors;
-		$expected = array(
-			'Comment' => array(
-				'Article' => array(
-					'body' => array('This field cannot be left blank')
-				)
-			)
-		);
-		$this->assertSame($expected, $result);
-
-		$result = $TestModel->Comment->Attachment->saveAll($data, array('validate' => 'only', 'atomic' => false, 'deep' => true));
-		$expected = array(
-			'Attachment' => true,
-			'Comment' => false
-		);
-		$this->assertEquals($expected, $result);
-
-		$data['Comment']['comment'] = '';
-		$result = $TestModel->Comment->Attachment->saveAll($data, array('validate' => 'only', 'deep' => true));
-		$this->assertFalse($result);
-
-		$result = $TestModel->Comment->Attachment->validationErrors;
-		$expected = array(
-			'Comment' => array(
-				'comment' => array('This field cannot be left blank')
-			)
-		);
-		$this->assertSame($expected, $result);
-
-		$result = $TestModel->Comment->Attachment->saveAll($data, array('validate' => 'only', 'atomic' => false, 'deep' => true));
-		$expected = array(
-			'Attachment' => true,
-			'Comment' => false
-		);
-		$this->assertEquals($expected, $result);
-
-		$data['Attachment']['attachment'] = '';
-		$result = $TestModel->Comment->Attachment->saveAll($data, array('validate' => 'only', 'deep' => true));
-		$this->assertFalse($result);
-
-		$result = $TestModel->Comment->Attachment->validationErrors;
-		$expected = array('attachment' => array('This field cannot be left blank'));
-		$this->assertSame($expected, $result);
-
-		$result = $TestModel->Comment->validationErrors;
-		$expected = array('comment' => array('This field cannot be left blank'));
-		$this->assertSame($expected, $result);
-
-		$result = $TestModel->Comment->Attachment->saveAll($data, array('validate' => 'only', 'atomic' => false, 'deep' => true));
-		$expected = array(
-			'Attachment' => false,
-			'Comment' => false
-		);
-		$this->assertEquals($expected, $result);
-	}
 
 /**
  * testSaveAllNotDeepAssociated method
@@ -3676,148 +3391,6 @@ class ModelWriteTest extends BaseModelTest {
 		$result = $TestModel->saveAll($data, array('deep' => false));
 		$this->assertTrue($result);
 	}
-/**
- * testSaveAllNotDeepValidateOnly
- * tests the validate methods to not validate deeper recursive data
- *
- * @return void
- */
-	public function testSaveAllNotDeepValidateOnly() {
-		$this->loadFixtures('Article', 'Comment', 'User', 'Attachment');
-		$TestModel = new Article();
-		$TestModel->hasMany['Comment']['order'] = array('Comment.created' => 'ASC');
-		$TestModel->hasAndBelongsToMany = array();
-		$TestModel->Comment->Attachment->validate['attachment'] = 'notEmpty';
-		$TestModel->Comment->validate['comment'] = 'notEmpty';
-
-		$result = $TestModel->saveAll(
-			array(
-				'Article' => array('id' => 2, 'body' => ''),
-				'Comment' => array(
-					array('comment' => 'First new comment', 'published' => 'Y', 'User' => array('user' => '', 'password' => 'newuserpass')),
-					array('comment' => 'Second new comment', 'published' => 'Y', 'user_id' => 2)
-				)
-			),
-			array('validate' => 'only', 'deep' => false)
-		);
-		$this->assertFalse($result);
-
-		$expected = array('body' => array('This field cannot be left blank'));
-		$result = $TestModel->validationErrors;
-		$this->assertSame($expected, $result);
-
-		$result = $TestModel->saveAll(
-			array(
-				'Article' => array('id' => 2, 'body' => 'Ignore invalid user data'),
-				'Comment' => array(
-					array('comment' => 'First new comment', 'published' => 'Y', 'User' => array('user' => '', 'password' => 'newuserpass')),
-					array('comment' => 'Second new comment', 'published' => 'Y', 'user_id' => 2)
-				)
-			),
-			array('validate' => 'only', 'deep' => false)
-		);
-		$this->assertTrue($result);
-
-		$result = $TestModel->saveAll(
-			array(
-				'Article' => array('id' => 2, 'body' => 'Ignore invalid user data'),
-				'Comment' => array(
-					array('comment' => 'First new comment', 'published' => 'Y', 'User' => array('user' => '', 'password' => 'newuserpass')),
-					array('comment' => 'Second new comment', 'published' => 'Y', 'user_id' => 2)
-				)
-			),
-			array('validate' => 'only', 'atomic' => false, 'deep' => false)
-		);
-		$expected = array(
-			'Article' => true,
-			'Comment' => array(
-                true,
-                true
-			)
-		);
-		$this->assertSame($expected, $result);
-
-		$result = $TestModel->saveAll(array(
-			'Article' => array('id' => 2, 'body' => 'Ignore invalid attachment data'),
-			'Comment' => array(
-				array('comment' => 'Third new comment', 'published' => 'Y', 'user_id' => 5),
-				array('comment' => 'Fourth new comment', 'published' => 'Y', 'user_id' => 2, 'Attachment' => array('attachment' => ''))
-			)
-		),
-		array('validate' => 'only', 'deep' => false)
-		);
-		$this->assertTrue($result);
-
-		$result = $TestModel->saveAll(array(
-			'Article' => array('id' => 2, 'body' => 'Ignore invalid attachment data'),
-			'Comment' => array(
-				array('comment' => 'Third new comment', 'published' => 'Y', 'user_id' => 5),
-				array('comment' => 'Fourth new comment', 'published' => 'Y', 'user_id' => 2, 'Attachment' => array('attachment' => ''))
-			)
-		),
-		array('validate' => 'only', 'atomic' => false, 'deep' => false)
-		);
-		$expected = array(
-			'Article' => true,
-			'Comment' => array(
-                true,
-                true
-			)
-		);
-		$this->assertSame($expected, $result);
-
-		$expected = array();
-		$result = $TestModel->validationErrors;
-		$this->assertSame($expected, $result);
-
-		$data = array(
-			'Attachment' => array(
-				'attachment' => 'deepsave insert',
-			),
-			'Comment' => array(
-				'comment' => 'First comment deepsave insert',
-				'published' => 'Y',
-				'user_id' => 5,
-				'Article' => array(
-					'title' => 'First Article deepsave insert ignored',
-					'body' => 'First Article Body deepsave insert',
-					'User' => array(
-						'user' => '',
-						'password' => 'magic'
-					),
-				),
-			)
-		);
-
-		$result = $TestModel->Comment->Attachment->saveAll($data, array('validate' => 'only', 'deep' => false));
-		$this->assertTrue($result);
-
-		$result = $TestModel->Comment->Attachment->validationErrors;
-		$expected = array();
-		$this->assertSame($expected, $result);
-
-		$result = $TestModel->Comment->Attachment->saveAll($data, array('validate' => 'only', 'atomic' => false, 'deep' => false));
-		$expected = array(
-			'Attachment' => true,
-			'Comment' => true
-		);
-		$this->assertEquals($expected, $result);
-
-		$data['Comment']['Article']['body'] = '';
-		$result = $TestModel->Comment->Attachment->saveAll($data, array('validate' => 'only', 'deep' => false));
-		$this->assertTrue($result);
-
-		$result = $TestModel->Comment->Attachment->validationErrors;
-		$expected = array();
-		$this->assertSame($expected, $result);
-
-		$result = $TestModel->Comment->Attachment->saveAll($data, array('validate' => 'only', 'atomic' => false, 'deep' => false));
-		$expected = array(
-			'Attachment' => true,
-			'Comment' => true
-		);
-		$this->assertEquals($expected, $result);
-	}
 
 /**
  * testSaveAllHasMany method
@@ -4427,59 +4000,6 @@ class ModelWriteTest extends BaseModelTest {
 	}
 
 /**
- * testSaveAllValidationOnly method
- *
- * @return void
- */
-	public function testSaveAllValidationOnly() {
-		$this->loadFixtures('Comment', 'Attachment');
-		$TestModel = new Comment();
-		$TestModel->Attachment->validate = array('attachment' => 'notEmpty');
-
-		$data = array(
-			'Comment' => array(
-				'comment' => 'This is the comment'
-			),
-			'Attachment' => array(
-				'attachment' => ''
-			)
-		);
-
-		$result = $TestModel->saveAll($data, array('validate' => 'only'));
-		$this->assertFalse($result);
-
-		$TestModel = new Article();
-		$TestModel->validate = array('title' => 'notEmpty');
-		$result = $TestModel->saveAll(
-			array(
-				0 => array('title' => ''),
-				1 => array('title' => 'title 1'),
-				2 => array('title' => 'title 2'),
-			),
-			array('validate' => 'only')
-		);
-		$this->assertFalse($result);
-		$expected = array(
-			0 => array('title' => array('This field cannot be left blank')),
-		);
-		$this->assertEquals($expected, $TestModel->validationErrors);
-
-		$result = $TestModel->saveAll(
-			array(
-				0 => array('title' => 'title 0'),
-				1 => array('title' => ''),
-				2 => array('title' => 'title 2'),
-			),
-			array('validate' => 'only')
-		);
-		$this->assertFalse($result);
-		$expected = array(
-			1 => array('title' => array('This field cannot be left blank')),
-		);
-		$this->assertEquals($expected, $TestModel->validationErrors);
-	}
-
-/**
  * testSaveAllValidateFirst method
  *
  * @return void
@@ -4618,82 +4138,6 @@ class ModelWriteTest extends BaseModelTest {
 	}
 
 /**
- * testSaveAllHasManyValidationOnly method
- *
- * @return void
- */
-	public function testSaveAllHasManyValidationOnly() {
-		$this->loadFixtures('Article', 'Comment', 'Attachment');
-		$TestModel = new Article();
-		$TestModel->belongsTo = $TestModel->hasAndBelongsToMany = array();
-		$TestModel->Comment->validate = array('comment' => 'notEmpty');
-
-		$result = $TestModel->saveAll(
-			array(
-				'Article' => array('id' => 2),
-				'Comment' => array(
-					array(
-						'id' => 1,
-						'comment' => '',
-						'published' => 'Y',
-						'user_id' => 1),
-					array(
-						'id' => 2,
-						'comment' =>
-						'comment',
-						'published' => 'Y',
-						'user_id' => 1
-			))),
-			array('validate' => 'only')
-		);
-		$this->assertFalse($result);
-
-		$result = $TestModel->saveAll(
-			array(
-				'Article' => array('id' => 2),
-				'Comment' => array(
-					array(
-						'id' => 1,
-						'comment' => '',
-						'published' => 'Y',
-						'user_id' => 1
-					),
-					array(
-						'id' => 2,
-						'comment' => 'comment',
-						'published' => 'Y',
-						'user_id' => 1
-					),
-					array(
-						'id' => 3,
-						'comment' => '',
-						'published' => 'Y',
-						'user_id' => 1
-			))),
-			array(
-				'validate' => 'only',
-				'atomic' => false
-		));
-		$expected = array(
-			'Article' => true,
-			'Comment' => array(false, true, false)
-		);
-		$this->assertSame($expected, $result);
-
-		$expected = array('Comment' => array(
-			0 => array('comment' => array('This field cannot be left blank')),
-			2 => array('comment' => array('This field cannot be left blank'))
-		));
-		$this->assertEquals($expected, $TestModel->validationErrors);
-
-		$expected = array(
-			0 => array('comment' => array('This field cannot be left blank')),
-			2 => array('comment' => array('This field cannot be left blank'))
-		);
-		$this->assertEquals($expected, $TestModel->Comment->validationErrors);
-	}
-
-/**
  * test that saveAll behaves like plain save() when supplied empty data
  *
  * @link http://cakephp.lighthouseapp.com/projects/42648/tickets/277-test-saveall-with-validation-returns-incorrect-boolean-when-saving-empty-data
@@ -5774,39 +5218,6 @@ class ModelWriteTest extends BaseModelTest {
 	}
 
 /**
- * testValidateMany method
- *
- * @return void
- */
-	public function testValidateMany() {
-		$TestModel = new Article();
-		$TestModel->validate = array('title' => 'notEmpty');
-		$result = $TestModel->validateMany(
-			array(
-				0 => array('title' => ''),
-				1 => array('title' => 'title 1'),
-				2 => array('title' => 'title 2'),
-		));
-		$this->assertFalse($result);
-		$expected = array(
-			0 => array('title' => array('This field cannot be left blank')),
-		);
-		$this->assertEquals($expected, $TestModel->validationErrors);
-
-		$result = $TestModel->validateMany(
-			array(
-				0 => array('title' => 'title 0'),
-				1 => array('title' => ''),
-				2 => array('title' => 'title 2'),
-		));
-		$this->assertFalse($result);
-		$expected = array(
-			1 => array('title' => array('This field cannot be left blank')),
-		);
-		$this->assertEquals($expected, $TestModel->validationErrors);
-	}
-
-/**
  * testSaveAssociatedValidateFirst method
  *
  * @return void
@@ -5944,93 +5355,6 @@ class ModelWriteTest extends BaseModelTest {
 	}
 
 /**
- * testValidateAssociated method
- *
- * @return void
- */
-	public function testValidateAssociated() {
-		$TestModel = new Comment();
-		$TestModel->Attachment->validate = array('attachment' => 'notEmpty');
-
-		$data = array(
-			'Comment' => array(
-				'comment' => 'This is the comment'
-			),
-			'Attachment' => array(
-				'attachment' => ''
-			)
-		);
-
-		$result = $TestModel->validateAssociated($data);
-		$this->assertFalse($result);
-
-		$TestModel = new Article();
-		$TestModel->belongsTo = $TestModel->hasAndBelongsToMany = array();
-		$TestModel->Comment->validate = array('comment' => 'notEmpty');
-
-		$result = $TestModel->validateAssociated(
-			array(
-				'Article' => array('id' => 2),
-				'Comment' => array(
-					array(
-						'id' => 1,
-						'comment' => '',
-						'published' => 'Y',
-						'user_id' => 1),
-					array(
-						'id' => 2,
-						'comment' =>
-						'comment',
-						'published' => 'Y',
-						'user_id' => 1
-		))));
-		$this->assertFalse($result);
-
-		$result = $TestModel->validateAssociated(
-			array(
-				'Article' => array('id' => 2),
-				'Comment' => array(
-					array(
-						'id' => 1,
-						'comment' => '',
-						'published' => 'Y',
-						'user_id' => 1
-					),
-					array(
-						'id' => 2,
-						'comment' => 'comment',
-						'published' => 'Y',
-						'user_id' => 1
-					),
-					array(
-						'id' => 3,
-						'comment' => '',
-						'published' => 'Y',
-						'user_id' => 1
-			))),
-			array(
-				'atomic' => false
-		));
-		$expected = array(
-			'Article' => true,
-			'Comment' => array(false, true, false)
-		);
-		$this->assertSame($expected, $result);
-
-		$expected = array('Comment' => array(
-			0 => array('comment' => array('This field cannot be left blank')),
-			2 => array('comment' => array('This field cannot be left blank'))
-		));
-		$this->assertEquals($expected, $TestModel->validationErrors);
-
-		$expected = array(
-			0 => array('comment' => array('This field cannot be left blank')),
-			2 => array('comment' => array('This field cannot be left blank'))
-		);
-		$this->assertEquals($expected, $TestModel->Comment->validationErrors);
-	}
-
-/**
  * test that saveMany behaves like plain save() when suplied empty data
  *
  * @link http://cakephp.lighthouseapp.com/projects/42648/tickets/277-test-saveall-with-validation-returns-incorrect-boolean-when-saving-empty-data
@@ -6479,42 +5803,6 @@ class ModelWriteTest extends BaseModelTest {
 	}
 
 /**
- * testSaveAllFieldListHasOne method
- *
- * @return void
- */
-	public function testSaveAllFieldListHasOne() {
-		$this->loadFixtures('Attachment', 'Comment', 'Article', 'User');
-		$TestModel = new Comment();
-
-		$TestModel->validate = array('comment' => 'notEmpty');
-		$TestModel->Attachment->validate = array('attachment' => 'notEmpty');
-
-		$record = array(
-			'Comment' => array(
-				'user_id' => 1,
-				'article_id' => 1,
-				'comment' => '',
-			),
-			'Attachment' => array(
-				'attachment' => ''
-			)
-		);
-		$result = $TestModel->saveAll($record, array('validate' => 'only'));
-		$this->assertFalse($result);
-
-		$fieldList = array(
-			'Comment' => array('id', 'article_id', 'user_id'),
-			'Attachment' => array('comment_id')
-		);
-		$result = $TestModel->saveAll($record, array(
-			'fieldList' => $fieldList, 'validate' => 'only'
-		));
-		$this->assertTrue($result);
-		$this->assertEmpty($TestModel->validationErrors);
-	}
-
-/**
  * testSaveAllDeepFieldListValidateBelongsTo
  *
  * @return void

+ 181 - 0
lib/Cake/Test/Case/Model/Validator/CakeFieldTest.php

@@ -0,0 +1,181 @@
+<?php
+/**
+ * CakeFieldTest file
+ *
+ * PHP 5
+ *
+ * CakePHP(tm) Tests <http://book.cakephp.org/view/1196/Testing>
+ * Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
+ *
+ * Licensed under The MIT License
+ * Redistributions of files must retain the above copyright notice
+ *
+ * @copyright     Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
+ * @link          http://book.cakephp.org/view/1196/Testing CakePHP(tm) Tests
+ * @package       Cake.Test.Case.Model.Validator
+ * @since         CakePHP(tm) v 2.2.0
+ * @license       MIT License (http://www.opensource.org/licenses/mit-license.php)
+ */
+
+require_once dirname(dirname(__FILE__)) . DS . 'ModelTestBase.php';
+
+/**
+ * CakeFieldTest
+ *
+ * @package       Cake.Test.Case.Model.Validator
+ */
+class CakeFieldTest extends BaseModelTest {
+
+/**
+ * setUp method
+ *
+ * @return void
+ */
+	public function setUp() {
+		parent::setUp();
+		$this->Article = new Article();
+		$this->Article->set(array('title' => '', 'body' => 'no title'));
+		$this->Validator = new ModelValidator($this->Article);
+		$this->Validator->getData();
+	}
+
+/**
+ * testConstruct method
+ *
+ * @return void
+ */
+	public function testConstruct() {
+		$Field = new CakeField($this->Validator, 'title', 'notEmpty');
+
+		$this->assertEquals(array('title' => '', 'body' => 'no title'), $Field->data);
+		$this->assertEquals('title', $Field->field);
+		$this->assertEquals(array('notEmpty'), $Field->ruleSet);
+	}
+
+/**
+ * testValidate method
+ *
+ * @return void
+ */
+	public function testValidate() {
+		$Field = new CakeField($this->Validator, 'title', 'notEmpty');
+
+		$result = $Field->validate();
+		$this->assertFalse($result);
+
+		$Field = new CakeField($this->Validator, 'body', 'notEmpty');
+
+		$result = $Field->validate();
+		$this->assertTrue($result);
+
+		$Field = new CakeField($this->Validator, 'nothere', array('notEmpty' => array('rule' => 'notEmpty', 'required' => true)));
+
+		$result = $Field->validate();
+		$this->assertFalse($result);
+	}
+
+/**
+ * testGetRule method
+ *
+ * @return void
+ */
+	public function testGetRule() {
+		$rules = array('notEmpty' => array('rule' => 'notEmpty', 'message' => 'Can not be empty'));
+		$Field = new CakeField($this->Validator, 'title', $rules);
+
+		$result = $Field->getRule('notEmpty');
+		$this->assertInstanceOf('CakeRule', $result);
+		$this->assertEquals('notEmpty', $result->rule);
+		$this->assertEquals(null, $result->required);
+		$this->assertEquals(false, $result->allowEmpty);
+		$this->assertEquals(null, $result->on);
+		$this->assertEquals(true, $result->last);
+		$this->assertEquals('Can not be empty', $result->message);
+		$this->assertEquals(array('title' => '', 'body' => 'no title'), $result->data);
+	}
+
+/**
+ * testGetRules method
+ *
+ * @return void
+ */
+	public function testGetRules() {
+		$rules = array('notEmpty' => array('rule' => 'notEmpty', 'message' => 'Can not be empty'));
+		$Field = new CakeField($this->Validator, 'title', $rules);
+
+		$result = $Field->getRules();
+		$this->assertEquals(array('notEmpty'), array_keys($result));
+		$this->assertInstanceOf('CakeRule', $result['notEmpty']);
+	}
+
+/**
+ * testSetRule method
+ *
+ * @return void
+ */
+	public function testSetRule() {
+		$rules = array('notEmpty' => array('rule' => 'notEmpty', 'message' => 'Can not be empty'));
+		$Field = new CakeField($this->Validator, 'title', $rules);
+		$Rule = new CakeRule($Field, $rules['notEmpty'], 'notEmpty');
+
+		$this->assertEquals($Rule, $Field->getRule('notEmpty'));
+
+		$rules = array('validEmail' => array('rule' => 'email', 'message' => 'Invalid email'));
+		$Rule = new CakeRule($Field, $rules['validEmail'], 'validEmail');
+		$Field->setRule('validEmail', $Rule);
+		$result = $Field->getRules();
+		$this->assertEquals(array('notEmpty', 'validEmail'), array_keys($result));
+
+		$rules = array('validEmail' => array('rule' => 'email', 'message' => 'Other message'));
+		$Rule = new CakeRule($Field, $rules['validEmail'], 'validEmail');
+		$Field->setRule('validEmail', $Rule);
+		$result = $Field->getRules();
+		$this->assertEquals(array('notEmpty', 'validEmail'), array_keys($result));
+		$result = $Field->getRule('validEmail');
+		$this->assertInstanceOf('CakeRule', $result);
+		$this->assertEquals('email', $result->rule);
+		$this->assertEquals(null, $result->required);
+		$this->assertEquals(false, $result->allowEmpty);
+		$this->assertEquals(null, $result->on);
+		$this->assertEquals(true, $result->last);
+		$this->assertEquals('Other message', $result->message);
+		$this->assertEquals(array('title' => '', 'body' => 'no title'), $result->data);
+	}
+
+/**
+ * testSetRules method
+ *
+ * @return void
+ */
+	public function testSetRules() {
+		$rule = array('notEmpty' => array('rule' => 'notEmpty', 'message' => 'Can not be empty'));
+		$Field = new CakeField($this->Validator, 'title', $rule);
+		$RuleEmpty = new CakeRule($Field, $rule['notEmpty'], 'notEmpty');
+
+		$rule = array('validEmail' => array('rule' => 'email', 'message' => 'Invalid email'));
+		$RuleEmail = new CakeRule($Field, $rule['validEmail'], 'validEmail');
+
+		$rules = array('validEmail' => $RuleEmail);
+		$Field->setRules($rules, false);
+		$result = $Field->getRules();
+		$this->assertEquals(array('validEmail'), array_keys($result));
+
+		$rules = array('notEmpty' => $RuleEmpty);
+		$Field->setRules($rules, true);
+		$result = $Field->getRules();
+		$this->assertEquals(array('validEmail', 'notEmpty'), array_keys($result));
+	}
+
+/**
+ * testGetValidator method
+ *
+ * @return void
+ */
+	public function testGetValidator() {
+		$rule = array('notEmpty' => array('rule' => 'notEmpty', 'message' => 'Can not be empty'));
+		$Field = new CakeField($this->Validator, 'title', $rule);
+		$result = $Field->getValidator();
+		$this->assertInstanceOf('ModelValidator', $result);
+	}
+
+}

+ 52 - 0
lib/Cake/Test/Case/Model/Validator/CakeRuleTest.php

@@ -0,0 +1,52 @@
+<?php
+/**
+ * CakeRuleTest file
+ *
+ * PHP 5
+ *
+ * CakePHP(tm) Tests <http://book.cakephp.org/view/1196/Testing>
+ * Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
+ *
+ * Licensed under The MIT License
+ * Redistributions of files must retain the above copyright notice
+ *
+ * @copyright     Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
+ * @link          http://book.cakephp.org/view/1196/Testing CakePHP(tm) Tests
+ * @package       Cake.Test.Case.Model.Validator
+ * @since         CakePHP(tm) v 2.2.0
+ * @license       MIT License (http://www.opensource.org/licenses/mit-license.php)
+ */
+
+require_once dirname(dirname(__FILE__)) . DS . 'ModelTestBase.php';
+
+/**
+ * CakeRuleTest
+ *
+ * @package       Cake.Test.Case.Model.Validator
+ */
+class CakeRuleTest extends BaseModelTest {
+
+/**
+ * setUp method
+ *
+ * @return void
+ */
+	public function setUp() {
+		parent::setUp();
+		$Article = new Article();
+		$Article->set(array('title' => '', 'body' => 'no title'));
+		$this->Validator = new ModelValidator($Article);
+		$this->Validator->getData();
+		$rule = array('notEmpty' => array('rule' => 'notEmpty', 'required' => true, 'last' => false));
+		$this->Field = new CakeField($this->Validator, 'body', $rule);
+	}
+
+/**
+ * testIsValid method
+ *
+ * @return void
+ */
+	public function testIsValid() {
+
+	}
+}