From c29f02688f77b7908e2e2835e4dbe5d3bf711a77 Mon Sep 17 00:00:00 2001 From: Christian Flothmann Date: Mon, 19 May 2025 08:45:42 +0200 Subject: [PATCH] add missing $extensions and $extensionsMessage to the Image constraint --- .../Component/Validator/Constraints/Image.php | 14 +++- .../Tests/Constraints/ImageValidatorTest.php | 73 +++++++++++++++++++ 2 files changed, 84 insertions(+), 3 deletions(-) diff --git a/src/Symfony/Component/Validator/Constraints/Image.php b/src/Symfony/Component/Validator/Constraints/Image.php index 43590f4f2425c..8fafa5044f201 100644 --- a/src/Symfony/Component/Validator/Constraints/Image.php +++ b/src/Symfony/Component/Validator/Constraints/Image.php @@ -64,7 +64,7 @@ class Image extends File */ protected static $errorNames = self::ERROR_NAMES; - public $mimeTypes = 'image/*'; + public $mimeTypes; public $minWidth; public $maxWidth; public $maxHeight; @@ -140,7 +140,9 @@ public function __construct( ?string $allowPortraitMessage = null, ?string $corruptedMessage = null, ?array $groups = null, - mixed $payload = null + mixed $payload = null, + array|string|null $extensions = null, + ?string $extensionsMessage = null, ) { parent::__construct( $options, @@ -163,7 +165,9 @@ public function __construct( $uploadExtensionErrorMessage, $uploadErrorMessage, $groups, - $payload + $payload, + $extensions, + $extensionsMessage, ); $this->minWidth = $minWidth ?? $this->minWidth; @@ -192,6 +196,10 @@ public function __construct( $this->allowPortraitMessage = $allowPortraitMessage ?? $this->allowPortraitMessage; $this->corruptedMessage = $corruptedMessage ?? $this->corruptedMessage; + if (null === $this->mimeTypes && [] === $this->extensions) { + $this->mimeTypes = 'image/*'; + } + if (!\in_array('image/*', (array) $this->mimeTypes, true) && !\array_key_exists('mimeTypesMessage', $options ?? []) && null === $mimeTypesMessage) { $this->mimeTypesMessage = 'The mime type of the file is invalid ({{ type }}). Allowed mime types are {{ types }}.'; } diff --git a/src/Symfony/Component/Validator/Tests/Constraints/ImageValidatorTest.php b/src/Symfony/Component/Validator/Tests/Constraints/ImageValidatorTest.php index 3e646cfa39572..c8341e95f0176 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/ImageValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/ImageValidatorTest.php @@ -11,6 +11,8 @@ namespace Symfony\Component\Validator\Tests\Constraints; +use Symfony\Component\HttpFoundation\File\File; +use Symfony\Component\Mime\MimeTypes; use Symfony\Component\Validator\Constraints\Image; use Symfony\Component\Validator\Constraints\ImageValidator; use Symfony\Component\Validator\Exception\ConstraintDefinitionException; @@ -579,4 +581,75 @@ public static function provideInvalidMimeTypeWithNarrowedSet() ]), ]; } + + /** + * @dataProvider providerValidExtension + */ + public function testExtensionValid(string $name) + { + if (!class_exists(MimeTypes::class)) { + $this->markTestSkipped('Guessing the mime type is not possible'); + } + + $constraint = new Image(mimeTypes: [], extensions: ['gif'], extensionsMessage: 'myMessage'); + + $this->validator->validate(new File(__DIR__.'/Fixtures/'.$name), $constraint); + + $this->assertNoViolation(); + } + + public static function providerValidExtension(): iterable + { + yield ['test.gif']; + yield ['test.png.gif']; + } + + /** + * @dataProvider provideInvalidExtension + */ + public function testExtensionInvalid(string $name, string $extension) + { + $path = __DIR__.'/Fixtures/'.$name; + $constraint = new Image(extensions: ['png', 'svg'], extensionsMessage: 'myMessage'); + + $this->validator->validate(new File($path), $constraint); + + $this->buildViolation('myMessage') + ->setParameters([ + '{{ file }}' => '"'.$path.'"', + '{{ extension }}' => '"'.$extension.'"', + '{{ extensions }}' => '"png", "svg"', + '{{ name }}' => '"'.$name.'"', + ]) + ->setCode(Image::INVALID_EXTENSION_ERROR) + ->assertRaised(); + } + + public static function provideInvalidExtension(): iterable + { + yield ['test.gif', 'gif']; + yield ['test.png.gif', 'gif']; + } + + public function testExtensionAutodetectMimeTypesInvalid() + { + if (!class_exists(MimeTypes::class)) { + $this->markTestSkipped('Guessing the mime type is not possible'); + } + + $path = __DIR__.'/Fixtures/invalid-content.gif'; + $constraint = new Image(mimeTypesMessage: 'myMessage', extensions: ['gif']); + + $this->validator->validate(new File($path), $constraint); + + $this->buildViolation('myMessage') + ->setParameters([ + '{{ file }}' => '"'.$path.'"', + '{{ name }}' => '"invalid-content.gif"', + '{{ type }}' => '"text/plain"', + '{{ types }}' => '"image/gif"', + ]) + ->setCode(Image::INVALID_MIME_TYPE_ERROR) + ->assertRaised(); + } }