DateTimeWidgetTest.php 35 KB

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