MultiCheckboxWidgetTest.php 9.3 KB

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