Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[Form][HttpFoundation] Fixes to FileField #83

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
4 commits merged into from
Mar 9, 2011
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/Symfony/Component/Form/FileField.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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;
Expand All @@ -86,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;
}
Expand Down
4 changes: 2 additions & 2 deletions src/Symfony/Component/HttpFoundation/File/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

/*
* This file is part of the Symfony package.
*
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
Expand Down Expand Up @@ -497,7 +497,7 @@ public function __construct($path)
*/
public function __toString()
{
return null === $this->getPath() ? '' : $this->getPath();
return null === $this->path ? '' : $this->path;
}

/**
Expand Down
15 changes: 10 additions & 5 deletions src/Symfony/Component/HttpFoundation/File/UploadedFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

/*
* This file is part of the Symfony package.
*
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
Expand Down Expand Up @@ -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();
}

/**
Expand Down
14 changes: 13 additions & 1 deletion tests/Symfony/Tests/Component/Form/FileFieldTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public function testGetOriginalName()
null
);

$this->assertEquals('original.gif', $file->getOriginalName());
$this->assertEquals('original.gif', $file->getName());
}
}
}