Revert default value of allowEmpty.

In 2.1, the default value was null, which does not impart any behavior.
In 2.2 this was changed to false, which makes it hard to validate ''
with validation methods.  Move some tests around and update tests that
probably should have been failing before.

Fixes #2983
This commit is contained in:
mark_story 2012-06-24 20:06:14 -04:00
parent b2ccdd46e5
commit eb7b66b37c
5 changed files with 71 additions and 40 deletions

View file

@ -85,7 +85,7 @@ class CakeValidationRule {
*
* @var boolean
*/
public $allowEmpty = false;
public $allowEmpty = null;
/**
* The 'on' key

View file

@ -2178,4 +2178,24 @@ class ModelValidationTest extends BaseModelTest {
$this->assertEquals($expected, $result);
}
public function testCustomMethodWithEmptyValue() {
$this->loadFixtures('Article');
$model = $this->getMock('Article', array('isLegit'));
$model->validate = array(
'title' => array(
'custom' => array(
'rule' => array('isLegit'),
'message' => 'is no good'
)
)
);
$model->expects($this->once())
->method('isLegit')
->will($this->returnValue(false));
$model->set(array('title' => ''));
$this->assertFalse($model->validates());
}
}

View file

@ -3002,23 +3002,6 @@ class ModelWriteTest extends BaseModelTest {
), array('atomic' => false));
$this->assertSame($result, array(true, true, true));
$TestModel->validate = array('title' => 'notEmpty', 'author_id' => 'numeric');
$result = $TestModel->saveAll(array(
array(
'id' => '1',
'title' => 'Un-Baleeted First Post',
'body' => 'Not Baleeted!',
'published' => 'Y'
),
array(
'id' => '2',
'title' => '',
'body' => 'Trying to get away with an empty title'
)
), array('validate' => true, 'atomic' => false));
$this->assertSame(array(true, false), $result);
$result = $TestModel->saveAll(array(
'Article' => array('id' => 2),
'Comment' => array(
@ -3034,6 +3017,25 @@ class ModelWriteTest extends BaseModelTest {
))
), array('validate' => true, 'atomic' => false));
$this->assertSame($result, array('Article' => true, 'Comment' => array(true, true)));
$TestModel->validate = array(
'title' => 'notEmpty',
'author_id' => 'numeric'
);
$result = $TestModel->saveAll(array(
array(
'id' => '1',
'title' => 'Un-Baleeted First Post',
'body' => 'Not Baleeted!',
'published' => 'Y'
),
array(
'id' => '2',
'title' => '',
'body' => 'Trying to get away with an empty title'
)
), array('validate' => true, 'atomic' => false));
$this->assertSame(array(true, false), $result);
}
/**
@ -3187,6 +3189,25 @@ class ModelWriteTest extends BaseModelTest {
$TestModel->hasMany['Comment']['order'] = array('Comment.created' => 'ASC');
$TestModel->hasAndBelongsToMany = array();
$data = array(
array(
'Article' => array('id' => 1),
'Comment' => array(
array('comment' => 'First comment deepsaved article 1', 'published' => 'Y', 'User' => array('user' => 'savemany', 'password' => 'manysaved')),
array('comment' => 'Second comment deepsaved article 1', 'published' => 'Y', 'user_id' => 2)
)
),
array(
'Article' => array('id' => 2),
'Comment' => array(
array('comment' => 'First comment deepsaved article 2', 'published' => 'Y', 'User' => array('user' => 'savemore', 'password' => 'moresaved')),
array('comment' => 'Second comment deepsaved article 2', 'published' => 'Y', 'user_id' => 2)
)
)
);
$result = $TestModel->saveAll($data, array('deep' => true));
$this->assertTrue($result);
$data = array(
array(
'id' => 1, 'body' => '',
@ -3220,6 +3241,7 @@ class ModelWriteTest extends BaseModelTest {
)
),
1 => array(
'body' => array('This field cannot be left blank'),
'Comment' => array(
0 => array(
'User' => array(
@ -3234,25 +3256,6 @@ class ModelWriteTest extends BaseModelTest {
);
$result = $TestModel->validationErrors;
$this->assertSame($expected, $result);
$data = array(
array(
'Article' => array('id' => 1),
'Comment' => array(
array('comment' => 'First comment deepsaved article 1', 'published' => 'Y', 'User' => array('user' => 'savemany', 'password' => 'manysaved')),
array('comment' => 'Second comment deepsaved article 1', 'published' => 'Y', 'user_id' => 2)
)
),
array(
'Article' => array('id' => 2),
'Comment' => array(
array('comment' => 'First comment deepsaved article 2', 'published' => 'Y', 'User' => array('user' => 'savemore', 'password' => 'moresaved')),
array('comment' => 'Second comment deepsaved article 2', 'published' => 'Y', 'user_id' => 2)
)
)
);
$result = $TestModel->saveAll($data, array('deep' => true));
$this->assertTrue($result);
}
/**
* testSaveAllDeepValidateOnly
@ -3661,7 +3664,8 @@ class ModelWriteTest extends BaseModelTest {
$data = array(
array(
'id' => 1, 'body' => '',
'id' => 1,
'body' => '',
'Comment' => array(
array('comment' => '', 'published' => 'Y', 'User' => array('user' => '', 'password' => 'manysaved')),
array('comment' => 'Second comment deepsaved article 1', 'published' => 'Y', 'user_id' => 2)
@ -3682,6 +3686,9 @@ class ModelWriteTest extends BaseModelTest {
$expected = array(
0 => array(
'body' => array('This field cannot be left blank')
),
1 => array(
'body' => array('This field cannot be left blank')
)
);
$result = $TestModel->validationErrors;

View file

@ -129,7 +129,7 @@ class CakeValidationRuleTest extends CakeTestCase {
*
* @return void
*/
public function testIsEmplyAllowed() {
public function testIsEmptyAllowed() {
$def = array('rule' => 'aRule', 'allowEmpty' => true);
$Rule = new CakeValidationRule($def);
$this->assertTrue($Rule->isEmptyAllowed());

View file

@ -273,7 +273,11 @@ class Article extends CakeTestModel {
*
* @var array
*/
public $validate = array('user_id' => 'numeric', 'title' => array('allowEmpty' => false, 'rule' => 'notEmpty'), 'body' => 'notEmpty');
public $validate = array(
'user_id' => 'numeric',
'title' => array('required' => false, 'rule' => 'notEmpty'),
'body' => 'notEmpty',
);
/**
* beforeSaveReturn property