From becc88b1fa56ddaf3d9d1845f0c45bed072a809c Mon Sep 17 00:00:00 2001 From: Florian Mast <flo.mast@web.de> Date: Fri, 4 Mar 2016 12:44:02 +0100 Subject: [PATCH] [BUGFIX] Solves PHP warning in FileAllowedTypesValidator Resolves: #72890 Resolves: #73618 Releases: master, 7.6 Change-Id: Ida8a9ace681b93d1d66b0e06c893a83e549f8411 Reviewed-on: https://review.typo3.org/46787 Reviewed-by: Nicole Cordes <typo3@cordes.co> Tested-by: Nicole Cordes <typo3@cordes.co> Reviewed-by: Frank Naegler <frank.naegler@typo3.org> Reviewed-by: Ralf Zimmermann <ralf.zimmermann@tritum.de> Tested-by: Ralf Zimmermann <ralf.zimmermann@tritum.de> Reviewed-by: Jan Helke <typo3@helke.de> Tested-by: Jan Helke <typo3@helke.de> Reviewed-by: Wouter Wolters <typo3@wouterwolters.nl> Tested-by: Wouter Wolters <typo3@wouterwolters.nl> --- .../Validator/FileAllowedTypesValidator.php | 16 ++-- .../FileAllowedTypesValidatorTest.php | 74 +++++++++++++------ 2 files changed, 63 insertions(+), 27 deletions(-) diff --git a/typo3/sysext/form/Classes/Domain/Validator/FileAllowedTypesValidator.php b/typo3/sysext/form/Classes/Domain/Validator/FileAllowedTypesValidator.php index 1a10361a0c8f..67ce42a7ca09 100755 --- a/typo3/sysext/form/Classes/Domain/Validator/FileAllowedTypesValidator.php +++ b/typo3/sysext/form/Classes/Domain/Validator/FileAllowedTypesValidator.php @@ -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 diff --git a/typo3/sysext/form/Tests/Unit/Validator/FileAllowedTypesValidatorTest.php b/typo3/sysext/form/Tests/Unit/Validator/FileAllowedTypesValidatorTest.php index e34d3e9881a7..e31b41b5f1f1 100644 --- a/typo3/sysext/form/Tests/Unit/Validator/FileAllowedTypesValidatorTest.php +++ b/typo3/sysext/form/Tests/Unit/Validator/FileAllowedTypesValidatorTest.php @@ -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()); } } -- GitLab