MultiCheckboxTest.php 6.7 KB

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