AbstractLayoutTest.php 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702
  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 testSingleChoiceNonRequired()
  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. 'required' => false,
  337. 'multiple' => false,
  338. 'expanded' => false,
  339. ));
  340. $this->assertWidgetMatchesXpath($form->createView(), array(),
  341. '/select
  342. [@name="na&me"]
  343. [not(@required)]
  344. [
  345. ./option[@value=""][.="[trans][/trans]"]
  346. /following-sibling::option[@value="&a"][@selected="selected"][.="[trans]Choice&A[/trans]"]
  347. /following-sibling::option[@value="&b"][not(@selected)][.="[trans]Choice&B[/trans]"]
  348. ]
  349. [count(./option)=3]
  350. '
  351. );
  352. }
  353. public function testSingleChoiceNonRequiredNoneSelected()
  354. {
  355. $form = $this->factory->createNamed('choice', 'na&me', null, array(
  356. 'property_path' => 'name',
  357. 'choices' => array('&a' => 'Choice&A', '&b' => 'Choice&B'),
  358. 'required' => false,
  359. 'multiple' => false,
  360. 'expanded' => false,
  361. ));
  362. $this->assertWidgetMatchesXpath($form->createView(), array(),
  363. '/select
  364. [@name="na&me"]
  365. [not(@required)]
  366. [
  367. ./option[@value=""][.="[trans][/trans]"]
  368. /following-sibling::option[@value="&a"][not(@selected)][.="[trans]Choice&A[/trans]"]
  369. /following-sibling::option[@value="&b"][not(@selected)][.="[trans]Choice&B[/trans]"]
  370. ]
  371. [count(./option)=3]
  372. '
  373. );
  374. }
  375. public function testSingleChoiceWithNonRequiredEmptyValue()
  376. {
  377. $form = $this->factory->createNamed('choice', 'na&me', '&a', array(
  378. 'property_path' => 'name',
  379. 'choices' => array('&a' => 'Choice&A', '&b' => 'Choice&B'),
  380. 'multiple' => false,
  381. 'expanded' => false,
  382. 'required' => false,
  383. 'empty_value' => 'Select&Anything&Not&Me',
  384. ));
  385. $this->assertWidgetMatchesXpath($form->createView(), array(),
  386. '/select
  387. [@name="na&me"]
  388. [not(@required)]
  389. [
  390. ./option[@value=""][not(@selected)][.="[trans]Select&Anything&Not&Me[/trans]"]
  391. /following-sibling::option[@value="&a"][@selected="selected"][.="[trans]Choice&A[/trans]"]
  392. /following-sibling::option[@value="&b"][not(@selected)][.="[trans]Choice&B[/trans]"]
  393. ]
  394. [count(./option)=3]
  395. '
  396. );
  397. }
  398. public function testSingleChoiceRequiredWithEmptyValue()
  399. {
  400. $form = $this->factory->createNamed('choice', 'na&me', '&a', array(
  401. 'property_path' => 'name',
  402. 'choices' => array('&a' => 'Choice&A', '&b' => 'Choice&B'),
  403. 'required' => true,
  404. 'multiple' => false,
  405. 'expanded' => false,
  406. 'empty_value' => 'Test&Me'
  407. ));
  408. $this->assertWidgetMatchesXpath($form->createView(), array(),
  409. '/select
  410. [@name="na&me"]
  411. [@required="required"]
  412. [
  413. ./option[@value=""][.="[trans]Test&Me[/trans]"]
  414. /following-sibling::option[@value="&a"][@selected="selected"][.="[trans]Choice&A[/trans]"]
  415. /following-sibling::option[@value="&b"][not(@selected)][.="[trans]Choice&B[/trans]"]
  416. ]
  417. [count(./option)=3]
  418. '
  419. );
  420. }
  421. public function testSingleChoiceRequiredWithEmptyValueViaView()
  422. {
  423. $form = $this->factory->createNamed('choice', 'na&me', '&a', array(
  424. 'property_path' => 'name',
  425. 'choices' => array('&a' => 'Choice&A', '&b' => 'Choice&B'),
  426. 'required' => true,
  427. 'multiple' => false,
  428. 'expanded' => false,
  429. ));
  430. $this->assertWidgetMatchesXpath($form->createView(), array('empty_value' => ''),
  431. '/select
  432. [@name="na&me"]
  433. [@required="required"]
  434. [
  435. ./option[@value=""][.="[trans][/trans]"]
  436. /following-sibling::option[@value="&a"][@selected="selected"][.="[trans]Choice&A[/trans]"]
  437. /following-sibling::option[@value="&b"][not(@selected)][.="[trans]Choice&B[/trans]"]
  438. ]
  439. [count(./option)=3]
  440. '
  441. );
  442. }
  443. public function testSingleChoiceGrouped()
  444. {
  445. $form = $this->factory->createNamed('choice', 'na&me', '&a', array(
  446. 'property_path' => 'name',
  447. 'choices' => array(
  448. 'Group&1' => array('&a' => 'Choice&A', '&b' => 'Choice&B'),
  449. 'Group&2' => array('&c' => 'Choice&C'),
  450. ),
  451. 'multiple' => false,
  452. 'expanded' => false,
  453. ));
  454. $this->assertWidgetMatchesXpath($form->createView(), array(),
  455. '/select
  456. [@name="na&me"]
  457. [./optgroup[@label="[trans]Group&1[/trans]"]
  458. [
  459. ./option[@value="&a"][@selected="selected"][.="[trans]Choice&A[/trans]"]
  460. /following-sibling::option[@value="&b"][not(@selected)][.="[trans]Choice&B[/trans]"]
  461. ]
  462. [count(./option)=2]
  463. ]
  464. [./optgroup[@label="[trans]Group&2[/trans]"]
  465. [./option[@value="&c"][not(@selected)][.="[trans]Choice&C[/trans]"]]
  466. [count(./option)=1]
  467. ]
  468. [count(./optgroup)=2]
  469. '
  470. );
  471. }
  472. public function testMultipleChoice()
  473. {
  474. $form = $this->factory->createNamed('choice', 'na&me', array('&a'), array(
  475. 'property_path' => 'name',
  476. 'choices' => array('&a' => 'Choice&A', '&b' => 'Choice&B'),
  477. 'multiple' => true,
  478. 'expanded' => false,
  479. ));
  480. $this->assertWidgetMatchesXpath($form->createView(), array(),
  481. '/select
  482. [@name="na&me[]"]
  483. [@multiple="multiple"]
  484. [
  485. ./option[@value="&a"][@selected="selected"][.="[trans]Choice&A[/trans]"]
  486. /following-sibling::option[@value="&b"][not(@selected)][.="[trans]Choice&B[/trans]"]
  487. ]
  488. [count(./option)=2]
  489. '
  490. );
  491. }
  492. public function testMultipleChoiceSkipEmptyValue()
  493. {
  494. $form = $this->factory->createNamed('choice', 'na&me', array('&a'), array(
  495. 'property_path' => 'name',
  496. 'choices' => array('&a' => 'Choice&A', '&b' => 'Choice&B'),
  497. 'multiple' => true,
  498. 'expanded' => false,
  499. 'empty_value' => 'Test&Me'
  500. ));
  501. $this->assertWidgetMatchesXpath($form->createView(), array(),
  502. '/select
  503. [@name="na&me[]"]
  504. [@multiple="multiple"]
  505. [
  506. ./option[@value="&a"][@selected="selected"][.="[trans]Choice&A[/trans]"]
  507. /following-sibling::option[@value="&b"][not(@selected)][.="[trans]Choice&B[/trans]"]
  508. ]
  509. [count(./option)=2]
  510. '
  511. );
  512. }
  513. public function testMultipleChoiceNonRequired()
  514. {
  515. $form = $this->factory->createNamed('choice', 'na&me', array('&a'), array(
  516. 'property_path' => 'name',
  517. 'choices' => array('&a' => 'Choice&A', '&b' => 'Choice&B'),
  518. 'required' => false,
  519. 'multiple' => true,
  520. 'expanded' => false,
  521. ));
  522. $this->assertWidgetMatchesXpath($form->createView(), array(),
  523. '/select
  524. [@name="na&me[]"]
  525. [@multiple="multiple"]
  526. [
  527. ./option[@value="&a"][@selected="selected"][.="[trans]Choice&A[/trans]"]
  528. /following-sibling::option[@value="&b"][not(@selected)][.="[trans]Choice&B[/trans]"]
  529. ]
  530. [count(./option)=2]
  531. '
  532. );
  533. }
  534. public function testSingleChoiceExpanded()
  535. {
  536. $form = $this->factory->createNamed('choice', 'na&me', '&a', array(
  537. 'property_path' => 'name',
  538. 'choices' => array('&a' => 'Choice&A', '&b' => 'Choice&B'),
  539. 'multiple' => false,
  540. 'expanded' => true,
  541. ));
  542. $this->assertWidgetMatchesXpath($form->createView(), array(),
  543. '/div
  544. [
  545. ./input[@type="radio"][@name="na&me"][@id="na&me_&a"][@checked]
  546. /following-sibling::label[@for="na&me_&a"][.="[trans]Choice&A[/trans]"]
  547. /following-sibling::input[@type="radio"][@name="na&me"][@id="na&me_&b"][not(@checked)]
  548. /following-sibling::label[@for="na&me_&b"][.="[trans]Choice&B[/trans]"]
  549. ]
  550. [count(./input)=2]
  551. '
  552. );
  553. }
  554. public function testSingleChoiceExpandedSkipEmptyValue()
  555. {
  556. $form = $this->factory->createNamed('choice', 'na&me', '&a', array(
  557. 'property_path' => 'name',
  558. 'choices' => array('&a' => 'Choice&A', '&b' => 'Choice&B'),
  559. 'multiple' => false,
  560. 'expanded' => true,
  561. 'empty_value' => 'Test&Me'
  562. ));
  563. $this->assertWidgetMatchesXpath($form->createView(), array(),
  564. '/div
  565. [
  566. ./input[@type="radio"][@name="na&me"][@id="na&me_&a"][@checked]
  567. /following-sibling::label[@for="na&me_&a"][.="[trans]Choice&A[/trans]"]
  568. /following-sibling::input[@type="radio"][@name="na&me"][@id="na&me_&b"][not(@checked)]
  569. /following-sibling::label[@for="na&me_&b"][.="[trans]Choice&B[/trans]"]
  570. ]
  571. [count(./input)=2]
  572. '
  573. );
  574. }
  575. public function testSingleChoiceExpandedWithBooleanValue()
  576. {
  577. $form = $this->factory->createNamed('choice', 'na&me', true, array(
  578. 'property_path' => 'name',
  579. 'choices' => array('1' => 'Choice&A', '0' => 'Choice&B'),
  580. 'multiple' => false,
  581. 'expanded' => true,
  582. ));
  583. $this->assertWidgetMatchesXpath($form->createView(), array(),
  584. '/div
  585. [
  586. ./input[@type="radio"][@name="na&me"][@id="na&me_1"][@checked]
  587. /following-sibling::label[@for="na&me_1"][.="[trans]Choice&A[/trans]"]
  588. /following-sibling::input[@type="radio"][@name="na&me"][@id="na&me_0"][not(@checked)]
  589. /following-sibling::label[@for="na&me_0"][.="[trans]Choice&B[/trans]"]
  590. ]
  591. [count(./input)=2]
  592. '
  593. );
  594. }
  595. public function testMultipleChoiceExpanded()
  596. {
  597. $form = $this->factory->createNamed('choice', 'na&me', array('&a', '&c'), array(
  598. 'property_path' => 'name',
  599. 'choices' => array('&a' => 'Choice&A', '&b' => 'Choice&B', '&c' => 'Choice&C'),
  600. 'multiple' => true,
  601. 'expanded' => true,
  602. 'required' => true,
  603. ));
  604. $this->assertWidgetMatchesXpath($form->createView(), array(),
  605. '/div
  606. [
  607. ./input[@type="checkbox"][@name="na&me[&a]"][@id="na&me_&a"][@checked][not(@required)]
  608. /following-sibling::label[@for="na&me_&a"][.="[trans]Choice&A[/trans]"]
  609. /following-sibling::input[@type="checkbox"][@name="na&me[&b]"][@id="na&me_&b"][not(@checked)][not(@required)]
  610. /following-sibling::label[@for="na&me_&b"][.="[trans]Choice&B[/trans]"]
  611. /following-sibling::input[@type="checkbox"][@name="na&me[&c]"][@id="na&me_&c"][@checked][not(@required)]
  612. /following-sibling::label[@for="na&me_&c"][.="[trans]Choice&C[/trans]"]
  613. ]
  614. [count(./input)=3]
  615. '
  616. );
  617. }
  618. public function testCountry()
  619. {
  620. $form = $this->factory->createNamed('country', 'na&me', 'AT', array(
  621. 'property_path' => 'name',
  622. ));
  623. $this->assertWidgetMatchesXpath($form->createView(), array(),
  624. '/select
  625. [@name="na&me"]
  626. [./option[@value="AT"][@selected="selected"][.="[trans]Austria[/trans]"]]
  627. [count(./option)>200]
  628. '
  629. );
  630. }
  631. public function testCountryWithEmptyValue()
  632. {
  633. $form = $this->factory->createNamed('country', 'na&me', 'AT', array(
  634. 'property_path' => 'name',
  635. 'empty_value' => 'Select&Country',
  636. 'required' => false,
  637. ));
  638. $this->assertWidgetMatchesXpath($form->createView(), array(),
  639. '/select
  640. [@name="na&me"]
  641. [./option[@value=""][not(@selected)][.="[trans]Select&Country[/trans]"]]
  642. [./option[@value="AT"][@selected="selected"][.="[trans]Austria[/trans]"]]
  643. [count(./option)>201]
  644. '
  645. );
  646. }
  647. public function testCsrf()
  648. {
  649. $this->csrfProvider->expects($this->any())
  650. ->method('generateCsrfToken')
  651. ->will($this->returnValue('foo&bar'));
  652. $form = $this->factory->createNamed('csrf', 'na&me', null, array(
  653. 'property_path' => 'name',
  654. ));
  655. $this->assertWidgetMatchesXpath($form->createView(), array(),
  656. '/input
  657. [@type="hidden"]
  658. [@value="foo&bar"]
  659. '
  660. );
  661. }
  662. public function testDateTime()
  663. {
  664. $form = $this->factory->createNamed('datetime', 'na&me', '2011-02-03 04:05:06', array(
  665. 'property_path' => 'name',
  666. 'input' => 'string',
  667. 'with_seconds' => false,
  668. ));
  669. $this->assertWidgetMatchesXpath($form->createView(), array(),
  670. '/div
  671. [
  672. ./div
  673. [@id="na&me_date"]
  674. [
  675. ./select
  676. [@id="na&me_date_month"]
  677. [./option[@value="2"][@selected="selected"]]
  678. /following-sibling::select
  679. [@id="na&me_date_day"]
  680. [./option[@value="3"][@selected="selected"]]
  681. /following-sibling::select
  682. [@id="na&me_date_year"]
  683. [./option[@value="2011"][@selected="selected"]]
  684. ]
  685. /following-sibling::div
  686. [@id="na&me_time"]
  687. [
  688. ./select
  689. [@id="na&me_time_hour"]
  690. [./option[@value="4"][@selected="selected"]]
  691. /following-sibling::select
  692. [@id="na&me_time_minute"]
  693. [./option[@value="5"][@selected="selected"]]
  694. ]
  695. ]
  696. [count(.//select)=5]
  697. '
  698. );
  699. }
  700. public function testDateTimeWithEmptyValueGlobal()
  701. {
  702. $form = $this->factory->createNamed('datetime', 'na&me', null, array(
  703. 'property_path' => 'name',
  704. 'input' => 'string',
  705. 'empty_value' => 'Change&Me',
  706. 'required' => false,
  707. ));
  708. $this->assertWidgetMatchesXpath($form->createView(), array(),
  709. '/div
  710. [
  711. ./div
  712. [@id="na&me_date"]
  713. [
  714. ./select
  715. [@id="na&me_date_month"]
  716. [./option[@value=""][.="[trans]Change&Me[/trans]"]]
  717. /following-sibling::select
  718. [@id="na&me_date_day"]
  719. [./option[@value=""][.="[trans]Change&Me[/trans]"]]
  720. /following-sibling::select
  721. [@id="na&me_date_year"]
  722. [./option[@value=""][.="[trans]Change&Me[/trans]"]]
  723. ]
  724. /following-sibling::div
  725. [@id="na&me_time"]
  726. [
  727. ./select
  728. [@id="na&me_time_hour"]
  729. [./option[@value=""][.="[trans]Change&Me[/trans]"]]
  730. /following-sibling::select
  731. [@id="na&me_time_minute"]
  732. [./option[@value=""][.="[trans]Change&Me[/trans]"]]
  733. ]
  734. ]
  735. [count(.//select)=5]
  736. '
  737. );
  738. }
  739. public function testDateTimeWithEmptyValueOnTime()
  740. {
  741. $form = $this->factory->createNamed('datetime', 'na&me', '2011-02-03', array(
  742. 'property_path' => 'name',
  743. 'input' => 'string',
  744. 'empty_value' => array('hour' => 'Change&Me', 'minute' => 'Change&Me'),
  745. 'required' => false,
  746. ));
  747. $this->assertWidgetMatchesXpath($form->createView(), array(),
  748. '/div
  749. [
  750. ./div
  751. [@id="na&me_date"]
  752. [
  753. ./select
  754. [@id="na&me_date_month"]
  755. [./option[@value="2"][@selected="selected"]]
  756. /following-sibling::select
  757. [@id="na&me_date_day"]
  758. [./option[@value="3"][@selected="selected"]]
  759. /following-sibling::select
  760. [@id="na&me_date_year"]
  761. [./option[@value="2011"][@selected="selected"]]
  762. ]
  763. /following-sibling::div
  764. [@id="na&me_time"]
  765. [
  766. ./select
  767. [@id="na&me_time_hour"]
  768. [./option[@value=""][.="[trans]Change&Me[/trans]"]]
  769. /following-sibling::select
  770. [@id="na&me_time_minute"]
  771. [./option[@value=""][.="[trans]Change&Me[/trans]"]]
  772. ]
  773. ]
  774. [count(.//select)=5]
  775. '
  776. );
  777. }
  778. public function testDateTimeWithSeconds()
  779. {
  780. $form = $this->factory->createNamed('datetime', 'na&me', '2011-02-03 04:05:06', array(
  781. 'property_path' => 'name',
  782. 'input' => 'string',
  783. 'with_seconds' => true,
  784. ));
  785. $this->assertWidgetMatchesXpath($form->createView(), array(),
  786. '/div
  787. [
  788. ./div
  789. [@id="na&me_date"]
  790. [
  791. ./select
  792. [@id="na&me_date_month"]
  793. [./option[@value="2"][@selected="selected"]]
  794. /following-sibling::select
  795. [@id="na&me_date_day"]
  796. [./option[@value="3"][@selected="selected"]]
  797. /following-sibling::select
  798. [@id="na&me_date_year"]
  799. [./option[@value="2011"][@selected="selected"]]
  800. ]
  801. /following-sibling::div
  802. [@id="na&me_time"]
  803. [
  804. ./select
  805. [@id="na&me_time_hour"]
  806. [./option[@value="4"][@selected="selected"]]
  807. /following-sibling::select
  808. [@id="na&me_time_minute"]
  809. [./option[@value="5"][@selected="selected"]]
  810. /following-sibling::select
  811. [@id="na&me_time_second"]
  812. [./option[@value="6"][@selected="selected"]]
  813. ]
  814. ]
  815. [count(.//select)=6]
  816. '
  817. );
  818. }
  819. public function testDateTimeSingleText()
  820. {
  821. $form = $this->factory->createNamed('datetime', 'na&me', '2011-02-03 04:05:06', array(
  822. 'property_path' => 'name',
  823. 'input' => 'string',
  824. 'date_widget' => 'single_text',
  825. 'time_widget' => 'single_text',
  826. ));
  827. $this->assertWidgetMatchesXpath($form->createView(), array(),
  828. '/div
  829. [
  830. ./input
  831. [@type="text"]
  832. [@id="na&me_date"]
  833. [@name="na&me[date]"]
  834. [@value="Feb 3, 2011"]
  835. /following-sibling::input
  836. [@type="text"]
  837. [@id="na&me_time"]
  838. [@name="na&me[time]"]
  839. [@value="04:05:00"]
  840. ]
  841. '
  842. );
  843. }
  844. public function testDateTimeWithWidgetSingleText()
  845. {
  846. $form = $this->factory->createNamed('datetime', 'name', '2011-02-03 04:05:06', array(
  847. 'property_path' => 'name',
  848. 'input' => 'string',
  849. 'widget' => 'single_text',
  850. ));
  851. $this->assertWidgetMatchesXpath($form->createView(), array(),
  852. '/input
  853. [@type="text"]
  854. [@name="name"]
  855. [@value="2011-02-03 04:05:00"]
  856. '
  857. );
  858. }
  859. public function testDateTimeWithWidgetSingleTextIgnoreDateAndTimeWidgets()
  860. {
  861. $form = $this->factory->createNamed('datetime', 'na&me', '2011-02-03 04:05:06', array(
  862. 'property_path' => 'name',
  863. 'input' => 'string',
  864. 'date_widget' => 'choice',
  865. 'time_widget' => 'choice',
  866. 'widget' => 'single_text',
  867. ));
  868. $this->assertWidgetMatchesXpath($form->createView(), array(),
  869. '/input
  870. [@type="text"]
  871. [@name="na&me"]
  872. [@value="2011-02-03 04:05:00"]
  873. '
  874. );
  875. }
  876. public function testDateChoice()
  877. {
  878. $form = $this->factory->createNamed('date', 'na&me', '2011-02-03', array(
  879. 'property_path' => 'name',
  880. 'input' => 'string',
  881. 'widget' => 'choice',
  882. ));
  883. $this->assertWidgetMatchesXpath($form->createView(), array(),
  884. '/div
  885. [
  886. ./select
  887. [@id="na&me_month"]
  888. [./option[@value="2"][@selected="selected"]]
  889. /following-sibling::select
  890. [@id="na&me_day"]
  891. [./option[@value="3"][@selected="selected"]]
  892. /following-sibling::select
  893. [@id="na&me_year"]
  894. [./option[@value="2011"][@selected="selected"]]
  895. ]
  896. [count(./select)=3]
  897. '
  898. );
  899. }
  900. public function testDateChoiceWithEmptyValueGlobal()
  901. {
  902. $form = $this->factory->createNamed('date', 'na&me', null, array(
  903. 'property_path' => 'name',
  904. 'input' => 'string',
  905. 'widget' => 'choice',
  906. 'empty_value' => 'Change&Me',
  907. 'required' => false,
  908. ));
  909. $this->assertWidgetMatchesXpath($form->createView(), array(),
  910. '/div
  911. [
  912. ./select
  913. [@id="na&me_month"]
  914. [./option[@value=""][.="[trans]Change&Me[/trans]"]]
  915. /following-sibling::select
  916. [@id="na&me_day"]
  917. [./option[@value=""][.="[trans]Change&Me[/trans]"]]
  918. /following-sibling::select
  919. [@id="na&me_year"]
  920. [./option[@value=""][.="[trans]Change&Me[/trans]"]]
  921. ]
  922. [count(./select)=3]
  923. '
  924. );
  925. }
  926. public function testDateChoiceWithEmptyValueOnYear()
  927. {
  928. $form = $this->factory->createNamed('date', 'na&me', null, array(
  929. 'property_path' => 'name',
  930. 'input' => 'string',
  931. 'widget' => 'choice',
  932. 'required' => false,
  933. 'empty_value' => array('year' => 'Change&Me'),
  934. ));
  935. $this->assertWidgetMatchesXpath($form->createView(), array(),
  936. '/div
  937. [
  938. ./select
  939. [@id="na&me_month"]
  940. [./option[@value="1"]]
  941. /following-sibling::select
  942. [@id="na&me_day"]
  943. [./option[@value="1"]]
  944. /following-sibling::select
  945. [@id="na&me_year"]
  946. [./option[@value=""][.="[trans]Change&Me[/trans]"]]
  947. ]
  948. [count(./select)=3]
  949. '
  950. );
  951. }
  952. public function testDateText()
  953. {
  954. $form = $this->factory->createNamed('date', 'na&me', '2011-02-03', array(
  955. 'property_path' => 'name',
  956. 'input' => 'string',
  957. 'widget' => 'text',
  958. ));
  959. $this->assertWidgetMatchesXpath($form->createView(), array(),
  960. '/div
  961. [
  962. ./input
  963. [@id="na&me_month"]
  964. [@type="text"]
  965. [@value="2"]
  966. /following-sibling::input
  967. [@id="na&me_day"]
  968. [@type="text"]
  969. [@value="3"]
  970. /following-sibling::input
  971. [@id="na&me_year"]
  972. [@type="text"]
  973. [@value="2011"]
  974. ]
  975. [count(./input)=3]
  976. '
  977. );
  978. }
  979. public function testDateSingleText()
  980. {
  981. $form = $this->factory->createNamed('date', 'na&me', '2011-02-03', array(
  982. 'property_path' => 'name',
  983. 'input' => 'string',
  984. 'widget' => 'single_text',
  985. ));
  986. $this->assertWidgetMatchesXpath($form->createView(), array(),
  987. '/input
  988. [@type="text"]
  989. [@name="na&me"]
  990. [@value="Feb 3, 2011"]
  991. '
  992. );
  993. }
  994. public function testBirthDay()
  995. {
  996. $form = $this->factory->createNamed('birthday', 'na&me', '2000-02-03', array(
  997. 'property_path' => 'name',
  998. 'input' => 'string',
  999. ));
  1000. $this->assertWidgetMatchesXpath($form->createView(), array(),
  1001. '/div
  1002. [
  1003. ./select
  1004. [@id="na&me_month"]
  1005. [./option[@value="2"][@selected="selected"]]
  1006. /following-sibling::select
  1007. [@id="na&me_day"]
  1008. [./option[@value="3"][@selected="selected"]]
  1009. /following-sibling::select
  1010. [@id="na&me_year"]
  1011. [./option[@value="2000"][@selected="selected"]]
  1012. ]
  1013. [count(./select)=3]
  1014. '
  1015. );
  1016. }
  1017. public function testBirthDayWithEmptyValue()
  1018. {
  1019. $form = $this->factory->createNamed('birthday', 'na&me', '1950-01-01', array(
  1020. 'property_path' => 'name',
  1021. 'input' => 'string',
  1022. 'empty_value' => '',
  1023. 'required' => false,
  1024. ));
  1025. $this->assertWidgetMatchesXpath($form->createView(), array(),
  1026. '/div
  1027. [
  1028. ./select
  1029. [@id="na&me_month"]
  1030. [./option[@value=""][.="[trans][/trans]"]]
  1031. [./option[@value="1"][@selected="selected"]]
  1032. /following-sibling::select
  1033. [@id="na&me_day"]
  1034. [./option[@value=""][.="[trans][/trans]"]]
  1035. [./option[@value="1"][@selected="selected"]]
  1036. /following-sibling::select
  1037. [@id="na&me_year"]
  1038. [./option[@value=""][.="[trans][/trans]"]]
  1039. [./option[@value="1950"][@selected="selected"]]
  1040. ]
  1041. [count(./select)=3]
  1042. '
  1043. );
  1044. }
  1045. public function testEmail()
  1046. {
  1047. $form = $this->factory->createNamed('email', 'na&me', 'foo&bar', array(
  1048. 'property_path' => 'name',
  1049. ));
  1050. $this->assertWidgetMatchesXpath($form->createView(), array(),
  1051. '/input
  1052. [@type="email"]
  1053. [@name="na&me"]
  1054. [@value="foo&bar"]
  1055. [not(@maxlength)]
  1056. '
  1057. );
  1058. }
  1059. public function testEmailWithMaxLength()
  1060. {
  1061. $form = $this->factory->createNamed('email', 'na&me', 'foo&bar', array(
  1062. 'property_path' => 'name',
  1063. 'max_length' => 123,
  1064. ));
  1065. $this->assertWidgetMatchesXpath($form->createView(), array(),
  1066. '/input
  1067. [@type="email"]
  1068. [@name="na&me"]
  1069. [@value="foo&bar"]
  1070. [@maxlength="123"]
  1071. '
  1072. );
  1073. }
  1074. public function testFile()
  1075. {
  1076. $form = $this->factory->createNamed('file', 'na&me', null, array(
  1077. 'property_path' => 'name',
  1078. ));
  1079. $this->assertWidgetMatchesXpath($form->createView(), array(),
  1080. '/input
  1081. [@type="file"]
  1082. '
  1083. );
  1084. }
  1085. public function testHidden()
  1086. {
  1087. $form = $this->factory->createNamed('hidden', 'na&me', 'foo&bar', array(
  1088. 'property_path' => 'name',
  1089. ));
  1090. $this->assertWidgetMatchesXpath($form->createView(), array(),
  1091. '/input
  1092. [@type="hidden"]
  1093. [@name="na&me"]
  1094. [@value="foo&bar"]
  1095. '
  1096. );
  1097. }
  1098. public function testInteger()
  1099. {
  1100. $form = $this->factory->createNamed('integer', 'na&me', 123, array(
  1101. 'property_path' => 'name',
  1102. ));
  1103. $this->assertWidgetMatchesXpath($form->createView(), array(),
  1104. '/input
  1105. [@type="number"]
  1106. [@name="na&me"]
  1107. [@value="123"]
  1108. '
  1109. );
  1110. }
  1111. public function testLanguage()
  1112. {
  1113. $form = $this->factory->createNamed('language', 'na&me', 'de', array(
  1114. 'property_path' => 'name',
  1115. ));
  1116. $this->assertWidgetMatchesXpath($form->createView(), array(),
  1117. '/select
  1118. [@name="na&me"]
  1119. [./option[@value="de"][@selected="selected"][.="[trans]German[/trans]"]]
  1120. [count(./option)>200]
  1121. '
  1122. );
  1123. }
  1124. public function testLocale()
  1125. {
  1126. $form = $this->factory->createNamed('locale', 'na&me', 'de_AT', array(
  1127. 'property_path' => 'name',
  1128. ));
  1129. $this->assertWidgetMatchesXpath($form->createView(), array(),
  1130. '/select
  1131. [@name="na&me"]
  1132. [./option[@value="de_AT"][@selected="selected"][.="[trans]German (Austria)[/trans]"]]
  1133. [count(./option)>200]
  1134. '
  1135. );
  1136. }
  1137. public function testMoney()
  1138. {
  1139. $form = $this->factory->createNamed('money', 'na&me', 1234.56, array(
  1140. 'property_path' => 'name',
  1141. 'currency' => 'EUR',
  1142. ));
  1143. $this->assertWidgetMatchesXpath($form->createView(), array(),
  1144. '/input
  1145. [@type="text"]
  1146. [@name="na&me"]
  1147. [@value="1234.56"]
  1148. [contains(.., "€")]
  1149. '
  1150. );
  1151. }
  1152. public function testNumber()
  1153. {
  1154. $form = $this->factory->createNamed('number', 'na&me', 1234.56, array(
  1155. 'property_path' => 'name',
  1156. ));
  1157. $this->assertWidgetMatchesXpath($form->createView(), array(),
  1158. '/input
  1159. [@type="text"]
  1160. [@name="na&me"]
  1161. [@value="1234.56"]
  1162. '
  1163. );
  1164. }
  1165. public function testPassword()
  1166. {
  1167. $form = $this->factory->createNamed('password', 'na&me', 'foo&bar', array(
  1168. 'property_path' => 'name',
  1169. ));
  1170. $this->assertWidgetMatchesXpath($form->createView(), array(),
  1171. '/input
  1172. [@type="password"]
  1173. [@name="na&me"]
  1174. [@value=""]
  1175. '
  1176. );
  1177. }
  1178. public function testPasswordBoundNotAlwaysEmpty()
  1179. {
  1180. $form = $this->factory->createNamed('password', 'na&me', null, array(
  1181. 'property_path' => 'name',
  1182. 'always_empty' => false,
  1183. ));
  1184. $form->bind('foo&bar');
  1185. $this->assertWidgetMatchesXpath($form->createView(), array(),
  1186. '/input
  1187. [@type="password"]
  1188. [@name="na&me"]
  1189. [@value="foo&bar"]
  1190. '
  1191. );
  1192. }
  1193. public function testPasswordWithMaxLength()
  1194. {
  1195. $form = $this->factory->createNamed('password', 'na&me', 'foo&bar', array(
  1196. 'property_path' => 'name',
  1197. 'max_length' => 123,
  1198. ));
  1199. $this->assertWidgetMatchesXpath($form->createView(), array(),
  1200. '/input
  1201. [@type="password"]
  1202. [@name="na&me"]
  1203. [@value=""]
  1204. [@maxlength="123"]
  1205. '
  1206. );
  1207. }
  1208. public function testPercent()
  1209. {
  1210. $form = $this->factory->createNamed('percent', 'na&me', 0.1, array(
  1211. 'property_path' => 'name',
  1212. ));
  1213. $this->assertWidgetMatchesXpath($form->createView(), array(),
  1214. '/input
  1215. [@type="text"]
  1216. [@name="na&me"]
  1217. [@value="10"]
  1218. [contains(.., "%")]
  1219. '
  1220. );
  1221. }
  1222. public function testCheckedRadio()
  1223. {
  1224. $form = $this->factory->createNamed('radio', 'na&me', true, array(
  1225. 'property_path' => 'name',
  1226. ));
  1227. $this->assertWidgetMatchesXpath($form->createView(), array(),
  1228. '/input
  1229. [@type="radio"]
  1230. [@name="na&me"]
  1231. [@checked="checked"]
  1232. [@value=""]
  1233. '
  1234. );
  1235. }
  1236. public function testCheckedRadioWithValue()
  1237. {
  1238. $form = $this->factory->createNamed('radio', 'na&me', true, array(
  1239. 'property_path' => 'name',
  1240. 'value' => 'foo&bar',
  1241. ));
  1242. $this->assertWidgetMatchesXpath($form->createView(), array(),
  1243. '/input
  1244. [@type="radio"]
  1245. [@name="na&me"]
  1246. [@checked="checked"]
  1247. [@value="foo&bar"]
  1248. '
  1249. );
  1250. }
  1251. public function testUncheckedRadio()
  1252. {
  1253. $form = $this->factory->createNamed('radio', 'na&me', false, array(
  1254. 'property_path' => 'name',
  1255. ));
  1256. $this->assertWidgetMatchesXpath($form->createView(), array(),
  1257. '/input
  1258. [@type="radio"]
  1259. [@name="na&me"]
  1260. [not(@checked)]
  1261. '
  1262. );
  1263. }
  1264. public function testTextarea()
  1265. {
  1266. $form = $this->factory->createNamed('textarea', 'na&me', 'foo&bar', array(
  1267. 'property_path' => 'name',
  1268. ));
  1269. $this->assertWidgetMatchesXpath($form->createView(), array(),
  1270. '/textarea
  1271. [@name="na&me"]
  1272. [.="foo&bar"]
  1273. '
  1274. );
  1275. }
  1276. public function testText()
  1277. {
  1278. $form = $this->factory->createNamed('text', 'na&me', 'foo&bar', array(
  1279. 'property_path' => 'name',
  1280. ));
  1281. $this->assertWidgetMatchesXpath($form->createView(), array(),
  1282. '/input
  1283. [@type="text"]
  1284. [@name="na&me"]
  1285. [@value="foo&bar"]
  1286. [not(@maxlength)]
  1287. '
  1288. );
  1289. }
  1290. public function testTextWithMaxLength()
  1291. {
  1292. $form = $this->factory->createNamed('text', 'na&me', 'foo&bar', array(
  1293. 'property_path' => 'name',
  1294. 'max_length' => 123,
  1295. ));
  1296. $this->assertWidgetMatchesXpath($form->createView(), array(),
  1297. '/input
  1298. [@type="text"]
  1299. [@name="na&me"]
  1300. [@value="foo&bar"]
  1301. [@maxlength="123"]
  1302. '
  1303. );
  1304. }
  1305. public function testSearch()
  1306. {
  1307. $form = $this->factory->createNamed('search', 'na&me', 'foo&bar', array(
  1308. 'property_path' => 'name',
  1309. ));
  1310. $this->assertWidgetMatchesXpath($form->createView(), array(),
  1311. '/input
  1312. [@type="search"]
  1313. [@name="na&me"]
  1314. [@value="foo&bar"]
  1315. [not(@maxlength)]
  1316. '
  1317. );
  1318. }
  1319. public function testTime()
  1320. {
  1321. $form = $this->factory->createNamed('time', 'na&me', '04:05:06', array(
  1322. 'property_path' => 'name',
  1323. 'input' => 'string',
  1324. 'with_seconds' => false,
  1325. ));
  1326. $this->assertWidgetMatchesXpath($form->createView(), array(),
  1327. '/div
  1328. [
  1329. ./select
  1330. [@id="na&me_hour"]
  1331. [@size="1"]
  1332. [./option[@value="4"][@selected="selected"]]
  1333. /following-sibling::select
  1334. [@id="na&me_minute"]
  1335. [@size="1"]
  1336. [./option[@value="5"][@selected="selected"]]
  1337. ]
  1338. [count(./select)=2]
  1339. '
  1340. );
  1341. }
  1342. public function testTimeWithSeconds()
  1343. {
  1344. $form = $this->factory->createNamed('time', 'na&me', '04:05:06', array(
  1345. 'property_path' => 'name',
  1346. 'input' => 'string',
  1347. 'with_seconds' => true,
  1348. ));
  1349. $this->assertWidgetMatchesXpath($form->createView(), array(),
  1350. '/div
  1351. [
  1352. ./select
  1353. [@id="na&me_hour"]
  1354. [@size="1"]
  1355. [./option[@value="4"][@selected="selected"]]
  1356. [count(./option)>23]
  1357. /following-sibling::select
  1358. [@id="na&me_minute"]
  1359. [@size="1"]
  1360. [./option[@value="5"][@selected="selected"]]
  1361. [count(./option)>59]
  1362. /following-sibling::select
  1363. [@id="na&me_second"]
  1364. [@size="1"]
  1365. [./option[@value="6"][@selected="selected"]]
  1366. [count(./option)>59]
  1367. ]
  1368. [count(./select)=3]
  1369. '
  1370. );
  1371. }
  1372. public function testTimeText()
  1373. {
  1374. $form = $this->factory->createNamed('time', 'na&me', '04:05:06', array(
  1375. 'property_path' => 'name',
  1376. 'input' => 'string',
  1377. 'widget' => 'text',
  1378. ));
  1379. $this->assertWidgetMatchesXpath($form->createView(), array(),
  1380. '/div
  1381. [
  1382. ./input
  1383. [@type="text"]
  1384. [@id="na&me_hour"]
  1385. [@name="na&me[hour]"]
  1386. [@value="04"]
  1387. [@size="1"]
  1388. [@required="required"]
  1389. /following-sibling::input
  1390. [@type="text"]
  1391. [@id="na&me_minute"]
  1392. [@name="na&me[minute]"]
  1393. [@value="05"]
  1394. [@size="1"]
  1395. [@required="required"]
  1396. ]
  1397. [count(./input)=2]
  1398. '
  1399. );
  1400. }
  1401. public function testTimeSingleText()
  1402. {
  1403. $form = $this->factory->createNamed('time', 'na&me', '04:05:06', array(
  1404. 'property_path' => 'name',
  1405. 'input' => 'string',
  1406. 'widget' => 'single_text',
  1407. ));
  1408. $this->assertWidgetMatchesXpath($form->createView(), array(),
  1409. '/input
  1410. [@type="text"]
  1411. [@name="na&me"]
  1412. [@value="04:05:00"]
  1413. '
  1414. );
  1415. }
  1416. public function testTimeWithEmptyValueGlobal()
  1417. {
  1418. $form = $this->factory->createNamed('time', 'na&me', null, array(
  1419. 'property_path' => 'name',
  1420. 'input' => 'string',
  1421. 'empty_value' => 'Change&Me',
  1422. 'required' => false,
  1423. ));
  1424. $this->assertWidgetMatchesXpath($form->createView(), array(),
  1425. '/div
  1426. [
  1427. ./select
  1428. [@id="na&me_hour"]
  1429. [./option[@value=""][.="[trans]Change&Me[/trans]"]]
  1430. [count(./option)>24]
  1431. /following-sibling::select
  1432. [@id="na&me_minute"]
  1433. [./option[@value=""][.="[trans]Change&Me[/trans]"]]
  1434. [count(./option)>60]
  1435. ]
  1436. [count(./select)=2]
  1437. '
  1438. );
  1439. }
  1440. public function testTimeWithEmptyValueOnYear()
  1441. {
  1442. $form = $this->factory->createNamed('time', 'na&me', null, array(
  1443. 'property_path' => 'name',
  1444. 'input' => 'string',
  1445. 'required' => false,
  1446. 'empty_value' => array('hour' => 'Change&Me'),
  1447. ));
  1448. $this->assertWidgetMatchesXpath($form->createView(), array(),
  1449. '/div
  1450. [
  1451. ./select
  1452. [@id="na&me_hour"]
  1453. [./option[@value=""][.="[trans]Change&Me[/trans]"]]
  1454. [count(./option)>24]
  1455. /following-sibling::select
  1456. [@id="na&me_minute"]
  1457. [./option[@value="1"]]
  1458. [count(./option)>59]
  1459. ]
  1460. [count(./select)=2]
  1461. '
  1462. );
  1463. }
  1464. public function testTimezone()
  1465. {
  1466. $form = $this->factory->createNamed('timezone', 'na&me', 'Europe/Vienna', array(
  1467. 'property_path' => 'name',
  1468. ));
  1469. $this->assertWidgetMatchesXpath($form->createView(), array(),
  1470. '/select
  1471. [@name="na&me"]
  1472. [@required="required"]
  1473. [./optgroup
  1474. [@label="[trans]Europe[/trans]"]
  1475. [./option[@value="Europe/Vienna"][@selected="selected"][.="[trans]Vienna[/trans]"]]
  1476. ]
  1477. [count(./optgroup)>10]
  1478. [count(.//option)>200]
  1479. '
  1480. );
  1481. }
  1482. public function testTimezoneWithEmptyValue()
  1483. {
  1484. $form = $this->factory->createNamed('timezone', 'na&me', null, array(
  1485. 'property_path' => 'name',
  1486. 'empty_value' => 'Select&Timezone',
  1487. 'required' => false,
  1488. ));
  1489. $this->assertWidgetMatchesXpath($form->createView(), array(),
  1490. '/select
  1491. [./option[@value=""][.="[trans]Select&Timezone[/trans]"]]
  1492. [count(./optgroup)>10]
  1493. [count(.//option)>201]
  1494. '
  1495. );
  1496. }
  1497. public function testUrl()
  1498. {
  1499. $url = 'http://www.google.com?foo1=bar1&foo2=bar2';
  1500. $form = $this->factory->createNamed('url', 'na&me', $url, array(
  1501. 'property_path' => 'name',
  1502. ));
  1503. $this->assertWidgetMatchesXpath($form->createView(), array(),
  1504. '/input
  1505. [@type="url"]
  1506. [@name="na&me"]
  1507. [@value="http://www.google.com?foo1=bar1&foo2=bar2"]
  1508. '
  1509. );
  1510. }
  1511. public function testCollectionPrototype()
  1512. {
  1513. $form = $this->factory->createNamedBuilder('form', 'na&me', array('items' => array('one', 'two', 'three')))
  1514. ->add('items', 'collection', array('allow_add' => true))
  1515. ->getForm()
  1516. ->createView();
  1517. $html = $this->renderWidget($form);
  1518. $this->assertMatchesXpath($html,
  1519. '//div[@id="na&me_items"][@data-prototype]
  1520. |
  1521. //table[@id="na&me_items"][@data-prototype]
  1522. '
  1523. );
  1524. }
  1525. }