AbstractLayoutTest.php 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046
  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 testSearch()
  833. {
  834. $form = $this->factory->createNamed('search', 'na&me', 'foo&bar', array(
  835. 'property_path' => 'name',
  836. ));
  837. $this->assertWidgetMatchesXpath($form->createView(), array(),
  838. '/input
  839. [@type="search"]
  840. [@name="na&me"]
  841. [@value="foo&bar"]
  842. [not(@maxlength)]
  843. '
  844. );
  845. }
  846. public function testTime()
  847. {
  848. $form = $this->factory->createNamed('time', 'na&me', '04:05:06', array(
  849. 'property_path' => 'name',
  850. 'input' => 'string',
  851. 'with_seconds' => false,
  852. ));
  853. $this->assertWidgetMatchesXpath($form->createView(), array(),
  854. '/div
  855. [
  856. ./select
  857. [@id="na&me_hour"]
  858. [@size="1"]
  859. [./option[@value="4"][@selected="selected"]]
  860. /following-sibling::select
  861. [@id="na&me_minute"]
  862. [@size="1"]
  863. [./option[@value="5"][@selected="selected"]]
  864. ]
  865. [count(./select)=2]
  866. '
  867. );
  868. }
  869. public function testTimeWithSeconds()
  870. {
  871. $form = $this->factory->createNamed('time', 'na&me', '04:05:06', array(
  872. 'property_path' => 'name',
  873. 'input' => 'string',
  874. 'with_seconds' => true,
  875. ));
  876. $this->assertWidgetMatchesXpath($form->createView(), array(),
  877. '/div
  878. [
  879. ./select
  880. [@id="na&me_hour"]
  881. [@size="1"]
  882. [./option[@value="4"][@selected="selected"]]
  883. /following-sibling::select
  884. [@id="na&me_minute"]
  885. [@size="1"]
  886. [./option[@value="5"][@selected="selected"]]
  887. /following-sibling::select
  888. [@id="na&me_second"]
  889. [@size="1"]
  890. [./option[@value="6"][@selected="selected"]]
  891. ]
  892. [count(./select)=3]
  893. '
  894. );
  895. }
  896. public function testTimezone()
  897. {
  898. $form = $this->factory->createNamed('timezone', 'na&me', 'Europe/Vienna', array(
  899. 'property_path' => 'name',
  900. ));
  901. $this->assertWidgetMatchesXpath($form->createView(), array(),
  902. '/select
  903. [@name="na&me"]
  904. [./optgroup
  905. [@label="Europe"]
  906. [./option[@value="Europe/Vienna"][@selected="selected"][.="Vienna"]]
  907. ]
  908. [count(./optgroup)>10]
  909. [count(.//option)>200]
  910. '
  911. );
  912. }
  913. public function testUrl()
  914. {
  915. $url = 'http://www.google.com?foo1=bar1&foo2=bar2';
  916. $form = $this->factory->createNamed('url', 'na&me', $url, array(
  917. 'property_path' => 'name',
  918. ));
  919. $this->assertWidgetMatchesXpath($form->createView(), array(),
  920. '/input
  921. [@type="url"]
  922. [@name="na&me"]
  923. [@value="http://www.google.com?foo1=bar1&foo2=bar2"]
  924. '
  925. );
  926. }
  927. }