silverstripe-framework/tests/forms/DateFieldTest.php
Ingo Schommer c945e23c62 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
2011-02-02 14:18:38 +13:00

172 lines
5.4 KiB
PHP

<?php
/**
* @package sapphire
* @subpackage tests
*/
class DateFieldTest 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 testValidateMinDate() {
$f = new DateField('Date');
$f->setConfig('min', '2009-03-31');
$f->setValue('2010-03-31');
$this->assertTrue($f->validate(new RequiredFields()), 'Date above min date');
$f = new DateField('Date');
$f->setConfig('min', '2009-03-31');
$f->setValue('1999-03-31');
$this->assertFalse($f->validate(new RequiredFields()), 'Date below min date');
$f = new DateField('Date');
$f->setConfig('min', '2009-03-31');
$f->setValue('2009-03-31');
$this->assertTrue($f->validate(new RequiredFields()), 'Date matching min date');
}
function testValidateMinDateStrtotime() {
$f = new DateField('Date');
$f->setConfig('min', '-7 days');
$f->setValue(strftime('%F', strtotime('-8 days')));
$this->assertFalse($f->validate(new RequiredFields()), 'Date below min date, with strtotime');
$f = new DateField('Date');
$f->setConfig('min', '-7 days');
$f->setValue(strftime('%F', strtotime('-7 days')));
$this->assertTrue($f->validate(new RequiredFields()), 'Date matching min date, with strtotime');
}
function testValidateMaxDateStrtotime() {
$f = new DateField('Date');
$f->setConfig('max', '7 days');
$f->setValue(strftime('%F', strtotime('8 days')));
$this->assertFalse($f->validate(new RequiredFields()), 'Date above max date, with strtotime');
$f = new DateField('Date');
$f->setConfig('max', '7 days');
$f->setValue(strftime('%F', strtotime('7 days')));
$this->assertTrue($f->validate(new RequiredFields()), 'Date matching max date, with strtotime');
}
function testValidateMaxDate() {
$f = new DateField('Date');
$f->setConfig('max', '2009-03-31');
$f->setValue('1999-03-31');
$this->assertTrue($f->validate(new RequiredFields()), 'Date above min date');
$f = new DateField('Date');
$f->setConfig('max', '2009-03-31');
$f->setValue('2010-03-31');
$this->assertFalse($f->validate(new RequiredFields()), 'Date above max date');
$f = new DateField('Date');
$f->setConfig('max', '2009-03-31');
$f->setValue('2009-03-31');
$this->assertTrue($f->validate(new RequiredFields()), 'Date matching max date');
}
function testConstructorWithoutArgs() {
$f = new DateField('Date');
$this->assertEquals($f->dataValue(), null);
}
function testConstructorWithDateString() {
$f = new DateField('Date', 'Date', '29/03/2003');
$this->assertEquals($f->dataValue(), '2003-03-29');
}
function testSetValueWithDateString() {
$f = new DateField('Date', 'Date');
$f->setValue('29/03/2003');
$this->assertEquals($f->dataValue(), '2003-03-29');
}
function testSetValueWithDateArray() {
$f = new DateField('Date', 'Date');
$f->setConfig('dmyfields', true);
$f->setValue(array('day' => 29, 'month' => 03, 'year' => 2003));
$this->assertEquals($f->dataValue(), '2003-03-29');
}
function testConstructorWithIsoDate() {
// used by Form->loadDataFrom()
$f = new DateField('Date', 'Date', '2003-03-29');
$this->assertEquals($f->dataValue(), '2003-03-29');
}
function testValidateDMY() {
$f = new DateField('Date', 'Date', '29/03/2003');
$this->assertTrue($f->validate(new RequiredFields()));
$f = new DateField('Date', 'Date', 'wrong');
$this->assertFalse($f->validate(new RequiredFields()));
}
function testValidateArray() {
$f = new DateField('Date', 'Date');
$f->setConfig('dmyfields', true);
$f->setValue(array('day' => 29, 'month' => 03, 'year' => 2003));
$this->assertTrue($f->validate(new RequiredFields()));
// TODO Fix array validation
// $f = new DateField('Date', 'Date', array('day' => 9999, 'month' => 9999, 'year' => 9999));
// $this->assertFalse($f->validate(new RequiredFields()));
}
function testValidateArrayValue() {
$f = new DateField('Date', 'Date');
$this->assertTrue($f->validateArrayValue(array('day' => 29, 'month' => 03, 'year' => 2003)));
$this->assertFalse($f->validateArrayValue(array('month' => 03, 'year' => 2003)));
$this->assertFalse($f->validateArrayValue(array('day' => 99, 'month' => 99, 'year' => 2003)));
}
function testFormatEnNz() {
/* We get YYYY-MM-DD format as the data value for DD/MM/YYYY input value */
$f = new DateField('Date', 'Date', '29/03/2003');
$this->assertEquals($f->dataValue(), '2003-03-29');
}
function testSetLocale() {
// should get en_NZ by default through setUp()
$f = new DateField('Date', 'Date', '29/03/2003');
$f->setLocale('de_DE');
$f->setValue('29.06.2006');
$this->assertEquals($f->dataValue(), '2006-06-29');
}
/**
* Note: This is mostly tested for legacy reasons
*/
function testMDYFormat() {
$dateField = new DateField('Date', 'Date');
$dateField->setConfig('dateformat', 'd/M/Y');
$dateField->setValue('31/03/2003');
$this->assertEquals(
$dateField->dataValue(),
'2003-03-31',
"We get MM-DD-YYYY format as the data value for YYYY-MM-DD input value"
);
$dateField2 = new DateField('Date', 'Date');
$dateField2->setConfig('dateformat', 'd/M/Y');
$dateField2->setValue('04/3/03');
$this->assertEquals(
$dateField2->dataValue(),
'2003-03-04',
"Even if input value hasn't got leading 0's in it we still get the correct data value"
);
}
}
?>