2009-02-02 00:49:53 +01:00
|
|
|
<?php
|
2016-08-19 00:51:35 +02:00
|
|
|
|
2016-10-14 03:30:05 +02:00
|
|
|
namespace SilverStripe\Forms\Tests;
|
|
|
|
|
2017-04-03 02:00:59 +02:00
|
|
|
use IntlDateFormatter;
|
2021-10-27 04:39:47 +02:00
|
|
|
use LogicException;
|
2016-08-19 00:51:35 +02:00
|
|
|
use SilverStripe\Dev\SapphireTest;
|
|
|
|
use SilverStripe\Forms\DateField;
|
2018-10-20 18:15:42 +02:00
|
|
|
use SilverStripe\Forms\DateField_Disabled;
|
2016-08-19 00:51:35 +02:00
|
|
|
use SilverStripe\Forms\RequiredFields;
|
|
|
|
use SilverStripe\i18n\i18n;
|
2018-10-20 18:15:42 +02:00
|
|
|
use SilverStripe\ORM\FieldType\DBDate;
|
2017-01-26 05:20:08 +01:00
|
|
|
use SilverStripe\ORM\FieldType\DBDatetime;
|
2016-08-19 00:51:35 +02:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
class DateFieldTest extends SapphireTest
|
|
|
|
{
|
2021-10-27 04:39:47 +02:00
|
|
|
protected function setUp(): void
|
2016-12-16 05:34:21 +01:00
|
|
|
{
|
|
|
|
parent::setUp();
|
|
|
|
i18n::set_locale('en_NZ');
|
2017-01-26 05:20:08 +01:00
|
|
|
DBDatetime::set_mock_now('2011-02-01 8:34:00');
|
2016-12-16 05:34:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testValidateMinDate()
|
|
|
|
{
|
2017-01-26 05:20:08 +01:00
|
|
|
$dateField = new DateField('Date');
|
|
|
|
$dateField->setMinDate('2009-03-31');
|
|
|
|
$dateField->setValue('2010-03-31');
|
|
|
|
$this->assertTrue($dateField->validate(new RequiredFields()), 'Date above min date');
|
|
|
|
|
|
|
|
$dateField = new DateField('Date');
|
|
|
|
$dateField->setMinDate('2009-03-31');
|
|
|
|
$dateField->setValue('1999-03-31');
|
|
|
|
$this->assertFalse($dateField->validate(new RequiredFields()), 'Date below min date');
|
|
|
|
|
|
|
|
$dateField = new DateField('Date');
|
|
|
|
$dateField->setMinDate('2009-03-31');
|
|
|
|
$dateField->setValue('2009-03-31');
|
|
|
|
$this->assertTrue($dateField->validate(new RequiredFields()), 'Date matching min date');
|
2016-12-16 05:34:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testValidateMinDateStrtotime()
|
|
|
|
{
|
|
|
|
$f = new DateField('Date');
|
2017-01-26 05:20:08 +01:00
|
|
|
$f->setMinDate('-7 days');
|
2022-04-11 07:22:22 +02:00
|
|
|
$f->setValue(date('Y-m-d', strtotime('-8 days', DBDatetime::now()->getTimestamp())));
|
2016-12-16 05:34:21 +01:00
|
|
|
$this->assertFalse($f->validate(new RequiredFields()), 'Date below min date, with strtotime');
|
|
|
|
|
|
|
|
$f = new DateField('Date');
|
2017-01-26 05:20:08 +01:00
|
|
|
$f->setMinDate('-7 days');
|
2022-04-11 07:22:22 +02:00
|
|
|
$f->setValue(date('Y-m-d', strtotime('-7 days', DBDatetime::now()->getTimestamp())));
|
2016-12-16 05:34:21 +01:00
|
|
|
$this->assertTrue($f->validate(new RequiredFields()), 'Date matching min date, with strtotime');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testValidateMaxDateStrtotime()
|
|
|
|
{
|
|
|
|
$f = new DateField('Date');
|
2017-01-26 05:20:08 +01:00
|
|
|
$f->setMaxDate('7 days');
|
2022-04-11 07:22:22 +02:00
|
|
|
$f->setValue(date('Y-m-d', strtotime('8 days', DBDatetime::now()->getTimestamp())));
|
2016-12-16 05:34:21 +01:00
|
|
|
$this->assertFalse($f->validate(new RequiredFields()), 'Date above max date, with strtotime');
|
|
|
|
|
|
|
|
$f = new DateField('Date');
|
2017-01-26 05:20:08 +01:00
|
|
|
$f->setMaxDate('7 days');
|
2022-04-11 07:22:22 +02:00
|
|
|
$f->setValue(date('Y-m-d', strtotime('7 days', DBDatetime::now()->getTimestamp())));
|
2016-12-16 05:34:21 +01:00
|
|
|
$this->assertTrue($f->validate(new RequiredFields()), 'Date matching max date, with strtotime');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testValidateMaxDate()
|
|
|
|
{
|
|
|
|
$f = new DateField('Date');
|
2017-01-26 05:20:08 +01:00
|
|
|
$f->setMaxDate('2009-03-31');
|
2016-12-16 05:34:21 +01:00
|
|
|
$f->setValue('1999-03-31');
|
|
|
|
$this->assertTrue($f->validate(new RequiredFields()), 'Date above min date');
|
|
|
|
|
|
|
|
$f = new DateField('Date');
|
2017-01-26 05:20:08 +01:00
|
|
|
$f->setMaxDate('2009-03-31');
|
2016-12-16 05:34:21 +01:00
|
|
|
$f->setValue('2010-03-31');
|
|
|
|
$this->assertFalse($f->validate(new RequiredFields()), 'Date above max date');
|
|
|
|
|
|
|
|
$f = new DateField('Date');
|
2017-01-26 05:20:08 +01:00
|
|
|
$f->setMaxDate('2009-03-31');
|
2016-12-16 05:34:21 +01:00
|
|
|
$f->setValue('2009-03-31');
|
|
|
|
$this->assertTrue($f->validate(new RequiredFields()), 'Date matching max date');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testConstructorWithoutArgs()
|
|
|
|
{
|
|
|
|
$f = new DateField('Date');
|
|
|
|
$this->assertEquals($f->dataValue(), null);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testConstructorWithDateString()
|
|
|
|
{
|
|
|
|
$f = new DateField('Date', 'Date', '29/03/2003');
|
2017-01-26 05:20:08 +01:00
|
|
|
$this->assertEquals(null, $f->dataValue());
|
|
|
|
$f = new DateField('Date', 'Date', '2003-03-29 12:23:00');
|
|
|
|
$this->assertEquals('2003-03-29', $f->dataValue());
|
|
|
|
}
|
|
|
|
|
2017-04-27 01:53:43 +02:00
|
|
|
public function testSetValue()
|
2017-01-26 05:20:08 +01:00
|
|
|
{
|
2017-04-27 01:53:43 +02:00
|
|
|
$f = (new DateField('Date', 'Date'))->setValue('notadate');
|
|
|
|
$this->assertNull($f->Value(), 'Invalid input ignored');
|
|
|
|
|
|
|
|
$f = (new DateField('Date', 'Date'))->setValue('-1 day');
|
|
|
|
$this->assertEquals($f->Value(), '2011-01-31', 'Relative dates accepted');
|
|
|
|
|
|
|
|
$f = (new DateField('Date', 'Date'))->setValue('2011-01-31');
|
|
|
|
$this->assertEquals($f->Value(), '2011-01-31', 'ISO format accepted');
|
|
|
|
|
|
|
|
$f = (new DateField('Date', 'Date'))->setValue('2011-01-31 23:59:59');
|
|
|
|
$this->assertEquals($f->Value(), '2011-01-31', 'ISO format with time accepted');
|
2016-12-16 05:34:21 +01:00
|
|
|
}
|
|
|
|
|
2017-04-27 01:53:43 +02:00
|
|
|
public function testSetValueWithLocalisedDateString()
|
2016-12-16 05:34:21 +01:00
|
|
|
{
|
|
|
|
$f = new DateField('Date', 'Date');
|
2017-04-03 02:00:59 +02:00
|
|
|
$f->setHTML5(false);
|
2017-01-26 05:20:08 +01:00
|
|
|
$f->setSubmittedValue('29/03/2003');
|
2016-12-16 05:34:21 +01:00
|
|
|
$this->assertEquals($f->dataValue(), '2003-03-29');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testConstructorWithIsoDate()
|
|
|
|
{
|
|
|
|
// used by Form->loadDataFrom()
|
|
|
|
$f = new DateField('Date', 'Date', '2003-03-29');
|
|
|
|
$this->assertEquals($f->dataValue(), '2003-03-29');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testValidateDMY()
|
|
|
|
{
|
2017-01-26 05:20:08 +01:00
|
|
|
// Constructor only accepts iso8601
|
2016-12-16 05:34:21 +01:00
|
|
|
$f = new DateField('Date', 'Date', '29/03/2003');
|
2017-01-26 05:20:08 +01:00
|
|
|
$this->assertFalse($f->validate(new RequiredFields()));
|
|
|
|
|
|
|
|
// Set via submitted value (localised) accepts this, however
|
|
|
|
$f = new DateField('Date', 'Date');
|
|
|
|
$f->setSubmittedValue('29/03/2003');
|
|
|
|
$this->assertTrue($f->validate(new RequiredFields()));
|
|
|
|
|
|
|
|
$f = new DateField('Date', 'Date', '2003-03-29');
|
2016-12-16 05:34:21 +01:00
|
|
|
$this->assertTrue($f->validate(new RequiredFields()));
|
|
|
|
|
|
|
|
$f = new DateField('Date', 'Date', 'wrong');
|
|
|
|
$this->assertFalse($f->validate(new RequiredFields()));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testFormatEnNz()
|
|
|
|
{
|
|
|
|
/* We get YYYY-MM-DD format as the data value for DD/MM/YYYY input value */
|
2017-01-26 05:20:08 +01:00
|
|
|
$f = new DateField('Date', 'Date');
|
2017-04-03 02:00:59 +02:00
|
|
|
$f->setHTML5(false);
|
2017-01-26 05:20:08 +01:00
|
|
|
$f->setSubmittedValue('29/03/2003');
|
2016-12-16 05:34:21 +01:00
|
|
|
$this->assertEquals($f->dataValue(), '2003-03-29');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testSetLocale()
|
|
|
|
{
|
|
|
|
// should get en_NZ by default through setUp()
|
2017-01-26 05:20:08 +01:00
|
|
|
i18n::set_locale('de_DE');
|
2016-12-16 05:34:21 +01:00
|
|
|
$f = new DateField('Date', 'Date', '29/03/2003');
|
2017-04-03 02:00:59 +02:00
|
|
|
$f->setHTML5(false);
|
2016-12-16 05:34:21 +01:00
|
|
|
$f->setValue('29.06.2006');
|
|
|
|
$this->assertEquals($f->dataValue(), '2006-06-29');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Note: This is mostly tested for legacy reasons
|
|
|
|
*/
|
|
|
|
public function testMDYFormat()
|
|
|
|
{
|
|
|
|
$dateField = new DateField('Date', 'Date');
|
2017-04-03 02:00:59 +02:00
|
|
|
$dateField->setHTML5(false);
|
2017-01-26 05:20:08 +01:00
|
|
|
$dateField->setDateFormat('d/M/y');
|
|
|
|
$dateField->setSubmittedValue('31/03/2003');
|
2016-12-16 05:34:21 +01:00
|
|
|
$this->assertEquals(
|
|
|
|
'2003-03-31',
|
2017-01-26 05:20:08 +01:00
|
|
|
$dateField->dataValue(),
|
2016-12-16 05:34:21 +01:00
|
|
|
"We get MM-DD-YYYY format as the data value for YYYY-MM-DD input value"
|
|
|
|
);
|
|
|
|
|
|
|
|
$dateField2 = new DateField('Date', 'Date');
|
2017-04-03 02:00:59 +02:00
|
|
|
$dateField2->setHTML5(false);
|
2017-01-26 05:20:08 +01:00
|
|
|
$dateField2->setDateFormat('d/M/y');
|
|
|
|
$dateField2->setSubmittedValue('04/3/03');
|
2016-12-16 05:34:21 +01:00
|
|
|
$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"
|
|
|
|
);
|
|
|
|
}
|
2017-03-30 23:37:21 +02:00
|
|
|
|
|
|
|
public function testHtml5WithCustomFormatThrowsException()
|
|
|
|
{
|
2021-10-27 04:39:47 +02:00
|
|
|
$this->expectException(\LogicException::class);
|
|
|
|
$this->expectExceptionMessageMatches('/Please opt-out .* if using setDateFormat/');
|
2017-03-30 23:37:21 +02:00
|
|
|
$dateField = new DateField('Date', 'Date');
|
|
|
|
$dateField->setValue('2010-03-31');
|
|
|
|
$dateField->setDateFormat('d/M/y');
|
|
|
|
$dateField->Value();
|
|
|
|
}
|
2017-04-03 02:00:59 +02:00
|
|
|
|
|
|
|
public function testHtml5WithCustomDateLengthThrowsException()
|
|
|
|
{
|
2021-10-27 04:39:47 +02:00
|
|
|
$this->expectException(\LogicException::class);
|
|
|
|
$this->expectExceptionMessageMatches('/Please opt-out .* if using setDateLength/');
|
2017-04-03 02:00:59 +02:00
|
|
|
$dateField = new DateField('Date', 'Date');
|
|
|
|
$dateField->setValue('2010-03-31');
|
|
|
|
$dateField->setDateLength(IntlDateFormatter::MEDIUM);
|
|
|
|
$dateField->Value();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testHtml5WithCustomLocaleThrowsException()
|
|
|
|
{
|
2021-10-27 04:39:47 +02:00
|
|
|
$this->expectException(\LogicException::class);
|
|
|
|
$this->expectExceptionMessageMatches('/Please opt-out .* if using setLocale/');
|
2017-04-03 02:00:59 +02:00
|
|
|
$dateField = new DateField('Date', 'Date');
|
|
|
|
$dateField->setValue('2010-03-31');
|
|
|
|
$dateField->setLocale('de_DE');
|
|
|
|
$dateField->Value();
|
|
|
|
}
|
2018-10-20 18:15:42 +02:00
|
|
|
|
|
|
|
public function testGetDateFormatHTML5()
|
|
|
|
{
|
|
|
|
$field = new DateField('Date');
|
|
|
|
$field->setHTML5(true);
|
|
|
|
$this->assertSame(DBDate::ISO_DATE, $field->getDateFormat());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetDateFormatViaSetter()
|
|
|
|
{
|
|
|
|
$field = new DateField('Date');
|
|
|
|
$field->setHTML5(false);
|
|
|
|
$field->setDateFormat('d-m-Y');
|
|
|
|
$this->assertSame('d-m-Y', $field->getDateFormat());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetAttributes()
|
|
|
|
{
|
|
|
|
$field = new DateField('Date');
|
|
|
|
$field
|
|
|
|
->setHTML5(true)
|
|
|
|
->setMinDate('1980-05-10')
|
|
|
|
->setMaxDate('1980-05-20');
|
|
|
|
|
|
|
|
$result = $field->getAttributes();
|
|
|
|
$this->assertSame('1980-05-10', $result['min']);
|
|
|
|
$this->assertSame('1980-05-20', $result['max']);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testSetSubmittedValueNull()
|
|
|
|
{
|
|
|
|
$field = new DateField('Date');
|
|
|
|
$field->setSubmittedValue(false);
|
|
|
|
$this->assertNull($field->Value());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testPerformReadonlyTransformation()
|
|
|
|
{
|
|
|
|
$field = new DateField('Date');
|
|
|
|
$result = $field->performReadonlyTransformation();
|
|
|
|
$this->assertInstanceOf(DateField_Disabled::class, $result);
|
|
|
|
$this->assertTrue($result->isReadonly());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testValidateWithoutValueReturnsTrue()
|
|
|
|
{
|
|
|
|
$field = new DateField('Date');
|
|
|
|
$validator = new RequiredFields();
|
|
|
|
$this->assertTrue($field->validate($validator));
|
|
|
|
}
|
2009-02-02 00:49:53 +01:00
|
|
|
}
|