MultiCheckboxTest.php 5.4 KB

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