AbstractLayoutTest.php 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791
  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. ));
  1337. $this->assertWidgetMatchesXpath($form->createView(), array(),
  1338. '/textarea
  1339. [@name="na&me"]
  1340. [.="foo&bar"]
  1341. '
  1342. );
  1343. }
  1344. public function testText()
  1345. {
  1346. $form = $this->factory->createNamed('text', 'na&me', 'foo&bar', array(
  1347. 'property_path' => 'name',
  1348. ));
  1349. $this->assertWidgetMatchesXpath($form->createView(), array(),
  1350. '/input
  1351. [@type="text"]
  1352. [@name="na&me"]
  1353. [@value="foo&bar"]
  1354. [not(@maxlength)]
  1355. '
  1356. );
  1357. }
  1358. public function testTextWithMaxLength()
  1359. {
  1360. $form = $this->factory->createNamed('text', 'na&me', 'foo&bar', array(
  1361. 'property_path' => 'name',
  1362. 'max_length' => 123,
  1363. ));
  1364. $this->assertWidgetMatchesXpath($form->createView(), array(),
  1365. '/input
  1366. [@type="text"]
  1367. [@name="na&me"]
  1368. [@value="foo&bar"]
  1369. [@maxlength="123"]
  1370. '
  1371. );
  1372. }
  1373. public function testSearch()
  1374. {
  1375. $form = $this->factory->createNamed('search', 'na&me', 'foo&bar', array(
  1376. 'property_path' => 'name',
  1377. ));
  1378. $this->assertWidgetMatchesXpath($form->createView(), array(),
  1379. '/input
  1380. [@type="search"]
  1381. [@name="na&me"]
  1382. [@value="foo&bar"]
  1383. [not(@maxlength)]
  1384. '
  1385. );
  1386. }
  1387. public function testTime()
  1388. {
  1389. $form = $this->factory->createNamed('time', 'na&me', '04:05:06', array(
  1390. 'property_path' => 'name',
  1391. 'input' => 'string',
  1392. 'with_seconds' => false,
  1393. ));
  1394. $this->assertWidgetMatchesXpath($form->createView(), array(),
  1395. '/div
  1396. [
  1397. ./select
  1398. [@id="na&me_hour"]
  1399. [@size="1"]
  1400. [./option[@value="4"][@selected="selected"]]
  1401. /following-sibling::select
  1402. [@id="na&me_minute"]
  1403. [@size="1"]
  1404. [./option[@value="5"][@selected="selected"]]
  1405. ]
  1406. [count(./select)=2]
  1407. '
  1408. );
  1409. }
  1410. public function testTimeWithSeconds()
  1411. {
  1412. $form = $this->factory->createNamed('time', 'na&me', '04:05:06', array(
  1413. 'property_path' => 'name',
  1414. 'input' => 'string',
  1415. 'with_seconds' => true,
  1416. ));
  1417. $this->assertWidgetMatchesXpath($form->createView(), array(),
  1418. '/div
  1419. [
  1420. ./select
  1421. [@id="na&me_hour"]
  1422. [@size="1"]
  1423. [./option[@value="4"][@selected="selected"]]
  1424. [count(./option)>23]
  1425. /following-sibling::select
  1426. [@id="na&me_minute"]
  1427. [@size="1"]
  1428. [./option[@value="5"][@selected="selected"]]
  1429. [count(./option)>59]
  1430. /following-sibling::select
  1431. [@id="na&me_second"]
  1432. [@size="1"]
  1433. [./option[@value="6"][@selected="selected"]]
  1434. [count(./option)>59]
  1435. ]
  1436. [count(./select)=3]
  1437. '
  1438. );
  1439. }
  1440. public function testTimeText()
  1441. {
  1442. $form = $this->factory->createNamed('time', 'na&me', '04:05:06', array(
  1443. 'property_path' => 'name',
  1444. 'input' => 'string',
  1445. 'widget' => 'text',
  1446. ));
  1447. $this->assertWidgetMatchesXpath($form->createView(), array(),
  1448. '/div
  1449. [
  1450. ./input
  1451. [@type="text"]
  1452. [@id="na&me_hour"]
  1453. [@name="na&me[hour]"]
  1454. [@value="04"]
  1455. [@size="1"]
  1456. [@required="required"]
  1457. /following-sibling::input
  1458. [@type="text"]
  1459. [@id="na&me_minute"]
  1460. [@name="na&me[minute]"]
  1461. [@value="05"]
  1462. [@size="1"]
  1463. [@required="required"]
  1464. ]
  1465. [count(./input)=2]
  1466. '
  1467. );
  1468. }
  1469. public function testTimeSingleText()
  1470. {
  1471. $form = $this->factory->createNamed('time', 'na&me', '04:05:06', array(
  1472. 'property_path' => 'name',
  1473. 'input' => 'string',
  1474. 'widget' => 'single_text',
  1475. ));
  1476. $this->assertWidgetMatchesXpath($form->createView(), array(),
  1477. '/input
  1478. [@type="text"]
  1479. [@name="na&me"]
  1480. [@value="04:05:00"]
  1481. '
  1482. );
  1483. }
  1484. public function testTimeWithEmptyValueGlobal()
  1485. {
  1486. $form = $this->factory->createNamed('time', 'na&me', null, array(
  1487. 'property_path' => 'name',
  1488. 'input' => 'string',
  1489. 'empty_value' => 'Change&Me',
  1490. 'required' => false,
  1491. ));
  1492. $this->assertWidgetMatchesXpath($form->createView(), array(),
  1493. '/div
  1494. [
  1495. ./select
  1496. [@id="na&me_hour"]
  1497. [./option[@value=""][.="[trans]Change&Me[/trans]"]]
  1498. [count(./option)>24]
  1499. /following-sibling::select
  1500. [@id="na&me_minute"]
  1501. [./option[@value=""][.="[trans]Change&Me[/trans]"]]
  1502. [count(./option)>60]
  1503. ]
  1504. [count(./select)=2]
  1505. '
  1506. );
  1507. }
  1508. public function testTimeWithEmptyValueOnYear()
  1509. {
  1510. $form = $this->factory->createNamed('time', 'na&me', null, array(
  1511. 'property_path' => 'name',
  1512. 'input' => 'string',
  1513. 'required' => false,
  1514. 'empty_value' => array('hour' => 'Change&Me'),
  1515. ));
  1516. $this->assertWidgetMatchesXpath($form->createView(), array(),
  1517. '/div
  1518. [
  1519. ./select
  1520. [@id="na&me_hour"]
  1521. [./option[@value=""][.="[trans]Change&Me[/trans]"]]
  1522. [count(./option)>24]
  1523. /following-sibling::select
  1524. [@id="na&me_minute"]
  1525. [./option[@value="1"]]
  1526. [count(./option)>59]
  1527. ]
  1528. [count(./select)=2]
  1529. '
  1530. );
  1531. }
  1532. public function testTimeErrorBubbling()
  1533. {
  1534. $child = $this->factory->createNamed('time', 'time');
  1535. $form = $this->factory->createNamed('form', 'form')->add($child);
  1536. $child->addError(new FormError('Error!'));
  1537. $view = $form->createView();
  1538. $this->assertEmpty($this->renderErrors($view));
  1539. $this->assertNotEmpty($this->renderErrors($view['time']));
  1540. }
  1541. public function testTimezone()
  1542. {
  1543. $form = $this->factory->createNamed('timezone', 'na&me', 'Europe/Vienna', array(
  1544. 'property_path' => 'name',
  1545. ));
  1546. $this->assertWidgetMatchesXpath($form->createView(), array(),
  1547. '/select
  1548. [@name="na&me"]
  1549. [@required="required"]
  1550. [./optgroup
  1551. [@label="[trans]Europe[/trans]"]
  1552. [./option[@value="Europe/Vienna"][@selected="selected"][.="[trans]Vienna[/trans]"]]
  1553. ]
  1554. [count(./optgroup)>10]
  1555. [count(.//option)>200]
  1556. '
  1557. );
  1558. }
  1559. public function testTimezoneWithEmptyValue()
  1560. {
  1561. $form = $this->factory->createNamed('timezone', 'na&me', null, array(
  1562. 'property_path' => 'name',
  1563. 'empty_value' => 'Select&Timezone',
  1564. 'required' => false,
  1565. ));
  1566. $this->assertWidgetMatchesXpath($form->createView(), array(),
  1567. '/select
  1568. [./option[@value=""][.="[trans]Select&Timezone[/trans]"]]
  1569. [count(./optgroup)>10]
  1570. [count(.//option)>201]
  1571. '
  1572. );
  1573. }
  1574. public function testUrl()
  1575. {
  1576. $url = 'http://www.google.com?foo1=bar1&foo2=bar2';
  1577. $form = $this->factory->createNamed('url', 'na&me', $url, array(
  1578. 'property_path' => 'name',
  1579. ));
  1580. $this->assertWidgetMatchesXpath($form->createView(), array(),
  1581. '/input
  1582. [@type="url"]
  1583. [@name="na&me"]
  1584. [@value="http://www.google.com?foo1=bar1&foo2=bar2"]
  1585. '
  1586. );
  1587. }
  1588. public function testCollectionPrototype()
  1589. {
  1590. $form = $this->factory->createNamedBuilder('form', 'na&me', array('items' => array('one', 'two', 'three')))
  1591. ->add('items', 'collection', array('allow_add' => true))
  1592. ->getForm()
  1593. ->createView();
  1594. $html = $this->renderWidget($form);
  1595. $this->assertMatchesXpath($html,
  1596. '//div[@id="na&me_items"][@data-prototype]
  1597. |
  1598. //table[@id="na&me_items"][@data-prototype]
  1599. '
  1600. );
  1601. }
  1602. }