AbstractLayoutTest.php 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030
  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. $dispatcher = new EventDispatcher();
  25. $this->csrfProvider = $this->getMock('Symfony\Component\Form\Extension\Csrf\CsrfProvider\CsrfProviderInterface');
  26. $storage = new \Symfony\Component\HttpFoundation\File\TemporaryStorage('foo', \sys_get_temp_dir());
  27. $this->factory = new FormFactory(array(
  28. new CoreExtension($storage),
  29. new CsrfExtension($this->csrfProvider),
  30. ));
  31. }
  32. protected function assertXpathNodeValue(\DomElement $element, $expression, $nodeValue)
  33. {
  34. $xpath = new \DOMXPath($element->ownerDocument);
  35. $nodeList = $xpath->evaluate($expression);
  36. $this->assertEquals(1, $nodeList->length);
  37. $this->assertEquals($nodeValue, $nodeList->item(0)->nodeValue);
  38. }
  39. protected function assertMatchesXpath($html, $expression, $count = 1)
  40. {
  41. $dom = new \DomDocument('UTF-8');
  42. try {
  43. // Wrap in <root> node so we can load HTML with multiple tags at
  44. // the top level
  45. $dom->loadXml('<root>'.$html.'</root>');
  46. } catch (\Exception $e) {
  47. return $this->fail(sprintf(
  48. "Failed loading HTML:\n\n%s\n\nError: %s",
  49. $html,
  50. $e->getMessage()
  51. ));
  52. }
  53. $xpath = new \DOMXPath($dom);
  54. $nodeList = $xpath->evaluate('/root'.$expression);
  55. if ($nodeList->length != $count) {
  56. $dom->formatOutput = true;
  57. $this->fail(sprintf(
  58. "Failed asserting that \n\n%s\n\nmatches exactly %s. Matches %s in \n\n%s",
  59. $expression,
  60. $count == 1 ? 'once' : $count . ' times',
  61. $nodeList->length == 1 ? 'once' : $nodeList->length . ' times',
  62. // strip away <root> and </root>
  63. substr($dom->saveHTML(), 6, -8)
  64. ));
  65. }
  66. }
  67. protected function assertWidgetMatchesXpath(FormView $view, array $vars, $xpath)
  68. {
  69. // include ampersands everywhere to validate escaping
  70. $html = $this->renderWidget($view, array_merge(array(
  71. 'id' => 'my&id',
  72. 'attr' => array('class' => 'my&class'),
  73. ), $vars));
  74. $xpath = trim($xpath).'
  75. [@id="my&id"]
  76. [@class="my&class"]';
  77. $this->assertMatchesXpath($html, $xpath);
  78. }
  79. abstract protected function renderEnctype(FormView $view);
  80. abstract protected function renderLabel(FormView $view, $label = null);
  81. abstract protected function renderErrors(FormView $view);
  82. abstract protected function renderWidget(FormView $view, array $vars = array());
  83. abstract protected function renderRow(FormView $view, array $vars = array());
  84. abstract protected function renderRest(FormView $view, array $vars = array());
  85. public function testEnctype()
  86. {
  87. $form = $this->factory->createNamedBuilder('form', 'na&me', null, array(
  88. 'property_path' => 'name',
  89. ))
  90. ->add('file', 'file')
  91. ->getForm();
  92. $this->assertEquals('enctype="multipart/form-data"', $this->renderEnctype($form->createView()));
  93. }
  94. public function testNoEnctype()
  95. {
  96. $form = $this->factory->createNamedBuilder('form', 'na&me', null, array(
  97. 'property_path' => 'name',
  98. ))
  99. ->add('text', 'text')
  100. ->getForm();
  101. $this->assertEquals('', $this->renderEnctype($form->createView()));
  102. }
  103. public function testLabel()
  104. {
  105. $form = $this->factory->createNamed('text', 'na&me', null, array(
  106. 'property_path' => 'name',
  107. ));
  108. $html = $this->renderLabel($form->createView());
  109. $this->assertMatchesXpath($html,
  110. '/label
  111. [@for="na&me"]
  112. [.="[trans]Na&me[/trans]"]
  113. '
  114. );
  115. }
  116. public function testLabelWithCustomTextPassedAsOption()
  117. {
  118. $form = $this->factory->createNamed('text', 'na&me', null, array(
  119. 'property_path' => 'name',
  120. 'label' => 'Custom label',
  121. ));
  122. $html = $this->renderLabel($form->createView());
  123. $this->assertMatchesXpath($html,
  124. '/label
  125. [@for="na&me"]
  126. [.="[trans]Custom label[/trans]"]
  127. '
  128. );
  129. }
  130. public function testLabelWithCustomTextPassedDirectly()
  131. {
  132. $form = $this->factory->createNamed('text', 'na&me', null, array(
  133. 'property_path' => 'name',
  134. ));
  135. $html = $this->renderLabel($form->createView(), 'Custom label');
  136. $this->assertMatchesXpath($html,
  137. '/label
  138. [@for="na&me"]
  139. [.="[trans]Custom label[/trans]"]
  140. '
  141. );
  142. }
  143. public function testErrors()
  144. {
  145. $form = $this->factory->createNamed('text', 'na&me', null, array(
  146. 'property_path' => 'name',
  147. ));
  148. $form->addError(new FormError('Error 1'));
  149. $form->addError(new FormError('Error 2'));
  150. $view = $form->createView();
  151. $html = $this->renderErrors($view);
  152. $this->assertMatchesXpath($html,
  153. '/ul
  154. [
  155. ./li[.="[trans]Error 1[/trans]"]
  156. /following-sibling::li[.="[trans]Error 2[/trans]"]
  157. ]
  158. [count(./li)=2]
  159. '
  160. );
  161. }
  162. public function testWidgetById()
  163. {
  164. $form = $this->factory->createNamed('text', 'text_id');
  165. $html = $this->renderWidget($form->createView());
  166. $this->assertMatchesXpath($html,
  167. '/div
  168. [
  169. ./input
  170. [@type="text"]
  171. [@id="text_id"]
  172. ]
  173. [@id="container"]
  174. '
  175. );
  176. }
  177. public function testCheckedCheckbox()
  178. {
  179. $form = $this->factory->createNamed('checkbox', 'na&me', true, array(
  180. 'property_path' => 'name',
  181. ));
  182. $this->assertWidgetMatchesXpath($form->createView(), array(),
  183. '/input
  184. [@type="checkbox"]
  185. [@name="na&me"]
  186. [@checked="checked"]
  187. [@value="1"]
  188. '
  189. );
  190. }
  191. public function testCheckedCheckboxWithValue()
  192. {
  193. $form = $this->factory->createNamed('checkbox', 'na&me', true, array(
  194. 'property_path' => 'name',
  195. 'value' => 'foo&bar',
  196. ));
  197. $this->assertWidgetMatchesXpath($form->createView(), array(),
  198. '/input
  199. [@type="checkbox"]
  200. [@name="na&me"]
  201. [@checked="checked"]
  202. [@value="foo&bar"]
  203. '
  204. );
  205. }
  206. public function testUncheckedCheckbox()
  207. {
  208. $form = $this->factory->createNamed('checkbox', 'na&me', false, array(
  209. 'property_path' => 'name',
  210. ));
  211. $this->assertWidgetMatchesXpath($form->createView(), array(),
  212. '/input
  213. [@type="checkbox"]
  214. [@name="na&me"]
  215. [not(@checked)]
  216. '
  217. );
  218. }
  219. public function testSingleChoice()
  220. {
  221. $form = $this->factory->createNamed('choice', 'na&me', '&a', array(
  222. 'property_path' => 'name',
  223. 'choices' => array('&a' => 'Choice&A', '&b' => 'Choice&B'),
  224. 'multiple' => false,
  225. 'expanded' => false,
  226. ));
  227. $this->assertWidgetMatchesXpath($form->createView(), array(),
  228. '/select
  229. [@name="na&me"]
  230. [
  231. ./option[@value="&a"][@selected="selected"][.="Choice&A"]
  232. /following-sibling::option[@value="&b"][not(@selected)][.="Choice&B"]
  233. ]
  234. [count(./option)=2]
  235. '
  236. );
  237. }
  238. public function testSingleChoiceWithPreferred()
  239. {
  240. $form = $this->factory->createNamed('choice', 'na&me', '&a', array(
  241. 'property_path' => 'name',
  242. 'choices' => array('&a' => 'Choice&A', '&b' => 'Choice&B'),
  243. 'preferred_choices' => array('&b'),
  244. 'multiple' => false,
  245. 'expanded' => false,
  246. ));
  247. $this->assertWidgetMatchesXpath($form->createView(), array('separator' => '-- sep --'),
  248. '/select
  249. [@name="na&me"]
  250. [
  251. ./option[@value="&b"][not(@selected)][.="Choice&B"]
  252. /following-sibling::option[@disabled="disabled"][not(@selected)][.="-- sep --"]
  253. /following-sibling::option[@value="&a"][@selected="selected"][.="Choice&A"]
  254. ]
  255. [count(./option)=3]
  256. '
  257. );
  258. }
  259. public function testSingleChoiceNonRequired()
  260. {
  261. $form = $this->factory->createNamed('choice', 'na&me', '&a', array(
  262. 'property_path' => 'name',
  263. 'choices' => array('&a' => 'Choice&A', '&b' => 'Choice&B'),
  264. 'required' => false,
  265. 'multiple' => false,
  266. 'expanded' => false,
  267. ));
  268. $this->assertWidgetMatchesXpath($form->createView(), array(),
  269. '/select
  270. [@name="na&me"]
  271. [
  272. ./option[@value=""][.=""]
  273. /following-sibling::option[@value="&a"][@selected="selected"][.="Choice&A"]
  274. /following-sibling::option[@value="&b"][not(@selected)][.="Choice&B"]
  275. ]
  276. [count(./option)=3]
  277. '
  278. );
  279. }
  280. public function testSingleChoiceGrouped()
  281. {
  282. $form = $this->factory->createNamed('choice', 'na&me', '&a', array(
  283. 'property_path' => 'name',
  284. 'choices' => array(
  285. 'Group&1' => array('&a' => 'Choice&A', '&b' => 'Choice&B'),
  286. 'Group&2' => array('&c' => 'Choice&C'),
  287. ),
  288. 'multiple' => false,
  289. 'expanded' => false,
  290. ));
  291. $this->assertWidgetMatchesXpath($form->createView(), array(),
  292. '/select
  293. [@name="na&me"]
  294. [./optgroup[@label="Group&1"]
  295. [
  296. ./option[@value="&a"][@selected="selected"][.="Choice&A"]
  297. /following-sibling::option[@value="&b"][not(@selected)][.="Choice&B"]
  298. ]
  299. [count(./option)=2]
  300. ]
  301. [./optgroup[@label="Group&2"]
  302. [./option[@value="&c"][not(@selected)][.="Choice&C"]]
  303. [count(./option)=1]
  304. ]
  305. [count(./optgroup)=2]
  306. '
  307. );
  308. }
  309. public function testMultipleChoice()
  310. {
  311. $form = $this->factory->createNamed('choice', 'na&me', array('&a'), array(
  312. 'property_path' => 'name',
  313. 'choices' => array('&a' => 'Choice&A', '&b' => 'Choice&B'),
  314. 'multiple' => true,
  315. 'expanded' => false,
  316. ));
  317. $this->assertWidgetMatchesXpath($form->createView(), array(),
  318. '/select
  319. [@name="na&me[]"]
  320. [@multiple="multiple"]
  321. [
  322. ./option[@value="&a"][@selected="selected"][.="Choice&A"]
  323. /following-sibling::option[@value="&b"][not(@selected)][.="Choice&B"]
  324. ]
  325. [count(./option)=2]
  326. '
  327. );
  328. }
  329. public function testMultipleChoiceNonRequired()
  330. {
  331. $form = $this->factory->createNamed('choice', 'na&me', array('&a'), array(
  332. 'property_path' => 'name',
  333. 'choices' => array('&a' => 'Choice&A', '&b' => 'Choice&B'),
  334. 'required' => false,
  335. 'multiple' => true,
  336. 'expanded' => false,
  337. ));
  338. $this->assertWidgetMatchesXpath($form->createView(), array(),
  339. '/select
  340. [@name="na&me[]"]
  341. [@multiple="multiple"]
  342. [
  343. ./option[@value="&a"][@selected="selected"][.="Choice&A"]
  344. /following-sibling::option[@value="&b"][not(@selected)][.="Choice&B"]
  345. ]
  346. [count(./option)=2]
  347. '
  348. );
  349. }
  350. public function testSingleChoiceExpanded()
  351. {
  352. $form = $this->factory->createNamed('choice', 'na&me', '&a', array(
  353. 'property_path' => 'name',
  354. 'choices' => array('&a' => 'Choice&A', '&b' => 'Choice&B'),
  355. 'multiple' => false,
  356. 'expanded' => true,
  357. ));
  358. $this->assertWidgetMatchesXpath($form->createView(), array(),
  359. '/div
  360. [
  361. ./input[@type="radio"][@name="na&me"][@id="na&me_&a"][@checked]
  362. /following-sibling::label[@for="na&me_&a"][.="[trans]Choice&A[/trans]"]
  363. /following-sibling::input[@type="radio"][@name="na&me"][@id="na&me_&b"][not(@checked)]
  364. /following-sibling::label[@for="na&me_&b"][.="[trans]Choice&B[/trans]"]
  365. ]
  366. [count(./input)=2]
  367. '
  368. );
  369. }
  370. public function testMultipleChoiceExpanded()
  371. {
  372. $form = $this->factory->createNamed('choice', 'na&me', array('&a', '&c'), array(
  373. 'property_path' => 'name',
  374. 'choices' => array('&a' => 'Choice&A', '&b' => 'Choice&B', '&c' => 'Choice&C'),
  375. 'multiple' => true,
  376. 'expanded' => true,
  377. 'required' => true,
  378. ));
  379. $this->assertWidgetMatchesXpath($form->createView(), array(),
  380. '/div
  381. [
  382. ./input[@type="checkbox"][@name="na&me[&a]"][@id="na&me_&a"][@checked][not(@required)]
  383. /following-sibling::label[@for="na&me_&a"][.="[trans]Choice&A[/trans]"]
  384. /following-sibling::input[@type="checkbox"][@name="na&me[&b]"][@id="na&me_&b"][not(@checked)][not(@required)]
  385. /following-sibling::label[@for="na&me_&b"][.="[trans]Choice&B[/trans]"]
  386. /following-sibling::input[@type="checkbox"][@name="na&me[&c]"][@id="na&me_&c"][@checked][not(@required)]
  387. /following-sibling::label[@for="na&me_&c"][.="[trans]Choice&C[/trans]"]
  388. ]
  389. [count(./input)=3]
  390. '
  391. );
  392. }
  393. public function testCountry()
  394. {
  395. $form = $this->factory->createNamed('country', 'na&me', 'AT', array(
  396. 'property_path' => 'name',
  397. ));
  398. $this->assertWidgetMatchesXpath($form->createView(), array(),
  399. '/select
  400. [@name="na&me"]
  401. [./option[@value="AT"][@selected="selected"][.="Austria"]]
  402. [count(./option)>200]
  403. '
  404. );
  405. }
  406. public function testCsrf()
  407. {
  408. $this->csrfProvider->expects($this->any())
  409. ->method('generateCsrfToken')
  410. ->will($this->returnValue('foo&bar'));
  411. $form = $this->factory->createNamed('csrf', 'na&me', null, array(
  412. 'property_path' => 'name',
  413. ));
  414. $this->assertWidgetMatchesXpath($form->createView(), array(),
  415. '/input
  416. [@type="hidden"]
  417. [@value="foo&bar"]
  418. '
  419. );
  420. }
  421. public function testDateTime()
  422. {
  423. $form = $this->factory->createNamed('datetime', 'na&me', '2011-02-03 04:05:06', array(
  424. 'property_path' => 'name',
  425. 'input' => 'string',
  426. 'with_seconds' => false,
  427. ));
  428. $this->assertWidgetMatchesXpath($form->createView(), array(),
  429. '/div
  430. [
  431. ./div
  432. [@id="na&me_date"]
  433. [
  434. ./select
  435. [@id="na&me_date_month"]
  436. [./option[@value="2"][@selected="selected"]]
  437. /following-sibling::select
  438. [@id="na&me_date_day"]
  439. [./option[@value="3"][@selected="selected"]]
  440. /following-sibling::select
  441. [@id="na&me_date_year"]
  442. [./option[@value="2011"][@selected="selected"]]
  443. ]
  444. /following-sibling::div
  445. [@id="na&me_time"]
  446. [
  447. ./select
  448. [@id="na&me_time_hour"]
  449. [./option[@value="4"][@selected="selected"]]
  450. /following-sibling::select
  451. [@id="na&me_time_minute"]
  452. [./option[@value="5"][@selected="selected"]]
  453. ]
  454. ]
  455. [count(.//select)=5]
  456. '
  457. );
  458. }
  459. public function testDateTimeWithSeconds()
  460. {
  461. $form = $this->factory->createNamed('datetime', 'na&me', '2011-02-03 04:05:06', array(
  462. 'property_path' => 'name',
  463. 'input' => 'string',
  464. 'with_seconds' => true,
  465. ));
  466. $this->assertWidgetMatchesXpath($form->createView(), array(),
  467. '/div
  468. [
  469. ./div
  470. [@id="na&me_date"]
  471. [
  472. ./select
  473. [@id="na&me_date_month"]
  474. [./option[@value="2"][@selected="selected"]]
  475. /following-sibling::select
  476. [@id="na&me_date_day"]
  477. [./option[@value="3"][@selected="selected"]]
  478. /following-sibling::select
  479. [@id="na&me_date_year"]
  480. [./option[@value="2011"][@selected="selected"]]
  481. ]
  482. /following-sibling::div
  483. [@id="na&me_time"]
  484. [
  485. ./select
  486. [@id="na&me_time_hour"]
  487. [./option[@value="4"][@selected="selected"]]
  488. /following-sibling::select
  489. [@id="na&me_time_minute"]
  490. [./option[@value="5"][@selected="selected"]]
  491. /following-sibling::select
  492. [@id="na&me_time_second"]
  493. [./option[@value="6"][@selected="selected"]]
  494. ]
  495. ]
  496. [count(.//select)=6]
  497. '
  498. );
  499. }
  500. public function testDateChoice()
  501. {
  502. $form = $this->factory->createNamed('date', 'na&me', '2011-02-03', array(
  503. 'property_path' => 'name',
  504. 'input' => 'string',
  505. 'widget' => 'choice',
  506. ));
  507. $this->assertWidgetMatchesXpath($form->createView(), array(),
  508. '/div
  509. [
  510. ./select
  511. [@id="na&me_month"]
  512. [./option[@value="2"][@selected="selected"]]
  513. /following-sibling::select
  514. [@id="na&me_day"]
  515. [./option[@value="3"][@selected="selected"]]
  516. /following-sibling::select
  517. [@id="na&me_year"]
  518. [./option[@value="2011"][@selected="selected"]]
  519. ]
  520. [count(./select)=3]
  521. '
  522. );
  523. }
  524. public function testDateText()
  525. {
  526. $form = $this->factory->createNamed('date', 'na&me', '2011-02-03', array(
  527. 'property_path' => 'name',
  528. 'input' => 'string',
  529. 'widget' => 'text',
  530. ));
  531. $this->assertWidgetMatchesXpath($form->createView(), array(),
  532. '/div
  533. [
  534. ./input
  535. [@id="na&me_month"]
  536. [@type="text"]
  537. [@value="2"]
  538. /following-sibling::input
  539. [@id="na&me_day"]
  540. [@type="text"]
  541. [@value="3"]
  542. /following-sibling::input
  543. [@id="na&me_year"]
  544. [@type="text"]
  545. [@value="2011"]
  546. ]
  547. [count(./input)=3]
  548. '
  549. );
  550. }
  551. public function testDateSingleText()
  552. {
  553. $form = $this->factory->createNamed('date', 'na&me', '2011-02-03', array(
  554. 'property_path' => 'name',
  555. 'input' => 'string',
  556. 'widget' => 'single-text',
  557. ));
  558. $this->assertWidgetMatchesXpath($form->createView(), array(),
  559. '/input
  560. [@type="text"]
  561. [@name="na&me"]
  562. [@value="Feb 3, 2011"]
  563. '
  564. );
  565. }
  566. public function testEmail()
  567. {
  568. $form = $this->factory->createNamed('email', 'na&me', 'foo&bar', array(
  569. 'property_path' => 'name',
  570. ));
  571. $this->assertWidgetMatchesXpath($form->createView(), array(),
  572. '/input
  573. [@type="email"]
  574. [@name="na&me"]
  575. [@value="foo&bar"]
  576. [not(@maxlength)]
  577. '
  578. );
  579. }
  580. public function testEmailWithMaxLength()
  581. {
  582. $form = $this->factory->createNamed('email', 'na&me', 'foo&bar', array(
  583. 'property_path' => 'name',
  584. 'max_length' => 123,
  585. ));
  586. $this->assertWidgetMatchesXpath($form->createView(), array(),
  587. '/input
  588. [@type="email"]
  589. [@name="na&me"]
  590. [@value="foo&bar"]
  591. [@maxlength="123"]
  592. '
  593. );
  594. }
  595. public function testFile()
  596. {
  597. $form = $this->factory->createNamed('file', 'na&me', null, array(
  598. 'property_path' => 'name',
  599. ));
  600. $this->assertWidgetMatchesXpath($form->createView(), array(),
  601. '/div
  602. [
  603. ./input[@type="file"][@id="na&me_file"]
  604. /following-sibling::input[@type="hidden"][@id="na&me_token"]
  605. /following-sibling::input[@type="hidden"][@id="na&me_name"]
  606. /following-sibling::input[@type="hidden"][@id="na&me_originalName"]
  607. ]
  608. [count(./input)=4]
  609. '
  610. );
  611. }
  612. public function testHidden()
  613. {
  614. $form = $this->factory->createNamed('hidden', 'na&me', 'foo&bar', array(
  615. 'property_path' => 'name',
  616. ));
  617. $this->assertWidgetMatchesXpath($form->createView(), array(),
  618. '/input
  619. [@type="hidden"]
  620. [@name="na&me"]
  621. [@value="foo&bar"]
  622. '
  623. );
  624. }
  625. public function testInteger()
  626. {
  627. $form = $this->factory->createNamed('integer', 'na&me', 123, array(
  628. 'property_path' => 'name',
  629. ));
  630. $this->assertWidgetMatchesXpath($form->createView(), array(),
  631. '/input
  632. [@type="number"]
  633. [@name="na&me"]
  634. [@value="123"]
  635. '
  636. );
  637. }
  638. public function testLanguage()
  639. {
  640. $form = $this->factory->createNamed('language', 'na&me', 'de', array(
  641. 'property_path' => 'name',
  642. ));
  643. $this->assertWidgetMatchesXpath($form->createView(), array(),
  644. '/select
  645. [@name="na&me"]
  646. [./option[@value="de"][@selected="selected"][.="German"]]
  647. [count(./option)>200]
  648. '
  649. );
  650. }
  651. public function testLocale()
  652. {
  653. $form = $this->factory->createNamed('locale', 'na&me', 'de_AT', array(
  654. 'property_path' => 'name',
  655. ));
  656. $this->assertWidgetMatchesXpath($form->createView(), array(),
  657. '/select
  658. [@name="na&me"]
  659. [./option[@value="de_AT"][@selected="selected"][.="German (Austria)"]]
  660. [count(./option)>200]
  661. '
  662. );
  663. }
  664. public function testMoney()
  665. {
  666. $form = $this->factory->createNamed('money', 'na&me', 1234.56, array(
  667. 'property_path' => 'name',
  668. 'currency' => 'EUR',
  669. ));
  670. $this->assertWidgetMatchesXpath($form->createView(), array(),
  671. '/input
  672. [@type="text"]
  673. [@name="na&me"]
  674. [@value="1234.56"]
  675. [contains(.., "€")]
  676. '
  677. );
  678. }
  679. public function testNumber()
  680. {
  681. $form = $this->factory->createNamed('number', 'na&me', 1234.56, array(
  682. 'property_path' => 'name',
  683. ));
  684. $this->assertWidgetMatchesXpath($form->createView(), array(),
  685. '/input
  686. [@type="text"]
  687. [@name="na&me"]
  688. [@value="1234.56"]
  689. '
  690. );
  691. }
  692. public function testPassword()
  693. {
  694. $form = $this->factory->createNamed('password', 'na&me', 'foo&bar', array(
  695. 'property_path' => 'name',
  696. ));
  697. $this->assertWidgetMatchesXpath($form->createView(), array(),
  698. '/input
  699. [@type="password"]
  700. [@name="na&me"]
  701. [@value=""]
  702. '
  703. );
  704. }
  705. public function testPasswordBoundNotAlwaysEmpty()
  706. {
  707. $form = $this->factory->createNamed('password', 'na&me', null, array(
  708. 'property_path' => 'name',
  709. 'always_empty' => false,
  710. ));
  711. $form->bind('foo&bar');
  712. $this->assertWidgetMatchesXpath($form->createView(), array(),
  713. '/input
  714. [@type="password"]
  715. [@name="na&me"]
  716. [@value="foo&bar"]
  717. '
  718. );
  719. }
  720. public function testPasswordWithMaxLength()
  721. {
  722. $form = $this->factory->createNamed('password', 'na&me', 'foo&bar', array(
  723. 'property_path' => 'name',
  724. 'max_length' => 123,
  725. ));
  726. $this->assertWidgetMatchesXpath($form->createView(), array(),
  727. '/input
  728. [@type="password"]
  729. [@name="na&me"]
  730. [@value=""]
  731. [@maxlength="123"]
  732. '
  733. );
  734. }
  735. public function testPercent()
  736. {
  737. $form = $this->factory->createNamed('percent', 'na&me', 0.1, array(
  738. 'property_path' => 'name',
  739. ));
  740. $this->assertWidgetMatchesXpath($form->createView(), array(),
  741. '/input
  742. [@type="text"]
  743. [@name="na&me"]
  744. [@value="10"]
  745. [contains(.., "%")]
  746. '
  747. );
  748. }
  749. public function testCheckedRadio()
  750. {
  751. $form = $this->factory->createNamed('radio', 'na&me', true, array(
  752. 'property_path' => 'name',
  753. ));
  754. $this->assertWidgetMatchesXpath($form->createView(), array(),
  755. '/input
  756. [@type="radio"]
  757. [@name="na&me"]
  758. [@checked="checked"]
  759. [@value=""]
  760. '
  761. );
  762. }
  763. public function testCheckedRadioWithValue()
  764. {
  765. $form = $this->factory->createNamed('radio', 'na&me', true, array(
  766. 'property_path' => 'name',
  767. 'value' => 'foo&bar',
  768. ));
  769. $this->assertWidgetMatchesXpath($form->createView(), array(),
  770. '/input
  771. [@type="radio"]
  772. [@name="na&me"]
  773. [@checked="checked"]
  774. [@value="foo&bar"]
  775. '
  776. );
  777. }
  778. public function testUncheckedRadio()
  779. {
  780. $form = $this->factory->createNamed('radio', 'na&me', false, array(
  781. 'property_path' => 'name',
  782. ));
  783. $this->assertWidgetMatchesXpath($form->createView(), array(),
  784. '/input
  785. [@type="radio"]
  786. [@name="na&me"]
  787. [not(@checked)]
  788. '
  789. );
  790. }
  791. public function testTextarea()
  792. {
  793. $form = $this->factory->createNamed('textarea', 'na&me', 'foo&bar', array(
  794. 'property_path' => 'name',
  795. ));
  796. $this->assertWidgetMatchesXpath($form->createView(), array(),
  797. '/textarea
  798. [@name="na&me"]
  799. [.="foo&bar"]
  800. '
  801. );
  802. }
  803. public function testText()
  804. {
  805. $form = $this->factory->createNamed('text', 'na&me', 'foo&bar', array(
  806. 'property_path' => 'name',
  807. ));
  808. $this->assertWidgetMatchesXpath($form->createView(), array(),
  809. '/input
  810. [@type="text"]
  811. [@name="na&me"]
  812. [@value="foo&bar"]
  813. [not(@maxlength)]
  814. '
  815. );
  816. }
  817. public function testTextWithMaxLength()
  818. {
  819. $form = $this->factory->createNamed('text', 'na&me', 'foo&bar', array(
  820. 'property_path' => 'name',
  821. 'max_length' => 123,
  822. ));
  823. $this->assertWidgetMatchesXpath($form->createView(), array(),
  824. '/input
  825. [@type="text"]
  826. [@name="na&me"]
  827. [@value="foo&bar"]
  828. [@maxlength="123"]
  829. '
  830. );
  831. }
  832. public function testTime()
  833. {
  834. $form = $this->factory->createNamed('time', 'na&me', '04:05:06', array(
  835. 'property_path' => 'name',
  836. 'input' => 'string',
  837. 'with_seconds' => false,
  838. ));
  839. $this->assertWidgetMatchesXpath($form->createView(), array(),
  840. '/div
  841. [
  842. ./select
  843. [@id="na&me_hour"]
  844. [@size="1"]
  845. [./option[@value="4"][@selected="selected"]]
  846. /following-sibling::select
  847. [@id="na&me_minute"]
  848. [@size="1"]
  849. [./option[@value="5"][@selected="selected"]]
  850. ]
  851. [count(./select)=2]
  852. '
  853. );
  854. }
  855. public function testTimeWithSeconds()
  856. {
  857. $form = $this->factory->createNamed('time', 'na&me', '04:05:06', array(
  858. 'property_path' => 'name',
  859. 'input' => 'string',
  860. 'with_seconds' => true,
  861. ));
  862. $this->assertWidgetMatchesXpath($form->createView(), array(),
  863. '/div
  864. [
  865. ./select
  866. [@id="na&me_hour"]
  867. [@size="1"]
  868. [./option[@value="4"][@selected="selected"]]
  869. /following-sibling::select
  870. [@id="na&me_minute"]
  871. [@size="1"]
  872. [./option[@value="5"][@selected="selected"]]
  873. /following-sibling::select
  874. [@id="na&me_second"]
  875. [@size="1"]
  876. [./option[@value="6"][@selected="selected"]]
  877. ]
  878. [count(./select)=3]
  879. '
  880. );
  881. }
  882. public function testTimezone()
  883. {
  884. $form = $this->factory->createNamed('timezone', 'na&me', 'Europe/Vienna', array(
  885. 'property_path' => 'name',
  886. ));
  887. $this->assertWidgetMatchesXpath($form->createView(), array(),
  888. '/select
  889. [@name="na&me"]
  890. [./optgroup
  891. [@label="Europe"]
  892. [./option[@value="Europe/Vienna"][@selected="selected"][.="Vienna"]]
  893. ]
  894. [count(./optgroup)>10]
  895. [count(.//option)>200]
  896. '
  897. );
  898. }
  899. public function testUrl()
  900. {
  901. $url = 'http://www.google.com?foo1=bar1&foo2=bar2';
  902. $form = $this->factory->createNamed('url', 'na&me', $url, array(
  903. 'property_path' => 'name',
  904. ));
  905. $this->assertWidgetMatchesXpath($form->createView(), array(),
  906. '/input
  907. [@type="url"]
  908. [@name="na&me"]
  909. [@value="http://www.google.com?foo1=bar1&foo2=bar2"]
  910. '
  911. );
  912. }
  913. }