DateTimeWidgetTest.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  5. * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  6. *
  7. * Licensed under The MIT License
  8. * For full copyright and license information, please see the LICENSE.txt
  9. * Redistributions of files must retain the above copyright notice.
  10. *
  11. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  12. * @link https://cakephp.org CakePHP(tm) Project
  13. * @since 3.0.0
  14. * @license https://opensource.org/licenses/mit-license.php MIT License
  15. */
  16. namespace Cake\Test\TestCase\View\Widget;
  17. use Cake\TestSuite\TestCase;
  18. use Cake\View\Form\NullContext;
  19. use Cake\View\StringTemplate;
  20. use Cake\View\Widget\DateTimeWidget;
  21. use DateTime;
  22. use PHPUnit\Framework\Attributes\DataProvider;
  23. /**
  24. * DateTimeWidget test case
  25. */
  26. class DateTimeWidgetTest extends TestCase
  27. {
  28. /**
  29. * @var \Cake\View\Form\NullContext
  30. */
  31. protected $context;
  32. /**
  33. * @var \Cake\View\StringTemplate
  34. */
  35. protected $templates;
  36. /**
  37. * @var \Cake\View\Widget\DateTimeWidget
  38. */
  39. protected $DateTime;
  40. public function setUp(): void
  41. {
  42. parent::setUp();
  43. $templates = [
  44. 'input' => '<input type="{{type}}" name="{{name}}"{{attrs}}>',
  45. ];
  46. $this->templates = new StringTemplate($templates);
  47. $this->context = new NullContext([]);
  48. $this->DateTime = new DateTimeWidget($this->templates);
  49. }
  50. /**
  51. * Data provider for testing various acceptable selected values.
  52. *
  53. * @return array
  54. */
  55. public static function selectedValuesProvider(): array
  56. {
  57. $date = new DateTime('2014-01-20 12:30:45');
  58. return [
  59. 'DateTime' => [$date],
  60. 'string' => [$date->format('Y-m-d H:i:s')],
  61. 'int string' => [$date->format('U')],
  62. 'int' => [$date->getTimestamp()],
  63. ];
  64. }
  65. /**
  66. * test rendering selected values.
  67. *
  68. * @param mixed $selected
  69. */
  70. #[DataProvider('selectedValuesProvider')]
  71. public function testRenderValid($selected): void
  72. {
  73. $result = $this->DateTime->render(['val' => $selected], $this->context);
  74. $expected = [
  75. 'input' => [
  76. 'type' => 'datetime-local',
  77. 'name' => '',
  78. 'value' => '2014-01-20T12:30:45',
  79. 'step' => '1',
  80. ],
  81. ];
  82. $this->assertHtml($expected, $result);
  83. }
  84. /**
  85. * testTimezoneOption
  86. */
  87. public function testTimezoneOption(): void
  88. {
  89. $result = $this->DateTime->render([
  90. 'val' => '2019-02-03 10:00:00',
  91. 'timezone' => 'Asia/Kolkata',
  92. ], $this->context);
  93. $expected = [
  94. 'input' => [
  95. 'type' => 'datetime-local',
  96. 'name' => '',
  97. 'value' => '2019-02-03T15:30:00',
  98. 'step' => '1',
  99. ],
  100. ];
  101. $this->assertHtml($expected, $result);
  102. }
  103. public function testUnsettingStep(): void
  104. {
  105. $result = $this->DateTime->render([
  106. 'val' => '2019-02-03 10:11:12',
  107. 'step' => null,
  108. ], $this->context);
  109. $expected = [
  110. 'input' => [
  111. 'type' => 'datetime-local',
  112. 'name' => '',
  113. 'value' => '2019-02-03T10:11:12',
  114. ],
  115. ];
  116. $this->assertHtml($expected, $result);
  117. $result = $this->DateTime->render([
  118. 'val' => '2019-02-03 10:11:12',
  119. 'step' => false,
  120. ], $this->context);
  121. $expected = [
  122. 'input' => [
  123. 'type' => 'datetime-local',
  124. 'name' => '',
  125. 'value' => '2019-02-03T10:11:12',
  126. ],
  127. ];
  128. $this->assertHtml($expected, $result);
  129. }
  130. public function testDatetimeFormat(): void
  131. {
  132. $result = $this->DateTime->render([
  133. 'val' => '2019-02-03 10:11:12',
  134. 'format' => 'Y-m-d\TH:i',
  135. ], $this->context);
  136. $expected = [
  137. 'input' => [
  138. 'type' => 'datetime-local',
  139. 'name' => '',
  140. 'value' => '2019-02-03T10:11',
  141. ],
  142. ];
  143. $this->assertHtml($expected, $result);
  144. $result = $this->DateTime->render([
  145. 'val' => '2019-02-03 10:11:12',
  146. 'format' => 'Y-m-d\TH:i',
  147. 'step' => 120,
  148. ], $this->context);
  149. $expected = [
  150. 'input' => [
  151. 'type' => 'datetime-local',
  152. 'name' => '',
  153. 'step' => '120',
  154. 'value' => '2019-02-03T10:11',
  155. ],
  156. ];
  157. $this->assertHtml($expected, $result);
  158. $result = $this->DateTime->render([
  159. 'type' => 'time',
  160. 'val' => '10:11:12',
  161. 'format' => 'H:i',
  162. ], $this->context);
  163. $expected = [
  164. 'input' => [
  165. 'type' => 'time',
  166. 'name' => '',
  167. 'value' => '10:11',
  168. ],
  169. ];
  170. $this->assertHtml($expected, $result);
  171. $result = $this->DateTime->render([
  172. 'type' => 'time',
  173. 'val' => '10:11:12',
  174. 'format' => 'H:i',
  175. 'step' => 120,
  176. ], $this->context);
  177. $expected = [
  178. 'input' => [
  179. 'type' => 'time',
  180. 'name' => '',
  181. 'step' => '120',
  182. 'value' => '10:11',
  183. ],
  184. ];
  185. $this->assertHtml($expected, $result);
  186. }
  187. /**
  188. * Test rendering with templateVars
  189. */
  190. public function testRenderTemplateVars(): void
  191. {
  192. $templates = [
  193. 'input' => '<input type="{{type}}" name="{{name}}"{{attrs}}><span>{{help}}</span>',
  194. ];
  195. $this->templates->add($templates);
  196. $result = $this->DateTime->render([
  197. 'name' => 'date',
  198. 'templateVars' => ['help' => 'some help'],
  199. ], $this->context);
  200. $this->assertStringContainsString('<span>some help</span>', $result);
  201. }
  202. /**
  203. * testRenderInvalidTypeException
  204. */
  205. public function testRenderInvalidTypeException(): void
  206. {
  207. $this->expectException('InvalidArgumentException');
  208. $this->expectExceptionMessage('Invalid type `foo` for input tag, expected datetime-local, date, time, month or week');
  209. $this->DateTime->render(['type' => 'foo', 'val' => new DateTime()], $this->context);
  210. }
  211. /**
  212. * Test that secureFields omits removed selects
  213. */
  214. public function testSecureFields(): void
  215. {
  216. $data = [
  217. 'name' => 'date',
  218. ];
  219. $result = $this->DateTime->secureFields($data);
  220. $expected = [
  221. 'date',
  222. ];
  223. $this->assertEquals($expected, $result);
  224. }
  225. }