Skip to content

Commit

Permalink
Revert default value of allowEmpty.
Browse files Browse the repository at this point in the history
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
  • Loading branch information
markstory committed Jun 25, 2012
1 parent b2ccdd4 commit eb7b66b
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 40 deletions.
2 changes: 1 addition & 1 deletion lib/Cake/Model/Validator/CakeValidationRule.php
Expand Up @@ -85,7 +85,7 @@ class CakeValidationRule {
*
* @var boolean
*/
public $allowEmpty = false;
public $allowEmpty = null;

/**
* The 'on' key
Expand Down
20 changes: 20 additions & 0 deletions lib/Cake/Test/Case/Model/ModelValidationTest.php
Expand Up @@ -2178,4 +2178,24 @@ public function testCustomMethodsWithCakeValidationSet() {
$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());
}

}
81 changes: 44 additions & 37 deletions lib/Cake/Test/Case/Model/ModelWriteTest.php
Expand Up @@ -3002,23 +3002,6 @@ public function testSaveAllAtomic() {
), 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(
Expand All @@ -3034,6 +3017,25 @@ public function testSaveAllAtomic() {
))
), 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);
}

/**
Expand Down Expand Up @@ -3187,6 +3189,25 @@ public function testSaveAllDeepMany() {
$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' => '',
Expand Down Expand Up @@ -3220,6 +3241,7 @@ public function testSaveAllDeepMany() {
)
),
1 => array(
'body' => array('This field cannot be left blank'),
'Comment' => array(
0 => array(
'User' => array(
Expand All @@ -3234,25 +3256,6 @@ public function testSaveAllDeepMany() {
);
$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
Expand Down Expand Up @@ -3661,7 +3664,8 @@ public function testSaveAllNotDeepMany() {

$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)
Expand All @@ -3682,6 +3686,9 @@ public function testSaveAllNotDeepMany() {
$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;
Expand Down
Expand Up @@ -129,7 +129,7 @@ public function testIsRequired() {
*
* @return void
*/
public function testIsEmplyAllowed() {
public function testIsEmptyAllowed() {
$def = array('rule' => 'aRule', 'allowEmpty' => true);
$Rule = new CakeValidationRule($def);
$this->assertTrue($Rule->isEmptyAllowed());
Expand Down
6 changes: 5 additions & 1 deletion lib/Cake/Test/Case/Model/models.php
Expand Up @@ -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
Expand Down

0 comments on commit eb7b66b

Please sign in to comment.