From 88cfc4c0114b4ec7f371467cc2e3017f43d51f3a Mon Sep 17 00:00:00 2001 From: Jordi Boggiano Date: Wed, 9 Feb 2011 15:09:46 +0100 Subject: [PATCH 1/4] [Form] Add exception for missing enctype attribute --- src/Symfony/Component/Form/FileField.php | 4 ++++ tests/Symfony/Tests/Component/Form/FileFieldTest.php | 12 ++++++++++++ 2 files changed, 16 insertions(+) diff --git a/src/Symfony/Component/Form/FileField.php b/src/Symfony/Component/Form/FileField.php index 7941b8b602dea..3e0ebdf0d5b63 100644 --- a/src/Symfony/Component/Form/FileField.php +++ b/src/Symfony/Component/Form/FileField.php @@ -13,6 +13,7 @@ use Symfony\Component\HttpFoundation\File\File; use Symfony\Component\Form\Exception\FormException; +use Symfony\Component\HttpFoundation\File\UploadedFile; /** * A file field to upload files. @@ -66,6 +67,9 @@ protected function configure() protected function preprocessData(array $data) { if ($data['file']) { + if (!$data['file'] instanceof UploadedFile) { + throw new \UnexpectedValueException('Uploaded file is not of type UploadedFile, your form tag is probably missing the enctype="multipart/form-data" attribute.'); + } switch ($data['file']->getError()) { case UPLOAD_ERR_INI_SIZE: $this->iniSizeExceeded = true; diff --git a/tests/Symfony/Tests/Component/Form/FileFieldTest.php b/tests/Symfony/Tests/Component/Form/FileFieldTest.php index 4f0ecca9c5010..72a834cc5341e 100644 --- a/tests/Symfony/Tests/Component/Form/FileFieldTest.php +++ b/tests/Symfony/Tests/Component/Form/FileFieldTest.php @@ -111,6 +111,18 @@ public function testSubmitKeepsUploadedFilesOnErrors() $this->assertEquals(realpath($tmpPath), realpath($this->field->getData())); } + /** + * @expectedException UnexpectedValueException + */ + public function testSubmitFailsOnMissingMultipart() + { + $this->field->submit(array( + 'file' => 'foo.jpg', + 'token' => '12345', + 'original_name' => 'original_name.jpg', + )); + } + public function testSubmitKeepsOldFileIfNotOverwritten() { $oldPath = tempnam(sys_get_temp_dir(), 'FileFieldTest'); From 2a5fcdfb1a372e02e555701299d547ca12432542 Mon Sep 17 00:00:00 2001 From: Jordi Boggiano Date: Wed, 9 Feb 2011 19:52:21 +0100 Subject: [PATCH 2/4] [HttpFoundation] Minor optimization --- src/Symfony/Component/HttpFoundation/File/File.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Symfony/Component/HttpFoundation/File/File.php b/src/Symfony/Component/HttpFoundation/File/File.php index ea03ac4f0942b..d72405602852d 100644 --- a/src/Symfony/Component/HttpFoundation/File/File.php +++ b/src/Symfony/Component/HttpFoundation/File/File.php @@ -2,7 +2,7 @@ /* * This file is part of the Symfony package. - * + * * (c) Fabien Potencier * * For the full copyright and license information, please view the LICENSE @@ -497,7 +497,7 @@ public function __construct($path) */ public function __toString() { - return null === $this->getPath() ? '' : $this->getPath(); + return null === $this->path ? '' : $this->path; } /** From 991b1ed22504de51fe2ebca6eb898c4fa4da2e0c Mon Sep 17 00:00:00 2001 From: Jordi Boggiano Date: Wed, 9 Feb 2011 20:04:59 +0100 Subject: [PATCH 3/4] [HttpFoundation] UploadedFile::getOriginalName is now overriding getName --- src/Symfony/Component/Form/FileField.php | 2 +- .../HttpFoundation/File/UploadedFile.php | 15 ++++++++++----- .../Tests/Component/Form/FileFieldTest.php | 2 +- .../HttpFoundation/File/UploadedFileTest.php | 2 +- 4 files changed, 13 insertions(+), 8 deletions(-) diff --git a/src/Symfony/Component/Form/FileField.php b/src/Symfony/Component/Form/FileField.php index 3e0ebdf0d5b63..593982d63ea71 100644 --- a/src/Symfony/Component/Form/FileField.php +++ b/src/Symfony/Component/Form/FileField.php @@ -90,7 +90,7 @@ protected function preprocessData(array $data) default: $data['file']->move($this->getTmpDir()); $data['file']->rename($this->getTmpName($data['token'])); - $data['original_name'] = $data['file']->getOriginalName(); + $data['original_name'] = $data['file']->getName(); $data['file'] = ''; break; } diff --git a/src/Symfony/Component/HttpFoundation/File/UploadedFile.php b/src/Symfony/Component/HttpFoundation/File/UploadedFile.php index cf897885c936c..8386d73b6c149 100644 --- a/src/Symfony/Component/HttpFoundation/File/UploadedFile.php +++ b/src/Symfony/Component/HttpFoundation/File/UploadedFile.php @@ -2,7 +2,7 @@ /* * This file is part of the Symfony package. - * + * * (c) Fabien Potencier * * For the full copyright and license information, please view the LICENSE @@ -101,13 +101,18 @@ public function getMimeType() } /** - * Returns the original file name including its extension. + * Returns the absolute file name without dots + * + * Until the uploaded file is moved, it will return the name of the temporary file * - * @returns string The file name + * @returns string The file path */ - public function getOriginalName() + public function getName() { - return $this->originalName; + if (!$this->moved) { + return $this->originalName; + } + return parent::getName(); } /** diff --git a/tests/Symfony/Tests/Component/Form/FileFieldTest.php b/tests/Symfony/Tests/Component/Form/FileFieldTest.php index 72a834cc5341e..b6a44bbc98d4e 100644 --- a/tests/Symfony/Tests/Component/Form/FileFieldTest.php +++ b/tests/Symfony/Tests/Component/Form/FileFieldTest.php @@ -70,7 +70,7 @@ public function testSubmitUploadsNewFiles() $that->createTmpFile($tmpPath); })); $file->expects($this->any()) - ->method('getOriginalName') + ->method('getName') ->will($this->returnValue('original_name.jpg')); $this->field->submit(array( diff --git a/tests/Symfony/Tests/Component/HttpFoundation/File/UploadedFileTest.php b/tests/Symfony/Tests/Component/HttpFoundation/File/UploadedFileTest.php index a1e3249a150d8..bc924f2bb6481 100644 --- a/tests/Symfony/Tests/Component/HttpFoundation/File/UploadedFileTest.php +++ b/tests/Symfony/Tests/Component/HttpFoundation/File/UploadedFileTest.php @@ -95,7 +95,7 @@ public function testGetOriginalName() null ); - $this->assertEquals('original.gif', $file->getOriginalName()); + $this->assertEquals('original.gif', $file->getName()); } } } \ No newline at end of file From 30a8f4fd42592f7a5d03fbe38b198da9272e78fc Mon Sep 17 00:00:00 2001 From: Jordi Boggiano Date: Wed, 9 Feb 2011 20:05:23 +0100 Subject: [PATCH 4/4] [HttpFoundation] Fixed test breaking on windows --- .../Component/HttpFoundation/File/MimeType/MimeTypeTest.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/tests/Symfony/Tests/Component/HttpFoundation/File/MimeType/MimeTypeTest.php b/tests/Symfony/Tests/Component/HttpFoundation/File/MimeType/MimeTypeTest.php index 56032dfd9ad29..389f93e040ad9 100644 --- a/tests/Symfony/Tests/Component/HttpFoundation/File/MimeType/MimeTypeTest.php +++ b/tests/Symfony/Tests/Component/HttpFoundation/File/MimeType/MimeTypeTest.php @@ -59,6 +59,9 @@ public function testGuessWithIncorrectPath() public function testGuessWithNonReadablePath() { + if (strstr(PHP_OS, 'WIN')) { + $this->markTestSkipped('Can not verify chmod operations on Windows'); + } $path = __DIR__.'/../Fixtures/to_delete'; touch($path); chmod($path, 0333);