silverstripe-framework/tests/php/Forms/DatetimeFieldTest.php

534 lines
21 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 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 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. (from r99360) git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@102859 467b73ca-7a2a-4603-9d3b-597d59a354a9
2010-04-14 06:38:40 +02:00
<?php
2016-10-14 03:30:05 +02:00
namespace SilverStripe\Forms\Tests;
use IntlDateFormatter;
use SilverStripe\Control\Controller;
API Refactor bootstrap, request handling See https://github.com/silverstripe/silverstripe-framework/pull/7037 and https://github.com/silverstripe/silverstripe-framework/issues/6681 Squashed commit of the following: commit 8f65e5653211240650eaa4fa65bb83b45aae6d58 Author: Ingo Schommer <me@chillu.com> Date: Thu Jun 22 22:25:50 2017 +1200 Fixed upgrade guide spelling commit 76f95944fa89b0b540704b8d744329f690f9698c Author: Damian Mooyman <damian@silverstripe.com> Date: Thu Jun 22 16:38:34 2017 +1200 BUG Fix non-test class manifest including sapphiretest / functionaltest commit 9379834cb4b2e5177a2600049feec05bf111c16b Author: Damian Mooyman <damian@silverstripe.com> Date: Thu Jun 22 15:50:47 2017 +1200 BUG Fix nesting bug in Kernel commit 188ce35d82599360c40f0f2de29579c56fb90761 Author: Damian Mooyman <damian@silverstripe.com> Date: Thu Jun 22 15:14:51 2017 +1200 BUG fix db bootstrapping issues commit 7ed4660e7a63915e8e974deeaba9807bc4d38b0d Author: Damian Mooyman <damian@silverstripe.com> Date: Thu Jun 22 14:49:07 2017 +1200 BUG Fix issue in DetailedErrorFormatter commit 738f50c497166f81ccbe3f40fbcff895ce71f82f Author: Damian Mooyman <damian@silverstripe.com> Date: Thu Jun 22 11:49:19 2017 +1200 Upgrading notes on mysite/_config.php commit 6279d28e5e455916f902a2f963c014d8899f7fc7 Author: Damian Mooyman <damian@silverstripe.com> Date: Thu Jun 22 11:43:28 2017 +1200 Update developer documentation commit 5c90d53a84ef0139c729396949a7857fae60436f Author: Damian Mooyman <damian@silverstripe.com> Date: Thu Jun 22 10:48:44 2017 +1200 Update installer to not use global databaseConfig commit f9b2ba4755371f08bd95f6908ac612fcbb7ca205 Author: Damian Mooyman <damian@silverstripe.com> Date: Wed Jun 21 21:04:39 2017 +1200 Fix behat issues commit 5b59a912b60282b4dad4ef10ed3b97c5d0a761ac Author: Damian Mooyman <damian@silverstripe.com> Date: Wed Jun 21 17:07:11 2017 +1200 Move HTTPApplication to SilverStripe\Control namespace commit e2c4a18f637bdd3d276619554de60ee8b4d95ced Author: Damian Mooyman <damian@silverstripe.com> Date: Wed Jun 21 16:29:03 2017 +1200 More documentation Fix up remaining tests Refactor temp DB into TempDatabase class so it’s available outside of unit tests. commit 5d235e64f341d6251bfe9f4833f15cc8593c5034 Author: Damian Mooyman <damian@silverstripe.com> Date: Wed Jun 21 12:13:15 2017 +1200 API HTTPRequestBuilder::createFromEnvironment() now cleans up live globals BUG Fix issue with SSViewer Fix Security / View tests commit d88d4ed4e48291cb65407f222f190064b1f1deeb Author: Damian Mooyman <damian@silverstripe.com> Date: Tue Jun 20 16:39:43 2017 +1200 API Refactor AppKernel into CoreKernel commit f7946aec3391139ae1b4029c353c327a36552b36 Author: Damian Mooyman <damian@silverstripe.com> Date: Tue Jun 20 16:00:40 2017 +1200 Docs and minor cleanup commit 12bd31f9366327650b5c0c0f96cd0327d44faf0a Author: Damian Mooyman <damian@silverstripe.com> Date: Tue Jun 20 15:34:34 2017 +1200 API Remove OutputMiddleware API Move environment / global / ini management into Environment class API Move getTempFolder into TempFolder class API Implement HTTPRequestBuilder / CLIRequestBuilder BUG Restore SS_ALLOWED_HOSTS check in original location API CoreKernel now requires $basePath to be passed in API Refactor installer.php to use application to bootstrap API move memstring conversion globals to Convert BUG Fix error in CoreKernel nesting not un-nesting itself properly. commit bba979114624247cf463cf2a8c9e4be9a7c3a772 Author: Damian Mooyman <damian@silverstripe.com> Date: Mon Jun 19 18:07:53 2017 +1200 API Create HTTPMiddleware and standardise middleware for request handling commit 2a10c2397bdc53001013f607b5d38087ce6c0730 Author: Damian Mooyman <damian@silverstripe.com> Date: Mon Jun 19 17:42:42 2017 +1200 Fixed ORM tests commit d75a8d1d93398af4bd0432df9e4bc6295c15a3fe Author: Damian Mooyman <damian@silverstripe.com> Date: Mon Jun 19 17:15:07 2017 +1200 FIx i18n tests commit 06364af3c379c931889c4cc34dd920fee3db204a Author: Damian Mooyman <damian@silverstripe.com> Date: Mon Jun 19 16:59:34 2017 +1200 Fix controller namespace Move states to sub namespace commit 2a278e2953d2dbb19f78d91c919048e1fc935436 Author: Damian Mooyman <damian@silverstripe.com> Date: Mon Jun 19 12:49:45 2017 +1200 Fix forms namespace commit b65c21241bee019730027071d815dbf7571197a4 Author: Damian Mooyman <damian@silverstripe.com> Date: Thu Jun 15 18:56:48 2017 +1200 Update API usages commit d1d4375c95a264a6b63cbaefc2c1d12f808bfd82 Author: Damian Mooyman <damian@silverstripe.com> Date: Thu Jun 15 18:41:44 2017 +1200 API Refactor $flush into HTPPApplication API Enforce health check in Controller::pushCurrent() API Better global backup / restore Updated Director::test() to use new API commit b220534f06732db4fa940d8724c2a85c0ba2495a Author: Damian Mooyman <damian@silverstripe.com> Date: Tue Jun 13 22:05:57 2017 +1200 Move app nesting to a test state helper commit 603704165c08d0c1c81fd5e6bb9506326eeee17b Author: Damian Mooyman <damian@silverstripe.com> Date: Tue Jun 13 21:46:04 2017 +1200 Restore kernel stack to fix multi-level nesting commit 2f6336a15bf79dc8c2edd44cec1931da2dd51c28 Author: Damian Mooyman <damian@silverstripe.com> Date: Tue Jun 13 17:23:21 2017 +1200 API Implement kernel nesting commit fc7188da7d6ad6785354bab61f08700454c81d91 Author: Damian Mooyman <damian@silverstripe.com> Date: Tue Jun 13 15:43:13 2017 +1200 Fix core tests commit a0ae7235148fffd71f2f02d1fe7fe45bf3aa39eb Author: Damian Mooyman <damian@silverstripe.com> Date: Tue Jun 13 15:23:52 2017 +1200 Fix manifest tests commit ca033952513633e182040d3d13e1caa9000ca184 Author: Damian Mooyman <damian@silverstripe.com> Date: Tue Jun 13 15:00:00 2017 +1200 API Move extension management into test state commit c66d4339777663a8a04661fea32a0cf35b95d20f Author: Damian Mooyman <damian@silverstripe.com> Date: Tue Jun 13 14:10:59 2017 +1200 API Refactor SapphireTest state management into SapphireTestState API Remove Injector::unregisterAllObjects() API Remove FakeController commit f26ae75c6ecaafa0dec1093264e0187191e6764d Author: Damian Mooyman <damian@silverstripe.com> Date: Mon Jun 12 18:04:34 2017 +1200 Implement basic CLI application object commit 001d5596621404892de0a5413392379eff990641 Author: Damian Mooyman <damian@silverstripe.com> Date: Mon Jun 12 17:39:38 2017 +1200 Remove references to SapphireTest::is_running_test() Upgrade various code commit de079c041dacd96bc4f4b66421fa2b2cc4c320f8 Author: Damian Mooyman <damian@silverstripe.com> Date: Wed Jun 7 18:07:33 2017 +1200 API Implement APP object API Refactor of Session
2017-06-22 12:50:45 +02:00
use SilverStripe\Dev\SapphireTest;
use SilverStripe\Forms\DatetimeField;
use SilverStripe\Forms\FieldList;
use SilverStripe\Forms\Form;
API Refactor bootstrap, request handling See https://github.com/silverstripe/silverstripe-framework/pull/7037 and https://github.com/silverstripe/silverstripe-framework/issues/6681 Squashed commit of the following: commit 8f65e5653211240650eaa4fa65bb83b45aae6d58 Author: Ingo Schommer <me@chillu.com> Date: Thu Jun 22 22:25:50 2017 +1200 Fixed upgrade guide spelling commit 76f95944fa89b0b540704b8d744329f690f9698c Author: Damian Mooyman <damian@silverstripe.com> Date: Thu Jun 22 16:38:34 2017 +1200 BUG Fix non-test class manifest including sapphiretest / functionaltest commit 9379834cb4b2e5177a2600049feec05bf111c16b Author: Damian Mooyman <damian@silverstripe.com> Date: Thu Jun 22 15:50:47 2017 +1200 BUG Fix nesting bug in Kernel commit 188ce35d82599360c40f0f2de29579c56fb90761 Author: Damian Mooyman <damian@silverstripe.com> Date: Thu Jun 22 15:14:51 2017 +1200 BUG fix db bootstrapping issues commit 7ed4660e7a63915e8e974deeaba9807bc4d38b0d Author: Damian Mooyman <damian@silverstripe.com> Date: Thu Jun 22 14:49:07 2017 +1200 BUG Fix issue in DetailedErrorFormatter commit 738f50c497166f81ccbe3f40fbcff895ce71f82f Author: Damian Mooyman <damian@silverstripe.com> Date: Thu Jun 22 11:49:19 2017 +1200 Upgrading notes on mysite/_config.php commit 6279d28e5e455916f902a2f963c014d8899f7fc7 Author: Damian Mooyman <damian@silverstripe.com> Date: Thu Jun 22 11:43:28 2017 +1200 Update developer documentation commit 5c90d53a84ef0139c729396949a7857fae60436f Author: Damian Mooyman <damian@silverstripe.com> Date: Thu Jun 22 10:48:44 2017 +1200 Update installer to not use global databaseConfig commit f9b2ba4755371f08bd95f6908ac612fcbb7ca205 Author: Damian Mooyman <damian@silverstripe.com> Date: Wed Jun 21 21:04:39 2017 +1200 Fix behat issues commit 5b59a912b60282b4dad4ef10ed3b97c5d0a761ac Author: Damian Mooyman <damian@silverstripe.com> Date: Wed Jun 21 17:07:11 2017 +1200 Move HTTPApplication to SilverStripe\Control namespace commit e2c4a18f637bdd3d276619554de60ee8b4d95ced Author: Damian Mooyman <damian@silverstripe.com> Date: Wed Jun 21 16:29:03 2017 +1200 More documentation Fix up remaining tests Refactor temp DB into TempDatabase class so it’s available outside of unit tests. commit 5d235e64f341d6251bfe9f4833f15cc8593c5034 Author: Damian Mooyman <damian@silverstripe.com> Date: Wed Jun 21 12:13:15 2017 +1200 API HTTPRequestBuilder::createFromEnvironment() now cleans up live globals BUG Fix issue with SSViewer Fix Security / View tests commit d88d4ed4e48291cb65407f222f190064b1f1deeb Author: Damian Mooyman <damian@silverstripe.com> Date: Tue Jun 20 16:39:43 2017 +1200 API Refactor AppKernel into CoreKernel commit f7946aec3391139ae1b4029c353c327a36552b36 Author: Damian Mooyman <damian@silverstripe.com> Date: Tue Jun 20 16:00:40 2017 +1200 Docs and minor cleanup commit 12bd31f9366327650b5c0c0f96cd0327d44faf0a Author: Damian Mooyman <damian@silverstripe.com> Date: Tue Jun 20 15:34:34 2017 +1200 API Remove OutputMiddleware API Move environment / global / ini management into Environment class API Move getTempFolder into TempFolder class API Implement HTTPRequestBuilder / CLIRequestBuilder BUG Restore SS_ALLOWED_HOSTS check in original location API CoreKernel now requires $basePath to be passed in API Refactor installer.php to use application to bootstrap API move memstring conversion globals to Convert BUG Fix error in CoreKernel nesting not un-nesting itself properly. commit bba979114624247cf463cf2a8c9e4be9a7c3a772 Author: Damian Mooyman <damian@silverstripe.com> Date: Mon Jun 19 18:07:53 2017 +1200 API Create HTTPMiddleware and standardise middleware for request handling commit 2a10c2397bdc53001013f607b5d38087ce6c0730 Author: Damian Mooyman <damian@silverstripe.com> Date: Mon Jun 19 17:42:42 2017 +1200 Fixed ORM tests commit d75a8d1d93398af4bd0432df9e4bc6295c15a3fe Author: Damian Mooyman <damian@silverstripe.com> Date: Mon Jun 19 17:15:07 2017 +1200 FIx i18n tests commit 06364af3c379c931889c4cc34dd920fee3db204a Author: Damian Mooyman <damian@silverstripe.com> Date: Mon Jun 19 16:59:34 2017 +1200 Fix controller namespace Move states to sub namespace commit 2a278e2953d2dbb19f78d91c919048e1fc935436 Author: Damian Mooyman <damian@silverstripe.com> Date: Mon Jun 19 12:49:45 2017 +1200 Fix forms namespace commit b65c21241bee019730027071d815dbf7571197a4 Author: Damian Mooyman <damian@silverstripe.com> Date: Thu Jun 15 18:56:48 2017 +1200 Update API usages commit d1d4375c95a264a6b63cbaefc2c1d12f808bfd82 Author: Damian Mooyman <damian@silverstripe.com> Date: Thu Jun 15 18:41:44 2017 +1200 API Refactor $flush into HTPPApplication API Enforce health check in Controller::pushCurrent() API Better global backup / restore Updated Director::test() to use new API commit b220534f06732db4fa940d8724c2a85c0ba2495a Author: Damian Mooyman <damian@silverstripe.com> Date: Tue Jun 13 22:05:57 2017 +1200 Move app nesting to a test state helper commit 603704165c08d0c1c81fd5e6bb9506326eeee17b Author: Damian Mooyman <damian@silverstripe.com> Date: Tue Jun 13 21:46:04 2017 +1200 Restore kernel stack to fix multi-level nesting commit 2f6336a15bf79dc8c2edd44cec1931da2dd51c28 Author: Damian Mooyman <damian@silverstripe.com> Date: Tue Jun 13 17:23:21 2017 +1200 API Implement kernel nesting commit fc7188da7d6ad6785354bab61f08700454c81d91 Author: Damian Mooyman <damian@silverstripe.com> Date: Tue Jun 13 15:43:13 2017 +1200 Fix core tests commit a0ae7235148fffd71f2f02d1fe7fe45bf3aa39eb Author: Damian Mooyman <damian@silverstripe.com> Date: Tue Jun 13 15:23:52 2017 +1200 Fix manifest tests commit ca033952513633e182040d3d13e1caa9000ca184 Author: Damian Mooyman <damian@silverstripe.com> Date: Tue Jun 13 15:00:00 2017 +1200 API Move extension management into test state commit c66d4339777663a8a04661fea32a0cf35b95d20f Author: Damian Mooyman <damian@silverstripe.com> Date: Tue Jun 13 14:10:59 2017 +1200 API Refactor SapphireTest state management into SapphireTestState API Remove Injector::unregisterAllObjects() API Remove FakeController commit f26ae75c6ecaafa0dec1093264e0187191e6764d Author: Damian Mooyman <damian@silverstripe.com> Date: Mon Jun 12 18:04:34 2017 +1200 Implement basic CLI application object commit 001d5596621404892de0a5413392379eff990641 Author: Damian Mooyman <damian@silverstripe.com> Date: Mon Jun 12 17:39:38 2017 +1200 Remove references to SapphireTest::is_running_test() Upgrade various code commit de079c041dacd96bc4f4b66421fa2b2cc4c320f8 Author: Damian Mooyman <damian@silverstripe.com> Date: Wed Jun 7 18:07:33 2017 +1200 API Implement APP object API Refactor of Session
2017-06-22 12:50:45 +02:00
use SilverStripe\Forms\FormAction;
use SilverStripe\Forms\RequiredFields;
use SilverStripe\Forms\Tests\DatetimeFieldTest\Model;
use SilverStripe\i18n\i18n;
2017-04-26 03:49:40 +02:00
use SilverStripe\ORM\FieldType\DBDatetime;
class DatetimeFieldTest extends SapphireTest
{
protected $timezone = null;
protected function setUp(): void
{
parent::setUp();
i18n::set_locale('en_NZ');
// Fix now to prevent race conditions
DBDatetime::set_mock_now('2010-04-04');
$this->timezone = date_default_timezone_get();
}
protected function tearDown(): void
{
DBDatetime::clear_mock_now();
2022-04-14 03:12:59 +02:00
date_default_timezone_set($this->timezone ?? '');
2020-04-06 07:03:11 +02:00
parent::tearDown();
}
public function testFormSaveInto()
{
$dateTimeField = new DatetimeField('MyDatetime');
$form = $this->getMockForm();
$form->Fields()->push($dateTimeField);
2017-04-20 05:08:44 +02:00
$dateTimeField->setSubmittedValue('2003-03-29T23:59:38');
2017-04-03 09:35:40 +02:00
$validator = new RequiredFields();
$this->assertTrue($dateTimeField->validate($validator));
$m = new Model();
$form->saveInto($m);
$this->assertEquals('2003-03-29 23:59:38', $m->MyDatetime);
2017-04-03 09:35:40 +02:00
}
public function testFormSaveIntoLocalised()
{
$dateTimeField = new DatetimeField('MyDatetime');
2017-04-20 05:08:44 +02:00
$dateTimeField
2017-04-03 09:35:40 +02:00
->setHTML5(false)
->setLocale('en_NZ');
$form = $this->getMockForm();
$form->Fields()->push($dateTimeField);
// en_NZ standard format
2017-04-20 05:08:44 +02:00
$dateTimeField->setSubmittedValue('29/03/2003 11:59:38 pm');
$validator = new RequiredFields();
$this->assertTrue($dateTimeField->validate($validator));
$m = new Model();
$form->saveInto($m);
$this->assertEquals('2003-03-29 23:59:38', $m->MyDatetime);
}
public 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');
}
public function testDataValueWithTimezone()
{
// Berlin and Auckland have 12h time difference in northern hemisphere winter
date_default_timezone_set('Europe/Berlin');
$f = new DatetimeField('Datetime');
$f->setTimezone('Pacific/Auckland');
$f->setSubmittedValue('2003-01-30T23:59:38'); // frontend timezone (Auckland)
$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()
{
$f = new DatetimeField('Datetime');
$this->assertEquals($f->dataValue(), null);
}
2017-04-26 23:18:38 +02:00
public function testConstructorWithLocalizedDateSetsNullValue()
{
$f = new DatetimeField('Datetime', 'Datetime', '29/03/2003 23:59:38');
$this->assertNull($f->Value());
}
public 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');
}
public function testSetValueWithDateTimeString()
{
$f = new DatetimeField('Datetime', 'Datetime');
$f->setValue('2003-03-29 23:59:38');
2018-06-14 08:14:12 +02:00
$this->assertEquals('2003-03-29 23:59:38', $f->dataValue(), 'Accepts ISO');
$f = new DatetimeField('Datetime', 'Datetime');
$f->setValue('2003-03-29T23:59:38');
2018-06-14 08:14:12 +02:00
$this->assertEquals('2003-03-29 23:59:38', $f->dataValue(), 'Accepts normalised ISO');
}
public function testSubmittedValue()
{
$datetimeField = new DatetimeField('Datetime', 'Datetime');
2017-04-20 05:08:44 +02:00
$datetimeField->setSubmittedValue('2003-03-29 23:00:00');
2017-04-03 09:35:40 +02:00
$this->assertEquals($datetimeField->dataValue(), '2003-03-29 23:00:00');
$datetimeField = new DatetimeField('Datetime', 'Datetime');
$datetimeField->setSubmittedValue('2003-03-29T23:00:00');
$this->assertEquals($datetimeField->dataValue(), '2003-03-29 23:00:00', 'Normalised ISO');
2017-04-03 09:35:40 +02:00
}
2017-04-20 05:08:44 +02:00
public function testSetValueWithLocalised()
2017-04-03 09:35:40 +02:00
{
$datetimeField = new DatetimeField('Datetime', 'Datetime');
2017-04-20 05:08:44 +02:00
$datetimeField
2017-04-03 09:35:40 +02:00
->setHTML5(false)
->setLocale('en_NZ');
2017-04-20 05:08:44 +02:00
$datetimeField->setSubmittedValue('29/03/2003 11:00:00 pm');
$this->assertEquals($datetimeField->dataValue(), '2003-03-29 23:00:00');
2017-04-26 23:18:38 +02:00
// Some localisation packages exclude the ',' in default medium format
$this->assertMatchesRegularExpression(
'#29/03/2003(,)? 11:00:00 (PM|pm)#',
2017-04-26 23:18:38 +02:00
$datetimeField->Value(),
'User value is formatted, and in user timezone'
);
}
public function testValidate()
{
$field = new DatetimeField('Datetime', 'Datetime', '2003-03-29 23:59:38');
$this->assertTrue($field->validate(new RequiredFields()));
$field = new DatetimeField('Datetime', 'Datetime', '2003-03-29T23:59:38');
$this->assertTrue($field->validate(new RequiredFields()), 'Normalised ISO');
$field = new DatetimeField('Datetime', 'Datetime', '2003-03-29');
$this->assertFalse($field->validate(new RequiredFields()), 'Leaving out time');
$field = (new DatetimeField('Datetime', 'Datetime'))
->setSubmittedValue('2003-03-29T00:00');
$this->assertTrue($field->validate(new RequiredFields()), 'Leaving out seconds (like many browsers)');
$field = new DatetimeField('Datetime', 'Datetime', 'wrong');
$this->assertFalse($field->validate(new RequiredFields()));
2017-04-26 03:49:29 +02:00
$field = new DatetimeField('Datetime', 'Datetime', false);
$this->assertTrue($field->validate(new RequiredFields()));
}
2017-04-27 01:53:34 +02:00
public function testSetMinDate()
{
$f = (new DatetimeField('Datetime'))->setMinDatetime('2009-03-31T23:00:00');
$this->assertEquals($f->getMinDatetime(), '2009-03-31 23:00:00', 'Retains ISO');
2017-04-27 01:53:34 +02:00
$f = (new DatetimeField('Datetime'))->setMinDatetime('2009-03-31 23:00:00');
$this->assertEquals($f->getMinDatetime(), '2009-03-31 23:00:00', 'Converts normalised ISO to ISO');
2017-04-27 01:53:34 +02:00
$f = (new DatetimeField('Datetime'))->setMinDatetime('invalid');
$this->assertNull($f->getMinDatetime(), 'Ignores invalid values');
}
public function testSetMaxDate()
{
$f = (new DatetimeField('Datetime'))->setMaxDatetime('2009-03-31T23:00:00');
$this->assertEquals($f->getMaxDatetime(), '2009-03-31 23:00:00', 'Retains ISO');
2017-04-27 01:53:34 +02:00
$f = (new DatetimeField('Datetime'))->setMaxDatetime('2009-03-31 23:00:00');
$this->assertEquals($f->getMaxDatetime(), '2009-03-31 23:00:00', 'Converts normalised ISO to ISO');
2017-04-27 01:53:34 +02:00
$f = (new DatetimeField('Datetime'))->setMaxDatetime('invalid');
$this->assertNull($f->getMaxDatetime(), 'Ignores invalid values');
}
2017-04-26 03:49:40 +02:00
public function testValidateMinDate()
{
$dateField = new DatetimeField('Datetime');
$dateField->setMinDatetime('2009-03-31 23:00:00');
$dateField->setValue('2009-03-31 23:00:01');
$this->assertTrue($dateField->validate(new RequiredFields()), 'Time above min datetime');
$dateField = new DatetimeField('Datetime');
$dateField->setMinDatetime('2009-03-31 23:00:00');
$dateField->setValue('2009-03-31 22:00:00');
$this->assertFalse($dateField->validate(new RequiredFields()), 'Time below min datetime');
$dateField = new DatetimeField('Datetime');
$dateField->setMinDatetime('2009-03-31 23:00:00');
$dateField->setValue('2009-03-31 23:00:00');
$this->assertTrue($dateField->validate(new RequiredFields()), 'Date and time matching min datetime');
$dateField = new DatetimeField('Datetime');
$dateField->setMinDatetime('2009-03-31 23:00:00');
$dateField->setValue('2008-03-31 23:00:00');
$this->assertFalse($dateField->validate(new RequiredFields()), 'Date below min datetime');
}
public function testValidateMinDateWithSubmittedValueAndTimezone()
{
// Berlin and Auckland have 12h time difference in northern hemisphere winter
date_default_timezone_set('Europe/Berlin');
$dateField = new DatetimeField('Datetime');
$dateField->setTimezone('Pacific/Auckland');
$dateField->setMinDatetime('2009-01-30 23:00:00'); // server timezone (Berlin)
$dateField->setSubmittedValue('2009-01-31T11:00:01'); // frontend timezone (Auckland)
$this->assertTrue($dateField->validate(new RequiredFields()), 'Time above min datetime');
$dateField = new DatetimeField('Datetime');
$dateField->setTimezone('Pacific/Auckland');
$dateField->setMinDatetime('2009-01-30 23:00:00');
$dateField->setSubmittedValue('2009-01-31T10:00:00');
$this->assertFalse($dateField->validate(new RequiredFields()), 'Time below min datetime');
$dateField = new DatetimeField('Datetime');
$dateField->setTimezone('Pacific/Auckland');
$dateField->setMinDatetime('2009-01-30 23:00:00');
$dateField->setSubmittedValue('2009-01-31T11:00:00');
$this->assertTrue($dateField->validate(new RequiredFields()), 'Date and time matching min datetime');
$dateField = new DatetimeField('Datetime');
$dateField->setTimezone('Pacific/Auckland');
$dateField->setMinDatetime('2009-01-30 23:00:00');
$dateField->setSubmittedValue('2008-01-31T11:00:00');
$this->assertFalse($dateField->validate(new RequiredFields()), 'Date below min datetime');
2017-04-26 03:49:40 +02:00
}
public function testValidateMinDateStrtotime()
{
$f = new DatetimeField('Datetime');
$f->setMinDatetime('-7 days');
$f->setValue(date('Y-m-d H:i:s', strtotime('-8 days', DBDatetime::now()->getTimestamp())));
2017-04-26 03:49:40 +02:00
$this->assertFalse($f->validate(new RequiredFields()), 'Date below min datetime, with strtotime');
$f = new DatetimeField('Datetime');
$f->setMinDatetime('-7 days');
$f->setValue(date('Y-m-d H:i:s', strtotime('-7 days', DBDatetime::now()->getTimestamp())));
2017-04-26 03:49:40 +02:00
$this->assertTrue($f->validate(new RequiredFields()), 'Date matching min datetime, with strtotime');
}
public function testValidateMaxDateStrtotime()
{
$f = new DatetimeField('Datetime');
$f->setMaxDatetime('7 days');
$f->setValue(date('Y-m-d H:i:s', strtotime('8 days', DBDatetime::now()->getTimestamp())));
2017-04-26 03:49:40 +02:00
$this->assertFalse($f->validate(new RequiredFields()), 'Date above max date, with strtotime');
$f = new DatetimeField('Datetime');
$f->setMaxDatetime('7 days');
$f->setValue(date('Y-m-d H:i:s', strtotime('7 days', DBDatetime::now()->getTimestamp())));
2017-04-26 03:49:40 +02:00
$this->assertTrue($f->validate(new RequiredFields()), 'Date matching max date, with strtotime');
}
public function testValidateMaxDate()
{
$f = new DatetimeField('Datetime');
$f->setMaxDatetime('2009-03-31 23:00:00');
$f->setValue('2009-03-31 22:00:00');
$this->assertTrue($f->validate(new RequiredFields()), 'Time below max datetime');
$f = new DatetimeField('Datetime');
$f->setMaxDatetime('2009-03-31 23:00:00');
$f->setValue('2010-03-31 23:00:01');
$this->assertFalse($f->validate(new RequiredFields()), 'Time above max datetime');
$f = new DatetimeField('Datetime');
$f->setMaxDatetime('2009-03-31 23:00:00');
$f->setValue('2009-03-31 23:00:00');
$this->assertTrue($f->validate(new RequiredFields()), 'Date and time matching max datetime');
$f = new DatetimeField('Datetime');
$f->setMaxDatetime('2009-03-31 23:00:00');
$f->setValue('2010-03-31 23:00:00');
$this->assertFalse($f->validate(new RequiredFields()), 'Date above max datetime');
}
public function testValidateMaxDateWithSubmittedValueAndTimezone()
{
// Berlin and Auckland have 12h time difference in northern hemisphere winter
date_default_timezone_set('Europe/Berlin');
$f = new DatetimeField('Datetime');
$f->setTimezone('Pacific/Auckland');
$f->setMaxDatetime('2009-01-31 23:00:00'); // server timezone (Berlin)
$f->setSubmittedValue('2009-01-31T10:00:00'); // frontend timezone (Auckland)
$this->assertTrue($f->validate(new RequiredFields()), 'Time below max datetime');
$f = new DatetimeField('Datetime');
$f->setTimezone('Pacific/Auckland');
$f->setMaxDatetime('2009-01-31 23:00:00');
$f->setSubmittedValue('2010-01-31T11:00:01');
$this->assertFalse($f->validate(new RequiredFields()), 'Time above max datetime');
$f = new DatetimeField('Datetime');
$f->setTimezone('Pacific/Auckland');
$f->setMaxDatetime('2009-01-31 23:00:00');
$f->setSubmittedValue('2009-01-31T11:00:00');
$this->assertTrue($f->validate(new RequiredFields()), 'Date and time matching max datetime');
$f = new DatetimeField('Datetime');
$f->setTimezone('Pacific/Auckland');
$f->setMaxDatetime('2009-01-31 23:00:00');
$f->setSubmittedValue('2010-01-31T11:00:00');
$this->assertFalse($f->validate(new RequiredFields()), 'Date above max datetime');
2017-04-26 03:49:40 +02:00
}
public function testTimezoneSetValueLocalised()
{
date_default_timezone_set('Europe/Berlin');
// Berlin and Auckland have 12h time difference in northern hemisphere winter
$datetimeField = new DatetimeField('Datetime', 'Datetime');
2017-04-03 09:35:40 +02:00
2017-04-20 05:08:44 +02:00
$datetimeField
2017-04-03 09:35:40 +02:00
->setHTML5(false)
2017-04-26 23:18:38 +02:00
->setDatetimeFormat('dd/MM/y HH:mm:ss');
2017-04-03 09:35:40 +02:00
$datetimeField->setTimezone('Pacific/Auckland');
$datetimeField->setValue('2003-12-24 23:59:59');
$this->assertEquals(
2017-04-26 23:18:38 +02:00
'25/12/2003 11:59:59',
$datetimeField->Value(),
'User value is formatted, and in user timezone'
);
2017-04-20 05:08:44 +02:00
$this->assertEquals(
'2003-12-24 23:59:59',
$datetimeField->dataValue(),
'Data value is in ISO format, and in server timezone'
);
}
public function testTimezoneSetValueWithHtml5()
{
date_default_timezone_set('Europe/Berlin');
// Berlin and Auckland have 12h time difference in northern hemisphere winter
$datetimeField = new DatetimeField('Datetime', 'Datetime');
$datetimeField->setTimezone('Pacific/Auckland');
$datetimeField->setValue('2003-12-24 23:59:59');
$this->assertEquals(
'2003-12-25T11:59:59',
$datetimeField->Value(),
'User value is in normalised ISO format and in user timezone'
);
$this->assertEquals(
'2003-12-24 23:59:59',
$datetimeField->dataValue(),
'Data value is in ISO format, and in server timezone'
);
}
public function testTimezoneSetSubmittedValueLocalised()
{
date_default_timezone_set('Europe/Berlin');
// Berlin and Auckland have 12h time difference in northern hemisphere summer, but Berlin and Moscow only 2h.
$datetimeField = new DatetimeField('Datetime', 'Datetime');
2017-04-03 09:35:40 +02:00
2017-04-20 05:08:44 +02:00
$datetimeField
2017-04-03 09:35:40 +02:00
->setHTML5(false)
->setLocale('en_NZ');
$datetimeField->setTimezone('Europe/Moscow');
2017-04-20 05:08:44 +02:00
// pass in default format, at user time (Moscow)
$datetimeField->setSubmittedValue('24/06/2003 11:59:59 pm');
$this->assertTrue($datetimeField->validate(new RequiredFields()));
$this->assertEquals('2003-06-24 21:59:59', $datetimeField->dataValue(), 'Data value matches server timezone');
}
public function testGetName()
{
$field = new DatetimeField('Datetime');
$this->assertEquals('Datetime', $field->getName());
}
public function testSetName()
{
$field = new DatetimeField('Datetime', 'Datetime');
$field->setName('CustomDatetime');
$this->assertEquals('CustomDatetime', $field->getName());
}
public function testSchemaDataDefaultsIncludesMinMax()
{
$field = new DatetimeField('Datetime');
$field->setMinDatetime('2009-03-31 23:00:00');
$field->setMaxDatetime('2010-03-31 23:00:00');
$defaults = $field->getSchemaDataDefaults();
$this->assertEquals($defaults['data']['min'], '2009-03-31T23:00:00');
$this->assertEquals($defaults['data']['max'], '2010-03-31T23:00:00');
}
public function testSchemaDataDefaultsAdjustsMinMaxToTimezone()
{
date_default_timezone_set('Europe/Berlin');
// Berlin and Auckland have 12h time difference in northern hemisphere summer, but Berlin and Moscow only 2h.
$field = new DatetimeField('Datetime');
$field->setTimezone('Pacific/Auckland');
$field->setMinDatetime('2009-01-31 11:00:00'); // server timezone
$field->setMaxDatetime('2010-01-31 11:00:00'); // server timezone
$defaults = $field->getSchemaDataDefaults();
$this->assertEquals($defaults['data']['min'], '2009-01-31T23:00:00'); // frontend timezone
$this->assertEquals($defaults['data']['max'], '2010-01-31T23:00:00'); // frontend timezone
}
public function testAttributesIncludesMinMax()
{
$field = new DatetimeField('Datetime');
$field->setMinDatetime('2009-03-31 23:00:00');
$field->setMaxDatetime('2010-03-31 23:00:00');
$attrs = $field->getAttributes();
$this->assertEquals($attrs['min'], '2009-03-31T23:00:00');
$this->assertEquals($attrs['max'], '2010-03-31T23:00:00');
}
public function testAttributesAdjustsMinMaxToTimezone()
{
date_default_timezone_set('Europe/Berlin');
// Berlin and Auckland have 12h time difference in northern hemisphere summer, but Berlin and Moscow only 2h.
$field = new DatetimeField('Datetime');
$field->setTimezone('Pacific/Auckland');
$field->setMinDatetime('2009-01-31 11:00:00'); // server timezone
$field->setMaxDatetime('2010-01-31 11:00:00'); // server timezone
$attrs = $field->getAttributes();
$this->assertEquals($attrs['min'], '2009-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());
}
public function testSetTimezoneThrowsExceptionWhenChangingTimezoneAfterSettingValue()
{
$this->expectException(\BadMethodCallException::class);
$this->expectExceptionMessage("Can't change timezone after setting a value");
date_default_timezone_set('Europe/Berlin');
$field = new DatetimeField('Datetime', 'Time', '2003-03-29 23:59:38');
$field->setTimezone('Pacific/Auckland');
}
public function testModifyReturnNewField(): void
{
$globalStateNow = '2020-01-01 00:00:00';
DBDatetime::set_mock_now($globalStateNow);
// Suppose we need to know the current time in our feature, we store it in a variable
// Make this field immutable, so future modifications don't apply to any other object references
$now = DBDatetime::now()->setImmutable(true);
// Later in the code we want to know the time value for 10 days later, we can reuse our $now variable
$later = $now->modify('+ 10 days')->Rfc2822();
// Our expectation is that this code should not apply the change to our
// $now variable declared earlier in the code
$this->assertSame('2020-01-11 00:00:00', $later, 'We expect to get a future datetime');
$this->assertSame($globalStateNow, $now->Rfc2822(), 'We expect to get the current datetime');
}
protected function getMockForm()
{
/** @skipUpgrade */
return new Form(
API Refactor bootstrap, request handling See https://github.com/silverstripe/silverstripe-framework/pull/7037 and https://github.com/silverstripe/silverstripe-framework/issues/6681 Squashed commit of the following: commit 8f65e5653211240650eaa4fa65bb83b45aae6d58 Author: Ingo Schommer <me@chillu.com> Date: Thu Jun 22 22:25:50 2017 +1200 Fixed upgrade guide spelling commit 76f95944fa89b0b540704b8d744329f690f9698c Author: Damian Mooyman <damian@silverstripe.com> Date: Thu Jun 22 16:38:34 2017 +1200 BUG Fix non-test class manifest including sapphiretest / functionaltest commit 9379834cb4b2e5177a2600049feec05bf111c16b Author: Damian Mooyman <damian@silverstripe.com> Date: Thu Jun 22 15:50:47 2017 +1200 BUG Fix nesting bug in Kernel commit 188ce35d82599360c40f0f2de29579c56fb90761 Author: Damian Mooyman <damian@silverstripe.com> Date: Thu Jun 22 15:14:51 2017 +1200 BUG fix db bootstrapping issues commit 7ed4660e7a63915e8e974deeaba9807bc4d38b0d Author: Damian Mooyman <damian@silverstripe.com> Date: Thu Jun 22 14:49:07 2017 +1200 BUG Fix issue in DetailedErrorFormatter commit 738f50c497166f81ccbe3f40fbcff895ce71f82f Author: Damian Mooyman <damian@silverstripe.com> Date: Thu Jun 22 11:49:19 2017 +1200 Upgrading notes on mysite/_config.php commit 6279d28e5e455916f902a2f963c014d8899f7fc7 Author: Damian Mooyman <damian@silverstripe.com> Date: Thu Jun 22 11:43:28 2017 +1200 Update developer documentation commit 5c90d53a84ef0139c729396949a7857fae60436f Author: Damian Mooyman <damian@silverstripe.com> Date: Thu Jun 22 10:48:44 2017 +1200 Update installer to not use global databaseConfig commit f9b2ba4755371f08bd95f6908ac612fcbb7ca205 Author: Damian Mooyman <damian@silverstripe.com> Date: Wed Jun 21 21:04:39 2017 +1200 Fix behat issues commit 5b59a912b60282b4dad4ef10ed3b97c5d0a761ac Author: Damian Mooyman <damian@silverstripe.com> Date: Wed Jun 21 17:07:11 2017 +1200 Move HTTPApplication to SilverStripe\Control namespace commit e2c4a18f637bdd3d276619554de60ee8b4d95ced Author: Damian Mooyman <damian@silverstripe.com> Date: Wed Jun 21 16:29:03 2017 +1200 More documentation Fix up remaining tests Refactor temp DB into TempDatabase class so it’s available outside of unit tests. commit 5d235e64f341d6251bfe9f4833f15cc8593c5034 Author: Damian Mooyman <damian@silverstripe.com> Date: Wed Jun 21 12:13:15 2017 +1200 API HTTPRequestBuilder::createFromEnvironment() now cleans up live globals BUG Fix issue with SSViewer Fix Security / View tests commit d88d4ed4e48291cb65407f222f190064b1f1deeb Author: Damian Mooyman <damian@silverstripe.com> Date: Tue Jun 20 16:39:43 2017 +1200 API Refactor AppKernel into CoreKernel commit f7946aec3391139ae1b4029c353c327a36552b36 Author: Damian Mooyman <damian@silverstripe.com> Date: Tue Jun 20 16:00:40 2017 +1200 Docs and minor cleanup commit 12bd31f9366327650b5c0c0f96cd0327d44faf0a Author: Damian Mooyman <damian@silverstripe.com> Date: Tue Jun 20 15:34:34 2017 +1200 API Remove OutputMiddleware API Move environment / global / ini management into Environment class API Move getTempFolder into TempFolder class API Implement HTTPRequestBuilder / CLIRequestBuilder BUG Restore SS_ALLOWED_HOSTS check in original location API CoreKernel now requires $basePath to be passed in API Refactor installer.php to use application to bootstrap API move memstring conversion globals to Convert BUG Fix error in CoreKernel nesting not un-nesting itself properly. commit bba979114624247cf463cf2a8c9e4be9a7c3a772 Author: Damian Mooyman <damian@silverstripe.com> Date: Mon Jun 19 18:07:53 2017 +1200 API Create HTTPMiddleware and standardise middleware for request handling commit 2a10c2397bdc53001013f607b5d38087ce6c0730 Author: Damian Mooyman <damian@silverstripe.com> Date: Mon Jun 19 17:42:42 2017 +1200 Fixed ORM tests commit d75a8d1d93398af4bd0432df9e4bc6295c15a3fe Author: Damian Mooyman <damian@silverstripe.com> Date: Mon Jun 19 17:15:07 2017 +1200 FIx i18n tests commit 06364af3c379c931889c4cc34dd920fee3db204a Author: Damian Mooyman <damian@silverstripe.com> Date: Mon Jun 19 16:59:34 2017 +1200 Fix controller namespace Move states to sub namespace commit 2a278e2953d2dbb19f78d91c919048e1fc935436 Author: Damian Mooyman <damian@silverstripe.com> Date: Mon Jun 19 12:49:45 2017 +1200 Fix forms namespace commit b65c21241bee019730027071d815dbf7571197a4 Author: Damian Mooyman <damian@silverstripe.com> Date: Thu Jun 15 18:56:48 2017 +1200 Update API usages commit d1d4375c95a264a6b63cbaefc2c1d12f808bfd82 Author: Damian Mooyman <damian@silverstripe.com> Date: Thu Jun 15 18:41:44 2017 +1200 API Refactor $flush into HTPPApplication API Enforce health check in Controller::pushCurrent() API Better global backup / restore Updated Director::test() to use new API commit b220534f06732db4fa940d8724c2a85c0ba2495a Author: Damian Mooyman <damian@silverstripe.com> Date: Tue Jun 13 22:05:57 2017 +1200 Move app nesting to a test state helper commit 603704165c08d0c1c81fd5e6bb9506326eeee17b Author: Damian Mooyman <damian@silverstripe.com> Date: Tue Jun 13 21:46:04 2017 +1200 Restore kernel stack to fix multi-level nesting commit 2f6336a15bf79dc8c2edd44cec1931da2dd51c28 Author: Damian Mooyman <damian@silverstripe.com> Date: Tue Jun 13 17:23:21 2017 +1200 API Implement kernel nesting commit fc7188da7d6ad6785354bab61f08700454c81d91 Author: Damian Mooyman <damian@silverstripe.com> Date: Tue Jun 13 15:43:13 2017 +1200 Fix core tests commit a0ae7235148fffd71f2f02d1fe7fe45bf3aa39eb Author: Damian Mooyman <damian@silverstripe.com> Date: Tue Jun 13 15:23:52 2017 +1200 Fix manifest tests commit ca033952513633e182040d3d13e1caa9000ca184 Author: Damian Mooyman <damian@silverstripe.com> Date: Tue Jun 13 15:00:00 2017 +1200 API Move extension management into test state commit c66d4339777663a8a04661fea32a0cf35b95d20f Author: Damian Mooyman <damian@silverstripe.com> Date: Tue Jun 13 14:10:59 2017 +1200 API Refactor SapphireTest state management into SapphireTestState API Remove Injector::unregisterAllObjects() API Remove FakeController commit f26ae75c6ecaafa0dec1093264e0187191e6764d Author: Damian Mooyman <damian@silverstripe.com> Date: Mon Jun 12 18:04:34 2017 +1200 Implement basic CLI application object commit 001d5596621404892de0a5413392379eff990641 Author: Damian Mooyman <damian@silverstripe.com> Date: Mon Jun 12 17:39:38 2017 +1200 Remove references to SapphireTest::is_running_test() Upgrade various code commit de079c041dacd96bc4f4b66421fa2b2cc4c320f8 Author: Damian Mooyman <damian@silverstripe.com> Date: Wed Jun 7 18:07:33 2017 +1200 API Implement APP object API Refactor of Session
2017-06-22 12:50:45 +02:00
Controller::curr(),
'Form',
new FieldList(),
new FieldList(
new FormAction('doSubmit')
)
);
}
}