[BUGFIX] Fix NumberRangeValidator using startRange and endRange 09/45109/7
authorStephan Großberndt <stephan@grossberndt.de>
Wed, 9 Dec 2015 19:46:12 +0000 (20:46 +0100)
committerMarkus Klein <markus.klein@typo3.org>
Wed, 23 Dec 2015 11:32:23 +0000 (12:32 +0100)
Re-enable the validation using "startRange" and "endRange" in
NumberRangeValidator instead of using the default values from "minimum"
and "maximum".

Resolves: #72047
Releases: 6.2
Change-Id: I11b7f3699f60964906f2b84a5581491ce255e3ac
Reviewed-on: https://review.typo3.org/45109
Reviewed-by: Helmut Hummel <helmut.hummel@typo3.org>
Tested-by: Helmut Hummel <helmut.hummel@typo3.org>
Reviewed-by: Markus Klein <markus.klein@typo3.org>
Tested-by: Markus Klein <markus.klein@typo3.org>
typo3/sysext/extbase/Classes/Validation/Validator/NumberRangeValidator.php
typo3/sysext/extbase/Tests/Unit/Validation/Validator/BeforeExtbase14/NumberRangeValidatorTest.php
typo3/sysext/extbase/Tests/Unit/Validation/Validator/NumberRangeValidatorTest.php

index 6d4f4b0..ae5bf92 100644 (file)
@@ -52,14 +52,31 @@ class NumberRangeValidator extends AbstractValidator {
                 * @todo: remove this fallback to startRange/endRange in 6.3 when the setOptions() method is removed too
                 * @deprecated since Extbase 1.4, will be removed two versions after Extbase 6.1
                 */
-               if (isset($this->options['minimum'])) {
+               if (isset($this->options['minimum']) && isset($this->options['startRange'])) {
+                       if ($this->options['minimum'] !== 0) {
+                               $minimum = $this->options['minimum'];
+                       } elseif ($this->options['startRange'] !== 0) {
+                               $minimum = $this->options['startRange'];
+                       } else {
+                               $minimum = 0;
+                       }
+               } elseif (isset($this->options['minimum'])) {
                        $minimum = $this->options['minimum'];
                } elseif (isset($this->options['startRange'])) {
                        $minimum = $this->options['startRange'];
                } else {
                        $minimum = 0;
                }
-               if (isset($this->options['maximum'])) {
+
+               if (isset($this->options['maximum']) && isset($this->options['endRange'])) {
+                       if ($this->options['maximum'] !== PHP_INT_MAX) {
+                               $maximum = $this->options['maximum'];
+                       } elseif ($this->options['endRange'] !== PHP_INT_MAX) {
+                               $maximum = $this->options['endRange'];
+                       } else {
+                               $maximum = PHP_INT_MAX;
+                       }
+               } elseif (isset($this->options['maximum'])) {
                        $maximum = $this->options['maximum'];
                } elseif (isset($this->options['endRange'])) {
                        $maximum = $this->options['endRange'];
index 978d8e2..b7e86bc 100644 (file)
@@ -26,6 +26,7 @@ class NumberRangeValidatorTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
         * @test
         */
        public function deprecatedOptionsAreStillSupported() {
+               // Expectation here is, that no exception is thrown, as it would be with unsupported options
                $this->getMock(
                        'TYPO3\\CMS\\Extbase\\Validation\\Validator\\NumberRangeValidator',
                        array(),
@@ -37,7 +38,16 @@ class NumberRangeValidatorTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
         * @test
         */
        public function numberRangeValidatorReturnsTrueForASimpleIntegerInRange() {
-               $numberRangeValidator = $this->getMock('TYPO3\\CMS\\Extbase\\Validation\\Validator\\NumberRangeValidator', array('addError'), array(), '', FALSE);
+               $numberRangeValidator = $this->getMock('TYPO3\\CMS\\Extbase\\Validation\\Validator\\NumberRangeValidator', array('addError'), array(array('startRange' => 0, 'endRange' => 1000)));
+               $numberRangeValidator->expects($this->never())->method('addError');
+               $numberRangeValidator->isValid(10.5);
+       }
+
+       /**
+        * @test
+        */
+       public function numberRangeValidatorReturnsTrueForASimpleIntegerInRangeWhenOptionsProvidedWithSetOptions() {
+               $numberRangeValidator = $this->getMock('TYPO3\\CMS\\Extbase\\Validation\\Validator\\NumberRangeValidator', array('addError'));
                $numberRangeValidator->expects($this->never())->method('addError');
                $numberRangeValidator->setOptions(array('startRange' => 0, 'endRange' => 1000));
                $numberRangeValidator->isValid(10.5);
@@ -47,7 +57,16 @@ class NumberRangeValidatorTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
         * @test
         */
        public function numberRangeValidatorReturnsFalseForANumberOutOfRange() {
-               $numberRangeValidator = $this->getMock('TYPO3\\CMS\\Extbase\\Validation\\Validator\\NumberRangeValidator', array('addError', 'translateErrorMessage'), array(), '', FALSE);
+               $numberRangeValidator = $this->getMock('TYPO3\\CMS\\Extbase\\Validation\\Validator\\NumberRangeValidator', array('addError', 'translateErrorMessage'), array(array('startRange' => 0, 'endRange' => 1000)));
+               $numberRangeValidator->expects($this->once())->method('addError');
+               $numberRangeValidator->isValid(1000.1);
+       }
+
+       /**
+        * @test
+        */
+       public function numberRangeValidatorReturnsFalseForANumberOutOfRangeWhenOptionsProvidedWithSetOptions() {
+               $numberRangeValidator = $this->getMock('TYPO3\\CMS\\Extbase\\Validation\\Validator\\NumberRangeValidator', array('addError', 'translateErrorMessage'));
                $numberRangeValidator->expects($this->once())->method('addError');
                $numberRangeValidator->setOptions(array('startRange' => 0, 'endRange' => 1000));
                $numberRangeValidator->isValid(1000.1);
@@ -57,7 +76,16 @@ class NumberRangeValidatorTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
         * @test
         */
        public function numberRangeValidatorReturnsTrueForANumberInReversedRange() {
-               $numberRangeValidator = $this->getMock('TYPO3\\CMS\\Extbase\\Validation\\Validator\\NumberRangeValidator', array('addError'), array(), '', FALSE);
+               $numberRangeValidator = $this->getMock('TYPO3\\CMS\\Extbase\\Validation\\Validator\\NumberRangeValidator', array('addError'), array(array('startRange' => 1000, 'endRange' => 0)));
+               $numberRangeValidator->expects($this->never())->method('addError');
+               $numberRangeValidator->isValid(100);
+       }
+
+       /**
+        * @test
+        */
+       public function numberRangeValidatorReturnsTrueForANumberInReversedRangeWhenOptionsProvidedWithSetOptions() {
+               $numberRangeValidator = $this->getMock('TYPO3\\CMS\\Extbase\\Validation\\Validator\\NumberRangeValidator', array('addError'));
                $numberRangeValidator->expects($this->never())->method('addError');
                $numberRangeValidator->setOptions(array('startRange' => 1000, 'endRange' => 0));
                $numberRangeValidator->isValid(100);
@@ -67,7 +95,16 @@ class NumberRangeValidatorTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
         * @test
         */
        public function numberRangeValidatorReturnsFalseForAString() {
-               $numberRangeValidator = $this->getMock('TYPO3\\CMS\\Extbase\\Validation\\Validator\\NumberRangeValidator', array('addError', 'translateErrorMessage'), array(), '', FALSE);
+               $numberRangeValidator = $this->getMock('TYPO3\\CMS\\Extbase\\Validation\\Validator\\NumberRangeValidator', array('addError', 'translateErrorMessage'), array(array('startRange' => 0, 'endRange' => 1000)));
+               $numberRangeValidator->expects($this->once())->method('addError');
+               $numberRangeValidator->isValid('not a number');
+       }
+
+       /**
+        * @test
+        */
+       public function numberRangeValidatorReturnsFalseForAStringWhenOptionsProvidedWithSetOptions() {
+               $numberRangeValidator = $this->getMock('TYPO3\\CMS\\Extbase\\Validation\\Validator\\NumberRangeValidator', array('addError', 'translateErrorMessage'));
                $numberRangeValidator->expects($this->once())->method('addError');
                $numberRangeValidator->setOptions(array('startRange' => 0, 'endRange' => 1000));
                $numberRangeValidator->isValid('not a number');
@@ -80,9 +117,22 @@ class NumberRangeValidatorTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
                $startRange = 1;
                $endRange = 42;
 
-               $numberRangeValidator = $this->getMock('TYPO3\\CMS\\Extbase\\Validation\\Validator\\NumberRangeValidator', array('addError', 'translateErrorMessage'), array(), '', FALSE);
+               $numberRangeValidator = $this->getMock('TYPO3\\CMS\\Extbase\\Validation\\Validator\\NumberRangeValidator', array('addError', 'translateErrorMessage'), array(array('startRange' => $startRange, 'endRange' => $endRange)));
                // we only test for the error key, after the translation method is mocked.
-               $numberRangeValidator->expects($this->once())->method('addError')->with(NULL, 1221561046, array(1, 42));
+               $numberRangeValidator->expects($this->once())->method('addError')->with(NULL, 1221561046, array($startRange, $endRange));
+               $numberRangeValidator->isValid(4711);
+       }
+
+       /**
+        * @test
+        */
+       public function numberRangeValidatorCreatesTheCorrectErrorForANumberOutOfRangeWhenOptionsProvidedWithSetOptions() {
+               $startRange = 1;
+               $endRange = 42;
+
+               $numberRangeValidator = $this->getMock('TYPO3\\CMS\\Extbase\\Validation\\Validator\\NumberRangeValidator', array('addError', 'translateErrorMessage'));
+               // we only test for the error key, after the translation method is mocked.
+               $numberRangeValidator->expects($this->once())->method('addError')->with(NULL, 1221561046, array($startRange, $endRange));
                $numberRangeValidator->setOptions(array('startRange' => $startRange, 'endRange' => $endRange));
                $numberRangeValidator->isValid(4711);
        }
@@ -91,10 +141,21 @@ class NumberRangeValidatorTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
         * @test
         */
        public function numberRangeValidatorCreatesTheCorrectErrorForAStringSubject() {
-               $numberRangeValidator = $this->getMock('TYPO3\\CMS\\Extbase\\Validation\\Validator\\NumberRangeValidator', array('addError', 'translateErrorMessage'), array(), '', FALSE);
+               $numberRangeValidator = $this->getMock('TYPO3\\CMS\\Extbase\\Validation\\Validator\\NumberRangeValidator', array('addError', 'translateErrorMessage'), array(array('startRange' => 0, 'endRange' => 42)));
+               // we only test for the error key, after the translation method is mocked.
+               $numberRangeValidator->expects($this->once())->method('addError')->with(NULL, 1221563685);
+               $numberRangeValidator->isValid('this is not between 0 an 42');
+       }
+
+       /**
+        * @test
+        */
+       public function numberRangeValidatorCreatesTheCorrectErrorForAStringSubjectWhenOptionsProvidedWithSetOptions() {
+               $numberRangeValidator = $this->getMock('TYPO3\\CMS\\Extbase\\Validation\\Validator\\NumberRangeValidator', array('addError', 'translateErrorMessage'));
                // we only test for the error key, after the translation method is mocked.
                $numberRangeValidator->expects($this->once())->method('addError')->with(NULL, 1221563685);
                $numberRangeValidator->setOptions(array('startRange' => 0, 'endRange' => 42));
                $numberRangeValidator->isValid('this is not between 0 an 42');
        }
+
 }
index 1e5c307..8ee58db 100644 (file)
@@ -67,4 +67,5 @@ class NumberRangeValidatorTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
                $validator = $this->getMock($this->validatorClassName, array('translateErrorMessage'), array($options));
                $this->assertTrue($validator->validate('not a number')->hasErrors());
        }
+
 }