AbstractLayoutTest.php 46 KB

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