DateTimeTest.php 26 KB

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