FormTest.php 9.4 KB

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