DateTimeWidgetTest.php 43 KB

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