FormTest.php 8.2 KB

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