CheckboxTest.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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 CakePHP(tm) v3.0
  13. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Test\TestCase\View\Input;
  16. use Cake\TestSuite\TestCase;
  17. use Cake\View\Input\Checkbox;
  18. use Cake\View\StringTemplate;
  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();
  34. $this->templates->add($templates);
  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);
  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);
  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);
  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);
  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);
  119. $this->assertTags($result, $expected);
  120. $data['val'] = '1';
  121. $result = $checkbox->render($data);
  122. $this->assertTags($result, $expected);
  123. $data = [
  124. 'name' => 'Comment[spam]',
  125. 'value' => 1,
  126. 'val' => '1x',
  127. ];
  128. $result = $checkbox->render($data);
  129. $expected = [
  130. 'input' => [
  131. 'type' => 'checkbox',
  132. 'name' => 'Comment[spam]',
  133. 'value' => 1,
  134. ]
  135. ];
  136. $this->assertTags($result, $expected);
  137. }
  138. /**
  139. * Test rendering checked checkboxes with value.
  140. *
  141. * @return void
  142. */
  143. public function testRenderCheckedValue() {
  144. $checkbox = new Checkbox($this->templates);
  145. $data = [
  146. 'name' => 'Comment[spam]',
  147. 'value' => 1,
  148. 'checked' => 1,
  149. ];
  150. $result = $checkbox->render($data);
  151. $expected = [
  152. 'input' => [
  153. 'type' => 'checkbox',
  154. 'name' => 'Comment[spam]',
  155. 'value' => 1,
  156. 'checked' => 'checked',
  157. ]
  158. ];
  159. $this->assertTags($result, $expected);
  160. }
  161. }