Skip to content

[Validator] add missing $extensions and $extensionsMessage to the Image constraint #60478

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions src/Symfony/Component/Validator/Constraints/Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand All @@ -163,7 +165,9 @@ public function __construct(
$uploadExtensionErrorMessage,
$uploadErrorMessage,
$groups,
$payload
$payload,
$extensions,
$extensionsMessage,
);

$this->minWidth = $minWidth ?? $this->minWidth;
Expand Down Expand Up @@ -192,6 +196,10 @@ public function __construct(
$this->allowPortraitMessage = $allowPortraitMessage ?? $this->allowPortraitMessage;
$this->corruptedMessage = $corruptedMessage ?? $this->corruptedMessage;

if (null === $this->mimeTypes && [] === $this->extensions) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

doesn't the second check change the behavior? extensions is null by default, so we won't set mimeTypes to its default anymore, right?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The property is initialised with an empty array in the parent class which then also has this line in its constructor (which is called before the code here is run):

$this->extensions = $extensions ?? $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 }}.';
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
}
}
Loading