AbstractLayoutTest.php 46 KB

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