FormTest.php 8.1 KB

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