MultiCheckboxWidgetTest.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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. 'options' => [
  96. ['value' => '1', 'text' => 'CakePHP', 'data-test' => 'val'],
  97. ['value' => '2', 'text' => 'Development', 'class' => 'custom'],
  98. ]
  99. ];
  100. $result = $input->render($data, $this->context);
  101. $expected = [
  102. ['div' => ['class' => 'checkbox']],
  103. ['input' => [
  104. 'type' => 'checkbox',
  105. 'name' => 'Tags[id][]',
  106. 'value' => 1,
  107. 'id' => 'tags-id-1',
  108. 'data-test' => 'val',
  109. ]],
  110. ['label' => ['for' => 'tags-id-1']],
  111. 'CakePHP',
  112. '/label',
  113. '/div',
  114. ['div' => ['class' => 'checkbox']],
  115. ['input' => [
  116. 'type' => 'checkbox',
  117. 'name' => 'Tags[id][]',
  118. 'value' => 2,
  119. 'id' => 'tags-id-2',
  120. 'class' => 'custom',
  121. ]],
  122. ['label' => ['for' => 'tags-id-2']],
  123. 'Development',
  124. '/label',
  125. '/div',
  126. ];
  127. $this->assertHtml($expected, $result);
  128. }
  129. /**
  130. * Test render escpaing options.
  131. *
  132. * @return void
  133. */
  134. public function testRenderEscaping()
  135. {
  136. $label = new LabelWidget($this->templates);
  137. $input = new MultiCheckboxWidget($this->templates, $label);
  138. $data = [
  139. 'name' => 'Tags[id]',
  140. 'options' => [
  141. '>' => '>>',
  142. ]
  143. ];
  144. $result = $input->render($data, $this->context);
  145. $expected = [
  146. ['div' => ['class' => 'checkbox']],
  147. ['input' => [
  148. 'type' => 'checkbox',
  149. 'name' => 'Tags[id][]',
  150. 'value' => '&gt;',
  151. 'id' => 'tags-id',
  152. ]],
  153. ['label' => ['for' => 'tags-id']],
  154. '&gt;&gt;',
  155. '/label',
  156. '/div',
  157. ];
  158. $this->assertHtml($expected, $result);
  159. }
  160. /**
  161. * Test render selected checkboxes.
  162. *
  163. * @return void
  164. */
  165. public function testRenderSelected()
  166. {
  167. $label = new LabelWidget($this->templates);
  168. $input = new MultiCheckboxWidget($this->templates, $label);
  169. $data = [
  170. 'name' => 'Tags[id]',
  171. 'options' => [
  172. 1 => 'CakePHP',
  173. '1x' => 'Development',
  174. ],
  175. 'val' => [1],
  176. 'disabled' => false
  177. ];
  178. $result = $input->render($data, $this->context);
  179. $expected = [
  180. ['div' => ['class' => 'checkbox']],
  181. ['input' => [
  182. 'type' => 'checkbox',
  183. 'name' => 'Tags[id][]',
  184. 'value' => 1,
  185. 'id' => 'tags-id-1',
  186. 'checked' => 'checked'
  187. ]],
  188. ['label' => ['class' => 'selected', 'for' => 'tags-id-1']],
  189. 'CakePHP',
  190. '/label',
  191. '/div',
  192. ['div' => ['class' => 'checkbox']],
  193. ['input' => [
  194. 'type' => 'checkbox',
  195. 'name' => 'Tags[id][]',
  196. 'value' => '1x',
  197. 'id' => 'tags-id-1x',
  198. ]],
  199. ['label' => ['for' => 'tags-id-1x']],
  200. 'Development',
  201. '/label',
  202. '/div',
  203. ];
  204. $this->assertHtml($expected, $result);
  205. $data['val'] = 1;
  206. $result = $input->render($data, $this->context);
  207. $this->assertHtml($expected, $result);
  208. $data['val'] = '1';
  209. $result = $input->render($data, $this->context);
  210. $this->assertHtml($expected, $result);
  211. }
  212. /**
  213. * Test render disabled checkboxes.
  214. *
  215. * @return void
  216. */
  217. public function testRenderDisabled()
  218. {
  219. $label = new LabelWidget($this->templates);
  220. $input = new MultiCheckboxWidget($this->templates, $label);
  221. $data = [
  222. 'name' => 'Tags[id]',
  223. 'options' => [
  224. 1 => 'CakePHP',
  225. '1x' => 'Development',
  226. ],
  227. 'disabled' => true,
  228. ];
  229. $result = $input->render($data, $this->context);
  230. $expected = [
  231. ['div' => ['class' => 'checkbox']],
  232. ['input' => [
  233. 'type' => 'checkbox',
  234. 'name' => 'Tags[id][]',
  235. 'value' => 1,
  236. 'id' => 'tags-id-1',
  237. 'disabled' => 'disabled'
  238. ]],
  239. ['label' => ['for' => 'tags-id-1']],
  240. 'CakePHP',
  241. '/label',
  242. '/div',
  243. ['div' => ['class' => 'checkbox']],
  244. ['input' => [
  245. 'type' => 'checkbox',
  246. 'name' => 'Tags[id][]',
  247. 'value' => '1x',
  248. 'id' => 'tags-id-1x',
  249. 'disabled' => 'disabled'
  250. ]],
  251. ['label' => ['for' => 'tags-id-1x']],
  252. 'Development',
  253. '/label',
  254. '/div',
  255. ];
  256. $this->assertHtml($expected, $result);
  257. $data['disabled'] = 'a string';
  258. $result = $input->render($data, $this->context);
  259. $this->assertHtml($expected, $result);
  260. $data['disabled'] = ['1', '1x'];
  261. $this->assertHtml($expected, $result);
  262. $data = [
  263. 'name' => 'Tags[id]',
  264. 'options' => [
  265. 1 => 'CakePHP',
  266. '1x' => 'Development',
  267. ],
  268. 'disabled' => [1]
  269. ];
  270. $result = $input->render($data, $this->context);
  271. $expected = [
  272. ['div' => ['class' => 'checkbox']],
  273. ['input' => [
  274. 'type' => 'checkbox',
  275. 'name' => 'Tags[id][]',
  276. 'value' => 1,
  277. 'id' => 'tags-id-1',
  278. 'disabled' => 'disabled'
  279. ]],
  280. ['label' => ['for' => 'tags-id-1']],
  281. 'CakePHP',
  282. '/label',
  283. '/div',
  284. ['div' => ['class' => 'checkbox']],
  285. ['input' => [
  286. 'type' => 'checkbox',
  287. 'name' => 'Tags[id][]',
  288. 'value' => '1x',
  289. 'id' => 'tags-id-1x',
  290. ]],
  291. ['label' => ['for' => 'tags-id-1x']],
  292. 'Development',
  293. '/label',
  294. '/div',
  295. ];
  296. $this->assertHtml($expected, $result);
  297. }
  298. }