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

View file

@ -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

View file

@ -14,17 +14,17 @@
*
* @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 {

View file

@ -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,9 +23,9 @@ 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 {
@ -33,7 +33,7 @@ class CakeRule {
/**
* Holds a reference to the parent field
*
* @var ModelField
* @var CakeField
*/
protected $_field = null;
@ -52,7 +52,7 @@ class CakeRule {
protected $_valid = true;
/**
* Holds the index under which the Vaildator was attached
* Holds the index under which the Validator was attached
*
* @var mixed
*/
@ -145,7 +145,7 @@ class CakeRule {
/**
* Constructor
*
* @param ModelField $field
* @param CakeField $field
* @param array $validator [optional] The validator properties
* @param mixed $index [optional]
*/
@ -172,7 +172,7 @@ class CakeRule {
* @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;
}
@ -185,7 +185,7 @@ class CakeRule {
* @return boolean
*/
public function isRequired() {
if ($this->required === true || $this->required === false) {
if (is_bool($this->required)) {
return $this->required;
}
@ -237,7 +237,7 @@ 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)) {
@ -269,7 +269,7 @@ class CakeRule {
/**
* Gets the parent field
*
* @return ModelField
* @return CakeField
*/
public function getField() {
return $this->_field;
@ -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);

View file

@ -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 Load diff

View file

@ -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
@ -4426,59 +3999,6 @@ class ModelWriteTest extends BaseModelTest {
$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
*
@ -4617,82 +4137,6 @@ class ModelWriteTest extends BaseModelTest {
$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
*
@ -5773,39 +5217,6 @@ class ModelWriteTest extends BaseModelTest {
$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
*
@ -5943,93 +5354,6 @@ class ModelWriteTest extends BaseModelTest {
$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
*
@ -6478,42 +5802,6 @@ class ModelWriteTest extends BaseModelTest {
$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
*

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