bug #13769 [Form] NativeRequestHandler file handling fix (mpajunen)

This PR was squashed before being merged into the 2.3 branch (closes #13769).

Discussion
----------

[Form] NativeRequestHandler file handling fix

| Q             | A
| ------------- | ---
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #13746
| License       | MIT
| Doc PR        | n/a

`NativeRequestHandler` reused the local variable for the form name `$name` as a loop variable for processing the `$_FILES` array. A separate variable is now used.

Two new test cases are included:
- Handling a request with multiple files
- Handling a request with file upload and a nameless form

Both tests fail without the fix. The test cases could probably be better though.

Commits
-------

9b3421f [Form] NativeRequestHandler file handling fix
This commit is contained in:
Fabien Potencier 2015-02-25 09:33:53 +01:00
commit ab7b3a8119
6 changed files with 52 additions and 8 deletions

View File

@ -98,8 +98,8 @@ class NativeRequestHandler implements RequestHandlerInterface
}
$fixedFiles = array();
foreach ($_FILES as $name => $file) {
$fixedFiles[$name] = self::stripEmptyFiles(self::fixPhpFilesArray($file));
foreach ($_FILES as $fileKey => $file) {
$fixedFiles[$fileKey] = self::stripEmptyFiles(self::fixPhpFilesArray($file));
}
if ('' === $name) {

View File

@ -266,6 +266,50 @@ abstract class AbstractRequestHandlerTest extends \PHPUnit_Framework_TestCase
$this->requestHandler->handleRequest($form, $this->request);
}
/**
* @dataProvider methodExceptGetProvider
*/
public function testSubmitMultipleFiles($method)
{
$form = $this->getMockForm('param1', $method);
$file = $this->getMockFile();
$this->setRequestData($method, array(
'param1' => null,
), array(
'param2' => $this->getMockFile('2'),
'param1' => $file,
'param3' => $this->getMockFile('3'),
));
$form->expects($this->once())
->method('submit')
->with($file, 'PATCH' !== $method);
$this->requestHandler->handleRequest($form, $this->request);
}
/**
* @dataProvider methodExceptGetProvider
*/
public function testSubmitFileWithNamelessForm($method)
{
$form = $this->getMockForm(null, $method);
$file = $this->getMockFile();
$this->setRequestData($method, array(
'' => null,
), array(
'' => $file,
));
$form->expects($this->once())
->method('submit')
->with($file, 'PATCH' !== $method);
$this->requestHandler->handleRequest($form, $this->request);
}
/**
* @dataProvider getPostMaxSizeFixtures
*/
@ -314,7 +358,7 @@ abstract class AbstractRequestHandlerTest extends \PHPUnit_Framework_TestCase
abstract protected function getRequestHandler();
abstract protected function getMockFile();
abstract protected function getMockFile($suffix = '');
protected function getMockForm($name, $method = null, $compound = true)
{

View File

@ -46,8 +46,8 @@ class HttpFoundationRequestHandlerTest extends AbstractRequestHandlerTest
return new HttpFoundationRequestHandler($this->serverParams);
}
protected function getMockFile()
protected function getMockFile($suffix = '')
{
return new UploadedFile(__DIR__.'/../../Fixtures/foo', 'foo');
return new UploadedFile(__DIR__.'/../../Fixtures/foo'.$suffix, 'foo'.$suffix);
}
}

View File

@ -206,12 +206,12 @@ class NativeRequestHandlerTest extends AbstractRequestHandlerTest
return new NativeRequestHandler($this->serverParams);
}
protected function getMockFile()
protected function getMockFile($suffix = '')
{
return array(
'name' => 'upload.txt',
'name' => 'upload'.$suffix.'.txt',
'type' => 'text/plain',
'tmp_name' => 'owfdskjasdfsa',
'tmp_name' => 'owfdskjasdfsa'.$suffix,
'error' => UPLOAD_ERR_OK,
'size' => 100,
);