AbstractLayoutTest.php 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013
  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 testCsrfWithNonRootParent()
  422. {
  423. $form = $this->factory->createNamed('csrf', 'na&me', null, array(
  424. 'property_path' => 'name',
  425. ));
  426. $form->setParent($this->factory->create('form'));
  427. $form->getParent()->setParent($this->factory->create('form'));
  428. $html = $this->renderWidget($form->createView());
  429. $this->assertEquals('', trim($html));
  430. }
  431. public function testDateTime()
  432. {
  433. $form = $this->factory->createNamed('datetime', 'na&me', '2011-02-03 04:05:06', array(
  434. 'property_path' => 'name',
  435. 'input' => 'string',
  436. 'with_seconds' => false,
  437. ));
  438. $this->assertWidgetMatchesXpath($form->createView(), array(),
  439. '/div
  440. [
  441. ./div
  442. [@id="na&me_date"]
  443. [
  444. ./select
  445. [@id="na&me_date_month"]
  446. [./option[@value="2"][@selected="selected"]]
  447. /following-sibling::select
  448. [@id="na&me_date_day"]
  449. [./option[@value="3"][@selected="selected"]]
  450. /following-sibling::select
  451. [@id="na&me_date_year"]
  452. [./option[@value="2011"][@selected="selected"]]
  453. ]
  454. /following-sibling::div
  455. [@id="na&me_time"]
  456. [
  457. ./select
  458. [@id="na&me_time_hour"]
  459. [./option[@value="4"][@selected="selected"]]
  460. /following-sibling::select
  461. [@id="na&me_time_minute"]
  462. [./option[@value="5"][@selected="selected"]]
  463. ]
  464. ]
  465. [count(.//select)=5]
  466. '
  467. );
  468. }
  469. public function testDateTimeWithSeconds()
  470. {
  471. $form = $this->factory->createNamed('datetime', 'na&me', '2011-02-03 04:05:06', array(
  472. 'property_path' => 'name',
  473. 'input' => 'string',
  474. 'with_seconds' => true,
  475. ));
  476. $this->assertWidgetMatchesXpath($form->createView(), array(),
  477. '/div
  478. [
  479. ./div
  480. [@id="na&me_date"]
  481. [
  482. ./select
  483. [@id="na&me_date_month"]
  484. [./option[@value="2"][@selected="selected"]]
  485. /following-sibling::select
  486. [@id="na&me_date_day"]
  487. [./option[@value="3"][@selected="selected"]]
  488. /following-sibling::select
  489. [@id="na&me_date_year"]
  490. [./option[@value="2011"][@selected="selected"]]
  491. ]
  492. /following-sibling::div
  493. [@id="na&me_time"]
  494. [
  495. ./select
  496. [@id="na&me_time_hour"]
  497. [./option[@value="4"][@selected="selected"]]
  498. /following-sibling::select
  499. [@id="na&me_time_minute"]
  500. [./option[@value="5"][@selected="selected"]]
  501. /following-sibling::select
  502. [@id="na&me_time_second"]
  503. [./option[@value="6"][@selected="selected"]]
  504. ]
  505. ]
  506. [count(.//select)=6]
  507. '
  508. );
  509. }
  510. public function testDateChoice()
  511. {
  512. $form = $this->factory->createNamed('date', 'na&me', '2011-02-03', array(
  513. 'property_path' => 'name',
  514. 'input' => 'string',
  515. 'widget' => 'choice',
  516. ));
  517. $this->assertWidgetMatchesXpath($form->createView(), array(),
  518. '/div
  519. [
  520. ./select
  521. [@id="na&me_month"]
  522. [./option[@value="2"][@selected="selected"]]
  523. /following-sibling::select
  524. [@id="na&me_day"]
  525. [./option[@value="3"][@selected="selected"]]
  526. /following-sibling::select
  527. [@id="na&me_year"]
  528. [./option[@value="2011"][@selected="selected"]]
  529. ]
  530. [count(./select)=3]
  531. '
  532. );
  533. }
  534. public function testDateText()
  535. {
  536. $form = $this->factory->createNamed('date', 'na&me', '2011-02-03', array(
  537. 'property_path' => 'name',
  538. 'input' => 'string',
  539. 'widget' => 'text',
  540. ));
  541. $this->assertWidgetMatchesXpath($form->createView(), array(),
  542. '/input
  543. [@type="text"]
  544. [@name="na&me"]
  545. [@value="Feb 3, 2011"]
  546. '
  547. );
  548. }
  549. public function testEmail()
  550. {
  551. $form = $this->factory->createNamed('email', 'na&me', 'foo&bar', array(
  552. 'property_path' => 'name',
  553. ));
  554. $this->assertWidgetMatchesXpath($form->createView(), array(),
  555. '/input
  556. [@type="email"]
  557. [@name="na&me"]
  558. [@value="foo&bar"]
  559. [not(@maxlength)]
  560. '
  561. );
  562. }
  563. public function testEmailWithMaxLength()
  564. {
  565. $form = $this->factory->createNamed('email', 'na&me', 'foo&bar', array(
  566. 'property_path' => 'name',
  567. 'max_length' => 123,
  568. ));
  569. $this->assertWidgetMatchesXpath($form->createView(), array(),
  570. '/input
  571. [@type="email"]
  572. [@name="na&me"]
  573. [@value="foo&bar"]
  574. [@maxlength="123"]
  575. '
  576. );
  577. }
  578. public function testFile()
  579. {
  580. $form = $this->factory->createNamed('file', 'na&me', null, array(
  581. 'property_path' => 'name',
  582. ));
  583. $this->assertWidgetMatchesXpath($form->createView(), array(),
  584. '/div
  585. [
  586. ./input[@type="file"][@id="na&me_file"]
  587. /following-sibling::input[@type="hidden"][@id="na&me_token"]
  588. /following-sibling::input[@type="hidden"][@id="na&me_name"]
  589. ]
  590. [count(./input)=3]
  591. '
  592. );
  593. }
  594. public function testHidden()
  595. {
  596. $form = $this->factory->createNamed('hidden', 'na&me', 'foo&bar', array(
  597. 'property_path' => 'name',
  598. ));
  599. $this->assertWidgetMatchesXpath($form->createView(), array(),
  600. '/input
  601. [@type="hidden"]
  602. [@name="na&me"]
  603. [@value="foo&bar"]
  604. '
  605. );
  606. }
  607. public function testInteger()
  608. {
  609. $form = $this->factory->createNamed('integer', 'na&me', 123, array(
  610. 'property_path' => 'name',
  611. ));
  612. $this->assertWidgetMatchesXpath($form->createView(), array(),
  613. '/input
  614. [@type="number"]
  615. [@name="na&me"]
  616. [@value="123"]
  617. '
  618. );
  619. }
  620. public function testLanguage()
  621. {
  622. $form = $this->factory->createNamed('language', 'na&me', 'de', array(
  623. 'property_path' => 'name',
  624. ));
  625. $this->assertWidgetMatchesXpath($form->createView(), array(),
  626. '/select
  627. [@name="na&me"]
  628. [./option[@value="de"][@selected="selected"][.="German"]]
  629. [count(./option)>200]
  630. '
  631. );
  632. }
  633. public function testLocale()
  634. {
  635. $form = $this->factory->createNamed('locale', 'na&me', 'de_AT', array(
  636. 'property_path' => 'name',
  637. ));
  638. $this->assertWidgetMatchesXpath($form->createView(), array(),
  639. '/select
  640. [@name="na&me"]
  641. [./option[@value="de_AT"][@selected="selected"][.="German (Austria)"]]
  642. [count(./option)>200]
  643. '
  644. );
  645. }
  646. public function testMoney()
  647. {
  648. $form = $this->factory->createNamed('money', 'na&me', 1234.56, array(
  649. 'property_path' => 'name',
  650. 'currency' => 'EUR',
  651. ));
  652. $this->assertWidgetMatchesXpath($form->createView(), array(),
  653. '/input
  654. [@type="text"]
  655. [@name="na&me"]
  656. [@value="1234.56"]
  657. [contains(.., "€")]
  658. '
  659. );
  660. }
  661. public function testNumber()
  662. {
  663. $form = $this->factory->createNamed('number', 'na&me', 1234.56, array(
  664. 'property_path' => 'name',
  665. ));
  666. $this->assertWidgetMatchesXpath($form->createView(), array(),
  667. '/input
  668. [@type="text"]
  669. [@name="na&me"]
  670. [@value="1234.56"]
  671. '
  672. );
  673. }
  674. public function testPassword()
  675. {
  676. $form = $this->factory->createNamed('password', 'na&me', 'foo&bar', array(
  677. 'property_path' => 'name',
  678. ));
  679. $this->assertWidgetMatchesXpath($form->createView(), array(),
  680. '/input
  681. [@type="password"]
  682. [@name="na&me"]
  683. [@value=""]
  684. '
  685. );
  686. }
  687. public function testPasswordBoundNotAlwaysEmpty()
  688. {
  689. $form = $this->factory->createNamed('password', 'na&me', null, array(
  690. 'property_path' => 'name',
  691. 'always_empty' => false,
  692. ));
  693. $form->bind('foo&bar');
  694. $this->assertWidgetMatchesXpath($form->createView(), array(),
  695. '/input
  696. [@type="password"]
  697. [@name="na&me"]
  698. [@value="foo&bar"]
  699. '
  700. );
  701. }
  702. public function testPasswordWithMaxLength()
  703. {
  704. $form = $this->factory->createNamed('password', 'na&me', 'foo&bar', array(
  705. 'property_path' => 'name',
  706. 'max_length' => 123,
  707. ));
  708. $this->assertWidgetMatchesXpath($form->createView(), array(),
  709. '/input
  710. [@type="password"]
  711. [@name="na&me"]
  712. [@value=""]
  713. [@maxlength="123"]
  714. '
  715. );
  716. }
  717. public function testPercent()
  718. {
  719. $form = $this->factory->createNamed('percent', 'na&me', 0.1, array(
  720. 'property_path' => 'name',
  721. ));
  722. $this->assertWidgetMatchesXpath($form->createView(), array(),
  723. '/input
  724. [@type="text"]
  725. [@name="na&me"]
  726. [@value="10"]
  727. [contains(.., "%")]
  728. '
  729. );
  730. }
  731. public function testCheckedRadio()
  732. {
  733. $form = $this->factory->createNamed('radio', 'na&me', true, array(
  734. 'property_path' => 'name',
  735. ));
  736. $this->assertWidgetMatchesXpath($form->createView(), array(),
  737. '/input
  738. [@type="radio"]
  739. [@name="na&me"]
  740. [@checked="checked"]
  741. [@value=""]
  742. '
  743. );
  744. }
  745. public function testCheckedRadioWithValue()
  746. {
  747. $form = $this->factory->createNamed('radio', 'na&me', true, array(
  748. 'property_path' => 'name',
  749. 'value' => 'foo&bar',
  750. ));
  751. $this->assertWidgetMatchesXpath($form->createView(), array(),
  752. '/input
  753. [@type="radio"]
  754. [@name="na&me"]
  755. [@checked="checked"]
  756. [@value="foo&bar"]
  757. '
  758. );
  759. }
  760. public function testUncheckedRadio()
  761. {
  762. $form = $this->factory->createNamed('radio', 'na&me', false, array(
  763. 'property_path' => 'name',
  764. ));
  765. $this->assertWidgetMatchesXpath($form->createView(), array(),
  766. '/input
  767. [@type="radio"]
  768. [@name="na&me"]
  769. [not(@checked)]
  770. '
  771. );
  772. }
  773. public function testTextarea()
  774. {
  775. $form = $this->factory->createNamed('textarea', 'na&me', 'foo&bar', array(
  776. 'property_path' => 'name',
  777. ));
  778. $this->assertWidgetMatchesXpath($form->createView(), array(),
  779. '/textarea
  780. [@name="na&me"]
  781. [.="foo&bar"]
  782. '
  783. );
  784. }
  785. public function testText()
  786. {
  787. $form = $this->factory->createNamed('text', 'na&me', 'foo&bar', array(
  788. 'property_path' => 'name',
  789. ));
  790. $this->assertWidgetMatchesXpath($form->createView(), array(),
  791. '/input
  792. [@type="text"]
  793. [@name="na&me"]
  794. [@value="foo&bar"]
  795. [not(@maxlength)]
  796. '
  797. );
  798. }
  799. public function testTextWithMaxLength()
  800. {
  801. $form = $this->factory->createNamed('text', 'na&me', 'foo&bar', array(
  802. 'property_path' => 'name',
  803. 'max_length' => 123,
  804. ));
  805. $this->assertWidgetMatchesXpath($form->createView(), array(),
  806. '/input
  807. [@type="text"]
  808. [@name="na&me"]
  809. [@value="foo&bar"]
  810. [@maxlength="123"]
  811. '
  812. );
  813. }
  814. public function testTime()
  815. {
  816. $form = $this->factory->createNamed('time', 'na&me', '04:05:06', array(
  817. 'property_path' => 'name',
  818. 'input' => 'string',
  819. 'with_seconds' => false,
  820. ));
  821. $this->assertWidgetMatchesXpath($form->createView(), array(),
  822. '/div
  823. [
  824. ./select
  825. [@id="na&me_hour"]
  826. [@size="1"]
  827. [./option[@value="4"][@selected="selected"]]
  828. /following-sibling::select
  829. [@id="na&me_minute"]
  830. [@size="1"]
  831. [./option[@value="5"][@selected="selected"]]
  832. ]
  833. [count(./select)=2]
  834. '
  835. );
  836. }
  837. public function testTimeWithSeconds()
  838. {
  839. $form = $this->factory->createNamed('time', 'na&me', '04:05:06', array(
  840. 'property_path' => 'name',
  841. 'input' => 'string',
  842. 'with_seconds' => true,
  843. ));
  844. $this->assertWidgetMatchesXpath($form->createView(), array(),
  845. '/div
  846. [
  847. ./select
  848. [@id="na&me_hour"]
  849. [@size="1"]
  850. [./option[@value="4"][@selected="selected"]]
  851. /following-sibling::select
  852. [@id="na&me_minute"]
  853. [@size="1"]
  854. [./option[@value="5"][@selected="selected"]]
  855. /following-sibling::select
  856. [@id="na&me_second"]
  857. [@size="1"]
  858. [./option[@value="6"][@selected="selected"]]
  859. ]
  860. [count(./select)=3]
  861. '
  862. );
  863. }
  864. public function testTimezone()
  865. {
  866. $form = $this->factory->createNamed('timezone', 'na&me', 'Europe/Vienna', array(
  867. 'property_path' => 'name',
  868. ));
  869. $this->assertWidgetMatchesXpath($form->createView(), array(),
  870. '/select
  871. [@name="na&me"]
  872. [./optgroup
  873. [@label="Europe"]
  874. [./option[@value="Europe/Vienna"][@selected="selected"][.="Vienna"]]
  875. ]
  876. [count(./optgroup)>10]
  877. [count(.//option)>200]
  878. '
  879. );
  880. }
  881. public function testUrl()
  882. {
  883. $url = 'http://www.google.com?foo1=bar1&foo2=bar2';
  884. $form = $this->factory->createNamed('url', 'na&me', $url, array(
  885. 'property_path' => 'name',
  886. ));
  887. $this->assertWidgetMatchesXpath($form->createView(), array(),
  888. '/input
  889. [@type="url"]
  890. [@name="na&me"]
  891. [@value="http://www.google.com?foo1=bar1&foo2=bar2"]
  892. '
  893. );
  894. }
  895. }