MultiCheckboxTest.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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. public function setUp() {
  24. parent::setUp();
  25. $templates = [
  26. 'checkbox' => '<input type="checkbox" name="{{name}}" value="{{value}}"{{attrs}}>',
  27. 'label' => '<label{{attrs}}>{{text}}</label>',
  28. 'checkboxContainer' => '<div class="checkbox">{{input}}{{label}}</div>',
  29. ];
  30. $this->templates = new StringTemplate();
  31. $this->templates->add($templates);
  32. }
  33. /**
  34. * Test render simple option sets.
  35. *
  36. * @return void
  37. */
  38. public function testRenderSimple() {
  39. $input = new MultiCheckbox($this->templates);
  40. $data = [
  41. 'name' => 'Tags[id]',
  42. 'options' => [
  43. 1 => 'CakePHP',
  44. 2 => 'Development',
  45. ]
  46. ];
  47. $result = $input->render($data);
  48. $expected = [
  49. ['div' => ['class' => 'checkbox']],
  50. ['input' => [
  51. 'type' => 'checkbox',
  52. 'name' => 'Tags[id][]',
  53. 'value' => 1,
  54. 'id' => 'tags-id-1',
  55. ]],
  56. ['label' => ['for' => 'tags-id-1']],
  57. 'CakePHP',
  58. '/label',
  59. '/div',
  60. ['div' => ['class' => 'checkbox']],
  61. ['input' => [
  62. 'type' => 'checkbox',
  63. 'name' => 'Tags[id][]',
  64. 'value' => 2,
  65. 'id' => 'tags-id-2',
  66. ]],
  67. ['label' => ['for' => 'tags-id-2']],
  68. 'Development',
  69. '/label',
  70. '/div',
  71. ];
  72. $this->assertTags($result, $expected);
  73. }
  74. /**
  75. * Test render escpaing options.
  76. *
  77. * @return void
  78. */
  79. public function testRenderEscaping() {
  80. $input = new MultiCheckbox($this->templates);
  81. $data = [
  82. 'name' => 'Tags[id]',
  83. 'options' => [
  84. '>' => '>>',
  85. ]
  86. ];
  87. $result = $input->render($data);
  88. $expected = [
  89. ['div' => ['class' => 'checkbox']],
  90. ['input' => [
  91. 'type' => 'checkbox',
  92. 'name' => 'Tags[id][]',
  93. 'value' => '&gt;',
  94. 'id' => 'tags-id',
  95. ]],
  96. ['label' => ['for' => 'tags-id']],
  97. '&gt;&gt;',
  98. '/label',
  99. '/div',
  100. ];
  101. $this->assertTags($result, $expected);
  102. }
  103. /**
  104. * Test render selected checkboxes.
  105. *
  106. * @return void
  107. */
  108. public function testRenderSelected() {
  109. $input = new MultiCheckbox($this->templates);
  110. $data = [
  111. 'name' => 'Tags[id]',
  112. 'options' => [
  113. 1 => 'CakePHP',
  114. '1x' => 'Development',
  115. ],
  116. 'val' => [1],
  117. 'disabled' => false
  118. ];
  119. $result = $input->render($data);
  120. $expected = [
  121. ['div' => ['class' => 'checkbox']],
  122. ['input' => [
  123. 'type' => 'checkbox',
  124. 'name' => 'Tags[id][]',
  125. 'value' => 1,
  126. 'id' => 'tags-id-1',
  127. 'checked' => 'checked'
  128. ]],
  129. ['label' => ['for' => 'tags-id-1']],
  130. 'CakePHP',
  131. '/label',
  132. '/div',
  133. ['div' => ['class' => 'checkbox']],
  134. ['input' => [
  135. 'type' => 'checkbox',
  136. 'name' => 'Tags[id][]',
  137. 'value' => '1x',
  138. 'id' => 'tags-id-1x',
  139. ]],
  140. ['label' => ['for' => 'tags-id-1x']],
  141. 'Development',
  142. '/label',
  143. '/div',
  144. ];
  145. $this->assertTags($result, $expected);
  146. $data['val'] = 1;
  147. $result = $input->render($data);
  148. $this->assertTags($result, $expected);
  149. $data['val'] = '1';
  150. $result = $input->render($data);
  151. $this->assertTags($result, $expected);
  152. }
  153. /**
  154. * Test render disabled checkboxes.
  155. *
  156. * @return void
  157. */
  158. public function testRenderDisabled() {
  159. $input = new MultiCheckbox($this->templates);
  160. $data = [
  161. 'name' => 'Tags[id]',
  162. 'options' => [
  163. 1 => 'CakePHP',
  164. '1x' => 'Development',
  165. ],
  166. 'disabled' => true,
  167. ];
  168. $result = $input->render($data);
  169. $expected = [
  170. ['div' => ['class' => 'checkbox']],
  171. ['input' => [
  172. 'type' => 'checkbox',
  173. 'name' => 'Tags[id][]',
  174. 'value' => 1,
  175. 'id' => 'tags-id-1',
  176. 'disabled' => 'disabled'
  177. ]],
  178. ['label' => ['for' => 'tags-id-1']],
  179. 'CakePHP',
  180. '/label',
  181. '/div',
  182. ['div' => ['class' => 'checkbox']],
  183. ['input' => [
  184. 'type' => 'checkbox',
  185. 'name' => 'Tags[id][]',
  186. 'value' => '1x',
  187. 'id' => 'tags-id-1x',
  188. 'disabled' => 'disabled'
  189. ]],
  190. ['label' => ['for' => 'tags-id-1x']],
  191. 'Development',
  192. '/label',
  193. '/div',
  194. ];
  195. $this->assertTags($result, $expected);
  196. $data = [
  197. 'name' => 'Tags[id]',
  198. 'options' => [
  199. 1 => 'CakePHP',
  200. '1x' => 'Development',
  201. ],
  202. 'disabled' => [1]
  203. ];
  204. $result = $input->render($data);
  205. $expected = [
  206. ['div' => ['class' => 'checkbox']],
  207. ['input' => [
  208. 'type' => 'checkbox',
  209. 'name' => 'Tags[id][]',
  210. 'value' => 1,
  211. 'id' => 'tags-id-1',
  212. 'disabled' => 'disabled'
  213. ]],
  214. ['label' => ['for' => 'tags-id-1']],
  215. 'CakePHP',
  216. '/label',
  217. '/div',
  218. ['div' => ['class' => 'checkbox']],
  219. ['input' => [
  220. 'type' => 'checkbox',
  221. 'name' => 'Tags[id][]',
  222. 'value' => '1x',
  223. 'id' => 'tags-id-1x',
  224. ]],
  225. ['label' => ['for' => 'tags-id-1x']],
  226. 'Development',
  227. '/label',
  228. '/div',
  229. ];
  230. $this->assertTags($result, $expected);
  231. }
  232. }