FormTest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Tests\Component\Form;
  11. require_once __DIR__ . '/Fixtures/Author.php';
  12. require_once __DIR__ . '/Fixtures/TestField.php';
  13. use Symfony\Component\Form\Form;
  14. use Symfony\Component\Form\FormContext;
  15. use Symfony\Component\Form\Field;
  16. use Symfony\Component\Form\HiddenField;
  17. use Symfony\Component\Form\FieldGroup;
  18. use Symfony\Component\Form\PropertyPath;
  19. use Symfony\Component\HttpFoundation\Request;
  20. use Symfony\Component\HttpFoundation\File\UploadedFile;
  21. use Symfony\Component\Validator\ConstraintViolation;
  22. use Symfony\Component\Validator\ConstraintViolationList;
  23. use Symfony\Tests\Component\Form\Fixtures\Author;
  24. use Symfony\Tests\Component\Form\Fixtures\TestField;
  25. class FormTest_PreconfiguredForm extends Form
  26. {
  27. protected function configure()
  28. {
  29. $this->add(new Field('firstName'));
  30. parent::configure();
  31. }
  32. }
  33. class TestSetDataBeforeConfigureForm extends Form
  34. {
  35. protected $testCase;
  36. protected $object;
  37. public function __construct($testCase, $name, $object, $validator)
  38. {
  39. $this->testCase = $testCase;
  40. $this->object = $object;
  41. parent::__construct($name, $object, $validator);
  42. }
  43. protected function configure()
  44. {
  45. $this->testCase->assertEquals($this->object, $this->getData());
  46. parent::configure();
  47. }
  48. }
  49. class FormTest extends \PHPUnit_Framework_TestCase
  50. {
  51. protected $validator;
  52. protected $form;
  53. public static function setUpBeforeClass()
  54. {
  55. @session_start();
  56. }
  57. protected function setUp()
  58. {
  59. $this->validator = $this->createMockValidator();
  60. $this->form = new Form('author', new Author(), $this->validator);
  61. }
  62. public function testConstructInitializesObject()
  63. {
  64. $this->assertEquals(new Author(), $this->form->getData());
  65. }
  66. public function testSetDataBeforeConfigure()
  67. {
  68. new TestSetDataBeforeConfigureForm($this, 'author', new Author(), $this->validator);
  69. }
  70. public function testNoCsrfProtectionByDefault()
  71. {
  72. $form = new Form('author', new Author(), $this->validator);
  73. $this->assertFalse($form->isCsrfProtected());
  74. }
  75. public function testCsrfProtectionCanBeEnabled()
  76. {
  77. $form = new Form('author', new Author(), $this->validator, array(
  78. 'csrf_provider' => $this->createMockCsrfProvider(),
  79. ));
  80. $this->assertTrue($form->isCsrfProtected());
  81. }
  82. public function testCsrfFieldNameCanBeSet()
  83. {
  84. $form = new Form('author', new Author(), $this->validator, array(
  85. 'csrf_provider' => $this->createMockCsrfProvider(),
  86. 'csrf_field_name' => 'foobar',
  87. ));
  88. $this->assertEquals('foobar', $form->getCsrfFieldName());
  89. }
  90. public function testCsrfProtectedFormsHaveExtraField()
  91. {
  92. $provider = $this->createMockCsrfProvider();
  93. $provider->expects($this->once())
  94. ->method('generateCsrfToken')
  95. ->with($this->equalTo('Symfony\Component\Form\Form'))
  96. ->will($this->returnValue('ABCDEF'));
  97. $form = new Form('author', new Author(), $this->validator, array(
  98. 'csrf_provider' => $provider,
  99. ));
  100. $this->assertTrue($form->has($this->form->getCsrfFieldName()));
  101. $field = $form->get($form->getCsrfFieldName());
  102. $this->assertTrue($field instanceof HiddenField);
  103. $this->assertEquals('ABCDEF', $field->getDisplayedData());
  104. }
  105. public function testIsCsrfTokenValidPassesIfCsrfProtectionIsDisabled()
  106. {
  107. $this->form->bind(array());
  108. $this->assertTrue($this->form->isCsrfTokenValid());
  109. }
  110. public function testIsCsrfTokenValidPasses()
  111. {
  112. $provider = $this->createMockCsrfProvider();
  113. $provider->expects($this->once())
  114. ->method('isCsrfTokenValid')
  115. ->with($this->equalTo('Symfony\Component\Form\Form'), $this->equalTo('ABCDEF'))
  116. ->will($this->returnValue(true));
  117. $form = new Form('author', new Author(), $this->validator, array(
  118. 'csrf_provider' => $provider,
  119. ));
  120. $field = $form->getCsrfFieldName();
  121. $form->bind(array($field => 'ABCDEF'));
  122. $this->assertTrue($form->isCsrfTokenValid());
  123. }
  124. public function testIsCsrfTokenValidFails()
  125. {
  126. $provider = $this->createMockCsrfProvider();
  127. $provider->expects($this->once())
  128. ->method('isCsrfTokenValid')
  129. ->with($this->equalTo('Symfony\Component\Form\Form'), $this->equalTo('ABCDEF'))
  130. ->will($this->returnValue(false));
  131. $form = new Form('author', new Author(), $this->validator, array(
  132. 'csrf_provider' => $provider,
  133. ));
  134. $field = $form->getCsrfFieldName();
  135. $form->bind(array($field => 'ABCDEF'));
  136. $this->assertFalse($form->isCsrfTokenValid());
  137. }
  138. public function testValidationGroupNullByDefault()
  139. {
  140. $this->assertNull($this->form->getValidationGroups());
  141. }
  142. public function testValidationGroupsCanBeSetToString()
  143. {
  144. $form = new Form('author', new Author(), $this->validator, array(
  145. 'validation_groups' => 'group',
  146. ));
  147. $this->assertEquals(array('group'), $form->getValidationGroups());
  148. }
  149. public function testValidationGroupsCanBeSetToArray()
  150. {
  151. $form = new Form('author', new Author(), $this->validator, array(
  152. 'validation_groups' => array('group1', 'group2'),
  153. ));
  154. $this->assertEquals(array('group1', 'group2'), $form->getValidationGroups());
  155. }
  156. public function testBindUsesValidationGroups()
  157. {
  158. $field = $this->createMockField('firstName');
  159. $form = new Form('author', new Author(), $this->validator, array(
  160. 'validation_groups' => 'group',
  161. ));
  162. $form->add($field);
  163. $this->validator->expects($this->once())
  164. ->method('validate')
  165. ->with($this->equalTo($form), $this->equalTo(array('group')));
  166. $form->bind(array()); // irrelevant
  167. }
  168. public function testBindThrowsExceptionIfNoValidatorIsSet()
  169. {
  170. $field = $this->createMockField('firstName');
  171. $form = new Form('author', new Author());
  172. $form->add($field);
  173. $this->setExpectedException('Symfony\Component\Form\Exception\FormException');
  174. $form->bind(array()); // irrelevant
  175. }
  176. public function testBindRequest()
  177. {
  178. $values = array(
  179. 'author' => array(
  180. 'name' => 'Bernhard',
  181. 'image' => array('filename' => 'foobar.png'),
  182. ),
  183. );
  184. $files = array(
  185. 'author' => array(
  186. 'error' => array('image' => array('file' => UPLOAD_ERR_OK)),
  187. 'name' => array('image' => array('file' => 'upload.png')),
  188. 'size' => array('image' => array('file' => 123)),
  189. 'tmp_name' => array('image' => array('file' => 'abcdef.png')),
  190. 'type' => array('image' => array('file' => 'image/png')),
  191. ),
  192. );
  193. $request = new Request(array(), $values, array(), array(), $files);
  194. $form = new Form('author', null, $this->createMockValidator());
  195. $form->add(new TestField('name'));
  196. $imageForm = new FieldGroup('image');
  197. $imageForm->add(new TestField('file'));
  198. $imageForm->add(new TestField('filename'));
  199. $form->add($imageForm);
  200. $form->bindRequest($request);
  201. $file = new UploadedFile('abcdef.png', 'upload.png', 'image/png', 123, UPLOAD_ERR_OK);
  202. $this->assertEquals('Bernhard', $form['name']->getData());
  203. $this->assertEquals('foobar.png', $form['image']['filename']->getData());
  204. $this->assertEquals($file, $form['image']['file']->getData());
  205. }
  206. public function testBindGlobals()
  207. {
  208. $_POST = array(
  209. 'author' => array(
  210. 'name' => 'Bernhard',
  211. 'image' => array('filename' => 'foobar.png'),
  212. ),
  213. );
  214. $_FILES = array(
  215. 'author' => array(
  216. 'error' => array('image' => array('file' => UPLOAD_ERR_OK)),
  217. 'name' => array('image' => array('file' => 'upload.png')),
  218. 'size' => array('image' => array('file' => 123)),
  219. 'tmp_name' => array('image' => array('file' => 'abcdef.png')),
  220. 'type' => array('image' => array('file' => 'image/png')),
  221. ),
  222. );
  223. $form = new Form('author', null, $this->createMockValidator());
  224. $form->add(new TestField('name'));
  225. $imageForm = new FieldGroup('image');
  226. $imageForm->add(new TestField('file'));
  227. $imageForm->add(new TestField('filename'));
  228. $form->add($imageForm);
  229. $form->bindGlobals();
  230. $file = new UploadedFile('abcdef.png', 'upload.png', 'image/png', 123, UPLOAD_ERR_OK);
  231. $this->assertEquals('Bernhard', $form['name']->getData());
  232. $this->assertEquals('foobar.png', $form['image']['filename']->getData());
  233. $this->assertEquals($file, $form['image']['file']->getData());
  234. }
  235. public function testUpdateFromPropertyIsIgnoredIfFormHasObject()
  236. {
  237. $author = new Author();
  238. $author->child = new Author();
  239. $standaloneChild = new Author();
  240. $form = new Form('child', $standaloneChild);
  241. $form->updateFromProperty($author);
  242. // should not be $author->child!!
  243. $this->assertSame($standaloneChild, $form->getData());
  244. }
  245. public function testUpdateFromPropertyIsNotIgnoredIfFormHasNoObject()
  246. {
  247. $author = new Author();
  248. $author->child = new Author();
  249. $form = new Form('child');
  250. $form->updateFromProperty($author);
  251. // should not be $author->child!!
  252. $this->assertSame($author->child, $form->getData());
  253. }
  254. public function testUpdatePropertyIsIgnoredIfFormHasObject()
  255. {
  256. $author = new Author();
  257. $author->child = $child = new Author();
  258. $standaloneChild = new Author();
  259. $form = new Form('child', $standaloneChild);
  260. $form->updateProperty($author);
  261. // $author->child was not modified
  262. $this->assertSame($child, $author->child);
  263. }
  264. public function testUpdatePropertyIsNotIgnoredIfFormHasNoObject()
  265. {
  266. $author = new Author();
  267. $child = new Author();
  268. $form = new Form('child');
  269. $form->setData($child);
  270. $form->updateProperty($author);
  271. // $author->child was set
  272. $this->assertSame($child, $author->child);
  273. }
  274. protected function createMockField($key)
  275. {
  276. $field = $this->getMock(
  277. 'Symfony\Component\Form\FieldInterface',
  278. array(),
  279. array(),
  280. '',
  281. false, // don't use constructor
  282. false // don't call parent::__clone
  283. );
  284. $field->expects($this->any())
  285. ->method('getKey')
  286. ->will($this->returnValue($key));
  287. return $field;
  288. }
  289. protected function createMockFieldGroup($key)
  290. {
  291. $field = $this->getMock(
  292. 'Symfony\Component\Form\FieldGroup',
  293. array(),
  294. array(),
  295. '',
  296. false, // don't use constructor
  297. false // don't call parent::__clone
  298. );
  299. $field->expects($this->any())
  300. ->method('getKey')
  301. ->will($this->returnValue($key));
  302. return $field;
  303. }
  304. protected function createMultipartMockField($key)
  305. {
  306. $field = $this->createMockField($key);
  307. $field->expects($this->any())
  308. ->method('isMultipart')
  309. ->will($this->returnValue(true));
  310. return $field;
  311. }
  312. protected function createMockValidator()
  313. {
  314. return $this->getMock('Symfony\Component\Validator\ValidatorInterface');
  315. }
  316. protected function createMockCsrfProvider()
  317. {
  318. return $this->getMock('Symfony\Component\Form\CsrfProvider\CsrfProviderInterface');
  319. }
  320. }