AbstractDivLayoutTest.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  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 testForm()
  250. {
  251. $form = $this->factory->createNamedBuilder('form', 'name')
  252. ->add('firstName', 'text')
  253. ->add('lastName', 'text')
  254. ->getForm();
  255. $this->assertWidgetMatchesXpath($form->createView(), array(),
  256. '/div
  257. [
  258. ./input[@type="hidden"][@id="name__token"]
  259. /following-sibling::div
  260. [
  261. ./label[@for="name_firstName"]
  262. /following-sibling::input[@type="text"][@id="name_firstName"]
  263. ]
  264. /following-sibling::div
  265. [
  266. ./label[@for="name_lastName"]
  267. /following-sibling::input[@type="text"][@id="name_lastName"]
  268. ]
  269. ]
  270. [count(.//input)=3]
  271. '
  272. );
  273. }
  274. public function testRepeated()
  275. {
  276. $form = $this->factory->createNamed('repeated', 'name', 'foobar', array(
  277. 'type' => 'text',
  278. ));
  279. $this->assertWidgetMatchesXpath($form->createView(), array(),
  280. '/div
  281. [
  282. ./div
  283. [
  284. ./label[@for="name_first"]
  285. /following-sibling::input[@type="text"][@id="name_first"]
  286. ]
  287. /following-sibling::div
  288. [
  289. ./label[@for="name_second"]
  290. /following-sibling::input[@type="text"][@id="name_second"]
  291. ]
  292. ]
  293. [count(.//input)=2]
  294. '
  295. );
  296. }
  297. }