From 4848b6318962e2c1b4d6897a2222a0e393a0d3f0 Mon Sep 17 00:00:00 2001 From: Schlaefer Date: Tue, 13 May 2014 09:31:03 +0200 Subject: [PATCH 1/5] closes #3303 RFC: Rename Validator::between() into Validator::length() --- lib/Cake/Model/Model.php | 10 +++++----- lib/Cake/Model/ModelValidator.php | 2 +- lib/Cake/Model/Validator/CakeValidationSet.php | 2 +- lib/Cake/Test/Case/Model/ModelValidationTest.php | 2 +- lib/Cake/Test/Case/Utility/ValidationTest.php | 10 +++++----- lib/Cake/Utility/Validation.php | 2 +- 6 files changed, 14 insertions(+), 14 deletions(-) diff --git a/lib/Cake/Model/Model.php b/lib/Cake/Model/Model.php index 2b6840943..ef305e107 100644 --- a/lib/Cake/Model/Model.php +++ b/lib/Cake/Model/Model.php @@ -142,8 +142,8 @@ class Model extends Object implements CakeEventListener { * * {{{ * public $validate = array( - * 'age' => array( - * 'rule' => array('between', 5, 25) + * 'length' => array( + * 'rule' => array('lengthBetween', 5, 25) * ) * ); * }}} @@ -171,9 +171,9 @@ class Model extends Object implements CakeEventListener { * * {{{ * public $validate = array( - * 'age' => array( - * 'rule' => array('between', 5, 25), - * 'message' => array('The age must be between %d and %d.') + * 'length' => array( + * 'rule' => array('lengthBetween', 5, 15), + * 'message' => array('Between %d to %d characters') * ) * ); * }}} diff --git a/lib/Cake/Model/ModelValidator.php b/lib/Cake/Model/ModelValidator.php index d61ba8b66..377866aa9 100644 --- a/lib/Cake/Model/ModelValidator.php +++ b/lib/Cake/Model/ModelValidator.php @@ -539,7 +539,7 @@ class ModelValidator implements ArrayAccess, IteratorAggregate, Countable { * ->add('user_id', 'valid', array('rule' => 'numeric', 'message' => 'Invalid User')) * * $validator->add('password', array( - * 'size' => array('rule' => array('between', 8, 20)), + * 'size' => array('rule' => array('lengthBetween', 8, 20)), * 'hasSpecialCharacter' => array('rule' => 'validateSpecialchar', 'message' => 'not valid') * )); * }}} diff --git a/lib/Cake/Model/Validator/CakeValidationSet.php b/lib/Cake/Model/Validator/CakeValidationSet.php index 80b9a79cc..e6817e50c 100644 --- a/lib/Cake/Model/Validator/CakeValidationSet.php +++ b/lib/Cake/Model/Validator/CakeValidationSet.php @@ -186,7 +186,7 @@ class CakeValidationSet implements ArrayAccess, IteratorAggregate, Countable { * {{{ * $set * ->setRule('required', array('rule' => 'notEmpty', 'required' => true)) - * ->setRule('inRange', array('rule' => array('between', 4, 10)) + * ->setRule('between', array('rule' => array('lengthBetween', 4, 10)) * }}} * * @param string $name The name under which the rule should be set diff --git a/lib/Cake/Test/Case/Model/ModelValidationTest.php b/lib/Cake/Test/Case/Model/ModelValidationTest.php index 8a3d266fd..75d015fcf 100644 --- a/lib/Cake/Test/Case/Model/ModelValidationTest.php +++ b/lib/Cake/Test/Case/Model/ModelValidationTest.php @@ -767,7 +767,7 @@ class ModelValidationTest extends BaseModelTest { 'last' => false ), 'between' => array( - 'rule' => array('between', 5, 15), + 'rule' => array('lengthBetween', 5, 15), 'message' => array('You may enter up to %s chars (minimum is %s chars)', 14, 6) ) ) diff --git a/lib/Cake/Test/Case/Utility/ValidationTest.php b/lib/Cake/Test/Case/Utility/ValidationTest.php index 245217d2a..e4db17c2e 100644 --- a/lib/Cake/Test/Case/Utility/ValidationTest.php +++ b/lib/Cake/Test/Case/Utility/ValidationTest.php @@ -208,12 +208,12 @@ class ValidationTest extends CakeTestCase { * @return void */ public function testBetween() { - $this->assertTrue(Validation::between('abcdefg', 1, 7)); - $this->assertTrue(Validation::between('', 0, 7)); - $this->assertTrue(Validation::between('אกあアꀀ豈', 1, 7)); + $this->assertTrue(Validation::lengthBetween('abcdefg', 1, 7)); + $this->assertTrue(Validation::lengthBetween('', 0, 7)); + $this->assertTrue(Validation::lengthBetween('אกあアꀀ豈', 1, 7)); - $this->assertFalse(Validation::between('abcdefg', 1, 6)); - $this->assertFalse(Validation::between('ÆΔΩЖÇ', 1, 3)); + $this->assertFalse(Validation::lengthBetween('abcdefg', 1, 6)); + $this->assertFalse(Validation::lengthBetween('ÆΔΩЖÇ', 1, 3)); } /** diff --git a/lib/Cake/Utility/Validation.php b/lib/Cake/Utility/Validation.php index a2d08bb65..5930e7f8a 100644 --- a/lib/Cake/Utility/Validation.php +++ b/lib/Cake/Utility/Validation.php @@ -104,7 +104,7 @@ class Validation { * @param integer $max Maximum value in range (inclusive) * @return boolean Success */ - public static function between($check, $min, $max) { + public static function lengthBetween($check, $min, $max) { $length = mb_strlen($check); return ($length >= $min && $length <= $max); } From 815838f150db98c02d9b026f51ac12dfb85641fe Mon Sep 17 00:00:00 2001 From: Schlaefer Date: Tue, 13 May 2014 10:46:25 +0200 Subject: [PATCH 2/5] replaces validator 'between' with 'lengthBetween' --- .../Test/Case/Model/ModelValidationTest.php | 22 +++++++++---------- lib/Cake/Test/test_app/Model/Extract.php | 2 +- lib/Cake/Test/test_app/Model/PersisterOne.php | 2 +- 3 files changed, 13 insertions(+), 13 deletions(-) diff --git a/lib/Cake/Test/Case/Model/ModelValidationTest.php b/lib/Cake/Test/Case/Model/ModelValidationTest.php index 75d015fcf..b04bbb46d 100644 --- a/lib/Cake/Test/Case/Model/ModelValidationTest.php +++ b/lib/Cake/Test/Case/Model/ModelValidationTest.php @@ -1844,7 +1844,7 @@ class ModelValidationTest extends BaseModelTest { $set = array( 'numeric' => array('rule' => 'numeric', 'allowEmpty' => false), - 'range' => array('rule' => array('between', 1, 5), 'allowEmpty' => false), + 'between' => array('rule' => array('lengthBetween', 1, 5), 'allowEmpty' => false), ); $Validator['other'] = $set; $rules = $Validator['other']; @@ -1853,7 +1853,7 @@ class ModelValidationTest extends BaseModelTest { $validators = $rules->getRules(); $this->assertCount(2, $validators); $this->assertEquals('numeric', $validators['numeric']->rule); - $this->assertEquals(array('between', 1, 5), $validators['range']->rule); + $this->assertEquals(array('lengthBetween', 1, 5), $validators['between']->rule); $Validator['new'] = new CakeValidationSet('new', $set, array()); $rules = $Validator['new']; @@ -1862,7 +1862,7 @@ class ModelValidationTest extends BaseModelTest { $validators = $rules->getRules(); $this->assertCount(2, $validators); $this->assertEquals('numeric', $validators['numeric']->rule); - $this->assertEquals(array('between', 1, 5), $validators['range']->rule); + $this->assertEquals(array('lengthBetween', 1, 5), $validators['between']->rule); } /** @@ -1917,7 +1917,7 @@ class ModelValidationTest extends BaseModelTest { $set = array( 'numeric' => array('rule' => 'numeric', 'allowEmpty' => false), - 'range' => array('rule' => array('between', 1, 5), 'allowEmpty' => false), + 'range' => array('rule' => array('lengthBetween', 1, 5), 'allowEmpty' => false), ); $Validator['other'] = $set; $this->assertCount(4, $Validator); @@ -1938,14 +1938,14 @@ class ModelValidationTest extends BaseModelTest { $Validator = $TestModel->validator(); $Validator->add('other', 'numeric', array('rule' => 'numeric', 'allowEmpty' => false)); - $Validator->add('other', 'range', array('rule' => array('between', 1, 5), 'allowEmpty' => false)); + $Validator->add('other', 'between', array('rule' => array('lengthBetween', 1, 5), 'allowEmpty' => false)); $rules = $Validator['other']; $this->assertEquals('other', $rules->field); $validators = $rules->getRules(); $this->assertCount(2, $validators); $this->assertEquals('numeric', $validators['numeric']->rule); - $this->assertEquals(array('between', 1, 5), $validators['range']->rule); + $this->assertEquals(array('lengthBetween', 1, 5), $validators['between']->rule); } /** @@ -1962,13 +1962,13 @@ class ModelValidationTest extends BaseModelTest { $this->assertFalse(isset($Validator['title'])); $Validator->add('other', 'numeric', array('rule' => 'numeric', 'allowEmpty' => false)); - $Validator->add('other', 'range', array('rule' => array('between', 1, 5), 'allowEmpty' => false)); + $Validator->add('other', 'between', array('rule' => array('lengthBetween', 1, 5), 'allowEmpty' => false)); $this->assertTrue(isset($Validator['other'])); $Validator->remove('other', 'numeric'); $this->assertTrue(isset($Validator['other'])); $this->assertFalse(isset($Validator['other']['numeric'])); - $this->assertTrue(isset($Validator['other']['range'])); + $this->assertTrue(isset($Validator['other']['between'])); } /** @@ -2126,7 +2126,7 @@ class ModelValidationTest extends BaseModelTest { $set = array( 'numeric' => array('rule' => 'numeric', 'allowEmpty' => false), - 'range' => array('rule' => array('between', 1, 5), 'allowEmpty' => false), + 'between' => array('rule' => array('lengthBetween', 1, 5), 'allowEmpty' => false), ); $Validator->add('other', $set); @@ -2136,11 +2136,11 @@ class ModelValidationTest extends BaseModelTest { $validators = $rules->getRules(); $this->assertCount(2, $validators); $this->assertEquals('numeric', $validators['numeric']->rule); - $this->assertEquals(array('between', 1, 5), $validators['range']->rule); + $this->assertEquals(array('lengthBetween', 1, 5), $validators['between']->rule); $set = new CakeValidationSet('other', array( 'a' => array('rule' => 'numeric', 'allowEmpty' => false), - 'b' => array('rule' => array('between', 1, 5), 'allowEmpty' => false), + 'b' => array('rule' => array('lengthBetween', 1, 5), 'allowEmpty' => false), )); $Validator->add('other', $set); diff --git a/lib/Cake/Test/test_app/Model/Extract.php b/lib/Cake/Test/test_app/Model/Extract.php index 0cd7218c4..2c5469055 100644 --- a/lib/Cake/Test/test_app/Model/Extract.php +++ b/lib/Cake/Test/test_app/Model/Extract.php @@ -36,7 +36,7 @@ class Extract extends AppModel { 'message' => 'double "quoted" validation' ), 'between' => array( - 'rule' => array('between', 5, 15), + 'rule' => array('lengthBetween', 5, 15), 'message' => "single 'quoted' validation" ) ), diff --git a/lib/Cake/Test/test_app/Model/PersisterOne.php b/lib/Cake/Test/test_app/Model/PersisterOne.php index 0a14b0f4a..e5c699945 100644 --- a/lib/Cake/Test/test_app/Model/PersisterOne.php +++ b/lib/Cake/Test/test_app/Model/PersisterOne.php @@ -38,7 +38,7 @@ class PersisterOne extends AppModel { 'message' => 'Post title is required' ), 'between' => array( - 'rule' => array('between', 5, 15), + 'rule' => array('lengthBetween', 5, 15), 'message' => array('You may enter up to %s chars (minimum is %s chars)', 14, 6) ) ), From 021d76562082305e90e96b17e0a0cd88063c21ee Mon Sep 17 00:00:00 2001 From: Schlaefer Date: Tue, 13 May 2014 10:46:50 +0200 Subject: [PATCH 3/5] fixes falling test case for 'lengthBetween' validator --- lib/Cake/Test/Case/Console/Command/Task/ModelTaskTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/Cake/Test/Case/Console/Command/Task/ModelTaskTest.php b/lib/Cake/Test/Case/Console/Command/Task/ModelTaskTest.php index c16cc1e20..77e23ad90 100644 --- a/lib/Cake/Test/Case/Console/Command/Task/ModelTaskTest.php +++ b/lib/Cake/Test/Case/Console/Command/Task/ModelTaskTest.php @@ -400,7 +400,7 @@ class ModelTaskTest extends CakeTestCase { public function testInteractiveDoValidationWithSkipping() { $this->Task->expects($this->any()) ->method('in') - ->will($this->onConsecutiveCalls('35', '24', 'n', '11', 's')); + ->will($this->onConsecutiveCalls('35', '24', 'n', '10', 's')); $this->Task->interactive = true; $Model = $this->getMock('Model'); $Model->primaryKey = 'id'; From 3ac731273ee046acbe93c6c75906fcbc637bc192 Mon Sep 17 00:00:00 2001 From: Schlaefer Date: Tue, 13 May 2014 16:31:32 +0200 Subject: [PATCH 4/5] adds backwards compatible 'between' alias for 'lengthBetween' validation rule --- lib/Cake/Test/Case/Utility/ValidationTest.php | 4 ++-- lib/Cake/Utility/Validation.php | 14 ++++++++++++++ 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/lib/Cake/Test/Case/Utility/ValidationTest.php b/lib/Cake/Test/Case/Utility/ValidationTest.php index e4db17c2e..c7e36d70f 100644 --- a/lib/Cake/Test/Case/Utility/ValidationTest.php +++ b/lib/Cake/Test/Case/Utility/ValidationTest.php @@ -203,11 +203,11 @@ class ValidationTest extends CakeTestCase { } /** - * testBetween method + * testLengthBetween method * * @return void */ - public function testBetween() { + public function testLengthBetween() { $this->assertTrue(Validation::lengthBetween('abcdefg', 1, 7)); $this->assertTrue(Validation::lengthBetween('', 0, 7)); $this->assertTrue(Validation::lengthBetween('אกあアꀀ豈', 1, 7)); diff --git a/lib/Cake/Utility/Validation.php b/lib/Cake/Utility/Validation.php index 5930e7f8a..b7c7b9464 100644 --- a/lib/Cake/Utility/Validation.php +++ b/lib/Cake/Utility/Validation.php @@ -109,6 +109,20 @@ class Validation { return ($length >= $min && $length <= $max); } +/** + * Alias of Validator::lengthBetween() for backwards compatibility. + * + * @see Validator::lengthBetween() + * @deprecated Deprecated since 2.6, use Validator::lengthBetween() instead. + * @param string $check Value to check for length + * @param integer $min Minimum value in range (inclusive) + * @param integer $max Maximum value in range (inclusive) + * @return boolean Success + */ + public static function between($check, $min, $max) { + return self::lengthBetween($check, $min, $max); + } + /** * Returns true if field is left blank -OR- only whitespace characters are present in its value * Whitespace characters include Space, Tab, Carriage Return, Newline From 6b80ae35b122f7f19cce9cbdebe0e0ea6a06a84b Mon Sep 17 00:00:00 2001 From: Schlaefer Date: Wed, 14 May 2014 10:55:24 +0200 Subject: [PATCH 5/5] fixes model task test-cases for 'between' validation rule alias --- .../Test/Case/Console/Command/Task/ModelTaskTest.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/lib/Cake/Test/Case/Console/Command/Task/ModelTaskTest.php b/lib/Cake/Test/Case/Console/Command/Task/ModelTaskTest.php index 77e23ad90..bb77e375f 100644 --- a/lib/Cake/Test/Case/Console/Command/Task/ModelTaskTest.php +++ b/lib/Cake/Test/Case/Console/Command/Task/ModelTaskTest.php @@ -315,7 +315,7 @@ class ModelTaskTest extends CakeTestCase { $this->Task->initValidations(); $this->Task->interactive = true; $this->Task->expects($this->any())->method('in') - ->will($this->onConsecutiveCalls('24', 'y', '18', 'n')); + ->will($this->onConsecutiveCalls('25', 'y', '19', 'n')); $result = $this->Task->fieldValidation('text', array('type' => 'string', 'length' => 10, 'null' => false)); $expected = array('notEmpty' => 'notEmpty', 'maxLength' => 'maxLength'); @@ -333,7 +333,7 @@ class ModelTaskTest extends CakeTestCase { $this->Task->interactive = true; $this->Task->expects($this->any())->method('in') - ->will($this->onConsecutiveCalls('999999', '24', 'n')); + ->will($this->onConsecutiveCalls('999999', '25', 'n')); $this->Task->expects($this->at(10))->method('out') ->with($this->stringContains('make a valid')); @@ -368,7 +368,7 @@ class ModelTaskTest extends CakeTestCase { $this->Task->initValidations(); $this->Task->interactive = true; $this->Task->expects($this->any())->method('in') - ->will($this->onConsecutiveCalls('24', 'y', 's')); + ->will($this->onConsecutiveCalls('25', 'y', 's')); $result = $this->Task->fieldValidation('text', array('type' => 'string', 'length' => 10, 'null' => false)); $expected = array('notEmpty' => 'notEmpty', '_skipFields' => true); @@ -384,7 +384,7 @@ class ModelTaskTest extends CakeTestCase { $this->Task->initValidations(); $this->Task->interactive = true; $this->Task->expects($this->any())->method('in') - ->will($this->onConsecutiveCalls('24', 's')); + ->will($this->onConsecutiveCalls('25', 's')); $result = $this->Task->fieldValidation('text', array('type' => 'string', 'length' => 10, 'null' => false)); $expected = array('notEmpty' => 'notEmpty', '_skipFields' => true); @@ -400,7 +400,7 @@ class ModelTaskTest extends CakeTestCase { public function testInteractiveDoValidationWithSkipping() { $this->Task->expects($this->any()) ->method('in') - ->will($this->onConsecutiveCalls('35', '24', 'n', '10', 's')); + ->will($this->onConsecutiveCalls('36', '25', 'n', '11', 's')); $this->Task->interactive = true; $Model = $this->getMock('Model'); $Model->primaryKey = 'id';