AbstractLayoutTest.php 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653
  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 testDateTimeSingleText()
  805. {
  806. $form = $this->factory->createNamed('datetime', 'na&me', '2011-02-03 04:05:06', array(
  807. 'property_path' => 'name',
  808. 'input' => 'string',
  809. 'date_widget' => 'single_text',
  810. 'time_widget' => 'single_text',
  811. ));
  812. $this->assertWidgetMatchesXpath($form->createView(), array(),
  813. '/div
  814. [
  815. ./input
  816. [@type="text"]
  817. [@id="na&me_date"]
  818. [@name="na&me[date]"]
  819. [@value="Feb 3, 2011"]
  820. /following-sibling::input
  821. [@type="text"]
  822. [@id="na&me_time"]
  823. [@name="na&me[time]"]
  824. [@value="04:05:00"]
  825. ]
  826. '
  827. );
  828. }
  829. public function testDateTimeWithWidgetSingleText()
  830. {
  831. $form = $this->factory->createNamed('datetime', 'name', '2011-02-03 04:05:06', array(
  832. 'property_path' => 'name',
  833. 'input' => 'string',
  834. 'widget' => 'single_text',
  835. ));
  836. $this->assertWidgetMatchesXpath($form->createView(), array(),
  837. '/input
  838. [@type="text"]
  839. [@name="name"]
  840. [@value="2011-02-03 04:05:00"]
  841. '
  842. );
  843. }
  844. public function testDateTimeWithWidgetSingleTextIgnoreDateAndTimeWidgets()
  845. {
  846. $form = $this->factory->createNamed('datetime', 'na&me', '2011-02-03 04:05:06', array(
  847. 'property_path' => 'name',
  848. 'input' => 'string',
  849. 'date_widget' => 'choice',
  850. 'time_widget' => 'choice',
  851. 'widget' => 'single_text',
  852. ));
  853. $this->assertWidgetMatchesXpath($form->createView(), array(),
  854. '/input
  855. [@type="text"]
  856. [@name="na&me"]
  857. [@value="2011-02-03 04:05:00"]
  858. '
  859. );
  860. }
  861. public function testDateChoice()
  862. {
  863. $form = $this->factory->createNamed('date', 'na&me', '2011-02-03', array(
  864. 'property_path' => 'name',
  865. 'input' => 'string',
  866. 'widget' => 'choice',
  867. ));
  868. $this->assertWidgetMatchesXpath($form->createView(), array(),
  869. '/div
  870. [
  871. ./select
  872. [@id="na&me_month"]
  873. [./option[@value="2"][@selected="selected"]]
  874. /following-sibling::select
  875. [@id="na&me_day"]
  876. [./option[@value="3"][@selected="selected"]]
  877. /following-sibling::select
  878. [@id="na&me_year"]
  879. [./option[@value="2011"][@selected="selected"]]
  880. ]
  881. [count(./select)=3]
  882. '
  883. );
  884. }
  885. public function testDateChoiceWithEmptyValueGlobal()
  886. {
  887. $form = $this->factory->createNamed('date', 'na&me', null, array(
  888. 'property_path' => 'name',
  889. 'input' => 'string',
  890. 'widget' => 'choice',
  891. 'empty_value' => 'Change&Me',
  892. 'required' => false,
  893. ));
  894. $this->assertWidgetMatchesXpath($form->createView(), array(),
  895. '/div
  896. [
  897. ./select
  898. [@id="na&me_month"]
  899. [./option[@value=""][.="[trans]Change&Me[/trans]"]]
  900. /following-sibling::select
  901. [@id="na&me_day"]
  902. [./option[@value=""][.="[trans]Change&Me[/trans]"]]
  903. /following-sibling::select
  904. [@id="na&me_year"]
  905. [./option[@value=""][.="[trans]Change&Me[/trans]"]]
  906. ]
  907. [count(./select)=3]
  908. '
  909. );
  910. }
  911. public function testDateChoiceWithEmptyValueOnYear()
  912. {
  913. $form = $this->factory->createNamed('date', 'na&me', null, array(
  914. 'property_path' => 'name',
  915. 'input' => 'string',
  916. 'widget' => 'choice',
  917. 'required' => false,
  918. 'empty_value' => array('year' => 'Change&Me'),
  919. ));
  920. $this->assertWidgetMatchesXpath($form->createView(), array(),
  921. '/div
  922. [
  923. ./select
  924. [@id="na&me_month"]
  925. [./option[@value="1"]]
  926. /following-sibling::select
  927. [@id="na&me_day"]
  928. [./option[@value="1"]]
  929. /following-sibling::select
  930. [@id="na&me_year"]
  931. [./option[@value=""][.="[trans]Change&Me[/trans]"]]
  932. ]
  933. [count(./select)=3]
  934. '
  935. );
  936. }
  937. public function testDateText()
  938. {
  939. $form = $this->factory->createNamed('date', 'na&me', '2011-02-03', array(
  940. 'property_path' => 'name',
  941. 'input' => 'string',
  942. 'widget' => 'text',
  943. ));
  944. $this->assertWidgetMatchesXpath($form->createView(), array(),
  945. '/div
  946. [
  947. ./input
  948. [@id="na&me_month"]
  949. [@type="text"]
  950. [@value="2"]
  951. /following-sibling::input
  952. [@id="na&me_day"]
  953. [@type="text"]
  954. [@value="3"]
  955. /following-sibling::input
  956. [@id="na&me_year"]
  957. [@type="text"]
  958. [@value="2011"]
  959. ]
  960. [count(./input)=3]
  961. '
  962. );
  963. }
  964. public function testDateSingleText()
  965. {
  966. $form = $this->factory->createNamed('date', 'na&me', '2011-02-03', array(
  967. 'property_path' => 'name',
  968. 'input' => 'string',
  969. 'widget' => 'single_text',
  970. ));
  971. $this->assertWidgetMatchesXpath($form->createView(), array(),
  972. '/input
  973. [@type="text"]
  974. [@name="na&me"]
  975. [@value="Feb 3, 2011"]
  976. '
  977. );
  978. }
  979. public function testBirthDay()
  980. {
  981. $form = $this->factory->createNamed('birthday', 'na&me', '2000-02-03', array(
  982. 'property_path' => 'name',
  983. 'input' => 'string',
  984. ));
  985. $this->assertWidgetMatchesXpath($form->createView(), array(),
  986. '/div
  987. [
  988. ./select
  989. [@id="na&me_month"]
  990. [./option[@value="2"][@selected="selected"]]
  991. /following-sibling::select
  992. [@id="na&me_day"]
  993. [./option[@value="3"][@selected="selected"]]
  994. /following-sibling::select
  995. [@id="na&me_year"]
  996. [./option[@value="2000"][@selected="selected"]]
  997. ]
  998. [count(./select)=3]
  999. '
  1000. );
  1001. }
  1002. public function testBirthDayWithEmptyValue()
  1003. {
  1004. $form = $this->factory->createNamed('birthday', 'na&me', '1950-01-01', array(
  1005. 'property_path' => 'name',
  1006. 'input' => 'string',
  1007. 'empty_value' => '',
  1008. 'required' => false,
  1009. ));
  1010. $this->assertWidgetMatchesXpath($form->createView(), array(),
  1011. '/div
  1012. [
  1013. ./select
  1014. [@id="na&me_month"]
  1015. [./option[@value=""][.="[trans][/trans]"]]
  1016. [./option[@value="1"][@selected="selected"]]
  1017. /following-sibling::select
  1018. [@id="na&me_day"]
  1019. [./option[@value=""][.="[trans][/trans]"]]
  1020. [./option[@value="1"][@selected="selected"]]
  1021. /following-sibling::select
  1022. [@id="na&me_year"]
  1023. [./option[@value=""][.="[trans][/trans]"]]
  1024. [./option[@value="1950"][@selected="selected"]]
  1025. ]
  1026. [count(./select)=3]
  1027. '
  1028. );
  1029. }
  1030. public function testEmail()
  1031. {
  1032. $form = $this->factory->createNamed('email', 'na&me', 'foo&bar', array(
  1033. 'property_path' => 'name',
  1034. ));
  1035. $this->assertWidgetMatchesXpath($form->createView(), array(),
  1036. '/input
  1037. [@type="email"]
  1038. [@name="na&me"]
  1039. [@value="foo&bar"]
  1040. [not(@maxlength)]
  1041. '
  1042. );
  1043. }
  1044. public function testEmailWithMaxLength()
  1045. {
  1046. $form = $this->factory->createNamed('email', 'na&me', 'foo&bar', array(
  1047. 'property_path' => 'name',
  1048. 'max_length' => 123,
  1049. ));
  1050. $this->assertWidgetMatchesXpath($form->createView(), array(),
  1051. '/input
  1052. [@type="email"]
  1053. [@name="na&me"]
  1054. [@value="foo&bar"]
  1055. [@maxlength="123"]
  1056. '
  1057. );
  1058. }
  1059. public function testFile()
  1060. {
  1061. $form = $this->factory->createNamed('file', 'na&me', null, array(
  1062. 'property_path' => 'name',
  1063. ));
  1064. $this->assertWidgetMatchesXpath($form->createView(), array(),
  1065. '/input
  1066. [@type="file"]
  1067. '
  1068. );
  1069. }
  1070. public function testHidden()
  1071. {
  1072. $form = $this->factory->createNamed('hidden', 'na&me', 'foo&bar', array(
  1073. 'property_path' => 'name',
  1074. ));
  1075. $this->assertWidgetMatchesXpath($form->createView(), array(),
  1076. '/input
  1077. [@type="hidden"]
  1078. [@name="na&me"]
  1079. [@value="foo&bar"]
  1080. '
  1081. );
  1082. }
  1083. public function testInteger()
  1084. {
  1085. $form = $this->factory->createNamed('integer', 'na&me', 123, array(
  1086. 'property_path' => 'name',
  1087. ));
  1088. $this->assertWidgetMatchesXpath($form->createView(), array(),
  1089. '/input
  1090. [@type="number"]
  1091. [@name="na&me"]
  1092. [@value="123"]
  1093. '
  1094. );
  1095. }
  1096. public function testLanguage()
  1097. {
  1098. $form = $this->factory->createNamed('language', 'na&me', 'de', array(
  1099. 'property_path' => 'name',
  1100. ));
  1101. $this->assertWidgetMatchesXpath($form->createView(), array(),
  1102. '/select
  1103. [@name="na&me"]
  1104. [./option[@value="de"][@selected="selected"][.="German"]]
  1105. [count(./option)>200]
  1106. '
  1107. );
  1108. }
  1109. public function testLocale()
  1110. {
  1111. $form = $this->factory->createNamed('locale', 'na&me', 'de_AT', array(
  1112. 'property_path' => 'name',
  1113. ));
  1114. $this->assertWidgetMatchesXpath($form->createView(), array(),
  1115. '/select
  1116. [@name="na&me"]
  1117. [./option[@value="de_AT"][@selected="selected"][.="German (Austria)"]]
  1118. [count(./option)>200]
  1119. '
  1120. );
  1121. }
  1122. public function testMoney()
  1123. {
  1124. $form = $this->factory->createNamed('money', 'na&me', 1234.56, array(
  1125. 'property_path' => 'name',
  1126. 'currency' => 'EUR',
  1127. ));
  1128. $this->assertWidgetMatchesXpath($form->createView(), array(),
  1129. '/input
  1130. [@type="text"]
  1131. [@name="na&me"]
  1132. [@value="1234.56"]
  1133. [contains(.., "€")]
  1134. '
  1135. );
  1136. }
  1137. public function testNumber()
  1138. {
  1139. $form = $this->factory->createNamed('number', 'na&me', 1234.56, array(
  1140. 'property_path' => 'name',
  1141. ));
  1142. $this->assertWidgetMatchesXpath($form->createView(), array(),
  1143. '/input
  1144. [@type="text"]
  1145. [@name="na&me"]
  1146. [@value="1234.56"]
  1147. '
  1148. );
  1149. }
  1150. public function testPassword()
  1151. {
  1152. $form = $this->factory->createNamed('password', 'na&me', 'foo&bar', array(
  1153. 'property_path' => 'name',
  1154. ));
  1155. $this->assertWidgetMatchesXpath($form->createView(), array(),
  1156. '/input
  1157. [@type="password"]
  1158. [@name="na&me"]
  1159. [@value=""]
  1160. '
  1161. );
  1162. }
  1163. public function testPasswordBoundNotAlwaysEmpty()
  1164. {
  1165. $form = $this->factory->createNamed('password', 'na&me', null, array(
  1166. 'property_path' => 'name',
  1167. 'always_empty' => false,
  1168. ));
  1169. $form->bind('foo&bar');
  1170. $this->assertWidgetMatchesXpath($form->createView(), array(),
  1171. '/input
  1172. [@type="password"]
  1173. [@name="na&me"]
  1174. [@value="foo&bar"]
  1175. '
  1176. );
  1177. }
  1178. public function testPasswordWithMaxLength()
  1179. {
  1180. $form = $this->factory->createNamed('password', 'na&me', 'foo&bar', array(
  1181. 'property_path' => 'name',
  1182. 'max_length' => 123,
  1183. ));
  1184. $this->assertWidgetMatchesXpath($form->createView(), array(),
  1185. '/input
  1186. [@type="password"]
  1187. [@name="na&me"]
  1188. [@value=""]
  1189. [@maxlength="123"]
  1190. '
  1191. );
  1192. }
  1193. public function testPercent()
  1194. {
  1195. $form = $this->factory->createNamed('percent', 'na&me', 0.1, array(
  1196. 'property_path' => 'name',
  1197. ));
  1198. $this->assertWidgetMatchesXpath($form->createView(), array(),
  1199. '/input
  1200. [@type="text"]
  1201. [@name="na&me"]
  1202. [@value="10"]
  1203. [contains(.., "%")]
  1204. '
  1205. );
  1206. }
  1207. public function testCheckedRadio()
  1208. {
  1209. $form = $this->factory->createNamed('radio', 'na&me', true, array(
  1210. 'property_path' => 'name',
  1211. ));
  1212. $this->assertWidgetMatchesXpath($form->createView(), array(),
  1213. '/input
  1214. [@type="radio"]
  1215. [@name="na&me"]
  1216. [@checked="checked"]
  1217. [@value=""]
  1218. '
  1219. );
  1220. }
  1221. public function testCheckedRadioWithValue()
  1222. {
  1223. $form = $this->factory->createNamed('radio', 'na&me', true, array(
  1224. 'property_path' => 'name',
  1225. 'value' => 'foo&bar',
  1226. ));
  1227. $this->assertWidgetMatchesXpath($form->createView(), array(),
  1228. '/input
  1229. [@type="radio"]
  1230. [@name="na&me"]
  1231. [@checked="checked"]
  1232. [@value="foo&bar"]
  1233. '
  1234. );
  1235. }
  1236. public function testUncheckedRadio()
  1237. {
  1238. $form = $this->factory->createNamed('radio', 'na&me', false, array(
  1239. 'property_path' => 'name',
  1240. ));
  1241. $this->assertWidgetMatchesXpath($form->createView(), array(),
  1242. '/input
  1243. [@type="radio"]
  1244. [@name="na&me"]
  1245. [not(@checked)]
  1246. '
  1247. );
  1248. }
  1249. public function testTextarea()
  1250. {
  1251. $form = $this->factory->createNamed('textarea', 'na&me', 'foo&bar', array(
  1252. 'property_path' => 'name',
  1253. ));
  1254. $this->assertWidgetMatchesXpath($form->createView(), array(),
  1255. '/textarea
  1256. [@name="na&me"]
  1257. [.="foo&bar"]
  1258. '
  1259. );
  1260. }
  1261. public function testText()
  1262. {
  1263. $form = $this->factory->createNamed('text', 'na&me', 'foo&bar', array(
  1264. 'property_path' => 'name',
  1265. ));
  1266. $this->assertWidgetMatchesXpath($form->createView(), array(),
  1267. '/input
  1268. [@type="text"]
  1269. [@name="na&me"]
  1270. [@value="foo&bar"]
  1271. [not(@maxlength)]
  1272. '
  1273. );
  1274. }
  1275. public function testTextWithMaxLength()
  1276. {
  1277. $form = $this->factory->createNamed('text', 'na&me', 'foo&bar', array(
  1278. 'property_path' => 'name',
  1279. 'max_length' => 123,
  1280. ));
  1281. $this->assertWidgetMatchesXpath($form->createView(), array(),
  1282. '/input
  1283. [@type="text"]
  1284. [@name="na&me"]
  1285. [@value="foo&bar"]
  1286. [@maxlength="123"]
  1287. '
  1288. );
  1289. }
  1290. public function testSearch()
  1291. {
  1292. $form = $this->factory->createNamed('search', 'na&me', 'foo&bar', array(
  1293. 'property_path' => 'name',
  1294. ));
  1295. $this->assertWidgetMatchesXpath($form->createView(), array(),
  1296. '/input
  1297. [@type="search"]
  1298. [@name="na&me"]
  1299. [@value="foo&bar"]
  1300. [not(@maxlength)]
  1301. '
  1302. );
  1303. }
  1304. public function testTime()
  1305. {
  1306. $form = $this->factory->createNamed('time', 'na&me', '04:05:06', array(
  1307. 'property_path' => 'name',
  1308. 'input' => 'string',
  1309. 'with_seconds' => false,
  1310. ));
  1311. $this->assertWidgetMatchesXpath($form->createView(), array(),
  1312. '/div
  1313. [
  1314. ./select
  1315. [@id="na&me_hour"]
  1316. [@size="1"]
  1317. [./option[@value="4"][@selected="selected"]]
  1318. /following-sibling::select
  1319. [@id="na&me_minute"]
  1320. [@size="1"]
  1321. [./option[@value="5"][@selected="selected"]]
  1322. ]
  1323. [count(./select)=2]
  1324. '
  1325. );
  1326. }
  1327. public function testTimeWithSeconds()
  1328. {
  1329. $form = $this->factory->createNamed('time', 'na&me', '04:05:06', array(
  1330. 'property_path' => 'name',
  1331. 'input' => 'string',
  1332. 'with_seconds' => true,
  1333. ));
  1334. $this->assertWidgetMatchesXpath($form->createView(), array(),
  1335. '/div
  1336. [
  1337. ./select
  1338. [@id="na&me_hour"]
  1339. [@size="1"]
  1340. [./option[@value="4"][@selected="selected"]]
  1341. [count(./option)>23]
  1342. /following-sibling::select
  1343. [@id="na&me_minute"]
  1344. [@size="1"]
  1345. [./option[@value="5"][@selected="selected"]]
  1346. [count(./option)>59]
  1347. /following-sibling::select
  1348. [@id="na&me_second"]
  1349. [@size="1"]
  1350. [./option[@value="6"][@selected="selected"]]
  1351. [count(./option)>59]
  1352. ]
  1353. [count(./select)=3]
  1354. '
  1355. );
  1356. }
  1357. public function testTimeSingleText()
  1358. {
  1359. $form = $this->factory->createNamed('time', 'na&me', '04:05:06', array(
  1360. 'property_path' => 'name',
  1361. 'input' => 'string',
  1362. 'widget' => 'single_text',
  1363. ));
  1364. $this->assertWidgetMatchesXpath($form->createView(), array(),
  1365. '/input
  1366. [@type="text"]
  1367. [@name="na&me"]
  1368. [@value="04:05:06"]
  1369. '
  1370. );
  1371. }
  1372. public function testTimeWithEmptyValueGlobal()
  1373. {
  1374. $form = $this->factory->createNamed('time', 'na&me', null, array(
  1375. 'property_path' => 'name',
  1376. 'input' => 'string',
  1377. 'empty_value' => 'Change&Me',
  1378. 'required' => false,
  1379. ));
  1380. $this->assertWidgetMatchesXpath($form->createView(), array(),
  1381. '/div
  1382. [
  1383. ./select
  1384. [@id="na&me_hour"]
  1385. [./option[@value=""][.="[trans]Change&Me[/trans]"]]
  1386. [count(./option)>24]
  1387. /following-sibling::select
  1388. [@id="na&me_minute"]
  1389. [./option[@value=""][.="[trans]Change&Me[/trans]"]]
  1390. [count(./option)>60]
  1391. ]
  1392. [count(./select)=2]
  1393. '
  1394. );
  1395. }
  1396. public function testTimeWithEmptyValueOnYear()
  1397. {
  1398. $form = $this->factory->createNamed('time', 'na&me', null, array(
  1399. 'property_path' => 'name',
  1400. 'input' => 'string',
  1401. 'required' => false,
  1402. 'empty_value' => array('hour' => 'Change&Me'),
  1403. ));
  1404. $this->assertWidgetMatchesXpath($form->createView(), array(),
  1405. '/div
  1406. [
  1407. ./select
  1408. [@id="na&me_hour"]
  1409. [./option[@value=""][.="[trans]Change&Me[/trans]"]]
  1410. [count(./option)>24]
  1411. /following-sibling::select
  1412. [@id="na&me_minute"]
  1413. [./option[@value="1"]]
  1414. [count(./option)>59]
  1415. ]
  1416. [count(./select)=2]
  1417. '
  1418. );
  1419. }
  1420. public function testTimezone()
  1421. {
  1422. $form = $this->factory->createNamed('timezone', 'na&me', 'Europe/Vienna', array(
  1423. 'property_path' => 'name',
  1424. ));
  1425. $this->assertWidgetMatchesXpath($form->createView(), array(),
  1426. '/select
  1427. [@name="na&me"]
  1428. [@required="required"]
  1429. [./optgroup
  1430. [@label="Europe"]
  1431. [./option[@value="Europe/Vienna"][@selected="selected"][.="Vienna"]]
  1432. ]
  1433. [count(./optgroup)>10]
  1434. [count(.//option)>200]
  1435. '
  1436. );
  1437. }
  1438. public function testTimezoneWithEmptyValue()
  1439. {
  1440. $form = $this->factory->createNamed('timezone', 'na&me', null, array(
  1441. 'property_path' => 'name',
  1442. 'empty_value' => 'Select&Timezone',
  1443. 'required' => false,
  1444. ));
  1445. $this->assertWidgetMatchesXpath($form->createView(), array(),
  1446. '/select
  1447. [./option[@value=""][.="[trans]Select&Timezone[/trans]"]]
  1448. [count(./optgroup)>10]
  1449. [count(.//option)>201]
  1450. '
  1451. );
  1452. }
  1453. public function testUrl()
  1454. {
  1455. $url = 'http://www.google.com?foo1=bar1&foo2=bar2';
  1456. $form = $this->factory->createNamed('url', 'na&me', $url, array(
  1457. 'property_path' => 'name',
  1458. ));
  1459. $this->assertWidgetMatchesXpath($form->createView(), array(),
  1460. '/input
  1461. [@type="url"]
  1462. [@name="na&me"]
  1463. [@value="http://www.google.com?foo1=bar1&foo2=bar2"]
  1464. '
  1465. );
  1466. }
  1467. public function testCollectionPrototype()
  1468. {
  1469. $form = $this->factory->createNamedBuilder('form', 'na&me', array('items' => array('one', 'two', 'three')))
  1470. ->add('items', 'collection', array('allow_add' => true))
  1471. ->getForm()
  1472. ->createView();
  1473. $html = $this->renderWidget($form);
  1474. $this->assertMatchesXpath($html,
  1475. '//script
  1476. [@id="na&me_items_prototype"]
  1477. [@type="text/html"]
  1478. '
  1479. );
  1480. }
  1481. }