10000 Merge remote branch 'Seldaek/file_field' · umpirski/symfony@1fe25bc · GitHub
[go: up one dir, main page]

Skip to content

Commit 1fe25bc

Browse files
committed
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
2 parents 17ef911 + 30a8f4f commit 1fe25bc

File tree

6 files changed

+34
-10
lines changed

6 files changed

+34
-10
lines changed

src/Symfony/Component/Form/FileField.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use Symfony\Component\HttpFoundation\File\File;
1515
use Symfony\Component\Form\Exception\FormException;
16+
use Symfony\Component\HttpFoundation\File\UploadedFile;
1617

1718
/**
1819
* A file field to upload files.
@@ -66,6 +67,9 @@ protected function configure()
6667
protected function preprocessData(array $data)
6768
{
6869
if ($data['file']) {
70+
if (!$data['file'] instanceof UploadedFile) {
71+
throw new \UnexpectedValueException('Uploaded file is not of type UploadedFile, your form tag is probably missing the enctype="multipart/form-data" attribute.');
72+
}
6973
switch ($data['file']->getError()) {
7074
case UPLOAD_ERR_INI_SIZE:
7175
$this->iniSizeExceeded = true;
@@ -86,7 +90,7 @@ protected function preprocessData(array $data)
8690
default:
8791
$data['file']->move($this->getTmpDir());
8892
$data['file']->rename($this->getTmpName($data['token']));
89-
$data['original_name'] = $data['file']->getOriginalName();
93+
$data['original_name'] = $data['file']->getName();
9094
$data['file'] = '';
9195
break;
9296
}

src/Symfony/Component/HttpFoundation/File/File.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
/*
44
* This file is part of the Symfony package.
5-
*
5+
*
66
* (c) Fabien Potencier <fabien@symfony.com>
77
*
88
* For the full copyright and license information, please view the LICENSE
@@ -497,7 +497,7 @@ public function __construct($path)
497497
*/
498498
public function __toString()
499499
{
500-
return null === $this->getPath() ? '' : $this->getPath();
500+
return null === $this->path ? '' : $this->path;
501501
}
502502

503503
/**

src/Symfony/Component/HttpFoundation/File/UploadedFile.php

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
/*
44
* This file is part of the Symfony package.
5-
*
5+
*
66
* (c) Fabien Potencier <fabien@symfony.com>
77
*
88
* For the full copyright and license information, please view the LICENSE
@@ -101,13 +101,18 @@ public function getMimeType()
101101
}
102102

103103
/**
104-
* Returns the original file name including its extension.
104+
* Returns the absolute file name without dots
105+
*
106+
* Until the uploaded file is moved, it will return the name of the temporary file
105107
*
106-
* @returns string The file name
108+
* @returns string The file path
107109
*/
108-
public function getOriginalName()
110+
public function getName()
109111
{
110-
return $this->originalName;
112+
if (!$this->moved) {
113+
return $this->originalName;
114+
}
115+
return parent::getName();
111116
}
112117

113118
/**

tests/Symfony/Tests/Component/Form/FileFieldTest.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public function testSubmitUploadsNewFiles()
7070
$that->createTmpFile($tmpPath);
7171
}));
7272
$file->expects($this->any())
73-
->method('getOriginalName')
73+
->method('getName')
7474
->will($this->returnValue('original_name.jpg'));
7575

7676
$this->field->submit(array(
@@ -111,6 +111,18 @@ public function testSubmitKeepsUploadedFilesOnErrors()
111111
$this->assertEquals(realpath($tmpPath), realpath($this->field->getData()));
112112
}
113113

114+
/**
115+
* @expectedException UnexpectedValueException
116+
*/
117+
public function testSubmitFailsOnMissingMultipart()
118+
{
119+
$this->field->submit(array(
120+
'file' => 'foo.jpg',
121+
'token' => '12345',
122+
'original_name' => 'original_name.jpg',
123+
));
124+
}
125+
114126
public function testSubmitKeepsOldFileIfNotOverwritten()
115127
{
116128
$oldPath = tempnam(sys_get_temp_dir(), 'FileFieldTest');

tests/Symfony/Tests/Component/HttpFoundation/File/MimeType/MimeTypeTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@ public function testGuessWithIncorrectPath()
5959

6060
public function testGuessWithNonReadablePath()
6161
{
62+
if (strstr(PHP_OS, 'WIN')) {
63+
$this->markTestSkipped('Can not verify chmod operations on Windows');
64+
}
6265
$path = __DIR__.'/../Fixtures/to_delete';
6366
touch($path);
6467
chmod($path, 0333);

tests/Symfony/Tests/Component/HttpFoundation/File/UploadedFileTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ public function testGetOriginalName()
9595
null
9696
);
9797

98-
$this->assertEquals('original.gif', $file->getOriginalName());
98+
$this->assertEquals('original.gif', $file->getName());
9999
}
100100
}
101101
}

0 commit comments

Comments
 (0)
0