Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Refactoring again CakeRule, making all tests pass

  • Loading branch information...
commit d348bf68074fb9100dd3a26f260e73985bf1e28b 1 parent 9404085
@lorenzo lorenzo authored
View
7 lib/Cake/Model/ModelValidator.php
@@ -376,8 +376,11 @@ public function getOptions($name = null) {
* @param string $validationDomain [optional] The validation domain to be used.
* @return ModelValidator
*/
- public function setValidationDomain($validationDomain) {
- $model->validationDomain = $validationDomain;
+ public function setValidationDomain($validationDomain = null) {
+ if (empty($validationDomain)) {
+ $validationDomain = 'default';
+ }
+ $this->getModel()->validationDomain = $validationDomain;
return $this;
}
View
13 lib/Cake/Model/Validator/CakeField.php
@@ -85,7 +85,7 @@ public function __construct($fieldName, $ruleSet) {
}
foreach ($ruleSet as $index => $validateProp) {
- $this->_rules[$index] = new CakeRule($this->field, $validateProp, $index);
+ $this->_rules[$index] = new CakeRule($index, $validateProp);
}
$this->ruleSet = $ruleSet;
}
@@ -122,17 +122,16 @@ public function validate($data, $isUpdate = false) {
continue;
}
- $checkRequired = $rule->checkRequired($data);
+ $checkRequired = $rule->checkRequired($this->field, $data);
if (!$checkRequired && array_key_exists($this->field, $data)) {
- if ($rule->checkEmpty($data)) {
+ if ($rule->checkEmpty($this->field, $data)) {
break;
}
- $rule->dispatchValidation($data, $this->_methods);
+ $rule->dispatchValidation($this->field, $data, $this->_methods);
}
- if ($checkRequired || !$rule->isValid($data)) {
+ if ($checkRequired || !$rule->isValid()) {
$errors[] = $this->_processValidationResponse($rule);
-
if ($rule->isLast()) {
break;
}
@@ -234,8 +233,6 @@ protected function _processValidationResponse($rule) {
} else {
$message = __d($this->_validationDomain, $name);
}
- //} elseif (!$rule->checkRequired() && is_numeric($name) && count($this->ruleSet) > 1) {
- // $this->_errorMessage = $this->_index + 1;
} else {
$message = __d('cake_dev', 'This field cannot be left blank');
}
View
38 lib/Cake/Model/Validator/CakeRule.php
@@ -31,13 +31,6 @@
class CakeRule {
/**
- * Holds a reference to the parent field
- *
- * @var CakeField
- */
- protected $_field = null;
-
-/**
* The 'valid' value
*
* @var mixed
@@ -127,8 +120,7 @@ class CakeRule {
* @param array $validator [optional] The validator properties
* @param mixed $index [optional]
*/
- public function __construct($field, $validator = array(), $index = null) {
- $this->_field = $field;
+ public function __construct($index = null, $validator = array()) {
$this->_index = $index;
$this->_addValidatorProps($validator);
}
@@ -170,12 +162,12 @@ public function isRequired() {
* @param array $data data to check rule against
* @return boolean
*/
- public function checkRequired(&$data) {
+ public function checkRequired($field, &$data) {
return (
- (!isset($data[$this->_field]) && $this->isRequired() === true) ||
+ (!isset($data[$field]) && $this->isRequired() === true) ||
(
- isset($data[$this->_field]) && (empty($data[$this->_field]) &&
- !is_numeric($data[$this->_field])) && $this->allowEmpty === false
+ isset($data[$field]) && (empty($data[$field]) &&
+ !is_numeric($data[$field])) && $this->allowEmpty === false
)
);
}
@@ -186,8 +178,8 @@ public function checkRequired(&$data) {
* @param array $data data to check rule against
* @return boolean
*/
- public function checkEmpty(&$data) {
- if (empty($data[$this->_field]) && $data[$this->_field] != '0' && $this->allowEmpty === true) {
+ public function checkEmpty($field, &$data) {
+ if (empty($data[$field]) && $data[$field] != '0' && $this->allowEmpty === true) {
return true;
}
return false;
@@ -261,21 +253,21 @@ public function isUpdate($exists = false) {
*
* @return boolean True if the rule could be dispatched, false otherwise
*/
- public function dispatchValidation(&$data, &$methods) {
- $this->_parseRule($data);
+ public function dispatchValidation($field, &$data, &$methods) {
+ $this->_parseRule($field, $data);
$validator = $this->getPropertiesArray();
$rule = strtolower($this->_rule);
if (isset($methods[$rule])) {
$this->_ruleParams[] = array_merge($validator, $this->_passedOptions);
- $this->_ruleParams[0] = array($this->_field => $this->_ruleParams[0]);
+ $this->_ruleParams[0] = array($field => $this->_ruleParams[0]);
$this->_valid = call_user_func_array($methods[$rule], $this->_ruleParams);
} elseif (class_exists('Validation') && method_exists('Validation', $this->_rule)) {
$this->_valid = call_user_func_array(array('Validation', $this->_rule), $this->_ruleParams);
} elseif (is_string($validator['rule'])) {
- $this->_valid = preg_match($this->_rule, $data[$this->_field]);
+ $this->_valid = preg_match($this->_rule, $data[$field]);
} elseif (Configure::read('debug') > 0) {
- trigger_error(__d('cake_dev', 'Could not find validation handler %s for %s', $this->_rule, $this->_field), E_USER_WARNING);
+ trigger_error(__d('cake_dev', 'Could not find validation handler %s for %s', $this->_rule, $field), E_USER_WARNING);
return false;
}
@@ -319,13 +311,13 @@ protected function _addValidatorProps($validator = array()) {
*
* @return void
*/
- protected function _parseRule(&$data) {
+ protected function _parseRule($field, &$data) {
if (is_array($this->rule)) {
$this->_rule = $this->rule[0];
- $this->_ruleParams = array_merge(array($data[$this->_field]), array_values(array_slice($this->rule, 1)));
+ $this->_ruleParams = array_merge(array($data[$field]), array_values(array_slice($this->rule, 1)));
} else {
$this->_rule = $this->rule;
- $this->_ruleParams = array($data[$this->_field]);
+ $this->_ruleParams = array($data[$field]);
}
}
View
3  lib/Cake/Test/Case/Model/ModelValidationTest.php
@@ -1660,10 +1660,9 @@ public function testGetMethods() {
$Validator = $TestModel->validator();
$result = $Validator->getMethods();
- $this->assertEquals(array('model', 'behaviors', 'validator'), array_keys($result));
$expected = array_map('strtolower', get_class_methods('Article'));
- $this->assertEquals($expected, $result['model']);
+ $this->assertEquals($expected, array_keys($result));
}
/**
View
68 lib/Cake/Test/Case/Model/Validator/CakeFieldTest.php
@@ -17,14 +17,14 @@
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
-require_once dirname(dirname(__FILE__)) . DS . 'ModelTestBase.php';
+App::uses('CakeField', 'Model/Validator');
/**
* CakeFieldTest
*
* @package Cake.Test.Case.Model.Validator
*/
-class CakeFieldTest extends BaseModelTest {
+class CakeFieldTest extends CakeTestModel {
/**
* setUp method
@@ -32,11 +32,7 @@ class CakeFieldTest extends BaseModelTest {
* @return void
*/
public function setUp() {
- $this->skipIf(true);
parent::setUp();
- $this->Article = new Article();
- $this->Article->set(array('title' => '', 'body' => 'no title'));
- $this->Validator = new ModelValidator($this->Article);
}
/**
@@ -45,20 +41,26 @@ public function setUp() {
* @return void
*/
public function testValidate() {
- $Field = new CakeField($this->Validator, 'title', 'notEmpty');
+ $Field = new CakeField('title', 'notEmpty');
+ $data = array(
+ 'title' => '',
+ 'body' => 'a body'
+ );
- $result = $Field->validate();
- $this->assertFalse($result);
+ $result = $Field->validate($data);
+ $expected = array('This field cannot be left blank');
+ $this->assertEquals($expected, $result);
- $Field = new CakeField($this->Validator, 'body', 'notEmpty');
+ $Field = new CakeField('body', 'notEmpty');
- $result = $Field->validate();
- $this->assertTrue($result);
+ $result = $Field->validate($data);
+ $this->assertEmpty($result);
- $Field = new CakeField($this->Validator, 'nothere', array('notEmpty' => array('rule' => 'notEmpty', 'required' => true)));
+ $Field = new CakeField('nothere', array('notEmpty' => array('rule' => 'notEmpty', 'required' => true)));
- $result = $Field->validate();
- $this->assertFalse($result);
+ $result = $Field->validate($data);
+ $expected = array('notEmpty');
+ $this->assertEquals($expected, $result);
}
/**
@@ -68,7 +70,11 @@ public function testValidate() {
*/
public function testGetRule() {
$rules = array('notEmpty' => array('rule' => 'notEmpty', 'message' => 'Can not be empty'));
- $Field = new CakeField($this->Validator, 'title', $rules);
+ $Field = new CakeField('title', $rules);
+ $data = array(
+ 'title' => '',
+ 'body' => 'a body'
+ );
$result = $Field->getRule('notEmpty');
$this->assertInstanceOf('CakeRule', $result);
@@ -78,7 +84,6 @@ public function testGetRule() {
$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);
}
/**
@@ -88,7 +93,7 @@ public function testGetRule() {
*/
public function testGetRules() {
$rules = array('notEmpty' => array('rule' => 'notEmpty', 'message' => 'Can not be empty'));
- $Field = new CakeField($this->Validator, 'title', $rules);
+ $Field = new CakeField('title', $rules);
$result = $Field->getRules();
$this->assertEquals(array('notEmpty'), array_keys($result));
@@ -102,19 +107,19 @@ public function testGetRules() {
*/
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');
+ $Field = new CakeField('title', $rules);
+ $Rule = new CakeRule('notEmpty', $rules['notEmpty']);
$this->assertEquals($Rule, $Field->getRule('notEmpty'));
$rules = array('validEmail' => array('rule' => 'email', 'message' => 'Invalid email'));
- $Rule = new CakeRule($Field, $rules['validEmail'], 'validEmail');
+ $Rule = new CakeRule('validEmail', $rules['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');
+ $Rule = new CakeRule('validEmail', $rules['validEmail']);
$Field->setRule('validEmail', $Rule);
$result = $Field->getRules();
$this->assertEquals(array('notEmpty', 'validEmail'), array_keys($result));
@@ -126,7 +131,6 @@ public function testSetRule() {
$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);
}
/**
@@ -136,11 +140,11 @@ public function testSetRule() {
*/
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');
+ $Field = new CakeField('title', $rule);
+ $RuleEmpty = new CakeRule('title', $rule['notEmpty'], 'notEmpty');
$rule = array('validEmail' => array('rule' => 'email', 'message' => 'Invalid email'));
- $RuleEmail = new CakeRule($Field, $rule['validEmail'], 'validEmail');
+ $RuleEmail = new CakeRule('email', $rule['validEmail'], 'validEmail');
$rules = array('validEmail' => $RuleEmail);
$Field->setRules($rules, false);
@@ -153,16 +157,4 @@ public function testSetRules() {
$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
10 lib/Cake/Test/Case/Model/Validator/CakeRuleTest.php
@@ -17,14 +17,14 @@
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
-require_once dirname(dirname(__FILE__)) . DS . 'ModelTestBase.php';
+App::uses('CakeRule', 'Model/Validator');
/**
* CakeRuleTest
*
* @package Cake.Test.Case.Model.Validator
*/
-class CakeRuleTest extends BaseModelTest {
+class CakeRuleTest extends CakeTestModel {
/**
* setUp method
@@ -33,12 +33,6 @@ class CakeRuleTest extends BaseModelTest {
*/
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);
}
/**
Please sign in to comment.
Something went wrong with that request. Please try again.