AbstractLayoutTest.php 51 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792
  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\Tests\Component\Form;
  11. use Symfony\Component\Form\FormError;
  12. use Symfony\Component\Form\FormView;
  13. use Symfony\Component\Form\FormFactory;
  14. use Symfony\Component\Form\Extension\Core\CoreExtension;
  15. use Symfony\Component\Form\Extension\Csrf\CsrfExtension;
  16. abstract class AbstractLayoutTest extends \PHPUnit_Framework_TestCase
  17. {
  18. protected $csrfProvider;
  19. protected $factory;
  20. protected function setUp()
  21. {
  22. if (!extension_loaded('intl')) {
  23. $this->markTestSkipped('The "intl" extension is not available');
  24. }
  25. \Locale::setDefault('en');
  26. $this->csrfProvider = $this->getMock('Symfony\Component\Form\Extension\Csrf\CsrfProvider\CsrfProviderInterface');
  27. $this->factory = new FormFactory(array(
  28. new CoreExtension(),
  29. new CsrfExtension($this->csrfProvider),
  30. ));
  31. }
  32. protected function tearDown()
  33. {
  34. $this->csrfProvider = null;
  35. $this->factory = null;
  36. }
  37. protected function assertXpathNodeValue(\DomElement $element, $expression, $nodeValue)
  38. {
  39. $xpath = new \DOMXPath($element->ownerDocument);
  40. $nodeList = $xpath->evaluate($expression);
  41. $this->assertEquals(1, $nodeList->length);
  42. $this->assertEquals($nodeValue, $nodeList->item(0)->nodeValue);
  43. }
  44. protected function assertMatchesXpath($html, $expression, $count = 1)
  45. {
  46. $dom = new \DomDocument('UTF-8');
  47. try {
  48. // Wrap in <root> node so we can load HTML with multiple tags at
  49. // the top level
  50. $dom->loadXml('<root>'.$html.'</root>');
  51. } catch (\Exception $e) {
  52. return $this->fail(sprintf(
  53. "Failed loading HTML:\n\n%s\n\nError: %s",
  54. $html,
  55. $e->getMessage()
  56. ));
  57. }
  58. $xpath = new \DOMXPath($dom);
  59. $nodeList = $xpath->evaluate('/root'.$expression);
  60. if ($nodeList->length != $count) {
  61. $dom->formatOutput = true;
  62. $this->fail(sprintf(
  63. "Failed asserting that \n\n%s\n\nmatches exactly %s. Matches %s in \n\n%s",
  64. $expression,
  65. $count == 1 ? 'once' : $count . ' times',
  66. $nodeList->length == 1 ? 'once' : $nodeList->length . ' times',
  67. // strip away <root> and </root>
  68. substr($dom->saveHTML(), 6, -8)
  69. ));
  70. }
  71. }
  72. protected function assertWidgetMatchesXpath(FormView $view, array $vars, $xpath)
  73. {
  74. // include ampersands everywhere to validate escaping
  75. $html = $this->renderWidget($view, array_merge(array(
  76. 'id' => 'my&id',
  77. 'attr' => array('class' => 'my&class'),
  78. ), $vars));
  79. $xpath = trim($xpath).'
  80. [@id="my&id"]
  81. [@class="my&class"]';
  82. $this->assertMatchesXpath($html, $xpath);
  83. }
  84. abstract protected function renderEnctype(FormView $view);
  85. abstract protected function renderLabel(FormView $view, $label = null, array $vars = array());
  86. abstract protected function renderErrors(FormView $view);
  87. abstract protected function renderWidget(FormView $view, array $vars = array());
  88. abstract protected function renderRow(FormView $view, array $vars = array());
  89. abstract protected function renderRest(FormView $view, array $vars = array());
  90. abstract protected function setTheme(FormView $view, array $themes);
  91. public function testEnctype()
  92. {
  93. $form = $this->factory->createNamedBuilder('form', 'na&me', null, array(
  94. 'property_path' => 'name',
  95. ))
  96. ->add('file', 'file')
  97. ->getForm();
  98. $this->assertEquals('enctype="multipart/form-data"', $this->renderEnctype($form->createView()));
  99. }
  100. public function testNoEnctype()
  101. {
  102. $form = $this->factory->createNamedBuilder('form', 'na&me', null, array(
  103. 'property_path' => 'name',
  104. ))
  105. ->add('text', 'text')
  106. ->getForm();
  107. $this->assertEquals('', $this->renderEnctype($form->createView()));
  108. }
  109. public function testLabel()
  110. {
  111. $form = $this->factory->createNamed('text', 'na&me', null, array(
  112. 'property_path' => 'name',
  113. ));
  114. $view = $form->createView();
  115. $this->renderWidget($view, array('label' => 'foo'));
  116. $html = $this->renderLabel($view);
  117. $this->assertMatchesXpath($html,
  118. '/label
  119. [@for="na&me"]
  120. [.="[trans]Na&me[/trans]"]
  121. '
  122. );
  123. }
  124. public function testLabelOnForm()
  125. {
  126. $form = $this->factory->createNamed('date', 'na&me', null, array(
  127. 'property_path' => 'name',
  128. ));
  129. $view = $form->createView();
  130. $this->renderWidget($view, array('label' => 'foo'));
  131. $html = $this->renderLabel($view);
  132. $this->assertMatchesXpath($html,
  133. '/label
  134. [@class=" required"]
  135. [.="[trans]Na&me[/trans]"]
  136. '
  137. );
  138. }
  139. public function testLabelWithCustomTextPassedAsOption()
  140. {
  141. $form = $this->factory->createNamed('text', 'na&me', null, array(
  142. 'property_path' => 'name',
  143. 'label' => 'Custom label',
  144. ));
  145. $html = $this->renderLabel($form->createView());
  146. $this->assertMatchesXpath($html,
  147. '/label
  148. [@for="na&me"]
  149. [.="[trans]Custom label[/trans]"]
  150. '
  151. );
  152. }
  153. public function testLabelWithCustomTextPassedDirectly()
  154. {
  155. $form = $this->factory->createNamed('text', 'na&me', null, array(
  156. 'property_path' => 'name',
  157. ));
  158. $html = $this->renderLabel($form->createView(), 'Custom label');
  159. $this->assertMatchesXpath($html,
  160. '/label
  161. [@for="na&me"]
  162. [.="[trans]Custom label[/trans]"]
  163. '
  164. );
  165. }
  166. public function testLabelWithCustomTextPassedAsOptionAndDirectly()
  167. {
  168. $form = $this->factory->createNamed('text', 'na&me', null, array(
  169. 'property_path' => 'name',
  170. 'label' => 'Custom label',
  171. ));
  172. $html = $this->renderLabel($form->createView(), 'Overridden label');
  173. $this->assertMatchesXpath($html,
  174. '/label
  175. [@for="na&me"]
  176. [.="[trans]Overridden label[/trans]"]
  177. '
  178. );
  179. }
  180. public function testLabelWithCustomOptionsPassedDirectly()
  181. {
  182. $form = $this->factory->createNamed('text', 'na&me', null, array(
  183. 'property_path' => 'name',
  184. ));
  185. $html = $this->renderLabel($form->createView(), null, array(
  186. 'attr' => array(
  187. 'class' => 'my&class'
  188. ),
  189. ));
  190. $this->assertMatchesXpath($html,
  191. '/label
  192. [@for="na&me"]
  193. [@class="my&class required"]
  194. '
  195. );
  196. }
  197. public function testLabelWithCustomTextAndCustomOptionsPassedDirectly()
  198. {
  199. $form = $this->factory->createNamed('text', 'na&me', null, array(
  200. 'property_path' => 'name',
  201. ));
  202. $html = $this->renderLabel($form->createView(), 'Custom label', array(
  203. 'attr' => array(
  204. 'class' => 'my&class'
  205. ),
  206. ));
  207. $this->assertMatchesXpath($html,
  208. '/label
  209. [@for="na&me"]
  210. [@class="my&class required"]
  211. [.="[trans]Custom label[/trans]"]
  212. '
  213. );
  214. }
  215. public function testErrors()
  216. {
  217. $form = $this->factory->createNamed('text', 'na&me', null, array(
  218. 'property_path' => 'name',
  219. ));
  220. $form->addError(new FormError('Error 1'));
  221. $form->addError(new FormError('Error 2'));
  222. $view = $form->createView();
  223. $html = $this->renderErrors($view);
  224. $this->assertMatchesXpath($html,
  225. '/ul
  226. [
  227. ./li[.="[trans]Error 1[/trans]"]
  228. /following-sibling::li[.="[trans]Error 2[/trans]"]
  229. ]
  230. [count(./li)=2]
  231. '
  232. );
  233. }
  234. public function testWidgetById()
  235. {
  236. $form = $this->factory->createNamed('text', 'text_id');
  237. $html = $this->renderWidget($form->createView());
  238. $this->assertMatchesXpath($html,
  239. '/div
  240. [
  241. ./input
  242. [@type="text"]
  243. [@id="text_id"]
  244. ]
  245. [@id="container"]
  246. '
  247. );
  248. }
  249. public function testCheckedCheckbox()
  250. {
  251. $form = $this->factory->createNamed('checkbox', 'na&me', true, array(
  252. 'property_path' => 'name',
  253. ));
  254. $this->assertWidgetMatchesXpath($form->createView(), array(),
  255. '/input
  256. [@type="checkbox"]
  257. [@name="na&me"]
  258. [@checked="checked"]
  259. [@value="1"]
  260. '
  261. );
  262. }
  263. public function testCheckedCheckboxWithValue()
  264. {
  265. $form = $this->factory->createNamed('checkbox', 'na&me', true, array(
  266. 'property_path' => 'name',
  267. 'value' => 'foo&bar',
  268. ));
  269. $this->assertWidgetMatchesXpath($form->createView(), array(),
  270. '/input
  271. [@type="checkbox"]
  272. [@name="na&me"]
  273. [@checked="checked"]
  274. [@value="foo&bar"]
  275. '
  276. );
  277. }
  278. public function testUncheckedCheckbox()
  279. {
  280. $form = $this->factory->createNamed('checkbox', 'na&me', false, array(
  281. 'property_path' => 'name',
  282. ));
  283. $this->assertWidgetMatchesXpath($form->createView(), array(),
  284. '/input
  285. [@type="checkbox"]
  286. [@name="na&me"]
  287. [not(@checked)]
  288. '
  289. );
  290. }
  291. public function testSingleChoice()
  292. {
  293. $form = $this->factory->createNamed('choice', 'na&me', '&a', array(
  294. 'property_path' => 'name',
  295. 'choices' => array('&a' => 'Choice&A', '&b' => 'Choice&B'),
  296. 'multiple' => false,
  297. 'expanded' => false,
  298. ));
  299. $this->assertWidgetMatchesXpath($form->createView(), array(),
  300. '/select
  301. [@name="na&me"]
  302. [@required="required"]
  303. [
  304. ./option[@value="&a"][@selected="selected"][.="[trans]Choice&A[/trans]"]
  305. /following-sibling::option[@value="&b"][not(@selected)][.="[trans]Choice&B[/trans]"]
  306. ]
  307. [count(./option)=2]
  308. '
  309. );
  310. }
  311. public function testSingleChoiceWithPreferred()
  312. {
  313. $form = $this->factory->createNamed('choice', 'na&me', '&a', array(
  314. 'property_path' => 'name',
  315. 'choices' => array('&a' => 'Choice&A', '&b' => 'Choice&B'),
  316. 'preferred_choices' => array('&b'),
  317. 'multiple' => false,
  318. 'expanded' => false,
  319. ));
  320. $this->assertWidgetMatchesXpath($form->createView(), array('separator' => '-- sep --'),
  321. '/select
  322. [@name="na&me"]
  323. [@required="required"]
  324. [
  325. ./option[@value="&b"][not(@selected)][.="[trans]Choice&B[/trans]"]
  326. /following-sibling::option[@disabled="disabled"][not(@selected)][.="-- sep --"]
  327. /following-sibling::option[@value="&a"][@selected="selected"][.="[trans]Choice&A[/trans]"]
  328. ]
  329. [count(./option)=3]
  330. '
  331. );
  332. }
  333. public function testSingleChoiceWithPreferredAndNoSeparator()
  334. {
  335. $form = $this->factory->createNamed('choice', 'na&me', '&a', array(
  336. 'property_path' => 'name',
  337. 'choices' => array('&a' => 'Choice&A', '&b' => 'Choice&B'),
  338. 'preferred_choices' => array('&b'),
  339. 'multiple' => false,
  340. 'expanded' => false,
  341. ));
  342. $this->assertWidgetMatchesXpath($form->createView(), array('separator' => null),
  343. '/select
  344. [@name="na&me"]
  345. [@required="required"]
  346. [
  347. ./option[@value="&b"][not(@selected)][.="[trans]Choice&B[/trans]"]
  348. /following-sibling::option[@value="&a"][@selected="selected"][.="[trans]Choice&A[/trans]"]
  349. ]
  350. [count(./option)=2]
  351. '
  352. );
  353. }
  354. public function testSingleChoiceWithPreferredAndBlankSeparator()
  355. {
  356. $form = $this->factory->createNamed('choice', 'na&me', '&a', array(
  357. 'property_path' => 'name',
  358. 'choices' => array('&a' => 'Choice&A', '&b' => 'Choice&B'),
  359. 'preferred_choices' => array('&b'),
  360. 'multiple' => false,
  361. 'expanded' => false,
  362. ));
  363. $this->assertWidgetMatchesXpath($form->createView(), array('separator' => ''),
  364. '/select
  365. [@name="na&me"]
  366. [@required="required"]
  367. [
  368. ./option[@value="&b"][not(@selected)][.="[trans]Choice&B[/trans]"]
  369. /following-sibling::option[@disabled="disabled"][not(@selected)][.=""]
  370. /following-sibling::option[@value="&a"][@selected="selected"][.="[trans]Choice&A[/trans]"]
  371. ]
  372. [count(./option)=3]
  373. '
  374. );
  375. }
  376. public function testChoiceWithOnlyPreferred()
  377. {
  378. $form = $this->factory->createNamed('choice', 'na&me', '&a', array(
  379. 'property_path' => 'name',
  380. 'choices' => array('&a' => 'Choice&A', '&b' => 'Choice&B'),
  381. 'preferred_choices' => array('&a', '&b'),
  382. 'multiple' => false,
  383. 'expanded' => false,
  384. ));
  385. $this->assertWidgetMatchesXpath($form->createView(), array(),
  386. '/select
  387. [count(./option)=2]
  388. '
  389. );
  390. }
  391. public function testSingleChoiceNonRequired()
  392. {
  393. $form = $this->factory->createNamed('choice', 'na&me', '&a', array(
  394. 'property_path' => 'name',
  395. 'choices' => array('&a' => 'Choice&A', '&b' => 'Choice&B'),
  396. 'required' => false,
  397. 'multiple' => false,
  398. 'expanded' => false,
  399. ));
  400. $this->assertWidgetMatchesXpath($form->createView(), array(),
  401. '/select
  402. [@name="na&me"]
  403. [not(@required)]
  404. [
  405. ./option[@value=""][.="[trans][/trans]"]
  406. /following-sibling::option[@value="&a"][@selected="selected"][.="[trans]Choice&A[/trans]"]
  407. /following-sibling::option[@value="&b"][not(@selected)][.="[trans]Choice&B[/trans]"]
  408. ]
  409. [count(./option)=3]
  410. '
  411. );
  412. }
  413. public function testSingleChoiceNonRequiredNoneSelected()
  414. {
  415. $form = $this->factory->createNamed('choice', 'na&me', null, array(
  416. 'property_path' => 'name',
  417. 'choices' => array('&a' => 'Choice&A', '&b' => 'Choice&B'),
  418. 'required' => false,
  419. 'multiple' => false,
  420. 'expanded' => false,
  421. ));
  422. $this->assertWidgetMatchesXpath($form->createView(), array(),
  423. '/select
  424. [@name="na&me"]
  425. [not(@required)]
  426. [
  427. ./option[@value=""][.="[trans][/trans]"]
  428. /following-sibling::option[@value="&a"][not(@selected)][.="[trans]Choice&A[/trans]"]
  429. /following-sibling::option[@value="&b"][not(@selected)][.="[trans]Choice&B[/trans]"]
  430. ]
  431. [count(./option)=3]
  432. '
  433. );
  434. }
  435. public function testSingleChoiceWithNonRequiredEmptyValue()
  436. {
  437. $form = $this->factory->createNamed('choice', 'na&me', '&a', array(
  438. 'property_path' => 'name',
  439. 'choices' => array('&a' => 'Choice&A', '&b' => 'Choice&B'),
  440. 'multiple' => false,
  441. 'expanded' => false,
  442. 'required' => false,
  443. 'empty_value' => 'Select&Anything&Not&Me',
  444. ));
  445. $this->assertWidgetMatchesXpath($form->createView(), array(),
  446. '/select
  447. [@name="na&me"]
  448. [not(@required)]
  449. [
  450. ./option[@value=""][not(@selected)][.="[trans]Select&Anything&Not&Me[/trans]"]
  451. /following-sibling::option[@value="&a"][@selected="selected"][.="[trans]Choice&A[/trans]"]
  452. /following-sibling::option[@value="&b"][not(@selected)][.="[trans]Choice&B[/trans]"]
  453. ]
  454. [count(./option)=3]
  455. '
  456. );
  457. }
  458. public function testSingleChoiceRequiredWithEmptyValue()
  459. {
  460. $form = $this->factory->createNamed('choice', 'na&me', '&a', array(
  461. 'property_path' => 'name',
  462. 'choices' => array('&a' => 'Choice&A', '&b' => 'Choice&B'),
  463. 'required' => true,
  464. 'multiple' => false,
  465. 'expanded' => false,
  466. 'empty_value' => 'Test&Me'
  467. ));
  468. $this->assertWidgetMatchesXpath($form->createView(), array(),
  469. '/select
  470. [@name="na&me"]
  471. [@required="required"]
  472. [
  473. ./option[@value=""][.="[trans]Test&Me[/trans]"]
  474. /following-sibling::option[@value="&a"][@selected="selected"][.="[trans]Choice&A[/trans]"]
  475. /following-sibling::option[@value="&b"][not(@selected)][.="[trans]Choice&B[/trans]"]
  476. ]
  477. [count(./option)=3]
  478. '
  479. );
  480. }
  481. public function testSingleChoiceRequiredWithEmptyValueViaView()
  482. {
  483. $form = $this->factory->createNamed('choice', 'na&me', '&a', array(
  484. 'property_path' => 'name',
  485. 'choices' => array('&a' => 'Choice&A', '&b' => 'Choice&B'),
  486. 'required' => true,
  487. 'multiple' => false,
  488. 'expanded' => false,
  489. ));
  490. $this->assertWidgetMatchesXpath($form->createView(), array('empty_value' => ''),
  491. '/select
  492. [@name="na&me"]
  493. [@required="required"]
  494. [
  495. ./option[@value=""][.="[trans][/trans]"]
  496. /following-sibling::option[@value="&a"][@selected="selected"][.="[trans]Choice&A[/trans]"]
  497. /following-sibling::option[@value="&b"][not(@selected)][.="[trans]Choice&B[/trans]"]
  498. ]
  499. [count(./option)=3]
  500. '
  501. );
  502. }
  503. public function testSingleChoiceGrouped()
  504. {
  505. $form = $this->factory->createNamed('choice', 'na&me', '&a', array(
  506. 'property_path' => 'name',
  507. 'choices' => array(
  508. 'Group&1' => array('&a' => 'Choice&A', '&b' => 'Choice&B'),
  509. 'Group&2' => array('&c' => 'Choice&C'),
  510. ),
  511. 'multiple' => false,
  512. 'expanded' => false,
  513. ));
  514. $this->assertWidgetMatchesXpath($form->createView(), array(),
  515. '/select
  516. [@name="na&me"]
  517. [./optgroup[@label="[trans]Group&1[/trans]"]
  518. [
  519. ./option[@value="&a"][@selected="selected"][.="[trans]Choice&A[/trans]"]
  520. /following-sibling::option[@value="&b"][not(@selected)][.="[trans]Choice&B[/trans]"]
  521. ]
  522. [count(./option)=2]
  523. ]
  524. [./optgroup[@label="[trans]Group&2[/trans]"]
  525. [./option[@value="&c"][not(@selected)][.="[trans]Choice&C[/trans]"]]
  526. [count(./option)=1]
  527. ]
  528. [count(./optgroup)=2]
  529. '
  530. );
  531. }
  532. public function testMultipleChoice()
  533. {
  534. $form = $this->factory->createNamed('choice', 'na&me', array('&a'), array(
  535. 'property_path' => 'name',
  536. 'choices' => array('&a' => 'Choice&A', '&b' => 'Choice&B'),
  537. 'multiple' => true,
  538. 'expanded' => false,
  539. ));
  540. $this->assertWidgetMatchesXpath($form->createView(), array(),
  541. '/select
  542. [@name="na&me[]"]
  543. [@multiple="multiple"]
  544. [
  545. ./option[@value="&a"][@selected="selected"][.="[trans]Choice&A[/trans]"]
  546. /following-sibling::option[@value="&b"][not(@selected)][.="[trans]Choice&B[/trans]"]
  547. ]
  548. [count(./option)=2]
  549. '
  550. );
  551. }
  552. public function testMultipleChoiceSkipEmptyValue()
  553. {
  554. $form = $this->factory->createNamed('choice', 'na&me', array('&a'), array(
  555. 'property_path' => 'name',
  556. 'choices' => array('&a' => 'Choice&A', '&b' => 'Choice&B'),
  557. 'multiple' => true,
  558. 'expanded' => false,
  559. 'empty_value' => 'Test&Me'
  560. ));
  561. $this->assertWidgetMatchesXpath($form->createView(), array(),
  562. '/select
  563. [@name="na&me[]"]
  564. [@multiple="multiple"]
  565. [
  566. ./option[@value="&a"][@selected="selected"][.="[trans]Choice&A[/trans]"]
  567. /following-sibling::option[@value="&b"][not(@selected)][.="[trans]Choice&B[/trans]"]
  568. ]
  569. [count(./option)=2]
  570. '
  571. );
  572. }
  573. public function testMultipleChoiceNonRequired()
  574. {
  575. $form = $this->factory->createNamed('choice', 'na&me', array('&a'), array(
  576. 'property_path' => 'name',
  577. 'choices' => array('&a' => 'Choice&A', '&b' => 'Choice&B'),
  578. 'required' => false,
  579. 'multiple' => true,
  580. 'expanded' => false,
  581. ));
  582. $this->assertWidgetMatchesXpath($form->createView(), array(),
  583. '/select
  584. [@name="na&me[]"]
  585. [@multiple="multiple"]
  586. [
  587. ./option[@value="&a"][@selected="selected"][.="[trans]Choice&A[/trans]"]
  588. /following-sibling::option[@value="&b"][not(@selected)][.="[trans]Choice&B[/trans]"]
  589. ]
  590. [count(./option)=2]
  591. '
  592. );
  593. }
  594. public function testSingleChoiceExpanded()
  595. {
  596. $form = $this->factory->createNamed('choice', 'na&me', '&a', array(
  597. 'property_path' => 'name',
  598. 'choices' => array('&a' => 'Choice&A', '&b' => 'Choice&B'),
  599. 'multiple' => false,
  600. 'expanded' => true,
  601. ));
  602. $this->assertWidgetMatchesXpath($form->createView(), array(),
  603. '/div
  604. [
  605. ./input[@type="radio"][@name="na&me"][@id="na&me_&a"][@checked]
  606. /following-sibling::label[@for="na&me_&a"][.="[trans]Choice&A[/trans]"]
  607. /following-sibling::input[@type="radio"][@name="na&me"][@id="na&me_&b"][not(@checked)]
  608. /following-sibling::label[@for="na&me_&b"][.="[trans]Choice&B[/trans]"]
  609. ]
  610. [count(./input)=2]
  611. '
  612. );
  613. }
  614. public function testSingleChoiceExpandedSkipEmptyValue()
  615. {
  616. $form = $this->factory->createNamed('choice', 'na&me', '&a', array(
  617. 'property_path' => 'name',
  618. 'choices' => array('&a' => 'Choice&A', '&b' => 'Choice&B'),
  619. 'multiple' => false,
  620. 'expanded' => true,
  621. 'empty_value' => 'Test&Me'
  622. ));
  623. $this->assertWidgetMatchesXpath($form->createView(), array(),
  624. '/div
  625. [
  626. ./input[@type="radio"][@name="na&me"][@id="na&me_&a"][@checked]
  627. /following-sibling::label[@for="na&me_&a"][.="[trans]Choice&A[/trans]"]
  628. /following-sibling::input[@type="radio"][@name="na&me"][@id="na&me_&b"][not(@checked)]
  629. /following-sibling::label[@for="na&me_&b"][.="[trans]Choice&B[/trans]"]
  630. ]
  631. [count(./input)=2]
  632. '
  633. );
  634. }
  635. public function testSingleChoiceExpandedWithBooleanValue()
  636. {
  637. $form = $this->factory->createNamed('choice', 'na&me', true, array(
  638. 'property_path' => 'name',
  639. 'choices' => array('1' => 'Choice&A', '0' => 'Choice&B'),
  640. 'multiple' => false,
  641. 'expanded' => true,
  642. ));
  643. $this->assertWidgetMatchesXpath($form->createView(), array(),
  644. '/div
  645. [
  646. ./input[@type="radio"][@name="na&me"][@id="na&me_1"][@checked]
  647. /following-sibling::label[@for="na&me_1"][.="[trans]Choice&A[/trans]"]
  648. /following-sibling::input[@type="radio"][@name="na&me"][@id="na&me_0"][not(@checked)]
  649. /following-sibling::label[@for="na&me_0"][.="[trans]Choice&B[/trans]"]
  650. ]
  651. [count(./input)=2]
  652. '
  653. );
  654. }
  655. public function testMultipleChoiceExpanded()
  656. {
  657. $form = $this->factory->createNamed('choice', 'na&me', array('&a', '&c'), array(
  658. 'property_path' => 'name',
  659. 'choices' => array('&a' => 'Choice&A', '&b' => 'Choice&B', '&c' => 'Choice&C'),
  660. 'multiple' => true,
  661. 'expanded' => true,
  662. 'required' => true,
  663. ));
  664. $this->assertWidgetMatchesXpath($form->createView(), array(),
  665. '/div
  666. [
  667. ./input[@type="checkbox"][@name="na&me[&a]"][@id="na&me_&a"][@checked][not(@required)]
  668. /following-sibling::label[@for="na&me_&a"][.="[trans]Choice&A[/trans]"]
  669. /following-sibling::input[@type="checkbox"][@name="na&me[&b]"][@id="na&me_&b"][not(@checked)][not(@required)]
  670. /following-sibling::label[@for="na&me_&b"][.="[trans]Choice&B[/trans]"]
  671. /following-sibling::input[@type="checkbox"][@name="na&me[&c]"][@id="na&me_&c"][@checked][not(@required)]
  672. /following-sibling::label[@for="na&me_&c"][.="[trans]Choice&C[/trans]"]
  673. ]
  674. [count(./input)=3]
  675. '
  676. );
  677. }
  678. public function testCountry()
  679. {
  680. $form = $this->factory->createNamed('country', 'na&me', 'AT', array(
  681. 'property_path' => 'name',
  682. ));
  683. $this->assertWidgetMatchesXpath($form->createView(), array(),
  684. '/select
  685. [@name="na&me"]
  686. [./option[@value="AT"][@selected="selected"][.="[trans]Austria[/trans]"]]
  687. [count(./option)>200]
  688. '
  689. );
  690. }
  691. public function testCountryWithEmptyValue()
  692. {
  693. $form = $this->factory->createNamed('country', 'na&me', 'AT', array(
  694. 'property_path' => 'name',
  695. 'empty_value' => 'Select&Country',
  696. 'required' => false,
  697. ));
  698. $this->assertWidgetMatchesXpath($form->createView(), array(),
  699. '/select
  700. [@name="na&me"]
  701. [./option[@value=""][not(@selected)][.="[trans]Select&Country[/trans]"]]
  702. [./option[@value="AT"][@selected="selected"][.="[trans]Austria[/trans]"]]
  703. [count(./option)>201]
  704. '
  705. );
  706. }
  707. public function testCsrf()
  708. {
  709. $this->csrfProvider->expects($this->any())
  710. ->method('generateCsrfToken')
  711. ->will($this->returnValue('foo&bar'));
  712. $form = $this->factory->createNamed('csrf', 'na&me', null, array(
  713. 'property_path' => 'name',
  714. ));
  715. $this->assertWidgetMatchesXpath($form->createView(), array(),
  716. '/input
  717. [@type="hidden"]
  718. [@value="foo&bar"]
  719. '
  720. );
  721. }
  722. public function testDateTime()
  723. {
  724. $form = $this->factory->createNamed('datetime', 'na&me', '2011-02-03 04:05:06', array(
  725. 'property_path' => 'name',
  726. 'input' => 'string',
  727. 'with_seconds' => false,
  728. ));
  729. $this->assertWidgetMatchesXpath($form->createView(), array(),
  730. '/div
  731. [
  732. ./div
  733. [@id="na&me_date"]
  734. [
  735. ./select
  736. [@id="na&me_date_month"]
  737. [./option[@value="2"][@selected="selected"]]
  738. /following-sibling::select
  739. [@id="na&me_date_day"]
  740. [./option[@value="3"][@selected="selected"]]
  741. /following-sibling::select
  742. [@id="na&me_date_year"]
  743. [./option[@value="2011"][@selected="selected"]]
  744. ]
  745. /following-sibling::div
  746. [@id="na&me_time"]
  747. [
  748. ./select
  749. [@id="na&me_time_hour"]
  750. [./option[@value="4"][@selected="selected"]]
  751. /following-sibling::select
  752. [@id="na&me_time_minute"]
  753. [./option[@value="5"][@selected="selected"]]
  754. ]
  755. ]
  756. [count(.//select)=5]
  757. '
  758. );
  759. }
  760. public function testDateTimeWithEmptyValueGlobal()
  761. {
  762. $form = $this->factory->createNamed('datetime', 'na&me', null, array(
  763. 'property_path' => 'name',
  764. 'input' => 'string',
  765. 'empty_value' => 'Change&Me',
  766. 'required' => false,
  767. ));
  768. $this->assertWidgetMatchesXpath($form->createView(), array(),
  769. '/div
  770. [
  771. ./div
  772. [@id="na&me_date"]
  773. [
  774. ./select
  775. [@id="na&me_date_month"]
  776. [./option[@value=""][.="[trans]Change&Me[/trans]"]]
  777. /following-sibling::select
  778. [@id="na&me_date_day"]
  779. [./option[@value=""][.="[trans]Change&Me[/trans]"]]
  780. /following-sibling::select
  781. [@id="na&me_date_year"]
  782. [./option[@value=""][.="[trans]Change&Me[/trans]"]]
  783. ]
  784. /following-sibling::div
  785. [@id="na&me_time"]
  786. [
  787. ./select
  788. [@id="na&me_time_hour"]
  789. [./option[@value=""][.="[trans]Change&Me[/trans]"]]
  790. /following-sibling::select
  791. [@id="na&me_time_minute"]
  792. [./option[@value=""][.="[trans]Change&Me[/trans]"]]
  793. ]
  794. ]
  795. [count(.//select)=5]
  796. '
  797. );
  798. }
  799. public function testDateTimeWithEmptyValueOnTime()
  800. {
  801. $form = $this->factory->createNamed('datetime', 'na&me', '2011-02-03', array(
  802. 'property_path' => 'name',
  803. 'input' => 'string',
  804. 'empty_value' => array('hour' => 'Change&Me', 'minute' => 'Change&Me'),
  805. 'required' => false,
  806. ));
  807. $this->assertWidgetMatchesXpath($form->createView(), array(),
  808. '/div
  809. [
  810. ./div
  811. [@id="na&me_date"]
  812. [
  813. ./select
  814. [@id="na&me_date_month"]
  815. [./option[@value="2"][@selected="selected"]]
  816. /following-sibling::select
  817. [@id="na&me_date_day"]
  818. [./option[@value="3"][@selected="selected"]]
  819. /following-sibling::select
  820. [@id="na&me_date_year"]
  821. [./option[@value="2011"][@selected="selected"]]
  822. ]
  823. /following-sibling::div
  824. [@id="na&me_time"]
  825. [
  826. ./select
  827. [@id="na&me_time_hour"]
  828. [./option[@value=""][.="[trans]Change&Me[/trans]"]]
  829. /following-sibling::select
  830. [@id="na&me_time_minute"]
  831. [./option[@value=""][.="[trans]Change&Me[/trans]"]]
  832. ]
  833. ]
  834. [count(.//select)=5]
  835. '
  836. );
  837. }
  838. public function testDateTimeWithSeconds()
  839. {
  840. $form = $this->factory->createNamed('datetime', 'na&me', '2011-02-03 04:05:06', array(
  841. 'property_path' => 'name',
  842. 'input' => 'string',
  843. 'with_seconds' => true,
  844. ));
  845. $this->assertWidgetMatchesXpath($form->createView(), array(),
  846. '/div
  847. [
  848. ./div
  849. [@id="na&me_date"]
  850. [
  851. ./select
  852. [@id="na&me_date_month"]
  853. [./option[@value="2"][@selected="selected"]]
  854. /following-sibling::select
  855. [@id="na&me_date_day"]
  856. [./option[@value="3"][@selected="selected"]]
  857. /following-sibling::select
  858. [@id="na&me_date_year"]
  859. [./option[@value="2011"][@selected="selected"]]
  860. ]
  861. /following-sibling::div
  862. [@id="na&me_time"]
  863. [
  864. ./select
  865. [@id="na&me_time_hour"]
  866. [./option[@value="4"][@selected="selected"]]
  867. /following-sibling::select
  868. [@id="na&me_time_minute"]
  869. [./option[@value="5"][@selected="selected"]]
  870. /following-sibling::select
  871. [@id="na&me_time_second"]
  872. [./option[@value="6"][@selected="selected"]]
  873. ]
  874. ]
  875. [count(.//select)=6]
  876. '
  877. );
  878. }
  879. public function testDateTimeSingleText()
  880. {
  881. $form = $this->factory->createNamed('datetime', 'na&me', '2011-02-03 04:05:06', array(
  882. 'property_path' => 'name',
  883. 'input' => 'string',
  884. 'date_widget' => 'single_text',
  885. 'time_widget' => 'single_text',
  886. ));
  887. $this->assertWidgetMatchesXpath($form->createView(), array(),
  888. '/div
  889. [
  890. ./input
  891. [@type="text"]
  892. [@id="na&me_date"]
  893. [@name="na&me[date]"]
  894. [@value="Feb 3, 2011"]
  895. /following-sibling::input
  896. [@type="text"]
  897. [@id="na&me_time"]
  898. [@name="na&me[time]"]
  899. [@value="04:05:00"]
  900. ]
  901. '
  902. );
  903. }
  904. public function testDateTimeWithWidgetSingleText()
  905. {
  906. $form = $this->factory->createNamed('datetime', 'name', '2011-02-03 04:05:06', array(
  907. 'property_path' => 'name',
  908. 'input' => 'string',
  909. 'widget' => 'single_text',
  910. ));
  911. $this->assertWidgetMatchesXpath($form->createView(), array(),
  912. '/input
  913. [@type="text"]
  914. [@name="name"]
  915. [@value="2011-02-03 04:05:00"]
  916. '
  917. );
  918. }
  919. public function testDateTimeWithWidgetSingleTextIgnoreDateAndTimeWidgets()
  920. {
  921. $form = $this->factory->createNamed('datetime', 'na&me', '2011-02-03 04:05:06', array(
  922. 'property_path' => 'name',
  923. 'input' => 'string',
  924. 'date_widget' => 'choice',
  925. 'time_widget' => 'choice',
  926. 'widget' => 'single_text',
  927. ));
  928. $this->assertWidgetMatchesXpath($form->createView(), array(),
  929. '/input
  930. [@type="text"]
  931. [@name="na&me"]
  932. [@value="2011-02-03 04:05:00"]
  933. '
  934. );
  935. }
  936. public function testDateChoice()
  937. {
  938. $form = $this->factory->createNamed('date', 'na&me', '2011-02-03', array(
  939. 'property_path' => 'name',
  940. 'input' => 'string',
  941. 'widget' => 'choice',
  942. ));
  943. $this->assertWidgetMatchesXpath($form->createView(), array(),
  944. '/div
  945. [
  946. ./select
  947. [@id="na&me_month"]
  948. [./option[@value="2"][@selected="selected"]]
  949. /following-sibling::select
  950. [@id="na&me_day"]
  951. [./option[@value="3"][@selected="selected"]]
  952. /following-sibling::select
  953. [@id="na&me_year"]
  954. [./option[@value="2011"][@selected="selected"]]
  955. ]
  956. [count(./select)=3]
  957. '
  958. );
  959. }
  960. public function testDateChoiceWithEmptyValueGlobal()
  961. {
  962. $form = $this->factory->createNamed('date', 'na&me', null, array(
  963. 'property_path' => 'name',
  964. 'input' => 'string',
  965. 'widget' => 'choice',
  966. 'empty_value' => 'Change&Me',
  967. 'required' => false,
  968. ));
  969. $this->assertWidgetMatchesXpath($form->createView(), array(),
  970. '/div
  971. [
  972. ./select
  973. [@id="na&me_month"]
  974. [./option[@value=""][.="[trans]Change&Me[/trans]"]]
  975. /following-sibling::select
  976. [@id="na&me_day"]
  977. [./option[@value=""][.="[trans]Change&Me[/trans]"]]
  978. /following-sibling::select
  979. [@id="na&me_year"]
  980. [./option[@value=""][.="[trans]Change&Me[/trans]"]]
  981. ]
  982. [count(./select)=3]
  983. '
  984. );
  985. }
  986. public function testDateChoiceWithEmptyValueOnYear()
  987. {
  988. $form = $this->factory->createNamed('date', 'na&me', null, array(
  989. 'property_path' => 'name',
  990. 'input' => 'string',
  991. 'widget' => 'choice',
  992. 'required' => false,
  993. 'empty_value' => array('year' => 'Change&Me'),
  994. ));
  995. $this->assertWidgetMatchesXpath($form->createView(), array(),
  996. '/div
  997. [
  998. ./select
  999. [@id="na&me_month"]
  1000. [./option[@value="1"]]
  1001. /following-sibling::select
  1002. [@id="na&me_day"]
  1003. [./option[@value="1"]]
  1004. /following-sibling::select
  1005. [@id="na&me_year"]
  1006. [./option[@value=""][.="[trans]Change&Me[/trans]"]]
  1007. ]
  1008. [count(./select)=3]
  1009. '
  1010. );
  1011. }
  1012. public function testDateText()
  1013. {
  1014. $form = $this->factory->createNamed('date', 'na&me', '2011-02-03', array(
  1015. 'property_path' => 'name',
  1016. 'input' => 'string',
  1017. 'widget' => 'text',
  1018. ));
  1019. $this->assertWidgetMatchesXpath($form->createView(), array(),
  1020. '/div
  1021. [
  1022. ./input
  1023. [@id="na&me_month"]
  1024. [@type="text"]
  1025. [@value="2"]
  1026. /following-sibling::input
  1027. [@id="na&me_day"]
  1028. [@type="text"]
  1029. [@value="3"]
  1030. /following-sibling::input
  1031. [@id="na&me_year"]
  1032. [@type="text"]
  1033. [@value="2011"]
  1034. ]
  1035. [count(./input)=3]
  1036. '
  1037. );
  1038. }
  1039. public function testDateSingleText()
  1040. {
  1041. $form = $this->factory->createNamed('date', 'na&me', '2011-02-03', array(
  1042. 'property_path' => 'name',
  1043. 'input' => 'string',
  1044. 'widget' => 'single_text',
  1045. ));
  1046. $this->assertWidgetMatchesXpath($form->createView(), array(),
  1047. '/input
  1048. [@type="text"]
  1049. [@name="na&me"]
  1050. [@value="Feb 3, 2011"]
  1051. '
  1052. );
  1053. }
  1054. public function testDateErrorBubbling()
  1055. {
  1056. $child = $this->factory->createNamed('date', 'date');
  1057. $form = $this->factory->createNamed('form', 'form')->add($child);
  1058. $child->addError(new FormError('Error!'));
  1059. $view = $form->createView();
  1060. $this->assertEmpty($this->renderErrors($view));
  1061. $this->assertNotEmpty($this->renderErrors($view['date']));
  1062. }
  1063. public function testBirthDay()
  1064. {
  1065. $form = $this->factory->createNamed('birthday', 'na&me', '2000-02-03', array(
  1066. 'property_path' => 'name',
  1067. 'input' => 'string',
  1068. ));
  1069. $this->assertWidgetMatchesXpath($form->createView(), array(),
  1070. '/div
  1071. [
  1072. ./select
  1073. [@id="na&me_month"]
  1074. [./option[@value="2"][@selected="selected"]]
  1075. /following-sibling::select
  1076. [@id="na&me_day"]
  1077. [./option[@value="3"][@selected="selected"]]
  1078. /following-sibling::select
  1079. [@id="na&me_year"]
  1080. [./option[@value="2000"][@selected="selected"]]
  1081. ]
  1082. [count(./select)=3]
  1083. '
  1084. );
  1085. }
  1086. public function testBirthDayWithEmptyValue()
  1087. {
  1088. $form = $this->factory->createNamed('birthday', 'na&me', '1950-01-01', array(
  1089. 'property_path' => 'name',
  1090. 'input' => 'string',
  1091. 'empty_value' => '',
  1092. 'required' => false,
  1093. ));
  1094. $this->assertWidgetMatchesXpath($form->createView(), array(),
  1095. '/div
  1096. [
  1097. ./select
  1098. [@id="na&me_month"]
  1099. [./option[@value=""][.="[trans][/trans]"]]
  1100. [./option[@value="1"][@selected="selected"]]
  1101. /following-sibling::select
  1102. [@id="na&me_day"]
  1103. [./option[@value=""][.="[trans][/trans]"]]
  1104. [./option[@value="1"][@selected="selected"]]
  1105. /following-sibling::select
  1106. [@id="na&me_year"]
  1107. [./option[@value=""][.="[trans][/trans]"]]
  1108. [./option[@value="1950"][@selected="selected"]]
  1109. ]
  1110. [count(./select)=3]
  1111. '
  1112. );
  1113. }
  1114. public function testEmail()
  1115. {
  1116. $form = $this->factory->createNamed('email', 'na&me', 'foo&bar', array(
  1117. 'property_path' => 'name',
  1118. ));
  1119. $this->assertWidgetMatchesXpath($form->createView(), array(),
  1120. '/input
  1121. [@type="email"]
  1122. [@name="na&me"]
  1123. [@value="foo&bar"]
  1124. [not(@maxlength)]
  1125. '
  1126. );
  1127. }
  1128. public function testEmailWithMaxLength()
  1129. {
  1130. $form = $this->factory->createNamed('email', 'na&me', 'foo&bar', array(
  1131. 'property_path' => 'name',
  1132. 'max_length' => 123,
  1133. ));
  1134. $this->assertWidgetMatchesXpath($form->createView(), array(),
  1135. '/input
  1136. [@type="email"]
  1137. [@name="na&me"]
  1138. [@value="foo&bar"]
  1139. [@maxlength="123"]
  1140. '
  1141. );
  1142. }
  1143. public function testFile()
  1144. {
  1145. $form = $this->factory->createNamed('file', 'na&me', null, array(
  1146. 'property_path' => 'name',
  1147. ));
  1148. $this->assertWidgetMatchesXpath($form->createView(), array(),
  1149. '/input
  1150. [@type="file"]
  1151. '
  1152. );
  1153. }
  1154. public function testHidden()
  1155. {
  1156. $form = $this->factory->createNamed('hidden', 'na&me', 'foo&bar', array(
  1157. 'property_path' => 'name',
  1158. ));
  1159. $this->assertWidgetMatchesXpath($form->createView(), array(),
  1160. '/input
  1161. [@type="hidden"]
  1162. [@name="na&me"]
  1163. [@value="foo&bar"]
  1164. '
  1165. );
  1166. }
  1167. public function testInteger()
  1168. {
  1169. $form = $this->factory->createNamed('integer', 'na&me', 123, array(
  1170. 'property_path' => 'name',
  1171. ));
  1172. $this->assertWidgetMatchesXpath($form->createView(), array(),
  1173. '/input
  1174. [@type="number"]
  1175. [@name="na&me"]
  1176. [@value="123"]
  1177. '
  1178. );
  1179. }
  1180. public function testLanguage()
  1181. {
  1182. $form = $this->factory->createNamed('language', 'na&me', 'de', array(
  1183. 'property_path' => 'name',
  1184. ));
  1185. $this->assertWidgetMatchesXpath($form->createView(), array(),
  1186. '/select
  1187. [@name="na&me"]
  1188. [./option[@value="de"][@selected="selected"][.="[trans]German[/trans]"]]
  1189. [count(./option)>200]
  1190. '
  1191. );
  1192. }
  1193. public function testLocale()
  1194. {
  1195. $form = $this->factory->createNamed('locale', 'na&me', 'de_AT', array(
  1196. 'property_path' => 'name',
  1197. ));
  1198. $this->assertWidgetMatchesXpath($form->createView(), array(),
  1199. '/select
  1200. [@name="na&me"]
  1201. [./option[@value="de_AT"][@selected="selected"][.="[trans]German (Austria)[/trans]"]]
  1202. [count(./option)>200]
  1203. '
  1204. );
  1205. }
  1206. public function testMoney()
  1207. {
  1208. $form = $this->factory->createNamed('money', 'na&me', 1234.56, array(
  1209. 'property_path' => 'name',
  1210. 'currency' => 'EUR',
  1211. ));
  1212. $this->assertWidgetMatchesXpath($form->createView(), array(),
  1213. '/input
  1214. [@type="text"]
  1215. [@name="na&me"]
  1216. [@value="1234.56"]
  1217. [contains(.., "€")]
  1218. '
  1219. );
  1220. }
  1221. public function testNumber()
  1222. {
  1223. $form = $this->factory->createNamed('number', 'na&me', 1234.56, array(
  1224. 'property_path' => 'name',
  1225. ));
  1226. $this->assertWidgetMatchesXpath($form->createView(), array(),
  1227. '/input
  1228. [@type="text"]
  1229. [@name="na&me"]
  1230. [@value="1234.56"]
  1231. '
  1232. );
  1233. }
  1234. public function testPassword()
  1235. {
  1236. $form = $this->factory->createNamed('password', 'na&me', 'foo&bar', array(
  1237. 'property_path' => 'name',
  1238. ));
  1239. $this->assertWidgetMatchesXpath($form->createView(), array(),
  1240. '/input
  1241. [@type="password"]
  1242. [@name="na&me"]
  1243. '
  1244. );
  1245. }
  1246. public function testPasswordBoundNotAlwaysEmpty()
  1247. {
  1248. $form = $this->factory->createNamed('password', 'na&me', null, array(
  1249. 'property_path' => 'name',
  1250. 'always_empty' => false,
  1251. ));
  1252. $form->bind('foo&bar');
  1253. $this->assertWidgetMatchesXpath($form->createView(), array(),
  1254. '/input
  1255. [@type="password"]
  1256. [@name="na&me"]
  1257. [@value="foo&bar"]
  1258. '
  1259. );
  1260. }
  1261. public function testPasswordWithMaxLength()
  1262. {
  1263. $form = $this->factory->createNamed('password', 'na&me', 'foo&bar', array(
  1264. 'property_path' => 'name',
  1265. 'max_length' => 123,
  1266. ));
  1267. $this->assertWidgetMatchesXpath($form->createView(), array(),
  1268. '/input
  1269. [@type="password"]
  1270. [@name="na&me"]
  1271. [@maxlength="123"]
  1272. '
  1273. );
  1274. }
  1275. public function testPercent()
  1276. {
  1277. $form = $this->factory->createNamed('percent', 'na&me', 0.1, array(
  1278. 'property_path' => 'name',
  1279. ));
  1280. $this->assertWidgetMatchesXpath($form->createView(), array(),
  1281. '/input
  1282. [@type="text"]
  1283. [@name="na&me"]
  1284. [@value="10"]
  1285. [contains(.., "%")]
  1286. '
  1287. );
  1288. }
  1289. public function testCheckedRadio()
  1290. {
  1291. $form = $this->factory->createNamed('radio', 'na&me', true, array(
  1292. 'property_path' => 'name',
  1293. ));
  1294. $this->assertWidgetMatchesXpath($form->createView(), array(),
  1295. '/input
  1296. [@type="radio"]
  1297. [@name="na&me"]
  1298. [@checked="checked"]
  1299. [@value=""]
  1300. '
  1301. );
  1302. }
  1303. public function testCheckedRadioWithValue()
  1304. {
  1305. $form = $this->factory->createNamed('radio', 'na&me', true, array(
  1306. 'property_path' => 'name',
  1307. 'value' => 'foo&bar',
  1308. ));
  1309. $this->assertWidgetMatchesXpath($form->createView(), array(),
  1310. '/input
  1311. [@type="radio"]
  1312. [@name="na&me"]
  1313. [@checked="checked"]
  1314. [@value="foo&bar"]
  1315. '
  1316. );
  1317. }
  1318. public function testUncheckedRadio()
  1319. {
  1320. $form = $this->factory->createNamed('radio', 'na&me', false, array(
  1321. 'property_path' => 'name',
  1322. ));
  1323. $this->assertWidgetMatchesXpath($form->createView(), array(),
  1324. '/input
  1325. [@type="radio"]
  1326. [@name="na&me"]
  1327. [not(@checked)]
  1328. '
  1329. );
  1330. }
  1331. public function testTextarea()
  1332. {
  1333. $form = $this->factory->createNamed('textarea', 'na&me', 'foo&bar', array(
  1334. 'property_path' => 'name',
  1335. 'pattern' => 'foo',
  1336. ));
  1337. $this->assertWidgetMatchesXpath($form->createView(), array(),
  1338. '/textarea
  1339. [@name="na&me"]
  1340. [not(@pattern)]
  1341. [.="foo&bar"]
  1342. '
  1343. );
  1344. }
  1345. public function testText()
  1346. {
  1347. $form = $this->factory->createNamed('text', 'na&me', 'foo&bar', array(
  1348. 'property_path' => 'name',
  1349. ));
  1350. $this->assertWidgetMatchesXpath($form->createView(), array(),
  1351. '/input
  1352. [@type="text"]
  1353. [@name="na&me"]
  1354. [@value="foo&bar"]
  1355. [not(@maxlength)]
  1356. '
  1357. );
  1358. }
  1359. public function testTextWithMaxLength()
  1360. {
  1361. $form = $this->factory->createNamed('text', 'na&me', 'foo&bar', array(
  1362. 'property_path' => 'name',
  1363. 'max_length' => 123,
  1364. ));
  1365. $this->assertWidgetMatchesXpath($form->createView(), array(),
  1366. '/input
  1367. [@type="text"]
  1368. [@name="na&me"]
  1369. [@value="foo&bar"]
  1370. [@maxlength="123"]
  1371. '
  1372. );
  1373. }
  1374. public function testSearch()
  1375. {
  1376. $form = $this->factory->createNamed('search', 'na&me', 'foo&bar', array(
  1377. 'property_path' => 'name',
  1378. ));
  1379. $this->assertWidgetMatchesXpath($form->createView(), array(),
  1380. '/input
  1381. [@type="search"]
  1382. [@name="na&me"]
  1383. [@value="foo&bar"]
  1384. [not(@maxlength)]
  1385. '
  1386. );
  1387. }
  1388. public function testTime()
  1389. {
  1390. $form = $this->factory->createNamed('time', 'na&me', '04:05:06', array(
  1391. 'property_path' => 'name',
  1392. 'input' => 'string',
  1393. 'with_seconds' => false,
  1394. ));
  1395. $this->assertWidgetMatchesXpath($form->createView(), array(),
  1396. '/div
  1397. [
  1398. ./select
  1399. [@id="na&me_hour"]
  1400. [@size="1"]
  1401. [./option[@value="4"][@selected="selected"]]
  1402. /following-sibling::select
  1403. [@id="na&me_minute"]
  1404. [@size="1"]
  1405. [./option[@value="5"][@selected="selected"]]
  1406. ]
  1407. [count(./select)=2]
  1408. '
  1409. );
  1410. }
  1411. public function testTimeWithSeconds()
  1412. {
  1413. $form = $this->factory->createNamed('time', 'na&me', '04:05:06', array(
  1414. 'property_path' => 'name',
  1415. 'input' => 'string',
  1416. 'with_seconds' => true,
  1417. ));
  1418. $this->assertWidgetMatchesXpath($form->createView(), array(),
  1419. '/div
  1420. [
  1421. ./select
  1422. [@id="na&me_hour"]
  1423. [@size="1"]
  1424. [./option[@value="4"][@selected="selected"]]
  1425. [count(./option)>23]
  1426. /following-sibling::select
  1427. [@id="na&me_minute"]
  1428. [@size="1"]
  1429. [./option[@value="5"][@selected="selected"]]
  1430. [count(./option)>59]
  1431. /following-sibling::select
  1432. [@id="na&me_second"]
  1433. [@size="1"]
  1434. [./option[@value="6"][@selected="selected"]]
  1435. [count(./option)>59]
  1436. ]
  1437. [count(./select)=3]
  1438. '
  1439. );
  1440. }
  1441. public function testTimeText()
  1442. {
  1443. $form = $this->factory->createNamed('time', 'na&me', '04:05:06', array(
  1444. 'property_path' => 'name',
  1445. 'input' => 'string',
  1446. 'widget' => 'text',
  1447. ));
  1448. $this->assertWidgetMatchesXpath($form->createView(), array(),
  1449. '/div
  1450. [
  1451. ./input
  1452. [@type="text"]
  1453. [@id="na&me_hour"]
  1454. [@name="na&me[hour]"]
  1455. [@value="04"]
  1456. [@size="1"]
  1457. [@required="required"]
  1458. /following-sibling::input
  1459. [@type="text"]
  1460. [@id="na&me_minute"]
  1461. [@name="na&me[minute]"]
  1462. [@value="05"]
  1463. [@size="1"]
  1464. [@required="required"]
  1465. ]
  1466. [count(./input)=2]
  1467. '
  1468. );
  1469. }
  1470. public function testTimeSingleText()
  1471. {
  1472. $form = $this->factory->createNamed('time', 'na&me', '04:05:06', array(
  1473. 'property_path' => 'name',
  1474. 'input' => 'string',
  1475. 'widget' => 'single_text',
  1476. ));
  1477. $this->assertWidgetMatchesXpath($form->createView(), array(),
  1478. '/input
  1479. [@type="text"]
  1480. [@name="na&me"]
  1481. [@value="04:05:00"]
  1482. '
  1483. );
  1484. }
  1485. public function testTimeWithEmptyValueGlobal()
  1486. {
  1487. $form = $this->factory->createNamed('time', 'na&me', null, array(
  1488. 'property_path' => 'name',
  1489. 'input' => 'string',
  1490. 'empty_value' => 'Change&Me',
  1491. 'required' => false,
  1492. ));
  1493. $this->assertWidgetMatchesXpath($form->createView(), array(),
  1494. '/div
  1495. [
  1496. ./select
  1497. [@id="na&me_hour"]
  1498. [./option[@value=""][.="[trans]Change&Me[/trans]"]]
  1499. [count(./option)>24]
  1500. /following-sibling::select
  1501. [@id="na&me_minute"]
  1502. [./option[@value=""][.="[trans]Change&Me[/trans]"]]
  1503. [count(./option)>60]
  1504. ]
  1505. [count(./select)=2]
  1506. '
  1507. );
  1508. }
  1509. public function testTimeWithEmptyValueOnYear()
  1510. {
  1511. $form = $this->factory->createNamed('time', 'na&me', null, array(
  1512. 'property_path' => 'name',
  1513. 'input' => 'string',
  1514. 'required' => false,
  1515. 'empty_value' => array('hour' => 'Change&Me'),
  1516. ));
  1517. $this->assertWidgetMatchesXpath($form->createView(), array(),
  1518. '/div
  1519. [
  1520. ./select
  1521. [@id="na&me_hour"]
  1522. [./option[@value=""][.="[trans]Change&Me[/trans]"]]
  1523. [count(./option)>24]
  1524. /following-sibling::select
  1525. [@id="na&me_minute"]
  1526. [./option[@value="1"]]
  1527. [count(./option)>59]
  1528. ]
  1529. [count(./select)=2]
  1530. '
  1531. );
  1532. }
  1533. public function testTimeErrorBubbling()
  1534. {
  1535. $child = $this->factory->createNamed('time', 'time');
  1536. $form = $this->factory->createNamed('form', 'form')->add($child);
  1537. $child->addError(new FormError('Error!'));
  1538. $view = $form->createView();
  1539. $this->assertEmpty($this->renderErrors($view));
  1540. $this->assertNotEmpty($this->renderErrors($view['time']));
  1541. }
  1542. public function testTimezone()
  1543. {
  1544. $form = $this->factory->createNamed('timezone', 'na&me', 'Europe/Vienna', array(
  1545. 'property_path' => 'name',
  1546. ));
  1547. $this->assertWidgetMatchesXpath($form->createView(), array(),
  1548. '/select
  1549. [@name="na&me"]
  1550. [@required="required"]
  1551. [./optgroup
  1552. [@label="[trans]Europe[/trans]"]
  1553. [./option[@value="Europe/Vienna"][@selected="selected"][.="[trans]Vienna[/trans]"]]
  1554. ]
  1555. [count(./optgroup)>10]
  1556. [count(.//option)>200]
  1557. '
  1558. );
  1559. }
  1560. public function testTimezoneWithEmptyValue()
  1561. {
  1562. $form = $this->factory->createNamed('timezone', 'na&me', null, array(
  1563. 'property_path' => 'name',
  1564. 'empty_value' => 'Select&Timezone',
  1565. 'required' => false,
  1566. ));
  1567. $this->assertWidgetMatchesXpath($form->createView(), array(),
  1568. '/select
  1569. [./option[@value=""][.="[trans]Select&Timezone[/trans]"]]
  1570. [count(./optgroup)>10]
  1571. [count(.//option)>201]
  1572. '
  1573. );
  1574. }
  1575. public function testUrl()
  1576. {
  1577. $url = 'http://www.google.com?foo1=bar1&foo2=bar2';
  1578. $form = $this->factory->createNamed('url', 'na&me', $url, array(
  1579. 'property_path' => 'name',
  1580. ));
  1581. $this->assertWidgetMatchesXpath($form->createView(), array(),
  1582. '/input
  1583. [@type="url"]
  1584. [@name="na&me"]
  1585. [@value="http://www.google.com?foo1=bar1&foo2=bar2"]
  1586. '
  1587. );
  1588. }
  1589. public function testCollectionPrototype()
  1590. {
  1591. $form = $this->factory->createNamedBuilder('form', 'na&me', array('items' => array('one', 'two', 'three')))
  1592. ->add('items', 'collection', array('allow_add' => true))
  1593. ->getForm()
  1594. ->createView();
  1595. $html = $this->renderWidget($form);
  1596. $this->assertMatchesXpath($html,
  1597. '//div[@id="na&me_items"][@data-prototype]
  1598. |
  1599. //table[@id="na&me_items"][@data-prototype]
  1600. '
  1601. );
  1602. }
  1603. }