Pārlūkot izejas kodu

Merge remote branch 'Seldaek/file_field'

* Seldaek/file_field:
  [HttpFoundation] Fixed test breaking on windows
  [HttpFoundation] UploadedFile::getOriginalName is now overriding getName
  [HttpFoundation] Minor optimization
  [Form] Add exception for missing enctype attribute
Fabien Potencier 14 gadi atpakaļ
vecāks
revīzija
1fe25bc00a

+ 5 - 1
src/Symfony/Component/Form/FileField.php

@@ -13,6 +13,7 @@ namespace Symfony\Component\Form;
 
 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 @@ class FileField extends Form
     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;
@@ -86,7 +90,7 @@ class FileField extends Form
                 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;
             }

+ 2 - 2
src/Symfony/Component/HttpFoundation/File/File.php

@@ -2,7 +2,7 @@
 
 /*
  * This file is part of the Symfony package.
- * 
+ *
  * (c) Fabien Potencier <fabien@symfony.com>
  *
  * For the full copyright and license information, please view the LICENSE
@@ -497,7 +497,7 @@ class File
      */
     public function __toString()
     {
-        return null === $this->getPath() ? '' : $this->getPath();
+        return null === $this->path ? '' : $this->path;
     }
 
     /**

+ 10 - 5
src/Symfony/Component/HttpFoundation/File/UploadedFile.php

@@ -2,7 +2,7 @@
 
 /*
  * This file is part of the Symfony package.
- * 
+ *
  * (c) Fabien Potencier <fabien@symfony.com>
  *
  * For the full copyright and license information, please view the LICENSE
@@ -101,13 +101,18 @@ class UploadedFile extends File
     }
 
     /**
-     * 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();
     }
 
     /**

+ 13 - 1
tests/Symfony/Tests/Component/Form/FileFieldTest.php

@@ -70,7 +70,7 @@ class FileFieldTest extends \PHPUnit_Framework_TestCase
                 $that->createTmpFile($tmpPath);
              }));
         $file->expects($this->any())
-             ->method('getOriginalName')
+             ->method('getName')
              ->will($this->returnValue('original_name.jpg'));
 
         $this->field->submit(array(
@@ -111,6 +111,18 @@ class FileFieldTest extends \PHPUnit_Framework_TestCase
         $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');

+ 3 - 0
tests/Symfony/Tests/Component/HttpFoundation/File/MimeType/MimeTypeTest.php

@@ -59,6 +59,9 @@ class MimeTypeTest extends \PHPUnit_Framework_TestCase
 
     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);

+ 1 - 1
tests/Symfony/Tests/Component/HttpFoundation/File/UploadedFileTest.php

@@ -95,7 +95,7 @@ class UploadedFileTest extends \PHPUnit_Framework_TestCase
                 null
             );
 
-            $this->assertEquals('original.gif', $file->getOriginalName());
+            $this->assertEquals('original.gif', $file->getName());
         }
     }
 }