MultiCheckboxWidgetTest.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * For full copyright and license information, please see the LICENSE.txt
  8. * Redistributions of files must retain the above copyright notice.
  9. *
  10. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  11. * @link http://cakephp.org CakePHP(tm) Project
  12. * @since 3.0.0
  13. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Test\TestCase\View\Widget;
  16. use Cake\TestSuite\TestCase;
  17. use Cake\View\StringTemplate;
  18. use Cake\View\Widget\LabelWidget;
  19. use Cake\View\Widget\MultiCheckboxWidget;
  20. /**
  21. * MultiCheckbox test case.
  22. */
  23. class MultiCheckboxWidgetTest extends TestCase
  24. {
  25. /**
  26. * setup method.
  27. *
  28. * @return void
  29. */
  30. public function setUp()
  31. {
  32. parent::setUp();
  33. $templates = [
  34. 'checkbox' => '<input type="checkbox" name="{{name}}" value="{{value}}"{{attrs}}>',
  35. 'label' => '<label{{attrs}}>{{text}}</label>',
  36. 'checkboxWrapper' => '<div class="checkbox">{{input}}{{label}}</div>',
  37. ];
  38. $this->templates = new StringTemplate($templates);
  39. $this->context = $this->getMock('Cake\View\Form\ContextInterface');
  40. }
  41. /**
  42. * Test render simple option sets.
  43. *
  44. * @return void
  45. */
  46. public function testRenderSimple()
  47. {
  48. $label = new LabelWidget($this->templates);
  49. $input = new MultiCheckboxWidget($this->templates, $label);
  50. $data = [
  51. 'name' => 'Tags[id]',
  52. 'options' => [
  53. 1 => 'CakePHP',
  54. 2 => 'Development',
  55. ]
  56. ];
  57. $result = $input->render($data, $this->context);
  58. $expected = [
  59. ['div' => ['class' => 'checkbox']],
  60. ['input' => [
  61. 'type' => 'checkbox',
  62. 'name' => 'Tags[id][]',
  63. 'value' => 1,
  64. 'id' => 'tags-id-1',
  65. ]],
  66. ['label' => ['for' => 'tags-id-1']],
  67. 'CakePHP',
  68. '/label',
  69. '/div',
  70. ['div' => ['class' => 'checkbox']],
  71. ['input' => [
  72. 'type' => 'checkbox',
  73. 'name' => 'Tags[id][]',
  74. 'value' => 2,
  75. 'id' => 'tags-id-2',
  76. ]],
  77. ['label' => ['for' => 'tags-id-2']],
  78. 'Development',
  79. '/label',
  80. '/div',
  81. ];
  82. $this->assertHtml($expected, $result);
  83. }
  84. /**
  85. * Test render complex and additional attributes.
  86. *
  87. * @return void
  88. */
  89. public function testRenderComplex()
  90. {
  91. $label = new LabelWidget($this->templates);
  92. $input = new MultiCheckboxWidget($this->templates, $label);
  93. $data = [
  94. 'name' => 'Tags[id]',
  95. 'val' => 2,
  96. 'options' => [
  97. ['value' => '1', 'text' => 'CakePHP', 'data-test' => 'val'],
  98. ['value' => '2', 'text' => 'Development', 'class' => 'custom'],
  99. ]
  100. ];
  101. $result = $input->render($data, $this->context);
  102. $expected = [
  103. ['div' => ['class' => 'checkbox']],
  104. ['input' => [
  105. 'type' => 'checkbox',
  106. 'name' => 'Tags[id][]',
  107. 'value' => 1,
  108. 'id' => 'tags-id-1',
  109. 'data-test' => 'val',
  110. ]],
  111. ['label' => ['for' => 'tags-id-1']],
  112. 'CakePHP',
  113. '/label',
  114. '/div',
  115. ['div' => ['class' => 'checkbox']],
  116. ['input' => [
  117. 'type' => 'checkbox',
  118. 'checked' => 'checked',
  119. 'name' => 'Tags[id][]',
  120. 'value' => 2,
  121. 'id' => 'tags-id-2',
  122. 'class' => 'custom',
  123. ]],
  124. ['label' => ['class' => 'selected', 'for' => 'tags-id-2']],
  125. 'Development',
  126. '/label',
  127. '/div',
  128. ];
  129. $this->assertHtml($expected, $result);
  130. }
  131. /**
  132. * Test render escpaing options.
  133. *
  134. * @return void
  135. */
  136. public function testRenderEscaping()
  137. {
  138. $label = new LabelWidget($this->templates);
  139. $input = new MultiCheckboxWidget($this->templates, $label);
  140. $data = [
  141. 'name' => 'Tags[id]',
  142. 'options' => [
  143. '>' => '>>',
  144. ]
  145. ];
  146. $result = $input->render($data, $this->context);
  147. $expected = [
  148. ['div' => ['class' => 'checkbox']],
  149. ['input' => [
  150. 'type' => 'checkbox',
  151. 'name' => 'Tags[id][]',
  152. 'value' => '&gt;',
  153. 'id' => 'tags-id',
  154. ]],
  155. ['label' => ['for' => 'tags-id']],
  156. '&gt;&gt;',
  157. '/label',
  158. '/div',
  159. ];
  160. $this->assertHtml($expected, $result);
  161. }
  162. /**
  163. * Test render selected checkboxes.
  164. *
  165. * @return void
  166. */
  167. public function testRenderSelected()
  168. {
  169. $label = new LabelWidget($this->templates);
  170. $input = new MultiCheckboxWidget($this->templates, $label);
  171. $data = [
  172. 'name' => 'Tags[id]',
  173. 'options' => [
  174. 1 => 'CakePHP',
  175. '1x' => 'Development',
  176. ],
  177. 'val' => [1],
  178. 'disabled' => false
  179. ];
  180. $result = $input->render($data, $this->context);
  181. $expected = [
  182. ['div' => ['class' => 'checkbox']],
  183. ['input' => [
  184. 'type' => 'checkbox',
  185. 'name' => 'Tags[id][]',
  186. 'value' => 1,
  187. 'id' => 'tags-id-1',
  188. 'checked' => 'checked'
  189. ]],
  190. ['label' => ['class' => 'selected', 'for' => 'tags-id-1']],
  191. 'CakePHP',
  192. '/label',
  193. '/div',
  194. ['div' => ['class' => 'checkbox']],
  195. ['input' => [
  196. 'type' => 'checkbox',
  197. 'name' => 'Tags[id][]',
  198. 'value' => '1x',
  199. 'id' => 'tags-id-1x',
  200. ]],
  201. ['label' => ['for' => 'tags-id-1x']],
  202. 'Development',
  203. '/label',
  204. '/div',
  205. ];
  206. $this->assertHtml($expected, $result);
  207. $data['val'] = 1;
  208. $result = $input->render($data, $this->context);
  209. $this->assertHtml($expected, $result);
  210. $data['val'] = '1';
  211. $result = $input->render($data, $this->context);
  212. $this->assertHtml($expected, $result);
  213. }
  214. /**
  215. * Test render disabled checkboxes.
  216. *
  217. * @return void
  218. */
  219. public function testRenderDisabled()
  220. {
  221. $label = new LabelWidget($this->templates);
  222. $input = new MultiCheckboxWidget($this->templates, $label);
  223. $data = [
  224. 'name' => 'Tags[id]',
  225. 'options' => [
  226. 1 => 'CakePHP',
  227. '1x' => 'Development',
  228. ],
  229. 'disabled' => true,
  230. ];
  231. $result = $input->render($data, $this->context);
  232. $expected = [
  233. ['div' => ['class' => 'checkbox']],
  234. ['input' => [
  235. 'type' => 'checkbox',
  236. 'name' => 'Tags[id][]',
  237. 'value' => 1,
  238. 'id' => 'tags-id-1',
  239. 'disabled' => 'disabled'
  240. ]],
  241. ['label' => ['for' => 'tags-id-1']],
  242. 'CakePHP',
  243. '/label',
  244. '/div',
  245. ['div' => ['class' => 'checkbox']],
  246. ['input' => [
  247. 'type' => 'checkbox',
  248. 'name' => 'Tags[id][]',
  249. 'value' => '1x',
  250. 'id' => 'tags-id-1x',
  251. 'disabled' => 'disabled'
  252. ]],
  253. ['label' => ['for' => 'tags-id-1x']],
  254. 'Development',
  255. '/label',
  256. '/div',
  257. ];
  258. $this->assertHtml($expected, $result);
  259. $data['disabled'] = 'a string';
  260. $result = $input->render($data, $this->context);
  261. $this->assertHtml($expected, $result);
  262. $data['disabled'] = ['1', '1x'];
  263. $this->assertHtml($expected, $result);
  264. $data = [
  265. 'name' => 'Tags[id]',
  266. 'options' => [
  267. 1 => 'CakePHP',
  268. '1x' => 'Development',
  269. ],
  270. 'disabled' => [1]
  271. ];
  272. $result = $input->render($data, $this->context);
  273. $expected = [
  274. ['div' => ['class' => 'checkbox']],
  275. ['input' => [
  276. 'type' => 'checkbox',
  277. 'name' => 'Tags[id][]',
  278. 'value' => 1,
  279. 'id' => 'tags-id-1',
  280. 'disabled' => 'disabled'
  281. ]],
  282. ['label' => ['for' => 'tags-id-1']],
  283. 'CakePHP',
  284. '/label',
  285. '/div',
  286. ['div' => ['class' => 'checkbox']],
  287. ['input' => [
  288. 'type' => 'checkbox',
  289. 'name' => 'Tags[id][]',
  290. 'value' => '1x',
  291. 'id' => 'tags-id-1x',
  292. ]],
  293. ['label' => ['for' => 'tags-id-1x']],
  294. 'Development',
  295. '/label',
  296. '/div',
  297. ];
  298. $this->assertHtml($expected, $result);
  299. }
  300. }