Skip to content

Commit

Permalink
Implement year range extension.
Browse files Browse the repository at this point in the history
When the selected value is outside of the year bounds, the year range
should be extended to include the selected value.
  • Loading branch information
markstory committed Jan 26, 2014
1 parent d53039f commit c8782a2
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 1 deletion.
3 changes: 3 additions & 0 deletions src/View/Input/DateTime.php
Expand Up @@ -179,6 +179,9 @@ public function yearSelect($options = []) {
'options' => []
];

$options['start'] = min($options['val'], $options['start']);
$options['end'] = max($options['val'], $options['end']);

if (empty($options['options'])) {
$options['options'] = $this->_generateNumbers($options['start'], $options['end']);
}
Expand Down
56 changes: 55 additions & 1 deletion tests/TestCase/View/Input/DateTimeTest.php
Expand Up @@ -193,8 +193,62 @@ public function testRenderYearWidgetOrdering() {
$this->assertTags($result, $expected);
}

/**
* Test that a selected value outside of the chosen
* year boundary is also included as an option.
*
* @return void
*/
public function testRenderYearWidgetValueOutOfBounds() {
$this->markTestIncomplete();
$now = new \DateTime('2010-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,
]);
$expected = [
'select' => ['name' => 'date[year]'],
['option' => ['value' => '2010', 'selected' => 'selected']], '2010', '/option',
['option' => ['value' => '2011']], '2011', '/option',
['option' => ['value' => '2012']], '2012', '/option',
['option' => ['value' => '2013']], '2013', '/option',
['option' => ['value' => '2014']], '2014', '/option',
['option' => ['value' => '2015']], '2015', '/option',
'/select',
];
$this->assertTags($result, $expected);

$now = new \DateTime('2013-01-01 12:00:00');
$result = $this->DateTime->render([
'name' => 'date',
'year' => [
'start' => 2010,
'end' => 2011,
],
'month' => false,
'day' => false,
'hour' => false,
'minute' => false,
'second' => false,
'val' => $now,
]);
$expected = [
'select' => ['name' => 'date[year]'],
['option' => ['value' => '2010']], '2010', '/option',
['option' => ['value' => '2011']], '2011', '/option',
['option' => ['value' => '2012']], '2012', '/option',
['option' => ['value' => '2013', 'selected' => 'selected']], '2013', '/option',
'/select',
];
$this->assertTags($result, $expected);
}

public function testRenderMonthWidget() {
Expand Down

0 comments on commit c8782a2

Please sign in to comment.