AbstractLayoutTest.php 48 KB

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