FormTest.php 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  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 testValidationGroupsCanBeSet()
  130. {
  131. $form = new Form('author', new Author(), $this->validator);
  132. $this->assertNull($form->getValidationGroups());
  133. $form->setValidationGroups('group');
  134. $this->assertEquals(array('group'), $form->getValidationGroups());
  135. $form->setValidationGroups(array('group1', 'group2'));
  136. $this->assertEquals(array('group1', 'group2'), $form->getValidationGroups());
  137. $form->setValidationGroups(null);
  138. $this->assertNull($form->getValidationGroups());
  139. }
  140. public function testBindUsesValidationGroups()
  141. {
  142. $field = $this->createMockField('firstName');
  143. $form = new Form('author', new Author(), $this->validator);
  144. $form->add($field);
  145. $form->setValidationGroups('group');
  146. $this->validator->expects($this->once())
  147. ->method('validate')
  148. ->with($this->equalTo($form), $this->equalTo(array('group')));
  149. $form->bind(array()); // irrelevant
  150. }
  151. public function testBindThrowsExceptionIfNoValidatorIsSet()
  152. {
  153. $field = $this->createMockField('firstName');
  154. $form = new Form('author', new Author());
  155. $form->add($field);
  156. $form->setValidationGroups('group');
  157. $this->setExpectedException('Symfony\Component\Form\Exception\FormException');
  158. $form->bind(array()); // irrelevant
  159. }
  160. public function testMultipartFormsWithoutParentsRequireFiles()
  161. {
  162. $form = new Form('author', new Author(), $this->validator);
  163. $form->add($this->createMultipartMockField('file'));
  164. $this->setExpectedException('InvalidArgumentException');
  165. // should be given in second argument
  166. $form->bind(array('file' => 'test.txt'));
  167. }
  168. public function testMultipartFormsWithParentsRequireNoFiles()
  169. {
  170. $form = new Form('author', new Author(), $this->validator);
  171. $form->add($this->createMultipartMockField('file'));
  172. $form->setParent($this->createMockField('group'));
  173. // files are expected to be converted by the parent
  174. $form->bind(array('file' => 'test.txt'));
  175. }
  176. public function testUpdateFromPropertyIsIgnoredIfFormHasObject()
  177. {
  178. $author = new Author();
  179. $author->child = new Author();
  180. $standaloneChild = new Author();
  181. $form = new Form('child', $standaloneChild);
  182. $form->updateFromProperty($author);
  183. // should not be $author->child!!
  184. $this->assertSame($standaloneChild, $form->getData());
  185. }
  186. public function testUpdateFromPropertyIsNotIgnoredIfFormHasNoObject()
  187. {
  188. $author = new Author();
  189. $author->child = new Author();
  190. $form = new Form('child');
  191. $form->updateFromProperty($author);
  192. // should not be $author->child!!
  193. $this->assertSame($author->child, $form->getData());
  194. }
  195. public function testUpdatePropertyIsIgnoredIfFormHasObject()
  196. {
  197. $author = new Author();
  198. $author->child = $child = new Author();
  199. $standaloneChild = new Author();
  200. $form = new Form('child', $standaloneChild);
  201. $form->updateProperty($author);
  202. // $author->child was not modified
  203. $this->assertSame($child, $author->child);
  204. }
  205. public function testUpdatePropertyIsNotIgnoredIfFormHasNoObject()
  206. {
  207. $author = new Author();
  208. $child = new Author();
  209. $form = new Form('child');
  210. $form->setData($child);
  211. $form->updateProperty($author);
  212. // $author->child was set
  213. $this->assertSame($child, $author->child);
  214. }
  215. protected function createMockField($key)
  216. {
  217. $field = $this->getMock(
  218. 'Symfony\Component\Form\FieldInterface',
  219. array(),
  220. array(),
  221. '',
  222. false, // don't use constructor
  223. false // don't call parent::__clone
  224. );
  225. $field->expects($this->any())
  226. ->method('getKey')
  227. ->will($this->returnValue($key));
  228. return $field;
  229. }
  230. protected function createMockFieldGroup($key)
  231. {
  232. $field = $this->getMock(
  233. 'Symfony\Component\Form\FieldGroup',
  234. array(),
  235. array(),
  236. '',
  237. false, // don't use constructor
  238. false // don't call parent::__clone
  239. );
  240. $field->expects($this->any())
  241. ->method('getKey')
  242. ->will($this->returnValue($key));
  243. return $field;
  244. }
  245. protected function createMultipartMockField($key)
  246. {
  247. $field = $this->createMockField($key);
  248. $field->expects($this->any())
  249. ->method('isMultipart')
  250. ->will($this->returnValue(true));
  251. return $field;
  252. }
  253. protected function createMockValidator()
  254. {
  255. return $this->getMock('Symfony\Component\Validator\ValidatorInterface');
  256. }
  257. }