Skip to content
Permalink
Browse files

Change how options are used in Datetime widget and expand tests.

Stick with Florian's original idea of having separate option sets for
each input. This allows additional HTML attributes to be passed through
to the various input methods, and removes the need to prefix each
option. Doing this will make it easier to build an abstraction in
FormHelper that provides a more backwards compatible interface
implementing dateFormat and timeFormat.
  • Loading branch information...
markstory committed Jan 22, 2014
1 parent e4121d2 commit 2045dcfb954b1a4e771e253c33acda5d09ba8e68
Showing with 84 additions and 33 deletions.
  1. +19 −20 src/View/Input/DateTime.php
  2. +65 −13 tests/TestCase/View/Input/DateTimeTest.php
@@ -79,25 +79,21 @@ 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.
* @throws \RuntimeException when the name attribute is empty.
*/
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,
@@ -43,7 +43,7 @@ public function setUp() {
'selectMultiple' => '<select name="{{name}}[]" multiple="multiple"{{attrs}}>{{content}}</select>',
'option' => '<option value="{{value}}"{{attrs}}>{{text}}</option>',
'optgroup' => '<optgroup label="{{label}}"{{attrs}}>{{content}}</optgroup>',
'dateWidget' => '<div{{attrs}}>{{year}}-{{month}}-{{day}} {{hour}}:{{minute}}:{{second}}</div>'
'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('<option value="" selected="selected">YEAR</option>', $result);
$this->assertContains('<option value="" selected="selected">MONTH</option>', $result);
@@ -109,12 +106,67 @@ public function testRenderEmptyValues() {
$this->assertContains('<option value="" selected="selected">SECOND</option>', $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 = '<option value="%s" selected="selected">%s</option>';
$this->assertContains(sprintf($format, $year, $year), $result);
$format = '<option value="%s">%s</option>';
$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() {

0 comments on commit 2045dcf

Please sign in to comment.
You can’t perform that action at this time.