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