CheckboxWidgetTest.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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\CheckboxWidget;
  19. /**
  20. * Checkbox test case
  21. */
  22. class CheckboxWidgetTest extends TestCase
  23. {
  24. /**
  25. * setup method.
  26. *
  27. * @return void
  28. */
  29. public function setUp()
  30. {
  31. parent::setUp();
  32. $templates = [
  33. 'checkbox' => '<input type="checkbox" name="{{name}}" value="{{value}}"{{attrs}}>',
  34. ];
  35. $this->templates = new StringTemplate($templates);
  36. $this->context = $this->getMock('Cake\View\Form\ContextInterface');
  37. }
  38. /**
  39. * Test rendering simple checkboxes.
  40. *
  41. * @return void
  42. */
  43. public function testRenderSimple()
  44. {
  45. $checkbox = new CheckboxWidget($this->templates);
  46. $data = [
  47. 'name' => 'Comment[spam]',
  48. ];
  49. $result = $checkbox->render($data, $this->context);
  50. $expected = [
  51. 'input' => [
  52. 'type' => 'checkbox',
  53. 'name' => 'Comment[spam]',
  54. 'value' => 1,
  55. ]
  56. ];
  57. $this->assertHtml($expected, $result);
  58. $data = [
  59. 'name' => 'Comment[spam]',
  60. 'value' => 99,
  61. ];
  62. $result = $checkbox->render($data, $this->context);
  63. $expected = [
  64. 'input' => [
  65. 'type' => 'checkbox',
  66. 'name' => 'Comment[spam]',
  67. 'value' => 99,
  68. ]
  69. ];
  70. $this->assertHtml($expected, $result);
  71. }
  72. /**
  73. * Test rendering disabled checkboxes.
  74. *
  75. * @return void
  76. */
  77. public function testRenderDisabled()
  78. {
  79. $checkbox = new CheckboxWidget($this->templates);
  80. $data = [
  81. 'name' => 'Comment[spam]',
  82. 'disabled' => true,
  83. ];
  84. $result = $checkbox->render($data, $this->context);
  85. $expected = [
  86. 'input' => [
  87. 'type' => 'checkbox',
  88. 'name' => 'Comment[spam]',
  89. 'value' => 1,
  90. 'disabled' => 'disabled',
  91. ]
  92. ];
  93. $this->assertHtml($expected, $result);
  94. }
  95. /**
  96. * Test rendering checked checkboxes.
  97. *
  98. * @return void
  99. */
  100. public function testRenderChecked()
  101. {
  102. $checkbox = new CheckboxWidget($this->templates);
  103. $data = [
  104. 'name' => 'Comment[spam]',
  105. 'value' => 1,
  106. 'checked' => 1,
  107. ];
  108. $result = $checkbox->render($data, $this->context);
  109. $expected = [
  110. 'input' => [
  111. 'type' => 'checkbox',
  112. 'name' => 'Comment[spam]',
  113. 'value' => 1,
  114. 'checked' => 'checked',
  115. ]
  116. ];
  117. $this->assertHtml($expected, $result);
  118. $data = [
  119. 'name' => 'Comment[spam]',
  120. 'value' => 1,
  121. 'val' => 1,
  122. ];
  123. $result = $checkbox->render($data, $this->context);
  124. $this->assertHtml($expected, $result);
  125. $data['val'] = '1';
  126. $result = $checkbox->render($data, $this->context);
  127. $this->assertHtml($expected, $result);
  128. $data = [
  129. 'name' => 'Comment[spam]',
  130. 'value' => 1,
  131. 'val' => '1x',
  132. ];
  133. $result = $checkbox->render($data, $this->context);
  134. $expected = [
  135. 'input' => [
  136. 'type' => 'checkbox',
  137. 'name' => 'Comment[spam]',
  138. 'value' => 1,
  139. ]
  140. ];
  141. $this->assertHtml($expected, $result);
  142. }
  143. /**
  144. * Data provider for checkbox values
  145. *
  146. * @return array
  147. */
  148. public static function checkedProvider()
  149. {
  150. return [
  151. ['checked'],
  152. ['1'],
  153. [1],
  154. [true],
  155. ];
  156. }
  157. /**
  158. * Test rendering checked checkboxes with value.
  159. *
  160. * @dataProvider checkedProvider
  161. * @return void
  162. */
  163. public function testRenderCheckedValue($checked)
  164. {
  165. $checkbox = new CheckboxWidget($this->templates);
  166. $data = [
  167. 'name' => 'Comment[spam]',
  168. 'value' => 1,
  169. 'checked' => $checked,
  170. ];
  171. $result = $checkbox->render($data, $this->context);
  172. $expected = [
  173. 'input' => [
  174. 'type' => 'checkbox',
  175. 'name' => 'Comment[spam]',
  176. 'value' => 1,
  177. 'checked' => 'checked',
  178. ]
  179. ];
  180. $this->assertHtml($expected, $result);
  181. }
  182. /**
  183. * Data provider for checkbox values
  184. *
  185. * @return array
  186. */
  187. public static function uncheckedProvider()
  188. {
  189. return [
  190. [''],
  191. ['0'],
  192. [0],
  193. [false],
  194. [null],
  195. ];
  196. }
  197. /**
  198. * Test rendering unchecked checkboxes
  199. *
  200. * @dataProvider uncheckedProvider
  201. * @return void
  202. */
  203. public function testRenderUnCheckedValue($checked)
  204. {
  205. $checkbox = new CheckboxWidget($this->templates);
  206. $data = [
  207. 'name' => 'Comment[spam]',
  208. 'value' => 1,
  209. 'val' => 1,
  210. 'checked' => $checked,
  211. ];
  212. $result = $checkbox->render($data, $this->context);
  213. $expected = [
  214. 'input' => [
  215. 'type' => 'checkbox',
  216. 'name' => 'Comment[spam]',
  217. 'value' => 1,
  218. ]
  219. ];
  220. $this->assertHtml($expected, $result);
  221. }
  222. }