diff --git a/src/View/Input/DateTime.php b/src/View/Input/DateTime.php index 19ee3b4e1f2..b25e2ef63cf 100644 --- a/src/View/Input/DateTime.php +++ b/src/View/Input/DateTime.php @@ -79,7 +79,7 @@ public function __construct($templates, $selectBox) { * - `day` - Array of options for the day select box. * - `hour` - Array of options for the hour select box. * - `minute` - Array of options for the minute select box. - * - `second` - Array of options for the second select box. + * - `second` - Set to true to enable the seconds input. Defaults to false. * * @param array $data Data to render with. * @return string A generated select box. @@ -87,17 +87,13 @@ public function __construct($templates, $selectBox) { */ public function render(array $data) { $data += [ - 'name' => 'data', + 'name' => '', 'empty' => false, 'disabled' => null, 'val' => null, 'year' => [], - 'month' => [ - 'names' => false, - ], - 'day' => [ - 'names' => false, - ], + 'month' => [], + 'day' => [], 'hour' => [], 'minute' => [], 'second' => [], @@ -112,14 +108,13 @@ public function render(array $data) { $data[$select]['name'] = $data['name'] . "[" . $select . "]"; $data[$select]['val'] = $selected[$select]; - if (is_bool($data['empty'])) { + if (!isset($data[$select]['empty'])) { $data[$select]['empty'] = $data['empty']; } - if (isset($data['empty'][$select])) { - $data[$select]['empty'] = $data['empty'][$select]; + if (!isset($data[$select]['disabled'])) { + $data[$select]['disabled'] = $data['disabled']; } - $data[$select]['disabled'] = $data['disabled']; - $data[$select] += $data[$select]; + $templateOptions[$select] = $this->{$method}($data[$select]); } unset($data[$select]); @@ -176,17 +171,21 @@ protected function _deconstuctDate($value) { */ public function yearSelect($options = []) { $options += [ - 'name' => 'data[year]', + 'name' => '', 'val' => null, 'start' => date('Y', strtotime('-5 years')), 'end' => date('Y', strtotime('+5 years')), + 'order' => 'desc', 'options' => [] ]; if (empty($options['options'])) { $options['options'] = $this->_generateNumbers($options['start'], $options['end']); } - + if ($options['order'] === 'asc') { + $options['options'] = array_reverse($options['options'], true); + } + unset($options['start'], $options['end'], $options['order']); return $this->_select->render($options); } @@ -198,7 +197,7 @@ public function yearSelect($options = []) { */ public function monthSelect($options = []) { $options += [ - 'name' => 'data[month]', + 'name' => '', 'names' => false, 'val' => null, 'leadingZeroKey' => true, @@ -225,7 +224,7 @@ public function monthSelect($options = []) { */ public function daySelect($options = []) { $options += [ - 'name' => 'data[day]', + 'name' => '', 'val' => null, 'leadingZeroKey' => true, 'leadingZeroValue' => true, @@ -244,7 +243,7 @@ public function daySelect($options = []) { */ public function hourSelect($options = []) { $options += [ - 'name' => 'data[hour]', + 'name' => '', 'val' => null, 'leadingZeroKey' => true, 'leadingZeroValue' => true, @@ -263,7 +262,7 @@ public function hourSelect($options = []) { */ public function minuteSelect($options = []) { $options += [ - 'name' => 'data[minute]', + 'name' => '', 'val' => null, 'leadingZeroKey' => true, 'leadingZeroValue' => true, @@ -282,7 +281,7 @@ public function minuteSelect($options = []) { */ public function secondSelect($options = []) { $options += [ - 'name' => 'data[second]', + 'name' => '', 'val' => null, 'leadingZeroKey' => true, 'leadingZeroValue' => true, diff --git a/tests/TestCase/View/Input/DateTimeTest.php b/tests/TestCase/View/Input/DateTimeTest.php index 8eaa7845b28..38a768c5a98 100644 --- a/tests/TestCase/View/Input/DateTimeTest.php +++ b/tests/TestCase/View/Input/DateTimeTest.php @@ -43,7 +43,7 @@ public function setUp() { 'selectMultiple' => '', 'option' => '', 'optgroup' => '{{content}}', - 'dateWidget' => '{{year}}-{{month}}-{{day}} {{hour}}:{{minute}}:{{second}}' + 'dateWidget' => '{{year}}-{{month}}-{{day}} {{hour}}:{{minute}}:{{second}}' ]; $this->templates = new StringTemplate($templates); $this->selectBox = new SelectBox($this->templates); @@ -91,15 +91,12 @@ public function testRenderSelected($selected) { */ public function testRenderEmptyValues() { $result = $this->DateTime->render([ - 'empty' => [ - 'year' => 'YEAR', - 'month' => 'MONTH', - 'day' => 'DAY', - 'hour' => 'HOUR', - 'minute' => 'MINUTE', - 'second' => 'SECOND', - 'meridian' => 'MERIDIAN', - ] + 'year' => ['empty' => 'YEAR'], + 'month' => ['empty' => 'MONTH'], + 'day' => ['empty' => 'DAY'], + 'hour' => ['empty' => 'HOUR'], + 'minute' => ['empty' => 'MINUTE'], + 'second' => ['empty' => 'SECOND'], ]); $this->assertContains('', $result); $this->assertContains('', $result); @@ -109,12 +106,67 @@ public function testRenderEmptyValues() { $this->assertContains('', $result); } - public function testRenderYearWidget() { - $this->markTestIncomplete(); +/** + * Test rendering the default year widget. + * + * @return void + */ + public function testRenderYearWidgetDefaultRange() { + $now = new \DateTime(); + $result = $this->DateTime->render([ + 'month' => false, + 'day' => false, + 'hour' => false, + 'minute' => false, + 'second' => false, + 'val' => $now, + ]); + $year = $now->format('Y'); + $format = ''; + $this->assertContains(sprintf($format, $year, $year), $result); + + $format = ''; + $maxYear = $now->format('Y') + 5; + $minYear = $now->format('Y') - 5; + $this->assertContains(sprintf($format, $maxYear, $maxYear), $result); + $this->assertContains(sprintf($format, $minYear, $minYear), $result); + + $nope = $now->format('Y') + 6; + $this->assertNotContains(sprintf($format, $nope, $nope), $result); + + $nope = $now->format('Y') - 6; + $this->assertNotContains(sprintf($format, $nope, $nope), $result); } +/** + * Test ordering of year options. + * + * @return void + */ public function testRenderYearWidgetOrdering() { - $this->markTestIncomplete(); + $now = new \DateTime('2014-01-01 12:00:00'); + $result = $this->DateTime->render([ + 'name' => 'date', + 'year' => [ + 'start' => 2013, + 'end' => 2015, + ], + 'month' => false, + 'day' => false, + 'hour' => false, + 'minute' => false, + 'second' => false, + 'val' => $now, + 'orderYear' => 'asc', + ]); + $expected = [ + 'select' => ['name' => 'date[year]'], + ['option' => ['value' => '2013']], '2013', '/option', + ['option' => ['value' => '2014', 'selected' => 'selected']], '2014', '/option', + ['option' => ['value' => '2015']], '2015', '/option', + '/select', + ]; + $this->assertTags($result, $expected); } public function testRenderYearWidgetMinAndMax() {