CheckboxWidgetTest.php 7.7 KB

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