silverstripe-framework/tests/forms/DatetimeFieldTest.php

126 lines
3.3 KiB
PHP
Raw Normal View History

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 $timeformat 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 $futureOnly 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. git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/branches/2.4@99360 467b73ca-7a2a-4603-9d3b-597d59a354a9
2010-02-17 00:10:09 +01:00
<?php
/**
* @package sapphire
* @subpackage tests
*/
class DatetimeFieldTest extends SapphireTest {
function setUp() {
parent::setUp();
$this->originalLocale = i18n::get_locale();
i18n::set_locale('en_NZ');
}
function tearDown() {
parent::tearDown();
i18n::set_locale($this->originalLocale);
}
function testFormSaveInto() {
$form = new Form(
new Controller(),
'Form',
new FieldSet(
$f = new DatetimeField('MyDatetime', null)
),
new FieldSet(
new FormAction('doSubmit')
)
);
$f->setValue(array(
'date' => '29/03/2003',
'time' => '23:59:38'
));
$m = new DatetimeFieldTest_Model();
$form->saveInto($m);
$this->assertEquals('2003-03-29 23:59:38', $m->MyDatetime);
}
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');
$f = new DatetimeField('Datetime', null, '2003-03-29');
$this->assertEquals('2003-03-29 00:00:00', $f->dataValue(), 'From date string (no time)');
$f = new DatetimeField('Datetime', null, array('date' => '2003-03-29', 'time' => null));
$this->assertEquals('2003-03-29 00:00:00', $f->dataValue(), 'From date array (no time)');
}
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 $timeformat 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 $futureOnly 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. git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/branches/2.4@99360 467b73ca-7a2a-4603-9d3b-597d59a354a9
2010-02-17 00:10:09 +01:00
function testConstructorWithoutArgs() {
$f = new DatetimeField('Datetime');
$this->assertEquals($f->dataValue(), null);
}
// /**
// * @expectedException InvalidArgumentException
// */
// function testConstructorWithLocalizedDateString() {
// $f = new DatetimeField('Datetime', 'Datetime', '29/03/2003 23:59:38');
// }
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
// */
// function testSetValueWithDateString() {
// $f = new DatetimeField('Datetime', 'Datetime');
// $f->setValue('29/03/2003');
// }
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');
}
function testSetValueWithArray() {
$f = new DatetimeField('Datetime', 'Datetime');
// Values can only be localized (= non-ISO) in array notation
$f->setValue(array(
'date' => '29/03/2003',
'time' => '11pm'
));
$this->assertEquals($f->dataValue(), '2003-03-29 23:00:00');
}
function testSetValueWithDmyArray() {
$f = new DatetimeField('Datetime', 'Datetime');
$f->getDateField()->setConfig('dmyfields', true);
$f->setValue(array(
'date' => array('day' => 29, 'month' => 03, 'year' => 2003),
'time' => '11pm'
));
$this->assertEquals($f->dataValue(), '2003-03-29 23:00:00');
}
function testValidate() {
$f = new DatetimeField('Datetime', 'Datetime', '2003-03-29 23:59:38');
$this->assertTrue($f->validate(new RequiredFields()));
$f = new DatetimeField('Datetime', 'Datetime', 'wrong');
$this->assertFalse($f->validate(new RequiredFields()));
}
}
/**
* @package sapphire
* @subpackage tests
*/
class DatetimeFieldTest_Model extends DataObject implements TestOnly {
static $db = array(
'MyDatetime' => 'SS_Datetime'
);
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 $timeformat 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 $futureOnly 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. git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/branches/2.4@99360 467b73ca-7a2a-4603-9d3b-597d59a354a9
2010-02-17 00:10:09 +01:00
}