From c680d19f443d82e46c67cca498965679ded79494 Mon Sep 17 00:00:00 2001 From: Hayk Zakaryan Date: Tue, 9 Apr 2024 13:12:44 +0400 Subject: [PATCH] When calling UploadedFile::getErrorMessage() to a file which has no error and is uploaded successfully, it should not return an error --- .../HttpFoundation/File/UploadedFile.php | 30 +++++++++++++------ .../Tests/File/UploadedFileTest.php | 26 ++++++++++++++++ 2 files changed, 47 insertions(+), 9 deletions(-) diff --git a/src/Symfony/Component/HttpFoundation/File/UploadedFile.php b/src/Symfony/Component/HttpFoundation/File/UploadedFile.php index 74e929f9b89e5..2dcbc5a75398c 100644 --- a/src/Symfony/Component/HttpFoundation/File/UploadedFile.php +++ b/src/Symfony/Component/HttpFoundation/File/UploadedFile.php @@ -201,22 +201,22 @@ public function move(string $directory, ?string $name = null): File switch ($this->error) { case \UPLOAD_ERR_INI_SIZE: - throw new IniSizeFileException($this->getErrorMessage()); + throw new IniSizeFileException($this->getExceptionMessage()); case \UPLOAD_ERR_FORM_SIZE: - throw new FormSizeFileException($this->getErrorMessage()); + throw new FormSizeFileException($this->getExceptionMessage()); case \UPLOAD_ERR_PARTIAL: - throw new PartialFileException($this->getErrorMessage()); + throw new PartialFileException($this->getExceptionMessage()); case \UPLOAD_ERR_NO_FILE: - throw new NoFileException($this->getErrorMessage()); + throw new NoFileException($this->getExceptionMessage()); case \UPLOAD_ERR_CANT_WRITE: - throw new CannotWriteFileException($this->getErrorMessage()); + throw new CannotWriteFileException($this->getExceptionMessage()); case \UPLOAD_ERR_NO_TMP_DIR: - throw new NoTmpDirFileException($this->getErrorMessage()); + throw new NoTmpDirFileException($this->getExceptionMessage()); case \UPLOAD_ERR_EXTENSION: - throw new ExtensionFileException($this->getErrorMessage()); + throw new ExtensionFileException($this->getExceptionMessage()); } - throw new FileException($this->getErrorMessage()); + throw new FileException($this->getExceptionMessage()); } /** @@ -265,7 +265,7 @@ private static function parseFilesize(string $size): int|float /** * Returns an informative upload error message. */ - public function getErrorMessage(): string + private function getExceptionMessage(): string { static $errors = [ \UPLOAD_ERR_INI_SIZE => 'The file "%s" exceeds your upload_max_filesize ini directive (limit is %d KiB).', @@ -283,4 +283,16 @@ public function getErrorMessage(): string return sprintf($message, $this->getClientOriginalName(), $maxFilesize); } + + /** + * Retrieves a user-friendly error message for file upload issues, if any. + */ + public function getErrorMessage(): string + { + if (\UPLOAD_ERR_OK === $this->error) { + return ''; + } + + return $this->getExceptionMessage(); + } } diff --git a/src/Symfony/Component/HttpFoundation/Tests/File/UploadedFileTest.php b/src/Symfony/Component/HttpFoundation/Tests/File/UploadedFileTest.php index 9c18ad1839420..2b101550f9a3b 100644 --- a/src/Symfony/Component/HttpFoundation/Tests/File/UploadedFileTest.php +++ b/src/Symfony/Component/HttpFoundation/Tests/File/UploadedFileTest.php @@ -119,6 +119,32 @@ public function testErrorIsOkByDefault() $this->assertEquals(\UPLOAD_ERR_OK, $file->getError()); } + public function testInvalidFile() + { + $this->expectException(FileException::class); + $this->expectExceptionMessage('The file "original.gif" was not uploaded due to an unknown error.'); + + $file = new UploadedFile( + __DIR__.'/Fixtures/test.gif', + 'original.gif', + 'image/gif', + ); + + $file->move(__DIR__.'/Fixtures/directory'); + } + + public function testNoErrorMessageIfErrorIsUploadErrOk() + { + $file = new UploadedFile( + __DIR__.'/Fixtures/test.gif', + 'original.gif', + 'image/gif', + null + ); + + $this->assertEquals('', $file->getErrorMessage()); + } + public function testGetClientOriginalName() { $file = new UploadedFile( pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy