DateTimeWidgetTest.php 43 KB

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