From 1b253cb14d86e7732d3fd4010247fdf46e47c6b3 Mon Sep 17 00:00:00 2001 From: Vyacheslav Salakhutdinov Date: Sun, 15 Jun 2014 13:48:08 +0000 Subject: [PATCH] Disallow empty file in FileValidator --- src/Symfony/Component/Validator/CHANGELOG.md | 5 +++++ .../Component/Validator/Constraints/File.php | 1 + .../Validator/Constraints/FileValidator.php | 6 ++++-- .../Tests/Constraints/FileValidatorTest.php | 16 ++++++++++++++++ 4 files changed, 26 insertions(+), 2 deletions(-) diff --git a/src/Symfony/Component/Validator/CHANGELOG.md b/src/Symfony/Component/Validator/CHANGELOG.md index 34fe232dcf..4dcb568a61 100644 --- a/src/Symfony/Component/Validator/CHANGELOG.md +++ b/src/Symfony/Component/Validator/CHANGELOG.md @@ -1,6 +1,11 @@ CHANGELOG ========= +2.6.0 +----- + + * [BC BREAK] `FileValidator` disallow empty files + 2.5.0 ----- diff --git a/src/Symfony/Component/Validator/Constraints/File.php b/src/Symfony/Component/Validator/Constraints/File.php index 80527171ec..60d4ae14a7 100644 --- a/src/Symfony/Component/Validator/Constraints/File.php +++ b/src/Symfony/Component/Validator/Constraints/File.php @@ -29,6 +29,7 @@ class File extends Constraint public $notReadableMessage = 'The file is not readable.'; public $maxSizeMessage = 'The file is too large ({{ size }} {{ suffix }}). Allowed maximum size is {{ limit }} {{ suffix }}.'; public $mimeTypesMessage = 'The mime type of the file is invalid ({{ type }}). Allowed mime types are {{ types }}.'; + public $disallowEmptyMessage = 'An empty file is not allowed.'; public $uploadIniSizeErrorMessage = 'The file is too large. Allowed maximum size is {{ limit }} {{ suffix }}.'; public $uploadFormSizeErrorMessage = 'The file is too large.'; diff --git a/src/Symfony/Component/Validator/Constraints/FileValidator.php b/src/Symfony/Component/Validator/Constraints/FileValidator.php index 22273c4dcb..f967ac48d2 100644 --- a/src/Symfony/Component/Validator/Constraints/FileValidator.php +++ b/src/Symfony/Component/Validator/Constraints/FileValidator.php @@ -121,8 +121,10 @@ class FileValidator extends ConstraintValidator return; } - if ($constraint->maxSize) { - $sizeInBytes = filesize($path); + $sizeInBytes = filesize($path); + if (0 === $sizeInBytes) { + $this->context->addViolation($constraint->disallowEmptyMessage); + } elseif ($constraint->maxSize) { $limitInBytes = (int) $constraint->maxSize; if (preg_match('/^\d++k$/', $constraint->maxSize)) { diff --git a/src/Symfony/Component/Validator/Tests/Constraints/FileValidatorTest.php b/src/Symfony/Component/Validator/Tests/Constraints/FileValidatorTest.php index d2d2bcb34f..eb89551ce2 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/FileValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/FileValidatorTest.php @@ -29,6 +29,7 @@ abstract class FileValidatorTest extends \PHPUnit_Framework_TestCase $this->validator->initialize($this->context); $this->path = sys_get_temp_dir().DIRECTORY_SEPARATOR.'FileValidatorTest'; $this->file = fopen($this->path, 'w'); + fwrite($this->file, ' ', 1); } protected function tearDown() @@ -319,6 +320,21 @@ abstract class FileValidatorTest extends \PHPUnit_Framework_TestCase $this->validator->validate($file, $constraint); } + public function testDisallowEmpty() + { + ftruncate($this->file, 0); + + $constraint = new File(array( + 'disallowEmptyMessage' => 'myMessage', + )); + + $this->context->expects($this->once()) + ->method('addViolation') + ->with('myMessage'); + + $this->validator->validate($this->getFile($this->path), $constraint); + } + /** * @dataProvider uploadedFileErrorProvider */