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;
|
|
|
|
|
2014-09-15 15:48:43 +01:00
|
|
|
use Symfony\Component\Form\FormError;
|
|
|
|
use Symfony\Component\Form\FormFactory;
|
2014-09-08 09:53:21 +01:00
|
|
|
use Symfony\Component\Form\Forms;
|
2014-09-15 15:48:43 +01:00
|
|
|
use Symfony\Component\Form\RequestHandlerInterface;
|
2014-09-08 09:53:21 +01:00
|
|
|
|
2012-12-30 15:38:36 +00:00
|
|
|
/**
|
|
|
|
* @author Bernhard Schussek <bschussek@gmail.com>
|
|
|
|
*/
|
2013-04-20 14:56:42 +01:00
|
|
|
abstract class AbstractRequestHandlerTest extends \PHPUnit_Framework_TestCase
|
2012-12-30 15:38:36 +00:00
|
|
|
{
|
|
|
|
/**
|
2014-09-15 15:48:43 +01:00
|
|
|
* @var RequestHandlerInterface
|
2012-12-30 15:38:36 +00:00
|
|
|
*/
|
2013-04-20 14:56:42 +01:00
|
|
|
protected $requestHandler;
|
2012-12-30 15:38:36 +00:00
|
|
|
|
2014-09-08 09:53:21 +01:00
|
|
|
/**
|
2014-09-15 15:48:43 +01:00
|
|
|
* @var FormFactory
|
2014-09-08 09:53:21 +01:00
|
|
|
*/
|
|
|
|
protected $factory;
|
|
|
|
|
2012-12-30 15:38:36 +00:00
|
|
|
protected $request;
|
|
|
|
|
2014-09-15 15:48:43 +01:00
|
|
|
protected $serverParams;
|
|
|
|
|
2012-12-30 15:38:36 +00:00
|
|
|
protected function setUp()
|
|
|
|
{
|
2014-09-15 15:48:43 +01:00
|
|
|
$this->serverParams = $this->getMock(
|
|
|
|
'Symfony\Component\Form\Util\ServerParams',
|
|
|
|
array('getNormalizedIniPostMaxSize', 'getContentLength')
|
|
|
|
);
|
2013-04-20 14:56:42 +01:00
|
|
|
$this->requestHandler = $this->getRequestHandler();
|
2014-09-08 09:53:21 +01:00
|
|
|
$this->factory = Forms::createFormFactoryBuilder()->getFormFactory();
|
2012-12-30 15:38:36 +00:00
|
|
|
$this->request = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function methodExceptGetProvider()
|
|
|
|
{
|
|
|
|
return array(
|
|
|
|
array('POST'),
|
|
|
|
array('PUT'),
|
|
|
|
array('DELETE'),
|
|
|
|
array('PATCH'),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function methodProvider()
|
|
|
|
{
|
|
|
|
return array_merge(array(
|
|
|
|
array('GET'),
|
|
|
|
), $this->methodExceptGetProvider());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider methodProvider
|
|
|
|
*/
|
2013-04-20 16:32:55 +01:00
|
|
|
public function testSubmitIfNameInRequest($method)
|
2012-12-30 15:38:36 +00:00
|
|
|
{
|
|
|
|
$form = $this->getMockForm('param1', $method);
|
|
|
|
|
|
|
|
$this->setRequestData($method, array(
|
|
|
|
'param1' => 'DATA',
|
|
|
|
));
|
|
|
|
|
|
|
|
$form->expects($this->once())
|
2013-04-25 15:08:09 +01:00
|
|
|
->method('submit')
|
|
|
|
->with('DATA', 'PATCH' !== $method);
|
2012-12-30 15:38:36 +00:00
|
|
|
|
2013-04-20 14:56:42 +01:00
|
|
|
$this->requestHandler->handleRequest($form, $this->request);
|
2012-12-30 15:38:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider methodProvider
|
|
|
|
*/
|
2013-04-20 16:32:55 +01:00
|
|
|
public function testDoNotSubmitIfWrongRequestMethod($method)
|
2012-12-30 15:38:36 +00:00
|
|
|
{
|
|
|
|
$form = $this->getMockForm('param1', $method);
|
|
|
|
|
|
|
|
$otherMethod = 'POST' === $method ? 'PUT' : 'POST';
|
|
|
|
|
|
|
|
$this->setRequestData($otherMethod, array(
|
|
|
|
'param1' => 'DATA',
|
|
|
|
));
|
|
|
|
|
|
|
|
$form->expects($this->never())
|
2013-04-25 15:08:09 +01:00
|
|
|
->method('submit');
|
2012-12-30 15:38:36 +00:00
|
|
|
|
2013-04-20 14:56:42 +01:00
|
|
|
$this->requestHandler->handleRequest($form, $this->request);
|
2012-12-30 15:38:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider methodExceptGetProvider
|
|
|
|
*/
|
2013-08-01 16:20:43 +01:00
|
|
|
public function testDoNoSubmitSimpleFormIfNameNotInRequestAndNotGetRequest($method)
|
2012-12-30 15:38:36 +00:00
|
|
|
{
|
|
|
|
$form = $this->getMockForm('param1', $method, false);
|
|
|
|
|
|
|
|
$this->setRequestData($method, array(
|
|
|
|
'paramx' => array(),
|
|
|
|
));
|
|
|
|
|
2013-08-01 16:20:43 +01:00
|
|
|
$form->expects($this->never())
|
|
|
|
->method('submit');
|
2012-12-30 15:38:36 +00:00
|
|
|
|
2013-04-20 14:56:42 +01:00
|
|
|
$this->requestHandler->handleRequest($form, $this->request);
|
2012-12-30 15:38:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider methodExceptGetProvider
|
|
|
|
*/
|
2013-08-01 16:20:43 +01:00
|
|
|
public function testDoNotSubmitCompoundFormIfNameNotInRequestAndNotGetRequest($method)
|
2012-12-30 15:38:36 +00:00
|
|
|
{
|
|
|
|
$form = $this->getMockForm('param1', $method, true);
|
|
|
|
|
|
|
|
$this->setRequestData($method, array(
|
|
|
|
'paramx' => array(),
|
|
|
|
));
|
|
|
|
|
2013-08-01 16:20:43 +01:00
|
|
|
$form->expects($this->never())
|
|
|
|
->method('submit');
|
2012-12-30 15:38:36 +00:00
|
|
|
|
2013-04-20 14:56:42 +01:00
|
|
|
$this->requestHandler->handleRequest($form, $this->request);
|
2012-12-30 15:38:36 +00:00
|
|
|
}
|
|
|
|
|
2013-04-20 16:32:55 +01:00
|
|
|
public function testDoNotSubmitIfNameNotInRequestAndGetRequest()
|
2012-12-30 15:38:36 +00:00
|
|
|
{
|
|
|
|
$form = $this->getMockForm('param1', 'GET');
|
|
|
|
|
|
|
|
$this->setRequestData('GET', array(
|
|
|
|
'paramx' => array(),
|
|
|
|
));
|
|
|
|
|
|
|
|
$form->expects($this->never())
|
2013-04-25 15:08:09 +01:00
|
|
|
->method('submit');
|
2012-12-30 15:38:36 +00:00
|
|
|
|
2013-04-20 14:56:42 +01:00
|
|
|
$this->requestHandler->handleRequest($form, $this->request);
|
2012-12-30 15:38:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider methodProvider
|
|
|
|
*/
|
2013-04-20 16:32:55 +01:00
|
|
|
public function testSubmitFormWithEmptyNameIfAtLeastOneFieldInRequest($method)
|
2012-12-30 15:38:36 +00:00
|
|
|
{
|
|
|
|
$form = $this->getMockForm('', $method);
|
|
|
|
$form->expects($this->any())
|
|
|
|
->method('all')
|
|
|
|
->will($this->returnValue(array(
|
|
|
|
'param1' => $this->getMockForm('param1'),
|
|
|
|
'param2' => $this->getMockForm('param2'),
|
|
|
|
)));
|
|
|
|
|
|
|
|
$this->setRequestData($method, $requestData = array(
|
|
|
|
'param1' => 'submitted value',
|
|
|
|
'paramx' => 'submitted value',
|
|
|
|
));
|
|
|
|
|
|
|
|
$form->expects($this->once())
|
2013-04-25 15:08:09 +01:00
|
|
|
->method('submit')
|
|
|
|
->with($requestData, 'PATCH' !== $method);
|
2012-12-30 15:38:36 +00:00
|
|
|
|
2013-04-20 14:56:42 +01:00
|
|
|
$this->requestHandler->handleRequest($form, $this->request);
|
2012-12-30 15:38:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider methodProvider
|
|
|
|
*/
|
2013-04-20 16:32:55 +01:00
|
|
|
public function testDoNotSubmitFormWithEmptyNameIfNoFieldInRequest($method)
|
2012-12-30 15:38:36 +00:00
|
|
|
{
|
|
|
|
$form = $this->getMockForm('', $method);
|
|
|
|
$form->expects($this->any())
|
|
|
|
->method('all')
|
|
|
|
->will($this->returnValue(array(
|
|
|
|
'param1' => $this->getMockForm('param1'),
|
|
|
|
'param2' => $this->getMockForm('param2'),
|
|
|
|
)));
|
|
|
|
|
|
|
|
$this->setRequestData($method, array(
|
|
|
|
'paramx' => 'submitted value',
|
|
|
|
));
|
|
|
|
|
|
|
|
$form->expects($this->never())
|
2013-04-25 15:08:09 +01:00
|
|
|
->method('submit');
|
2012-12-30 15:38:36 +00:00
|
|
|
|
2013-04-20 14:56:42 +01:00
|
|
|
$this->requestHandler->handleRequest($form, $this->request);
|
2012-12-30 15:38:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider methodExceptGetProvider
|
|
|
|
*/
|
|
|
|
public function testMergeParamsAndFiles($method)
|
|
|
|
{
|
|
|
|
$form = $this->getMockForm('param1', $method);
|
|
|
|
$file = $this->getMockFile();
|
|
|
|
|
|
|
|
$this->setRequestData($method, array(
|
|
|
|
'param1' => array(
|
|
|
|
'field1' => 'DATA',
|
|
|
|
),
|
|
|
|
), array(
|
|
|
|
'param1' => array(
|
|
|
|
'field2' => $file,
|
|
|
|
),
|
|
|
|
));
|
|
|
|
|
|
|
|
$form->expects($this->once())
|
2013-04-25 15:08:09 +01:00
|
|
|
->method('submit')
|
2012-12-30 15:38:36 +00:00
|
|
|
->with(array(
|
|
|
|
'field1' => 'DATA',
|
|
|
|
'field2' => $file,
|
2013-04-25 15:08:09 +01:00
|
|
|
), 'PATCH' !== $method);
|
2012-12-30 15:38:36 +00:00
|
|
|
|
2013-04-20 14:56:42 +01:00
|
|
|
$this->requestHandler->handleRequest($form, $this->request);
|
2012-12-30 15:38:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider methodExceptGetProvider
|
|
|
|
*/
|
|
|
|
public function testParamTakesPrecedenceOverFile($method)
|
|
|
|
{
|
|
|
|
$form = $this->getMockForm('param1', $method);
|
|
|
|
$file = $this->getMockFile();
|
|
|
|
|
|
|
|
$this->setRequestData($method, array(
|
|
|
|
'param1' => 'DATA',
|
|
|
|
), array(
|
|
|
|
'param1' => $file,
|
|
|
|
));
|
|
|
|
|
|
|
|
$form->expects($this->once())
|
2013-04-25 15:08:09 +01:00
|
|
|
->method('submit')
|
|
|
|
->with('DATA', 'PATCH' !== $method);
|
2012-12-30 15:38:36 +00:00
|
|
|
|
2013-04-20 14:56:42 +01:00
|
|
|
$this->requestHandler->handleRequest($form, $this->request);
|
2012-12-30 15:38:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider methodExceptGetProvider
|
|
|
|
*/
|
2013-04-20 16:32:55 +01:00
|
|
|
public function testSubmitFileIfNoParam($method)
|
2012-12-30 15:38:36 +00:00
|
|
|
{
|
|
|
|
$form = $this->getMockForm('param1', $method);
|
|
|
|
$file = $this->getMockFile();
|
|
|
|
|
|
|
|
$this->setRequestData($method, array(
|
|
|
|
'param1' => null,
|
|
|
|
), array(
|
|
|
|
'param1' => $file,
|
|
|
|
));
|
|
|
|
|
|
|
|
$form->expects($this->once())
|
2013-04-25 15:08:09 +01:00
|
|
|
->method('submit')
|
|
|
|
->with($file, 'PATCH' !== $method);
|
2012-12-30 15:38:36 +00:00
|
|
|
|
2013-04-20 14:56:42 +01:00
|
|
|
$this->requestHandler->handleRequest($form, $this->request);
|
2012-12-30 15:38:36 +00:00
|
|
|
}
|
|
|
|
|
2015-02-23 22:10:57 +00:00
|
|
|
/**
|
|
|
|
* @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);
|
|
|
|
}
|
|
|
|
|
2014-09-15 15:48:43 +01:00
|
|
|
/**
|
|
|
|
* @dataProvider getPostMaxSizeFixtures
|
|
|
|
*/
|
|
|
|
public function testAddFormErrorIfPostMaxSizeExceeded($contentLength, $iniMax, $shouldFail, array $errorParams = array())
|
2014-09-08 09:53:21 +01:00
|
|
|
{
|
2014-09-15 15:48:43 +01:00
|
|
|
$this->serverParams->expects($this->once())
|
|
|
|
->method('getContentLength')
|
|
|
|
->will($this->returnValue($contentLength));
|
|
|
|
$this->serverParams->expects($this->any())
|
|
|
|
->method('getNormalizedIniPostMaxSize')
|
|
|
|
->will($this->returnValue($iniMax));
|
|
|
|
|
|
|
|
$options = array('post_max_size_message' => 'Max {{ max }}!');
|
2015-06-23 17:42:21 +01:00
|
|
|
$form = $this->factory->createNamed('name', 'Symfony\Component\Form\Extension\Core\Type\TextType', null, $options);
|
2014-09-08 09:53:21 +01:00
|
|
|
$this->setRequestData('POST', array(), array());
|
|
|
|
|
|
|
|
$this->requestHandler->handleRequest($form, $this->request);
|
|
|
|
|
2014-09-15 15:48:43 +01:00
|
|
|
if ($shouldFail) {
|
2015-05-03 15:08:06 +01:00
|
|
|
$error = new FormError($options['post_max_size_message'], null, $errorParams);
|
|
|
|
$error->setOrigin($form);
|
2014-09-08 09:53:21 +01:00
|
|
|
|
2015-05-03 15:08:06 +01:00
|
|
|
$this->assertEquals(array($error), iterator_to_array($form->getErrors()));
|
2014-09-15 15:48:43 +01:00
|
|
|
$this->assertTrue($form->isSubmitted());
|
|
|
|
} else {
|
|
|
|
$this->assertCount(0, $form->getErrors());
|
|
|
|
$this->assertFalse($form->isSubmitted());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getPostMaxSizeFixtures()
|
|
|
|
{
|
|
|
|
return array(
|
|
|
|
array(pow(1024, 3) + 1, '1G', true, array('{{ max }}' => '1G')),
|
|
|
|
array(pow(1024, 3), '1G', false),
|
|
|
|
array(pow(1024, 2) + 1, '1M', true, array('{{ max }}' => '1M')),
|
|
|
|
array(pow(1024, 2), '1M', false),
|
|
|
|
array(1024 + 1, '1K', true, array('{{ max }}' => '1K')),
|
|
|
|
array(1024, '1K', false),
|
|
|
|
array(null, '1K', false),
|
|
|
|
array(1024, '', false),
|
|
|
|
array(1024, 0, false),
|
|
|
|
);
|
2014-09-08 09:53:21 +01:00
|
|
|
}
|
|
|
|
|
2012-12-30 15:38:36 +00:00
|
|
|
abstract protected function setRequestData($method, $data, $files = array());
|
|
|
|
|
2013-04-20 14:56:42 +01:00
|
|
|
abstract protected function getRequestHandler();
|
2012-12-30 15:38:36 +00:00
|
|
|
|
2015-02-23 22:10:57 +00:00
|
|
|
abstract protected function getMockFile($suffix = '');
|
2012-12-30 15:38:36 +00:00
|
|
|
|
|
|
|
protected function getMockForm($name, $method = null, $compound = true)
|
|
|
|
{
|
|
|
|
$config = $this->getMock('Symfony\Component\Form\FormConfigInterface');
|
|
|
|
$config->expects($this->any())
|
|
|
|
->method('getMethod')
|
|
|
|
->will($this->returnValue($method));
|
|
|
|
$config->expects($this->any())
|
|
|
|
->method('getCompound')
|
|
|
|
->will($this->returnValue($compound));
|
|
|
|
|
|
|
|
$form = $this->getMock('Symfony\Component\Form\Test\FormInterface');
|
|
|
|
$form->expects($this->any())
|
|
|
|
->method('getName')
|
|
|
|
->will($this->returnValue($name));
|
|
|
|
$form->expects($this->any())
|
|
|
|
->method('getConfig')
|
|
|
|
->will($this->returnValue($config));
|
|
|
|
|
|
|
|
return $form;
|
|
|
|
}
|
|
|
|
}
|