DateTimeTest.php 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968
  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. $this->assertNotContains('meridian', $result, '24hrs has no meridian');
  500. }
  501. /**
  502. * Test rendering the hour picker in 24 hour mode.
  503. *
  504. * @return void
  505. */
  506. public function testRenderHourWidget24() {
  507. $now = new \DateTime('2010-09-09 13:00:00');
  508. $result = $this->DateTime->render([
  509. 'name' => 'date',
  510. 'year' => false,
  511. 'month' => false,
  512. 'day' => false,
  513. 'hour' => [
  514. 'format' => 24,
  515. 'data-foo' => 'test'
  516. ],
  517. 'minute' => false,
  518. 'second' => false,
  519. 'val' => $now,
  520. 'meridian' => [],
  521. ]);
  522. $this->assertContains('<select name="date[hour]" data-foo="test">', $result);
  523. $this->assertContains('<option value="00">0</option>', $result);
  524. $this->assertContains(
  525. '<option value="01">1</option>',
  526. $result,
  527. 'contain 1 am'
  528. );
  529. $this->assertContains(
  530. '<option value="05">5</option>',
  531. $result,
  532. 'contain 5 am'
  533. );
  534. $this->assertContains(
  535. '<option value="13" selected="selected">13</option>',
  536. $result,
  537. 'selected value present'
  538. );
  539. $this->assertContains('<option value="23">23</option>', $result);
  540. $this->assertNotContains('date[day]', $result, 'No day select.');
  541. $this->assertNotContains('value="0"', $result, 'No zero hour');
  542. $this->assertNotContains('value="24"', $result, 'No 25th hour');
  543. $this->assertNotContains('<select name="date[meridian]">', $result);
  544. $this->assertNotContains('<option value="pm" selected="selected">pm</option>', $result);
  545. }
  546. /**
  547. * test selecting various options in 24 hr mode.
  548. *
  549. * @return void
  550. */
  551. public function testRenderHour24SelectedValues() {
  552. $now = new \DateTime('2010-09-09 23:00:00');
  553. $data = [
  554. 'name' => 'date',
  555. 'year' => false,
  556. 'month' => false,
  557. 'day' => false,
  558. 'hour' => [],
  559. 'minute' => false,
  560. 'second' => false,
  561. 'val' => $now,
  562. ];
  563. $result = $this->DateTime->render($data);
  564. $this->assertContains('<option value="23" selected="selected">23</option>', $result);
  565. $data['val'] = '2010-09-09 23:00:00';
  566. $result = $this->DateTime->render($data);
  567. $this->assertContains('<option value="23" selected="selected">23</option>', $result);
  568. }
  569. /**
  570. * Test rendering the hour widget in 12 hour mode.
  571. *
  572. * @return void
  573. */
  574. public function testRenderHourWidget12() {
  575. $now = new \DateTime('2010-09-09 13:00:00');
  576. $result = $this->DateTime->render([
  577. 'name' => 'date',
  578. 'year' => false,
  579. 'month' => false,
  580. 'day' => false,
  581. 'hour' => [
  582. 'format' => 12,
  583. 'data-foo' => 'test'
  584. ],
  585. 'minute' => false,
  586. 'second' => false,
  587. 'val' => $now,
  588. ]);
  589. $this->assertContains('<select name="date[hour]" data-foo="test">', $result);
  590. $this->assertContains(
  591. '<option value="01" selected="selected">1</option>',
  592. $result,
  593. 'contain 1pm selected'
  594. );
  595. $this->assertContains(
  596. '<option value="05">5</option>',
  597. $result,
  598. 'contain 5'
  599. );
  600. $this->assertContains(
  601. '<option value="12">12</option>',
  602. $result,
  603. 'contain 12'
  604. );
  605. $this->assertNotContains(
  606. '<option value="13">13</option>',
  607. $result,
  608. 'selected value present'
  609. );
  610. $this->assertNotContains('date[day]', $result, 'No day select.');
  611. $this->assertNotContains('value="0"', $result, 'No zero hour');
  612. $this->assertContains('<select name="date[meridian]">', $result);
  613. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  614. }
  615. /**
  616. * Test rendering hour widget in 12 hour mode at midnight.
  617. *
  618. * @return void
  619. */
  620. public function testRenderHourWidget12Midnight() {
  621. $now = new \DateTime('2010-09-09 00:30:45');
  622. $result = $this->DateTime->render([
  623. 'name' => 'date',
  624. 'year' => false,
  625. 'month' => false,
  626. 'day' => false,
  627. 'hour' => [
  628. 'format' => 12,
  629. ],
  630. 'minute' => false,
  631. 'second' => false,
  632. 'val' => $now,
  633. ]);
  634. $this->assertContains(
  635. '<option value="12" selected="selected">12</option>',
  636. $result,
  637. '12 is selected'
  638. );
  639. }
  640. /**
  641. * Test rendering the hour picker in 12 hour mode.
  642. *
  643. * @return void
  644. */
  645. public function testRenderHourWidget12StartAndEnd() {
  646. $now = new \DateTime('2010-09-09 13:00:00');
  647. $result = $this->DateTime->render([
  648. 'name' => 'date',
  649. 'year' => false,
  650. 'month' => false,
  651. 'day' => false,
  652. 'hour' => [
  653. 'start' => 8,
  654. 'end' => 12
  655. ],
  656. 'minute' => false,
  657. 'second' => false,
  658. 'val' => $now,
  659. ]);
  660. $this->assertContains('<select name="date[hour]">', $result);
  661. $this->assertContains(
  662. '<option value="08">8</option>',
  663. $result,
  664. 'contains 8am'
  665. );
  666. $this->assertContains(
  667. '<option value="12">12</option>',
  668. $result,
  669. 'contains 8am'
  670. );
  671. $this->assertNotContains(
  672. '<option value="01">1</option>',
  673. $result,
  674. 'no 1 am'
  675. );
  676. $this->assertNotContains(
  677. '<option value="07">7</option>',
  678. $result,
  679. 'contain 7'
  680. );
  681. $this->assertNotContains(
  682. '<option value="13" selected="selected">13</option>',
  683. $result,
  684. 'selected value present'
  685. );
  686. }
  687. /**
  688. * Test rendering the minute widget with no options.
  689. *
  690. * @return void
  691. */
  692. public function testRenderMinuteWidget() {
  693. $now = new \DateTime('2010-09-09 13:25:00');
  694. $result = $this->DateTime->render([
  695. 'name' => 'date',
  696. 'year' => false,
  697. 'month' => false,
  698. 'day' => false,
  699. 'hour' => false,
  700. 'minute' => [
  701. 'data-foo' => 'test',
  702. ],
  703. 'second' => false,
  704. 'val' => $now,
  705. ]);
  706. $this->assertContains('<select name="date[minute]" data-foo="test">', $result);
  707. $this->assertContains(
  708. '<option value="00">00</option>',
  709. $result,
  710. 'contains 1'
  711. );
  712. $this->assertContains(
  713. '<option value="05">05</option>',
  714. $result,
  715. 'contains 05'
  716. );
  717. $this->assertContains(
  718. '<option value="25" selected="selected">25</option>',
  719. $result,
  720. 'selected value present'
  721. );
  722. $this->assertContains(
  723. '<option value="59">59</option>',
  724. $result,
  725. 'contains 59'
  726. );
  727. $this->assertNotContains('value="60"', $result, 'No 60 value');
  728. }
  729. /**
  730. * Test minutes with interval values.
  731. *
  732. * @return void
  733. */
  734. public function testRenderMinuteWidgetInterval() {
  735. $now = new \DateTime('2010-09-09 13:23:00');
  736. $result = $this->DateTime->render([
  737. 'name' => 'date',
  738. 'year' => false,
  739. 'month' => false,
  740. 'day' => false,
  741. 'hour' => false,
  742. 'minute' => [
  743. 'interval' => 5
  744. ],
  745. 'second' => false,
  746. 'val' => $now,
  747. ]);
  748. $this->assertContains('<select name="date[minute]">', $result);
  749. $this->assertContains(
  750. '<option value="00">00</option>',
  751. $result,
  752. 'contains 0'
  753. );
  754. $this->assertContains(
  755. '<option value="05">05</option>',
  756. $result,
  757. 'contains 05'
  758. );
  759. $this->assertContains(
  760. '<option value="25" selected="selected">25</option>',
  761. $result,
  762. 'selected value present'
  763. );
  764. $this->assertContains(
  765. '<option value="55">55</option>',
  766. $result,
  767. 'contains 59'
  768. );
  769. $this->assertNotContains('value="2"', $result, 'No 2 value');
  770. $this->assertNotContains('value="23"', $result, 'No 23 value');
  771. $this->assertNotContains('value="58"', $result, 'No 58 value');
  772. $this->assertNotContains('value="59"', $result, 'No 59 value');
  773. $this->assertNotContains('value="60"', $result, 'No 60 value');
  774. }
  775. /**
  776. * Test rounding up and down.
  777. *
  778. * @return void
  779. */
  780. public function testRenderMinuteWidgetIntervalRounding() {
  781. $now = new \DateTime('2010-09-09 13:22:00');
  782. $data = [
  783. 'name' => 'date',
  784. 'year' => false,
  785. 'month' => false,
  786. 'day' => false,
  787. 'hour' => false,
  788. 'minute' => [
  789. 'interval' => 5,
  790. 'round' => 'up',
  791. ],
  792. 'second' => false,
  793. 'val' => $now,
  794. ];
  795. $result = $this->DateTime->render($data);
  796. $this->assertContains(
  797. '<option value="25" selected="selected">25</option>',
  798. $result,
  799. 'selected value present'
  800. );
  801. $this->assertNotContains('value="23"', $result, 'No 23 value');
  802. $data['minute']['round'] = 'down';
  803. $result = $this->DateTime->render($data);
  804. $this->assertContains(
  805. '<option value="20" selected="selected">20</option>',
  806. $result,
  807. 'selected value present'
  808. );
  809. $this->assertNotContains('value="23"', $result, 'No 23 value');
  810. }
  811. /**
  812. * Test that minute interval rounding can effect hours and days.
  813. *
  814. * @return void
  815. */
  816. public function testMinuteIntervalHourRollover() {
  817. $now = new \DateTime('2010-09-09 23:58:00');
  818. $result = $this->DateTime->render([
  819. 'name' => 'date',
  820. 'year' => false,
  821. 'month' => false,
  822. 'minute' => [
  823. 'interval' => 5,
  824. 'round' => 'up',
  825. ],
  826. 'second' => false,
  827. 'val' => $now,
  828. ]);
  829. $this->assertContains(
  830. '<option value="00" selected="selected">00</option>',
  831. $result,
  832. 'selected minute present'
  833. );
  834. $this->assertContains(
  835. '<option value="10" selected="selected">10</option>',
  836. $result,
  837. 'selected day present'
  838. );
  839. }
  840. /**
  841. * Test render seconds basic.
  842. *
  843. * @return void
  844. */
  845. public function testRenderSecondsWidget() {
  846. $now = new \DateTime('2010-09-09 13:00:25');
  847. $result = $this->DateTime->render([
  848. 'name' => 'date',
  849. 'year' => false,
  850. 'month' => false,
  851. 'day' => false,
  852. 'hour' => false,
  853. 'minute' => false,
  854. 'second' => [
  855. 'data-foo' => 'test',
  856. ],
  857. 'val' => $now,
  858. ]);
  859. $this->assertContains('<select name="date[second]" data-foo="test">', $result);
  860. $this->assertContains(
  861. '<option value="01">01</option>',
  862. $result,
  863. 'contains 1'
  864. );
  865. $this->assertContains(
  866. '<option value="05">05</option>',
  867. $result,
  868. 'contains 05'
  869. );
  870. $this->assertContains(
  871. '<option value="25" selected="selected">25</option>',
  872. $result,
  873. 'selected value present'
  874. );
  875. $this->assertContains(
  876. '<option value="60">60</option>',
  877. $result,
  878. 'contains 60'
  879. );
  880. $this->assertNotContains('value="0"', $result, 'No zero value');
  881. $this->assertNotContains('value="61"', $result, 'No 61 value');
  882. }
  883. /**
  884. * Test the merdian select.
  885. *
  886. * @return void
  887. */
  888. public function testRenderMeridianWidget() {
  889. $now = new \DateTime('2010-09-09 13:00:25');
  890. $result = $this->DateTime->render([
  891. 'name' => 'date',
  892. 'year' => false,
  893. 'month' => false,
  894. 'day' => false,
  895. 'hour' => false,
  896. 'minute' => false,
  897. 'second' => false,
  898. 'meridian' => [],
  899. 'val' => $now,
  900. ]);
  901. $expected = [
  902. 'select' => ['name' => 'date[meridian]'],
  903. ['option' => ['value' => 'am']], 'am', '/option',
  904. ['option' => ['value' => 'pm', 'selected' => 'selected']], 'pm', '/option',
  905. '/select',
  906. ];
  907. $this->assertTags($result, $expected);
  908. $now = new \DateTime('2010-09-09 09:00:25');
  909. $result = $this->DateTime->render([
  910. 'name' => 'date',
  911. 'year' => false,
  912. 'month' => false,
  913. 'day' => false,
  914. 'hour' => false,
  915. 'minute' => false,
  916. 'second' => false,
  917. 'meridian' => [],
  918. 'val' => $now,
  919. ]);
  920. $expected = [
  921. 'select' => ['name' => 'date[meridian]'],
  922. ['option' => ['value' => 'am', 'selected' => 'selected']], 'am', '/option',
  923. ['option' => ['value' => 'pm']], 'pm', '/option',
  924. '/select',
  925. ];
  926. $this->assertTags($result, $expected);
  927. }
  928. }