[BUGFIX] Setting userFuncClassPrefix to empty string does not work
authorSusanne Moog <typo3@susannemoog.de>
Sun, 21 Aug 2011 09:40:29 +0000 (11:40 +0200)
committerChristian Kuhn <lolli@schwarzbu.ch>
Sun, 21 Aug 2011 15:47:32 +0000 (17:47 +0200)
Setting $GLOBALS['TYPO3_CONF_VARS']['FE']['userFuncClassPrefix']
to an empty string - to allow all prefixes - does not work as
expected as the check in t3lib_div expects a valid string with
length > 0. Adjust t3lib_div::hasValidClassPrefix to also
allow empty prefix var.

Change-Id: Idbd8cf7a547d9549f1c31dadd731eda48f52fcec
Resolves: #29133
Releases: 4.6
Reviewed-on: http://review.typo3.org/4445
Reviewed-by: Philipp Gampe
Tested-by: Philipp Gampe
Reviewed-by: Christian Kuhn
Tested-by: Christian Kuhn
t3lib/class.t3lib_div.php
tests/t3lib/class.t3lib_divTest.php

index b59c9fd..76f73ef 100644 (file)
@@ -4774,6 +4774,12 @@ final class t3lib_div {
         * @return bool TRUE if name is allowed
         */
        public static function hasValidClassPrefix($classRef, array $additionalPrefixes = array()) {
+               if(empty($classRef)) {
+                       return FALSE;
+               }
+               if(!is_string($classRef)) {
+                       throw new InvalidArgumentException('$classRef has to be of type string', 1313917992);
+               }
                $hasValidPrefix = FALSE;
                $validPrefixes = array('tx_', 'Tx_', $GLOBALS['TYPO3_CONF_VARS']['FE']['userFuncClassPrefix']);
                $classRef = trim($classRef);
@@ -4781,9 +4787,8 @@ final class t3lib_div {
                if (count($additionalPrefixes)) {
                        $validPrefixes = array_merge($validPrefixes, $additionalPrefixes);
                }
-
                foreach ($validPrefixes as $prefixToCheck) {
-                       if (self::isFirstPartOfStr($classRef, $prefixToCheck)) {
+                       if (self::isFirstPartOfStr($classRef, $prefixToCheck) || $prefixToCheck === '') {
                                $hasValidPrefix = TRUE;
                                break;
                        }
index ddf0665..e467590 100644 (file)
@@ -3318,10 +3318,10 @@ class t3lib_divTest extends tx_phpunit_testcase {
         */
        public function validClassPrefixDataProvider() {
                return array(
-                       array('tx_foo'),
-                       array('tx_foo_bar'),
-                       array('Tx_foo'),
-                       array($GLOBALS['TYPO3_CONF_VARS']['FE']['userFuncClassPrefix'] . 'foo'),
+                       'normal prefix' => array('tx_foo'),
+                       'normal long prefix' => array('tx_foo_bar'),
+                       'extbase named prefix' => array('Tx_foo'),
+                       'user func named prefix' => array('user_foo'),
                );
        }
 
@@ -3331,6 +3331,7 @@ class t3lib_divTest extends tx_phpunit_testcase {
         * @param string $className Class name to test
         */
        public function hasValidClassPrefixAcceptsValidPrefixes($className) {
+               $GLOBALS['TYPO3_CONF_VARS']['FE']['userFuncClassPrefix'] = 'user_';
                $this->assertTrue(
                        t3lib_div::hasValidClassPrefix($className)
                );
@@ -3341,7 +3342,6 @@ class t3lib_divTest extends tx_phpunit_testcase {
         */
        public function invalidClassPrefixDataProvider() {
                return array(
-                       array(''),
                        array('ab_c'),
                        array('txfoo'),
                        array('Txfoo'),
@@ -3356,6 +3356,7 @@ class t3lib_divTest extends tx_phpunit_testcase {
         * @param string $className Class name to test
         */
        public function hasValidClassPrefixRefusesInvalidPrefixes($className) {
+               $GLOBALS['TYPO3_CONF_VARS']['FE']['userFuncClassPrefix'] = 'user_';
                $this->assertFalse(
                        t3lib_div::hasValidClassPrefix($className)
                );
@@ -3364,10 +3365,53 @@ class t3lib_divTest extends tx_phpunit_testcase {
        /**
         * @test
         */
+       public function hasValidClassPrefixReturnsFalseIfEmptyClassNameGiven() {
+               $this->assertFalse(
+                       t3lib_div::hasValidClassPrefix('')
+               );
+       }
+
+       /**
+        * @return array
+        */
+       public function invalidClassReferenceDataTypeDataProvider() {
+               return array(
+                       'array' => array(array('someClassArray')),
+                       'integer' => array(123),
+                       'boolean' => array(TRUE),
+                       'object' => array(new stdClass()),
+               );
+       }
+
+       /**
+        * @test
+        * @dataProvider invalidClassReferenceDataTypeDataProvider
+        * @param string $className Class name to test
+        * @expectedException \InvalidArgumentException
+        */
+       public function hasValidClassPrefixThrowsExceptionForInvalidClassReferenceDataType($className) {
+               t3lib_div::hasValidClassPrefix($className);
+       }
+
+       /**
+        * @test
+        * @dataProvider invalidClassPrefixDataProvider
+        * @param string $className Class name to test
+        */
+       public function hasValidClassPrefixAllowsEmptyPrefix($className) {
+               $GLOBALS['TYPO3_CONF_VARS']['FE']['userFuncClassPrefix'] = '';
+               $this->assertTrue(
+                       t3lib_div::hasValidClassPrefix($className)
+               );
+       }
+
+       /**
+        * @test
+        */
        public function hasValidClassPrefixAcceptsAdditionalPrefixes() {
                $this->assertTrue(
                        t3lib_div::hasValidClassPrefix('customPrefix_foo', array('customPrefix_'))
                );
        }
 }
-?>
+?>
\ No newline at end of file