DateTimeWidgetTest.php 35 KB

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