[BUGFIX] Solves PHP warning in FileAllowedTypesValidator 57/46457/4
authorFlorian Mast <flo.mast@web.de>
Fri, 4 Mar 2016 11:44:02 +0000 (12:44 +0100)
committerWouter Wolters <typo3@wouterwolters.nl>
Fri, 4 Mar 2016 17:44:26 +0000 (18:44 +0100)
Resolves: #72890
Resolves: #73618
Releases: master, 7.6
Change-Id: Ida8a9ace681b93d1d66b0e06c893a83e549f8411
Reviewed-on: https://review.typo3.org/46457
Reviewed-by: Wouter Wolters <typo3@wouterwolters.nl>
Tested-by: Wouter Wolters <typo3@wouterwolters.nl>
typo3/sysext/form/Classes/Domain/Validator/FileAllowedTypesValidator.php
typo3/sysext/form/Tests/Unit/Validator/FileAllowedTypesValidatorTest.php

index 1a10361..67ce42a 100755 (executable)
@@ -38,16 +38,18 @@ class FileAllowedTypesValidator extends AbstractValidator
      * Check if the file mime type is allowed.
      *
      * The mime type is set in the propertymapper
+     *
      * @see TYPO3\CMS\Form\Domain\Property\TypeConverter::convertFrom
      *
-     * @param mixed $value
+     * @param array $value
      * @return void
      */
     public function isValid($value)
     {
         $allowedTypes = strtolower($this->options['types']);
-        $allowedMimeTypes = GeneralUtility::trimExplode(', ', $allowedTypes);
-        $fileMimeType = strtolower($value['type']);
+        $allowedMimeTypes = GeneralUtility::trimExplode(',', $allowedTypes, true);
+        $fileMimeType = !empty($value['type']) ? strtolower($value['type']) : '';
+
         if (!in_array($fileMimeType, $allowedMimeTypes, true)) {
             $this->addError(
                 $this->renderMessage(
@@ -69,7 +71,11 @@ class FileAllowedTypesValidator extends AbstractValidator
      */
     public function substituteMarkers($message)
     {
-        $message = str_replace('%allowedTypes', implode(',', $this->options['types']), $message);
+        $allowedTypes = strtolower($this->options['types']);
+        $allowedMimeTypes = GeneralUtility::trimExplode(',', $allowedTypes);
+        $allowedTypesStringForDisplay = implode(', ', $allowedMimeTypes);
+        $message = str_replace('%allowedTypes', $allowedTypesStringForDisplay, $message);
+
         return $message;
     }
-}
+}
\ No newline at end of file
index e34d3e9..e31b41b 100644 (file)
@@ -24,20 +24,24 @@ class FileAllowedTypesValidatorTest extends AbstractValidatorTest
      */
     protected $subjectClassName = \TYPO3\CMS\Form\Domain\Validator\FileAllowedTypesValidator::class;
 
-    protected function setUp()
-    {
-        $this->markTestSkipped('Erros found in specific implementation, see @todo remarks there');
-    }
-
     /**
      * @return array
      */
     public function validTypesProvider()
     {
         return array(
-            'pdf in (pdf)'       => array(array('application/pdf', 'application/pdf')),
-            'pdf in (pdf, json)' => array(array('application/pdf, application/json', 'application/pdf'))
-
+            'pdf in (pdf)' => [
+                'application/pdf',
+                [
+                    'type' => 'application/pdf',
+                ],
+            ],
+            'pdf in (pdf, json)' => [
+                'application/pdf, application/json',
+                [
+                    'type' => 'application/pdf',
+                ],
+            ],
         );
     }
 
@@ -47,38 +51,64 @@ class FileAllowedTypesValidatorTest extends AbstractValidatorTest
     public function invalidTypesProvider()
     {
         return array(
-            'xml in (pdf, json)' => array(array('application/pdf, application/json', 'application/xml')),
-            'xml in (pdf)'       => array(array('application/pdf, application/json', 'application/xml'))
+            'xml in (pdf, json)' => [
+                'application/pdf, application/json',
+                [
+                    'type' => 'application/xml',
+                ],
+            ],
+            'xml in (pdf)' => [
+                'application/pdf',
+                [
+                    'type' => 'application/xml',
+                ],
+            ],
+            'empty mimetype' => [
+                'application/pdf, application/json',
+                [
+                    'type' => '',
+                ],
+            ],
+            'empty value' => [
+                'application/pdf, application/json',
+                '',
+            ],
         );
     }
 
     /**
      * @test
+     * @param string $types
+     * @param array $value
      * @dataProvider validTypesProvider
      */
-    public function validateForValidInputHasEmptyErrorResult($input)
+    public function validateForValidInputHasEmptyErrorResult($types, $value)
     {
-        $options = array('element' => uniqid('test'), 'errorMessage' => uniqid('error'));
-        $options['types'] = $input[0];
+        $options = [
+            'element' => uniqid('test'),
+            'errorMessage' => uniqid('error'),
+            'types' => $types,
+        ];
         $subject = $this->createSubject($options);
 
-        $this->assertEmpty(
-            $subject->validate($input[1])->getErrors()
-        );
+        $this->assertEmpty($subject->validate($value)->getErrors());
     }
 
     /**
      * @test
+     * @param string $types
+     * @param array $value
      * @dataProvider invalidTypesProvider
      */
-    public function validateForInvalidInputHasNotEmptyErrorResult($input)
+    public function validateForInvalidInputHasNotEmptyErrorResult($types, $value)
     {
-        $options = array('element' => uniqid('test'), 'errorMessage' => uniqid('error'));
-        $options['types'] = $input[0];
+        $options = [
+            'element' => uniqid('test'),
+            'errorMessage' => uniqid('error'),
+            'types' => $types,
+        ];
         $subject = $this->createSubject($options);
 
-        $this->assertNotEmpty(
-            $subject->validate($input[1])->getErrors()
-        );
+        $this->assertNotEmpty($subject->validate($value)->getErrors());
     }
 }