MultiCheckboxTest.php 6.3 KB

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