MultiCheckboxTest.php 6.9 KB

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