From 6818268a27e010af960a7e3f1cc5b56600bef84d Mon Sep 17 00:00:00 2001 From: Mischa ter Smitten Date: Fri, 16 Dec 2016 21:34:41 +0100 Subject: [PATCH 1/3] New Validation::(min|max)ByteLength() addition --- lib/Cake/Test/Case/Utility/ValidationTest.php | 28 +++++++++++++++++++ lib/Cake/Utility/Validation.php | 26 +++++++++++++++-- 2 files changed, 52 insertions(+), 2 deletions(-) diff --git a/lib/Cake/Test/Case/Utility/ValidationTest.php b/lib/Cake/Test/Case/Utility/ValidationTest.php index 9b34c03b1..bec844dcb 100644 --- a/lib/Cake/Test/Case/Utility/ValidationTest.php +++ b/lib/Cake/Test/Case/Utility/ValidationTest.php @@ -1825,6 +1825,20 @@ class ValidationTest extends CakeTestCase { $this->assertFalse(Validation::maxLength('ÆΔΩЖÇ', 3)); } +/** + * maxLengthBytes method + * + * @return void + */ + public function testMaxLengthBytes() { + $this->assertTrue(Validation::maxLengthBytes('ab', 3)); + $this->assertTrue(Validation::maxLengthBytes('abc', 3)); + $this->assertTrue(Validation::maxLengthBytes('ÆΔΩЖÇ', 10)); + $this->assertTrue(Validation::maxLengthBytes('ÆΔΩЖÇ', 11)); + $this->assertFalse(Validation::maxLengthBytes('abcd', 3)); + $this->assertFalse(Validation::maxLengthBytes('ÆΔΩЖÇ', 9)); + } + /** * testMinLength method * @@ -1839,6 +1853,20 @@ class ValidationTest extends CakeTestCase { $this->assertTrue(Validation::minLength('ÆΔΩЖÇ', 2)); } +/** + * minLengthBytes method + * + * @return void + */ + public function testMinLengthBytes() { + $this->assertFalse(Validation::minLengthBytes('ab', 3)); + $this->assertFalse(Validation::minLengthBytes('ÆΔΩЖÇ', 11)); + $this->assertTrue(Validation::minLengthBytes('abc', 3)); + $this->assertTrue(Validation::minLengthBytes('abcd', 3)); + $this->assertTrue(Validation::minLengthBytes('ÆΔΩЖÇ', 10)); + $this->assertTrue(Validation::minLengthBytes('ÆΔΩЖÇ', 9)); + } + /** * testUrl method * diff --git a/lib/Cake/Utility/Validation.php b/lib/Cake/Utility/Validation.php index 123388463..8ee3c10a0 100644 --- a/lib/Cake/Utility/Validation.php +++ b/lib/Cake/Utility/Validation.php @@ -539,7 +539,7 @@ class Validation { } /** - * Checks whether the length of a string is greater or equal to a minimal length. + * Checks whether the length of a string (in character) is greater or equal to a minimal length. * * @param string $check The string to test * @param int $min The minimal string length @@ -550,7 +550,7 @@ class Validation { } /** - * Checks whether the length of a string is smaller or equal to a maximal length.. + * Checks whether the length of a string (in character) is smaller or equal to a maximal length.. * * @param string $check The string to test * @param int $max The maximal string length @@ -560,6 +560,28 @@ class Validation { return mb_strlen($check) <= $max; } +/** + * Checks whether the length of a string (in bytes) is greater or equal to a minimal length. + * + * @param string $check The string to test + * @param int $min The minimal string length + * @return bool Success + */ + public static function minLengthBytes($check, $min) { + return strlen($check) >= $min; + } + +/** + * Checks whether the length of a string (in bytes) is smaller or equal to a maximal length.. + * + * @param string $check The string to test + * @param int $max The maximal string length + * @return bool Success + */ + public static function maxLengthBytes($check, $max) { + return strlen($check) <= $max; + } + /** * Checks that a value is a monetary amount. * From 043858d9e61632127ed9eafb476637254385f577 Mon Sep 17 00:00:00 2001 From: Mischa ter Smitten Date: Sun, 18 Dec 2016 21:06:51 +0100 Subject: [PATCH 2/3] Fixed typo --- lib/Cake/Utility/Validation.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/Cake/Utility/Validation.php b/lib/Cake/Utility/Validation.php index 8ee3c10a0..da614de1d 100644 --- a/lib/Cake/Utility/Validation.php +++ b/lib/Cake/Utility/Validation.php @@ -539,7 +539,7 @@ class Validation { } /** - * Checks whether the length of a string (in character) is greater or equal to a minimal length. + * Checks whether the length of a string (in characters) is greater or equal to a minimal length. * * @param string $check The string to test * @param int $min The minimal string length @@ -550,7 +550,7 @@ class Validation { } /** - * Checks whether the length of a string (in character) is smaller or equal to a maximal length.. + * Checks whether the length of a string (in characters) is smaller or equal to a maximal length.. * * @param string $check The string to test * @param int $max The maximal string length From 7944f512adc477f46912b59200f96c6e60642fcf Mon Sep 17 00:00:00 2001 From: chinpei215 Date: Mon, 30 Jan 2017 03:21:58 +0900 Subject: [PATCH 3/3] Fix ModelTaskTest for tests passing --- .../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 da5b8d7cb..a93514005 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('26', 'y', '18', 'n')); $result = $this->Task->fieldValidation('text', array('type' => 'string', 'length' => 10, 'null' => false)); $expected = array('notBlank' => 'notBlank', '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', '26', '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('26', 'y', 's')); $result = $this->Task->fieldValidation('text', array('type' => 'string', 'length' => 10, 'null' => false)); $expected = array('notBlank' => 'notBlank', '_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('26', 's')); $result = $this->Task->fieldValidation('text', array('type' => 'string', 'length' => 10, 'null' => false)); $expected = array('notBlank' => 'notBlank', '_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('37', '26', 'n', '10', 's')); $this->Task->interactive = true; $Model = $this->getMock('Model'); $Model->primaryKey = 'id';