DateTimeWidgetTest.php 35 KB

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