FormTest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. <?php
  2. namespace Symfony\Tests\Components\Form;
  3. require_once __DIR__ . '/../../../../bootstrap.php';
  4. require_once __DIR__ . '/Fixtures/Author.php';
  5. require_once __DIR__ . '/Fixtures/TestField.php';
  6. use Symfony\Components\Form\Form;
  7. use Symfony\Components\Form\Field;
  8. use Symfony\Components\Form\HiddenField;
  9. use Symfony\Components\Form\FieldGroup;
  10. use Symfony\Components\Form\HtmlGenerator;
  11. use Symfony\Components\Form\PropertyPath;
  12. use Symfony\Components\File\UploadedFile;
  13. use Symfony\Components\Validator\ConstraintViolation;
  14. use Symfony\Components\Validator\ConstraintViolationList;
  15. use Symfony\Tests\Components\Form\Fixtures\Author;
  16. use Symfony\Tests\Components\Form\Fixtures\TestField;
  17. class FormTest_PreconfiguredForm extends Form
  18. {
  19. protected function configure()
  20. {
  21. $this->add(new Field('firstName'));
  22. }
  23. }
  24. class FormTest extends \PHPUnit_Framework_TestCase
  25. {
  26. protected $validator;
  27. protected $form;
  28. protected function setUp()
  29. {
  30. Form::disableDefaultCsrfProtection();
  31. Form::setDefaultCsrfSecret(null);
  32. $this->validator = $this->createMockValidator();
  33. $this->form = new Form('author', new Author(), $this->validator);
  34. }
  35. public function testConstructInitializesObject()
  36. {
  37. $this->assertEquals(new Author(), $this->form->getData());
  38. }
  39. public function testIsCsrfProtected()
  40. {
  41. $this->assertFalse($this->form->isCsrfProtected());
  42. $this->form->enableCsrfProtection();
  43. $this->assertTrue($this->form->isCsrfProtected());
  44. $this->form->disableCsrfProtection();
  45. $this->assertFalse($this->form->isCsrfProtected());
  46. }
  47. public function testNoCsrfProtectionByDefault()
  48. {
  49. $form = new Form('author', new Author(), $this->validator);
  50. $this->assertFalse($form->isCsrfProtected());
  51. }
  52. public function testDefaultCsrfProtectionCanBeEnabled()
  53. {
  54. Form::enableDefaultCsrfProtection();
  55. $form = new Form('author', new Author(), $this->validator);
  56. $this->assertTrue($form->isCsrfProtected());
  57. }
  58. public function testGeneratedCsrfSecretByDefault()
  59. {
  60. $form = new Form('author', new Author(), $this->validator);
  61. $this->assertTrue(strlen($form->getCsrfSecret()) >= 32);
  62. }
  63. public function testDefaultCsrfSecretCanBeSet()
  64. {
  65. Form::setDefaultCsrfSecret('foobar');
  66. $form = new Form('author', new Author(), $this->validator);
  67. $this->assertEquals('foobar', $form->getCsrfSecret());
  68. }
  69. public function testDefaultCsrfFieldNameCanBeSet()
  70. {
  71. Form::setDefaultCsrfFieldName('foobar');
  72. $form = new Form('author', new Author(), $this->validator);
  73. $this->assertEquals('foobar', $form->getCsrfFieldName());
  74. }
  75. public function testCsrfProtectedFormsHaveExtraField()
  76. {
  77. $this->form->enableCsrfProtection();
  78. $this->assertTrue($this->form->has($this->form->getCsrfFieldName()));
  79. $field = $this->form->get($this->form->getCsrfFieldName());
  80. $this->assertTrue($field instanceof HiddenField);
  81. $this->assertGreaterThanOrEqual(32, strlen($field->getDisplayedData()));
  82. }
  83. public function testIsCsrfTokenValidPassesIfCsrfProtectionIsDisabled()
  84. {
  85. $this->form->bind(array());
  86. $this->assertTrue($this->form->isCsrfTokenValid());
  87. }
  88. public function testIsCsrfTokenValidPasses()
  89. {
  90. $this->form->enableCsrfProtection();
  91. $field = $this->form->getCsrfFieldName();
  92. $token = $this->form->get($field)->getDisplayedData();
  93. $this->form->bind(array($field => $token));
  94. $this->assertTrue($this->form->isCsrfTokenValid());
  95. }
  96. public function testIsCsrfTokenValidFails()
  97. {
  98. $this->form->enableCsrfProtection();
  99. $field = $this->form->getCsrfFieldName();
  100. $this->form->bind(array($field => 'foobar'));
  101. $this->assertFalse($this->form->isCsrfTokenValid());
  102. }
  103. public function testDefaultLocaleCanBeSet()
  104. {
  105. Form::setDefaultLocale('de-DE-1996');
  106. $form = new Form('author', new Author(), $this->validator);
  107. $field = $this->getMock('Symfony\Components\Form\Field', array(), array(), '', false, false);
  108. $field->expects($this->any())
  109. ->method('getKey')
  110. ->will($this->returnValue('firstName'));
  111. $field->expects($this->once())
  112. ->method('setLocale')
  113. ->with($this->equalTo('de-DE-1996'));
  114. $form->add($field);
  115. }
  116. public function testDefaultTranslatorCanBeSet()
  117. {
  118. $translator = $this->getMock('Symfony\Components\I18N\TranslatorInterface');
  119. Form::setDefaultTranslator($translator);
  120. $form = new Form('author', new Author(), $this->validator);
  121. $field = $this->getMock('Symfony\Components\Form\Field', array(), array(), '', false, false);
  122. $field->expects($this->any())
  123. ->method('getKey')
  124. ->will($this->returnValue('firstName'));
  125. $field->expects($this->once())
  126. ->method('setTranslator')
  127. ->with($this->equalTo($translator));
  128. $form->add($field);
  129. }
  130. public function testValidationGroupsCanBeSet()
  131. {
  132. $form = new Form('author', new Author(), $this->validator);
  133. $this->assertNull($form->getValidationGroups());
  134. $form->setValidationGroups('group');
  135. $this->assertEquals(array('group'), $form->getValidationGroups());
  136. $form->setValidationGroups(array('group1', 'group2'));
  137. $this->assertEquals(array('group1', 'group2'), $form->getValidationGroups());
  138. $form->setValidationGroups(null);
  139. $this->assertNull($form->getValidationGroups());
  140. }
  141. public function testBindUsesValidationGroups()
  142. {
  143. $field = $this->createMockField('firstName');
  144. $form = new Form('author', new Author(), $this->validator);
  145. $form->add($field);
  146. $form->setValidationGroups('group');
  147. $this->validator->expects($this->once())
  148. ->method('validate')
  149. ->with($this->equalTo($form), $this->equalTo(array('group')));
  150. $form->bind(array()); // irrelevant
  151. }
  152. public function testBindConvertsUploadedFiles()
  153. {
  154. $tmpFile = $this->createTempFile();
  155. $file = new UploadedFile($tmpFile, basename($tmpFile), 'text/plain', 100, 0);
  156. $field = $this->createMockField('file');
  157. $field->expects($this->once())
  158. ->method('bind')
  159. ->with($this->equalTo($file));
  160. $form = new Form('author', new Author(), $this->validator);
  161. $form->add($field);
  162. // test
  163. $form->bind(array(), array('file' => array(
  164. 'name' => basename($tmpFile),
  165. 'type' => 'text/plain',
  166. 'tmp_name' => $tmpFile,
  167. 'error' => 0,
  168. 'size' => 100
  169. )));
  170. }
  171. public function testBindConvertsUploadedFilesWithPhpBug()
  172. {
  173. $tmpFile = $this->createTempFile();
  174. $file = new UploadedFile($tmpFile, basename($tmpFile), 'text/plain', 100, 0);
  175. $field = $this->createMockField('file');
  176. $field->expects($this->once())
  177. ->method('bind')
  178. ->with($this->equalTo($file));
  179. $form = new Form('author', new Author(), $this->validator);
  180. $form->add($field);
  181. // test
  182. $form->bind(array(), array(
  183. 'name' => array(
  184. 'file' => basename($tmpFile),
  185. ),
  186. 'type' => array(
  187. 'file' => 'text/plain',
  188. ),
  189. 'tmp_name' => array(
  190. 'file' => $tmpFile,
  191. ),
  192. 'error' => array(
  193. 'file' => 0,
  194. ),
  195. 'size' => array(
  196. 'file' => 100,
  197. ),
  198. ));
  199. }
  200. public function testBindConvertsNestedUploadedFilesWithPhpBug()
  201. {
  202. $tmpFile = $this->createTempFile();
  203. $file = new UploadedFile($tmpFile, basename($tmpFile), 'text/plain', 100, 0);
  204. $group = $this->getMock(
  205. 'Symfony\Components\Form\FieldGroup',
  206. array('bind'),
  207. array('child', array('property_path' => null))
  208. );
  209. $group->expects($this->once())
  210. ->method('bind')
  211. ->with($this->equalTo(array('file' => $file)));
  212. $form = new Form('author', new Author(), $this->validator);
  213. $form->add($group);
  214. // test
  215. $form->bind(array(), array(
  216. 'name' => array(
  217. 'child' => array('file' => basename($tmpFile)),
  218. ),
  219. 'type' => array(
  220. 'child' => array('file' => 'text/plain'),
  221. ),
  222. 'tmp_name' => array(
  223. 'child' => array('file' => $tmpFile),
  224. ),
  225. 'error' => array(
  226. 'child' => array('file' => 0),
  227. ),
  228. 'size' => array(
  229. 'child' => array('file' => 100),
  230. ),
  231. ));
  232. }
  233. public function testMultipartFormsWithoutParentsRequireFiles()
  234. {
  235. $form = new Form('author', new Author(), $this->validator);
  236. $form->add($this->createMultipartMockField('file'));
  237. $this->setExpectedException('InvalidArgumentException');
  238. // should be given in second argument
  239. $form->bind(array('file' => 'test.txt'));
  240. }
  241. public function testMultipartFormsWithParentsRequireNoFiles()
  242. {
  243. $form = new Form('author', new Author(), $this->validator);
  244. $form->add($this->createMultipartMockField('file'));
  245. $form->setParent($this->createMockField('group'));
  246. // files are expected to be converted by the parent
  247. $form->bind(array('file' => 'test.txt'));
  248. }
  249. public function testRenderFormTagProducesValidXhtml()
  250. {
  251. $form = new Form('author', new Author(), $this->validator);
  252. $this->assertEquals('<form action="url" method="post">', $form->renderFormTag('url'));
  253. }
  254. public function testSetCharsetAdjustsGenerator()
  255. {
  256. $form = $this->getMock(
  257. 'Symfony\Components\Form\Form',
  258. array('setGenerator'),
  259. array(),
  260. '',
  261. false // don't call original constructor
  262. );
  263. $form->expects($this->once())
  264. ->method('setGenerator')
  265. ->with($this->equalTo(new HtmlGenerator('iso-8859-1')));
  266. $form->setCharset('iso-8859-1');
  267. }
  268. protected function createMockField($key)
  269. {
  270. $field = $this->getMock(
  271. 'Symfony\Components\Form\FieldInterface',
  272. array(),
  273. array(),
  274. '',
  275. false, // don't use constructor
  276. false // don't call parent::__clone
  277. );
  278. $field->expects($this->any())
  279. ->method('getKey')
  280. ->will($this->returnValue($key));
  281. return $field;
  282. }
  283. protected function createMockFieldGroup($key)
  284. {
  285. $field = $this->getMock(
  286. 'Symfony\Components\Form\FieldGroup',
  287. array(),
  288. array(),
  289. '',
  290. false, // don't use constructor
  291. false // don't call parent::__clone
  292. );
  293. $field->expects($this->any())
  294. ->method('getKey')
  295. ->will($this->returnValue($key));
  296. return $field;
  297. }
  298. protected function createMultipartMockField($key)
  299. {
  300. $field = $this->createMockField($key);
  301. $field->expects($this->any())
  302. ->method('isMultipart')
  303. ->will($this->returnValue(true));
  304. return $field;
  305. }
  306. protected function createTempFile()
  307. {
  308. return tempnam(sys_get_temp_dir(), 'FormTest');
  309. }
  310. protected function createMockValidator()
  311. {
  312. return $this->getMock('Symfony\Components\Validator\ValidatorInterface');
  313. }
  314. }