Permalink
Browse files

Fix FormHelper year range for ranges outside of 1901-2038

  • Loading branch information...
dereuromark committed Mar 5, 2013
1 parent e3b870d commit a07c9f0c3c475503841aef2744ed0ae42ebe5189
Showing with 75 additions and 7 deletions.
  1. +67 −0 lib/Cake/Test/Case/View/Helper/FormHelperTest.php
  2. +8 −7 lib/Cake/View/Helper/FormHelper.php
@@ -5845,6 +5845,23 @@ public function testMonth() {
'*/select',
);
$this->assertTags($result, $expected);
$this->Form->request->data['Project']['release'] = '2050-02-10';
$result = $this->Form->month('Project.release');
$expected = array(
array('select' => array('name' => 'data[Project][release][month]', 'id' => 'ProjectReleaseMonth')),
array('option' => array('value' => '')),
'/option',
array('option' => array('value' => '01')),
'January',
'/option',
array('option' => array('value' => '02', 'selected' => 'selected')),
'February',
'/option',
'*/select',
);
$this->assertTags($result, $expected);
}
/**
@@ -5933,6 +5950,28 @@ public function testDay() {
'/select',
);
$this->assertTags($result, $expected);
$this->Form->request->data['Project']['release'] = '2050-10-10';
$result = $this->Form->day('Project.release');
$expected = array(
array('select' => array('name' => 'data[Project][release][day]', 'id' => 'ProjectReleaseDay')),
array('option' => array('value' => '')),
'/option',
array('option' => array('value' => '01')),
'1',
'/option',
array('option' => array('value' => '02')),
'2',
'/option',
$daysRegex,
array('option' => array('value' => '10', 'selected' => 'selected')),
'10',
'/option',
$daysRegex,
'/select',
);
$this->assertTags($result, $expected);
}
/**
@@ -6103,6 +6142,26 @@ public function testHour() {
$thisHour = date('H');
$optValue = date('G');
$this->assertRegExp('/<option value="' . $thisHour . '" selected="selected">' . $optValue . '<\/option>/', $result);
$this->Form->request->data['Model']['field'] = '2050-10-10 01:12:32';
$result = $this->Form->hour('Model.field', true);
$expected = array(
array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
array('option' => array('value' => '')),
'/option',
array('option' => array('value' => '00')),
'0',
'/option',
array('option' => array('value' => '01', 'selected' => 'selected')),
'1',
'/option',
array('option' => array('value' => '02')),
'2',
'/option',
$hoursRegex,
'/select',
);
$this->assertTags($result, $expected);
}
/**
@@ -6308,6 +6367,14 @@ public function testYearAutoExpandRange() {
$result = $matches[1];
$expected = range(2050, date('Y') - 20);
$this->assertEquals($result, $expected);
$this->Form->request->data['Project']['release'] = '1881-10-10';
$result = $this->Form->year('Project.release', 1890, 1900);
preg_match_all('/<option value="([\d]+)"/', $result, $matches);
$result = $matches[1];
$expected = range(1900, 1881);
$this->assertEquals($result, $expected);
}
/**
@@ -2077,7 +2077,7 @@ public function day($fieldName = null, $attributes = array()) {
$attributes = $this->_dateTimeSelected('day', $fieldName, $attributes);
if (strlen($attributes['value']) > 2) {
$attributes['value'] = date('d', strtotime($attributes['value']));
$attributes['value'] = date_create($attributes['value'])->format('d');
} elseif ($attributes['value'] === false) {
$attributes['value'] = null;
}
@@ -2123,7 +2123,7 @@ public function year($fieldName, $minYear = null, $maxYear = null, $attributes =
}
if (strlen($attributes['value']) > 4 || $attributes['value'] === 'now') {
$attributes['value'] = date('Y', strtotime($attributes['value']));
$attributes['value'] = date_create($attributes['value'])->format('Y');
} elseif ($attributes['value'] === false) {
$attributes['value'] = null;
}
@@ -2159,7 +2159,7 @@ public function month($fieldName, $attributes = array()) {
$attributes = $this->_dateTimeSelected('month', $fieldName, $attributes);
if (strlen($attributes['value']) > 2) {
$attributes['value'] = date('m', strtotime($attributes['value']));
$attributes['value'] = date_create($attributes['value'])->format('m');
} elseif ($attributes['value'] === false) {
$attributes['value'] = null;
}
@@ -2195,10 +2195,11 @@ public function hour($fieldName, $format24Hours = false, $attributes = array())
$attributes = $this->_dateTimeSelected('hour', $fieldName, $attributes);
if (strlen($attributes['value']) > 2) {
$Date = new DateTime($attributes['value']);
if ($format24Hours) {
$attributes['value'] = date('H', strtotime($attributes['value']));
$attributes['value'] = $Date->format('H');
} else {
$attributes['value'] = date('g', strtotime($attributes['value']));
$attributes['value'] = $Date->format('g');
}
} elseif ($attributes['value'] === false) {
$attributes['value'] = null;
@@ -2234,7 +2235,7 @@ public function minute($fieldName, $attributes = array()) {
$attributes = $this->_dateTimeSelected('min', $fieldName, $attributes);
if (strlen($attributes['value']) > 2) {
$attributes['value'] = date('i', strtotime($attributes['value']));
$attributes['value'] = date_create($attributes['value'])->format('i');
} elseif ($attributes['value'] === false) {
$attributes['value'] = null;
}
@@ -2301,7 +2302,7 @@ public function meridian($fieldName, $attributes = array()) {
$attributes['value'] = date('a');
}
} else {
$attributes['value'] = date('a', strtotime($value));
$attributes['value'] = date_create($attributes['value'])->format('a');
}
}
}

0 comments on commit a07c9f0

Please sign in to comment.