Skip to content

Commit

Permalink
Fix selected values in some cases.
Browse files Browse the repository at this point in the history
When second is missing, or when the meridian is present array values
need to be adjusted.
  • Loading branch information
markstory committed Feb 23, 2014
1 parent 8390040 commit 6d8062d
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 17 deletions.
7 changes: 7 additions & 0 deletions src/View/Widget/DateTime.php
Expand Up @@ -196,6 +196,13 @@ protected function _deconstructDate($value, $options) {
if (isset($value['year'], $value['month'], $value['day'])) {
$date->setDate($value['year'], $value['month'], $value['day']);
}
if (!isset($value['second'])) {
$value['second'] = 0;
}
if (isset($value['meridian'])) {
$isAm = strtolower($value['meridian']) === 'am';
$value['hour'] = $isAm ? $value['hour'] : $value['hour'] + 12;
}
if (isset($value['hour'], $value['minute'], $value['second'])) {
$date->setTime($value['hour'], $value['minute'], $value['second']);
}
Expand Down
30 changes: 13 additions & 17 deletions tests/TestCase/View/Helper/FormHelperTest.php
Expand Up @@ -5237,35 +5237,38 @@ public function testDatetimeMinuteInterval() {
extract($this->dateRegex);
$now = strtotime('now');

$result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('interval' => 5, 'value' => ''));
$result = $this->Form->dateTime('Contact.date', 'DMY', '12', array(
'interval' => 5,
'value' => ''
));
$expected = array(
array('select' => array('name' => 'Contact[date][day]')),
$daysRegex,
array('option' => array('value' => '')),
array('select' => array('name' => 'Contact[date][month]')),
$monthsRegex,
array('option' => array('selected' => 'selected', 'value' => '')),
'/option',
'*/select',

array('select' => array('name' => 'Contact[date][month]')),
$monthsRegex,
array('option' => array('value' => '')),
array('select' => array('name' => 'Contact[date][day]')),
$daysRegex,
array('option' => array('selected' => 'selected', 'value' => '')),
'/option',
'*/select',

array('select' => array('name' => 'Contact[date][year]')),
$yearsRegex,
array('option' => array('value' => '')),
array('option' => array('selected' => 'selected', 'value' => '')),
'/option',
'*/select',

array('select' => array('name' => 'Contact[date][hour]')),
$hoursRegex,
array('option' => array('value' => '')),
array('option' => array('selected' => 'selected', 'value' => '')),
'/option',
'*/select',

array('select' => array('name' => 'Contact[date][minute]')),
$minutesRegex,
array('option' => array('value' => '')),
array('option' => array('selected' => 'selected', 'value' => '')),
'/option',
array('option' => array('value' => '00')),
'00',
Expand All @@ -5277,15 +5280,8 @@ public function testDatetimeMinuteInterval() {
'10',
'/option',
'*/select',

array('select' => array('name' => 'Contact[date][meridian]')),
$meridianRegex,
array('option' => array('value' => '')),
'/option',
'*/select'
);
$this->assertTags($result, $expected);
$this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
}

/**
Expand Down
35 changes: 35 additions & 0 deletions tests/TestCase/View/Widget/DateTimeTest.php
Expand Up @@ -108,6 +108,41 @@ public function testRenderSelected($selected) {
$this->assertContains('<option value="45" selected="selected">45</option>', $result);
}

/**
* Test that render() works with an array for val that is missing seconds.
*
* @return void
*/
public function testRenderSelectedNoSeconds() {
$selected = [
'year' => '2014', 'month' => '01', 'day' => '20',
'hour' => '12', 'minute' => '30'
];
$result = $this->DateTime->render(['val' => $selected]);
$this->assertContains('<option value="2014" selected="selected">2014</option>', $result);
$this->assertContains('<option value="01" selected="selected">1</option>', $result);
$this->assertContains('<option value="20" selected="selected">20</option>', $result);
$this->assertContains('<option value="12" selected="selected">12</option>', $result);
$this->assertContains('<option value="30" selected="selected">30</option>', $result);
}

/**
* Test that render() adjusts hours based on meridian
*
* @return void
*/
public function testRenderSelectedMeridian() {
$selected = [
'year' => '2014', 'month' => '01', 'day' => '20',
'hour' => '7', 'minute' => '30', 'meridian' => 'pm'
];
$result = $this->DateTime->render(['val' => $selected]);
$this->assertContains('<option value="2014" selected="selected">2014</option>', $result);
$this->assertContains('<option value="01" selected="selected">1</option>', $result);
$this->assertContains('<option value="20" selected="selected">20</option>', $result);
$this->assertContains('<option value="19" selected="selected">19</option>', $result);
}

/**
* Test rendering widgets with empty values.
*
Expand Down

0 comments on commit 6d8062d

Please sign in to comment.