AbstractLayoutTest.php 50 KB

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