DateTimeWidgetTest.php 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (https://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. (https://cakefoundation.org)
  11. * @link https://cakephp.org CakePHP(tm) Project
  12. * @since 3.0.0
  13. * @license https://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. * @return void
  28. */
  29. public function setUp()
  30. {
  31. parent::setUp();
  32. $templates = [
  33. 'select' => '<select name="{{name}}"{{attrs}}>{{content}}</select>',
  34. 'option' => '<option value="{{value}}"{{attrs}}>{{text}}</option>',
  35. 'optgroup' => '<optgroup label="{{label}}"{{attrs}}>{{content}}</optgroup>',
  36. 'dateWidget' => '{{year}}{{month}}{{day}}{{hour}}{{minute}}{{second}}{{meridian}}',
  37. ];
  38. $this->templates = new StringTemplate($templates);
  39. $this->context = $this->getMockBuilder('Cake\View\Form\ContextInterface')->getMock();
  40. $this->selectBox = new SelectBoxWidget($this->templates);
  41. $this->DateTime = new DateTimeWidget($this->templates, $this->selectBox);
  42. }
  43. /**
  44. * Data provider for testing various types of invalid selected values.
  45. *
  46. * @return array
  47. */
  48. public static function invalidSelectedValuesProvider()
  49. {
  50. return [
  51. 'false' => [false],
  52. 'true' => [true],
  53. 'string' => ['Bag of poop'],
  54. 'array' => [[
  55. 'derp' => 'hurt',
  56. ]],
  57. ];
  58. }
  59. /**
  60. * test rendering selected values.
  61. *
  62. * @dataProvider invalidSelectedValuesProvider
  63. * @return void
  64. */
  65. public function testRenderSelectedInvalid($selected)
  66. {
  67. $result = $this->DateTime->render(['val' => $selected], $this->context);
  68. $now = new \DateTime();
  69. $format = '<option value="%s" selected="selected">%s</option>';
  70. $this->assertContains(
  71. sprintf($format, $now->format('Y'), $now->format('Y')),
  72. $result
  73. );
  74. }
  75. /**
  76. * test rendering empty selected.
  77. *
  78. * @return void
  79. */
  80. public function testRenderSelectedEmpty()
  81. {
  82. $result = $this->DateTime->render([
  83. 'val' => '',
  84. 'year' => ['empty' => true],
  85. 'month' => ['empty' => true],
  86. 'day' => ['empty' => true],
  87. 'hour' => ['empty' => true],
  88. 'minute' => ['empty' => true],
  89. ], $this->context);
  90. $this->assertContains('<option value="" selected="selected"></option>', $result);
  91. $this->assertNotRegExp('/value="\d+" selected="selected"/', $result);
  92. $result = $this->DateTime->render([
  93. 'val' => ['year' => '', 'month' => '', 'day' => '', 'hour' => '', 'minute' => ''],
  94. 'year' => ['empty' => true],
  95. 'month' => ['empty' => true],
  96. 'day' => ['empty' => true],
  97. 'hour' => ['empty' => true],
  98. 'minute' => ['empty' => true],
  99. ], $this->context);
  100. $this->assertContains('<option value="" selected="selected"></option>', $result);
  101. $this->assertNotRegExp('/value="\d+" selected="selected"/', $result);
  102. }
  103. /**
  104. * Test empty with custom values.
  105. *
  106. * @return void
  107. */
  108. public function testRenderEmptyCustom()
  109. {
  110. $result = $this->DateTime->render([
  111. 'val' => '',
  112. 'year' => [
  113. 'empty' => ['nope' => '(choose one)'],
  114. ],
  115. ], $this->context);
  116. $this->assertContains('<option value="nope">(choose one)</option>', $result);
  117. $this->assertNotContains('<optgroup', $result, 'No optgroups should be present.');
  118. }
  119. /**
  120. * Data provider for testing various acceptable selected values.
  121. *
  122. * @return array
  123. */
  124. public static function selectedValuesProvider()
  125. {
  126. $date = new \DateTime('2014-01-20 12:30:45');
  127. return [
  128. 'DateTime' => [$date],
  129. 'string' => [$date->format('Y-m-d H:i:s')],
  130. 'int string' => [$date->format('U')],
  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 12 hour widgets with empty options and no value.
  751. *
  752. * @return void
  753. */
  754. public function testRenderHourWidget12Empty()
  755. {
  756. $result = $this->DateTime->render([
  757. 'val' => ['hour' => '', 'minute' => '', 'meridian' => ''],
  758. 'year' => false,
  759. 'month' => false,
  760. 'day' => false,
  761. 'hour' => ['format' => 12, 'empty' => '--'],
  762. 'minute' => ['empty' => '--'],
  763. 'second' => false,
  764. 'meridian' => ['empty' => '--'],
  765. ], $this->context);
  766. $this->assertContains('<option value="" selected="selected">--</option>', $result);
  767. $this->assertNotRegExp('/value="\d+" selected="selected"/', $result);
  768. }
  769. /**
  770. * Test rendering hour widget in 12 hour mode at midnight.
  771. *
  772. * @return void
  773. */
  774. public function testRenderHourWidget12Midnight()
  775. {
  776. $now = new \DateTime('2010-09-09 00:30:45');
  777. $result = $this->DateTime->render([
  778. 'name' => 'date',
  779. 'year' => false,
  780. 'month' => false,
  781. 'day' => false,
  782. 'hour' => [
  783. 'format' => 12,
  784. ],
  785. 'minute' => false,
  786. 'second' => false,
  787. 'val' => $now,
  788. ], $this->context);
  789. $this->assertContains(
  790. '<option value="12" selected="selected">12</option>',
  791. $result,
  792. '12 is selected'
  793. );
  794. }
  795. /**
  796. * Test rendering the hour picker in 12 hour mode.
  797. *
  798. * @return void
  799. */
  800. public function testRenderHourWidget12StartAndEnd()
  801. {
  802. $now = new \DateTime('2010-09-09 13:00:00');
  803. $result = $this->DateTime->render([
  804. 'name' => 'date',
  805. 'year' => false,
  806. 'month' => false,
  807. 'day' => false,
  808. 'hour' => [
  809. 'start' => 8,
  810. 'end' => 12,
  811. ],
  812. 'minute' => false,
  813. 'second' => false,
  814. 'val' => $now,
  815. ], $this->context);
  816. $this->assertContains('<select name="date[hour]">', $result);
  817. $this->assertContains(
  818. '<option value="08">8</option>',
  819. $result,
  820. 'contains 8am'
  821. );
  822. $this->assertContains(
  823. '<option value="12">12</option>',
  824. $result,
  825. 'contains 8am'
  826. );
  827. $this->assertNotContains(
  828. '<option value="01">1</option>',
  829. $result,
  830. 'no 1 am'
  831. );
  832. $this->assertNotContains(
  833. '<option value="07">7</option>',
  834. $result,
  835. 'contain 7'
  836. );
  837. $this->assertNotContains(
  838. '<option value="13" selected="selected">13</option>',
  839. $result,
  840. 'selected value present'
  841. );
  842. }
  843. /**
  844. * Test rendering the minute widget with no options.
  845. *
  846. * @return void
  847. */
  848. public function testRenderMinuteWidget()
  849. {
  850. $now = new \DateTime('2010-09-09 13:25:00');
  851. $result = $this->DateTime->render([
  852. 'name' => 'date',
  853. 'year' => false,
  854. 'month' => false,
  855. 'day' => false,
  856. 'hour' => false,
  857. 'minute' => [
  858. 'data-foo' => 'test',
  859. ],
  860. 'second' => false,
  861. 'val' => $now,
  862. ], $this->context);
  863. $this->assertContains('<select name="date[minute]" data-foo="test">', $result);
  864. $this->assertContains(
  865. '<option value="00">00</option>',
  866. $result,
  867. 'contains 00'
  868. );
  869. $this->assertContains(
  870. '<option value="05">05</option>',
  871. $result,
  872. 'contains 05'
  873. );
  874. $this->assertContains(
  875. '<option value="25" selected="selected">25</option>',
  876. $result,
  877. 'selected value present'
  878. );
  879. $this->assertContains(
  880. '<option value="59">59</option>',
  881. $result,
  882. 'contains 59'
  883. );
  884. $this->assertNotContains('value="60"', $result, 'No 60 value');
  885. }
  886. /**
  887. * Test rendering the minute widget with empty at zero options.
  888. *
  889. * @return void
  890. */
  891. public function testRenderMinuteWidgetEmptyZeroDefault()
  892. {
  893. $now = new \DateTime('2010-09-09 13:00:23');
  894. $result = $this->DateTime->render([
  895. 'name' => 'date',
  896. 'year' => false,
  897. 'month' => false,
  898. 'day' => false,
  899. 'hour' => false,
  900. 'minute' => [
  901. 'data-foo' => 'test',
  902. ],
  903. 'empty' => '-',
  904. 'default' => '',
  905. 'second' => false,
  906. 'val' => $now,
  907. ], $this->context);
  908. $this->assertContains('<select name="date[minute]" data-foo="test">', $result);
  909. $this->assertContains(
  910. '<option value="">-</option>',
  911. $result,
  912. 'contains empty option -'
  913. );
  914. $this->assertContains(
  915. '<option value="00" selected="selected">00</option>',
  916. $result,
  917. 'selected value present and correct at 00'
  918. );
  919. $this->assertContains(
  920. '<option value="05">05</option>',
  921. $result,
  922. 'contains 05'
  923. );
  924. $this->assertContains(
  925. '<option value="25">25</option>',
  926. $result,
  927. 'contains 25'
  928. );
  929. $this->assertContains(
  930. '<option value="59">59</option>',
  931. $result,
  932. 'contains 59'
  933. );
  934. $this->assertNotContains(
  935. '<option value="" selected="selected">-</option>',
  936. $result,
  937. 'No 0 value as empty value'
  938. );
  939. $this->assertNotContains('value="0"', $result, 'No unpadded 0 value');
  940. $this->assertNotContains('value="60"', $result, 'No 60 value');
  941. }
  942. /**
  943. * Test minutes with interval values.
  944. *
  945. * @return void
  946. */
  947. public function testRenderMinuteWidgetInterval()
  948. {
  949. $now = new \DateTime('2010-09-09 13:23:00');
  950. $result = $this->DateTime->render([
  951. 'name' => 'date',
  952. 'year' => false,
  953. 'month' => false,
  954. 'day' => false,
  955. 'hour' => false,
  956. 'minute' => [
  957. 'interval' => 5,
  958. ],
  959. 'second' => false,
  960. 'val' => $now,
  961. ], $this->context);
  962. $this->assertContains('<select name="date[minute]">', $result);
  963. $this->assertContains(
  964. '<option value="00">00</option>',
  965. $result,
  966. 'contains 00'
  967. );
  968. $this->assertContains(
  969. '<option value="05">05</option>',
  970. $result,
  971. 'contains 05'
  972. );
  973. $this->assertContains(
  974. '<option value="25" selected="selected">25</option>',
  975. $result,
  976. 'selected value present'
  977. );
  978. $this->assertContains(
  979. '<option value="55">55</option>',
  980. $result,
  981. 'contains 55'
  982. );
  983. $this->assertNotContains('value="2"', $result, 'No 2 value');
  984. $this->assertNotContains('value="23"', $result, 'No 23 value');
  985. $this->assertNotContains('value="58"', $result, 'No 58 value');
  986. $this->assertNotContains('value="59"', $result, 'No 59 value');
  987. $this->assertNotContains('value="60"', $result, 'No 60 value');
  988. }
  989. /**
  990. * Test rounding up and down.
  991. *
  992. * @return void
  993. */
  994. public function testRenderMinuteWidgetIntervalRounding()
  995. {
  996. $now = new \DateTime('2010-09-09 13:22:00');
  997. $data = [
  998. 'name' => 'date',
  999. 'year' => false,
  1000. 'month' => false,
  1001. 'day' => false,
  1002. 'hour' => false,
  1003. 'minute' => [
  1004. 'interval' => 5,
  1005. 'round' => 'up',
  1006. ],
  1007. 'second' => false,
  1008. 'val' => $now,
  1009. ];
  1010. $result = $this->DateTime->render($data, $this->context);
  1011. $this->assertContains(
  1012. '<option value="25" selected="selected">25</option>',
  1013. $result,
  1014. 'selected value present'
  1015. );
  1016. $this->assertNotContains('value="23"', $result, 'No 23 value');
  1017. $data['minute']['round'] = 'down';
  1018. $result = $this->DateTime->render($data, $this->context);
  1019. $this->assertContains(
  1020. '<option value="20" selected="selected">20</option>',
  1021. $result,
  1022. 'selected value present'
  1023. );
  1024. $this->assertNotContains('value="23"', $result, 'No 23 value');
  1025. }
  1026. /**
  1027. * Test that minute interval rounding can effect hours and days.
  1028. *
  1029. * @return void
  1030. */
  1031. public function testMinuteIntervalHourRollover()
  1032. {
  1033. $now = new \DateTime('2010-09-09 23:58:00');
  1034. $result = $this->DateTime->render([
  1035. 'name' => 'date',
  1036. 'year' => false,
  1037. 'month' => false,
  1038. 'minute' => [
  1039. 'interval' => 5,
  1040. 'round' => 'up',
  1041. ],
  1042. 'second' => false,
  1043. 'val' => $now,
  1044. ], $this->context);
  1045. $this->assertContains(
  1046. '<option value="00" selected="selected">00</option>',
  1047. $result,
  1048. 'selected minute present'
  1049. );
  1050. $this->assertContains(
  1051. '<option value="10" selected="selected">10</option>',
  1052. $result,
  1053. 'selected day present'
  1054. );
  1055. }
  1056. /**
  1057. * Test render seconds basic.
  1058. *
  1059. * @return void
  1060. */
  1061. public function testRenderSecondsWidget()
  1062. {
  1063. $now = new \DateTime('2010-09-09 13:00:25');
  1064. $result = $this->DateTime->render([
  1065. 'name' => 'date',
  1066. 'year' => false,
  1067. 'month' => false,
  1068. 'day' => false,
  1069. 'hour' => false,
  1070. 'minute' => false,
  1071. 'second' => [
  1072. 'data-foo' => 'test',
  1073. ],
  1074. 'val' => $now,
  1075. ], $this->context);
  1076. $this->assertContains('<select name="date[second]" data-foo="test">', $result);
  1077. $this->assertContains(
  1078. '<option value="00">00</option>',
  1079. $result,
  1080. 'contains 00'
  1081. );
  1082. $this->assertContains(
  1083. '<option value="01">01</option>',
  1084. $result,
  1085. 'contains 01'
  1086. );
  1087. $this->assertContains(
  1088. '<option value="05">05</option>',
  1089. $result,
  1090. 'contains 05'
  1091. );
  1092. $this->assertContains(
  1093. '<option value="25" selected="selected">25</option>',
  1094. $result,
  1095. 'selected value present'
  1096. );
  1097. $this->assertContains(
  1098. '<option value="59">59</option>',
  1099. $result,
  1100. 'contains 59'
  1101. );
  1102. $this->assertNotContains('value="0"', $result, 'No unpadded zero value');
  1103. $this->assertNotContains('value="60"', $result, 'No 60 value');
  1104. }
  1105. /**
  1106. * Test the merdian select.
  1107. *
  1108. * @return void
  1109. */
  1110. public function testRenderMeridianWidget()
  1111. {
  1112. $now = new \DateTime('2010-09-09 13:00:25');
  1113. $result = $this->DateTime->render([
  1114. 'name' => 'date',
  1115. 'year' => false,
  1116. 'month' => false,
  1117. 'day' => false,
  1118. 'hour' => false,
  1119. 'minute' => false,
  1120. 'second' => false,
  1121. 'meridian' => [],
  1122. 'val' => $now,
  1123. ], $this->context);
  1124. $expected = [
  1125. 'select' => ['name' => 'date[meridian]'],
  1126. ['option' => ['value' => 'am']], 'am', '/option',
  1127. ['option' => ['value' => 'pm', 'selected' => 'selected']], 'pm', '/option',
  1128. '/select',
  1129. ];
  1130. $this->assertHtml($expected, $result);
  1131. $now = new \DateTime('2010-09-09 09:00:25');
  1132. $result = $this->DateTime->render([
  1133. 'name' => 'date',
  1134. 'year' => false,
  1135. 'month' => false,
  1136. 'day' => false,
  1137. 'hour' => false,
  1138. 'minute' => false,
  1139. 'second' => false,
  1140. 'meridian' => [],
  1141. 'val' => $now,
  1142. ], $this->context);
  1143. $expected = [
  1144. 'select' => ['name' => 'date[meridian]'],
  1145. ['option' => ['value' => 'am', 'selected' => 'selected']], 'am', '/option',
  1146. ['option' => ['value' => 'pm']], 'pm', '/option',
  1147. '/select',
  1148. ];
  1149. $this->assertHtml($expected, $result);
  1150. }
  1151. /**
  1152. * Test rendering with templateVars
  1153. *
  1154. * @return void
  1155. */
  1156. public function testRenderTemplateVars()
  1157. {
  1158. $templates = [
  1159. 'select' => '<select data-s="{{svar}}" name="{{name}}"{{attrs}}>{{content}}</select>',
  1160. 'option' => '<option data-o="{{ovar}}" value="{{value}}"{{attrs}}>{{text}}</option>',
  1161. 'optgroup' => '<optgroup label="{{label}}"{{attrs}}>{{content}}</optgroup>',
  1162. 'dateWidget' => '{{year}}{{month}}{{day}}{{hour}}{{minute}}{{second}}{{meridian}}{{help}}',
  1163. ];
  1164. $this->templates->add($templates);
  1165. $result = $this->DateTime->render([
  1166. 'name' => 'date',
  1167. 'year' => [
  1168. 'templateVars' => ['ovar' => 'not-default'],
  1169. ],
  1170. 'month' => [
  1171. 'names' => true,
  1172. ],
  1173. 'hour' => false,
  1174. 'minute' => false,
  1175. 'second' => false,
  1176. 'meridian' => [],
  1177. 'templateVars' => [
  1178. 'svar' => 's-val',
  1179. 'ovar' => 'o-val',
  1180. 'help' => 'some help',
  1181. ],
  1182. ], $this->context);
  1183. $this->assertContains('<option data-o="not-default" value="2021">2021</option>', $result);
  1184. $this->assertContains('<option data-o="o-val" value="01">January</option>', $result);
  1185. $this->assertContains('<select data-s="s-val" name="date[year]">', $result);
  1186. $this->assertContains('<select data-s="s-val" name="date[month]">', $result);
  1187. $this->assertContains('</select>some help', $result);
  1188. }
  1189. /**
  1190. * Test that secureFields omits removed selects
  1191. *
  1192. * @return void
  1193. */
  1194. public function testSecureFields()
  1195. {
  1196. $data = [
  1197. 'name' => 'date',
  1198. ];
  1199. $result = $this->DateTime->secureFields($data);
  1200. $expected = [
  1201. 'date[year]', 'date[month]', 'date[day]',
  1202. 'date[hour]', 'date[minute]', 'date[second]',
  1203. ];
  1204. $this->assertEquals($expected, $result, 'No meridian on 24hr input');
  1205. $data = [
  1206. 'name' => 'date',
  1207. 'hour' => ['format' => 24],
  1208. ];
  1209. $result = $this->DateTime->secureFields($data);
  1210. $this->assertEquals($expected, $result, 'No meridian on 24hr input');
  1211. $data = [
  1212. 'name' => 'date',
  1213. 'year' => false,
  1214. 'month' => false,
  1215. 'day' => false,
  1216. 'hour' => [
  1217. 'format' => 12,
  1218. 'data-foo' => 'test',
  1219. ],
  1220. 'minute' => false,
  1221. 'second' => false,
  1222. ];
  1223. $result = $this->DateTime->secureFields($data);
  1224. $expected = [
  1225. 'date[hour]', 'date[meridian]',
  1226. ];
  1227. $this->assertEquals($expected, $result);
  1228. }
  1229. }