FormTest.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  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\FormConfiguration;
  7. use Symfony\Component\Form\Field;
  8. use Symfony\Component\Form\HiddenField;
  9. use Symfony\Component\Form\FieldGroup;
  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. parent::configure();
  21. }
  22. }
  23. class TestSetDataBeforeConfigureForm extends Form
  24. {
  25. protected $testCase;
  26. protected $object;
  27. public function __construct($testCase, $name, $object, $validator)
  28. {
  29. $this->testCase = $testCase;
  30. $this->object = $object;
  31. parent::__construct($name, $object, $validator);
  32. }
  33. protected function configure()
  34. {
  35. $this->testCase->assertEquals($this->object, $this->getData());
  36. parent::configure();
  37. }
  38. }
  39. class FormTest extends \PHPUnit_Framework_TestCase
  40. {
  41. protected $validator;
  42. protected $form;
  43. public static function setUpBeforeClass()
  44. {
  45. @session_start();
  46. }
  47. protected function setUp()
  48. {
  49. FormConfiguration::disableDefaultCsrfProtection();
  50. FormConfiguration::setDefaultCsrfSecret(null);
  51. $this->validator = $this->createMockValidator();
  52. $this->form = new Form('author', new Author(), $this->validator);
  53. }
  54. public function testConstructInitializesObject()
  55. {
  56. $this->assertEquals(new Author(), $this->form->getData());
  57. }
  58. public function testSetDataBeforeConfigure()
  59. {
  60. new TestSetDataBeforeConfigureForm($this, 'author', new Author(), $this->validator);
  61. }
  62. public function testIsCsrfProtected()
  63. {
  64. $this->assertFalse($this->form->isCsrfProtected());
  65. $this->form->enableCsrfProtection();
  66. $this->assertTrue($this->form->isCsrfProtected());
  67. $this->form->disableCsrfProtection();
  68. $this->assertFalse($this->form->isCsrfProtected());
  69. }
  70. public function testNoCsrfProtectionByDefault()
  71. {
  72. $form = new Form('author', new Author(), $this->validator);
  73. $this->assertFalse($form->isCsrfProtected());
  74. }
  75. public function testDefaultCsrfProtectionCanBeEnabled()
  76. {
  77. FormConfiguration::enableDefaultCsrfProtection();
  78. $form = new Form('author', new Author(), $this->validator);
  79. $this->assertTrue($form->isCsrfProtected());
  80. }
  81. public function testGeneratedCsrfSecretByDefault()
  82. {
  83. $form = new Form('author', new Author(), $this->validator);
  84. $form->enableCsrfProtection();
  85. $this->assertTrue(strlen($form->getCsrfSecret()) >= 32);
  86. }
  87. public function testDefaultCsrfSecretCanBeSet()
  88. {
  89. FormConfiguration::setDefaultCsrfSecret('foobar');
  90. $form = new Form('author', new Author(), $this->validator);
  91. $form->enableCsrfProtection();
  92. $this->assertEquals('foobar', $form->getCsrfSecret());
  93. }
  94. public function testDefaultCsrfFieldNameCanBeSet()
  95. {
  96. FormConfiguration::setDefaultCsrfFieldName('foobar');
  97. $form = new Form('author', new Author(), $this->validator);
  98. $form->enableCsrfProtection();
  99. $this->assertEquals('foobar', $form->getCsrfFieldName());
  100. }
  101. public function testCsrfProtectedFormsHaveExtraField()
  102. {
  103. $this->form->enableCsrfProtection();
  104. $this->assertTrue($this->form->has($this->form->getCsrfFieldName()));
  105. $field = $this->form->get($this->form->getCsrfFieldName());
  106. $this->assertTrue($field instanceof HiddenField);
  107. $this->assertGreaterThanOrEqual(32, strlen($field->getDisplayedData()));
  108. }
  109. public function testIsCsrfTokenValidPassesIfCsrfProtectionIsDisabled()
  110. {
  111. $this->form->bind(array());
  112. $this->assertTrue($this->form->isCsrfTokenValid());
  113. }
  114. public function testIsCsrfTokenValidPasses()
  115. {
  116. $this->form->enableCsrfProtection();
  117. $field = $this->form->getCsrfFieldName();
  118. $token = $this->form->get($field)->getDisplayedData();
  119. $this->form->bind(array($field => $token));
  120. $this->assertTrue($this->form->isCsrfTokenValid());
  121. }
  122. public function testIsCsrfTokenValidFails()
  123. {
  124. $this->form->enableCsrfProtection();
  125. $field = $this->form->getCsrfFieldName();
  126. $this->form->bind(array($field => 'foobar'));
  127. $this->assertFalse($this->form->isCsrfTokenValid());
  128. }
  129. public function testDefaultLocaleCanBeSet()
  130. {
  131. FormConfiguration::setDefaultLocale('de-DE-1996');
  132. $form = new Form('author', new Author(), $this->validator);
  133. $field = $this->getMock('Symfony\Component\Form\Field', array(), array(), '', false, false);
  134. $field->expects($this->any())
  135. ->method('getKey')
  136. ->will($this->returnValue('firstName'));
  137. $field->expects($this->once())
  138. ->method('setLocale')
  139. ->with($this->equalTo('de-DE-1996'));
  140. $form->add($field);
  141. }
  142. public function testValidationGroupsCanBeSet()
  143. {
  144. $form = new Form('author', new Author(), $this->validator);
  145. $this->assertNull($form->getValidationGroups());
  146. $form->setValidationGroups('group');
  147. $this->assertEquals(array('group'), $form->getValidationGroups());
  148. $form->setValidationGroups(array('group1', 'group2'));
  149. $this->assertEquals(array('group1', 'group2'), $form->getValidationGroups());
  150. $form->setValidationGroups(null);
  151. $this->assertNull($form->getValidationGroups());
  152. }
  153. public function testBindUsesValidationGroups()
  154. {
  155. $field = $this->createMockField('firstName');
  156. $form = new Form('author', new Author(), $this->validator);
  157. $form->add($field);
  158. $form->setValidationGroups('group');
  159. $this->validator->expects($this->once())
  160. ->method('validate')
  161. ->with($this->equalTo($form), $this->equalTo(array('group')));
  162. $form->bind(array()); // irrelevant
  163. }
  164. public function testBindThrowsExceptionIfNoValidatorIsSet()
  165. {
  166. $field = $this->createMockField('firstName');
  167. $form = new Form('author', new Author());
  168. $form->add($field);
  169. $form->setValidationGroups('group');
  170. $this->setExpectedException('Symfony\Component\Form\Exception\FormException');
  171. $form->bind(array()); // irrelevant
  172. }
  173. public function testMultipartFormsWithoutParentsRequireFiles()
  174. {
  175. $form = new Form('author', new Author(), $this->validator);
  176. $form->add($this->createMultipartMockField('file'));
  177. $this->setExpectedException('InvalidArgumentException');
  178. // should be given in second argument
  179. $form->bind(array('file' => 'test.txt'));
  180. }
  181. public function testMultipartFormsWithParentsRequireNoFiles()
  182. {
  183. $form = new Form('author', new Author(), $this->validator);
  184. $form->add($this->createMultipartMockField('file'));
  185. $form->setParent($this->createMockField('group'));
  186. // files are expected to be converted by the parent
  187. $form->bind(array('file' => 'test.txt'));
  188. }
  189. public function testUpdateFromPropertyIsIgnoredIfFormHasObject()
  190. {
  191. $author = new Author();
  192. $author->child = new Author();
  193. $standaloneChild = new Author();
  194. $form = new Form('child', $standaloneChild);
  195. $form->updateFromProperty($author);
  196. // should not be $author->child!!
  197. $this->assertSame($standaloneChild, $form->getData());
  198. }
  199. public function testUpdateFromPropertyIsNotIgnoredIfFormHasNoObject()
  200. {
  201. $author = new Author();
  202. $author->child = new Author();
  203. $form = new Form('child');
  204. $form->updateFromProperty($author);
  205. // should not be $author->child!!
  206. $this->assertSame($author->child, $form->getData());
  207. }
  208. public function testUpdatePropertyIsIgnoredIfFormHasObject()
  209. {
  210. $author = new Author();
  211. $author->child = $child = new Author();
  212. $standaloneChild = new Author();
  213. $form = new Form('child', $standaloneChild);
  214. $form->updateProperty($author);
  215. // $author->child was not modified
  216. $this->assertSame($child, $author->child);
  217. }
  218. public function testUpdatePropertyIsNotIgnoredIfFormHasNoObject()
  219. {
  220. $author = new Author();
  221. $child = new Author();
  222. $form = new Form('child');
  223. $form->setData($child);
  224. $form->updateProperty($author);
  225. // $author->child was set
  226. $this->assertSame($child, $author->child);
  227. }
  228. protected function createMockField($key)
  229. {
  230. $field = $this->getMock(
  231. 'Symfony\Component\Form\FieldInterface',
  232. array(),
  233. array(),
  234. '',
  235. false, // don't use constructor
  236. false // don't call parent::__clone
  237. );
  238. $field->expects($this->any())
  239. ->method('getKey')
  240. ->will($this->returnValue($key));
  241. return $field;
  242. }
  243. protected function createMockFieldGroup($key)
  244. {
  245. $field = $this->getMock(
  246. 'Symfony\Component\Form\FieldGroup',
  247. array(),
  248. array(),
  249. '',
  250. false, // don't use constructor
  251. false // don't call parent::__clone
  252. );
  253. $field->expects($this->any())
  254. ->method('getKey')
  255. ->will($this->returnValue($key));
  256. return $field;
  257. }
  258. protected function createMultipartMockField($key)
  259. {
  260. $field = $this->createMockField($key);
  261. $field->expects($this->any())
  262. ->method('isMultipart')
  263. ->will($this->returnValue(true));
  264. return $field;
  265. }
  266. protected function createMockValidator()
  267. {
  268. return $this->getMock('Symfony\Component\Validator\ValidatorInterface');
  269. }
  270. }