AbstractLayoutTest.php 49 KB

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