AbstractLayoutTest.php 48 KB

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