From c621985f69930d97beb4778bbc4d385f869dc954 Mon Sep 17 00:00:00 2001 From: euromark Date: Mon, 7 Oct 2013 23:45:27 +0200 Subject: [PATCH 1/3] Fix whitelist to be modifiable from behaviors to work with validate. --- lib/Cake/Model/ModelValidator.php | 6 ++- .../Test/Case/Model/ModelValidationTest.php | 42 +++++++++++++++++++ 2 files changed, 47 insertions(+), 1 deletion(-) diff --git a/lib/Cake/Model/ModelValidator.php b/lib/Cake/Model/ModelValidator.php index bbc54c8da..70674938e 100644 --- a/lib/Cake/Model/ModelValidator.php +++ b/lib/Cake/Model/ModelValidator.php @@ -249,7 +249,11 @@ class ModelValidator implements ArrayAccess, IteratorAggregate, Countable { return $model->validationErrors; } - $fieldList = isset($options['fieldList']) ? $options['fieldList'] : array(); + $fieldList = $model->whitelist; + if (empty($fieldList) && !empty($options['fieldList'])) { + $fieldList = $options['fieldList']; + } + $exists = $model->exists(); $methods = $this->getMethods(); $fields = $this->_validationList($fieldList); diff --git a/lib/Cake/Test/Case/Model/ModelValidationTest.php b/lib/Cake/Test/Case/Model/ModelValidationTest.php index d313a33df..e8114b057 100644 --- a/lib/Cake/Test/Case/Model/ModelValidationTest.php +++ b/lib/Cake/Test/Case/Model/ModelValidationTest.php @@ -612,6 +612,30 @@ class ModelValidationTest extends BaseModelTest { $this->assertEquals(0, $joinRecords, 'Records were saved on the join table. %s'); } + public function testValidateWithFieldListAndBehavior() { + $TestModel = new ValidationTest1(); + $TestModel->validate = array( + 'title' => array( + 'rule' => 'alphaNumeric', + 'required' => true + ), + 'name' => array( + 'rule' => 'alphaNumeric', + 'required' => true + )); + $TestModel->Behaviors->attach('ValidationRule', array('fields' => array('name'))); + + $data = array( + 'title' => '', + 'name' => '', + ); + $result = $TestModel->save($data, array('fieldList' => array('title'))); + $this->assertFalse($result); + + $expected = array('title' => array('This field cannot be left blank'), 'name' => array('This field cannot be left blank')); + $this->assertEquals($expected, $TestModel->validationErrors); + } + /** * test that saveAll and with models with validation interact well * @@ -2380,3 +2404,21 @@ class ModelValidationTest extends BaseModelTest { } } + +/** + * Behavior for testing validation rules. + */ +class ValidationRuleBehavior extends ModelBehavior { + + public function setup(Model $Model, $config = array()) { + $this->settings[$Model->alias] = $config; + } + + public function beforeValidate(Model $Model, $options = array()) { + $fields = $this->settings[$Model->alias]['fields']; + foreach ($fields as $field) { + $Model->whitelist[] = $field; + } + } + +} \ No newline at end of file From 35ca40635e9616ff083084d60915ad3a13a070be Mon Sep 17 00:00:00 2001 From: euromark Date: Tue, 8 Oct 2013 00:32:11 +0200 Subject: [PATCH 2/3] Add doc block --- lib/Cake/Test/Case/Model/ModelValidationTest.php | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/lib/Cake/Test/Case/Model/ModelValidationTest.php b/lib/Cake/Test/Case/Model/ModelValidationTest.php index e8114b057..33560194e 100644 --- a/lib/Cake/Test/Case/Model/ModelValidationTest.php +++ b/lib/Cake/Test/Case/Model/ModelValidationTest.php @@ -612,6 +612,12 @@ class ModelValidationTest extends BaseModelTest { $this->assertEquals(0, $joinRecords, 'Records were saved on the join table. %s'); } +/** + * Test that if a behavior modifies the model's whitelist validation gets triggered + * properly for those fields. + * + * @return void + */ public function testValidateWithFieldListAndBehavior() { $TestModel = new ValidationTest1(); $TestModel->validate = array( @@ -2421,4 +2427,4 @@ class ValidationRuleBehavior extends ModelBehavior { } } -} \ No newline at end of file +} From 381a8c1216804eb0739cc069e49a3640d8e48015 Mon Sep 17 00:00:00 2001 From: euromark Date: Tue, 8 Oct 2013 00:47:47 +0200 Subject: [PATCH 3/3] Simplify test. --- lib/Cake/Test/Case/Model/ModelValidationTest.php | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/lib/Cake/Test/Case/Model/ModelValidationTest.php b/lib/Cake/Test/Case/Model/ModelValidationTest.php index 33560194e..7298513c4 100644 --- a/lib/Cake/Test/Case/Model/ModelValidationTest.php +++ b/lib/Cake/Test/Case/Model/ModelValidationTest.php @@ -622,12 +622,10 @@ class ModelValidationTest extends BaseModelTest { $TestModel = new ValidationTest1(); $TestModel->validate = array( 'title' => array( - 'rule' => 'alphaNumeric', - 'required' => true + 'rule' => 'notEmpty', ), 'name' => array( - 'rule' => 'alphaNumeric', - 'required' => true + 'rule' => 'notEmpty', )); $TestModel->Behaviors->attach('ValidationRule', array('fields' => array('name')));