mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 14:05:37 +02:00
Add more edge case tests for disabled DateFields and DatetimeField
This commit is contained in:
parent
97209bc919
commit
fd50ce6295
@ -2,10 +2,8 @@
|
|||||||
|
|
||||||
namespace SilverStripe\Forms\Tests;
|
namespace SilverStripe\Forms\Tests;
|
||||||
|
|
||||||
use IntlDateFormatter;
|
|
||||||
use SilverStripe\Dev\SapphireTest;
|
use SilverStripe\Dev\SapphireTest;
|
||||||
use SilverStripe\Forms\DateField_Disabled;
|
use SilverStripe\Forms\DateField_Disabled;
|
||||||
use SilverStripe\Forms\RequiredFields;
|
|
||||||
use SilverStripe\i18n\i18n;
|
use SilverStripe\i18n\i18n;
|
||||||
use SilverStripe\ORM\FieldType\DBDatetime;
|
use SilverStripe\ORM\FieldType\DBDatetime;
|
||||||
|
|
||||||
@ -76,4 +74,12 @@ class DateField_DisabledTest extends SapphireTest
|
|||||||
$actual = DateField_Disabled::create('Test')->setValue('This is not a date')->Field();
|
$actual = DateField_Disabled::create('Test')->setValue('This is not a date')->Field();
|
||||||
$this->assertEquals($expected, $actual);
|
$this->assertEquals($expected, $actual);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testType()
|
||||||
|
{
|
||||||
|
$field = new DateField_Disabled('Test');
|
||||||
|
$result = $field->Type();
|
||||||
|
$this->assertContains('readonly', $result, 'Disabled field should be treated as readonly');
|
||||||
|
$this->assertContains('date_disabled', $result, 'Field should contain date_disabled class');
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -86,6 +86,13 @@ class DatetimeFieldTest extends SapphireTest
|
|||||||
$this->assertEquals('2003-01-30 11:59:38', $f->dataValue()); // server timezone (Berlin)
|
$this->assertEquals('2003-01-30 11:59:38', $f->dataValue()); // server timezone (Berlin)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testSetSubmittedValueNull()
|
||||||
|
{
|
||||||
|
$field = new DatetimeField('Datetime');
|
||||||
|
$field->setSubmittedValue(false);
|
||||||
|
$this->assertNull($field->Value());
|
||||||
|
}
|
||||||
|
|
||||||
public function testConstructorWithoutArgs()
|
public function testConstructorWithoutArgs()
|
||||||
{
|
{
|
||||||
$f = new DatetimeField('Datetime');
|
$f = new DatetimeField('Datetime');
|
||||||
@ -148,21 +155,24 @@ class DatetimeFieldTest extends SapphireTest
|
|||||||
|
|
||||||
public function testValidate()
|
public function testValidate()
|
||||||
{
|
{
|
||||||
$f = new DatetimeField('Datetime', 'Datetime', '2003-03-29 23:59:38');
|
$field = new DatetimeField('Datetime', 'Datetime', '2003-03-29 23:59:38');
|
||||||
$this->assertTrue($f->validate(new RequiredFields()));
|
$this->assertTrue($field->validate(new RequiredFields()));
|
||||||
|
|
||||||
$f = new DatetimeField('Datetime', 'Datetime', '2003-03-29T23:59:38');
|
$field = new DatetimeField('Datetime', 'Datetime', '2003-03-29T23:59:38');
|
||||||
$this->assertTrue($f->validate(new RequiredFields()), 'Normalised ISO');
|
$this->assertTrue($field->validate(new RequiredFields()), 'Normalised ISO');
|
||||||
|
|
||||||
$f = new DatetimeField('Datetime', 'Datetime', '2003-03-29');
|
$field = new DatetimeField('Datetime', 'Datetime', '2003-03-29');
|
||||||
$this->assertFalse($f->validate(new RequiredFields()), 'Leaving out time');
|
$this->assertFalse($field->validate(new RequiredFields()), 'Leaving out time');
|
||||||
|
|
||||||
$f = (new DatetimeField('Datetime', 'Datetime'))
|
$field = (new DatetimeField('Datetime', 'Datetime'))
|
||||||
->setSubmittedValue('2003-03-29T00:00');
|
->setSubmittedValue('2003-03-29T00:00');
|
||||||
$this->assertTrue($f->validate(new RequiredFields()), 'Leaving out seconds (like many browsers)');
|
$this->assertTrue($field->validate(new RequiredFields()), 'Leaving out seconds (like many browsers)');
|
||||||
|
|
||||||
$f = new DatetimeField('Datetime', 'Datetime', 'wrong');
|
$field = new DatetimeField('Datetime', 'Datetime', 'wrong');
|
||||||
$this->assertFalse($f->validate(new RequiredFields()));
|
$this->assertFalse($field->validate(new RequiredFields()));
|
||||||
|
|
||||||
|
$field = new DatetimeField('Datetime', 'Datetime', false);
|
||||||
|
$this->assertTrue($field->validate(new RequiredFields()));
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testSetMinDate()
|
public function testSetMinDate()
|
||||||
@ -446,6 +456,51 @@ class DatetimeFieldTest extends SapphireTest
|
|||||||
$this->assertEquals($attrs['max'], '2010-01-31T23:00:00'); // frontend timezone
|
$this->assertEquals($attrs['max'], '2010-01-31T23:00:00'); // frontend timezone
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testAttributesNonHTML5()
|
||||||
|
{
|
||||||
|
$field = new DatetimeField('Datetime');
|
||||||
|
$field->setHTML5(false);
|
||||||
|
$result = $field->getAttributes();
|
||||||
|
$this->assertSame('text', $result['type']);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testFrontendToInternalEdgeCases()
|
||||||
|
{
|
||||||
|
$field = new DatetimeField('Datetime');
|
||||||
|
|
||||||
|
$this->assertNull($field->frontendToInternal(false));
|
||||||
|
$this->assertNull($field->frontendToInternal('sdfsdfsfs$%^&*'));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testInternalToFrontendEdgeCases()
|
||||||
|
{
|
||||||
|
$field = new DatetimeField('Datetime');
|
||||||
|
|
||||||
|
$this->assertNull($field->internalToFrontend(false));
|
||||||
|
$this->assertNull($field->internalToFrontend('sdfsdfsfs$%^&*'));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testPerformReadonlyTransformation()
|
||||||
|
{
|
||||||
|
$field = new DatetimeField('Datetime');
|
||||||
|
|
||||||
|
$result = $field->performReadonlyTransformation();
|
||||||
|
$this->assertInstanceOf(DatetimeField::class, $result);
|
||||||
|
$this->assertNotSame($result, $field, 'Readonly field should be cloned');
|
||||||
|
$this->assertTrue($result->isReadonly());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @expectedException \BadMethodCallException
|
||||||
|
* @expectedExceptionMessage Can't change timezone after setting a value
|
||||||
|
*/
|
||||||
|
public function testSetTimezoneThrowsExceptionWhenChangingTimezoneAfterSettingValue()
|
||||||
|
{
|
||||||
|
date_default_timezone_set('Europe/Berlin');
|
||||||
|
$field = new DatetimeField('Datetime', 'Time', '2003-03-29 23:59:38');
|
||||||
|
$field->setTimezone('Pacific/Auckland');
|
||||||
|
}
|
||||||
|
|
||||||
protected function getMockForm()
|
protected function getMockForm()
|
||||||
{
|
{
|
||||||
/** @skipUpgrade */
|
/** @skipUpgrade */
|
||||||
|
Loading…
Reference in New Issue
Block a user