AbstractLayoutTest.php 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684
  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 required"]
  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 required"]
  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"][.="[trans]Choice&A[/trans]"]
  288. /following-sibling::option[@value="&b"][not(@selected)][.="[trans]Choice&B[/trans]"]
  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)][.="[trans]Choice&B[/trans]"]
  309. /following-sibling::option[@disabled="disabled"][not(@selected)][.="-- sep --"]
  310. /following-sibling::option[@value="&a"][@selected="selected"][.="[trans]Choice&A[/trans]"]
  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"][.="[trans]Choice&A[/trans]"]
  332. /following-sibling::option[@value="&b"][not(@selected)][.="[trans]Choice&B[/trans]"]
  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)][.="[trans]Choice&A[/trans]"]
  354. /following-sibling::option[@value="&b"][not(@selected)][.="[trans]Choice&B[/trans]"]
  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"][.="[trans]Choice&A[/trans]"]
  377. /following-sibling::option[@value="&b"][not(@selected)][.="[trans]Choice&B[/trans]"]
  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"][.="[trans]Choice&A[/trans]"]
  400. /following-sibling::option[@value="&b"][not(@selected)][.="[trans]Choice&B[/trans]"]
  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"][.="[trans]Choice&A[/trans]"]
  422. /following-sibling::option[@value="&b"][not(@selected)][.="[trans]Choice&B[/trans]"]
  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="[trans]Group&1[/trans]"]
  443. [
  444. ./option[@value="&a"][@selected="selected"][.="[trans]Choice&A[/trans]"]
  445. /following-sibling::option[@value="&b"][not(@selected)][.="[trans]Choice&B[/trans]"]
  446. ]
  447. [count(./option)=2]
  448. ]
  449. [./optgroup[@label="[trans]Group&2[/trans]"]
  450. [./option[@value="&c"][not(@selected)][.="[trans]Choice&C[/trans]"]]
  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"][.="[trans]Choice&A[/trans]"]
  471. /following-sibling::option[@value="&b"][not(@selected)][.="[trans]Choice&B[/trans]"]
  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"][.="[trans]Choice&A[/trans]"]
  492. /following-sibling::option[@value="&b"][not(@selected)][.="[trans]Choice&B[/trans]"]
  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"][.="[trans]Choice&A[/trans]"]
  513. /following-sibling::option[@value="&b"][not(@selected)][.="[trans]Choice&B[/trans]"]
  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"][.="[trans]Austria[/trans]"]]
  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"][.="[trans]Austria[/trans]"]]
  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. [@id="na&me_time"]
  823. [@name="na&me[time]"]
  824. [@value="04:05:00"]
  825. ]
  826. '
  827. );
  828. }
  829. public function testDateTimeWithWidgetSingleText()
  830. {
  831. $form = $this->factory->createNamed('datetime', 'name', '2011-02-03 04:05:06', array(
  832. 'property_path' => 'name',
  833. 'input' => 'string',
  834. 'widget' => 'single_text',
  835. ));
  836. $this->assertWidgetMatchesXpath($form->createView(), array(),
  837. '/input
  838. [@type="text"]
  839. [@name="name"]
  840. [@value="2011-02-03 04:05:00"]
  841. '
  842. );
  843. }
  844. public function testDateTimeWithWidgetSingleTextIgnoreDateAndTimeWidgets()
  845. {
  846. $form = $this->factory->createNamed('datetime', 'na&me', '2011-02-03 04:05:06', array(
  847. 'property_path' => 'name',
  848. 'input' => 'string',
  849. 'date_widget' => 'choice',
  850. 'time_widget' => 'choice',
  851. 'widget' => 'single_text',
  852. ));
  853. $this->assertWidgetMatchesXpath($form->createView(), array(),
  854. '/input
  855. [@type="text"]
  856. [@name="na&me"]
  857. [@value="2011-02-03 04:05:00"]
  858. '
  859. );
  860. }
  861. public function testDateChoice()
  862. {
  863. $form = $this->factory->createNamed('date', 'na&me', '2011-02-03', array(
  864. 'property_path' => 'name',
  865. 'input' => 'string',
  866. 'widget' => 'choice',
  867. ));
  868. $this->assertWidgetMatchesXpath($form->createView(), array(),
  869. '/div
  870. [
  871. ./select
  872. [@id="na&me_month"]
  873. [./option[@value="2"][@selected="selected"]]
  874. /following-sibling::select
  875. [@id="na&me_day"]
  876. [./option[@value="3"][@selected="selected"]]
  877. /following-sibling::select
  878. [@id="na&me_year"]
  879. [./option[@value="2011"][@selected="selected"]]
  880. ]
  881. [count(./select)=3]
  882. '
  883. );
  884. }
  885. public function testDateChoiceWithEmptyValueGlobal()
  886. {
  887. $form = $this->factory->createNamed('date', 'na&me', null, array(
  888. 'property_path' => 'name',
  889. 'input' => 'string',
  890. 'widget' => 'choice',
  891. 'empty_value' => 'Change&Me',
  892. 'required' => false,
  893. ));
  894. $this->assertWidgetMatchesXpath($form->createView(), array(),
  895. '/div
  896. [
  897. ./select
  898. [@id="na&me_month"]
  899. [./option[@value=""][.="[trans]Change&Me[/trans]"]]
  900. /following-sibling::select
  901. [@id="na&me_day"]
  902. [./option[@value=""][.="[trans]Change&Me[/trans]"]]
  903. /following-sibling::select
  904. [@id="na&me_year"]
  905. [./option[@value=""][.="[trans]Change&Me[/trans]"]]
  906. ]
  907. [count(./select)=3]
  908. '
  909. );
  910. }
  911. public function testDateChoiceWithEmptyValueOnYear()
  912. {
  913. $form = $this->factory->createNamed('date', 'na&me', null, array(
  914. 'property_path' => 'name',
  915. 'input' => 'string',
  916. 'widget' => 'choice',
  917. 'required' => false,
  918. 'empty_value' => array('year' => 'Change&Me'),
  919. ));
  920. $this->assertWidgetMatchesXpath($form->createView(), array(),
  921. '/div
  922. [
  923. ./select
  924. [@id="na&me_month"]
  925. [./option[@value="1"]]
  926. /following-sibling::select
  927. [@id="na&me_day"]
  928. [./option[@value="1"]]
  929. /following-sibling::select
  930. [@id="na&me_year"]
  931. [./option[@value=""][.="[trans]Change&Me[/trans]"]]
  932. ]
  933. [count(./select)=3]
  934. '
  935. );
  936. }
  937. public function testDateText()
  938. {
  939. $form = $this->factory->createNamed('date', 'na&me', '2011-02-03', array(
  940. 'property_path' => 'name',
  941. 'input' => 'string',
  942. 'widget' => 'text',
  943. ));
  944. $this->assertWidgetMatchesXpath($form->createView(), array(),
  945. '/div
  946. [
  947. ./input
  948. [@id="na&me_month"]
  949. [@type="text"]
  950. [@value="2"]
  951. /following-sibling::input
  952. [@id="na&me_day"]
  953. [@type="text"]
  954. [@value="3"]
  955. /following-sibling::input
  956. [@id="na&me_year"]
  957. [@type="text"]
  958. [@value="2011"]
  959. ]
  960. [count(./input)=3]
  961. '
  962. );
  963. }
  964. public function testDateSingleText()
  965. {
  966. $form = $this->factory->createNamed('date', 'na&me', '2011-02-03', array(
  967. 'property_path' => 'name',
  968. 'input' => 'string',
  969. 'widget' => 'single_text',
  970. ));
  971. $this->assertWidgetMatchesXpath($form->createView(), array(),
  972. '/input
  973. [@type="text"]
  974. [@name="na&me"]
  975. [@value="Feb 3, 2011"]
  976. '
  977. );
  978. }
  979. public function testBirthDay()
  980. {
  981. $form = $this->factory->createNamed('birthday', 'na&me', '2000-02-03', array(
  982. 'property_path' => 'name',
  983. 'input' => 'string',
  984. ));
  985. $this->assertWidgetMatchesXpath($form->createView(), array(),
  986. '/div
  987. [
  988. ./select
  989. [@id="na&me_month"]
  990. [./option[@value="2"][@selected="selected"]]
  991. /following-sibling::select
  992. [@id="na&me_day"]
  993. [./option[@value="3"][@selected="selected"]]
  994. /following-sibling::select
  995. [@id="na&me_year"]
  996. [./option[@value="2000"][@selected="selected"]]
  997. ]
  998. [count(./select)=3]
  999. '
  1000. );
  1001. }
  1002. public function testBirthDayWithEmptyValue()
  1003. {
  1004. $form = $this->factory->createNamed('birthday', 'na&me', '1950-01-01', array(
  1005. 'property_path' => 'name',
  1006. 'input' => 'string',
  1007. 'empty_value' => '',
  1008. 'required' => false,
  1009. ));
  1010. $this->assertWidgetMatchesXpath($form->createView(), array(),
  1011. '/div
  1012. [
  1013. ./select
  1014. [@id="na&me_month"]
  1015. [./option[@value=""][.="[trans][/trans]"]]
  1016. [./option[@value="1"][@selected="selected"]]
  1017. /following-sibling::select
  1018. [@id="na&me_day"]
  1019. [./option[@value=""][.="[trans][/trans]"]]
  1020. [./option[@value="1"][@selected="selected"]]
  1021. /following-sibling::select
  1022. [@id="na&me_year"]
  1023. [./option[@value=""][.="[trans][/trans]"]]
  1024. [./option[@value="1950"][@selected="selected"]]
  1025. ]
  1026. [count(./select)=3]
  1027. '
  1028. );
  1029. }
  1030. public function testEmail()
  1031. {
  1032. $form = $this->factory->createNamed('email', 'na&me', 'foo&bar', array(
  1033. 'property_path' => 'name',
  1034. ));
  1035. $this->assertWidgetMatchesXpath($form->createView(), array(),
  1036. '/input
  1037. [@type="email"]
  1038. [@name="na&me"]
  1039. [@value="foo&bar"]
  1040. [not(@maxlength)]
  1041. '
  1042. );
  1043. }
  1044. public function testEmailWithMaxLength()
  1045. {
  1046. $form = $this->factory->createNamed('email', 'na&me', 'foo&bar', array(
  1047. 'property_path' => 'name',
  1048. 'max_length' => 123,
  1049. ));
  1050. $this->assertWidgetMatchesXpath($form->createView(), array(),
  1051. '/input
  1052. [@type="email"]
  1053. [@name="na&me"]
  1054. [@value="foo&bar"]
  1055. [@maxlength="123"]
  1056. '
  1057. );
  1058. }
  1059. public function testFile()
  1060. {
  1061. $form = $this->factory->createNamed('file', 'na&me', null, array(
  1062. 'property_path' => 'name',
  1063. ));
  1064. $this->assertWidgetMatchesXpath($form->createView(), array(),
  1065. '/input
  1066. [@type="file"]
  1067. '
  1068. );
  1069. }
  1070. public function testHidden()
  1071. {
  1072. $form = $this->factory->createNamed('hidden', 'na&me', 'foo&bar', array(
  1073. 'property_path' => 'name',
  1074. ));
  1075. $this->assertWidgetMatchesXpath($form->createView(), array(),
  1076. '/input
  1077. [@type="hidden"]
  1078. [@name="na&me"]
  1079. [@value="foo&bar"]
  1080. '
  1081. );
  1082. }
  1083. public function testInteger()
  1084. {
  1085. $form = $this->factory->createNamed('integer', 'na&me', 123, array(
  1086. 'property_path' => 'name',
  1087. ));
  1088. $this->assertWidgetMatchesXpath($form->createView(), array(),
  1089. '/input
  1090. [@type="number"]
  1091. [@name="na&me"]
  1092. [@value="123"]
  1093. '
  1094. );
  1095. }
  1096. public function testLanguage()
  1097. {
  1098. $form = $this->factory->createNamed('language', 'na&me', 'de', array(
  1099. 'property_path' => 'name',
  1100. ));
  1101. $this->assertWidgetMatchesXpath($form->createView(), array(),
  1102. '/select
  1103. [@name="na&me"]
  1104. [./option[@value="de"][@selected="selected"][.="[trans]German[/trans]"]]
  1105. [count(./option)>200]
  1106. '
  1107. );
  1108. }
  1109. public function testLocale()
  1110. {
  1111. $form = $this->factory->createNamed('locale', 'na&me', 'de_AT', array(
  1112. 'property_path' => 'name',
  1113. ));
  1114. $this->assertWidgetMatchesXpath($form->createView(), array(),
  1115. '/select
  1116. [@name="na&me"]
  1117. [./option[@value="de_AT"][@selected="selected"][.="[trans]German (Austria)[/trans]"]]
  1118. [count(./option)>200]
  1119. '
  1120. );
  1121. }
  1122. public function testMoney()
  1123. {
  1124. $form = $this->factory->createNamed('money', 'na&me', 1234.56, array(
  1125. 'property_path' => 'name',
  1126. 'currency' => 'EUR',
  1127. ));
  1128. $this->assertWidgetMatchesXpath($form->createView(), array(),
  1129. '/input
  1130. [@type="text"]
  1131. [@name="na&me"]
  1132. [@value="1234.56"]
  1133. [contains(.., "€")]
  1134. '
  1135. );
  1136. }
  1137. public function testNumber()
  1138. {
  1139. $form = $this->factory->createNamed('number', 'na&me', 1234.56, array(
  1140. 'property_path' => 'name',
  1141. ));
  1142. $this->assertWidgetMatchesXpath($form->createView(), array(),
  1143. '/input
  1144. [@type="text"]
  1145. [@name="na&me"]
  1146. [@value="1234.56"]
  1147. '
  1148. );
  1149. }
  1150. public function testPassword()
  1151. {
  1152. $form = $this->factory->createNamed('password', 'na&me', 'foo&bar', array(
  1153. 'property_path' => 'name',
  1154. ));
  1155. $this->assertWidgetMatchesXpath($form->createView(), array(),
  1156. '/input
  1157. [@type="password"]
  1158. [@name="na&me"]
  1159. [@value=""]
  1160. '
  1161. );
  1162. }
  1163. public function testPasswordBoundNotAlwaysEmpty()
  1164. {
  1165. $form = $this->factory->createNamed('password', 'na&me', null, array(
  1166. 'property_path' => 'name',
  1167. 'always_empty' => false,
  1168. ));
  1169. $form->bind('foo&bar');
  1170. $this->assertWidgetMatchesXpath($form->createView(), array(),
  1171. '/input
  1172. [@type="password"]
  1173. [@name="na&me"]
  1174. [@value="foo&bar"]
  1175. '
  1176. );
  1177. }
  1178. public function testPasswordWithMaxLength()
  1179. {
  1180. $form = $this->factory->createNamed('password', 'na&me', 'foo&bar', array(
  1181. 'property_path' => 'name',
  1182. 'max_length' => 123,
  1183. ));
  1184. $this->assertWidgetMatchesXpath($form->createView(), array(),
  1185. '/input
  1186. [@type="password"]
  1187. [@name="na&me"]
  1188. [@value=""]
  1189. [@maxlength="123"]
  1190. '
  1191. );
  1192. }
  1193. public function testPercent()
  1194. {
  1195. $form = $this->factory->createNamed('percent', 'na&me', 0.1, array(
  1196. 'property_path' => 'name',
  1197. ));
  1198. $this->assertWidgetMatchesXpath($form->createView(), array(),
  1199. '/input
  1200. [@type="text"]
  1201. [@name="na&me"]
  1202. [@value="10"]
  1203. [contains(.., "%")]
  1204. '
  1205. );
  1206. }
  1207. public function testCheckedRadio()
  1208. {
  1209. $form = $this->factory->createNamed('radio', 'na&me', true, array(
  1210. 'property_path' => 'name',
  1211. ));
  1212. $this->assertWidgetMatchesXpath($form->createView(), array(),
  1213. '/input
  1214. [@type="radio"]
  1215. [@name="na&me"]
  1216. [@checked="checked"]
  1217. [@value=""]
  1218. '
  1219. );
  1220. }
  1221. public function testCheckedRadioWithValue()
  1222. {
  1223. $form = $this->factory->createNamed('radio', 'na&me', true, array(
  1224. 'property_path' => 'name',
  1225. 'value' => 'foo&bar',
  1226. ));
  1227. $this->assertWidgetMatchesXpath($form->createView(), array(),
  1228. '/input
  1229. [@type="radio"]
  1230. [@name="na&me"]
  1231. [@checked="checked"]
  1232. [@value="foo&bar"]
  1233. '
  1234. );
  1235. }
  1236. public function testUncheckedRadio()
  1237. {
  1238. $form = $this->factory->createNamed('radio', 'na&me', false, array(
  1239. 'property_path' => 'name',
  1240. ));
  1241. $this->assertWidgetMatchesXpath($form->createView(), array(),
  1242. '/input
  1243. [@type="radio"]
  1244. [@name="na&me"]
  1245. [not(@checked)]
  1246. '
  1247. );
  1248. }
  1249. public function testTextarea()
  1250. {
  1251. $form = $this->factory->createNamed('textarea', 'na&me', 'foo&bar', array(
  1252. 'property_path' => 'name',
  1253. ));
  1254. $this->assertWidgetMatchesXpath($form->createView(), array(),
  1255. '/textarea
  1256. [@name="na&me"]
  1257. [.="foo&bar"]
  1258. '
  1259. );
  1260. }
  1261. public function testText()
  1262. {
  1263. $form = $this->factory->createNamed('text', 'na&me', 'foo&bar', array(
  1264. 'property_path' => 'name',
  1265. ));
  1266. $this->assertWidgetMatchesXpath($form->createView(), array(),
  1267. '/input
  1268. [@type="text"]
  1269. [@name="na&me"]
  1270. [@value="foo&bar"]
  1271. [not(@maxlength)]
  1272. '
  1273. );
  1274. }
  1275. public function testTextWithMaxLength()
  1276. {
  1277. $form = $this->factory->createNamed('text', 'na&me', 'foo&bar', array(
  1278. 'property_path' => 'name',
  1279. 'max_length' => 123,
  1280. ));
  1281. $this->assertWidgetMatchesXpath($form->createView(), array(),
  1282. '/input
  1283. [@type="text"]
  1284. [@name="na&me"]
  1285. [@value="foo&bar"]
  1286. [@maxlength="123"]
  1287. '
  1288. );
  1289. }
  1290. public function testSearch()
  1291. {
  1292. $form = $this->factory->createNamed('search', 'na&me', 'foo&bar', array(
  1293. 'property_path' => 'name',
  1294. ));
  1295. $this->assertWidgetMatchesXpath($form->createView(), array(),
  1296. '/input
  1297. [@type="search"]
  1298. [@name="na&me"]
  1299. [@value="foo&bar"]
  1300. [not(@maxlength)]
  1301. '
  1302. );
  1303. }
  1304. public function testTime()
  1305. {
  1306. $form = $this->factory->createNamed('time', 'na&me', '04:05:06', array(
  1307. 'property_path' => 'name',
  1308. 'input' => 'string',
  1309. 'with_seconds' => false,
  1310. ));
  1311. $this->assertWidgetMatchesXpath($form->createView(), array(),
  1312. '/div
  1313. [
  1314. ./select
  1315. [@id="na&me_hour"]
  1316. [@size="1"]
  1317. [./option[@value="4"][@selected="selected"]]
  1318. /following-sibling::select
  1319. [@id="na&me_minute"]
  1320. [@size="1"]
  1321. [./option[@value="5"][@selected="selected"]]
  1322. ]
  1323. [count(./select)=2]
  1324. '
  1325. );
  1326. }
  1327. public function testTimeWithSeconds()
  1328. {
  1329. $form = $this->factory->createNamed('time', 'na&me', '04:05:06', array(
  1330. 'property_path' => 'name',
  1331. 'input' => 'string',
  1332. 'with_seconds' => true,
  1333. ));
  1334. $this->assertWidgetMatchesXpath($form->createView(), array(),
  1335. '/div
  1336. [
  1337. ./select
  1338. [@id="na&me_hour"]
  1339. [@size="1"]
  1340. [./option[@value="4"][@selected="selected"]]
  1341. [count(./option)>23]
  1342. /following-sibling::select
  1343. [@id="na&me_minute"]
  1344. [@size="1"]
  1345. [./option[@value="5"][@selected="selected"]]
  1346. [count(./option)>59]
  1347. /following-sibling::select
  1348. [@id="na&me_second"]
  1349. [@size="1"]
  1350. [./option[@value="6"][@selected="selected"]]
  1351. [count(./option)>59]
  1352. ]
  1353. [count(./select)=3]
  1354. '
  1355. );
  1356. }
  1357. public function testTimeText()
  1358. {
  1359. $form = $this->factory->createNamed('time', 'na&me', '04:05:06', array(
  1360. 'property_path' => 'name',
  1361. 'input' => 'string',
  1362. 'widget' => 'text',
  1363. ));
  1364. $this->assertWidgetMatchesXpath($form->createView(), array(),
  1365. '/div
  1366. [
  1367. ./input
  1368. [@type="text"]
  1369. [@id="na&me_hour"]
  1370. [@name="na&me[hour]"]
  1371. [@value="04"]
  1372. [@size="1"]
  1373. [@required="required"]
  1374. /following-sibling::input
  1375. [@type="text"]
  1376. [@id="na&me_minute"]
  1377. [@name="na&me[minute]"]
  1378. [@value="05"]
  1379. [@size="1"]
  1380. [@required="required"]
  1381. ]
  1382. [count(./input)=2]
  1383. '
  1384. );
  1385. }
  1386. public function testTimeSingleText()
  1387. {
  1388. $form = $this->factory->createNamed('time', 'na&me', '04:05:06', array(
  1389. 'property_path' => 'name',
  1390. 'input' => 'string',
  1391. 'widget' => 'single_text',
  1392. ));
  1393. $this->assertWidgetMatchesXpath($form->createView(), array(),
  1394. '/input
  1395. [@type="text"]
  1396. [@name="na&me"]
  1397. [@value="04:05:00"]
  1398. '
  1399. );
  1400. }
  1401. public function testTimeWithEmptyValueGlobal()
  1402. {
  1403. $form = $this->factory->createNamed('time', 'na&me', null, array(
  1404. 'property_path' => 'name',
  1405. 'input' => 'string',
  1406. 'empty_value' => 'Change&Me',
  1407. 'required' => false,
  1408. ));
  1409. $this->assertWidgetMatchesXpath($form->createView(), array(),
  1410. '/div
  1411. [
  1412. ./select
  1413. [@id="na&me_hour"]
  1414. [./option[@value=""][.="[trans]Change&Me[/trans]"]]
  1415. [count(./option)>24]
  1416. /following-sibling::select
  1417. [@id="na&me_minute"]
  1418. [./option[@value=""][.="[trans]Change&Me[/trans]"]]
  1419. [count(./option)>60]
  1420. ]
  1421. [count(./select)=2]
  1422. '
  1423. );
  1424. }
  1425. public function testTimeWithEmptyValueOnYear()
  1426. {
  1427. $form = $this->factory->createNamed('time', 'na&me', null, array(
  1428. 'property_path' => 'name',
  1429. 'input' => 'string',
  1430. 'required' => false,
  1431. 'empty_value' => array('hour' => 'Change&Me'),
  1432. ));
  1433. $this->assertWidgetMatchesXpath($form->createView(), array(),
  1434. '/div
  1435. [
  1436. ./select
  1437. [@id="na&me_hour"]
  1438. [./option[@value=""][.="[trans]Change&Me[/trans]"]]
  1439. [count(./option)>24]
  1440. /following-sibling::select
  1441. [@id="na&me_minute"]
  1442. [./option[@value="1"]]
  1443. [count(./option)>59]
  1444. ]
  1445. [count(./select)=2]
  1446. '
  1447. );
  1448. }
  1449. public function testTimezone()
  1450. {
  1451. $form = $this->factory->createNamed('timezone', 'na&me', 'Europe/Vienna', array(
  1452. 'property_path' => 'name',
  1453. ));
  1454. $this->assertWidgetMatchesXpath($form->createView(), array(),
  1455. '/select
  1456. [@name="na&me"]
  1457. [@required="required"]
  1458. [./optgroup
  1459. [@label="[trans]Europe[/trans]"]
  1460. [./option[@value="Europe/Vienna"][@selected="selected"][.="[trans]Vienna[/trans]"]]
  1461. ]
  1462. [count(./optgroup)>10]
  1463. [count(.//option)>200]
  1464. '
  1465. );
  1466. }
  1467. public function testTimezoneWithEmptyValue()
  1468. {
  1469. $form = $this->factory->createNamed('timezone', 'na&me', null, array(
  1470. 'property_path' => 'name',
  1471. 'empty_value' => 'Select&Timezone',
  1472. 'required' => false,
  1473. ));
  1474. $this->assertWidgetMatchesXpath($form->createView(), array(),
  1475. '/select
  1476. [./option[@value=""][.="[trans]Select&Timezone[/trans]"]]
  1477. [count(./optgroup)>10]
  1478. [count(.//option)>201]
  1479. '
  1480. );
  1481. }
  1482. public function testUrl()
  1483. {
  1484. $url = 'http://www.google.com?foo1=bar1&foo2=bar2';
  1485. $form = $this->factory->createNamed('url', 'na&me', $url, array(
  1486. 'property_path' => 'name',
  1487. ));
  1488. $this->assertWidgetMatchesXpath($form->createView(), array(),
  1489. '/input
  1490. [@type="url"]
  1491. [@name="na&me"]
  1492. [@value="http://www.google.com?foo1=bar1&foo2=bar2"]
  1493. '
  1494. );
  1495. }
  1496. public function testCollectionPrototype()
  1497. {
  1498. $form = $this->factory->createNamedBuilder('form', 'na&me', array('items' => array('one', 'two', 'three')))
  1499. ->add('items', 'collection', array('allow_add' => true))
  1500. ->getForm()
  1501. ->createView();
  1502. $html = $this->renderWidget($form);
  1503. $this->assertMatchesXpath($html,
  1504. '//script
  1505. [@id="na&me_items_prototype"]
  1506. [@type="text/html"]
  1507. '
  1508. );
  1509. }
  1510. }