AbstractLayoutTest.php 44 KB

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