Skip to content

Commit c8782a2

Browse files
committed
Implement year range extension.
When the selected value is outside of the year bounds, the year range should be extended to include the selected value.
1 parent d53039f commit c8782a2

File tree

2 files changed

+58
-1
lines changed

2 files changed

+58
-1
lines changed

src/View/Input/DateTime.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,9 @@ public function yearSelect($options = []) {
179179
'options' => []
180180
];
181181

182+
$options['start'] = min($options['val'], $options['start']);
183+
$options['end'] = max($options['val'], $options['end']);
184+
182185
if (empty($options['options'])) {
183186
$options['options'] = $this->_generateNumbers($options['start'], $options['end']);
184187
}

tests/TestCase/View/Input/DateTimeTest.php

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,8 +193,62 @@ public function testRenderYearWidgetOrdering() {
193193
$this->assertTags($result, $expected);
194194
}
195195

196+
/**
197+
* Test that a selected value outside of the chosen
198+
* year boundary is also included as an option.
199+
*
200+
* @return void
201+
*/
196202
public function testRenderYearWidgetValueOutOfBounds() {
197-
$this->markTestIncomplete();
203+
$now = new \DateTime('2010-01-01 12:00:00');
204+
$result = $this->DateTime->render([
205+
'name' => 'date',
206+
'year' => [
207+
'start' => 2013,
208+
'end' => 2015,
209+
],
210+
'month' => false,
211+
'day' => false,
212+
'hour' => false,
213+
'minute' => false,
214+
'second' => false,
215+
'val' => $now,
216+
]);
217+
$expected = [
218+
'select' => ['name' => 'date[year]'],
219+
['option' => ['value' => '2010', 'selected' => 'selected']], '2010', '/option',
220+
['option' => ['value' => '2011']], '2011', '/option',
221+
['option' => ['value' => '2012']], '2012', '/option',
222+
['option' => ['value' => '2013']], '2013', '/option',
223+
['option' => ['value' => '2014']], '2014', '/option',
224+
['option' => ['value' => '2015']], '2015', '/option',
225+
'/select',
226+
];
227+
$this->assertTags($result, $expected);
228+
229+
$now = new \DateTime('2013-01-01 12:00:00');
230+
$result = $this->DateTime->render([
231+
'name' => 'date',
232+
'year' => [
233+
'start' => 2010,
234+
'end' => 2011,
235+
],
236+
'month' => false,
237+
'day' => false,
238+
'hour' => false,
239+
'minute' => false,
240+
'second' => false,
241+
'val' => $now,
242+
]);
243+
$expected = [
244+
'select' => ['name' => 'date[year]'],
245+
['option' => ['value' => '2010']], '2010', '/option',
246+
['option' => ['value' => '2011']], '2011', '/option',
247+
['option' => ['value' => '2012']], '2012', '/option',
248+
['option' => ['value' => '2013', 'selected' => 'selected']], '2013', '/option',
249+
'/select',
250+
];
251+
$this->assertTags($result, $expected);
198252
}
199253

200254
public function testRenderMonthWidget() {

0 commit comments

Comments
 (0)