NativeRequestHandlerTest.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Form\Tests;
  11. use Symfony\Component\Form\NativeRequestHandler;
  12. /**
  13. * @author Bernhard Schussek <bschussek@gmail.com>
  14. */
  15. class NativeRequestHandlerTest extends AbstractRequestHandlerTest
  16. {
  17. private static $serverBackup;
  18. public static function setUpBeforeClass()
  19. {
  20. self::$serverBackup = $_SERVER;
  21. }
  22. protected function setUp()
  23. {
  24. parent::setUp();
  25. $_GET = array();
  26. $_POST = array();
  27. $_FILES = array();
  28. $_SERVER = array(
  29. // PHPUnit needs this entry
  30. 'SCRIPT_NAME' => self::$serverBackup['SCRIPT_NAME'],
  31. );
  32. }
  33. protected function tearDown()
  34. {
  35. parent::tearDown();
  36. $_GET = array();
  37. $_POST = array();
  38. $_FILES = array();
  39. $_SERVER = self::$serverBackup;
  40. }
  41. /**
  42. * @expectedException \Symfony\Component\Form\Exception\UnexpectedTypeException
  43. */
  44. public function testRequestShouldBeNull()
  45. {
  46. $this->requestHandler->handleRequest($this->getMockForm('name', 'GET'), 'request');
  47. }
  48. public function testMethodOverrideHeaderTakesPrecedenceIfPost()
  49. {
  50. $form = $this->getMockForm('param1', 'PUT');
  51. $this->setRequestData('POST', array(
  52. 'param1' => 'DATA',
  53. ));
  54. $_SERVER['HTTP_X_HTTP_METHOD_OVERRIDE'] = 'PUT';
  55. $form->expects($this->once())
  56. ->method('submit')
  57. ->with('DATA');
  58. $this->requestHandler->handleRequest($form, $this->request);
  59. }
  60. public function testConvertEmptyUploadedFilesToNull()
  61. {
  62. $form = $this->getMockForm('param1', 'POST', false);
  63. $this->setRequestData('POST', array(), array('param1' => array(
  64. 'name' => '',
  65. 'type' => '',
  66. 'tmp_name' => '',
  67. 'error' => UPLOAD_ERR_NO_FILE,
  68. 'size' => 0,
  69. )));
  70. $form->expects($this->once())
  71. ->method('submit')
  72. ->with($this->identicalTo(null));
  73. $this->requestHandler->handleRequest($form, $this->request);
  74. }
  75. public function testFixBuggyFilesArray()
  76. {
  77. $form = $this->getMockForm('param1', 'POST', false);
  78. $this->setRequestData('POST', array(), array('param1' => array(
  79. 'name' => array(
  80. 'field' => 'upload.txt',
  81. ),
  82. 'type' => array(
  83. 'field' => 'text/plain',
  84. ),
  85. 'tmp_name' => array(
  86. 'field' => 'owfdskjasdfsa',
  87. ),
  88. 'error' => array(
  89. 'field' => UPLOAD_ERR_OK,
  90. ),
  91. 'size' => array(
  92. 'field' => 100,
  93. ),
  94. )));
  95. $form->expects($this->once())
  96. ->method('submit')
  97. ->with(array(
  98. 'field' => array(
  99. 'name' => 'upload.txt',
  100. 'type' => 'text/plain',
  101. 'tmp_name' => 'owfdskjasdfsa',
  102. 'error' => UPLOAD_ERR_OK,
  103. 'size' => 100,
  104. ),
  105. ));
  106. $this->requestHandler->handleRequest($form, $this->request);
  107. }
  108. public function testFixBuggyNestedFilesArray()
  109. {
  110. $form = $this->getMockForm('param1', 'POST');
  111. $this->setRequestData('POST', array(), array('param1' => array(
  112. 'name' => array(
  113. 'field' => array('subfield' => 'upload.txt'),
  114. ),
  115. 'type' => array(
  116. 'field' => array('subfield' => 'text/plain'),
  117. ),
  118. 'tmp_name' => array(
  119. 'field' => array('subfield' => 'owfdskjasdfsa'),
  120. ),
  121. 'error' => array(
  122. 'field' => array('subfield' => UPLOAD_ERR_OK),
  123. ),
  124. 'size' => array(
  125. 'field' => array('subfield' => 100),
  126. ),
  127. )));
  128. $form->expects($this->once())
  129. ->method('submit')
  130. ->with(array(
  131. 'field' => array(
  132. 'subfield' => array(
  133. 'name' => 'upload.txt',
  134. 'type' => 'text/plain',
  135. 'tmp_name' => 'owfdskjasdfsa',
  136. 'error' => UPLOAD_ERR_OK,
  137. 'size' => 100,
  138. ),
  139. ),
  140. ));
  141. $this->requestHandler->handleRequest($form, $this->request);
  142. }
  143. public function testMethodOverrideHeaderIgnoredIfNotPost()
  144. {
  145. $form = $this->getMockForm('param1', 'POST');
  146. $this->setRequestData('GET', array(
  147. 'param1' => 'DATA',
  148. ));
  149. $_SERVER['HTTP_X_HTTP_METHOD_OVERRIDE'] = 'PUT';
  150. $form->expects($this->never())
  151. ->method('submit');
  152. $this->requestHandler->handleRequest($form, $this->request);
  153. }
  154. protected function setRequestData($method, $data, $files = array())
  155. {
  156. if ('GET' === $method) {
  157. $_GET = $data;
  158. $_FILES = array();
  159. } else {
  160. $_POST = $data;
  161. $_FILES = $files;
  162. }
  163. $_SERVER = array(
  164. 'REQUEST_METHOD' => $method,
  165. // PHPUnit needs this entry
  166. 'SCRIPT_NAME' => self::$serverBackup['SCRIPT_NAME'],
  167. );
  168. }
  169. protected function getRequestHandler()
  170. {
  171. return new NativeRequestHandler($this->serverParams);
  172. }
  173. protected function getMockFile($suffix = '')
  174. {
  175. return array(
  176. 'name' => 'upload'.$suffix.'.txt',
  177. 'type' => 'text/plain',
  178. 'tmp_name' => 'owfdskjasdfsa'.$suffix,
  179. 'error' => UPLOAD_ERR_OK,
  180. 'size' => 100,
  181. );
  182. }
  183. protected function getInvalidFile()
  184. {
  185. return array(
  186. 'name' => 'upload.txt',
  187. 'type' => 'text/plain',
  188. 'tmp_name' => 'owfdskjasdfsa',
  189. 'error' => '0',
  190. 'size' => '100',
  191. );
  192. }
  193. }