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