Adding tests for Validator

This commit is contained in:
Ceeram 2012-04-23 13:02:26 +02:00
parent 324684c14f
commit ec9966ec6b
9 changed files with 1224 additions and 784 deletions

View file

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

View file

@ -15,7 +15,7 @@
* @copyright Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org) * @copyright Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://cakephp.org CakePHP(tm) Project * @link http://cakephp.org CakePHP(tm) Project
* @package Cake.Model * @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) * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/ */
App::uses('CakeField', 'Model/Validator'); App::uses('CakeField', 'Model/Validator');
@ -72,7 +72,7 @@ class ModelValidator {
public $options = array(); public $options = array();
/** /**
* Holds the ModelFields * Holds the CakeField objects array
* *
* @var 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. * 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. * @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) { public function getFields($name = null) {
if ($name !== null && !empty($this->_fields[$name])) { if ($name !== null && !empty($this->_fields[$name])) {
return $this->_fields[$name]; return $this->_fields[$name];
} elseif ($name !==null) {
return false;
} }
return $this->_fields; 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. * 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 * @param boolean $reset If true will reset the Validator $validate array to the Model's default

View file

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

View file

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

View file

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

File diff suppressed because it is too large Load diff

View file

@ -2958,16 +2958,6 @@ class ModelWriteTest extends BaseModelTest {
); );
$this->assertEquals($expected['Comment'], $model->validationErrors); $this->assertEquals($expected['Comment'], $model->validationErrors);
$this->assertEquals($expected['Attachment'], $model->Attachment->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)); $result = $TestModel->saveAll($data, array('deep' => true));
$this->assertTrue($result); $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 * testSaveAllNotDeepAssociated method
@ -3676,148 +3391,6 @@ class ModelWriteTest extends BaseModelTest {
$result = $TestModel->saveAll($data, array('deep' => false)); $result = $TestModel->saveAll($data, array('deep' => false));
$this->assertTrue($result); $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 * testSaveAllHasMany method
@ -4426,59 +3999,6 @@ class ModelWriteTest extends BaseModelTest {
$this->assertEquals($errors, $TestModel->validationErrors); $this->assertEquals($errors, $TestModel->validationErrors);
} }
/**
* 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 * testSaveAllValidateFirst method
* *
@ -4617,82 +4137,6 @@ class ModelWriteTest extends BaseModelTest {
$this->assertEquals($expected, $result); $this->assertEquals($expected, $result);
} }
/**
* 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 * test that saveAll behaves like plain save() when supplied empty data
* *
@ -5773,39 +5217,6 @@ class ModelWriteTest extends BaseModelTest {
$this->assertEquals($errors, $TestModel->validationErrors); $this->assertEquals($errors, $TestModel->validationErrors);
} }
/**
* 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 * testSaveAssociatedValidateFirst method
* *
@ -5943,93 +5354,6 @@ class ModelWriteTest extends BaseModelTest {
$this->assertEquals($expected, $result); $this->assertEquals($expected, $result);
} }
/**
* 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 * test that saveMany behaves like plain save() when suplied empty data
* *
@ -6478,42 +5802,6 @@ class ModelWriteTest extends BaseModelTest {
$this->assertEquals('', $result[0]['Comment'][1]['comment']); $this->assertEquals('', $result[0]['Comment'][1]['comment']);
} }
/**
* 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 * testSaveAllDeepFieldListValidateBelongsTo
* *

View file

@ -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);
}
}

View file

@ -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() {
}
}