AbstractLayoutTest.php 51 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793
  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. if (!extension_loaded('intl')) {
  24. $this->markTestSkipped('The "intl" extension is not available');
  25. }
  26. \Locale::setDefault('en');
  27. $this->csrfProvider = $this->getMock('Symfony\Component\Form\Extension\Csrf\CsrfProvider\CsrfProviderInterface');
  28. $this->factory = new FormFactory(array(
  29. new CoreExtension(),
  30. new CsrfExtension($this->csrfProvider),
  31. ));
  32. }
  33. protected function tearDown()
  34. {
  35. $this->csrfProvider = null;
  36. $this->factory = null;
  37. }
  38. protected function assertXpathNodeValue(\DomElement $element, $expression, $nodeValue)
  39. {
  40. $xpath = new \DOMXPath($element->ownerDocument);
  41. $nodeList = $xpath->evaluate($expression);
  42. $this->assertEquals(1, $nodeList->length);
  43. $this->assertEquals($nodeValue, $nodeList->item(0)->nodeValue);
  44. }
  45. protected function assertMatchesXpath($html, $expression, $count = 1)
  46. {
  47. $dom = new \DomDocument('UTF-8');
  48. try {
  49. // Wrap in <root> node so we can load HTML with multiple tags at
  50. // the top level
  51. $dom->loadXml('<root>'.$html.'</root>');
  52. } catch (\Exception $e) {
  53. return $this->fail(sprintf(
  54. "Failed loading HTML:\n\n%s\n\nError: %s",
  55. $html,
  56. $e->getMessage()
  57. ));
  58. }
  59. $xpath = new \DOMXPath($dom);
  60. $nodeList = $xpath->evaluate('/root'.$expression);
  61. if ($nodeList->length != $count) {
  62. $dom->formatOutput = true;
  63. $this->fail(sprintf(
  64. "Failed asserting that \n\n%s\n\nmatches exactly %s. Matches %s in \n\n%s",
  65. $expression,
  66. $count == 1 ? 'once' : $count . ' times',
  67. $nodeList->length == 1 ? 'once' : $nodeList->length . ' times',
  68. // strip away <root> and </root>
  69. substr($dom->saveHTML(), 6, -8)
  70. ));
  71. }
  72. }
  73. protected function assertWidgetMatchesXpath(FormView $view, array $vars, $xpath)
  74. {
  75. // include ampersands everywhere to validate escaping
  76. $html = $this->renderWidget($view, array_merge(array(
  77. 'id' => 'my&id',
  78. 'attr' => array('class' => 'my&class'),
  79. ), $vars));
  80. $xpath = trim($xpath).'
  81. [@id="my&id"]
  82. [@class="my&class"]';
  83. $this->assertMatchesXpath($html, $xpath);
  84. }
  85. abstract protected function renderEnctype(FormView $view);
  86. abstract protected function renderLabel(FormView $view, $label = null, array $vars = array());
  87. abstract protected function renderErrors(FormView $view);
  88. abstract protected function renderWidget(FormView $view, array $vars = array());
  89. abstract protected function renderRow(FormView $view, array $vars = array());
  90. abstract protected function renderRest(FormView $view, array $vars = array());
  91. abstract protected function setTheme(FormView $view, array $themes);
  92. public function testEnctype()
  93. {
  94. $form = $this->factory->createNamedBuilder('form', 'na&me', null, array(
  95. 'property_path' => 'name',
  96. ))
  97. ->add('file', 'file')
  98. ->getForm();
  99. $this->assertEquals('enctype="multipart/form-data"', $this->renderEnctype($form->createView()));
  100. }
  101. public function testNoEnctype()
  102. {
  103. $form = $this->factory->createNamedBuilder('form', 'na&me', null, array(
  104. 'property_path' => 'name',
  105. ))
  106. ->add('text', 'text')
  107. ->getForm();
  108. $this->assertEquals('', $this->renderEnctype($form->createView()));
  109. }
  110. public function testLabel()
  111. {
  112. $form = $this->factory->createNamed('text', 'na&me', null, array(
  113. 'property_path' => 'name',
  114. ));
  115. $view = $form->createView();
  116. $this->renderWidget($view, array('label' => 'foo'));
  117. $html = $this->renderLabel($view);
  118. $this->assertMatchesXpath($html,
  119. '/label
  120. [@for="na&me"]
  121. [.="[trans]Na&me[/trans]"]
  122. '
  123. );
  124. }
  125. public function testLabelOnForm()
  126. {
  127. $form = $this->factory->createNamed('date', 'na&me', null, array(
  128. 'property_path' => 'name',
  129. ));
  130. $view = $form->createView();
  131. $this->renderWidget($view, array('label' => 'foo'));
  132. $html = $this->renderLabel($view);
  133. $this->assertMatchesXpath($html,
  134. '/label
  135. [@class=" required"]
  136. [.="[trans]Na&me[/trans]"]
  137. '
  138. );
  139. }
  140. public function testLabelWithCustomTextPassedAsOption()
  141. {
  142. $form = $this->factory->createNamed('text', 'na&me', null, array(
  143. 'property_path' => 'name',
  144. 'label' => 'Custom label',
  145. ));
  146. $html = $this->renderLabel($form->createView());
  147. $this->assertMatchesXpath($html,
  148. '/label
  149. [@for="na&me"]
  150. [.="[trans]Custom label[/trans]"]
  151. '
  152. );
  153. }
  154. public function testLabelWithCustomTextPassedDirectly()
  155. {
  156. $form = $this->factory->createNamed('text', 'na&me', null, array(
  157. 'property_path' => 'name',
  158. ));
  159. $html = $this->renderLabel($form->createView(), 'Custom label');
  160. $this->assertMatchesXpath($html,
  161. '/label
  162. [@for="na&me"]
  163. [.="[trans]Custom label[/trans]"]
  164. '
  165. );
  166. }
  167. public function testLabelWithCustomTextPassedAsOptionAndDirectly()
  168. {
  169. $form = $this->factory->createNamed('text', 'na&me', null, array(
  170. 'property_path' => 'name',
  171. 'label' => 'Custom label',
  172. ));
  173. $html = $this->renderLabel($form->createView(), 'Overridden label');
  174. $this->assertMatchesXpath($html,
  175. '/label
  176. [@for="na&me"]
  177. [.="[trans]Overridden label[/trans]"]
  178. '
  179. );
  180. }
  181. public function testLabelWithCustomOptionsPassedDirectly()
  182. {
  183. $form = $this->factory->createNamed('text', 'na&me', null, array(
  184. 'property_path' => 'name',
  185. ));
  186. $html = $this->renderLabel($form->createView(), null, array(
  187. 'attr' => array(
  188. 'class' => 'my&class'
  189. ),
  190. ));
  191. $this->assertMatchesXpath($html,
  192. '/label
  193. [@for="na&me"]
  194. [@class="my&class required"]
  195. '
  196. );
  197. }
  198. public function testLabelWithCustomTextAndCustomOptionsPassedDirectly()
  199. {
  200. $form = $this->factory->createNamed('text', 'na&me', null, array(
  201. 'property_path' => 'name',
  202. ));
  203. $html = $this->renderLabel($form->createView(), 'Custom label', array(
  204. 'attr' => array(
  205. 'class' => 'my&class'
  206. ),
  207. ));
  208. $this->assertMatchesXpath($html,
  209. '/label
  210. [@for="na&me"]
  211. [@class="my&class required"]
  212. [.="[trans]Custom label[/trans]"]
  213. '
  214. );
  215. }
  216. public function testErrors()
  217. {
  218. $form = $this->factory->createNamed('text', 'na&me', null, array(
  219. 'property_path' => 'name',
  220. ));
  221. $form->addError(new FormError('Error 1'));
  222. $form->addError(new FormError('Error 2'));
  223. $view = $form->createView();
  224. $html = $this->renderErrors($view);
  225. $this->assertMatchesXpath($html,
  226. '/ul
  227. [
  228. ./li[.="[trans]Error 1[/trans]"]
  229. /following-sibling::li[.="[trans]Error 2[/trans]"]
  230. ]
  231. [count(./li)=2]
  232. '
  233. );
  234. }
  235. public function testWidgetById()
  236. {
  237. $form = $this->factory->createNamed('text', 'text_id');
  238. $html = $this->renderWidget($form->createView());
  239. $this->assertMatchesXpath($html,
  240. '/div
  241. [
  242. ./input
  243. [@type="text"]
  244. [@id="text_id"]
  245. ]
  246. [@id="container"]
  247. '
  248. );
  249. }
  250. public function testCheckedCheckbox()
  251. {
  252. $form = $this->factory->createNamed('checkbox', 'na&me', true, array(
  253. 'property_path' => 'name',
  254. ));
  255. $this->assertWidgetMatchesXpath($form->createView(), array(),
  256. '/input
  257. [@type="checkbox"]
  258. [@name="na&me"]
  259. [@checked="checked"]
  260. [@value="1"]
  261. '
  262. );
  263. }
  264. public function testCheckedCheckboxWithValue()
  265. {
  266. $form = $this->factory->createNamed('checkbox', 'na&me', true, array(
  267. 'property_path' => 'name',
  268. 'value' => 'foo&bar',
  269. ));
  270. $this->assertWidgetMatchesXpath($form->createView(), array(),
  271. '/input
  272. [@type="checkbox"]
  273. [@name="na&me"]
  274. [@checked="checked"]
  275. [@value="foo&bar"]
  276. '
  277. );
  278. }
  279. public function testUncheckedCheckbox()
  280. {
  281. $form = $this->factory->createNamed('checkbox', 'na&me', false, array(
  282. 'property_path' => 'name',
  283. ));
  284. $this->assertWidgetMatchesXpath($form->createView(), array(),
  285. '/input
  286. [@type="checkbox"]
  287. [@name="na&me"]
  288. [not(@checked)]
  289. '
  290. );
  291. }
  292. public function testSingleChoice()
  293. {
  294. $form = $this->factory->createNamed('choice', 'na&me', '&a', array(
  295. 'property_path' => 'name',
  296. 'choices' => array('&a' => 'Choice&A', '&b' => 'Choice&B'),
  297. 'multiple' => false,
  298. 'expanded' => false,
  299. ));
  300. $this->assertWidgetMatchesXpath($form->createView(), array(),
  301. '/select
  302. [@name="na&me"]
  303. [@required="required"]
  304. [
  305. ./option[@value="&a"][@selected="selected"][.="[trans]Choice&A[/trans]"]
  306. /following-sibling::option[@value="&b"][not(@selected)][.="[trans]Choice&B[/trans]"]
  307. ]
  308. [count(./option)=2]
  309. '
  310. );
  311. }
  312. public function testSingleChoiceWithPreferred()
  313. {
  314. $form = $this->factory->createNamed('choice', 'na&me', '&a', array(
  315. 'property_path' => 'name',
  316. 'choices' => array('&a' => 'Choice&A', '&b' => 'Choice&B'),
  317. 'preferred_choices' => array('&b'),
  318. 'multiple' => false,
  319. 'expanded' => false,
  320. ));
  321. $this->assertWidgetMatchesXpath($form->createView(), array('separator' => '-- sep --'),
  322. '/select
  323. [@name="na&me"]
  324. [@required="required"]
  325. [
  326. ./option[@value="&b"][not(@selected)][.="[trans]Choice&B[/trans]"]
  327. /following-sibling::option[@disabled="disabled"][not(@selected)][.="-- sep --"]
  328. /following-sibling::option[@value="&a"][@selected="selected"][.="[trans]Choice&A[/trans]"]
  329. ]
  330. [count(./option)=3]
  331. '
  332. );
  333. }
  334. public function testSingleChoiceWithPreferredAndNoSeparator()
  335. {
  336. $form = $this->factory->createNamed('choice', 'na&me', '&a', array(
  337. 'property_path' => 'name',
  338. 'choices' => array('&a' => 'Choice&A', '&b' => 'Choice&B'),
  339. 'preferred_choices' => array('&b'),
  340. 'multiple' => false,
  341. 'expanded' => false,
  342. ));
  343. $this->assertWidgetMatchesXpath($form->createView(), array('separator' => null),
  344. '/select
  345. [@name="na&me"]
  346. [@required="required"]
  347. [
  348. ./option[@value="&b"][not(@selected)][.="[trans]Choice&B[/trans]"]
  349. /following-sibling::option[@value="&a"][@selected="selected"][.="[trans]Choice&A[/trans]"]
  350. ]
  351. [count(./option)=2]
  352. '
  353. );
  354. }
  355. public function testSingleChoiceWithPreferredAndBlankSeparator()
  356. {
  357. $form = $this->factory->createNamed('choice', 'na&me', '&a', array(
  358. 'property_path' => 'name',
  359. 'choices' => array('&a' => 'Choice&A', '&b' => 'Choice&B'),
  360. 'preferred_choices' => array('&b'),
  361. 'multiple' => false,
  362. 'expanded' => false,
  363. ));
  364. $this->assertWidgetMatchesXpath($form->createView(), array('separator' => ''),
  365. '/select
  366. [@name="na&me"]
  367. [@required="required"]
  368. [
  369. ./option[@value="&b"][not(@selected)][.="[trans]Choice&B[/trans]"]
  370. /following-sibling::option[@disabled="disabled"][not(@selected)][.=""]
  371. /following-sibling::option[@value="&a"][@selected="selected"][.="[trans]Choice&A[/trans]"]
  372. ]
  373. [count(./option)=3]
  374. '
  375. );
  376. }
  377. public function testChoiceWithOnlyPreferred()
  378. {
  379. $form = $this->factory->createNamed('choice', 'na&me', '&a', array(
  380. 'property_path' => 'name',
  381. 'choices' => array('&a' => 'Choice&A', '&b' => 'Choice&B'),
  382. 'preferred_choices' => array('&a', '&b'),
  383. 'multiple' => false,
  384. 'expanded' => false,
  385. ));
  386. $this->assertWidgetMatchesXpath($form->createView(), array(),
  387. '/select
  388. [count(./option)=2]
  389. '
  390. );
  391. }
  392. public function testSingleChoiceNonRequired()
  393. {
  394. $form = $this->factory->createNamed('choice', 'na&me', '&a', array(
  395. 'property_path' => 'name',
  396. 'choices' => array('&a' => 'Choice&A', '&b' => 'Choice&B'),
  397. 'required' => false,
  398. 'multiple' => false,
  399. 'expanded' => false,
  400. ));
  401. $this->assertWidgetMatchesXpath($form->createView(), array(),
  402. '/select
  403. [@name="na&me"]
  404. [not(@required)]
  405. [
  406. ./option[@value=""][.="[trans][/trans]"]
  407. /following-sibling::option[@value="&a"][@selected="selected"][.="[trans]Choice&A[/trans]"]
  408. /following-sibling::option[@value="&b"][not(@selected)][.="[trans]Choice&B[/trans]"]
  409. ]
  410. [count(./option)=3]
  411. '
  412. );
  413. }
  414. public function testSingleChoiceNonRequiredNoneSelected()
  415. {
  416. $form = $this->factory->createNamed('choice', 'na&me', null, array(
  417. 'property_path' => 'name',
  418. 'choices' => array('&a' => 'Choice&A', '&b' => 'Choice&B'),
  419. 'required' => false,
  420. 'multiple' => false,
  421. 'expanded' => false,
  422. ));
  423. $this->assertWidgetMatchesXpath($form->createView(), array(),
  424. '/select
  425. [@name="na&me"]
  426. [not(@required)]
  427. [
  428. ./option[@value=""][.="[trans][/trans]"]
  429. /following-sibling::option[@value="&a"][not(@selected)][.="[trans]Choice&A[/trans]"]
  430. /following-sibling::option[@value="&b"][not(@selected)][.="[trans]Choice&B[/trans]"]
  431. ]
  432. [count(./option)=3]
  433. '
  434. );
  435. }
  436. public function testSingleChoiceWithNonRequiredEmptyValue()
  437. {
  438. $form = $this->factory->createNamed('choice', 'na&me', '&a', array(
  439. 'property_path' => 'name',
  440. 'choices' => array('&a' => 'Choice&A', '&b' => 'Choice&B'),
  441. 'multiple' => false,
  442. 'expanded' => false,
  443. 'required' => false,
  444. 'empty_value' => 'Select&Anything&Not&Me',
  445. ));
  446. $this->assertWidgetMatchesXpath($form->createView(), array(),
  447. '/select
  448. [@name="na&me"]
  449. [not(@required)]
  450. [
  451. ./option[@value=""][not(@selected)][.="[trans]Select&Anything&Not&Me[/trans]"]
  452. /following-sibling::option[@value="&a"][@selected="selected"][.="[trans]Choice&A[/trans]"]
  453. /following-sibling::option[@value="&b"][not(@selected)][.="[trans]Choice&B[/trans]"]
  454. ]
  455. [count(./option)=3]
  456. '
  457. );
  458. }
  459. public function testSingleChoiceRequiredWithEmptyValue()
  460. {
  461. $form = $this->factory->createNamed('choice', 'na&me', '&a', array(
  462. 'property_path' => 'name',
  463. 'choices' => array('&a' => 'Choice&A', '&b' => 'Choice&B'),
  464. 'required' => true,
  465. 'multiple' => false,
  466. 'expanded' => false,
  467. 'empty_value' => 'Test&Me'
  468. ));
  469. $this->assertWidgetMatchesXpath($form->createView(), array(),
  470. '/select
  471. [@name="na&me"]
  472. [@required="required"]
  473. [
  474. ./option[@value=""][.="[trans]Test&Me[/trans]"]
  475. /following-sibling::option[@value="&a"][@selected="selected"][.="[trans]Choice&A[/trans]"]
  476. /following-sibling::option[@value="&b"][not(@selected)][.="[trans]Choice&B[/trans]"]
  477. ]
  478. [count(./option)=3]
  479. '
  480. );
  481. }
  482. public function testSingleChoiceRequiredWithEmptyValueViaView()
  483. {
  484. $form = $this->factory->createNamed('choice', 'na&me', '&a', array(
  485. 'property_path' => 'name',
  486. 'choices' => array('&a' => 'Choice&A', '&b' => 'Choice&B'),
  487. 'required' => true,
  488. 'multiple' => false,
  489. 'expanded' => false,
  490. ));
  491. $this->assertWidgetMatchesXpath($form->createView(), array('empty_value' => ''),
  492. '/select
  493. [@name="na&me"]
  494. [@required="required"]
  495. [
  496. ./option[@value=""][.="[trans][/trans]"]
  497. /following-sibling::option[@value="&a"][@selected="selected"][.="[trans]Choice&A[/trans]"]
  498. /following-sibling::option[@value="&b"][not(@selected)][.="[trans]Choice&B[/trans]"]
  499. ]
  500. [count(./option)=3]
  501. '
  502. );
  503. }
  504. public function testSingleChoiceGrouped()
  505. {
  506. $form = $this->factory->createNamed('choice', 'na&me', '&a', array(
  507. 'property_path' => 'name',
  508. 'choices' => array(
  509. 'Group&1' => array('&a' => 'Choice&A', '&b' => 'Choice&B'),
  510. 'Group&2' => array('&c' => 'Choice&C'),
  511. ),
  512. 'multiple' => false,
  513. 'expanded' => false,
  514. ));
  515. $this->assertWidgetMatchesXpath($form->createView(), array(),
  516. '/select
  517. [@name="na&me"]
  518. [./optgroup[@label="[trans]Group&1[/trans]"]
  519. [
  520. ./option[@value="&a"][@selected="selected"][.="[trans]Choice&A[/trans]"]
  521. /following-sibling::option[@value="&b"][not(@selected)][.="[trans]Choice&B[/trans]"]
  522. ]
  523. [count(./option)=2]
  524. ]
  525. [./optgroup[@label="[trans]Group&2[/trans]"]
  526. [./option[@value="&c"][not(@selected)][.="[trans]Choice&C[/trans]"]]
  527. [count(./option)=1]
  528. ]
  529. [count(./optgroup)=2]
  530. '
  531. );
  532. }
  533. public function testMultipleChoice()
  534. {
  535. $form = $this->factory->createNamed('choice', 'na&me', array('&a'), array(
  536. 'property_path' => 'name',
  537. 'choices' => array('&a' => 'Choice&A', '&b' => 'Choice&B'),
  538. 'multiple' => true,
  539. 'expanded' => false,
  540. ));
  541. $this->assertWidgetMatchesXpath($form->createView(), array(),
  542. '/select
  543. [@name="na&me[]"]
  544. [@multiple="multiple"]
  545. [
  546. ./option[@value="&a"][@selected="selected"][.="[trans]Choice&A[/trans]"]
  547. /following-sibling::option[@value="&b"][not(@selected)][.="[trans]Choice&B[/trans]"]
  548. ]
  549. [count(./option)=2]
  550. '
  551. );
  552. }
  553. public function testMultipleChoiceSkipEmptyValue()
  554. {
  555. $form = $this->factory->createNamed('choice', 'na&me', array('&a'), array(
  556. 'property_path' => 'name',
  557. 'choices' => array('&a' => 'Choice&A', '&b' => 'Choice&B'),
  558. 'multiple' => true,
  559. 'expanded' => false,
  560. 'empty_value' => 'Test&Me'
  561. ));
  562. $this->assertWidgetMatchesXpath($form->createView(), array(),
  563. '/select
  564. [@name="na&me[]"]
  565. [@multiple="multiple"]
  566. [
  567. ./option[@value="&a"][@selected="selected"][.="[trans]Choice&A[/trans]"]
  568. /following-sibling::option[@value="&b"][not(@selected)][.="[trans]Choice&B[/trans]"]
  569. ]
  570. [count(./option)=2]
  571. '
  572. );
  573. }
  574. public function testMultipleChoiceNonRequired()
  575. {
  576. $form = $this->factory->createNamed('choice', 'na&me', array('&a'), array(
  577. 'property_path' => 'name',
  578. 'choices' => array('&a' => 'Choice&A', '&b' => 'Choice&B'),
  579. 'required' => false,
  580. 'multiple' => true,
  581. 'expanded' => false,
  582. ));
  583. $this->assertWidgetMatchesXpath($form->createView(), array(),
  584. '/select
  585. [@name="na&me[]"]
  586. [@multiple="multiple"]
  587. [
  588. ./option[@value="&a"][@selected="selected"][.="[trans]Choice&A[/trans]"]
  589. /following-sibling::option[@value="&b"][not(@selected)][.="[trans]Choice&B[/trans]"]
  590. ]
  591. [count(./option)=2]
  592. '
  593. );
  594. }
  595. public function testSingleChoiceExpanded()
  596. {
  597. $form = $this->factory->createNamed('choice', 'na&me', '&a', array(
  598. 'property_path' => 'name',
  599. 'choices' => array('&a' => 'Choice&A', '&b' => 'Choice&B'),
  600. 'multiple' => false,
  601. 'expanded' => true,
  602. ));
  603. $this->assertWidgetMatchesXpath($form->createView(), array(),
  604. '/div
  605. [
  606. ./input[@type="radio"][@name="na&me"][@id="na&me_&a"][@checked]
  607. /following-sibling::label[@for="na&me_&a"][.="[trans]Choice&A[/trans]"]
  608. /following-sibling::input[@type="radio"][@name="na&me"][@id="na&me_&b"][not(@checked)]
  609. /following-sibling::label[@for="na&me_&b"][.="[trans]Choice&B[/trans]"]
  610. ]
  611. [count(./input)=2]
  612. '
  613. );
  614. }
  615. public function testSingleChoiceExpandedSkipEmptyValue()
  616. {
  617. $form = $this->factory->createNamed('choice', 'na&me', '&a', array(
  618. 'property_path' => 'name',
  619. 'choices' => array('&a' => 'Choice&A', '&b' => 'Choice&B'),
  620. 'multiple' => false,
  621. 'expanded' => true,
  622. 'empty_value' => 'Test&Me'
  623. ));
  624. $this->assertWidgetMatchesXpath($form->createView(), array(),
  625. '/div
  626. [
  627. ./input[@type="radio"][@name="na&me"][@id="na&me_&a"][@checked]
  628. /following-sibling::label[@for="na&me_&a"][.="[trans]Choice&A[/trans]"]
  629. /following-sibling::input[@type="radio"][@name="na&me"][@id="na&me_&b"][not(@checked)]
  630. /following-sibling::label[@for="na&me_&b"][.="[trans]Choice&B[/trans]"]
  631. ]
  632. [count(./input)=2]
  633. '
  634. );
  635. }
  636. public function testSingleChoiceExpandedWithBooleanValue()
  637. {
  638. $form = $this->factory->createNamed('choice', 'na&me', true, array(
  639. 'property_path' => 'name',
  640. 'choices' => array('1' => 'Choice&A', '0' => 'Choice&B'),
  641. 'multiple' => false,
  642. 'expanded' => true,
  643. ));
  644. $this->assertWidgetMatchesXpath($form->createView(), array(),
  645. '/div
  646. [
  647. ./input[@type="radio"][@name="na&me"][@id="na&me_1"][@checked]
  648. /following-sibling::label[@for="na&me_1"][.="[trans]Choice&A[/trans]"]
  649. /following-sibling::input[@type="radio"][@name="na&me"][@id="na&me_0"][not(@checked)]
  650. /following-sibling::label[@for="na&me_0"][.="[trans]Choice&B[/trans]"]
  651. ]
  652. [count(./input)=2]
  653. '
  654. );
  655. }
  656. public function testMultipleChoiceExpanded()
  657. {
  658. $form = $this->factory->createNamed('choice', 'na&me', array('&a', '&c'), array(
  659. 'property_path' => 'name',
  660. 'choices' => array('&a' => 'Choice&A', '&b' => 'Choice&B', '&c' => 'Choice&C'),
  661. 'multiple' => true,
  662. 'expanded' => true,
  663. 'required' => true,
  664. ));
  665. $this->assertWidgetMatchesXpath($form->createView(), array(),
  666. '/div
  667. [
  668. ./input[@type="checkbox"][@name="na&me[&a]"][@id="na&me_&a"][@checked][not(@required)]
  669. /following-sibling::label[@for="na&me_&a"][.="[trans]Choice&A[/trans]"]
  670. /following-sibling::input[@type="checkbox"][@name="na&me[&b]"][@id="na&me_&b"][not(@checked)][not(@required)]
  671. /following-sibling::label[@for="na&me_&b"][.="[trans]Choice&B[/trans]"]
  672. /following-sibling::input[@type="checkbox"][@name="na&me[&c]"][@id="na&me_&c"][@checked][not(@required)]
  673. /following-sibling::label[@for="na&me_&c"][.="[trans]Choice&C[/trans]"]
  674. ]
  675. [count(./input)=3]
  676. '
  677. );
  678. }
  679. public function testCountry()
  680. {
  681. $form = $this->factory->createNamed('country', 'na&me', 'AT', array(
  682. 'property_path' => 'name',
  683. ));
  684. $this->assertWidgetMatchesXpath($form->createView(), array(),
  685. '/select
  686. [@name="na&me"]
  687. [./option[@value="AT"][@selected="selected"][.="[trans]Austria[/trans]"]]
  688. [count(./option)>200]
  689. '
  690. );
  691. }
  692. public function testCountryWithEmptyValue()
  693. {
  694. $form = $this->factory->createNamed('country', 'na&me', 'AT', array(
  695. 'property_path' => 'name',
  696. 'empty_value' => 'Select&Country',
  697. 'required' => false,
  698. ));
  699. $this->assertWidgetMatchesXpath($form->createView(), array(),
  700. '/select
  701. [@name="na&me"]
  702. [./option[@value=""][not(@selected)][.="[trans]Select&Country[/trans]"]]
  703. [./option[@value="AT"][@selected="selected"][.="[trans]Austria[/trans]"]]
  704. [count(./option)>201]
  705. '
  706. );
  707. }
  708. public function testCsrf()
  709. {
  710. $this->csrfProvider->expects($this->any())
  711. ->method('generateCsrfToken')
  712. ->will($this->returnValue('foo&bar'));
  713. $form = $this->factory->createNamed('csrf', 'na&me', null, array(
  714. 'property_path' => 'name',
  715. ));
  716. $this->assertWidgetMatchesXpath($form->createView(), array(),
  717. '/input
  718. [@type="hidden"]
  719. [@value="foo&bar"]
  720. '
  721. );
  722. }
  723. public function testDateTime()
  724. {
  725. $form = $this->factory->createNamed('datetime', 'na&me', '2011-02-03 04:05:06', array(
  726. 'property_path' => 'name',
  727. 'input' => 'string',
  728. 'with_seconds' => false,
  729. ));
  730. $this->assertWidgetMatchesXpath($form->createView(), array(),
  731. '/div
  732. [
  733. ./div
  734. [@id="na&me_date"]
  735. [
  736. ./select
  737. [@id="na&me_date_month"]
  738. [./option[@value="2"][@selected="selected"]]
  739. /following-sibling::select
  740. [@id="na&me_date_day"]
  741. [./option[@value="3"][@selected="selected"]]
  742. /following-sibling::select
  743. [@id="na&me_date_year"]
  744. [./option[@value="2011"][@selected="selected"]]
  745. ]
  746. /following-sibling::div
  747. [@id="na&me_time"]
  748. [
  749. ./select
  750. [@id="na&me_time_hour"]
  751. [./option[@value="4"][@selected="selected"]]
  752. /following-sibling::select
  753. [@id="na&me_time_minute"]
  754. [./option[@value="5"][@selected="selected"]]
  755. ]
  756. ]
  757. [count(.//select)=5]
  758. '
  759. );
  760. }
  761. public function testDateTimeWithEmptyValueGlobal()
  762. {
  763. $form = $this->factory->createNamed('datetime', 'na&me', null, array(
  764. 'property_path' => 'name',
  765. 'input' => 'string',
  766. 'empty_value' => 'Change&Me',
  767. 'required' => false,
  768. ));
  769. $this->assertWidgetMatchesXpath($form->createView(), array(),
  770. '/div
  771. [
  772. ./div
  773. [@id="na&me_date"]
  774. [
  775. ./select
  776. [@id="na&me_date_month"]
  777. [./option[@value=""][.="[trans]Change&Me[/trans]"]]
  778. /following-sibling::select
  779. [@id="na&me_date_day"]
  780. [./option[@value=""][.="[trans]Change&Me[/trans]"]]
  781. /following-sibling::select
  782. [@id="na&me_date_year"]
  783. [./option[@value=""][.="[trans]Change&Me[/trans]"]]
  784. ]
  785. /following-sibling::div
  786. [@id="na&me_time"]
  787. [
  788. ./select
  789. [@id="na&me_time_hour"]
  790. [./option[@value=""][.="[trans]Change&Me[/trans]"]]
  791. /following-sibling::select
  792. [@id="na&me_time_minute"]
  793. [./option[@value=""][.="[trans]Change&Me[/trans]"]]
  794. ]
  795. ]
  796. [count(.//select)=5]
  797. '
  798. );
  799. }
  800. public function testDateTimeWithEmptyValueOnTime()
  801. {
  802. $form = $this->factory->createNamed('datetime', 'na&me', '2011-02-03', array(
  803. 'property_path' => 'name',
  804. 'input' => 'string',
  805. 'empty_value' => array('hour' => 'Change&Me', 'minute' => 'Change&Me'),
  806. 'required' => false,
  807. ));
  808. $this->assertWidgetMatchesXpath($form->createView(), array(),
  809. '/div
  810. [
  811. ./div
  812. [@id="na&me_date"]
  813. [
  814. ./select
  815. [@id="na&me_date_month"]
  816. [./option[@value="2"][@selected="selected"]]
  817. /following-sibling::select
  818. [@id="na&me_date_day"]
  819. [./option[@value="3"][@selected="selected"]]
  820. /following-sibling::select
  821. [@id="na&me_date_year"]
  822. [./option[@value="2011"][@selected="selected"]]
  823. ]
  824. /following-sibling::div
  825. [@id="na&me_time"]
  826. [
  827. ./select
  828. [@id="na&me_time_hour"]
  829. [./option[@value=""][.="[trans]Change&Me[/trans]"]]
  830. /following-sibling::select
  831. [@id="na&me_time_minute"]
  832. [./option[@value=""][.="[trans]Change&Me[/trans]"]]
  833. ]
  834. ]
  835. [count(.//select)=5]
  836. '
  837. );
  838. }
  839. public function testDateTimeWithSeconds()
  840. {
  841. $form = $this->factory->createNamed('datetime', 'na&me', '2011-02-03 04:05:06', array(
  842. 'property_path' => 'name',
  843. 'input' => 'string',
  844. 'with_seconds' => true,
  845. ));
  846. $this->assertWidgetMatchesXpath($form->createView(), array(),
  847. '/div
  848. [
  849. ./div
  850. [@id="na&me_date"]
  851. [
  852. ./select
  853. [@id="na&me_date_month"]
  854. [./option[@value="2"][@selected="selected"]]
  855. /following-sibling::select
  856. [@id="na&me_date_day"]
  857. [./option[@value="3"][@selected="selected"]]
  858. /following-sibling::select
  859. [@id="na&me_date_year"]
  860. [./option[@value="2011"][@selected="selected"]]
  861. ]
  862. /following-sibling::div
  863. [@id="na&me_time"]
  864. [
  865. ./select
  866. [@id="na&me_time_hour"]
  867. [./option[@value="4"][@selected="selected"]]
  868. /following-sibling::select
  869. [@id="na&me_time_minute"]
  870. [./option[@value="5"][@selected="selected"]]
  871. /following-sibling::select
  872. [@id="na&me_time_second"]
  873. [./option[@value="6"][@selected="selected"]]
  874. ]
  875. ]
  876. [count(.//select)=6]
  877. '
  878. );
  879. }
  880. public function testDateTimeSingleText()
  881. {
  882. $form = $this->factory->createNamed('datetime', 'na&me', '2011-02-03 04:05:06', array(
  883. 'property_path' => 'name',
  884. 'input' => 'string',
  885. 'date_widget' => 'single_text',
  886. 'time_widget' => 'single_text',
  887. ));
  888. $this->assertWidgetMatchesXpath($form->createView(), array(),
  889. '/div
  890. [
  891. ./input
  892. [@type="text"]
  893. [@id="na&me_date"]
  894. [@name="na&me[date]"]
  895. [@value="Feb 3, 2011"]
  896. /following-sibling::input
  897. [@type="text"]
  898. [@id="na&me_time"]
  899. [@name="na&me[time]"]
  900. [@value="04:05:00"]
  901. ]
  902. '
  903. );
  904. }
  905. public function testDateTimeWithWidgetSingleText()
  906. {
  907. $form = $this->factory->createNamed('datetime', 'name', '2011-02-03 04:05:06', array(
  908. 'property_path' => 'name',
  909. 'input' => 'string',
  910. 'widget' => 'single_text',
  911. ));
  912. $this->assertWidgetMatchesXpath($form->createView(), array(),
  913. '/input
  914. [@type="text"]
  915. [@name="name"]
  916. [@value="2011-02-03 04:05:00"]
  917. '
  918. );
  919. }
  920. public function testDateTimeWithWidgetSingleTextIgnoreDateAndTimeWidgets()
  921. {
  922. $form = $this->factory->createNamed('datetime', 'na&me', '2011-02-03 04:05:06', array(
  923. 'property_path' => 'name',
  924. 'input' => 'string',
  925. 'date_widget' => 'choice',
  926. 'time_widget' => 'choice',
  927. 'widget' => 'single_text',
  928. ));
  929. $this->assertWidgetMatchesXpath($form->createView(), array(),
  930. '/input
  931. [@type="text"]
  932. [@name="na&me"]
  933. [@value="2011-02-03 04:05:00"]
  934. '
  935. );
  936. }
  937. public function testDateChoice()
  938. {
  939. $form = $this->factory->createNamed('date', 'na&me', '2011-02-03', array(
  940. 'property_path' => 'name',
  941. 'input' => 'string',
  942. 'widget' => 'choice',
  943. ));
  944. $this->assertWidgetMatchesXpath($form->createView(), array(),
  945. '/div
  946. [
  947. ./select
  948. [@id="na&me_month"]
  949. [./option[@value="2"][@selected="selected"]]
  950. /following-sibling::select
  951. [@id="na&me_day"]
  952. [./option[@value="3"][@selected="selected"]]
  953. /following-sibling::select
  954. [@id="na&me_year"]
  955. [./option[@value="2011"][@selected="selected"]]
  956. ]
  957. [count(./select)=3]
  958. '
  959. );
  960. }
  961. public function testDateChoiceWithEmptyValueGlobal()
  962. {
  963. $form = $this->factory->createNamed('date', 'na&me', null, array(
  964. 'property_path' => 'name',
  965. 'input' => 'string',
  966. 'widget' => 'choice',
  967. 'empty_value' => 'Change&Me',
  968. 'required' => false,
  969. ));
  970. $this->assertWidgetMatchesXpath($form->createView(), array(),
  971. '/div
  972. [
  973. ./select
  974. [@id="na&me_month"]
  975. [./option[@value=""][.="[trans]Change&Me[/trans]"]]
  976. /following-sibling::select
  977. [@id="na&me_day"]
  978. [./option[@value=""][.="[trans]Change&Me[/trans]"]]
  979. /following-sibling::select
  980. [@id="na&me_year"]
  981. [./option[@value=""][.="[trans]Change&Me[/trans]"]]
  982. ]
  983. [count(./select)=3]
  984. '
  985. );
  986. }
  987. public function testDateChoiceWithEmptyValueOnYear()
  988. {
  989. $form = $this->factory->createNamed('date', 'na&me', null, array(
  990. 'property_path' => 'name',
  991. 'input' => 'string',
  992. 'widget' => 'choice',
  993. 'required' => false,
  994. 'empty_value' => array('year' => 'Change&Me'),
  995. ));
  996. $this->assertWidgetMatchesXpath($form->createView(), array(),
  997. '/div
  998. [
  999. ./select
  1000. [@id="na&me_month"]
  1001. [./option[@value="1"]]
  1002. /following-sibling::select
  1003. [@id="na&me_day"]
  1004. [./option[@value="1"]]
  1005. /following-sibling::select
  1006. [@id="na&me_year"]
  1007. [./option[@value=""][.="[trans]Change&Me[/trans]"]]
  1008. ]
  1009. [count(./select)=3]
  1010. '
  1011. );
  1012. }
  1013. public function testDateText()
  1014. {
  1015. $form = $this->factory->createNamed('date', 'na&me', '2011-02-03', array(
  1016. 'property_path' => 'name',
  1017. 'input' => 'string',
  1018. 'widget' => 'text',
  1019. ));
  1020. $this->assertWidgetMatchesXpath($form->createView(), array(),
  1021. '/div
  1022. [
  1023. ./input
  1024. [@id="na&me_month"]
  1025. [@type="text"]
  1026. [@value="2"]
  1027. /following-sibling::input
  1028. [@id="na&me_day"]
  1029. [@type="text"]
  1030. [@value="3"]
  1031. /following-sibling::input
  1032. [@id="na&me_year"]
  1033. [@type="text"]
  1034. [@value="2011"]
  1035. ]
  1036. [count(./input)=3]
  1037. '
  1038. );
  1039. }
  1040. public function testDateSingleText()
  1041. {
  1042. $form = $this->factory->createNamed('date', 'na&me', '2011-02-03', array(
  1043. 'property_path' => 'name',
  1044. 'input' => 'string',
  1045. 'widget' => 'single_text',
  1046. ));
  1047. $this->assertWidgetMatchesXpath($form->createView(), array(),
  1048. '/input
  1049. [@type="text"]
  1050. [@name="na&me"]
  1051. [@value="Feb 3, 2011"]
  1052. '
  1053. );
  1054. }
  1055. public function testDateErrorBubbling()
  1056. {
  1057. $child = $this->factory->createNamed('date', 'date');
  1058. $form = $this->factory->createNamed('form', 'form')->add($child);
  1059. $child->addError(new FormError('Error!'));
  1060. $view = $form->createView();
  1061. $this->assertEmpty($this->renderErrors($view));
  1062. $this->assertNotEmpty($this->renderErrors($view['date']));
  1063. }
  1064. public function testBirthDay()
  1065. {
  1066. $form = $this->factory->createNamed('birthday', 'na&me', '2000-02-03', array(
  1067. 'property_path' => 'name',
  1068. 'input' => 'string',
  1069. ));
  1070. $this->assertWidgetMatchesXpath($form->createView(), array(),
  1071. '/div
  1072. [
  1073. ./select
  1074. [@id="na&me_month"]
  1075. [./option[@value="2"][@selected="selected"]]
  1076. /following-sibling::select
  1077. [@id="na&me_day"]
  1078. [./option[@value="3"][@selected="selected"]]
  1079. /following-sibling::select
  1080. [@id="na&me_year"]
  1081. [./option[@value="2000"][@selected="selected"]]
  1082. ]
  1083. [count(./select)=3]
  1084. '
  1085. );
  1086. }
  1087. public function testBirthDayWithEmptyValue()
  1088. {
  1089. $form = $this->factory->createNamed('birthday', 'na&me', '1950-01-01', array(
  1090. 'property_path' => 'name',
  1091. 'input' => 'string',
  1092. 'empty_value' => '',
  1093. 'required' => false,
  1094. ));
  1095. $this->assertWidgetMatchesXpath($form->createView(), array(),
  1096. '/div
  1097. [
  1098. ./select
  1099. [@id="na&me_month"]
  1100. [./option[@value=""][.="[trans][/trans]"]]
  1101. [./option[@value="1"][@selected="selected"]]
  1102. /following-sibling::select
  1103. [@id="na&me_day"]
  1104. [./option[@value=""][.="[trans][/trans]"]]
  1105. [./option[@value="1"][@selected="selected"]]
  1106. /following-sibling::select
  1107. [@id="na&me_year"]
  1108. [./option[@value=""][.="[trans][/trans]"]]
  1109. [./option[@value="1950"][@selected="selected"]]
  1110. ]
  1111. [count(./select)=3]
  1112. '
  1113. );
  1114. }
  1115. public function testEmail()
  1116. {
  1117. $form = $this->factory->createNamed('email', 'na&me', 'foo&bar', array(
  1118. 'property_path' => 'name',
  1119. ));
  1120. $this->assertWidgetMatchesXpath($form->createView(), array(),
  1121. '/input
  1122. [@type="email"]
  1123. [@name="na&me"]
  1124. [@value="foo&bar"]
  1125. [not(@maxlength)]
  1126. '
  1127. );
  1128. }
  1129. public function testEmailWithMaxLength()
  1130. {
  1131. $form = $this->factory->createNamed('email', 'na&me', 'foo&bar', array(
  1132. 'property_path' => 'name',
  1133. 'max_length' => 123,
  1134. ));
  1135. $this->assertWidgetMatchesXpath($form->createView(), array(),
  1136. '/input
  1137. [@type="email"]
  1138. [@name="na&me"]
  1139. [@value="foo&bar"]
  1140. [@maxlength="123"]
  1141. '
  1142. );
  1143. }
  1144. public function testFile()
  1145. {
  1146. $form = $this->factory->createNamed('file', 'na&me', null, array(
  1147. 'property_path' => 'name',
  1148. ));
  1149. $this->assertWidgetMatchesXpath($form->createView(), array(),
  1150. '/input
  1151. [@type="file"]
  1152. '
  1153. );
  1154. }
  1155. public function testHidden()
  1156. {
  1157. $form = $this->factory->createNamed('hidden', 'na&me', 'foo&bar', array(
  1158. 'property_path' => 'name',
  1159. ));
  1160. $this->assertWidgetMatchesXpath($form->createView(), array(),
  1161. '/input
  1162. [@type="hidden"]
  1163. [@name="na&me"]
  1164. [@value="foo&bar"]
  1165. '
  1166. );
  1167. }
  1168. public function testInteger()
  1169. {
  1170. $form = $this->factory->createNamed('integer', 'na&me', 123, array(
  1171. 'property_path' => 'name',
  1172. ));
  1173. $this->assertWidgetMatchesXpath($form->createView(), array(),
  1174. '/input
  1175. [@type="number"]
  1176. [@name="na&me"]
  1177. [@value="123"]
  1178. '
  1179. );
  1180. }
  1181. public function testLanguage()
  1182. {
  1183. $form = $this->factory->createNamed('language', 'na&me', 'de', array(
  1184. 'property_path' => 'name',
  1185. ));
  1186. $this->assertWidgetMatchesXpath($form->createView(), array(),
  1187. '/select
  1188. [@name="na&me"]
  1189. [./option[@value="de"][@selected="selected"][.="[trans]German[/trans]"]]
  1190. [count(./option)>200]
  1191. '
  1192. );
  1193. }
  1194. public function testLocale()
  1195. {
  1196. $form = $this->factory->createNamed('locale', 'na&me', 'de_AT', array(
  1197. 'property_path' => 'name',
  1198. ));
  1199. $this->assertWidgetMatchesXpath($form->createView(), array(),
  1200. '/select
  1201. [@name="na&me"]
  1202. [./option[@value="de_AT"][@selected="selected"][.="[trans]German (Austria)[/trans]"]]
  1203. [count(./option)>200]
  1204. '
  1205. );
  1206. }
  1207. public function testMoney()
  1208. {
  1209. $form = $this->factory->createNamed('money', 'na&me', 1234.56, array(
  1210. 'property_path' => 'name',
  1211. 'currency' => 'EUR',
  1212. ));
  1213. $this->assertWidgetMatchesXpath($form->createView(), array(),
  1214. '/input
  1215. [@type="text"]
  1216. [@name="na&me"]
  1217. [@value="1234.56"]
  1218. [contains(.., "€")]
  1219. '
  1220. );
  1221. }
  1222. public function testNumber()
  1223. {
  1224. $form = $this->factory->createNamed('number', 'na&me', 1234.56, array(
  1225. 'property_path' => 'name',
  1226. ));
  1227. $this->assertWidgetMatchesXpath($form->createView(), array(),
  1228. '/input
  1229. [@type="text"]
  1230. [@name="na&me"]
  1231. [@value="1234.56"]
  1232. '
  1233. );
  1234. }
  1235. public function testPassword()
  1236. {
  1237. $form = $this->factory->createNamed('password', 'na&me', 'foo&bar', array(
  1238. 'property_path' => 'name',
  1239. ));
  1240. $this->assertWidgetMatchesXpath($form->createView(), array(),
  1241. '/input
  1242. [@type="password"]
  1243. [@name="na&me"]
  1244. '
  1245. );
  1246. }
  1247. public function testPasswordBoundNotAlwaysEmpty()
  1248. {
  1249. $form = $this->factory->createNamed('password', 'na&me', null, array(
  1250. 'property_path' => 'name',
  1251. 'always_empty' => false,
  1252. ));
  1253. $form->bind('foo&bar');
  1254. $this->assertWidgetMatchesXpath($form->createView(), array(),
  1255. '/input
  1256. [@type="password"]
  1257. [@name="na&me"]
  1258. [@value="foo&bar"]
  1259. '
  1260. );
  1261. }
  1262. public function testPasswordWithMaxLength()
  1263. {
  1264. $form = $this->factory->createNamed('password', 'na&me', 'foo&bar', array(
  1265. 'property_path' => 'name',
  1266. 'max_length' => 123,
  1267. ));
  1268. $this->assertWidgetMatchesXpath($form->createView(), array(),
  1269. '/input
  1270. [@type="password"]
  1271. [@name="na&me"]
  1272. [@maxlength="123"]
  1273. '
  1274. );
  1275. }
  1276. public function testPercent()
  1277. {
  1278. $form = $this->factory->createNamed('percent', 'na&me', 0.1, array(
  1279. 'property_path' => 'name',
  1280. ));
  1281. $this->assertWidgetMatchesXpath($form->createView(), array(),
  1282. '/input
  1283. [@type="text"]
  1284. [@name="na&me"]
  1285. [@value="10"]
  1286. [contains(.., "%")]
  1287. '
  1288. );
  1289. }
  1290. public function testCheckedRadio()
  1291. {
  1292. $form = $this->factory->createNamed('radio', 'na&me', true, array(
  1293. 'property_path' => 'name',
  1294. ));
  1295. $this->assertWidgetMatchesXpath($form->createView(), array(),
  1296. '/input
  1297. [@type="radio"]
  1298. [@name="na&me"]
  1299. [@checked="checked"]
  1300. [@value=""]
  1301. '
  1302. );
  1303. }
  1304. public function testCheckedRadioWithValue()
  1305. {
  1306. $form = $this->factory->createNamed('radio', 'na&me', true, array(
  1307. 'property_path' => 'name',
  1308. 'value' => 'foo&bar',
  1309. ));
  1310. $this->assertWidgetMatchesXpath($form->createView(), array(),
  1311. '/input
  1312. [@type="radio"]
  1313. [@name="na&me"]
  1314. [@checked="checked"]
  1315. [@value="foo&bar"]
  1316. '
  1317. );
  1318. }
  1319. public function testUncheckedRadio()
  1320. {
  1321. $form = $this->factory->createNamed('radio', 'na&me', false, array(
  1322. 'property_path' => 'name',
  1323. ));
  1324. $this->assertWidgetMatchesXpath($form->createView(), array(),
  1325. '/input
  1326. [@type="radio"]
  1327. [@name="na&me"]
  1328. [not(@checked)]
  1329. '
  1330. );
  1331. }
  1332. public function testTextarea()
  1333. {
  1334. $form = $this->factory->createNamed('textarea', 'na&me', 'foo&bar', array(
  1335. 'property_path' => 'name',
  1336. 'pattern' => 'foo',
  1337. ));
  1338. $this->assertWidgetMatchesXpath($form->createView(), array(),
  1339. '/textarea
  1340. [@name="na&me"]
  1341. [not(@pattern)]
  1342. [.="foo&bar"]
  1343. '
  1344. );
  1345. }
  1346. public function testText()
  1347. {
  1348. $form = $this->factory->createNamed('text', 'na&me', 'foo&bar', array(
  1349. 'property_path' => 'name',
  1350. ));
  1351. $this->assertWidgetMatchesXpath($form->createView(), array(),
  1352. '/input
  1353. [@type="text"]
  1354. [@name="na&me"]
  1355. [@value="foo&bar"]
  1356. [not(@maxlength)]
  1357. '
  1358. );
  1359. }
  1360. public function testTextWithMaxLength()
  1361. {
  1362. $form = $this->factory->createNamed('text', 'na&me', 'foo&bar', array(
  1363. 'property_path' => 'name',
  1364. 'max_length' => 123,
  1365. ));
  1366. $this->assertWidgetMatchesXpath($form->createView(), array(),
  1367. '/input
  1368. [@type="text"]
  1369. [@name="na&me"]
  1370. [@value="foo&bar"]
  1371. [@maxlength="123"]
  1372. '
  1373. );
  1374. }
  1375. public function testSearch()
  1376. {
  1377. $form = $this->factory->createNamed('search', 'na&me', 'foo&bar', array(
  1378. 'property_path' => 'name',
  1379. ));
  1380. $this->assertWidgetMatchesXpath($form->createView(), array(),
  1381. '/input
  1382. [@type="search"]
  1383. [@name="na&me"]
  1384. [@value="foo&bar"]
  1385. [not(@maxlength)]
  1386. '
  1387. );
  1388. }
  1389. public function testTime()
  1390. {
  1391. $form = $this->factory->createNamed('time', 'na&me', '04:05:06', array(
  1392. 'property_path' => 'name',
  1393. 'input' => 'string',
  1394. 'with_seconds' => false,
  1395. ));
  1396. $this->assertWidgetMatchesXpath($form->createView(), array(),
  1397. '/div
  1398. [
  1399. ./select
  1400. [@id="na&me_hour"]
  1401. [@size="1"]
  1402. [./option[@value="4"][@selected="selected"]]
  1403. /following-sibling::select
  1404. [@id="na&me_minute"]
  1405. [@size="1"]
  1406. [./option[@value="5"][@selected="selected"]]
  1407. ]
  1408. [count(./select)=2]
  1409. '
  1410. );
  1411. }
  1412. public function testTimeWithSeconds()
  1413. {
  1414. $form = $this->factory->createNamed('time', 'na&me', '04:05:06', array(
  1415. 'property_path' => 'name',
  1416. 'input' => 'string',
  1417. 'with_seconds' => true,
  1418. ));
  1419. $this->assertWidgetMatchesXpath($form->createView(), array(),
  1420. '/div
  1421. [
  1422. ./select
  1423. [@id="na&me_hour"]
  1424. [@size="1"]
  1425. [./option[@value="4"][@selected="selected"]]
  1426. [count(./option)>23]
  1427. /following-sibling::select
  1428. [@id="na&me_minute"]
  1429. [@size="1"]
  1430. [./option[@value="5"][@selected="selected"]]
  1431. [count(./option)>59]
  1432. /following-sibling::select
  1433. [@id="na&me_second"]
  1434. [@size="1"]
  1435. [./option[@value="6"][@selected="selected"]]
  1436. [count(./option)>59]
  1437. ]
  1438. [count(./select)=3]
  1439. '
  1440. );
  1441. }
  1442. public function testTimeText()
  1443. {
  1444. $form = $this->factory->createNamed('time', 'na&me', '04:05:06', array(
  1445. 'property_path' => 'name',
  1446. 'input' => 'string',
  1447. 'widget' => 'text',
  1448. ));
  1449. $this->assertWidgetMatchesXpath($form->createView(), array(),
  1450. '/div
  1451. [
  1452. ./input
  1453. [@type="text"]
  1454. [@id="na&me_hour"]
  1455. [@name="na&me[hour]"]
  1456. [@value="04"]
  1457. [@size="1"]
  1458. [@required="required"]
  1459. /following-sibling::input
  1460. [@type="text"]
  1461. [@id="na&me_minute"]
  1462. [@name="na&me[minute]"]
  1463. [@value="05"]
  1464. [@size="1"]
  1465. [@required="required"]
  1466. ]
  1467. [count(./input)=2]
  1468. '
  1469. );
  1470. }
  1471. public function testTimeSingleText()
  1472. {
  1473. $form = $this->factory->createNamed('time', 'na&me', '04:05:06', array(
  1474. 'property_path' => 'name',
  1475. 'input' => 'string',
  1476. 'widget' => 'single_text',
  1477. ));
  1478. $this->assertWidgetMatchesXpath($form->createView(), array(),
  1479. '/input
  1480. [@type="text"]
  1481. [@name="na&me"]
  1482. [@value="04:05:00"]
  1483. '
  1484. );
  1485. }
  1486. public function testTimeWithEmptyValueGlobal()
  1487. {
  1488. $form = $this->factory->createNamed('time', 'na&me', null, array(
  1489. 'property_path' => 'name',
  1490. 'input' => 'string',
  1491. 'empty_value' => 'Change&Me',
  1492. 'required' => false,
  1493. ));
  1494. $this->assertWidgetMatchesXpath($form->createView(), array(),
  1495. '/div
  1496. [
  1497. ./select
  1498. [@id="na&me_hour"]
  1499. [./option[@value=""][.="[trans]Change&Me[/trans]"]]
  1500. [count(./option)>24]
  1501. /following-sibling::select
  1502. [@id="na&me_minute"]
  1503. [./option[@value=""][.="[trans]Change&Me[/trans]"]]
  1504. [count(./option)>60]
  1505. ]
  1506. [count(./select)=2]
  1507. '
  1508. );
  1509. }
  1510. public function testTimeWithEmptyValueOnYear()
  1511. {
  1512. $form = $this->factory->createNamed('time', 'na&me', null, array(
  1513. 'property_path' => 'name',
  1514. 'input' => 'string',
  1515. 'required' => false,
  1516. 'empty_value' => array('hour' => 'Change&Me'),
  1517. ));
  1518. $this->assertWidgetMatchesXpath($form->createView(), array(),
  1519. '/div
  1520. [
  1521. ./select
  1522. [@id="na&me_hour"]
  1523. [./option[@value=""][.="[trans]Change&Me[/trans]"]]
  1524. [count(./option)>24]
  1525. /following-sibling::select
  1526. [@id="na&me_minute"]
  1527. [./option[@value="1"]]
  1528. [count(./option)>59]
  1529. ]
  1530. [count(./select)=2]
  1531. '
  1532. );
  1533. }
  1534. public function testTimeErrorBubbling()
  1535. {
  1536. $child = $this->factory->createNamed('time', 'time');
  1537. $form = $this->factory->createNamed('form', 'form')->add($child);
  1538. $child->addError(new FormError('Error!'));
  1539. $view = $form->createView();
  1540. $this->assertEmpty($this->renderErrors($view));
  1541. $this->assertNotEmpty($this->renderErrors($view['time']));
  1542. }
  1543. public function testTimezone()
  1544. {
  1545. $form = $this->factory->createNamed('timezone', 'na&me', 'Europe/Vienna', array(
  1546. 'property_path' => 'name',
  1547. ));
  1548. $this->assertWidgetMatchesXpath($form->createView(), array(),
  1549. '/select
  1550. [@name="na&me"]
  1551. [@required="required"]
  1552. [./optgroup
  1553. [@label="[trans]Europe[/trans]"]
  1554. [./option[@value="Europe/Vienna"][@selected="selected"][.="[trans]Vienna[/trans]"]]
  1555. ]
  1556. [count(./optgroup)>10]
  1557. [count(.//option)>200]
  1558. '
  1559. );
  1560. }
  1561. public function testTimezoneWithEmptyValue()
  1562. {
  1563. $form = $this->factory->createNamed('timezone', 'na&me', null, array(
  1564. 'property_path' => 'name',
  1565. 'empty_value' => 'Select&Timezone',
  1566. 'required' => false,
  1567. ));
  1568. $this->assertWidgetMatchesXpath($form->createView(), array(),
  1569. '/select
  1570. [./option[@value=""][.="[trans]Select&Timezone[/trans]"]]
  1571. [count(./optgroup)>10]
  1572. [count(.//option)>201]
  1573. '
  1574. );
  1575. }
  1576. public function testUrl()
  1577. {
  1578. $url = 'http://www.google.com?foo1=bar1&foo2=bar2';
  1579. $form = $this->factory->createNamed('url', 'na&me', $url, array(
  1580. 'property_path' => 'name',
  1581. ));
  1582. $this->assertWidgetMatchesXpath($form->createView(), array(),
  1583. '/input
  1584. [@type="url"]
  1585. [@name="na&me"]
  1586. [@value="http://www.google.com?foo1=bar1&foo2=bar2"]
  1587. '
  1588. );
  1589. }
  1590. public function testCollectionPrototype()
  1591. {
  1592. $form = $this->factory->createNamedBuilder('form', 'na&me', array('items' => array('one', 'two', 'three')))
  1593. ->add('items', 'collection', array('allow_add' => true))
  1594. ->getForm()
  1595. ->createView();
  1596. $html = $this->renderWidget($form);
  1597. $this->assertMatchesXpath($html,
  1598. '//div[@id="na&me_items"][@data-prototype]
  1599. |
  1600. //table[@id="na&me_items"][@data-prototype]
  1601. '
  1602. );
  1603. }
  1604. }