AbstractLayoutTest.php 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001
  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. '/input
  533. [@type="text"]
  534. [@name="na&me"]
  535. [@value="Feb 3, 2011"]
  536. '
  537. );
  538. }
  539. public function testEmail()
  540. {
  541. $form = $this->factory->createNamed('email', 'na&me', 'foo&bar', array(
  542. 'property_path' => 'name',
  543. ));
  544. $this->assertWidgetMatchesXpath($form->createView(), array(),
  545. '/input
  546. [@type="email"]
  547. [@name="na&me"]
  548. [@value="foo&bar"]
  549. [not(@maxlength)]
  550. '
  551. );
  552. }
  553. public function testEmailWithMaxLength()
  554. {
  555. $form = $this->factory->createNamed('email', 'na&me', 'foo&bar', array(
  556. 'property_path' => 'name',
  557. 'max_length' => 123,
  558. ));
  559. $this->assertWidgetMatchesXpath($form->createView(), array(),
  560. '/input
  561. [@type="email"]
  562. [@name="na&me"]
  563. [@value="foo&bar"]
  564. [@maxlength="123"]
  565. '
  566. );
  567. }
  568. public function testFile()
  569. {
  570. $form = $this->factory->createNamed('file', 'na&me', null, array(
  571. 'property_path' => 'name',
  572. ));
  573. $this->assertWidgetMatchesXpath($form->createView(), array(),
  574. '/div
  575. [
  576. ./input[@type="file"][@id="na&me_file"]
  577. /following-sibling::input[@type="hidden"][@id="na&me_token"]
  578. /following-sibling::input[@type="hidden"][@id="na&me_name"]
  579. /following-sibling::input[@type="hidden"][@id="na&me_originalName"]
  580. ]
  581. [count(./input)=4]
  582. '
  583. );
  584. }
  585. public function testHidden()
  586. {
  587. $form = $this->factory->createNamed('hidden', 'na&me', 'foo&bar', array(
  588. 'property_path' => 'name',
  589. ));
  590. $this->assertWidgetMatchesXpath($form->createView(), array(),
  591. '/input
  592. [@type="hidden"]
  593. [@name="na&me"]
  594. [@value="foo&bar"]
  595. '
  596. );
  597. }
  598. public function testInteger()
  599. {
  600. $form = $this->factory->createNamed('integer', 'na&me', 123, array(
  601. 'property_path' => 'name',
  602. ));
  603. $this->assertWidgetMatchesXpath($form->createView(), array(),
  604. '/input
  605. [@type="number"]
  606. [@name="na&me"]
  607. [@value="123"]
  608. '
  609. );
  610. }
  611. public function testLanguage()
  612. {
  613. $form = $this->factory->createNamed('language', 'na&me', 'de', array(
  614. 'property_path' => 'name',
  615. ));
  616. $this->assertWidgetMatchesXpath($form->createView(), array(),
  617. '/select
  618. [@name="na&me"]
  619. [./option[@value="de"][@selected="selected"][.="German"]]
  620. [count(./option)>200]
  621. '
  622. );
  623. }
  624. public function testLocale()
  625. {
  626. $form = $this->factory->createNamed('locale', 'na&me', 'de_AT', array(
  627. 'property_path' => 'name',
  628. ));
  629. $this->assertWidgetMatchesXpath($form->createView(), array(),
  630. '/select
  631. [@name="na&me"]
  632. [./option[@value="de_AT"][@selected="selected"][.="German (Austria)"]]
  633. [count(./option)>200]
  634. '
  635. );
  636. }
  637. public function testMoney()
  638. {
  639. $form = $this->factory->createNamed('money', 'na&me', 1234.56, array(
  640. 'property_path' => 'name',
  641. 'currency' => 'EUR',
  642. ));
  643. $this->assertWidgetMatchesXpath($form->createView(), array(),
  644. '/input
  645. [@type="text"]
  646. [@name="na&me"]
  647. [@value="1234.56"]
  648. [contains(.., "€")]
  649. '
  650. );
  651. }
  652. public function testNumber()
  653. {
  654. $form = $this->factory->createNamed('number', 'na&me', 1234.56, array(
  655. 'property_path' => 'name',
  656. ));
  657. $this->assertWidgetMatchesXpath($form->createView(), array(),
  658. '/input
  659. [@type="text"]
  660. [@name="na&me"]
  661. [@value="1234.56"]
  662. '
  663. );
  664. }
  665. public function testPassword()
  666. {
  667. $form = $this->factory->createNamed('password', 'na&me', 'foo&bar', array(
  668. 'property_path' => 'name',
  669. ));
  670. $this->assertWidgetMatchesXpath($form->createView(), array(),
  671. '/input
  672. [@type="password"]
  673. [@name="na&me"]
  674. [@value=""]
  675. '
  676. );
  677. }
  678. public function testPasswordBoundNotAlwaysEmpty()
  679. {
  680. $form = $this->factory->createNamed('password', 'na&me', null, array(
  681. 'property_path' => 'name',
  682. 'always_empty' => false,
  683. ));
  684. $form->bind('foo&bar');
  685. $this->assertWidgetMatchesXpath($form->createView(), array(),
  686. '/input
  687. [@type="password"]
  688. [@name="na&me"]
  689. [@value="foo&bar"]
  690. '
  691. );
  692. }
  693. public function testPasswordWithMaxLength()
  694. {
  695. $form = $this->factory->createNamed('password', 'na&me', 'foo&bar', array(
  696. 'property_path' => 'name',
  697. 'max_length' => 123,
  698. ));
  699. $this->assertWidgetMatchesXpath($form->createView(), array(),
  700. '/input
  701. [@type="password"]
  702. [@name="na&me"]
  703. [@value=""]
  704. [@maxlength="123"]
  705. '
  706. );
  707. }
  708. public function testPercent()
  709. {
  710. $form = $this->factory->createNamed('percent', 'na&me', 0.1, array(
  711. 'property_path' => 'name',
  712. ));
  713. $this->assertWidgetMatchesXpath($form->createView(), array(),
  714. '/input
  715. [@type="text"]
  716. [@name="na&me"]
  717. [@value="10"]
  718. [contains(.., "%")]
  719. '
  720. );
  721. }
  722. public function testCheckedRadio()
  723. {
  724. $form = $this->factory->createNamed('radio', 'na&me', true, array(
  725. 'property_path' => 'name',
  726. ));
  727. $this->assertWidgetMatchesXpath($form->createView(), array(),
  728. '/input
  729. [@type="radio"]
  730. [@name="na&me"]
  731. [@checked="checked"]
  732. [@value=""]
  733. '
  734. );
  735. }
  736. public function testCheckedRadioWithValue()
  737. {
  738. $form = $this->factory->createNamed('radio', 'na&me', true, array(
  739. 'property_path' => 'name',
  740. 'value' => 'foo&bar',
  741. ));
  742. $this->assertWidgetMatchesXpath($form->createView(), array(),
  743. '/input
  744. [@type="radio"]
  745. [@name="na&me"]
  746. [@checked="checked"]
  747. [@value="foo&bar"]
  748. '
  749. );
  750. }
  751. public function testUncheckedRadio()
  752. {
  753. $form = $this->factory->createNamed('radio', 'na&me', false, array(
  754. 'property_path' => 'name',
  755. ));
  756. $this->assertWidgetMatchesXpath($form->createView(), array(),
  757. '/input
  758. [@type="radio"]
  759. [@name="na&me"]
  760. [not(@checked)]
  761. '
  762. );
  763. }
  764. public function testTextarea()
  765. {
  766. $form = $this->factory->createNamed('textarea', 'na&me', 'foo&bar', array(
  767. 'property_path' => 'name',
  768. ));
  769. $this->assertWidgetMatchesXpath($form->createView(), array(),
  770. '/textarea
  771. [@name="na&me"]
  772. [.="foo&bar"]
  773. '
  774. );
  775. }
  776. public function testText()
  777. {
  778. $form = $this->factory->createNamed('text', 'na&me', 'foo&bar', array(
  779. 'property_path' => 'name',
  780. ));
  781. $this->assertWidgetMatchesXpath($form->createView(), array(),
  782. '/input
  783. [@type="text"]
  784. [@name="na&me"]
  785. [@value="foo&bar"]
  786. [not(@maxlength)]
  787. '
  788. );
  789. }
  790. public function testTextWithMaxLength()
  791. {
  792. $form = $this->factory->createNamed('text', 'na&me', 'foo&bar', array(
  793. 'property_path' => 'name',
  794. 'max_length' => 123,
  795. ));
  796. $this->assertWidgetMatchesXpath($form->createView(), array(),
  797. '/input
  798. [@type="text"]
  799. [@name="na&me"]
  800. [@value="foo&bar"]
  801. [@maxlength="123"]
  802. '
  803. );
  804. }
  805. public function testTime()
  806. {
  807. $form = $this->factory->createNamed('time', 'na&me', '04:05:06', array(
  808. 'property_path' => 'name',
  809. 'input' => 'string',
  810. 'with_seconds' => false,
  811. ));
  812. $this->assertWidgetMatchesXpath($form->createView(), array(),
  813. '/div
  814. [
  815. ./select
  816. [@id="na&me_hour"]
  817. [@size="1"]
  818. [./option[@value="4"][@selected="selected"]]
  819. /following-sibling::select
  820. [@id="na&me_minute"]
  821. [@size="1"]
  822. [./option[@value="5"][@selected="selected"]]
  823. ]
  824. [count(./select)=2]
  825. '
  826. );
  827. }
  828. public function testTimeWithSeconds()
  829. {
  830. $form = $this->factory->createNamed('time', 'na&me', '04:05:06', array(
  831. 'property_path' => 'name',
  832. 'input' => 'string',
  833. 'with_seconds' => true,
  834. ));
  835. $this->assertWidgetMatchesXpath($form->createView(), array(),
  836. '/div
  837. [
  838. ./select
  839. [@id="na&me_hour"]
  840. [@size="1"]
  841. [./option[@value="4"][@selected="selected"]]
  842. /following-sibling::select
  843. [@id="na&me_minute"]
  844. [@size="1"]
  845. [./option[@value="5"][@selected="selected"]]
  846. /following-sibling::select
  847. [@id="na&me_second"]
  848. [@size="1"]
  849. [./option[@value="6"][@selected="selected"]]
  850. ]
  851. [count(./select)=3]
  852. '
  853. );
  854. }
  855. public function testTimezone()
  856. {
  857. $form = $this->factory->createNamed('timezone', 'na&me', 'Europe/Vienna', array(
  858. 'property_path' => 'name',
  859. ));
  860. $this->assertWidgetMatchesXpath($form->createView(), array(),
  861. '/select
  862. [@name="na&me"]
  863. [./optgroup
  864. [@label="Europe"]
  865. [./option[@value="Europe/Vienna"][@selected="selected"][.="Vienna"]]
  866. ]
  867. [count(./optgroup)>10]
  868. [count(.//option)>200]
  869. '
  870. );
  871. }
  872. public function testUrl()
  873. {
  874. $url = 'http://www.google.com?foo1=bar1&foo2=bar2';
  875. $form = $this->factory->createNamed('url', 'na&me', $url, array(
  876. 'property_path' => 'name',
  877. ));
  878. $this->assertWidgetMatchesXpath($form->createView(), array(),
  879. '/input
  880. [@type="url"]
  881. [@name="na&me"]
  882. [@value="http://www.google.com?foo1=bar1&foo2=bar2"]
  883. '
  884. );
  885. }
  886. }