2012-12-30 15:38:36 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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
|
|
|
|
* file that was distributed with this source code.
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Symfony\Component\Form\Tests;
|
|
|
|
|
2013-04-20 14:56:42 +01:00
|
|
|
use Symfony\Component\Form\NativeRequestHandler;
|
2012-12-30 15:38:36 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @author Bernhard Schussek <bschussek@gmail.com>
|
|
|
|
*/
|
2013-04-20 14:56:42 +01:00
|
|
|
class NativeRequestHandlerTest extends AbstractRequestHandlerTest
|
2012-12-30 15:38:36 +00:00
|
|
|
{
|
|
|
|
private static $serverBackup;
|
|
|
|
|
2019-08-02 18:02:27 +01:00
|
|
|
public static function setUpBeforeClass()
|
2012-12-30 15:38:36 +00:00
|
|
|
{
|
|
|
|
self::$serverBackup = $_SERVER;
|
|
|
|
}
|
|
|
|
|
2019-08-02 18:02:27 +01:00
|
|
|
protected function setUp()
|
2012-12-30 15:38:36 +00:00
|
|
|
{
|
|
|
|
parent::setUp();
|
|
|
|
|
2019-01-16 09:39:14 +00:00
|
|
|
$_GET = [];
|
|
|
|
$_POST = [];
|
|
|
|
$_FILES = [];
|
|
|
|
$_SERVER = [
|
2012-12-30 15:38:36 +00:00
|
|
|
// PHPUnit needs this entry
|
|
|
|
'SCRIPT_NAME' => self::$serverBackup['SCRIPT_NAME'],
|
2019-01-16 09:39:14 +00:00
|
|
|
];
|
2012-12-30 15:38:36 +00:00
|
|
|
}
|
|
|
|
|
2019-08-02 18:02:27 +01:00
|
|
|
protected function tearDown()
|
2012-12-30 15:38:36 +00:00
|
|
|
{
|
|
|
|
parent::tearDown();
|
|
|
|
|
2019-01-16 09:39:14 +00:00
|
|
|
$_GET = [];
|
|
|
|
$_POST = [];
|
|
|
|
$_FILES = [];
|
2012-12-30 15:38:36 +00:00
|
|
|
$_SERVER = self::$serverBackup;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testRequestShouldBeNull()
|
|
|
|
{
|
2019-08-01 23:48:42 +01:00
|
|
|
$this->expectException('Symfony\Component\Form\Exception\UnexpectedTypeException');
|
2019-01-18 17:22:25 +00:00
|
|
|
$this->requestHandler->handleRequest($this->createForm('name', 'GET'), 'request');
|
2012-12-30 15:38:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testMethodOverrideHeaderTakesPrecedenceIfPost()
|
|
|
|
{
|
2019-01-18 17:22:25 +00:00
|
|
|
$form = $this->createForm('param1', 'PUT');
|
2012-12-30 15:38:36 +00:00
|
|
|
|
2019-01-16 09:39:14 +00:00
|
|
|
$this->setRequestData('POST', [
|
2012-12-30 15:38:36 +00:00
|
|
|
'param1' => 'DATA',
|
2019-01-16 09:39:14 +00:00
|
|
|
]);
|
2012-12-30 15:38:36 +00:00
|
|
|
|
|
|
|
$_SERVER['HTTP_X_HTTP_METHOD_OVERRIDE'] = 'PUT';
|
|
|
|
|
2013-04-20 14:56:42 +01:00
|
|
|
$this->requestHandler->handleRequest($form, $this->request);
|
2019-01-18 17:22:25 +00:00
|
|
|
|
|
|
|
$this->assertTrue($form->isSubmitted());
|
|
|
|
$this->assertSame('DATA', $form->getData());
|
2012-12-30 15:38:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testConvertEmptyUploadedFilesToNull()
|
|
|
|
{
|
2019-01-18 17:22:25 +00:00
|
|
|
$form = $this->createForm('param1', 'POST', false);
|
2012-12-30 15:38:36 +00:00
|
|
|
|
2019-01-16 09:39:14 +00:00
|
|
|
$this->setRequestData('POST', [], ['param1' => [
|
2012-12-30 15:38:36 +00:00
|
|
|
'name' => '',
|
|
|
|
'type' => '',
|
|
|
|
'tmp_name' => '',
|
|
|
|
'error' => UPLOAD_ERR_NO_FILE,
|
2014-09-21 19:53:12 +01:00
|
|
|
'size' => 0,
|
2019-01-16 09:39:14 +00:00
|
|
|
]]);
|
2012-12-30 15:38:36 +00:00
|
|
|
|
2013-04-20 14:56:42 +01:00
|
|
|
$this->requestHandler->handleRequest($form, $this->request);
|
2019-01-18 17:22:25 +00:00
|
|
|
|
|
|
|
$this->assertTrue($form->isSubmitted());
|
|
|
|
$this->assertNull($form->getData());
|
2012-12-30 15:38:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testFixBuggyFilesArray()
|
|
|
|
{
|
2019-01-18 17:22:25 +00:00
|
|
|
$form = $this->createForm('param1', 'POST', true);
|
|
|
|
$fieldForm = $this->createBuilder('field', false, ['allow_file_upload' => true])->getForm();
|
|
|
|
$form->add($fieldForm);
|
2012-12-30 15:38:36 +00:00
|
|
|
|
2019-01-16 09:39:14 +00:00
|
|
|
$this->setRequestData('POST', [], ['param1' => [
|
|
|
|
'name' => [
|
2012-12-30 15:38:36 +00:00
|
|
|
'field' => 'upload.txt',
|
2019-01-16 09:39:14 +00:00
|
|
|
],
|
|
|
|
'type' => [
|
2012-12-30 15:38:36 +00:00
|
|
|
'field' => 'text/plain',
|
2019-01-16 09:39:14 +00:00
|
|
|
],
|
|
|
|
'tmp_name' => [
|
2012-12-30 15:38:36 +00:00
|
|
|
'field' => 'owfdskjasdfsa',
|
2019-01-16 09:39:14 +00:00
|
|
|
],
|
|
|
|
'error' => [
|
2012-12-30 15:38:36 +00:00
|
|
|
'field' => UPLOAD_ERR_OK,
|
2019-01-16 09:39:14 +00:00
|
|
|
],
|
|
|
|
'size' => [
|
2012-12-30 15:38:36 +00:00
|
|
|
'field' => 100,
|
2019-01-16 09:39:14 +00:00
|
|
|
],
|
|
|
|
]]);
|
2012-12-30 15:38:36 +00:00
|
|
|
|
2013-04-20 14:56:42 +01:00
|
|
|
$this->requestHandler->handleRequest($form, $this->request);
|
2019-01-18 17:22:25 +00:00
|
|
|
|
|
|
|
$this->assertTrue($form->isSubmitted());
|
|
|
|
$this->assertEquals([
|
|
|
|
'name' => 'upload.txt',
|
|
|
|
'type' => 'text/plain',
|
|
|
|
'tmp_name' => 'owfdskjasdfsa',
|
|
|
|
'error' => UPLOAD_ERR_OK,
|
|
|
|
'size' => 100,
|
|
|
|
], $fieldForm->getData());
|
2012-12-30 15:38:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testFixBuggyNestedFilesArray()
|
|
|
|
{
|
2019-01-18 17:22:25 +00:00
|
|
|
$form = $this->createForm('param1', 'POST', true);
|
|
|
|
$fieldForm = $this->createForm('field', null, true);
|
|
|
|
$form->add($fieldForm);
|
|
|
|
$subfieldForm = $this->createBuilder('subfield', false, ['allow_file_upload' => true])->getForm();
|
|
|
|
$fieldForm->add($subfieldForm);
|
2012-12-30 15:38:36 +00:00
|
|
|
|
2019-01-16 09:39:14 +00:00
|
|
|
$this->setRequestData('POST', [], ['param1' => [
|
|
|
|
'name' => [
|
|
|
|
'field' => ['subfield' => 'upload.txt'],
|
|
|
|
],
|
|
|
|
'type' => [
|
|
|
|
'field' => ['subfield' => 'text/plain'],
|
|
|
|
],
|
|
|
|
'tmp_name' => [
|
|
|
|
'field' => ['subfield' => 'owfdskjasdfsa'],
|
|
|
|
],
|
|
|
|
'error' => [
|
|
|
|
'field' => ['subfield' => UPLOAD_ERR_OK],
|
|
|
|
],
|
|
|
|
'size' => [
|
|
|
|
'field' => ['subfield' => 100],
|
|
|
|
],
|
|
|
|
]]);
|
2012-12-30 15:38:36 +00:00
|
|
|
|
2013-04-20 14:56:42 +01:00
|
|
|
$this->requestHandler->handleRequest($form, $this->request);
|
2019-01-18 17:22:25 +00:00
|
|
|
|
|
|
|
$this->assertTrue($subfieldForm->isSubmitted());
|
|
|
|
$this->assertEquals([
|
|
|
|
'name' => 'upload.txt',
|
|
|
|
'type' => 'text/plain',
|
|
|
|
'tmp_name' => 'owfdskjasdfsa',
|
|
|
|
'error' => UPLOAD_ERR_OK,
|
|
|
|
'size' => 100,
|
|
|
|
], $subfieldForm->getData());
|
2012-12-30 15:38:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testMethodOverrideHeaderIgnoredIfNotPost()
|
|
|
|
{
|
2019-01-18 17:22:25 +00:00
|
|
|
$form = $this->createForm('param1', 'POST');
|
2012-12-30 15:38:36 +00:00
|
|
|
|
2019-01-16 09:39:14 +00:00
|
|
|
$this->setRequestData('GET', [
|
2012-12-30 15:38:36 +00:00
|
|
|
'param1' => 'DATA',
|
2019-01-16 09:39:14 +00:00
|
|
|
]);
|
2012-12-30 15:38:36 +00:00
|
|
|
|
|
|
|
$_SERVER['HTTP_X_HTTP_METHOD_OVERRIDE'] = 'PUT';
|
|
|
|
|
2013-04-20 14:56:42 +01:00
|
|
|
$this->requestHandler->handleRequest($form, $this->request);
|
2019-01-18 17:22:25 +00:00
|
|
|
|
|
|
|
$this->assertFalse($form->isSubmitted());
|
2012-12-30 15:38:36 +00:00
|
|
|
}
|
|
|
|
|
2019-02-04 18:26:43 +00:00
|
|
|
public function testFormIgnoresMethodFieldIfRequestMethodIsMatched()
|
|
|
|
{
|
|
|
|
$form = $this->createForm('foo', 'PUT', true);
|
|
|
|
$form->add($this->createForm('bar'));
|
|
|
|
|
|
|
|
$this->setRequestData('PUT', [
|
|
|
|
'foo' => [
|
|
|
|
'_method' => 'PUT',
|
|
|
|
'bar' => 'baz',
|
|
|
|
],
|
|
|
|
]);
|
|
|
|
|
|
|
|
$this->requestHandler->handleRequest($form, $this->request);
|
|
|
|
|
|
|
|
$this->assertSame([], $form->getExtraData());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testFormDoesNotIgnoreMethodFieldIfRequestMethodIsNotMatched()
|
|
|
|
{
|
|
|
|
$form = $this->createForm('foo', 'PUT', true);
|
|
|
|
$form->add($this->createForm('bar'));
|
|
|
|
|
|
|
|
$this->setRequestData('PUT', [
|
|
|
|
'foo' => [
|
|
|
|
'_method' => 'DELETE',
|
|
|
|
'bar' => 'baz',
|
|
|
|
],
|
|
|
|
]);
|
|
|
|
|
|
|
|
$this->requestHandler->handleRequest($form, $this->request);
|
|
|
|
|
|
|
|
$this->assertSame(['_method' => 'DELETE'], $form->getExtraData());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testMethodSubFormIsSubmitted()
|
|
|
|
{
|
|
|
|
$form = $this->createForm('foo', 'PUT', true);
|
|
|
|
$form->add($this->createForm('_method'));
|
|
|
|
$form->add($this->createForm('bar'));
|
|
|
|
|
|
|
|
$this->setRequestData('PUT', [
|
|
|
|
'foo' => [
|
|
|
|
'_method' => 'PUT',
|
|
|
|
'bar' => 'baz',
|
|
|
|
],
|
|
|
|
]);
|
|
|
|
|
|
|
|
$this->requestHandler->handleRequest($form, $this->request);
|
|
|
|
|
|
|
|
$this->assertTrue($form->get('_method')->isSubmitted());
|
|
|
|
$this->assertSame('PUT', $form->get('_method')->getData());
|
|
|
|
}
|
|
|
|
|
2019-01-16 09:39:14 +00:00
|
|
|
protected function setRequestData($method, $data, $files = [])
|
2012-12-30 15:38:36 +00:00
|
|
|
{
|
|
|
|
if ('GET' === $method) {
|
|
|
|
$_GET = $data;
|
2019-01-16 09:39:14 +00:00
|
|
|
$_FILES = [];
|
2012-12-30 15:38:36 +00:00
|
|
|
} else {
|
|
|
|
$_POST = $data;
|
|
|
|
$_FILES = $files;
|
|
|
|
}
|
|
|
|
|
2019-01-16 09:39:14 +00:00
|
|
|
$_SERVER = [
|
2012-12-30 15:38:36 +00:00
|
|
|
'REQUEST_METHOD' => $method,
|
|
|
|
// PHPUnit needs this entry
|
|
|
|
'SCRIPT_NAME' => self::$serverBackup['SCRIPT_NAME'],
|
2019-01-16 09:39:14 +00:00
|
|
|
];
|
2012-12-30 15:38:36 +00:00
|
|
|
}
|
|
|
|
|
2013-04-20 14:56:42 +01:00
|
|
|
protected function getRequestHandler()
|
2012-12-30 15:38:36 +00:00
|
|
|
{
|
2014-09-15 15:48:43 +01:00
|
|
|
return new NativeRequestHandler($this->serverParams);
|
2012-12-30 15:38:36 +00:00
|
|
|
}
|
|
|
|
|
2019-02-06 20:50:10 +00:00
|
|
|
protected function getUploadedFile($suffix = '')
|
2012-12-30 15:38:36 +00:00
|
|
|
{
|
2019-01-16 09:39:14 +00:00
|
|
|
return [
|
2015-02-23 22:10:57 +00:00
|
|
|
'name' => 'upload'.$suffix.'.txt',
|
2012-12-30 15:38:36 +00:00
|
|
|
'type' => 'text/plain',
|
2015-02-23 22:10:57 +00:00
|
|
|
'tmp_name' => 'owfdskjasdfsa'.$suffix,
|
2012-12-30 15:38:36 +00:00
|
|
|
'error' => UPLOAD_ERR_OK,
|
|
|
|
'size' => 100,
|
2019-01-16 09:39:14 +00:00
|
|
|
];
|
2012-12-30 15:38:36 +00:00
|
|
|
}
|
2017-09-01 10:39:51 +01:00
|
|
|
|
|
|
|
protected function getInvalidFile()
|
|
|
|
{
|
2019-01-16 09:39:14 +00:00
|
|
|
return [
|
2017-09-01 10:39:51 +01:00
|
|
|
'name' => 'upload.txt',
|
|
|
|
'type' => 'text/plain',
|
|
|
|
'tmp_name' => 'owfdskjasdfsa',
|
|
|
|
'error' => '0',
|
|
|
|
'size' => '100',
|
2019-01-16 09:39:14 +00:00
|
|
|
];
|
2017-09-01 10:39:51 +01:00
|
|
|
}
|
2019-04-06 13:28:07 +01:00
|
|
|
|
|
|
|
protected function getFailedUploadedFile($errorCode)
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
'name' => 'upload.txt',
|
|
|
|
'type' => 'text/plain',
|
|
|
|
'tmp_name' => 'owfdskjasdfsa',
|
|
|
|
'error' => $errorCode,
|
|
|
|
'size' => 100,
|
|
|
|
];
|
|
|
|
}
|
2012-12-30 15:38:36 +00:00
|
|
|
}
|