DateTimeTest.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845
  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\Widget;
  16. use Cake\TestSuite\TestCase;
  17. use Cake\View\StringTemplate;
  18. use Cake\View\Widget\DateTime;
  19. use Cake\View\Widget\SelectBox;
  20. /**
  21. * DateTime input test case
  22. */
  23. class DateTimeTest extends TestCase {
  24. /**
  25. * @setUp
  26. *
  27. * @return void
  28. */
  29. public function setUp() {
  30. parent::setUp();
  31. $templates = [
  32. 'select' => '<select name="{{name}}"{{attrs}}>{{content}}</select>',
  33. 'option' => '<option value="{{value}}"{{attrs}}>{{text}}</option>',
  34. 'optgroup' => '<optgroup label="{{label}}"{{attrs}}>{{content}}</optgroup>',
  35. 'dateWidget' => '{{year}}{{month}}{{day}}{{hour}}{{minute}}{{second}}{{meridian}}'
  36. ];
  37. $this->templates = new StringTemplate($templates);
  38. $this->selectBox = new SelectBox($this->templates);
  39. $this->DateTime = new DateTime($this->templates, $this->selectBox);
  40. }
  41. /**
  42. * Data provider for testing various types of invalid selected values.
  43. *
  44. * @return array
  45. */
  46. public static function invalidSelectedValuesProvider() {
  47. $date = new \DateTime('2014-01-20 12:30:45');
  48. return [
  49. 'string' => ['Bag of poop'],
  50. 'int' => [-1],
  51. 'array' => [[
  52. 'derp' => 'hurt'
  53. ]]
  54. ];
  55. }
  56. /**
  57. * test rendering selected values.
  58. *
  59. * @dataProvider selectedValuesProvider
  60. * @return void
  61. */
  62. public function testRenderSelectedInvalid($selected) {
  63. $result = $this->DateTime->render(['val' => $selected]);
  64. $now = new \DateTime();
  65. $format = '<option value="%s" selected="selected">%s</option>';
  66. $this->assertContains(
  67. sprintf($format, $now->format('Y'), $now->format('Y')),
  68. $result
  69. );
  70. }
  71. /**
  72. * Data provider for testing various acceptable selected values.
  73. *
  74. * @return array
  75. */
  76. public static function selectedValuesProvider() {
  77. $date = new \DateTime('2014-01-20 12:30:45');
  78. return [
  79. 'DateTime' => [$date],
  80. 'string' => [$date->format('Y-m-d H:i:s')],
  81. 'int' => [$date->getTimestamp()],
  82. 'array' => [[
  83. 'year' => '2014', 'month' => '01', 'day' => '20',
  84. 'hour' => '12', 'minute' => '30', 'second' => '45',
  85. ]]
  86. ];
  87. }
  88. /**
  89. * test rendering selected values.
  90. *
  91. * @dataProvider selectedValuesProvider
  92. * @return void
  93. */
  94. public function testRenderSelected($selected) {
  95. $result = $this->DateTime->render(['val' => $selected]);
  96. $this->assertContains('<option value="2014" selected="selected">2014</option>', $result);
  97. $this->assertContains('<option value="01" selected="selected">1</option>', $result);
  98. $this->assertContains('<option value="20" selected="selected">20</option>', $result);
  99. $this->assertContains('<option value="12" selected="selected">12</option>', $result);
  100. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  101. $this->assertContains('<option value="45" selected="selected">45</option>', $result);
  102. }
  103. /**
  104. * Test rendering widgets with empty values.
  105. *
  106. * @retun void
  107. */
  108. public function testRenderEmptyValues() {
  109. $result = $this->DateTime->render([
  110. 'year' => ['empty' => 'YEAR'],
  111. 'month' => ['empty' => 'MONTH'],
  112. 'day' => ['empty' => 'DAY'],
  113. 'hour' => ['empty' => 'HOUR'],
  114. 'minute' => ['empty' => 'MINUTE'],
  115. 'second' => ['empty' => 'SECOND'],
  116. 'meridian' => ['empty' => 'MERIDIAN'],
  117. ]);
  118. $this->assertContains('<option value="" selected="selected">YEAR</option>', $result);
  119. $this->assertContains('<option value="" selected="selected">MONTH</option>', $result);
  120. $this->assertContains('<option value="" selected="selected">DAY</option>', $result);
  121. $this->assertContains('<option value="" selected="selected">HOUR</option>', $result);
  122. $this->assertContains('<option value="" selected="selected">MINUTE</option>', $result);
  123. $this->assertContains('<option value="" selected="selected">SECOND</option>', $result);
  124. $this->assertContains('<option value="" selected="selected">MERIDIAN</option>', $result);
  125. }
  126. /**
  127. * Test rendering the default year widget.
  128. *
  129. * @return void
  130. */
  131. public function testRenderYearWidgetDefaultRange() {
  132. $now = new \DateTime();
  133. $result = $this->DateTime->render([
  134. 'month' => false,
  135. 'day' => false,
  136. 'hour' => false,
  137. 'minute' => false,
  138. 'second' => false,
  139. 'val' => $now,
  140. ]);
  141. $year = $now->format('Y');
  142. $format = '<option value="%s" selected="selected">%s</option>';
  143. $this->assertContains(sprintf($format, $year, $year), $result);
  144. $format = '<option value="%s">%s</option>';
  145. $maxYear = $now->format('Y') + 5;
  146. $minYear = $now->format('Y') - 5;
  147. $this->assertContains(sprintf($format, $maxYear, $maxYear), $result);
  148. $this->assertContains(sprintf($format, $minYear, $minYear), $result);
  149. $nope = $now->format('Y') + 6;
  150. $this->assertNotContains(sprintf($format, $nope, $nope), $result);
  151. $nope = $now->format('Y') - 6;
  152. $this->assertNotContains(sprintf($format, $nope, $nope), $result);
  153. }
  154. /**
  155. * Test ordering of year options.
  156. *
  157. * @return void
  158. */
  159. public function testRenderYearWidgetOrdering() {
  160. $now = new \DateTime('2014-01-01 12:00:00');
  161. $result = $this->DateTime->render([
  162. 'name' => 'date',
  163. 'year' => [
  164. 'start' => 2013,
  165. 'end' => 2015,
  166. 'data-foo' => 'test',
  167. 'order' => 'desc',
  168. ],
  169. 'month' => false,
  170. 'day' => false,
  171. 'hour' => false,
  172. 'minute' => false,
  173. 'second' => false,
  174. 'val' => $now,
  175. 'orderYear' => 'asc',
  176. ]);
  177. $expected = [
  178. 'select' => ['name' => 'date[year]', 'data-foo' => 'test'],
  179. ['option' => ['value' => '2013']], '2013', '/option',
  180. ['option' => ['value' => '2014', 'selected' => 'selected']], '2014', '/option',
  181. ['option' => ['value' => '2015']], '2015', '/option',
  182. '/select',
  183. ];
  184. $this->assertTags($result, $expected);
  185. $result = $this->DateTime->render([
  186. 'name' => 'date',
  187. 'year' => [
  188. 'start' => 2013,
  189. 'end' => 2015,
  190. 'order' => 'asc'
  191. ],
  192. 'month' => false,
  193. 'day' => false,
  194. 'hour' => false,
  195. 'minute' => false,
  196. 'second' => false,
  197. 'val' => $now,
  198. ]);
  199. $expected = [
  200. 'select' => ['name' => 'date[year]'],
  201. ['option' => ['value' => '2015']], '2015', '/option',
  202. ['option' => ['value' => '2014', 'selected' => 'selected']], '2014', '/option',
  203. ['option' => ['value' => '2013']], '2013', '/option',
  204. '/select',
  205. ];
  206. $this->assertTags($result, $expected);
  207. }
  208. /**
  209. * Test that a selected value outside of the chosen
  210. * year boundary is also included as an option.
  211. *
  212. * @return void
  213. */
  214. public function testRenderYearWidgetValueOutOfBounds() {
  215. $now = new \DateTime('2010-01-01 12:00:00');
  216. $result = $this->DateTime->render([
  217. 'name' => 'date',
  218. 'year' => [
  219. 'start' => 2013,
  220. 'end' => 2015,
  221. ],
  222. 'month' => false,
  223. 'day' => false,
  224. 'hour' => false,
  225. 'minute' => false,
  226. 'second' => false,
  227. 'val' => $now,
  228. ]);
  229. $expected = [
  230. 'select' => ['name' => 'date[year]'],
  231. ['option' => ['value' => '2010', 'selected' => 'selected']], '2010', '/option',
  232. ['option' => ['value' => '2011']], '2011', '/option',
  233. ['option' => ['value' => '2012']], '2012', '/option',
  234. ['option' => ['value' => '2013']], '2013', '/option',
  235. ['option' => ['value' => '2014']], '2014', '/option',
  236. ['option' => ['value' => '2015']], '2015', '/option',
  237. '/select',
  238. ];
  239. $this->assertTags($result, $expected);
  240. $now = new \DateTime('2013-01-01 12:00:00');
  241. $result = $this->DateTime->render([
  242. 'name' => 'date',
  243. 'year' => [
  244. 'start' => 2010,
  245. 'end' => 2011,
  246. ],
  247. 'month' => false,
  248. 'day' => false,
  249. 'hour' => false,
  250. 'minute' => false,
  251. 'second' => false,
  252. 'val' => $now,
  253. ]);
  254. $expected = [
  255. 'select' => ['name' => 'date[year]'],
  256. ['option' => ['value' => '2010']], '2010', '/option',
  257. ['option' => ['value' => '2011']], '2011', '/option',
  258. ['option' => ['value' => '2012']], '2012', '/option',
  259. ['option' => ['value' => '2013', 'selected' => 'selected']], '2013', '/option',
  260. '/select',
  261. ];
  262. $this->assertTags($result, $expected);
  263. }
  264. /**
  265. * Test rendering the month widget
  266. *
  267. * @return void
  268. */
  269. public function testRenderMonthWidget() {
  270. $now = new \DateTime('2010-09-01 12:00:00');
  271. $result = $this->DateTime->render([
  272. 'name' => 'date',
  273. 'year' => false,
  274. 'day' => false,
  275. 'hour' => false,
  276. 'minute' => false,
  277. 'second' => false,
  278. 'val' => $now,
  279. ]);
  280. $expected = [
  281. 'select' => ['name' => 'date[month]'],
  282. ['option' => ['value' => '01']], '1', '/option',
  283. ['option' => ['value' => '02']], '2', '/option',
  284. ['option' => ['value' => '03']], '3', '/option',
  285. ['option' => ['value' => '04']], '4', '/option',
  286. ['option' => ['value' => '05']], '5', '/option',
  287. ['option' => ['value' => '06']], '6', '/option',
  288. ['option' => ['value' => '07']], '7', '/option',
  289. ['option' => ['value' => '08']], '8', '/option',
  290. ['option' => ['value' => '09', 'selected' => 'selected']], '9', '/option',
  291. ['option' => ['value' => '10']], '10', '/option',
  292. ['option' => ['value' => '11']], '11', '/option',
  293. ['option' => ['value' => '12']], '12', '/option',
  294. '/select',
  295. ];
  296. $this->assertTags($result, $expected);
  297. }
  298. /**
  299. * Test rendering month widget with names.
  300. *
  301. * @return void
  302. */
  303. public function testRenderMonthWidgetWithNames() {
  304. $now = new \DateTime('2010-09-01 12:00:00');
  305. $result = $this->DateTime->render([
  306. 'name' => 'date',
  307. 'year' => false,
  308. 'day' => false,
  309. 'hour' => false,
  310. 'minute' => false,
  311. 'second' => false,
  312. 'month' => ['data-foo' => 'test', 'names' => true],
  313. 'val' => $now,
  314. ]);
  315. $expected = [
  316. 'select' => ['name' => 'date[month]', 'data-foo' => 'test'],
  317. ['option' => ['value' => '01']], 'January', '/option',
  318. ['option' => ['value' => '02']], 'February', '/option',
  319. ['option' => ['value' => '03']], 'March', '/option',
  320. ['option' => ['value' => '04']], 'April', '/option',
  321. ['option' => ['value' => '05']], 'May', '/option',
  322. ['option' => ['value' => '06']], 'June', '/option',
  323. ['option' => ['value' => '07']], 'July', '/option',
  324. ['option' => ['value' => '08']], 'August', '/option',
  325. ['option' => ['value' => '09', 'selected' => 'selected']], 'September', '/option',
  326. ['option' => ['value' => '10']], 'October', '/option',
  327. ['option' => ['value' => '11']], 'November', '/option',
  328. ['option' => ['value' => '12']], 'December', '/option',
  329. '/select',
  330. ];
  331. $this->assertTags($result, $expected);
  332. }
  333. /**
  334. * Test rendering the day widget.
  335. *
  336. * @return void
  337. */
  338. public function testRenderDayWidget() {
  339. $now = new \DateTime('2010-09-09 12:00:00');
  340. $result = $this->DateTime->render([
  341. 'name' => 'date',
  342. 'year' => false,
  343. 'month' => false,
  344. 'day' => [
  345. 'data-foo' => 'test',
  346. ],
  347. 'hour' => false,
  348. 'minute' => false,
  349. 'second' => false,
  350. 'val' => $now,
  351. ]);
  352. $expected = [
  353. 'select' => ['name' => 'date[day]', 'data-foo' => 'test'],
  354. ['option' => ['value' => '01']], '1', '/option',
  355. ['option' => ['value' => '02']], '2', '/option',
  356. ['option' => ['value' => '03']], '3', '/option',
  357. ['option' => ['value' => '04']], '4', '/option',
  358. ['option' => ['value' => '05']], '5', '/option',
  359. ['option' => ['value' => '06']], '6', '/option',
  360. ['option' => ['value' => '07']], '7', '/option',
  361. ['option' => ['value' => '08']], '8', '/option',
  362. ['option' => ['value' => '09', 'selected' => 'selected']], '9', '/option',
  363. ['option' => ['value' => '10']], '10', '/option',
  364. ['option' => ['value' => '11']], '11', '/option',
  365. ['option' => ['value' => '12']], '12', '/option',
  366. ['option' => ['value' => '13']], '13', '/option',
  367. ['option' => ['value' => '14']], '14', '/option',
  368. ['option' => ['value' => '15']], '15', '/option',
  369. ['option' => ['value' => '16']], '16', '/option',
  370. ['option' => ['value' => '17']], '17', '/option',
  371. ['option' => ['value' => '18']], '18', '/option',
  372. ['option' => ['value' => '19']], '19', '/option',
  373. ['option' => ['value' => '20']], '20', '/option',
  374. ['option' => ['value' => '21']], '21', '/option',
  375. ['option' => ['value' => '22']], '22', '/option',
  376. ['option' => ['value' => '23']], '23', '/option',
  377. ['option' => ['value' => '24']], '24', '/option',
  378. ['option' => ['value' => '25']], '25', '/option',
  379. ['option' => ['value' => '26']], '26', '/option',
  380. ['option' => ['value' => '27']], '27', '/option',
  381. ['option' => ['value' => '28']], '28', '/option',
  382. ['option' => ['value' => '29']], '29', '/option',
  383. ['option' => ['value' => '30']], '30', '/option',
  384. ['option' => ['value' => '31']], '31', '/option',
  385. '/select',
  386. ];
  387. $this->assertTags($result, $expected);
  388. }
  389. /**
  390. * Test rendering the hour picker in 24 hour mode.
  391. *
  392. * @return void
  393. */
  394. public function testRenderHourWidget24StartAndEnd() {
  395. $now = new \DateTime('2010-09-09 13:00:00');
  396. $result = $this->DateTime->render([
  397. 'name' => 'date',
  398. 'year' => false,
  399. 'month' => false,
  400. 'day' => false,
  401. 'hour' => [
  402. 'start' => 8,
  403. 'end' => 16
  404. ],
  405. 'minute' => false,
  406. 'second' => false,
  407. 'val' => $now,
  408. ]);
  409. $this->assertContains('<select name="date[hour]">', $result);
  410. $this->assertNotContains(
  411. '<option value="01">1</option>',
  412. $result,
  413. 'no 1 am'
  414. );
  415. $this->assertNotContains(
  416. '<option value="07">7</option>',
  417. $result,
  418. 'contain 7'
  419. );
  420. $this->assertContains(
  421. '<option value="13" selected="selected">13</option>',
  422. $result,
  423. 'selected value present'
  424. );
  425. $this->assertNotContains(
  426. '<option value="17">17</option>',
  427. $result,
  428. 'contains 17 hours'
  429. );
  430. }
  431. /**
  432. * Test rendering the hour picker in 24 hour mode.
  433. *
  434. * @return void
  435. */
  436. public function testRenderHourWidget24() {
  437. $now = new \DateTime('2010-09-09 13:00:00');
  438. $result = $this->DateTime->render([
  439. 'name' => 'date',
  440. 'year' => false,
  441. 'month' => false,
  442. 'day' => false,
  443. 'hour' => [
  444. 'data-foo' => 'test'
  445. ],
  446. 'minute' => false,
  447. 'second' => false,
  448. 'val' => $now,
  449. ]);
  450. $this->assertContains('<select name="date[hour]" data-foo="test">', $result);
  451. $this->assertContains(
  452. '<option value="01">1</option>',
  453. $result,
  454. 'contain 1 am'
  455. );
  456. $this->assertContains(
  457. '<option value="05">5</option>',
  458. $result,
  459. 'contain 5 am'
  460. );
  461. $this->assertContains(
  462. '<option value="13" selected="selected">13</option>',
  463. $result,
  464. 'selected value present'
  465. );
  466. $this->assertContains(
  467. '<option value="24">24</option>',
  468. $result,
  469. 'contains 24 hours'
  470. );
  471. $this->assertNotContains('date[day]', $result, 'No day select.');
  472. $this->assertNotContains('value="0"', $result, 'No zero hour');
  473. $this->assertNotContains('value="25"', $result, 'No 25th hour');
  474. $this->assertNotContains('<select name="date[meridian]">', $result);
  475. $this->assertNotContains('<option value="pm" selected="selected">pm</option>', $result);
  476. }
  477. /**
  478. * Test rendering the hour widget in 12 hour mode.
  479. *
  480. * @return void
  481. */
  482. public function testRenderHourWidget12() {
  483. $now = new \DateTime('2010-09-09 13:00:00');
  484. $result = $this->DateTime->render([
  485. 'name' => 'date',
  486. 'year' => false,
  487. 'month' => false,
  488. 'day' => false,
  489. 'hour' => [
  490. 'format' => 12,
  491. 'data-foo' => 'test'
  492. ],
  493. 'minute' => false,
  494. 'second' => false,
  495. 'val' => $now,
  496. ]);
  497. $this->assertContains('<select name="date[hour]" data-foo="test">', $result);
  498. $this->assertContains(
  499. '<option value="01" selected="selected">1</option>',
  500. $result,
  501. 'contain 1pm selected'
  502. );
  503. $this->assertContains(
  504. '<option value="05">5</option>',
  505. $result,
  506. 'contain 5'
  507. );
  508. $this->assertContains(
  509. '<option value="12">12</option>',
  510. $result,
  511. 'contain 12'
  512. );
  513. $this->assertNotContains(
  514. '<option value="13">13</option>',
  515. $result,
  516. 'selected value present'
  517. );
  518. $this->assertNotContains('date[day]', $result, 'No day select.');
  519. $this->assertNotContains('value="0"', $result, 'No zero hour');
  520. $this->assertContains('<select name="date[meridian]">', $result);
  521. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  522. }
  523. /**
  524. * Test rendering the hour picker in 12 hour mode.
  525. *
  526. * @return void
  527. */
  528. public function testRenderHourWidget12StartAndEnd() {
  529. $now = new \DateTime('2010-09-09 13:00:00');
  530. $result = $this->DateTime->render([
  531. 'name' => 'date',
  532. 'year' => false,
  533. 'month' => false,
  534. 'day' => false,
  535. 'hour' => [
  536. 'start' => 8,
  537. 'end' => 12
  538. ],
  539. 'minute' => false,
  540. 'second' => false,
  541. 'val' => $now,
  542. ]);
  543. $this->assertContains('<select name="date[hour]">', $result);
  544. $this->assertContains(
  545. '<option value="08">8</option>',
  546. $result,
  547. 'contains 8am'
  548. );
  549. $this->assertContains(
  550. '<option value="12">12</option>',
  551. $result,
  552. 'contains 8am'
  553. );
  554. $this->assertNotContains(
  555. '<option value="01">1</option>',
  556. $result,
  557. 'no 1 am'
  558. );
  559. $this->assertNotContains(
  560. '<option value="07">7</option>',
  561. $result,
  562. 'contain 7'
  563. );
  564. $this->assertNotContains(
  565. '<option value="13" selected="selected">13</option>',
  566. $result,
  567. 'selected value present'
  568. );
  569. }
  570. /**
  571. * Test rendering the minute widget with no options.
  572. *
  573. * @return void
  574. */
  575. public function testRenderMinuteWidget() {
  576. $now = new \DateTime('2010-09-09 13:25:00');
  577. $result = $this->DateTime->render([
  578. 'name' => 'date',
  579. 'year' => false,
  580. 'month' => false,
  581. 'day' => false,
  582. 'hour' => false,
  583. 'minute' => [
  584. 'data-foo' => 'test',
  585. ],
  586. 'second' => false,
  587. 'val' => $now,
  588. ]);
  589. $this->assertContains('<select name="date[minute]" data-foo="test">', $result);
  590. $this->assertContains(
  591. '<option value="00">00</option>',
  592. $result,
  593. 'contains 1'
  594. );
  595. $this->assertContains(
  596. '<option value="05">05</option>',
  597. $result,
  598. 'contains 05'
  599. );
  600. $this->assertContains(
  601. '<option value="25" selected="selected">25</option>',
  602. $result,
  603. 'selected value present'
  604. );
  605. $this->assertContains(
  606. '<option value="59">59</option>',
  607. $result,
  608. 'contains 59'
  609. );
  610. $this->assertNotContains('value="60"', $result, 'No 60 value');
  611. }
  612. /**
  613. * Test minutes with interval values.
  614. *
  615. * @return void
  616. */
  617. public function testRenderMinuteWidgetInterval() {
  618. $now = new \DateTime('2010-09-09 13:23:00');
  619. $result = $this->DateTime->render([
  620. 'name' => 'date',
  621. 'year' => false,
  622. 'month' => false,
  623. 'day' => false,
  624. 'hour' => false,
  625. 'minute' => [
  626. 'interval' => 5
  627. ],
  628. 'second' => false,
  629. 'val' => $now,
  630. ]);
  631. $this->assertContains('<select name="date[minute]">', $result);
  632. $this->assertContains(
  633. '<option value="00">00</option>',
  634. $result,
  635. 'contains 0'
  636. );
  637. $this->assertContains(
  638. '<option value="05">05</option>',
  639. $result,
  640. 'contains 05'
  641. );
  642. $this->assertContains(
  643. '<option value="25" selected="selected">25</option>',
  644. $result,
  645. 'selected value present'
  646. );
  647. $this->assertContains(
  648. '<option value="55">55</option>',
  649. $result,
  650. 'contains 59'
  651. );
  652. $this->assertNotContains('value="2"', $result, 'No 2 value');
  653. $this->assertNotContains('value="23"', $result, 'No 23 value');
  654. $this->assertNotContains('value="58"', $result, 'No 58 value');
  655. $this->assertNotContains('value="59"', $result, 'No 59 value');
  656. $this->assertNotContains('value="60"', $result, 'No 60 value');
  657. }
  658. /**
  659. * Test rounding up and down.
  660. *
  661. * @return void
  662. */
  663. public function testRenderMinuteWidgetIntervalRounding() {
  664. $now = new \DateTime('2010-09-09 13:22:00');
  665. $data = [
  666. 'name' => 'date',
  667. 'year' => false,
  668. 'month' => false,
  669. 'day' => false,
  670. 'hour' => false,
  671. 'minute' => [
  672. 'interval' => 5,
  673. 'round' => 'up',
  674. ],
  675. 'second' => false,
  676. 'val' => $now,
  677. ];
  678. $result = $this->DateTime->render($data);
  679. $this->assertContains(
  680. '<option value="25" selected="selected">25</option>',
  681. $result,
  682. 'selected value present'
  683. );
  684. $this->assertNotContains('value="23"', $result, 'No 23 value');
  685. $data['minute']['round'] = 'down';
  686. $result = $this->DateTime->render($data);
  687. $this->assertContains(
  688. '<option value="20" selected="selected">20</option>',
  689. $result,
  690. 'selected value present'
  691. );
  692. $this->assertNotContains('value="23"', $result, 'No 23 value');
  693. }
  694. /**
  695. * Test that minute interval rounding can effect hours and days.
  696. *
  697. * @return void
  698. */
  699. public function testMinuteIntervalHourRollover() {
  700. $now = new \DateTime('2010-09-09 23:58:00');
  701. $result = $this->DateTime->render([
  702. 'name' => 'date',
  703. 'year' => false,
  704. 'month' => false,
  705. 'minute' => [
  706. 'interval' => 5,
  707. 'round' => 'up',
  708. ],
  709. 'second' => false,
  710. 'val' => $now,
  711. ]);
  712. $this->assertContains(
  713. '<option value="00" selected="selected">00</option>',
  714. $result,
  715. 'selected minute present'
  716. );
  717. $this->assertContains(
  718. '<option value="10" selected="selected">10</option>',
  719. $result,
  720. 'selected day present'
  721. );
  722. }
  723. /**
  724. * Test render seconds basic.
  725. *
  726. * @return void
  727. */
  728. public function testRenderSecondsWidget() {
  729. $now = new \DateTime('2010-09-09 13:00:25');
  730. $result = $this->DateTime->render([
  731. 'name' => 'date',
  732. 'year' => false,
  733. 'month' => false,
  734. 'day' => false,
  735. 'hour' => false,
  736. 'minute' => false,
  737. 'second' => [
  738. 'data-foo' => 'test',
  739. ],
  740. 'val' => $now,
  741. ]);
  742. $this->assertContains('<select name="date[second]" data-foo="test">', $result);
  743. $this->assertContains(
  744. '<option value="01">01</option>',
  745. $result,
  746. 'contains 1'
  747. );
  748. $this->assertContains(
  749. '<option value="05">05</option>',
  750. $result,
  751. 'contains 05'
  752. );
  753. $this->assertContains(
  754. '<option value="25" selected="selected">25</option>',
  755. $result,
  756. 'selected value present'
  757. );
  758. $this->assertContains(
  759. '<option value="60">60</option>',
  760. $result,
  761. 'contains 60'
  762. );
  763. $this->assertNotContains('value="0"', $result, 'No zero value');
  764. $this->assertNotContains('value="61"', $result, 'No 61 value');
  765. }
  766. /**
  767. * Test the merdian select.
  768. *
  769. * @return void
  770. */
  771. public function testRenderMeridianWidget() {
  772. $now = new \DateTime('2010-09-09 13:00:25');
  773. $result = $this->DateTime->render([
  774. 'name' => 'date',
  775. 'year' => false,
  776. 'month' => false,
  777. 'day' => false,
  778. 'hour' => false,
  779. 'minute' => false,
  780. 'second' => false,
  781. 'meridian' => [],
  782. 'val' => $now,
  783. ]);
  784. $expected = [
  785. 'select' => ['name' => 'date[meridian]'],
  786. ['option' => ['value' => 'am']], 'am', '/option',
  787. ['option' => ['value' => 'pm', 'selected' => 'selected']], 'pm', '/option',
  788. '/select',
  789. ];
  790. $this->assertTags($result, $expected);
  791. $now = new \DateTime('2010-09-09 09:00:25');
  792. $result = $this->DateTime->render([
  793. 'name' => 'date',
  794. 'year' => false,
  795. 'month' => false,
  796. 'day' => false,
  797. 'hour' => false,
  798. 'minute' => false,
  799. 'second' => false,
  800. 'meridian' => [],
  801. 'val' => $now,
  802. ]);
  803. $expected = [
  804. 'select' => ['name' => 'date[meridian]'],
  805. ['option' => ['value' => 'am', 'selected' => 'selected']], 'am', '/option',
  806. ['option' => ['value' => 'pm']], 'pm', '/option',
  807. '/select',
  808. ];
  809. $this->assertTags($result, $expected);
  810. }
  811. }