FEATURE New DatetimeField class (form field wrapper composed of DateField andTimeField)
FEATURE New DateField and TimeField form classes with more consistent API and easier localization
API CHANGE Date/time parsing in DateField, TimeField and DatetimeField defaults to i18n::get_locale() ('en_US') instead of using en_NZ/en_GB specific parsing. Use i18n::set_locale('en_NZ') in mysite/_config.php to revert to old behaviour.
API CHANGE constructor parameter in TimeField needs to be in ISO date notation (not PHP's date())
API CHANGE TimeField, DateField and related subclasses use Zend_Date for date parsing, meaning they're stricer than the previously used strtotime()
API CHANGE Removed DMYCalendarDateField and CalendarDateField, use DateField with setConfig('showcalendar')
API CHANGE Removed CompositeDateField, DMYDateField, use DateField with setConfig('dmyfields')
API CHANGE Removed DropdownTimeField, use TimeField with setConfig('showdropdown')
API CHANGE Removed PopupDateTimeField, use DatetimeField
API CHANGE Changed 'date', 'month' and 'year' HTML field names to lowercase in DMYDateField
API CHANGE Removed support for ambiguous date formats in DateField, e.g. '06/03/03'. Use DateField->setConfig('dateformat', '<format>') to revert to this behaviour.
API CHANGE Removed flag from DateField, CalendarDateField etc., use DateField->setConfig('min') and DateField->setConfig('max')
ENHANCEMENT Using Zend_Date for DateField and TimeField, with more robust date handling, starting localization support. Set globally via i18n::set_locale(), or for a field instance through setLocale(). Note: Javascript validation is not localized yet. (from r99360)
git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@102859 467b73ca-7a2a-4603-9d3b-597d59a354a9
2010-04-14 06:38:40 +02:00
|
|
|
<?php
|
2016-06-15 06:03:16 +02:00
|
|
|
|
2016-10-14 03:30:05 +02:00
|
|
|
namespace SilverStripe\Forms\Tests;
|
|
|
|
|
2016-08-19 00:51:35 +02:00
|
|
|
use SilverStripe\Dev\SapphireTest;
|
|
|
|
use SilverStripe\Control\Controller;
|
|
|
|
use SilverStripe\Forms\DatetimeField;
|
|
|
|
use SilverStripe\Forms\RequiredFields;
|
|
|
|
use SilverStripe\Forms\DateField;
|
2017-02-14 06:19:09 +01:00
|
|
|
use SilverStripe\Forms\SeparatedDateField;
|
2016-10-14 03:30:05 +02:00
|
|
|
use SilverStripe\Forms\Tests\DatetimeFieldTest\Model;
|
2016-08-19 00:51:35 +02:00
|
|
|
use SilverStripe\Forms\TimeField;
|
|
|
|
use SilverStripe\Forms\FieldList;
|
|
|
|
use SilverStripe\Forms\FormAction;
|
|
|
|
use SilverStripe\Forms\Form;
|
|
|
|
use SilverStripe\i18n\i18n;
|
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
class DatetimeFieldTest extends SapphireTest
|
|
|
|
{
|
2017-01-26 05:20:08 +01:00
|
|
|
protected $timezone = null;
|
2016-12-16 05:34:21 +01:00
|
|
|
|
|
|
|
public function setUp()
|
|
|
|
{
|
|
|
|
parent::setUp();
|
|
|
|
i18n::set_locale('en_NZ');
|
2017-01-26 05:20:08 +01:00
|
|
|
$this->timezone = date_default_timezone_get();
|
2016-12-16 05:34:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function tearDown()
|
|
|
|
{
|
2017-01-26 05:20:08 +01:00
|
|
|
date_default_timezone_set($this->timezone);
|
|
|
|
parent::tearDown(); // TODO: Change the autogenerated stub
|
2016-12-16 05:34:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testFormSaveInto()
|
|
|
|
{
|
2017-01-26 05:20:08 +01:00
|
|
|
$dateTimeField = new DatetimeField('MyDatetime');
|
2016-12-16 05:34:21 +01:00
|
|
|
$form = $this->getMockForm();
|
2017-01-26 05:20:08 +01:00
|
|
|
$form->Fields()->push($dateTimeField);
|
|
|
|
|
|
|
|
// en_NZ standard format
|
|
|
|
$dateTimeField->setSubmittedValue([
|
2016-12-16 05:34:21 +01:00
|
|
|
'date' => '29/03/2003',
|
2017-01-26 05:20:08 +01:00
|
|
|
'time' => '11:59:38 pm'
|
|
|
|
]);
|
|
|
|
$validator = new RequiredFields();
|
|
|
|
$this->assertTrue($dateTimeField->validate($validator));
|
2016-12-16 05:34:21 +01:00
|
|
|
$m = new Model();
|
|
|
|
$form->saveInto($m);
|
|
|
|
$this->assertEquals('2003-03-29 23:59:38', $m->MyDatetime);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testDataValue()
|
|
|
|
{
|
|
|
|
$f = new DatetimeField('Datetime');
|
|
|
|
$this->assertEquals(null, $f->dataValue(), 'Empty field');
|
|
|
|
|
|
|
|
$f = new DatetimeField('Datetime', null, '2003-03-29 23:59:38');
|
|
|
|
$this->assertEquals('2003-03-29 23:59:38', $f->dataValue(), 'From date/time string');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testConstructorWithoutArgs()
|
|
|
|
{
|
|
|
|
$f = new DatetimeField('Datetime');
|
|
|
|
$this->assertEquals($f->dataValue(), null);
|
|
|
|
}
|
|
|
|
|
|
|
|
// /**
|
|
|
|
// * @expectedException InvalidArgumentException
|
|
|
|
// */
|
|
|
|
// public function testConstructorWithLocalizedDateString() {
|
|
|
|
// $f = new DatetimeField('Datetime', 'Datetime', '29/03/2003 23:59:38');
|
|
|
|
// }
|
|
|
|
|
|
|
|
public function testConstructorWithIsoDate()
|
|
|
|
{
|
|
|
|
// used by Form->loadDataFrom()
|
|
|
|
$f = new DatetimeField('Datetime', 'Datetime', '2003-03-29 23:59:38');
|
|
|
|
$this->assertEquals($f->dataValue(), '2003-03-29 23:59:38');
|
|
|
|
}
|
|
|
|
|
|
|
|
// /**
|
|
|
|
// * @expectedException InvalidArgumentException
|
|
|
|
// */
|
|
|
|
// public function testSetValueWithDateString() {
|
|
|
|
// $f = new DatetimeField('Datetime', 'Datetime');
|
|
|
|
// $f->setValue('29/03/2003');
|
|
|
|
// }
|
|
|
|
|
|
|
|
public function testSetValueWithDateTimeString()
|
|
|
|
{
|
|
|
|
$f = new DatetimeField('Datetime', 'Datetime');
|
|
|
|
$f->setValue('2003-03-29 23:59:38');
|
|
|
|
$this->assertEquals($f->dataValue(), '2003-03-29 23:59:38');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testSetValueWithArray()
|
|
|
|
{
|
2017-01-26 05:20:08 +01:00
|
|
|
$datetimeField = new DatetimeField('Datetime', 'Datetime');
|
2016-12-16 05:34:21 +01:00
|
|
|
// Values can only be localized (= non-ISO) in array notation
|
2017-01-26 05:20:08 +01:00
|
|
|
$datetimeField->setSubmittedValue([
|
2016-12-16 05:34:21 +01:00
|
|
|
'date' => '29/03/2003',
|
2017-01-26 05:20:08 +01:00
|
|
|
'time' => '11:00:00 pm'
|
|
|
|
]);
|
|
|
|
$this->assertEquals($datetimeField->dataValue(), '2003-03-29 23:00:00');
|
2016-12-16 05:34:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testSetValueWithDmyArray()
|
|
|
|
{
|
|
|
|
$f = new DatetimeField('Datetime', 'Datetime');
|
2017-02-14 06:19:09 +01:00
|
|
|
$f->setDateField(new SeparatedDateField('Datetime[date]'));
|
2017-01-26 05:20:08 +01:00
|
|
|
$f->setSubmittedValue([
|
|
|
|
'date' => ['day' => 29, 'month' => 03, 'year' => 2003],
|
|
|
|
'time' => '11:00:00 pm'
|
|
|
|
]);
|
2016-12-16 05:34:21 +01:00
|
|
|
$this->assertEquals($f->dataValue(), '2003-03-29 23:00:00');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testValidate()
|
|
|
|
{
|
|
|
|
$f = new DatetimeField('Datetime', 'Datetime', '2003-03-29 23:59:38');
|
|
|
|
$this->assertTrue($f->validate(new RequiredFields()));
|
|
|
|
|
2017-01-26 05:20:08 +01:00
|
|
|
$f = new DatetimeField('Datetime', 'Datetime', '2003-03-29 00:00:00');
|
2016-12-16 05:34:21 +01:00
|
|
|
$this->assertTrue($f->validate(new RequiredFields()));
|
|
|
|
|
|
|
|
$f = new DatetimeField('Datetime', 'Datetime', 'wrong');
|
|
|
|
$this->assertFalse($f->validate(new RequiredFields()));
|
|
|
|
}
|
|
|
|
|
2017-01-26 05:20:08 +01:00
|
|
|
public function testTimezoneSet()
|
2016-12-16 05:34:21 +01:00
|
|
|
{
|
|
|
|
date_default_timezone_set('Europe/Berlin');
|
|
|
|
// Berlin and Auckland have 12h time difference in northern hemisphere winter
|
2017-01-26 05:20:08 +01:00
|
|
|
$datetimeField = new DatetimeField('Datetime', 'Datetime');
|
|
|
|
$datetimeField->setTimezone('Pacific/Auckland');
|
|
|
|
$datetimeField->setValue('2003-12-24 23:59:59');
|
2016-12-16 05:34:21 +01:00
|
|
|
$this->assertEquals(
|
2017-01-26 05:20:08 +01:00
|
|
|
'25/12/2003 11:59:59 AM',
|
|
|
|
$datetimeField->Value(),
|
2016-12-16 05:34:21 +01:00
|
|
|
'User value is formatted, and in user timezone'
|
|
|
|
);
|
2017-01-26 05:20:08 +01:00
|
|
|
$this->assertEquals('25/12/2003', $datetimeField->getDateField()->Value());
|
|
|
|
$this->assertEquals('11:59:59 AM', $datetimeField->getTimeField()->Value());
|
2016-12-16 05:34:21 +01:00
|
|
|
$this->assertEquals(
|
|
|
|
'2003-12-24 23:59:59',
|
2017-01-26 05:20:08 +01:00
|
|
|
$datetimeField->dataValue(),
|
2016-12-16 05:34:21 +01:00
|
|
|
'Data value is unformatted, and in server timezone'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testTimezoneFromConfig()
|
|
|
|
{
|
|
|
|
date_default_timezone_set('Europe/Berlin');
|
|
|
|
// Berlin and Auckland have 12h time difference in northern hemisphere summer, but Berlin and Moscow only 2h.
|
2017-01-26 05:20:08 +01:00
|
|
|
$datetimeField = new DatetimeField('Datetime', 'Datetime');
|
|
|
|
$datetimeField->setTimezone('Europe/Moscow');
|
|
|
|
$datetimeField->setSubmittedValue([
|
2016-12-16 05:34:21 +01:00
|
|
|
// pass in default format, at user time (Moscow)
|
|
|
|
'date' => '24/06/2003',
|
2017-01-26 05:20:08 +01:00
|
|
|
'time' => '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');
|
2016-12-16 05:34:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testSetDateField()
|
|
|
|
{
|
|
|
|
$form = $this->getMockForm();
|
|
|
|
$field = new DatetimeField('Datetime', 'Datetime');
|
|
|
|
$field->setForm($form);
|
2017-01-26 05:20:08 +01:00
|
|
|
$field->setSubmittedValue([
|
2016-12-16 05:34:21 +01:00
|
|
|
'date' => '24/06/2003',
|
|
|
|
'time' => '23:59:59',
|
2017-01-26 05:20:08 +01:00
|
|
|
]);
|
2016-12-16 05:34:21 +01:00
|
|
|
$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);
|
2017-01-26 05:20:08 +01:00
|
|
|
$field->setSubmittedValue([
|
2016-12-16 05:34:21 +01:00
|
|
|
'date' => '24/06/2003',
|
2017-01-26 05:20:08 +01:00
|
|
|
'time' => '11:59:59 pm',
|
|
|
|
]);
|
2016-12-16 05:34:21 +01:00
|
|
|
$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()
|
|
|
|
{
|
|
|
|
$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()
|
|
|
|
{
|
2017-01-26 05:20:08 +01:00
|
|
|
/** @skipUpgrade */
|
2016-12-16 05:34:21 +01:00
|
|
|
return new Form(
|
|
|
|
new Controller(),
|
|
|
|
'Form',
|
|
|
|
new FieldList(),
|
|
|
|
new FieldList(
|
|
|
|
new FormAction('doSubmit')
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
2010-10-15 05:48:39 +02:00
|
|
|
}
|