[BUGFIX] Make ValidatorResolver respect namespaces
authorTymoteusz Motylewski <t.motylewski@gmail.com>
Fri, 16 Nov 2012 16:23:24 +0000 (17:23 +0100)
committerMarkus Günther <mail@markus-guenther.de>
Sat, 17 Nov 2012 12:42:47 +0000 (13:42 +0100)
ValidatorResolver should parse namespaced validator
definitions like:
@validate Vendor.Extension:Foo
or
@validate Vendor\Extension\FooValidator

Change-Id: I37ce3f559f2917b149256d1b57a838c6394048e0
Fixes: #42791
Releases: 6.0
Reviewed-on: http://review.typo3.org/16491
Reviewed-by: Anja Leichsenring
Tested-by: Anja Leichsenring
Reviewed-by: Markus Günther
Tested-by: Markus Günther
typo3/sysext/extbase/Classes/Validation/ValidatorResolver.php
typo3/sysext/extbase/Tests/Unit/Validation/ValidatorResolverTest.php

index 58f9c1b..2ec1572 100644 (file)
@@ -39,7 +39,7 @@ class ValidatorResolver implements \TYPO3\CMS\Core\SingletonInterface {
         */
        const PATTERN_MATCH_VALIDATORS = '/
                        (?:^|,\\s*)
-                       (?P<validatorName>[a-z0-9_:]+)
+                       (?P<validatorName>[a-z0-9_:.\\\\]+)
                        \\s*
                        (?:\\(
                                (?P<validatorOptions>(?:\\s*[a-z0-9]+\\s*=\\s*(?:
@@ -230,7 +230,7 @@ class ValidatorResolver implements \TYPO3\CMS\Core\SingletonInterface {
                        }
                }
                // Custom validator for the class
-               $possibleValidatorClassName = str_replace('_Model_', '_Validator_', $dataType) . 'Validator';
+               $possibleValidatorClassName = str_replace(array('_Model_', '\\Model\\'), array('_Validator_','\\Validator\\'), $dataType) . 'Validator';
                $customValidator = $this->createValidator($possibleValidatorClassName);
                if ($customValidator !== NULL) {
                        $validatorConjunction->addValidator($customValidator);
index 14e143e..7560b17 100644 (file)
@@ -303,6 +303,34 @@ class ValidatorResolverTest extends \TYPO3\CMS\Extbase\Tests\Unit\BaseTestCase {
        }
 
        /**
+        * dataProvider for buildBaseValidatorConjunctionAddsValidatorFromConventionToTheReturnedConjunction
+        * @return array
+        */
+       public function modelNamesProvider() {
+               return array(
+                       'no replace' => array('F3_TestPackage_Quux', 'F3_TestPackage_QuuxValidator'),
+                       'replace in not namespaced class' => array('F3_TestPackage_Model_Quux', 'F3_TestPackage_Validator_QuuxValidator'),
+                       'replace in namespaced class' => array('F3\TestPackage\Model\Quux', 'F3\TestPackage\Validator\QuuxValidator')
+               );
+       }
+
+       /**
+        * @test
+        * @dataProvider modelNamesProvider
+        */
+       public function buildBaseValidatorConjunctionCreatesValidatorFromClassName($modelClassName, $validatorClassName) {
+               $mockConjunctionValidator = $this->getMock('TYPO3\\CMS\\Extbase\\Validation\\Validator\\ConjunctionValidator', array(), array(), '', FALSE);
+               $mockObjectManager = $this->getMock('TYPO3\\CMS\\Extbase\\Object\\ObjectManagerInterface', array(), array(), '', FALSE);
+               $mockObjectManager->expects($this->at(0))->method('get')->with('TYPO3\\CMS\\Extbase\\Validation\\Validator\\ConjunctionValidator')->will($this->returnValue($mockConjunctionValidator));
+               $validatorResolver = $this->getAccessibleMock('TYPO3\\CMS\\Extbase\\Validation\\ValidatorResolver', array('resolveValidatorObjectName', 'createValidator'));
+               //$validatorResolver->injectReflectionService($mockReflectionService);
+               $validatorResolver->injectObjectManager($mockObjectManager);
+               $validatorResolver->expects($this->once())->method('createValidator')->with($validatorClassName)->will($this->returnValue(NULL));
+               $result = $validatorResolver->_call('buildBaseValidatorConjunction', $modelClassName);
+               $this->assertSame($mockConjunctionValidator, $result);
+       }
+
+       /**
         * @test
         * @author Bastian Waidelich <bastian@typo3.org>
         */
@@ -452,7 +480,34 @@ class ValidatorResolverTest extends \TYPO3\CMS\Extbase\Tests\Unit\BaseTestCase {
                                                array('validatorName' => 'Bar', 'validatorOptions' => array('Quux' => '123', 'Pax' => 'a weird "string" with *freaky* \\stuff'))
                                        )
                                )
-                       )
+                       ),
+                       'namespaced validator class name' => array(
+                               'annotation' => '$var F3\TestPackage\Quux',
+                               'expected' => array(
+                                       'argumentName' => 'var',
+                                       'validators' => array(
+                                               array('validatorName' => 'F3\TestPackage\Quux', 'validatorOptions' => array())
+                                       )
+                               )
+                       ),
+                       'shorthand notation for system validator' => array(
+                               'annotation' => '$var TYPO3.CMS.Mypkg:MySecond',
+                               'expected' => array(
+                                       'argumentName' => 'var',
+                                       'validators' => array(
+                                               array('validatorName' => 'TYPO3.CMS.Mypkg:MySecond', 'validatorOptions' => array())
+                                       )
+                               )
+                       ),
+                       'shorthand notation for custom validator with parameter' => array(
+                               'annotation' => '$var Acme.Mypkg:MyThird(Foo="2")',
+                               'expected' => array(
+                                       'argumentName' => 'var',
+                                       'validators' => array(
+                                               array('validatorName' => 'Acme.Mypkg:MyThird', 'validatorOptions' => array('Foo' => '2'))
+                                       )
+                               )
+                       ),
                );
        }