CheckboxTest.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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($templates);
  34. }
  35. /**
  36. * Test rendering simple checkboxes.
  37. *
  38. * @return void
  39. */
  40. public function testRenderSimple() {
  41. $checkbox = new Checkbox($this->templates);
  42. $data = [
  43. 'name' => 'Comment[spam]',
  44. ];
  45. $result = $checkbox->render($data);
  46. $expected = [
  47. 'input' => [
  48. 'type' => 'checkbox',
  49. 'name' => 'Comment[spam]',
  50. 'value' => 1,
  51. ]
  52. ];
  53. $this->assertTags($result, $expected);
  54. $data = [
  55. 'name' => 'Comment[spam]',
  56. 'value' => 99,
  57. ];
  58. $result = $checkbox->render($data);
  59. $expected = [
  60. 'input' => [
  61. 'type' => 'checkbox',
  62. 'name' => 'Comment[spam]',
  63. 'value' => 99,
  64. ]
  65. ];
  66. $this->assertTags($result, $expected);
  67. }
  68. /**
  69. * Test rendering disabled checkboxes.
  70. *
  71. * @return void
  72. */
  73. public function testRenderDisabled() {
  74. $checkbox = new Checkbox($this->templates);
  75. $data = [
  76. 'name' => 'Comment[spam]',
  77. 'disabled' => true,
  78. ];
  79. $result = $checkbox->render($data);
  80. $expected = [
  81. 'input' => [
  82. 'type' => 'checkbox',
  83. 'name' => 'Comment[spam]',
  84. 'value' => 1,
  85. 'disabled' => 'disabled',
  86. ]
  87. ];
  88. $this->assertTags($result, $expected);
  89. }
  90. /**
  91. * Test rendering checked checkboxes.
  92. *
  93. * @return void
  94. */
  95. public function testRenderChecked() {
  96. $checkbox = new Checkbox($this->templates);
  97. $data = [
  98. 'name' => 'Comment[spam]',
  99. 'value' => 1,
  100. 'checked' => 1,
  101. ];
  102. $result = $checkbox->render($data);
  103. $expected = [
  104. 'input' => [
  105. 'type' => 'checkbox',
  106. 'name' => 'Comment[spam]',
  107. 'value' => 1,
  108. 'checked' => 'checked',
  109. ]
  110. ];
  111. $this->assertTags($result, $expected);
  112. $data = [
  113. 'name' => 'Comment[spam]',
  114. 'value' => 1,
  115. 'val' => 1,
  116. ];
  117. $result = $checkbox->render($data);
  118. $this->assertTags($result, $expected);
  119. $data['val'] = '1';
  120. $result = $checkbox->render($data);
  121. $this->assertTags($result, $expected);
  122. $data = [
  123. 'name' => 'Comment[spam]',
  124. 'value' => 1,
  125. 'val' => '1x',
  126. ];
  127. $result = $checkbox->render($data);
  128. $expected = [
  129. 'input' => [
  130. 'type' => 'checkbox',
  131. 'name' => 'Comment[spam]',
  132. 'value' => 1,
  133. ]
  134. ];
  135. $this->assertTags($result, $expected);
  136. }
  137. /**
  138. * Test rendering checked checkboxes with value.
  139. *
  140. * @return void
  141. */
  142. public function testRenderCheckedValue() {
  143. $checkbox = new Checkbox($this->templates);
  144. $data = [
  145. 'name' => 'Comment[spam]',
  146. 'value' => 1,
  147. 'checked' => 1,
  148. ];
  149. $result = $checkbox->render($data);
  150. $expected = [
  151. 'input' => [
  152. 'type' => 'checkbox',
  153. 'name' => 'Comment[spam]',
  154. 'value' => 1,
  155. 'checked' => 'checked',
  156. ]
  157. ];
  158. $this->assertTags($result, $expected);
  159. }
  160. }