AbstractDivLayoutTest.php 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  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. abstract class AbstractDivLayoutTest extends AbstractLayoutTest
  13. {
  14. public function testRow()
  15. {
  16. $form = $this->factory->createNamed('text', 'name');
  17. $form->addError(new FormError('Error!'));
  18. $view = $form->createView();
  19. $html = $this->renderRow($view);
  20. $this->assertMatchesXpath($html,
  21. '/div
  22. [
  23. ./label[@for="name"]
  24. /following-sibling::ul
  25. [./li[.="[trans]Error![/trans]"]]
  26. [count(./li)=1]
  27. /following-sibling::input[@id="name"]
  28. ]
  29. '
  30. );
  31. }
  32. public function testRowForwardsVariables()
  33. {
  34. $view = $this->factory->createNamed('text', 'name')->createView();
  35. $html = $this->renderRow($view, array('label' => 'foo&bar'));
  36. $this->assertMatchesXpath($html,
  37. '/div
  38. [
  39. ./label[@for="name"][.="[trans]foo&bar[/trans]"]
  40. /following-sibling::input[@id="name"]
  41. ]
  42. '
  43. );
  44. }
  45. public function testRepeatedRow()
  46. {
  47. $form = $this->factory->createNamed('repeated', 'name');
  48. $form->addError(new FormError('Error!'));
  49. $view = $form->createView();
  50. $html = $this->renderRow($view);
  51. $this->assertMatchesXpath($html,
  52. '/ul
  53. [./li[.="[trans]Error![/trans]"]]
  54. [count(./li)=1]
  55. /following-sibling::div
  56. [
  57. ./label[@for="name_first"]
  58. /following-sibling::input[@id="name_first"]
  59. ]
  60. /following-sibling::div
  61. [
  62. ./label[@for="name_second"]
  63. /following-sibling::input[@id="name_second"]
  64. ]
  65. '
  66. );
  67. }
  68. public function testRest()
  69. {
  70. $view = $this->factory->createNamedBuilder('form', 'name')
  71. ->add('field1', 'text')
  72. ->add('field2', 'repeated')
  73. ->add('field3', 'text')
  74. ->add('field4', 'text')
  75. ->getForm()
  76. ->createView();
  77. // Render field2 row -> does not implicitely call renderWidget because
  78. // it is a repeated field!
  79. $this->renderRow($view['field2']);
  80. // Render field3 widget
  81. $this->renderWidget($view['field3']);
  82. // Rest should only contain field1 and field4
  83. $html = $this->renderRest($view);
  84. $this->assertMatchesXpath($html,
  85. '/input
  86. [@type="hidden"]
  87. [@id="name__token"]
  88. /following-sibling::div
  89. [
  90. ./label[@for="name_field1"]
  91. /following-sibling::input[@type="text"][@id="name_field1"]
  92. ]
  93. /following-sibling::div
  94. [
  95. ./label[@for="name_field4"]
  96. /following-sibling::input[@type="text"][@id="name_field4"]
  97. ]
  98. [count(../div)=2]
  99. [count(..//label)=2]
  100. [count(..//input)=3]
  101. '
  102. );
  103. }
  104. public function testRestWithChildrenForms()
  105. {
  106. $child1 = $this->factory->createNamedBuilder('form', 'child1')
  107. ->add('field1', 'text')
  108. ->add('field2', 'text')
  109. ->getForm();
  110. $child2 = $this->factory->createNamedBuilder('form', 'child2')
  111. ->add('field1', 'text')
  112. ->add('field2', 'text')
  113. ->getForm();
  114. $view = $this->factory->createNamedBuilder('form', 'parent')
  115. ->getForm()
  116. ->add($child1)
  117. ->add($child2)
  118. ->createView();
  119. // Render child1.field1 row
  120. $this->renderRow($view['child1']['field1']);
  121. // Render child2.field2 widget (remember that widget don't render label)
  122. $this->renderWidget($view['child2']['field2']);
  123. // Rest should only contain child1.field2 and child2.field1
  124. $html = $this->renderRest($view);
  125. $this->assertMatchesXpath($html,
  126. '/input
  127. [@type="hidden"]
  128. [@id="parent__token"]
  129. /following-sibling::div
  130. [
  131. ./label[@for="parent_child1"]
  132. /following-sibling::div
  133. [@id="parent_child1"]
  134. [
  135. ./div
  136. [
  137. ./label[@for="parent_child1_field2"]
  138. /following-sibling::input[@id="parent_child1_field2"]
  139. ]
  140. ]
  141. ]
  142. /following-sibling::div
  143. [
  144. ./label[@for="parent_child2"]
  145. /following-sibling::div
  146. [@id="parent_child2"]
  147. [
  148. ./div
  149. [
  150. ./label[@for="parent_child2_field1"]
  151. /following-sibling::input[@id="parent_child2_field1"]
  152. ]
  153. /following-sibling::div
  154. [
  155. ./label[@for="parent_child2_field2"]
  156. ]
  157. ]
  158. ]
  159. '
  160. );
  161. }
  162. public function testRestAndRepeatedWithRow()
  163. {
  164. $view = $this->factory->createNamedBuilder('form', 'name')
  165. ->add('first', 'text')
  166. ->add('password', 'repeated')
  167. ->getForm()
  168. ->createView();
  169. $this->renderRow($view['password']);
  170. $html = $this->renderRest($view);
  171. $this->assertMatchesXpath($html,
  172. '/input
  173. [@type="hidden"]
  174. [@id="name__token"]
  175. /following-sibling::div
  176. [
  177. ./label[@for="name_first"]
  178. /following-sibling::input[@type="text"][@id="name_first"]
  179. ]
  180. [count(.//input)=1]
  181. '
  182. );
  183. }
  184. public function testRestAndRepeatedWithRowPerField()
  185. {
  186. $view = $this->factory->createNamedBuilder('form', 'name')
  187. ->add('first', 'text')
  188. ->add('password', 'repeated')
  189. ->getForm()
  190. ->createView();
  191. $this->renderRow($view['password']['first']);
  192. $this->renderRow($view['password']['second']);
  193. $html = $this->renderRest($view);
  194. $this->assertMatchesXpath($html,
  195. '/input
  196. [@type="hidden"]
  197. [@id="name__token"]
  198. /following-sibling::div
  199. [
  200. ./label[@for="name_first"]
  201. /following-sibling::input[@type="text"][@id="name_first"]
  202. ]
  203. [count(.//input)=1]
  204. [count(.//label)=1]
  205. '
  206. );
  207. }
  208. public function testRestAndRepeatedWithWidgetPerField()
  209. {
  210. $view = $this->factory->createNamedBuilder('form', 'name')
  211. ->add('first', 'text')
  212. ->add('password', 'repeated')
  213. ->getForm()
  214. ->createView();
  215. // The password form is considered as rendered as all its childrend
  216. // are rendered
  217. $this->renderWidget($view['password']['first']);
  218. $this->renderWidget($view['password']['second']);
  219. $html = $this->renderRest($view);
  220. $this->assertMatchesXpath($html,
  221. '/input
  222. [@type="hidden"]
  223. [@id="name__token"]
  224. /following-sibling::div
  225. [
  226. ./label[@for="name_first"]
  227. /following-sibling::input[@type="text"][@id="name_first"]
  228. ]
  229. [count(//input)=2]
  230. [count(//label)=1]
  231. '
  232. );
  233. }
  234. public function testCollection()
  235. {
  236. $form = $this->factory->createNamed('collection', 'name', array('a', 'b'), array(
  237. 'type' => 'text',
  238. ));
  239. $this->assertWidgetMatchesXpath($form->createView(), array(),
  240. '/div
  241. [
  242. ./div[./input[@type="text"][@value="a"]]
  243. /following-sibling::div[./input[@type="text"][@value="b"]]
  244. ]
  245. [count(./div[./input])=2]
  246. '
  247. );
  248. }
  249. public function testCollectionRow()
  250. {
  251. $collection = $this->factory->createNamedBuilder(
  252. 'collection',
  253. 'collection',
  254. array('a', 'b'),
  255. array('type' => 'text')
  256. );
  257. $form = $this->factory->createNamedBuilder('form', 'form')
  258. ->add($collection)
  259. ->getForm();
  260. $this->assertWidgetMatchesXpath($form->createView(), array(),
  261. '/div
  262. [
  263. ./input[@type="hidden"][@id="form__token"]
  264. /following-sibling::div
  265. [
  266. ./label[not(@for)]
  267. /following-sibling::div
  268. [
  269. ./div
  270. [
  271. ./label[@for="form_collection_0"]
  272. /following-sibling::input[@type="text"][@value="a"]
  273. ]
  274. /following-sibling::div
  275. [
  276. ./label[@for="form_collection_1"]
  277. /following-sibling::input[@type="text"][@value="b"]
  278. ]
  279. ]
  280. ]
  281. ]
  282. [count(.//input)=3]
  283. '
  284. );
  285. }
  286. public function testForm()
  287. {
  288. $form = $this->factory->createNamedBuilder('form', 'name')
  289. ->add('firstName', 'text')
  290. ->add('lastName', 'text')
  291. ->getForm();
  292. $this->assertWidgetMatchesXpath($form->createView(), array(),
  293. '/div
  294. [
  295. ./input[@type="hidden"][@id="name__token"]
  296. /following-sibling::div
  297. [
  298. ./label[@for="name_firstName"]
  299. /following-sibling::input[@type="text"][@id="name_firstName"]
  300. ]
  301. /following-sibling::div
  302. [
  303. ./label[@for="name_lastName"]
  304. /following-sibling::input[@type="text"][@id="name_lastName"]
  305. ]
  306. ]
  307. [count(.//input)=3]
  308. '
  309. );
  310. }
  311. public function testRepeated()
  312. {
  313. $form = $this->factory->createNamed('repeated', 'name', 'foobar', array(
  314. 'type' => 'text',
  315. ));
  316. $this->assertWidgetMatchesXpath($form->createView(), array(),
  317. '/div
  318. [
  319. ./div
  320. [
  321. ./label[@for="name_first"]
  322. /following-sibling::input[@type="text"][@id="name_first"]
  323. ]
  324. /following-sibling::div
  325. [
  326. ./label[@for="name_second"]
  327. /following-sibling::input[@type="text"][@id="name_second"]
  328. ]
  329. ]
  330. [count(.//input)=2]
  331. '
  332. );
  333. }
  334. }