DateTimeTest.php 24 KB

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