MultiCheckboxTest.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. ];
  105. $result = $input->render($data);
  106. $expected = [
  107. ['div' => ['class' => 'checkbox']],
  108. ['input' => [
  109. 'type' => 'checkbox',
  110. 'name' => 'Tags[id][]',
  111. 'value' => 1,
  112. 'id' => 'tags-id-1',
  113. 'checked' => 'checked'
  114. ]],
  115. ['label' => ['for' => 'tags-id-1']],
  116. 'CakePHP',
  117. '/label',
  118. '/div',
  119. ['div' => ['class' => 'checkbox']],
  120. ['input' => [
  121. 'type' => 'checkbox',
  122. 'name' => 'Tags[id][]',
  123. 'value' => '1x',
  124. 'id' => 'tags-id-1x',
  125. ]],
  126. ['label' => ['for' => 'tags-id-1x']],
  127. 'Development',
  128. '/label',
  129. '/div',
  130. ];
  131. $this->assertTags($result, $expected);
  132. $data['val'] = 1;
  133. $result = $input->render($data);
  134. $this->assertTags($result, $expected);
  135. $data['val'] = '1';
  136. $result = $input->render($data);
  137. $this->assertTags($result, $expected);
  138. }
  139. /**
  140. * Test render disabled checkboxes.
  141. *
  142. * @return void
  143. */
  144. public function testRenderDisabled() {
  145. $this->markTestIncomplete();
  146. }
  147. }