8000 [Validator] Improve Image constraint invalid mime type message by fancyweb · Pull Request #45090 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Validator] Improve Image constraint invalid mime type message #45090

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
Mar 28, 2022
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 t 8000 o load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/Symfony/Component/Validator/Constraints/Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -189,5 +189,9 @@ public function __construct(
$this->allowLandscapeMessage = $allowLandscapeMessage ?? $this->allowLandscapeMessage;
$this->allowPortraitMessage = $allowPortraitMessage ?? $this->allowPortraitMessage;
$this->corruptedMessage = $corruptedMessage ?? $this->corruptedMessage;

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 }}.';
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -547,4 +547,36 @@ public function provideDetectCorruptedConstraints(): iterable
new Image(detectCorrupted: true, corruptedMessage: 'myMessage'),
];
}

/**
* @dataProvider provideInvalidMimeTypeWithNarrowedSet
*/
public function testInvalidMimeTypeWithNarrowedSet(Image $constraint)
{
$this->validator->validate($this->image, $constraint);

$this->buildViolation('The mime type of the file is invalid ({{ type }}). Allowed mime types are {{ types }}.')
->setParameter('{{ file }}', sprintf('"%s"', $this->image))
->setParameter('{{ type }}', '"image/gif"')
->setParameter('{{ types }}', '"image/jpeg", "image/png"')
->setParameter('{{ name }}', '"test.gif"')
->setCode(Image::INVALID_MIME_TYPE_ERROR)
->assertRaised();
}

public function provideInvalidMimeTypeWithNarrowedSet()
{
yield 'Doctrine style' => [new Image([
'mimeTypes' => [
'image/jpeg',
'image/png',
],
])];
yield 'Named arguments' => [
new Image(mimeTypes: [
'image/jpeg',
'image/png',
]),
];
}
}
0