Fix tests related to date time

This commit is contained in:
Saophalkun Ponlu 2017-04-20 15:08:44 +12:00 committed by Ingo Schommer
parent 9d7eef7cf3
commit dba1f61f13
5 changed files with 32 additions and 148 deletions

View File

@ -10,12 +10,14 @@ use SilverStripe\ORM\FieldType\DBDatetime;
/** /**
* Form field used for editing date time string * Form field used for editing date time string
* *
* # Localization
*
* See {@link DateField}
*
* # Configuration * # Configuration
* *
* - "timezone": Set a different timezone for viewing. {@link dataValue()} will still save * - "timezone": Set a different timezone for viewing. {@link dataValue()} will still save
* the time in PHP's default timezone (date_default_timezone_get()), its only a view setting. * the time in PHP's default timezone (date_default_timezone_get()), its only a view setting.
* - "datetimeorder": An sprintf() template to determine in which order the date and time values will
* be combined. This is necessary as those separate formats are set in their invididual fields.
*/ */
class DatetimeField extends TextField class DatetimeField extends TextField
{ {
@ -162,6 +164,7 @@ class DatetimeField extends TextField
// Parse from submitted value // Parse from submitted value
$this->value = $this->localisedToISO8601($value); $this->value = $this->localisedToISO8601($value);
return $this; return $this;
} }
@ -222,7 +225,7 @@ class DatetimeField extends TextField
// Browsers expect ISO 8601 dates, localisation is handled on the client // Browsers expect ISO 8601 dates, localisation is handled on the client
$formatter->setPattern(DBDatetime::ISO_DATETIME); $formatter->setPattern(DBDatetime::ISO_DATETIME);
} elseif ($this->datetimeFormat) { } elseif ($this->datetimeFormat) {
// Don't invoke getDateFormat() directly to avoid infinite loop // Don't invoke getDatetimeFormat() directly to avoid infinite loop
$ok = $formatter->setPattern($this->datetimeFormat); $ok = $formatter->setPattern($this->datetimeFormat);
if (!$ok) { if (!$ok) {
throw new InvalidArgumentException("Invalid date format {$this->datetimeFormat}"); throw new InvalidArgumentException("Invalid date format {$this->datetimeFormat}");
@ -239,7 +242,7 @@ class DatetimeField extends TextField
* *
* @see http://userguide.icu-project.org/formatparse/datetime#TOC-Date-Field-Symbol-Table * @see http://userguide.icu-project.org/formatparse/datetime#TOC-Date-Field-Symbol-Table
*/ */
public function getDateFormat() public function getDatetimeFormat()
{ {
if ($this->getHTML5()) { if ($this->getHTML5()) {
// Browsers expect ISO 8601 dates, localisation is handled on the client // Browsers expect ISO 8601 dates, localisation is handled on the client
@ -557,7 +560,7 @@ class DatetimeField extends TextField
_t( _t(
'DateField.VALIDDATEFORMAT2', 'DateField.VALIDDATEFORMAT2',
"Please enter a valid date format ({format})", "Please enter a valid date format ({format})",
['format' => $this->getDateFormat()] ['format' => $this->getDatetimeFormat()]
) )
); );
return false; return false;
@ -636,24 +639,4 @@ class DatetimeField extends TextField
$this->timezone = $timezone; $this->timezone = $timezone;
return $this; return $this;
} }
/**
* @return string
*/
public function getDateTimeOrder()
{
return $this->dateTimeOrder;
}
/**
* Set date time order format string. Use {date} and {time} as placeholders.
*
* @param string $dateTimeOrder
* @return $this
*/
public function setDateTimeOrder($dateTimeOrder)
{
$this->dateTimeOrder = $dateTimeOrder;
return $this;
}
} }

View File

@ -132,32 +132,18 @@ class DBDatetime extends DBDate implements TemplateGlobalProvider
public function scaffoldFormField($title = null, $params = null) public function scaffoldFormField($title = null, $params = null)
{ {
$field = DatetimeField::create($this->name, $title); $field = DatetimeField::create($this->name, $title);
$dateFormat = $field->getDateField()->getDateFormat(); $dateTimeFormat = $field->getDatetimeFormat();
$timeFormat = $field->getTimeField()->getTimeFormat();
// Set date formatting hints and example // Set date formatting hints and example
$date = static::now()->Format($dateFormat); $date = static::now()->Format($dateTimeFormat);
$field $field
->getDateField()
->setDescription(_t( ->setDescription(_t(
'FormField.EXAMPLE', 'FormField.EXAMPLE',
'e.g. {format}', 'e.g. {format}',
'Example format', 'Example format',
[ 'format' => $date ] [ 'format' => $date ]
)) ))
->setAttribute('placeholder', $dateFormat); ->setAttribute('placeholder', $dateTimeFormat);
// Set time formatting hints and example
$time = static::now()->Format($timeFormat);
$field
->getTimeField()
->setDescription(_t(
'FormField.EXAMPLE',
'e.g. {format}',
'Example format',
[ 'format' => $time ]
))
->setAttribute('placeholder', $timeFormat);
return $field; return $field;
} }

View File

@ -37,26 +37,18 @@ class DatetimeFieldTest extends SapphireTest
$form = $this->getMockForm(); $form = $this->getMockForm();
$form->Fields()->push($dateTimeField); $form->Fields()->push($dateTimeField);
$dateTimeField->setSubmittedValue([ $dateTimeField->setSubmittedValue('2003-03-29T23:59:38');
'date' => '2003-03-29',
'time' => '23:59:38'
]);
$validator = new RequiredFields(); $validator = new RequiredFields();
$this->assertTrue($dateTimeField->validate($validator)); $this->assertTrue($dateTimeField->validate($validator));
$m = new Model(); $m = new Model();
$form->saveInto($m); $form->saveInto($m);
$this->assertEquals('2003-03-29 23:59:38', $m->MyDatetime); $this->assertEquals('2003-03-29T23:59:38', $m->MyDatetime);
} }
public function testFormSaveIntoLocalised() public function testFormSaveIntoLocalised()
{ {
$dateTimeField = new DatetimeField('MyDatetime'); $dateTimeField = new DatetimeField('MyDatetime');
$dateTimeField
$dateTimeField->getDateField()
->setHTML5(false)
->setLocale('en_NZ');
$dateTimeField->getTimeField()
->setHTML5(false) ->setHTML5(false)
->setLocale('en_NZ'); ->setLocale('en_NZ');
@ -64,15 +56,12 @@ class DatetimeFieldTest extends SapphireTest
$form->Fields()->push($dateTimeField); $form->Fields()->push($dateTimeField);
// en_NZ standard format // en_NZ standard format
$dateTimeField->setSubmittedValue([ $dateTimeField->setSubmittedValue('29/03/2003 11:59:38 pm');
'date' => '29/03/2003',
'time' => '11:59:38 pm'
]);
$validator = new RequiredFields(); $validator = new RequiredFields();
$this->assertTrue($dateTimeField->validate($validator)); $this->assertTrue($dateTimeField->validate($validator));
$m = new Model(); $m = new Model();
$form->saveInto($m); $form->saveInto($m);
$this->assertEquals('2003-03-29 23:59:38', $m->MyDatetime); $this->assertEquals('2003-03-29T23:59:38', $m->MyDatetime);
} }
public function testDataValue() public function testDataValue()
@ -119,33 +108,23 @@ class DatetimeFieldTest extends SapphireTest
$this->assertEquals($f->dataValue(), '2003-03-29 23:59:38'); $this->assertEquals($f->dataValue(), '2003-03-29 23:59:38');
} }
public function testSetValueWithArray() public function testSetValue()
{ {
$datetimeField = new DatetimeField('Datetime', 'Datetime'); $datetimeField = new DatetimeField('Datetime', 'Datetime');
$datetimeField->setSubmittedValue([ $datetimeField->setSubmittedValue('2003-03-29 23:00:00');
'date' => '2003-03-29',
'time' => '23:00:00'
]);
$this->assertEquals($datetimeField->dataValue(), '2003-03-29 23:00:00'); $this->assertEquals($datetimeField->dataValue(), '2003-03-29 23:00:00');
} }
public function testSetValueWithArrayLocalised() public function testSetValueWithLocalised()
{ {
$datetimeField = new DatetimeField('Datetime', 'Datetime'); $datetimeField = new DatetimeField('Datetime', 'Datetime');
$datetimeField->getDateField() $datetimeField
->setHTML5(false)
->setLocale('en_NZ');
$datetimeField->getTimeField()
->setHTML5(false) ->setHTML5(false)
->setLocale('en_NZ'); ->setLocale('en_NZ');
// Values can only be localized (= non-ISO) in array notation // Values can only be localized (= non-ISO) in array notation
$datetimeField->setSubmittedValue([ $datetimeField->setSubmittedValue('29/03/2003 11:00:00 pm');
'date' => '29/03/2003',
'time' => '11:00:00 pm'
]);
$this->assertEquals($datetimeField->dataValue(), '2003-03-29 23:00:00'); $this->assertEquals($datetimeField->dataValue(), '2003-03-29 23:00:00');
} }
@ -167,11 +146,7 @@ class DatetimeFieldTest extends SapphireTest
// Berlin and Auckland have 12h time difference in northern hemisphere winter // Berlin and Auckland have 12h time difference in northern hemisphere winter
$datetimeField = new DatetimeField('Datetime', 'Datetime'); $datetimeField = new DatetimeField('Datetime', 'Datetime');
$datetimeField->getDateField() $datetimeField
->setHTML5(false)
->setLocale('en_NZ');
$datetimeField->getTimeField()
->setHTML5(false) ->setHTML5(false)
->setLocale('en_NZ'); ->setLocale('en_NZ');
@ -182,8 +157,7 @@ class DatetimeFieldTest extends SapphireTest
$datetimeField->Value(), $datetimeField->Value(),
'User value is formatted, and in user timezone' 'User value is formatted, and in user timezone'
); );
$this->assertEquals('25/12/2003', $datetimeField->getDateField()->Value());
$this->assertEquals('11:59:59 AM', $datetimeField->getTimeField()->Value());
$this->assertEquals( $this->assertEquals(
'2003-12-24 23:59:59', '2003-12-24 23:59:59',
$datetimeField->dataValue(), $datetimeField->dataValue(),
@ -197,81 +171,22 @@ class DatetimeFieldTest extends SapphireTest
// Berlin and Auckland have 12h time difference in northern hemisphere summer, but Berlin and Moscow only 2h. // Berlin and Auckland have 12h time difference in northern hemisphere summer, but Berlin and Moscow only 2h.
$datetimeField = new DatetimeField('Datetime', 'Datetime'); $datetimeField = new DatetimeField('Datetime', 'Datetime');
$datetimeField->getDateField() $datetimeField
->setHTML5(false)
->setLocale('en_NZ');
$datetimeField->getTimeField()
->setHTML5(false) ->setHTML5(false)
->setLocale('en_NZ'); ->setLocale('en_NZ');
$datetimeField->setTimezone('Europe/Moscow'); $datetimeField->setTimezone('Europe/Moscow');
$datetimeField->setSubmittedValue([ // pass in default format, at user time (Moscow)
// pass in default format, at user time (Moscow) $datetimeField->setSubmittedValue('24/06/2003 11:59:59 pm');
'date' => '24/06/2003',
'time' => '11:59:59 pm',
]);
$this->assertTrue($datetimeField->validate(new RequiredFields())); $this->assertTrue($datetimeField->validate(new RequiredFields()));
$this->assertEquals('2003-06-24 21:59:59', $datetimeField->dataValue(), 'Data value matches server timezone'); $this->assertEquals('2003-06-24 21:59:59', $datetimeField->dataValue(), 'Data value matches server timezone');
} }
public function testSetDateField()
{
$form = $this->getMockForm();
$field = new DatetimeField('Datetime', 'Datetime');
$field->setForm($form);
$field->setSubmittedValue([
'date' => '2003-06-24',
'time' => '23:59:59',
]);
$dateField = new DateField('Datetime[date]');
$field->setDateField($dateField);
$this->assertEquals(
$dateField->getForm(),
$form,
'Sets form on new field'
);
$this->assertEquals(
'2003-06-24',
$dateField->dataValue(),
'Sets existing value on new field'
);
}
public function testSetTimeField()
{
$form = $this->getMockForm();
$field = new DatetimeField('Datetime', 'Datetime');
$field->setForm($form);
$field->setSubmittedValue([
'date' => '2003-06-24',
'time' => '23:59:59',
]);
$timeField = new TimeField('Datetime[time]');
$field->setTimeField($timeField);
$this->assertEquals(
$timeField->getForm(),
$form,
'Sets form on new field'
);
$this->assertEquals(
'23:59:59',
$timeField->dataValue(),
'Sets existing value on new field'
);
}
public function testGetName() public function testGetName()
{ {
$field = new DatetimeField('Datetime'); $field = new DatetimeField('Datetime');
$this->assertEquals('Datetime', $field->getName()); $this->assertEquals('Datetime', $field->getName());
$this->assertEquals('Datetime[date]', $field->getDateField()->getName());
$this->assertEquals('Datetime[time]', $field->getTimeField()->getName());
} }
public function testSetName() public function testSetName()
@ -279,8 +194,6 @@ class DatetimeFieldTest extends SapphireTest
$field = new DatetimeField('Datetime', 'Datetime'); $field = new DatetimeField('Datetime', 'Datetime');
$field->setName('CustomDatetime'); $field->setName('CustomDatetime');
$this->assertEquals('CustomDatetime', $field->getName()); $this->assertEquals('CustomDatetime', $field->getName());
$this->assertEquals('CustomDatetime[date]', $field->getDateField()->getName());
$this->assertEquals('CustomDatetime[time]', $field->getTimeField()->getName());
} }
protected function getMockForm() protected function getMockForm()

View File

@ -462,6 +462,8 @@ class FormSchemaTest extends SapphireTest
'extraClass' => 'date text', 'extraClass' => 'date text',
'description' => null, 'description' => null,
'rightTitle' => null, 'rightTitle' => null,
'html5' => true,
'lang' => 'en-US',
'leftTitle' => null, 'leftTitle' => null,
'readOnly' => false, 'readOnly' => false,
'disabled' => false, 'disabled' => false,

View File

@ -29,7 +29,7 @@ class DBDatetimeTest extends SapphireTest
public function testNowWithMockDate() public function testNowWithMockDate()
{ {
// Test setting // Test setting
$mockDate = '2001-12-31 22:10:59'; $mockDate = '2001-12-31T22:10:59';
DBDatetime::set_mock_now($mockDate); DBDatetime::set_mock_now($mockDate);
$systemDatetime = DBDatetime::create_field('Datetime', date('Y-m-d H:i:s')); $systemDatetime = DBDatetime::create_field('Datetime', date('Y-m-d H:i:s'));
$nowDatetime = DBDatetime::now(); $nowDatetime = DBDatetime::now();
@ -55,10 +55,10 @@ class DBDatetimeTest extends SapphireTest
$this->assertNull($date->getValue(), 'Boolean FALSE evaluates to NULL'); $this->assertNull($date->getValue(), 'Boolean FALSE evaluates to NULL');
$date = DBDatetime::create_field('Datetime', '0'); $date = DBDatetime::create_field('Datetime', '0');
$this->assertEquals('1970-01-01 00:00:00', $date->getValue(), 'String zero is UNIX epoch time'); $this->assertEquals('1970-01-01T00:00:00', $date->getValue(), 'String zero is UNIX epoch time');
$date = DBDatetime::create_field('Datetime', 0); $date = DBDatetime::create_field('Datetime', 0);
$this->assertEquals('1970-01-01 00:00:00', $date->getValue(), 'Numeric zero is UNIX epoch time'); $this->assertEquals('1970-01-01T00:00:00', $date->getValue(), 'Numeric zero is UNIX epoch time');
} }
public function testExtendedDateTimes() public function testExtendedDateTimes()
@ -98,7 +98,7 @@ class DBDatetimeTest extends SapphireTest
public function testURLDateTime() public function testURLDateTime()
{ {
$date = DBDatetime::create_field('Datetime', '2001-12-31 22:10:59'); $date = DBDatetime::create_field('Datetime', '2001-12-31 22:10:59');
$this->assertEquals('2001-12-31%2022%3A10%3A59', $date->URLDateTime()); $this->assertEquals('2001-12-31T22%3A10%3A59', $date->URLDateTime());
} }
public function testAgoInPast() public function testAgoInPast()