'',
'option' => '',
'optgroup' => '',
'dateWidget' => '{{year}}{{month}}{{day}}{{hour}}{{minute}}{{second}}{{meridian}}'
];
$this->templates = new StringTemplate($templates);
$this->context = $this->getMock('Cake\View\Form\ContextInterface');
$this->selectBox = new SelectBoxWidget($this->templates);
$this->DateTime = new DateTimeWidget($this->templates, $this->selectBox);
}
/**
* Data provider for testing various types of invalid selected values.
*
* @return array
*/
public static function invalidSelectedValuesProvider() {
return [
'null' => null,
'false' => false,
'true' => true,
'string' => ['Bag of poop'],
'int' => [-1],
'array' => [[
'derp' => 'hurt'
]]
];
}
/**
* test rendering selected values.
*
* @dataProvider selectedValuesProvider
* @return void
*/
public function testRenderSelectedInvalid($selected) {
$result = $this->DateTime->render(['val' => $selected], $this->context);
$now = new \DateTime();
$format = '';
$this->assertContains(
sprintf($format, $now->format('Y'), $now->format('Y')),
$result
);
}
/**
* Data provider for testing various acceptable selected values.
*
* @return array
*/
public static function selectedValuesProvider() {
$date = new \DateTime('2014-01-20 12:30:45');
return [
'DateTime' => [$date],
'string' => [$date->format('Y-m-d H:i:s')],
'int' => [$date->getTimestamp()],
'array' => [[
'year' => '2014', 'month' => '01', 'day' => '20',
'hour' => '12', 'minute' => '30', 'second' => '45',
]]
];
}
/**
* test rendering selected values.
*
* @dataProvider selectedValuesProvider
* @return void
*/
public function testRenderSelected($selected) {
$result = $this->DateTime->render(['val' => $selected], $this->context);
$this->assertContains('', $result);
$this->assertContains('', $result);
$this->assertContains('', $result);
$this->assertContains('', $result);
$this->assertContains('', $result);
$this->assertContains('', $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(['name' => 'created', 'val' => $selected], $this->context);
$this->assertContains('name="created[year]"', $result);
$this->assertContains('', $result);
$this->assertContains('name="created[month]"', $result);
$this->assertContains('', $result);
$this->assertContains('name="created[day]"', $result);
$this->assertContains('', $result);
$this->assertContains('name="created[hour]"', $result);
$this->assertContains('', $result);
$this->assertContains('name="created[minute]"', $result);
$this->assertContains('', $result);
$this->assertContains('name="created[second]"', $result);
$this->assertContains('', $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->context);
$this->assertContains('', $result);
$this->assertContains('', $result);
$this->assertContains('', $result);
$this->assertContains('', $result);
}
/**
* Test rendering widgets with empty values.
*
* @retun void
*/
public function testRenderEmptyValues() {
$result = $this->DateTime->render([
'year' => ['empty' => 'YEAR'],
'month' => ['empty' => 'MONTH'],
'day' => ['empty' => 'DAY'],
'hour' => ['empty' => 'HOUR'],
'minute' => ['empty' => 'MINUTE'],
'second' => ['empty' => 'SECOND'],
'meridian' => ['empty' => 'MERIDIAN'],
], $this->context);
$this->assertContains('', $result);
$this->assertContains('', $result);
$this->assertContains('', $result);
$this->assertContains('', $result);
$this->assertContains('', $result);
$this->assertContains('', $result);
$this->assertContains('', $result);
}
/**
* Test rendering the default year widget.
*
* @return void
*/
public function testRenderYearWidgetDefaultRange() {
$now = new \DateTime();
$result = $this->DateTime->render([
'month' => false,
'day' => false,
'hour' => false,
'minute' => false,
'second' => false,
'val' => $now,
], $this->context);
$year = $now->format('Y');
$format = '';
$this->assertContains(sprintf($format, $year, $year), $result);
$format = '';
$maxYear = $now->format('Y') + 5;
$minYear = $now->format('Y') - 5;
$this->assertContains(sprintf($format, $maxYear, $maxYear), $result);
$this->assertContains(sprintf($format, $minYear, $minYear), $result);
$nope = $now->format('Y') + 6;
$this->assertNotContains(sprintf($format, $nope, $nope), $result);
$nope = $now->format('Y') - 6;
$this->assertNotContains(sprintf($format, $nope, $nope), $result);
}
/**
* Test ordering of year options.
*
* @return void
*/
public function testRenderYearWidgetOrdering() {
$now = new \DateTime('2014-01-01 12:00:00');
$result = $this->DateTime->render([
'name' => 'date',
'year' => [
'start' => 2013,
'end' => 2015,
'data-foo' => 'test',
'order' => 'asc',
],
'month' => false,
'day' => false,
'hour' => false,
'minute' => false,
'second' => false,
'val' => $now,
'orderYear' => 'asc',
], $this->context);
$expected = [
'select' => ['name' => 'date[year]', 'data-foo' => 'test'],
['option' => ['value' => '2013']], '2013', '/option',
['option' => ['value' => '2014', 'selected' => 'selected']], '2014', '/option',
['option' => ['value' => '2015']], '2015', '/option',
'/select',
];
$this->assertHtml($expected, $result);
$result = $this->DateTime->render([
'name' => 'date',
'year' => [
'start' => 2013,
'end' => 2015,
'order' => 'desc'
],
'month' => false,
'day' => false,
'hour' => false,
'minute' => false,
'second' => false,
'val' => $now,
], $this->context);
$expected = [
'select' => ['name' => 'date[year]'],
['option' => ['value' => '2015']], '2015', '/option',
['option' => ['value' => '2014', 'selected' => 'selected']], '2014', '/option',
['option' => ['value' => '2013']], '2013', '/option',
'/select',
];
$this->assertHtml($expected, $result);
}
/**
* Test that a selected value outside of the chosen
* year boundary is also included as an option.
*
* @return void
*/
public function testRenderYearWidgetValueOutOfBounds() {
$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,
], $this->context);
$expected = [
'select' => ['name' => 'date[year]'],
['option' => ['value' => '2015']], '2015', '/option',
['option' => ['value' => '2014']], '2014', '/option',
['option' => ['value' => '2013']], '2013', '/option',
['option' => ['value' => '2012']], '2012', '/option',
['option' => ['value' => '2011']], '2011', '/option',
['option' => ['value' => '2010', 'selected' => 'selected']], '2010', '/option',
'/select',
];
$this->assertHtml($expected, $result);
$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,
], $this->context);
$expected = [
'select' => ['name' => 'date[year]'],
['option' => ['value' => '2013', 'selected' => 'selected']], '2013', '/option',
['option' => ['value' => '2012']], '2012', '/option',
['option' => ['value' => '2011']], '2011', '/option',
['option' => ['value' => '2010']], '2010', '/option',
'/select',
];
$this->assertHtml($expected, $result);
}
/**
* Test rendering the month widget
*
* @return void
*/
public function testRenderMonthWidget() {
$now = new \DateTime('2010-09-01 12:00:00');
$result = $this->DateTime->render([
'name' => 'date',
'year' => false,
'day' => false,
'hour' => false,
'minute' => false,
'second' => false,
'val' => $now,
], $this->context);
$expected = [
'select' => ['name' => 'date[month]'],
['option' => ['value' => '01']], '1', '/option',
['option' => ['value' => '02']], '2', '/option',
['option' => ['value' => '03']], '3', '/option',
['option' => ['value' => '04']], '4', '/option',
['option' => ['value' => '05']], '5', '/option',
['option' => ['value' => '06']], '6', '/option',
['option' => ['value' => '07']], '7', '/option',
['option' => ['value' => '08']], '8', '/option',
['option' => ['value' => '09', 'selected' => 'selected']], '9', '/option',
['option' => ['value' => '10']], '10', '/option',
['option' => ['value' => '11']], '11', '/option',
['option' => ['value' => '12']], '12', '/option',
'/select',
];
$this->assertHtml($expected, $result);
}
/**
* Test rendering month widget with names.
*
* @return void
*/
public function testRenderMonthWidgetWithNames() {
$now = new \DateTime('2010-09-01 12:00:00');
$result = $this->DateTime->render([
'name' => 'date',
'year' => false,
'day' => false,
'hour' => false,
'minute' => false,
'second' => false,
'month' => ['data-foo' => 'test', 'names' => true],
'val' => $now,
], $this->context);
$expected = [
'select' => ['name' => 'date[month]', 'data-foo' => 'test'],
['option' => ['value' => '01']], 'January', '/option',
['option' => ['value' => '02']], 'February', '/option',
['option' => ['value' => '03']], 'March', '/option',
['option' => ['value' => '04']], 'April', '/option',
['option' => ['value' => '05']], 'May', '/option',
['option' => ['value' => '06']], 'June', '/option',
['option' => ['value' => '07']], 'July', '/option',
['option' => ['value' => '08']], 'August', '/option',
['option' => ['value' => '09', 'selected' => 'selected']], 'September', '/option',
['option' => ['value' => '10']], 'October', '/option',
['option' => ['value' => '11']], 'November', '/option',
['option' => ['value' => '12']], 'December', '/option',
'/select',
];
$this->assertHtml($expected, $result);
}
/**
* Test rendering month widget with custom names.
*
* @return void
*/
public function testRenderMonthWidgetWithCustomNames() {
$now = new \DateTime('2010-09-01 12:00:00');
$result = $this->DateTime->render([
'name' => 'date',
'year' => false,
'day' => false,
'hour' => false,
'minute' => false,
'second' => false,
'month' => [
'names' => ['01' => 'Jan', '02' => 'Feb']
],
'val' => $now,
], $this->context);
$expected = [
'select' => ['name' => 'date[month]'],
['option' => ['value' => '01']], 'Jan', '/option',
['option' => ['value' => '02']], 'Feb', '/option',
'/select',
];
$this->assertHtml($expected, $result);
}
/**
* Test rendering the day widget.
*
* @return void
*/
public function testRenderDayWidget() {
$now = new \DateTime('2010-09-09 12:00:00');
$result = $this->DateTime->render([
'name' => 'date',
'year' => false,
'month' => false,
'day' => [
'data-foo' => 'test',
],
'hour' => false,
'minute' => false,
'second' => false,
'val' => $now,
], $this->context);
$expected = [
'select' => ['name' => 'date[day]', 'data-foo' => 'test'],
['option' => ['value' => '01']], '1', '/option',
['option' => ['value' => '02']], '2', '/option',
['option' => ['value' => '03']], '3', '/option',
['option' => ['value' => '04']], '4', '/option',
['option' => ['value' => '05']], '5', '/option',
['option' => ['value' => '06']], '6', '/option',
['option' => ['value' => '07']], '7', '/option',
['option' => ['value' => '08']], '8', '/option',
['option' => ['value' => '09', 'selected' => 'selected']], '9', '/option',
['option' => ['value' => '10']], '10', '/option',
['option' => ['value' => '11']], '11', '/option',
['option' => ['value' => '12']], '12', '/option',
['option' => ['value' => '13']], '13', '/option',
['option' => ['value' => '14']], '14', '/option',
['option' => ['value' => '15']], '15', '/option',
['option' => ['value' => '16']], '16', '/option',
['option' => ['value' => '17']], '17', '/option',
['option' => ['value' => '18']], '18', '/option',
['option' => ['value' => '19']], '19', '/option',
['option' => ['value' => '20']], '20', '/option',
['option' => ['value' => '21']], '21', '/option',
['option' => ['value' => '22']], '22', '/option',
['option' => ['value' => '23']], '23', '/option',
['option' => ['value' => '24']], '24', '/option',
['option' => ['value' => '25']], '25', '/option',
['option' => ['value' => '26']], '26', '/option',
['option' => ['value' => '27']], '27', '/option',
['option' => ['value' => '28']], '28', '/option',
['option' => ['value' => '29']], '29', '/option',
['option' => ['value' => '30']], '30', '/option',
['option' => ['value' => '31']], '31', '/option',
'/select',
];
$this->assertHtml($expected, $result);
}
/**
* Test rendering the hour picker in 24 hour mode.
*
* @return void
*/
public function testRenderHourWidget24StartAndEnd() {
$now = new \DateTime('2010-09-09 13:00:00');
$result = $this->DateTime->render([
'name' => 'date',
'year' => false,
'month' => false,
'day' => false,
'hour' => [
'start' => 8,
'end' => 16
],
'minute' => false,
'second' => false,
'val' => $now,
], $this->context);
$this->assertContains('