MultiCheckboxTest.php 6.3 KB

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