MultiCheckboxTest.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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 escpaing options.
  81. *
  82. * @return void
  83. */
  84. public function testRenderEscaping() {
  85. $input = new MultiCheckbox($this->templates);
  86. $data = [
  87. 'name' => 'Tags[id]',
  88. 'options' => [
  89. '>' => '>>',
  90. ]
  91. ];
  92. $result = $input->render($data);
  93. $expected = [
  94. ['div' => ['class' => 'checkbox']],
  95. ['input' => [
  96. 'type' => 'checkbox',
  97. 'name' => 'Tags[id][]',
  98. 'value' => '&gt;',
  99. 'id' => 'tags-id',
  100. ]],
  101. ['label' => ['for' => 'tags-id']],
  102. '&gt;&gt;',
  103. '/label',
  104. '/div',
  105. ];
  106. $this->assertTags($result, $expected);
  107. }
  108. /**
  109. * Test render selected checkboxes.
  110. *
  111. * @return void
  112. */
  113. public function testRenderSelected() {
  114. $input = new MultiCheckbox($this->templates);
  115. $data = [
  116. 'name' => 'Tags[id]',
  117. 'options' => [
  118. 1 => 'CakePHP',
  119. '1x' => 'Development',
  120. ],
  121. 'val' => [1],
  122. 'disabled' => false
  123. ];
  124. $result = $input->render($data);
  125. $expected = [
  126. ['div' => ['class' => 'checkbox']],
  127. ['input' => [
  128. 'type' => 'checkbox',
  129. 'name' => 'Tags[id][]',
  130. 'value' => 1,
  131. 'id' => 'tags-id-1',
  132. 'checked' => 'checked'
  133. ]],
  134. ['label' => ['for' => 'tags-id-1']],
  135. 'CakePHP',
  136. '/label',
  137. '/div',
  138. ['div' => ['class' => 'checkbox']],
  139. ['input' => [
  140. 'type' => 'checkbox',
  141. 'name' => 'Tags[id][]',
  142. 'value' => '1x',
  143. 'id' => 'tags-id-1x',
  144. ]],
  145. ['label' => ['for' => 'tags-id-1x']],
  146. 'Development',
  147. '/label',
  148. '/div',
  149. ];
  150. $this->assertTags($result, $expected);
  151. $data['val'] = 1;
  152. $result = $input->render($data);
  153. $this->assertTags($result, $expected);
  154. $data['val'] = '1';
  155. $result = $input->render($data);
  156. $this->assertTags($result, $expected);
  157. }
  158. /**
  159. * Test render disabled checkboxes.
  160. *
  161. * @return void
  162. */
  163. public function testRenderDisabled() {
  164. $input = new MultiCheckbox($this->templates);
  165. $data = [
  166. 'name' => 'Tags[id]',
  167. 'options' => [
  168. 1 => 'CakePHP',
  169. '1x' => 'Development',
  170. ],
  171. 'disabled' => true,
  172. ];
  173. $result = $input->render($data);
  174. $expected = [
  175. ['div' => ['class' => 'checkbox']],
  176. ['input' => [
  177. 'type' => 'checkbox',
  178. 'name' => 'Tags[id][]',
  179. 'value' => 1,
  180. 'id' => 'tags-id-1',
  181. 'disabled' => 'disabled'
  182. ]],
  183. ['label' => ['for' => 'tags-id-1']],
  184. 'CakePHP',
  185. '/label',
  186. '/div',
  187. ['div' => ['class' => 'checkbox']],
  188. ['input' => [
  189. 'type' => 'checkbox',
  190. 'name' => 'Tags[id][]',
  191. 'value' => '1x',
  192. 'id' => 'tags-id-1x',
  193. 'disabled' => 'disabled'
  194. ]],
  195. ['label' => ['for' => 'tags-id-1x']],
  196. 'Development',
  197. '/label',
  198. '/div',
  199. ];
  200. $this->assertTags($result, $expected);
  201. $data['disabled'] = ['1', '1x'];
  202. $this->assertTags($result, $expected);
  203. $data = [
  204. 'name' => 'Tags[id]',
  205. 'options' => [
  206. 1 => 'CakePHP',
  207. '1x' => 'Development',
  208. ],
  209. 'disabled' => [1]
  210. ];
  211. $result = $input->render($data);
  212. $expected = [
  213. ['div' => ['class' => 'checkbox']],
  214. ['input' => [
  215. 'type' => 'checkbox',
  216. 'name' => 'Tags[id][]',
  217. 'value' => 1,
  218. 'id' => 'tags-id-1',
  219. 'disabled' => 'disabled'
  220. ]],
  221. ['label' => ['for' => 'tags-id-1']],
  222. 'CakePHP',
  223. '/label',
  224. '/div',
  225. ['div' => ['class' => 'checkbox']],
  226. ['input' => [
  227. 'type' => 'checkbox',
  228. 'name' => 'Tags[id][]',
  229. 'value' => '1x',
  230. 'id' => 'tags-id-1x',
  231. ]],
  232. ['label' => ['for' => 'tags-id-1x']],
  233. 'Development',
  234. '/label',
  235. '/div',
  236. ];
  237. $this->assertTags($result, $expected);
  238. }
  239. }