Skip to content

Commit

Permalink
Improving test coverage for CakeRule
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzo committed Apr 30, 2012
1 parent d348bf6 commit 72e8992
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 9 deletions.
2 changes: 1 addition & 1 deletion lib/Cake/Model/Validator/CakeField.php
Expand Up @@ -127,7 +127,7 @@ public function validate($data, $isUpdate = false) {
if ($rule->checkEmpty($this->field, $data)) {
break;
}
$rule->dispatchValidation($this->field, $data, $this->_methods);
$rule->process($this->field, $data, $this->_methods);
}

if ($checkRequired || !$rule->isValid()) {
Expand Down
20 changes: 14 additions & 6 deletions lib/Cake/Model/Validator/CakeRule.php
Expand Up @@ -148,8 +148,10 @@ public function isRequired() {
return $this->required;
}
if (in_array($this->required, array('create', 'update'), true)) {
if ($this->required === 'create' && !$this->_recordExists || $this->required === 'update' && $this->_recordExists) {
if ($this->required === 'create' && !$this->isUpdate() || $this->required === 'update' && $this->isUpdate()) {
$this->required = true;
} else {
$this->required = false;
}
}

Expand Down Expand Up @@ -192,7 +194,7 @@ public function checkEmpty($field, &$data) {
*/
public function skip() {
if (!empty($this->on)) {
if ($this->on == 'create' && $this->_recordExists || $this->on == 'update' && !$this->_recordExists) {
if ($this->on == 'create' && $this->isUpdate() || $this->on == 'update' && !$this->isUpdate()) {
return true;
}
}
Expand Down Expand Up @@ -242,18 +244,24 @@ public function getPropertiesArray() {
* ir refers to wheter the model record it is validating exists
* exists in the collection or not (create or update operation)
*
* @return void
* If called with no parameters it will return whether this rule
* is configured for update operations or not.
*
* @return boolean
**/
public function isUpdate($exists = false) {
$this->_recordExists = $exists;
public function isUpdate($exists = null) {
if ($exists === null) {
return $this->_recordExists;
}
return $this->_recordExists = $exists;
}

/**
* Dispatches the validation rule to the given validator method
*
* @return boolean True if the rule could be dispatched, false otherwise
*/
public function dispatchValidation($field, &$data, &$methods) {
public function process($field, &$data, &$methods) {
$this->_parseRule($field, $data);

$validator = $this->getPropertiesArray();
Expand Down
93 changes: 91 additions & 2 deletions lib/Cake/Test/Case/Model/Validator/CakeRuleTest.php
Expand Up @@ -24,7 +24,7 @@
*
* @package Cake.Test.Case.Model.Validator
*/
class CakeRuleTest extends CakeTestModel {
class CakeRuleTest extends CakeTestCase {

/**
* setUp method
Expand All @@ -36,11 +36,100 @@ public function setUp() {
}

/**
* testIsValid method
* Auxiliary method to test custom validators
*
* @return boolean
**/
public function myTestRule() {
return false;
}

/**
* Auxiliary method to test custom validators
*
* @return boolean
**/
public function myTestRule2() {
return true;
}

/**
* Auxiliary method to test custom validators
*
* @return string
**/
public function myTestRule3() {
return 'string';
}

/**
* Test isValid method
*
* @return void
*/
public function testIsValid() {
$def = array('rule' => 'notEmpty', 'message' => 'Can not be empty');
$data = array(
'fieldName' => ''
);
$methods = array();

$Rule = new CakeRule('notEmpty', $def);
$Rule->process('fieldName', $data, $methods);
$this->assertFalse($Rule->isValid());

$data = array('fieldName' => 'not empty');
$Rule->process('fieldName', $data, $methods);
$this->assertTrue($Rule->isValid());
}
/**
* tests that passing custom validation methods work
*
* @return void
*/
public function testCustomMethods() {
$def = array('rule' => 'myTestRule');
$data = array(
'fieldName' => 'some data'
);
$methods = array('mytestrule' => array($this, 'myTestRule'));

$Rule = new CakeRule('custom', $def);
$Rule->process('fieldName', $data, $methods);
$this->assertFalse($Rule->isValid());

$methods = array('mytestrule' => array($this, 'myTestRule2'));
$Rule->process('fieldName', $data, $methods);
$this->assertTrue($Rule->isValid());

$methods = array('mytestrule' => array($this, 'myTestRule3'));
$Rule->process('fieldName', $data, $methods);
$this->assertFalse($Rule->isValid());
}

/**
* Test isRequired method
*
* @return void
*/
public function testIsRequired() {
$def = array('rule' => 'notEmpty', 'required' => true);
$Rule = new CakeRule('required', $def);
$this->assertTrue($Rule->isRequired());

$def = array('rule' => 'notEmpty', 'required' => false);
$Rule = new CakeRule('required', $def);
$this->assertFalse($Rule->isRequired());

$def = array('rule' => 'notEmpty', 'required' => 'create');
$Rule = new CakeRule('required', $def);
$this->assertTrue($Rule->isRequired());

$def = array('rule' => 'notEmpty', 'required' => 'update');
$Rule = new CakeRule('required', $def);
$this->assertFalse($Rule->isRequired());

$Rule->isUpdate(true);
$this->assertTrue($Rule->isRequired());
}
}

0 comments on commit 72e8992

Please sign in to comment.