From 97209bc919e5ce6f991f224fc9deadbbd58332f7 Mon Sep 17 00:00:00 2001 From: Robbie Averill Date: Sat, 20 Oct 2018 18:15:42 +0200 Subject: [PATCH] Add edge case unit tests for DateField --- src/Forms/DateField.php | 2 +- tests/php/Forms/DateFieldTest.php | 52 +++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+), 1 deletion(-) diff --git a/src/Forms/DateField.php b/src/Forms/DateField.php index a9c72e033..a58113b6f 100644 --- a/src/Forms/DateField.php +++ b/src/Forms/DateField.php @@ -10,7 +10,7 @@ use SilverStripe\ORM\FieldType\DBDatetime; use SilverStripe\ORM\ValidationResult; /** - * Form used for editing a date stirng + * Form used for editing a date string * * Caution: The form field does not include any JavaScript or CSS when used outside of the CMS context, * since the required frontend dependencies are included through CMS bundling. diff --git a/tests/php/Forms/DateFieldTest.php b/tests/php/Forms/DateFieldTest.php index 5ea4d9a03..87065fc8a 100644 --- a/tests/php/Forms/DateFieldTest.php +++ b/tests/php/Forms/DateFieldTest.php @@ -5,8 +5,10 @@ namespace SilverStripe\Forms\Tests; use IntlDateFormatter; use SilverStripe\Dev\SapphireTest; use SilverStripe\Forms\DateField; +use SilverStripe\Forms\DateField_Disabled; use SilverStripe\Forms\RequiredFields; use SilverStripe\i18n\i18n; +use SilverStripe\ORM\FieldType\DBDate; use SilverStripe\ORM\FieldType\DBDatetime; /** @@ -225,4 +227,54 @@ class DateFieldTest extends SapphireTest $dateField->setLocale('de_DE'); $dateField->Value(); } + + 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)); + } }