From 35fc85ec07f352f392f51addd6dbf369a967e5e3 Mon Sep 17 00:00:00 2001 From: Pierre du Plessis Date: Tue, 19 May 2020 10:49:18 +0200 Subject: [PATCH] [Validator] Use Mime component to determine mime type for file validator --- .../Validator/Constraints/FileValidator.php | 13 +++- .../Tests/Constraints/FileValidatorTest.php | 71 ++++-------------- .../Tests/Constraints/Fixtures/blank.jpg | Bin 0 -> 2872 bytes .../Tests/Constraints/Fixtures/blank.pdf | Bin 0 -> 4911 bytes 4 files changed, 24 insertions(+), 60 deletions(-) create mode 100644 src/Symfony/Component/Validator/Tests/Constraints/Fixtures/blank.jpg create mode 100644 src/Symfony/Component/Validator/Tests/Constraints/Fixtures/blank.pdf diff --git a/src/Symfony/Component/Validator/Constraints/FileValidator.php b/src/Symfony/Component/Validator/Constraints/FileValidator.php index 52e937944188b..7cdf5b1aeda4d 100644 --- a/src/Symfony/Component/Validator/Constraints/FileValidator.php +++ b/src/Symfony/Component/Validator/Constraints/FileValidator.php @@ -13,8 +13,10 @@ use Symfony\Component\HttpFoundation\File\File as FileObject; use Symfony\Component\HttpFoundation\File\UploadedFile; +use Symfony\Component\Mime\MimeTypes; use Symfony\Component\Validator\Constraint; use Symfony\Component\Validator\ConstraintValidator; +use Symfony\Component\Validator\Exception\LogicException; use Symfony\Component\Validator\Exception\UnexpectedTypeException; use Symfony\Component\Validator\Exception\UnexpectedValueException; @@ -170,12 +172,17 @@ public function validate($value, Constraint $constraint) } if ($constraint->mimeTypes) { - if (!$value instanceof FileObject) { - $value = new FileObject($value); + if (class_exists(MimeTypes::class)) { + $mime = MimeTypes::getDefault()->guessMimeType($path); + } elseif ($value instanceof FileObject) { + $mime = $value->getMimeType(); + } elseif (!class_exists(FileObject::class)) { + throw new LogicException('You cannot validate the mime-type of files as the Mime component is not installed. Try running "composer require symfony/mime".'); + } else { + $mime = (new FileObject($value))->getMimeType(); } $mimeTypes = (array) $constraint->mimeTypes; - $mime = $value->getMimeType(); foreach ($mimeTypes as $mimeType) { if ($mimeType === $mime) { diff --git a/src/Symfony/Component/Validator/Tests/Constraints/FileValidatorTest.php b/src/Symfony/Component/Validator/Tests/Constraints/FileValidatorTest.php index 0bbac3c6b70a9..6e024431d4153 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/FileValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/FileValidatorTest.php @@ -11,6 +11,7 @@ namespace Symfony\Component\Validator\Tests\Constraints; +use Symfony\Component\HttpFoundation\File\File as FileObject; use Symfony\Component\HttpFoundation\File\UploadedFile; use Symfony\Component\Validator\Constraints\File; use Symfony\Component\Validator\Constraints\FileValidator; @@ -283,21 +284,10 @@ public function testBinaryFormat($bytesWritten, $limit, $binaryFormat, $sizeAsSt public function testValidMimeType() { - $file = $this - ->getMockBuilder('Symfony\Component\HttpFoundation\File\File') - ->setConstructorArgs([__DIR__.'/Fixtures/foo']) - ->getMock(); - $file - ->expects($this->once()) - ->method('getPathname') - ->willReturn($this->path); - $file - ->expects($this->once()) - ->method('getMimeType') - ->willReturn('image/jpg'); + $file = new FileObject(__DIR__.'/Fixtures/blank.jpg'); $constraint = new File([ - 'mimeTypes' => ['image/png', 'image/jpg'], + 'mimeTypes' => ['image/png', 'image/jpeg'], ]); $this->validator->validate($file, $constraint); @@ -307,18 +297,7 @@ public function testValidMimeType() public function testValidWildcardMimeType() { - $file = $this - ->getMockBuilder('Symfony\Component\HttpFoundation\File\File') - ->setConstructorArgs([__DIR__.'/Fixtures/foo']) - ->getMock(); - $file - ->expects($this->once()) - ->method('getPathname') - ->willReturn($this->path); - $file - ->expects($this->once()) - ->method('getMimeType') - ->willReturn('image/jpg'); + $file = new FileObject(__DIR__.'/Fixtures/test.gif'); $constraint = new File([ 'mimeTypes' => ['image/*'], @@ -331,21 +310,10 @@ public function testValidWildcardMimeType() public function testInvalidMimeType() { - $file = $this - ->getMockBuilder('Symfony\Component\HttpFoundation\File\File') - ->setConstructorArgs([__DIR__.'/Fixtures/foo']) - ->getMock(); - $file - ->expects($this->once()) - ->method('getPathname') - ->willReturn($this->path); - $file - ->expects($this->once()) - ->method('getMimeType') - ->willReturn('application/pdf'); + $file = new FileObject(__DIR__.'/Fixtures/blank.pdf'); $constraint = new File([ - 'mimeTypes' => ['image/png', 'image/jpg'], + 'mimeTypes' => ['image/png', 'image/jpeg'], 'mimeTypesMessage' => 'myMessage', ]); @@ -353,30 +321,19 @@ public function testInvalidMimeType() $this->buildViolation('myMessage') ->setParameter('{{ type }}', '"application/pdf"') - ->setParameter('{{ types }}', '"image/png", "image/jpg"') - ->setParameter('{{ file }}', '"'.$this->path.'"') - ->setParameter('{{ name }}', '"'.basename($this->path).'"') + ->setParameter('{{ types }}', '"image/png", "image/jpeg"') + ->setParameter('{{ file }}', '"'.__DIR__.'/Fixtures/blank.pdf'.'"') + ->setParameter('{{ name }}', '"blank.pdf"') ->setCode(File::INVALID_MIME_TYPE_ERROR) ->assertRaised(); } public function testInvalidWildcardMimeType() { - $file = $this - ->getMockBuilder('Symfony\Component\HttpFoundation\File\File') - ->setConstructorArgs([__DIR__.'/Fixtures/foo']) - ->getMock(); - $file - ->expects($this->once()) - ->method('getPathname') - ->willReturn($this->path); - $file - ->expects($this->once()) - ->method('getMimeType') - ->willReturn('application/pdf'); + $file = new FileObject(__DIR__.'/Fixtures/blank.pdf'); $constraint = new File([ - 'mimeTypes' => ['image/*', 'image/jpg'], + 'mimeTypes' => ['image/*', 'image/jpeg'], 'mimeTypesMessage' => 'myMessage', ]); @@ -384,9 +341,9 @@ public function testInvalidWildcardMimeType() $this->buildViolation('myMessage') ->setParameter('{{ type }}', '"application/pdf"') - ->setParameter('{{ types }}', '"image/*", "image/jpg"') - ->setParameter('{{ file }}', '"'.$this->path.'"') - ->setParameter('{{ name }}', '"'.basename($this->path).'"') + ->setParameter('{{ types }}', '"image/*", "image/jpeg"') + ->setParameter('{{ file }}', '"'.__DIR__.'/Fixtures/blank.pdf'.'"') + ->setParameter('{{ name }}', '"blank.pdf"') ->setCode(File::INVALID_MIME_TYPE_ERROR) ->assertRaised(); } diff --git a/src/Symfony/Component/Validator/Tests/Constraints/Fixtures/blank.jpg b/src/Symfony/Component/Validator/Tests/Constraints/Fixtures/blank.jpg new file mode 100644 index 0000000000000000000000000000000000000000..51246b40a12659568b6f569032fd26ece541b552 GIT binary patch literal 2872 zcmeH@y$u2}427TV%bgRhAQH@T-AOP7!>|lYLBSCGG;9)_tB9zm@rxtbFS6`>@_M$*V$9FsJ;5L$^}^& zbP~$oOp>Wwv0c{0r=r=Lwe8)iy7fz~s++&v2Sy>AA{i6ThqlKnC*TB}fD>>6PQVE` Kfj=iOmHQih0U7=P literal 0 HcmV?d00001 diff --git a/src/Symfony/Component/Validator/Tests/Constraints/Fixtures/blank.pdf b/src/Symfony/Component/Validator/Tests/Constraints/Fixtures/blank.pdf new file mode 100644 index 0000000000000000000000000000000000000000..757bb1f36404ff475efeb6fda3350468de425004 GIT binary patch literal 4911 zcmeHLe{2)i9XCJ9kXvt2mjY9@?&LNdEIr@5cX$4AvrUq7+=Rr$*iixkCeC;7k}GGQ zxjQFLTE;>IZRpl^i&V8+D^*duij``EwSTSch#*r}3D`unF|A58CN>q(GNE*6bcnsP zoj7)l#iah3lP$gbzW4pS&-e4*_u1dq6putn+T(RKUHtpG^DZZ32|(zn$6Uc66;(A0 z8B|mgEfhhLE+cF*Op9QtZj>3Y1_?-esjUP}^^Rl_)suQGX{Ezq(=w2lb~y)UgU@d3 zIt3aJ-TGkv&ZjTTzWNaVjd8WH@id%y_exV&hI_&LlZSt{d9H8f&*s#G;2)-i2PT=-pI`j<)7@J??fBP+Ig{Oe_4CistH+5q6RP)Tf!xKmP-#PZJ z@taSAiS^w;e(BfDJO4fO7JWe*oO)rySNSnx@|V{eTl@Swu70%Vl={-!-`jPqch}>g zCtBY4wB@eJf4+EW^d@=I@x(9U$6QX7kxN11mMs?K{vXxYKt0h;#6kR5$NxBdmO9hC z4Sab0Z1<*TyzENnnwiT__O5&N@>d@E`RnWc`j-`}H#p{Q-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