mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 14:05:37 +02:00
Fix tests related to date time
This commit is contained in:
parent
9d7eef7cf3
commit
dba1f61f13
@ -10,12 +10,14 @@ use SilverStripe\ORM\FieldType\DBDatetime;
|
||||
/**
|
||||
* Form field used for editing date time string
|
||||
*
|
||||
* # Localization
|
||||
*
|
||||
* See {@link DateField}
|
||||
*
|
||||
* # Configuration
|
||||
*
|
||||
* - "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.
|
||||
* - "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
|
||||
{
|
||||
@ -162,6 +164,7 @@ class DatetimeField extends TextField
|
||||
|
||||
// Parse from submitted value
|
||||
$this->value = $this->localisedToISO8601($value);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
@ -222,7 +225,7 @@ class DatetimeField extends TextField
|
||||
// Browsers expect ISO 8601 dates, localisation is handled on the client
|
||||
$formatter->setPattern(DBDatetime::ISO_DATETIME);
|
||||
} 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);
|
||||
if (!$ok) {
|
||||
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
|
||||
*/
|
||||
public function getDateFormat()
|
||||
public function getDatetimeFormat()
|
||||
{
|
||||
if ($this->getHTML5()) {
|
||||
// Browsers expect ISO 8601 dates, localisation is handled on the client
|
||||
@ -557,7 +560,7 @@ class DatetimeField extends TextField
|
||||
_t(
|
||||
'DateField.VALIDDATEFORMAT2',
|
||||
"Please enter a valid date format ({format})",
|
||||
['format' => $this->getDateFormat()]
|
||||
['format' => $this->getDatetimeFormat()]
|
||||
)
|
||||
);
|
||||
return false;
|
||||
@ -636,24 +639,4 @@ class DatetimeField extends TextField
|
||||
$this->timezone = $timezone;
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
@ -132,32 +132,18 @@ class DBDatetime extends DBDate implements TemplateGlobalProvider
|
||||
public function scaffoldFormField($title = null, $params = null)
|
||||
{
|
||||
$field = DatetimeField::create($this->name, $title);
|
||||
$dateFormat = $field->getDateField()->getDateFormat();
|
||||
$timeFormat = $field->getTimeField()->getTimeFormat();
|
||||
$dateTimeFormat = $field->getDatetimeFormat();
|
||||
|
||||
// Set date formatting hints and example
|
||||
$date = static::now()->Format($dateFormat);
|
||||
$date = static::now()->Format($dateTimeFormat);
|
||||
$field
|
||||
->getDateField()
|
||||
->setDescription(_t(
|
||||
'FormField.EXAMPLE',
|
||||
'e.g. {format}',
|
||||
'Example format',
|
||||
[ 'format' => $date ]
|
||||
))
|
||||
->setAttribute('placeholder', $dateFormat);
|
||||
|
||||
// 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);
|
||||
->setAttribute('placeholder', $dateTimeFormat);
|
||||
|
||||
return $field;
|
||||
}
|
||||
|
@ -37,26 +37,18 @@ class DatetimeFieldTest extends SapphireTest
|
||||
$form = $this->getMockForm();
|
||||
$form->Fields()->push($dateTimeField);
|
||||
|
||||
$dateTimeField->setSubmittedValue([
|
||||
'date' => '2003-03-29',
|
||||
'time' => '23:59:38'
|
||||
]);
|
||||
$dateTimeField->setSubmittedValue('2003-03-29T23:59:38');
|
||||
$validator = new RequiredFields();
|
||||
$this->assertTrue($dateTimeField->validate($validator));
|
||||
$m = new Model();
|
||||
$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()
|
||||
{
|
||||
$dateTimeField = new DatetimeField('MyDatetime');
|
||||
|
||||
$dateTimeField->getDateField()
|
||||
->setHTML5(false)
|
||||
->setLocale('en_NZ');
|
||||
|
||||
$dateTimeField->getTimeField()
|
||||
$dateTimeField
|
||||
->setHTML5(false)
|
||||
->setLocale('en_NZ');
|
||||
|
||||
@ -64,15 +56,12 @@ class DatetimeFieldTest extends SapphireTest
|
||||
$form->Fields()->push($dateTimeField);
|
||||
|
||||
// en_NZ standard format
|
||||
$dateTimeField->setSubmittedValue([
|
||||
'date' => '29/03/2003',
|
||||
'time' => '11:59:38 pm'
|
||||
]);
|
||||
$dateTimeField->setSubmittedValue('29/03/2003 11:59:38 pm');
|
||||
$validator = new RequiredFields();
|
||||
$this->assertTrue($dateTimeField->validate($validator));
|
||||
$m = new Model();
|
||||
$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()
|
||||
@ -119,33 +108,23 @@ class DatetimeFieldTest extends SapphireTest
|
||||
$this->assertEquals($f->dataValue(), '2003-03-29 23:59:38');
|
||||
}
|
||||
|
||||
public function testSetValueWithArray()
|
||||
public function testSetValue()
|
||||
{
|
||||
$datetimeField = new DatetimeField('Datetime', 'Datetime');
|
||||
$datetimeField->setSubmittedValue([
|
||||
'date' => '2003-03-29',
|
||||
'time' => '23:00:00'
|
||||
]);
|
||||
$datetimeField->setSubmittedValue('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->getDateField()
|
||||
->setHTML5(false)
|
||||
->setLocale('en_NZ');
|
||||
|
||||
$datetimeField->getTimeField()
|
||||
$datetimeField
|
||||
->setHTML5(false)
|
||||
->setLocale('en_NZ');
|
||||
|
||||
// Values can only be localized (= non-ISO) in array notation
|
||||
$datetimeField->setSubmittedValue([
|
||||
'date' => '29/03/2003',
|
||||
'time' => '11:00:00 pm'
|
||||
]);
|
||||
$datetimeField->setSubmittedValue('29/03/2003 11:00:00 pm');
|
||||
$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
|
||||
$datetimeField = new DatetimeField('Datetime', 'Datetime');
|
||||
|
||||
$datetimeField->getDateField()
|
||||
->setHTML5(false)
|
||||
->setLocale('en_NZ');
|
||||
|
||||
$datetimeField->getTimeField()
|
||||
$datetimeField
|
||||
->setHTML5(false)
|
||||
->setLocale('en_NZ');
|
||||
|
||||
@ -182,8 +157,7 @@ class DatetimeFieldTest extends SapphireTest
|
||||
$datetimeField->Value(),
|
||||
'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(
|
||||
'2003-12-24 23:59:59',
|
||||
$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.
|
||||
$datetimeField = new DatetimeField('Datetime', 'Datetime');
|
||||
|
||||
$datetimeField->getDateField()
|
||||
->setHTML5(false)
|
||||
->setLocale('en_NZ');
|
||||
|
||||
$datetimeField->getTimeField()
|
||||
$datetimeField
|
||||
->setHTML5(false)
|
||||
->setLocale('en_NZ');
|
||||
|
||||
$datetimeField->setTimezone('Europe/Moscow');
|
||||
$datetimeField->setSubmittedValue([
|
||||
// pass in default format, at user time (Moscow)
|
||||
'date' => '24/06/2003',
|
||||
'time' => '11:59:59 pm',
|
||||
]);
|
||||
// pass in default format, at user time (Moscow)
|
||||
$datetimeField->setSubmittedValue('24/06/2003 11:59:59 pm');
|
||||
$this->assertTrue($datetimeField->validate(new RequiredFields()));
|
||||
$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()
|
||||
{
|
||||
$field = new DatetimeField('Datetime');
|
||||
|
||||
$this->assertEquals('Datetime', $field->getName());
|
||||
$this->assertEquals('Datetime[date]', $field->getDateField()->getName());
|
||||
$this->assertEquals('Datetime[time]', $field->getTimeField()->getName());
|
||||
}
|
||||
|
||||
public function testSetName()
|
||||
@ -279,8 +194,6 @@ class DatetimeFieldTest extends SapphireTest
|
||||
$field = new DatetimeField('Datetime', 'Datetime');
|
||||
$field->setName('CustomDatetime');
|
||||
$this->assertEquals('CustomDatetime', $field->getName());
|
||||
$this->assertEquals('CustomDatetime[date]', $field->getDateField()->getName());
|
||||
$this->assertEquals('CustomDatetime[time]', $field->getTimeField()->getName());
|
||||
}
|
||||
|
||||
protected function getMockForm()
|
||||
|
@ -462,6 +462,8 @@ class FormSchemaTest extends SapphireTest
|
||||
'extraClass' => 'date text',
|
||||
'description' => null,
|
||||
'rightTitle' => null,
|
||||
'html5' => true,
|
||||
'lang' => 'en-US',
|
||||
'leftTitle' => null,
|
||||
'readOnly' => false,
|
||||
'disabled' => false,
|
||||
|
@ -29,7 +29,7 @@ class DBDatetimeTest extends SapphireTest
|
||||
public function testNowWithMockDate()
|
||||
{
|
||||
// Test setting
|
||||
$mockDate = '2001-12-31 22:10:59';
|
||||
$mockDate = '2001-12-31T22:10:59';
|
||||
DBDatetime::set_mock_now($mockDate);
|
||||
$systemDatetime = DBDatetime::create_field('Datetime', date('Y-m-d H:i:s'));
|
||||
$nowDatetime = DBDatetime::now();
|
||||
@ -55,10 +55,10 @@ class DBDatetimeTest extends SapphireTest
|
||||
$this->assertNull($date->getValue(), 'Boolean FALSE evaluates to NULL');
|
||||
|
||||
$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);
|
||||
$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()
|
||||
@ -98,7 +98,7 @@ class DBDatetimeTest extends SapphireTest
|
||||
public function testURLDateTime()
|
||||
{
|
||||
$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()
|
||||
|
Loading…
Reference in New Issue
Block a user