DateTimeWidgetTest.php 27 KB

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