MultiCheckboxTest.php 5.0 KB

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