CompoundFormTest.php 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.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\Component\Form\Tests;
  11. use PHPUnit\Framework\TestCase;
  12. use Symfony\Component\Form\Extension\Core\DataMapper\PropertyPathMapper;
  13. use Symfony\Component\Form\Extension\HttpFoundation\HttpFoundationRequestHandler;
  14. use Symfony\Component\Form\FormError;
  15. use Symfony\Component\Form\Forms;
  16. use Symfony\Component\Form\FormView;
  17. use Symfony\Component\Form\SubmitButtonBuilder;
  18. use Symfony\Component\Form\Tests\Fixtures\FixedDataTransformer;
  19. use Symfony\Component\HttpFoundation\File\UploadedFile;
  20. use Symfony\Component\HttpFoundation\Request;
  21. class CompoundFormTest extends AbstractFormTest
  22. {
  23. public function testValidIfAllChildrenAreValid()
  24. {
  25. $this->form->add($this->getBuilder('firstName')->getForm());
  26. $this->form->add($this->getBuilder('lastName')->getForm());
  27. $this->form->submit(array(
  28. 'firstName' => 'Bernhard',
  29. 'lastName' => 'Schussek',
  30. ));
  31. $this->assertTrue($this->form->isValid());
  32. }
  33. public function testInvalidIfChildIsInvalid()
  34. {
  35. $this->form->add($this->getBuilder('firstName')->getForm());
  36. $this->form->add($this->getBuilder('lastName')->getForm());
  37. $this->form->submit(array(
  38. 'firstName' => 'Bernhard',
  39. 'lastName' => 'Schussek',
  40. ));
  41. $this->form->get('lastName')->addError(new FormError('Invalid'));
  42. $this->assertFalse($this->form->isValid());
  43. }
  44. public function testDisabledFormsValidEvenIfChildrenInvalid()
  45. {
  46. $form = $this->getBuilder('person')
  47. ->setDisabled(true)
  48. ->setCompound(true)
  49. ->setDataMapper($this->getDataMapper())
  50. ->add($this->getBuilder('name'))
  51. ->getForm();
  52. $form->submit(array('name' => 'Jacques Doe'));
  53. $form->get('name')->addError(new FormError('Invalid'));
  54. $this->assertTrue($form->isValid());
  55. }
  56. public function testSubmitForwardsNullIfNotClearMissingButValueIsExplicitlyNull()
  57. {
  58. $child = $this->getMockForm('firstName');
  59. $this->form->add($child);
  60. $child->expects($this->once())
  61. ->method('submit')
  62. ->with($this->equalTo(null));
  63. $this->form->submit(array('firstName' => null), false);
  64. }
  65. public function testSubmitForwardsNullIfValueIsMissing()
  66. {
  67. $child = $this->getMockForm('firstName');
  68. $this->form->add($child);
  69. $child->expects($this->once())
  70. ->method('submit')
  71. ->with($this->equalTo(null));
  72. $this->form->submit(array());
  73. }
  74. public function testSubmitDoesNotForwardNullIfNotClearMissing()
  75. {
  76. $child = $this->getMockForm('firstName');
  77. $this->form->add($child);
  78. $child->expects($this->never())
  79. ->method('submit');
  80. $this->form->submit(array(), false);
  81. }
  82. public function testSubmitDoesNotAddExtraFieldForNullValues()
  83. {
  84. $factory = Forms::createFormFactoryBuilder()
  85. ->getFormFactory();
  86. $child = $factory->createNamed('file', 'Symfony\Component\Form\Extension\Core\Type\FileType', null, array('auto_initialize' => false));
  87. $this->form->add($child);
  88. $this->form->submit(array('file' => null), false);
  89. $this->assertCount(0, $this->form->getExtraData());
  90. }
  91. public function testClearMissingFlagIsForwarded()
  92. {
  93. $child = $this->getMockForm('firstName');
  94. $this->form->add($child);
  95. $child->expects($this->once())
  96. ->method('submit')
  97. ->with($this->equalTo('foo'), false);
  98. $this->form->submit(array('firstName' => 'foo'), false);
  99. }
  100. public function testCloneChildren()
  101. {
  102. $child = $this->getBuilder('child')->getForm();
  103. $this->form->add($child);
  104. $clone = clone $this->form;
  105. $this->assertNotSame($this->form, $clone);
  106. $this->assertNotSame($child, $clone['child']);
  107. $this->assertNotSame($this->form['child'], $clone['child']);
  108. }
  109. public function testNotEmptyIfChildNotEmpty()
  110. {
  111. $child = $this->getMockForm();
  112. $child->expects($this->once())
  113. ->method('isEmpty')
  114. ->will($this->returnValue(false));
  115. $this->form->setData(null);
  116. $this->form->add($child);
  117. $this->assertFalse($this->form->isEmpty());
  118. }
  119. public function testAdd()
  120. {
  121. $child = $this->getBuilder('foo')->getForm();
  122. $this->form->add($child);
  123. $this->assertTrue($this->form->has('foo'));
  124. $this->assertSame($this->form, $child->getParent());
  125. $this->assertSame(array('foo' => $child), $this->form->all());
  126. }
  127. public function testAddUsingNameAndType()
  128. {
  129. $child = $this->getBuilder('foo')->getForm();
  130. $this->factory->expects($this->once())
  131. ->method('createNamed')
  132. ->with('foo', 'Symfony\Component\Form\Extension\Core\Type\TextType', null, array(
  133. 'bar' => 'baz',
  134. 'auto_initialize' => false,
  135. ))
  136. ->will($this->returnValue($child));
  137. $this->form->add('foo', 'Symfony\Component\Form\Extension\Core\Type\TextType', array('bar' => 'baz'));
  138. $this->assertTrue($this->form->has('foo'));
  139. $this->assertSame($this->form, $child->getParent());
  140. $this->assertSame(array('foo' => $child), $this->form->all());
  141. }
  142. public function testAddUsingIntegerNameAndType()
  143. {
  144. $child = $this->getBuilder(0)->getForm();
  145. $this->factory->expects($this->once())
  146. ->method('createNamed')
  147. ->with('0', 'Symfony\Component\Form\Extension\Core\Type\TextType', null, array(
  148. 'bar' => 'baz',
  149. 'auto_initialize' => false,
  150. ))
  151. ->will($this->returnValue($child));
  152. // in order to make casting unnecessary
  153. $this->form->add(0, 'Symfony\Component\Form\Extension\Core\Type\TextType', array('bar' => 'baz'));
  154. $this->assertTrue($this->form->has(0));
  155. $this->assertSame($this->form, $child->getParent());
  156. $this->assertSame(array(0 => $child), $this->form->all());
  157. }
  158. public function testAddWithoutType()
  159. {
  160. $child = $this->getBuilder('foo')->getForm();
  161. $this->factory->expects($this->once())
  162. ->method('createNamed')
  163. ->with('foo', 'Symfony\Component\Form\Extension\Core\Type\TextType')
  164. ->will($this->returnValue($child));
  165. $this->form->add('foo');
  166. $this->assertTrue($this->form->has('foo'));
  167. $this->assertSame($this->form, $child->getParent());
  168. $this->assertSame(array('foo' => $child), $this->form->all());
  169. }
  170. public function testAddUsingNameButNoType()
  171. {
  172. $this->form = $this->getBuilder('name', null, '\stdClass')
  173. ->setCompound(true)
  174. ->setDataMapper($this->getDataMapper())
  175. ->getForm();
  176. $child = $this->getBuilder('foo')->getForm();
  177. $this->factory->expects($this->once())
  178. ->method('createForProperty')
  179. ->with('\stdClass', 'foo')
  180. ->will($this->returnValue($child));
  181. $this->form->add('foo');
  182. $this->assertTrue($this->form->has('foo'));
  183. $this->assertSame($this->form, $child->getParent());
  184. $this->assertSame(array('foo' => $child), $this->form->all());
  185. }
  186. public function testAddUsingNameButNoTypeAndOptions()
  187. {
  188. $this->form = $this->getBuilder('name', null, '\stdClass')
  189. ->setCompound(true)
  190. ->setDataMapper($this->getDataMapper())
  191. ->getForm();
  192. $child = $this->getBuilder('foo')->getForm();
  193. $this->factory->expects($this->once())
  194. ->method('createForProperty')
  195. ->with('\stdClass', 'foo', null, array(
  196. 'bar' => 'baz',
  197. 'auto_initialize' => false,
  198. ))
  199. ->will($this->returnValue($child));
  200. $this->form->add('foo', null, array('bar' => 'baz'));
  201. $this->assertTrue($this->form->has('foo'));
  202. $this->assertSame($this->form, $child->getParent());
  203. $this->assertSame(array('foo' => $child), $this->form->all());
  204. }
  205. /**
  206. * @expectedException \Symfony\Component\Form\Exception\AlreadySubmittedException
  207. */
  208. public function testAddThrowsExceptionIfAlreadySubmitted()
  209. {
  210. $this->form->submit(array());
  211. $this->form->add($this->getBuilder('foo')->getForm());
  212. }
  213. public function testRemove()
  214. {
  215. $child = $this->getBuilder('foo')->getForm();
  216. $this->form->add($child);
  217. $this->form->remove('foo');
  218. $this->assertNull($child->getParent());
  219. $this->assertCount(0, $this->form);
  220. }
  221. /**
  222. * @expectedException \Symfony\Component\Form\Exception\AlreadySubmittedException
  223. */
  224. public function testRemoveThrowsExceptionIfAlreadySubmitted()
  225. {
  226. $this->form->add($this->getBuilder('foo')->setCompound(false)->getForm());
  227. $this->form->submit(array('foo' => 'bar'));
  228. $this->form->remove('foo');
  229. }
  230. public function testRemoveIgnoresUnknownName()
  231. {
  232. $this->form->remove('notexisting');
  233. $this->assertCount(0, $this->form);
  234. }
  235. public function testArrayAccess()
  236. {
  237. $child = $this->getBuilder('foo')->getForm();
  238. $this->form[] = $child;
  239. $this->assertArrayHasKey('foo', $this->form);
  240. $this->assertSame($child, $this->form['foo']);
  241. unset($this->form['foo']);
  242. $this->assertArrayNotHasKey('foo', $this->form);
  243. }
  244. public function testCountable()
  245. {
  246. $this->form->add($this->getBuilder('foo')->getForm());
  247. $this->form->add($this->getBuilder('bar')->getForm());
  248. $this->assertCount(2, $this->form);
  249. }
  250. public function testIterator()
  251. {
  252. $this->form->add($this->getBuilder('foo')->getForm());
  253. $this->form->add($this->getBuilder('bar')->getForm());
  254. $this->assertSame($this->form->all(), iterator_to_array($this->form));
  255. }
  256. public function testAddMapsViewDataToFormIfInitialized()
  257. {
  258. $test = $this;
  259. $mapper = $this->getDataMapper();
  260. $form = $this->getBuilder()
  261. ->setCompound(true)
  262. ->setDataMapper($mapper)
  263. ->addViewTransformer(new FixedDataTransformer(array(
  264. '' => '',
  265. 'foo' => 'bar',
  266. )))
  267. ->setData('foo')
  268. ->getForm();
  269. $child = $this->getBuilder()->getForm();
  270. $mapper->expects($this->once())
  271. ->method('mapDataToForms')
  272. ->with('bar', $this->isInstanceOf('\RecursiveIteratorIterator'))
  273. ->will($this->returnCallback(function ($data, \RecursiveIteratorIterator $iterator) use ($child, $test) {
  274. $test->assertInstanceOf('Symfony\Component\Form\Util\InheritDataAwareIterator', $iterator->getInnerIterator());
  275. $test->assertSame(array($child->getName() => $child), iterator_to_array($iterator));
  276. }));
  277. $form->initialize();
  278. $form->add($child);
  279. }
  280. public function testAddDoesNotMapViewDataToFormIfNotInitialized()
  281. {
  282. $mapper = $this->getDataMapper();
  283. $form = $this->getBuilder()
  284. ->setCompound(true)
  285. ->setDataMapper($mapper)
  286. ->getForm();
  287. $child = $this->getBuilder()->getForm();
  288. $mapper->expects($this->never())
  289. ->method('mapDataToForms');
  290. $form->add($child);
  291. }
  292. public function testAddDoesNotMapViewDataToFormIfInheritData()
  293. {
  294. $mapper = $this->getDataMapper();
  295. $form = $this->getBuilder()
  296. ->setCompound(true)
  297. ->setDataMapper($mapper)
  298. ->setInheritData(true)
  299. ->getForm();
  300. $child = $this->getBuilder()->getForm();
  301. $mapper->expects($this->never())
  302. ->method('mapDataToForms');
  303. $form->initialize();
  304. $form->add($child);
  305. }
  306. public function testSetDataSupportsDynamicAdditionAndRemovalOfChildren()
  307. {
  308. $form = $this->getBuilder()
  309. ->setCompound(true)
  310. // We test using PropertyPathMapper on purpose. The traversal logic
  311. // is currently contained in InheritDataAwareIterator, but even
  312. // if that changes, this test should still function.
  313. ->setDataMapper(new PropertyPathMapper())
  314. ->getForm();
  315. $child = $this->getMockForm('child');
  316. $childToBeRemoved = $this->getMockForm('removed');
  317. $childToBeAdded = $this->getMockForm('added');
  318. $form->add($child);
  319. $form->add($childToBeRemoved);
  320. $child->expects($this->once())
  321. ->method('setData')
  322. ->will($this->returnCallback(function () use ($form, $childToBeAdded) {
  323. $form->remove('removed');
  324. $form->add($childToBeAdded);
  325. }));
  326. $childToBeRemoved->expects($this->never())
  327. ->method('setData');
  328. // once when it it is created, once when it is added
  329. $childToBeAdded->expects($this->exactly(2))
  330. ->method('setData');
  331. // pass NULL to all children
  332. $form->setData(array());
  333. }
  334. public function testSetDataMapsViewDataToChildren()
  335. {
  336. $test = $this;
  337. $mapper = $this->getDataMapper();
  338. $form = $this->getBuilder()
  339. ->setCompound(true)
  340. ->setDataMapper($mapper)
  341. ->addViewTransformer(new FixedDataTransformer(array(
  342. '' => '',
  343. 'foo' => 'bar',
  344. )))
  345. ->getForm();
  346. $form->add($child1 = $this->getBuilder('firstName')->getForm());
  347. $form->add($child2 = $this->getBuilder('lastName')->getForm());
  348. $mapper->expects($this->once())
  349. ->method('mapDataToForms')
  350. ->with('bar', $this->isInstanceOf('\RecursiveIteratorIterator'))
  351. ->will($this->returnCallback(function ($data, \RecursiveIteratorIterator $iterator) use ($child1, $child2, $test) {
  352. $test->assertInstanceOf('Symfony\Component\Form\Util\InheritDataAwareIterator', $iterator->getInnerIterator());
  353. $test->assertSame(array('firstName' => $child1, 'lastName' => $child2), iterator_to_array($iterator));
  354. }));
  355. $form->setData('foo');
  356. }
  357. public function testSubmitSupportsDynamicAdditionAndRemovalOfChildren()
  358. {
  359. $child = $this->getMockForm('child');
  360. $childToBeRemoved = $this->getMockForm('removed');
  361. $childToBeAdded = $this->getMockForm('added');
  362. $this->form->add($child);
  363. $this->form->add($childToBeRemoved);
  364. $form = $this->form;
  365. $child->expects($this->once())
  366. ->method('submit')
  367. ->will($this->returnCallback(function () use ($form, $childToBeAdded) {
  368. $form->remove('removed');
  369. $form->add($childToBeAdded);
  370. }));
  371. $childToBeRemoved->expects($this->never())
  372. ->method('submit');
  373. $childToBeAdded->expects($this->once())
  374. ->method('submit');
  375. // pass NULL to all children
  376. $this->form->submit(array());
  377. }
  378. public function testSubmitMapsSubmittedChildrenOntoExistingViewData()
  379. {
  380. $test = $this;
  381. $mapper = $this->getDataMapper();
  382. $form = $this->getBuilder()
  383. ->setCompound(true)
  384. ->setDataMapper($mapper)
  385. ->addViewTransformer(new FixedDataTransformer(array(
  386. '' => '',
  387. 'foo' => 'bar',
  388. )))
  389. ->setData('foo')
  390. ->getForm();
  391. $form->add($child1 = $this->getBuilder('firstName')->setCompound(false)->getForm());
  392. $form->add($child2 = $this->getBuilder('lastName')->setCompound(false)->getForm());
  393. $mapper->expects($this->once())
  394. ->method('mapFormsToData')
  395. ->with($this->isInstanceOf('\RecursiveIteratorIterator'), 'bar')
  396. ->will($this->returnCallback(function (\RecursiveIteratorIterator $iterator) use ($child1, $child2, $test) {
  397. $test->assertInstanceOf('Symfony\Component\Form\Util\InheritDataAwareIterator', $iterator->getInnerIterator());
  398. $test->assertSame(array('firstName' => $child1, 'lastName' => $child2), iterator_to_array($iterator));
  399. $test->assertEquals('Bernhard', $child1->getData());
  400. $test->assertEquals('Schussek', $child2->getData());
  401. }));
  402. $form->submit(array(
  403. 'firstName' => 'Bernhard',
  404. 'lastName' => 'Schussek',
  405. ));
  406. }
  407. public function testMapFormsToDataIsNotInvokedIfInheritData()
  408. {
  409. $mapper = $this->getDataMapper();
  410. $form = $this->getBuilder()
  411. ->setCompound(true)
  412. ->setDataMapper($mapper)
  413. ->setInheritData(true)
  414. ->addViewTransformer(new FixedDataTransformer(array(
  415. '' => '',
  416. 'foo' => 'bar',
  417. )))
  418. ->getForm();
  419. $form->add($child1 = $this->getBuilder('firstName')->setCompound(false)->getForm());
  420. $form->add($child2 = $this->getBuilder('lastName')->setCompound(false)->getForm());
  421. $mapper->expects($this->never())
  422. ->method('mapFormsToData');
  423. $form->submit(array(
  424. 'firstName' => 'Bernhard',
  425. 'lastName' => 'Schussek',
  426. ));
  427. }
  428. /*
  429. * https://github.com/symfony/symfony/issues/4480
  430. */
  431. public function testSubmitRestoresViewDataIfCompoundAndEmpty()
  432. {
  433. $mapper = $this->getDataMapper();
  434. $object = new \stdClass();
  435. $form = $this->getBuilder('name', null, 'stdClass')
  436. ->setCompound(true)
  437. ->setDataMapper($mapper)
  438. ->setData($object)
  439. ->getForm();
  440. $form->submit(array());
  441. $this->assertSame($object, $form->getData());
  442. }
  443. public function testSubmitMapsSubmittedChildrenOntoEmptyData()
  444. {
  445. $test = $this;
  446. $mapper = $this->getDataMapper();
  447. $object = new \stdClass();
  448. $form = $this->getBuilder()
  449. ->setCompound(true)
  450. ->setDataMapper($mapper)
  451. ->setEmptyData($object)
  452. ->setData(null)
  453. ->getForm();
  454. $form->add($child = $this->getBuilder('name')->setCompound(false)->getForm());
  455. $mapper->expects($this->once())
  456. ->method('mapFormsToData')
  457. ->with($this->isInstanceOf('\RecursiveIteratorIterator'), $object)
  458. ->will($this->returnCallback(function (\RecursiveIteratorIterator $iterator) use ($child, $test) {
  459. $test->assertInstanceOf('Symfony\Component\Form\Util\InheritDataAwareIterator', $iterator->getInnerIterator());
  460. $test->assertSame(array('name' => $child), iterator_to_array($iterator));
  461. }));
  462. $form->submit(array(
  463. 'name' => 'Bernhard',
  464. ));
  465. }
  466. public function requestMethodProvider()
  467. {
  468. return array(
  469. array('POST'),
  470. array('PUT'),
  471. array('DELETE'),
  472. array('PATCH'),
  473. );
  474. }
  475. /**
  476. * @dataProvider requestMethodProvider
  477. */
  478. public function testSubmitPostOrPutRequest($method)
  479. {
  480. $path = tempnam(sys_get_temp_dir(), 'sf2');
  481. touch($path);
  482. $values = array(
  483. 'author' => array(
  484. 'name' => 'Bernhard',
  485. 'image' => array('filename' => 'foobar.png'),
  486. ),
  487. );
  488. $files = array(
  489. 'author' => array(
  490. 'error' => array('image' => UPLOAD_ERR_OK),
  491. 'name' => array('image' => 'upload.png'),
  492. 'size' => array('image' => 123),
  493. 'tmp_name' => array('image' => $path),
  494. 'type' => array('image' => 'image/png'),
  495. ),
  496. );
  497. $request = new Request(array(), $values, array(), array(), $files, array(
  498. 'REQUEST_METHOD' => $method,
  499. ));
  500. $form = $this->getBuilder('author')
  501. ->setMethod($method)
  502. ->setCompound(true)
  503. ->setDataMapper($this->getDataMapper())
  504. ->setRequestHandler(new HttpFoundationRequestHandler())
  505. ->getForm();
  506. $form->add($this->getBuilder('name')->getForm());
  507. $form->add($this->getBuilder('image')->getForm());
  508. $form->handleRequest($request);
  509. $file = new UploadedFile($path, 'upload.png', 'image/png', 123, UPLOAD_ERR_OK);
  510. $this->assertEquals('Bernhard', $form['name']->getData());
  511. $this->assertEquals($file, $form['image']->getData());
  512. unlink($path);
  513. }
  514. /**
  515. * @dataProvider requestMethodProvider
  516. */
  517. public function testSubmitPostOrPutRequestWithEmptyRootFormName($method)
  518. {
  519. $path = tempnam(sys_get_temp_dir(), 'sf2');
  520. touch($path);
  521. $values = array(
  522. 'name' => 'Bernhard',
  523. 'extra' => 'data',
  524. );
  525. $files = array(
  526. 'image' => array(
  527. 'error' => UPLOAD_ERR_OK,
  528. 'name' => 'upload.png',
  529. 'size' => 123,
  530. 'tmp_name' => $path,
  531. 'type' => 'image/png',
  532. ),
  533. );
  534. $request = new Request(array(), $values, array(), array(), $files, array(
  535. 'REQUEST_METHOD' => $method,
  536. ));
  537. $form = $this->getBuilder('')
  538. ->setMethod($method)
  539. ->setCompound(true)
  540. ->setDataMapper($this->getDataMapper())
  541. ->setRequestHandler(new HttpFoundationRequestHandler())
  542. ->getForm();
  543. $form->add($this->getBuilder('name')->getForm());
  544. $form->add($this->getBuilder('image')->getForm());
  545. $form->handleRequest($request);
  546. $file = new UploadedFile($path, 'upload.png', 'image/png', 123, UPLOAD_ERR_OK);
  547. $this->assertEquals('Bernhard', $form['name']->getData());
  548. $this->assertEquals($file, $form['image']->getData());
  549. $this->assertEquals(array('extra' => 'data'), $form->getExtraData());
  550. unlink($path);
  551. }
  552. /**
  553. * @dataProvider requestMethodProvider
  554. */
  555. public function testSubmitPostOrPutRequestWithSingleChildForm($method)
  556. {
  557. $path = tempnam(sys_get_temp_dir(), 'sf2');
  558. touch($path);
  559. $files = array(
  560. 'image' => array(
  561. 'error' => UPLOAD_ERR_OK,
  562. 'name' => 'upload.png',
  563. 'size' => 123,
  564. 'tmp_name' => $path,
  565. 'type' => 'image/png',
  566. ),
  567. );
  568. $request = new Request(array(), array(), array(), array(), $files, array(
  569. 'REQUEST_METHOD' => $method,
  570. ));
  571. $form = $this->getBuilder('image', null, null, array('allow_file_upload' => true))
  572. ->setMethod($method)
  573. ->setRequestHandler(new HttpFoundationRequestHandler())
  574. ->getForm();
  575. $form->handleRequest($request);
  576. $file = new UploadedFile($path, 'upload.png', 'image/png', 123, UPLOAD_ERR_OK);
  577. $this->assertEquals($file, $form->getData());
  578. unlink($path);
  579. }
  580. /**
  581. * @dataProvider requestMethodProvider
  582. */
  583. public function testSubmitPostOrPutRequestWithSingleChildFormUploadedFile($method)
  584. {
  585. $path = tempnam(sys_get_temp_dir(), 'sf2');
  586. touch($path);
  587. $values = array(
  588. 'name' => 'Bernhard',
  589. );
  590. $request = new Request(array(), $values, array(), array(), array(), array(
  591. 'REQUEST_METHOD' => $method,
  592. ));
  593. $form = $this->getBuilder('name')
  594. ->setMethod($method)
  595. ->setRequestHandler(new HttpFoundationRequestHandler())
  596. ->getForm();
  597. $form->handleRequest($request);
  598. $this->assertEquals('Bernhard', $form->getData());
  599. unlink($path);
  600. }
  601. public function testSubmitGetRequest()
  602. {
  603. $values = array(
  604. 'author' => array(
  605. 'firstName' => 'Bernhard',
  606. 'lastName' => 'Schussek',
  607. ),
  608. );
  609. $request = new Request($values, array(), array(), array(), array(), array(
  610. 'REQUEST_METHOD' => 'GET',
  611. ));
  612. $form = $this->getBuilder('author')
  613. ->setMethod('GET')
  614. ->setCompound(true)
  615. ->setDataMapper($this->getDataMapper())
  616. ->setRequestHandler(new HttpFoundationRequestHandler())
  617. ->getForm();
  618. $form->add($this->getBuilder('firstName')->getForm());
  619. $form->add($this->getBuilder('lastName')->getForm());
  620. $form->handleRequest($request);
  621. $this->assertEquals('Bernhard', $form['firstName']->getData());
  622. $this->assertEquals('Schussek', $form['lastName']->getData());
  623. }
  624. public function testSubmitGetRequestWithEmptyRootFormName()
  625. {
  626. $values = array(
  627. 'firstName' => 'Bernhard',
  628. 'lastName' => 'Schussek',
  629. 'extra' => 'data',
  630. );
  631. $request = new Request($values, array(), array(), array(), array(), array(
  632. 'REQUEST_METHOD' => 'GET',
  633. ));
  634. $form = $this->getBuilder('')
  635. ->setMethod('GET')
  636. ->setCompound(true)
  637. ->setDataMapper($this->getDataMapper())
  638. ->setRequestHandler(new HttpFoundationRequestHandler())
  639. ->getForm();
  640. $form->add($this->getBuilder('firstName')->getForm());
  641. $form->add($this->getBuilder('lastName')->getForm());
  642. $form->handleRequest($request);
  643. $this->assertEquals('Bernhard', $form['firstName']->getData());
  644. $this->assertEquals('Schussek', $form['lastName']->getData());
  645. $this->assertEquals(array('extra' => 'data'), $form->getExtraData());
  646. }
  647. /**
  648. * @group legacy
  649. */
  650. public function testGetErrorsAsStringDeep()
  651. {
  652. $parent = $this->getBuilder()
  653. ->setCompound(true)
  654. ->setDataMapper($this->getDataMapper())
  655. ->getForm();
  656. $this->form->addError(new FormError('Error!'));
  657. $parent->add($this->form);
  658. $parent->add($this->getBuilder('foo')->getForm());
  659. $this->assertSame(
  660. "name:\n".
  661. " ERROR: Error!\n",
  662. $parent->getErrorsAsString()
  663. );
  664. }
  665. /**
  666. * @group legacy
  667. */
  668. public function testGetErrorsAsStringDeepWithIndentation()
  669. {
  670. $parent = $this->getBuilder()
  671. ->setCompound(true)
  672. ->setDataMapper($this->getDataMapper())
  673. ->getForm();
  674. $this->form->addError(new FormError('Error!'));
  675. $parent->add($this->form);
  676. $parent->add($this->getBuilder('foo')->getForm());
  677. $this->assertSame(
  678. " name:\n".
  679. " ERROR: Error!\n",
  680. $parent->getErrorsAsString(4)
  681. );
  682. }
  683. public function testGetErrors()
  684. {
  685. $this->form->addError($error1 = new FormError('Error 1'));
  686. $this->form->addError($error2 = new FormError('Error 2'));
  687. $errors = $this->form->getErrors();
  688. $this->assertSame(
  689. "ERROR: Error 1\n".
  690. "ERROR: Error 2\n",
  691. (string) $errors
  692. );
  693. $this->assertSame(array($error1, $error2), iterator_to_array($errors));
  694. }
  695. public function testGetErrorsDeep()
  696. {
  697. $this->form->addError($error1 = new FormError('Error 1'));
  698. $this->form->addError($error2 = new FormError('Error 2'));
  699. $childForm = $this->getBuilder('Child')->getForm();
  700. $childForm->addError($nestedError = new FormError('Nested Error'));
  701. $this->form->add($childForm);
  702. $errors = $this->form->getErrors(true);
  703. $this->assertSame(
  704. "ERROR: Error 1\n".
  705. "ERROR: Error 2\n".
  706. "ERROR: Nested Error\n",
  707. (string) $errors
  708. );
  709. $this->assertSame(
  710. array($error1, $error2, $nestedError),
  711. iterator_to_array($errors)
  712. );
  713. }
  714. public function testGetErrorsDeepRecursive()
  715. {
  716. $this->form->addError($error1 = new FormError('Error 1'));
  717. $this->form->addError($error2 = new FormError('Error 2'));
  718. $childForm = $this->getBuilder('Child')->getForm();
  719. $childForm->addError($nestedError = new FormError('Nested Error'));
  720. $this->form->add($childForm);
  721. $errors = $this->form->getErrors(true, false);
  722. $this->assertSame(
  723. "ERROR: Error 1\n".
  724. "ERROR: Error 2\n".
  725. "Child:\n".
  726. " ERROR: Nested Error\n",
  727. (string) $errors
  728. );
  729. $errorsAsArray = iterator_to_array($errors);
  730. $this->assertSame($error1, $errorsAsArray[0]);
  731. $this->assertSame($error2, $errorsAsArray[1]);
  732. $this->assertInstanceOf('Symfony\Component\Form\FormErrorIterator', $errorsAsArray[2]);
  733. $nestedErrorsAsArray = iterator_to_array($errorsAsArray[2]);
  734. $this->assertCount(1, $nestedErrorsAsArray);
  735. $this->assertSame($nestedError, $nestedErrorsAsArray[0]);
  736. }
  737. // Basic cases are covered in SimpleFormTest
  738. public function testCreateViewWithChildren()
  739. {
  740. $type = $this->getMockBuilder('Symfony\Component\Form\ResolvedFormTypeInterface')->getMock();
  741. $options = array('a' => 'Foo', 'b' => 'Bar');
  742. $field1 = $this->getMockForm('foo');
  743. $field2 = $this->getMockForm('bar');
  744. $view = new FormView();
  745. $field1View = new FormView();
  746. $field2View = new FormView();
  747. $this->form = $this->getBuilder('form', null, null, $options)
  748. ->setCompound(true)
  749. ->setDataMapper($this->getDataMapper())
  750. ->setType($type)
  751. ->getForm();
  752. $this->form->add($field1);
  753. $this->form->add($field2);
  754. $test = $this;
  755. $assertChildViewsEqual = function (array $childViews) use ($test) {
  756. return function (FormView $view) use ($test, $childViews) {
  757. /* @var TestCase $test */
  758. $test->assertSame($childViews, $view->children);
  759. };
  760. };
  761. // First create the view
  762. $type->expects($this->once())
  763. ->method('createView')
  764. ->will($this->returnValue($view));
  765. // Then build it for the form itself
  766. $type->expects($this->once())
  767. ->method('buildView')
  768. ->with($view, $this->form, $options)
  769. ->will($this->returnCallback($assertChildViewsEqual(array())));
  770. // Then add the first child form
  771. $field1->expects($this->once())
  772. ->method('createView')
  773. ->will($this->returnValue($field1View));
  774. // Then the second child form
  775. $field2->expects($this->once())
  776. ->method('createView')
  777. ->will($this->returnValue($field2View));
  778. // Again build the view for the form itself. This time the child views
  779. // exist.
  780. $type->expects($this->once())
  781. ->method('finishView')
  782. ->with($view, $this->form, $options)
  783. ->will($this->returnCallback($assertChildViewsEqual(array('foo' => $field1View, 'bar' => $field2View))));
  784. $this->assertSame($view, $this->form->createView());
  785. }
  786. public function testNoClickedButtonBeforeSubmission()
  787. {
  788. $this->assertNull($this->form->getClickedButton());
  789. }
  790. public function testNoClickedButton()
  791. {
  792. $button = $this->getMockBuilder('Symfony\Component\Form\SubmitButton')
  793. ->setConstructorArgs(array(new SubmitButtonBuilder('submit')))
  794. ->setMethods(array('isClicked'))
  795. ->getMock();
  796. $button->expects($this->any())
  797. ->method('isClicked')
  798. ->will($this->returnValue(false));
  799. $parentForm = $this->getBuilder('parent')->getForm();
  800. $nestedForm = $this->getBuilder('nested')->getForm();
  801. $this->form->setParent($parentForm);
  802. $this->form->add($button);
  803. $this->form->add($nestedForm);
  804. $this->form->submit(array());
  805. $this->assertNull($this->form->getClickedButton());
  806. }
  807. public function testClickedButton()
  808. {
  809. $button = $this->getMockBuilder('Symfony\Component\Form\SubmitButton')
  810. ->setConstructorArgs(array(new SubmitButtonBuilder('submit')))
  811. ->setMethods(array('isClicked'))
  812. ->getMock();
  813. $button->expects($this->any())
  814. ->method('isClicked')
  815. ->will($this->returnValue(true));
  816. $this->form->add($button);
  817. $this->form->submit(array());
  818. $this->assertSame($button, $this->form->getClickedButton());
  819. }
  820. public function testClickedButtonFromNestedForm()
  821. {
  822. $button = $this->getBuilder('submit')->getForm();
  823. $nestedForm = $this->getMockBuilder('Symfony\Component\Form\Form')
  824. ->setConstructorArgs(array($this->getBuilder('nested')))
  825. ->setMethods(array('getClickedButton'))
  826. ->getMock();
  827. $nestedForm->expects($this->any())
  828. ->method('getClickedButton')
  829. ->will($this->returnValue($button));
  830. $this->form->add($nestedForm);
  831. $this->form->submit(array());
  832. $this->assertSame($button, $this->form->getClickedButton());
  833. }
  834. public function testClickedButtonFromParentForm()
  835. {
  836. $button = $this->getBuilder('submit')->getForm();
  837. $parentForm = $this->getMockBuilder('Symfony\Component\Form\Form')
  838. ->setConstructorArgs(array($this->getBuilder('parent')))
  839. ->setMethods(array('getClickedButton'))
  840. ->getMock();
  841. $parentForm->expects($this->any())
  842. ->method('getClickedButton')
  843. ->will($this->returnValue($button));
  844. $this->form->setParent($parentForm);
  845. $this->form->submit(array());
  846. $this->assertSame($button, $this->form->getClickedButton());
  847. }
  848. public function testDisabledButtonIsNotSubmitted()
  849. {
  850. $button = new SubmitButtonBuilder('submit');
  851. $submit = $button
  852. ->setDisabled(true)
  853. ->getForm();
  854. $form = $this->createForm()
  855. ->add($this->getBuilder('text')->getForm())
  856. ->add($submit)
  857. ;
  858. $form->submit(array(
  859. 'text' => '',
  860. 'submit' => '',
  861. ));
  862. $this->assertTrue($submit->isDisabled());
  863. $this->assertFalse($submit->isClicked());
  864. $this->assertFalse($submit->isSubmitted());
  865. }
  866. public function testFileUpload()
  867. {
  868. $reqHandler = new HttpFoundationRequestHandler();
  869. $this->form->add($this->getBuilder('foo')->setRequestHandler($reqHandler)->getForm());
  870. $this->form->add($this->getBuilder('bar')->setRequestHandler($reqHandler)->getForm());
  871. $this->form->submit(array(
  872. 'foo' => 'Foo',
  873. 'bar' => new UploadedFile(__FILE__, 'upload.png', 'image/png', 123, UPLOAD_ERR_OK),
  874. ));
  875. $this->assertSame('Submitted data was expected to be text or number, file upload given.', $this->form->get('bar')->getTransformationFailure()->getMessage());
  876. $this->assertNull($this->form->get('bar')->getData());
  877. }
  878. protected function createForm()
  879. {
  880. return $this->getBuilder()
  881. ->setCompound(true)
  882. ->setDataMapper($this->getDataMapper())
  883. ->getForm();
  884. }
  885. }