FormTest.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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. $this->assertTrue(strlen($form->getCsrfSecret()) >= 32);
  78. }
  79. public function testDefaultCsrfSecretCanBeSet()
  80. {
  81. Form::setDefaultCsrfSecret('foobar');
  82. $form = new Form('author', new Author(), $this->validator);
  83. $this->assertEquals('foobar', $form->getCsrfSecret());
  84. }
  85. public function testDefaultCsrfFieldNameCanBeSet()
  86. {
  87. Form::setDefaultCsrfFieldName('foobar');
  88. $form = new Form('author', new Author(), $this->validator);
  89. $this->assertEquals('foobar', $form->getCsrfFieldName());
  90. }
  91. public function testCsrfProtectedFormsHaveExtraField()
  92. {
  93. $this->form->enableCsrfProtection();
  94. $this->assertTrue($this->form->has($this->form->getCsrfFieldName()));
  95. $field = $this->form->get($this->form->getCsrfFieldName());
  96. $this->assertTrue($field instanceof HiddenField);
  97. $this->assertGreaterThanOrEqual(32, strlen($field->getDisplayedData()));
  98. }
  99. public function testIsCsrfTokenValidPassesIfCsrfProtectionIsDisabled()
  100. {
  101. $this->form->bind(array());
  102. $this->assertTrue($this->form->isCsrfTokenValid());
  103. }
  104. public function testIsCsrfTokenValidPasses()
  105. {
  106. $this->form->enableCsrfProtection();
  107. $field = $this->form->getCsrfFieldName();
  108. $token = $this->form->get($field)->getDisplayedData();
  109. $this->form->bind(array($field => $token));
  110. $this->assertTrue($this->form->isCsrfTokenValid());
  111. }
  112. public function testIsCsrfTokenValidFails()
  113. {
  114. $this->form->enableCsrfProtection();
  115. $field = $this->form->getCsrfFieldName();
  116. $this->form->bind(array($field => 'foobar'));
  117. $this->assertFalse($this->form->isCsrfTokenValid());
  118. }
  119. public function testDefaultLocaleCanBeSet()
  120. {
  121. Form::setDefaultLocale('de-DE-1996');
  122. $form = new Form('author', new Author(), $this->validator);
  123. $field = $this->getMock('Symfony\Component\Form\Field', array(), array(), '', false, false);
  124. $field->expects($this->any())
  125. ->method('getKey')
  126. ->will($this->returnValue('firstName'));
  127. $field->expects($this->once())
  128. ->method('setLocale')
  129. ->with($this->equalTo('de-DE-1996'));
  130. $form->add($field);
  131. }
  132. public function testValidationGroupsCanBeSet()
  133. {
  134. $form = new Form('author', new Author(), $this->validator);
  135. $this->assertNull($form->getValidationGroups());
  136. $form->setValidationGroups('group');
  137. $this->assertEquals(array('group'), $form->getValidationGroups());
  138. $form->setValidationGroups(array('group1', 'group2'));
  139. $this->assertEquals(array('group1', 'group2'), $form->getValidationGroups());
  140. $form->setValidationGroups(null);
  141. $this->assertNull($form->getValidationGroups());
  142. }
  143. public function testBindUsesValidationGroups()
  144. {
  145. $field = $this->createMockField('firstName');
  146. $form = new Form('author', new Author(), $this->validator);
  147. $form->add($field);
  148. $form->setValidationGroups('group');
  149. $this->validator->expects($this->once())
  150. ->method('validate')
  151. ->with($this->equalTo($form), $this->equalTo(array('group')));
  152. $form->bind(array()); // irrelevant
  153. }
  154. public function testMultipartFormsWithoutParentsRequireFiles()
  155. {
  156. $form = new Form('author', new Author(), $this->validator);
  157. $form->add($this->createMultipartMockField('file'));
  158. $this->setExpectedException('InvalidArgumentException');
  159. // should be given in second argument
  160. $form->bind(array('file' => 'test.txt'));
  161. }
  162. public function testMultipartFormsWithParentsRequireNoFiles()
  163. {
  164. $form = new Form('author', new Author(), $this->validator);
  165. $form->add($this->createMultipartMockField('file'));
  166. $form->setParent($this->createMockField('group'));
  167. // files are expected to be converted by the parent
  168. $form->bind(array('file' => 'test.txt'));
  169. }
  170. protected function createMockField($key)
  171. {
  172. $field = $this->getMock(
  173. 'Symfony\Component\Form\FieldInterface',
  174. array(),
  175. array(),
  176. '',
  177. false, // don't use constructor
  178. false // don't call parent::__clone
  179. );
  180. $field->expects($this->any())
  181. ->method('getKey')
  182. ->will($this->returnValue($key));
  183. return $field;
  184. }
  185. protected function createMockFieldGroup($key)
  186. {
  187. $field = $this->getMock(
  188. 'Symfony\Component\Form\FieldGroup',
  189. array(),
  190. array(),
  191. '',
  192. false, // don't use constructor
  193. false // don't call parent::__clone
  194. );
  195. $field->expects($this->any())
  196. ->method('getKey')
  197. ->will($this->returnValue($key));
  198. return $field;
  199. }
  200. protected function createMultipartMockField($key)
  201. {
  202. $field = $this->createMockField($key);
  203. $field->expects($this->any())
  204. ->method('isMultipart')
  205. ->will($this->returnValue(true));
  206. return $field;
  207. }
  208. protected function createMockValidator()
  209. {
  210. return $this->getMock('Symfony\Component\Validator\ValidatorInterface');
  211. }
  212. }