CheckboxWidgetTest.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  5. * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  6. *
  7. * Licensed under The MIT License
  8. * For full copyright and license information, please see the LICENSE.txt
  9. * Redistributions of files must retain the above copyright notice.
  10. *
  11. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  12. * @link https://cakephp.org CakePHP(tm) Project
  13. * @since 3.0.0
  14. * @license https://opensource.org/licenses/mit-license.php MIT License
  15. */
  16. namespace Cake\Test\TestCase\View\Widget;
  17. use Cake\TestSuite\TestCase;
  18. use Cake\View\Form\NullContext;
  19. use Cake\View\StringTemplate;
  20. use Cake\View\Widget\CheckboxWidget;
  21. /**
  22. * Checkbox test case
  23. */
  24. class CheckboxWidgetTest extends TestCase
  25. {
  26. /**
  27. * @var \Cake\View\Form\NullContext
  28. */
  29. protected $context;
  30. /**
  31. * @var \Cake\View\StringTemplate
  32. */
  33. protected $templates;
  34. /**
  35. * setup method.
  36. */
  37. public function setUp(): void
  38. {
  39. parent::setUp();
  40. $templates = [
  41. 'checkbox' => '<input type="checkbox" name="{{name}}" value="{{value}}"{{attrs}}>',
  42. ];
  43. $this->templates = new StringTemplate($templates);
  44. $this->context = new NullContext([]);
  45. }
  46. /**
  47. * Test rendering simple checkboxes.
  48. */
  49. public function testRenderSimple(): void
  50. {
  51. $checkbox = new CheckboxWidget($this->templates);
  52. $data = [
  53. 'name' => 'Comment[spam]',
  54. ];
  55. $result = $checkbox->render($data, $this->context);
  56. $expected = [
  57. 'input' => [
  58. 'type' => 'checkbox',
  59. 'name' => 'Comment[spam]',
  60. 'value' => 1,
  61. ],
  62. ];
  63. $this->assertHtml($expected, $result);
  64. $data = [
  65. 'name' => 'Comment[spam]',
  66. 'value' => 99,
  67. ];
  68. $result = $checkbox->render($data, $this->context);
  69. $expected = [
  70. 'input' => [
  71. 'type' => 'checkbox',
  72. 'name' => 'Comment[spam]',
  73. 'value' => 99,
  74. ],
  75. ];
  76. $this->assertHtml($expected, $result);
  77. }
  78. /**
  79. * Test rendering disabled checkboxes.
  80. */
  81. public function testRenderDisabled(): void
  82. {
  83. $checkbox = new CheckboxWidget($this->templates);
  84. $data = [
  85. 'name' => 'Comment[spam]',
  86. 'disabled' => true,
  87. ];
  88. $result = $checkbox->render($data, $this->context);
  89. $expected = [
  90. 'input' => [
  91. 'type' => 'checkbox',
  92. 'name' => 'Comment[spam]',
  93. 'value' => 1,
  94. 'disabled' => 'disabled',
  95. ],
  96. ];
  97. $this->assertHtml($expected, $result);
  98. }
  99. /**
  100. * Test rendering checked checkboxes.
  101. */
  102. public function testRenderChecked(): void
  103. {
  104. $checkbox = new CheckboxWidget($this->templates);
  105. $data = [
  106. 'name' => 'Comment[spam]',
  107. 'value' => 1,
  108. 'checked' => 1,
  109. ];
  110. $result = $checkbox->render($data, $this->context);
  111. $expected = [
  112. 'input' => [
  113. 'type' => 'checkbox',
  114. 'name' => 'Comment[spam]',
  115. 'value' => 1,
  116. 'checked' => 'checked',
  117. ],
  118. ];
  119. $this->assertHtml($expected, $result);
  120. $data = [
  121. 'name' => 'Comment[spam]',
  122. 'value' => 1,
  123. 'val' => 1,
  124. ];
  125. $result = $checkbox->render($data, $this->context);
  126. $this->assertHtml($expected, $result);
  127. $data['val'] = '1';
  128. $result = $checkbox->render($data, $this->context);
  129. $this->assertHtml($expected, $result);
  130. $data = [
  131. 'name' => 'Comment[spam]',
  132. 'value' => 1,
  133. 'val' => '1x',
  134. ];
  135. $result = $checkbox->render($data, $this->context);
  136. $expected = [
  137. 'input' => [
  138. 'type' => 'checkbox',
  139. 'name' => 'Comment[spam]',
  140. 'value' => 1,
  141. ],
  142. ];
  143. $this->assertHtml($expected, $result);
  144. }
  145. /**
  146. * Data provider for checkbox values
  147. *
  148. * @return array
  149. */
  150. public static function checkedProvider(): array
  151. {
  152. return [
  153. ['checked'],
  154. ['1'],
  155. [1],
  156. [true],
  157. ];
  158. }
  159. /**
  160. * Test rendering checked checkboxes with value.
  161. *
  162. * @dataProvider checkedProvider
  163. * @param mixed $checked
  164. */
  165. public function testRenderCheckedValue($checked): void
  166. {
  167. $checkbox = new CheckboxWidget($this->templates);
  168. $data = [
  169. 'name' => 'Comment[spam]',
  170. 'value' => 1,
  171. 'checked' => $checked,
  172. ];
  173. $result = $checkbox->render($data, $this->context);
  174. $expected = [
  175. 'input' => [
  176. 'type' => 'checkbox',
  177. 'name' => 'Comment[spam]',
  178. 'value' => 1,
  179. 'checked' => 'checked',
  180. ],
  181. ];
  182. $this->assertHtml($expected, $result);
  183. }
  184. /**
  185. * Data provider for checkbox values
  186. *
  187. * @return array
  188. */
  189. public static function uncheckedProvider(): array
  190. {
  191. return [
  192. [''],
  193. ['0'],
  194. [0],
  195. [false],
  196. [null],
  197. ];
  198. }
  199. /**
  200. * Test rendering unchecked checkboxes
  201. *
  202. * @dataProvider uncheckedProvider
  203. * @param mixed $checked
  204. */
  205. public function testRenderUnCheckedValue($checked): void
  206. {
  207. $checkbox = new CheckboxWidget($this->templates);
  208. $data = [
  209. 'name' => 'Comment[spam]',
  210. 'value' => 1,
  211. 'val' => 1,
  212. 'checked' => $checked,
  213. ];
  214. $result = $checkbox->render($data, $this->context);
  215. $expected = [
  216. 'input' => [
  217. 'type' => 'checkbox',
  218. 'name' => 'Comment[spam]',
  219. 'value' => 1,
  220. ],
  221. ];
  222. $this->assertHtml($expected, $result);
  223. }
  224. /**
  225. * Ensure templateVars option is hooked up.
  226. */
  227. public function testRenderTemplateVars(): void
  228. {
  229. $this->templates->add([
  230. 'checkbox' => '<input type="checkbox" custom="{{custom}}" name="{{name}}" value="{{value}}"{{attrs}}>',
  231. ]);
  232. $checkbox = new CheckboxWidget($this->templates);
  233. $data = [
  234. 'templateVars' => ['custom' => 'value'],
  235. 'name' => 'Comment[spam]',
  236. 'value' => 1,
  237. ];
  238. $result = $checkbox->render($data, $this->context);
  239. $expected = [
  240. 'input' => [
  241. 'type' => 'checkbox',
  242. 'custom' => 'value',
  243. 'name' => 'Comment[spam]',
  244. 'value' => 1,
  245. ],
  246. ];
  247. $this->assertHtml($expected, $result);
  248. }
  249. /**
  250. * testRenderCustomAttributes method
  251. *
  252. * Test render with custom attributes.
  253. */
  254. public function testRenderCustomAttributes(): void
  255. {
  256. $checkbox = new CheckboxWidget($this->templates);
  257. $result = $checkbox->render([
  258. 'name' => 'Model[field]',
  259. 'class' => 'my-class',
  260. 'data-ref' => 'custom-attr',
  261. 'value' => 1,
  262. ], $this->context);
  263. $expected = [
  264. 'input' => [
  265. 'type' => 'checkbox',
  266. 'name' => 'Model[field]',
  267. 'value' => '1',
  268. 'class' => 'my-class',
  269. 'data-ref' => 'custom-attr',
  270. ],
  271. ];
  272. $this->assertHtml($expected, $result);
  273. }
  274. }