AbstractLayoutTest.php 28 KB

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