Permalink
Browse files

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.
  • Loading branch information...
1 parent d53039f commit c8782a26e8f740fcd6e8bb0f9ea22bf7b5c3d266 @markstory markstory committed Jan 22, 2014
Showing with 58 additions and 1 deletion.
  1. +3 −0 src/View/Input/DateTime.php
  2. +55 −1 tests/TestCase/View/Input/DateTimeTest.php
@@ -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']);
}
@@ -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() {

0 comments on commit c8782a2

Please sign in to comment.