DateTimeWidgetTest.php 37 KB

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