This repository has been archived on 2023-08-20. You can view files and clone it, but cannot push or open issues or pull requests.
symfony/src/Symfony/Component/Form/Tests/NativeRequestHandlerTest.php

231 lines
5.9 KiB
PHP
Raw Normal View History

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;
use Symfony\Component\Form\NativeRequestHandler;
2012-12-30 15:38:36 +00:00
/**
* @author Bernhard Schussek <bschussek@gmail.com>
*/
class NativeRequestHandlerTest extends AbstractRequestHandlerTest
2012-12-30 15:38:36 +00:00
{
private static $serverBackup;
public static function setUpBeforeClass()
{
self::$serverBackup = $_SERVER;
}
protected function setUp()
{
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
}
protected function tearDown()
{
parent::tearDown();
2019-01-16 09:39:14 +00:00
$_GET = [];
$_POST = [];
$_FILES = [];
2012-12-30 15:38:36 +00:00
$_SERVER = self::$serverBackup;
}
/**
* @expectedException \Symfony\Component\Form\Exception\UnexpectedTypeException
*/
public function testRequestShouldBeNull()
{
$this->requestHandler->handleRequest($this->getMockForm('name', 'GET'), 'request');
2012-12-30 15:38:36 +00:00
}
public function testMethodOverrideHeaderTakesPrecedenceIfPost()
{
$form = $this->getMockForm('param1', 'PUT');
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';
$form->expects($this->once())
->method('submit')
2012-12-30 15:38:36 +00:00
->with('DATA');
$this->requestHandler->handleRequest($form, $this->request);
2012-12-30 15:38:36 +00:00
}
public function testConvertEmptyUploadedFilesToNull()
{
$form = $this->getMockForm('param1', 'POST', false);
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
$form->expects($this->once())
->method('submit')
2012-12-30 15:38:36 +00:00
->with($this->identicalTo(null));
$this->requestHandler->handleRequest($form, $this->request);
2012-12-30 15:38:36 +00:00
}
public function testFixBuggyFilesArray()
{
$form = $this->getMockForm('param1', 'POST', false);
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
$form->expects($this->once())
->method('submit')
2019-01-16 09:39:14 +00:00
->with([
'field' => [
2012-12-30 15:38:36 +00:00
'name' => 'upload.txt',
'type' => 'text/plain',
'tmp_name' => 'owfdskjasdfsa',
'error' => UPLOAD_ERR_OK,
'size' => 100,
2019-01-16 09:39:14 +00:00
],
]);
2012-12-30 15:38:36 +00:00
$this->requestHandler->handleRequest($form, $this->request);
2012-12-30 15:38:36 +00:00
}
public function testFixBuggyNestedFilesArray()
{
$form = $this->getMockForm('param1', 'POST');
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
$form->expects($this->once())
->method('submit')
2019-01-16 09:39:14 +00:00
->with([
'field' => [
'subfield' => [
2012-12-30 15:38:36 +00:00
'name' => 'upload.txt',
'type' => 'text/plain',
'tmp_name' => 'owfdskjasdfsa',
'error' => UPLOAD_ERR_OK,
'size' => 100,
2019-01-16 09:39:14 +00:00
],
],
]);
2012-12-30 15:38:36 +00:00
$this->requestHandler->handleRequest($form, $this->request);
2012-12-30 15:38:36 +00:00
}
public function testMethodOverrideHeaderIgnoredIfNotPost()
{
$form = $this->getMockForm('param1', 'POST');
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';
$form->expects($this->never())
->method('submit');
2012-12-30 15:38:36 +00:00
$this->requestHandler->handleRequest($form, $this->request);
2012-12-30 15:38:36 +00:00
}
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
}
protected function getRequestHandler()
2012-12-30 15:38:36 +00:00
{
return new NativeRequestHandler($this->serverParams);
2012-12-30 15:38:36 +00:00
}
protected function getMockFile($suffix = '')
2012-12-30 15:38:36 +00:00
{
2019-01-16 09:39:14 +00:00
return [
'name' => 'upload'.$suffix.'.txt',
2012-12-30 15:38:36 +00:00
'type' => 'text/plain',
'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
}
protected function getInvalidFile()
{
2019-01-16 09:39:14 +00:00
return [
'name' => 'upload.txt',
'type' => 'text/plain',
'tmp_name' => 'owfdskjasdfsa',
'error' => '0',
'size' => '100',
2019-01-16 09:39:14 +00:00
];
}
2012-12-30 15:38:36 +00:00
}