CheckboxTest.php 4.7 KB

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