2009-06-18 02:10:02 +02:00
|
|
|
<?php
|
|
|
|
/**
|
API CHANGE: Renamed conflicting classes to have an "SS_" namespace, and renamed existing "SS" namespace to "SS_". The affected classes are: HTTPRequest, HTTPResponse, Query, Database, SSBacktrace, SSCli, SSDatetime, SSDatetimeTest, SSLog, SSLogTest, SSLogEmailWriter, SSLogErrorEmailFormatter, SSLogErrorFileFormatter, SSLogFileWriter and SSZendLog.
MINOR: Replaced usage of renamed classes with the new namespaced name.
From: Andrew Short <andrewjshort@gmail.com>
git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@90075 467b73ca-7a2a-4603-9d3b-597d59a354a9
2009-10-26 04:06:31 +01:00
|
|
|
* Tests for {@link SS_Datetime} class.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2009-06-18 02:10:02 +02:00
|
|
|
* @todo Current date comparisons are slightly dodgy, as they only compare
|
|
|
|
* the current date (not hour, minute, second) and assume that the date
|
|
|
|
* doesn't switch throughout the test execution. This means tests might
|
|
|
|
* fail when run at 23:59:59.
|
|
|
|
*
|
2012-04-12 08:02:46 +02:00
|
|
|
* @package framework
|
2009-06-18 02:10:02 +02:00
|
|
|
* @subpackage tests
|
|
|
|
*/
|
API CHANGE: Renamed conflicting classes to have an "SS_" namespace, and renamed existing "SS" namespace to "SS_". The affected classes are: HTTPRequest, HTTPResponse, Query, Database, SSBacktrace, SSCli, SSDatetime, SSDatetimeTest, SSLog, SSLogTest, SSLogEmailWriter, SSLogErrorEmailFormatter, SSLogErrorFileFormatter, SSLogFileWriter and SSZendLog.
MINOR: Replaced usage of renamed classes with the new namespaced name.
From: Andrew Short <andrewjshort@gmail.com>
git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@90075 467b73ca-7a2a-4603-9d3b-597d59a354a9
2009-10-26 04:06:31 +01:00
|
|
|
class SS_DatetimeTest extends SapphireTest {
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testNowWithSystemDate() {
|
2012-03-27 06:57:42 +02:00
|
|
|
$systemDatetime = DBField::create_field('SS_Datetime', date('Y-m-d H:i:s'));
|
API CHANGE: Renamed conflicting classes to have an "SS_" namespace, and renamed existing "SS" namespace to "SS_". The affected classes are: HTTPRequest, HTTPResponse, Query, Database, SSBacktrace, SSCli, SSDatetime, SSDatetimeTest, SSLog, SSLogTest, SSLogEmailWriter, SSLogErrorEmailFormatter, SSLogErrorFileFormatter, SSLogFileWriter and SSZendLog.
MINOR: Replaced usage of renamed classes with the new namespaced name.
From: Andrew Short <andrewjshort@gmail.com>
git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@90075 467b73ca-7a2a-4603-9d3b-597d59a354a9
2009-10-26 04:06:31 +01:00
|
|
|
$nowDatetime = SS_Datetime::now();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-06-18 02:10:02 +02:00
|
|
|
$this->assertEquals($systemDatetime->Date(), $nowDatetime->Date());
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testNowWithMockDate() {
|
2009-06-18 02:10:02 +02:00
|
|
|
// Test setting
|
|
|
|
$mockDate = '2001-12-31 22:10:59';
|
API CHANGE: Renamed conflicting classes to have an "SS_" namespace, and renamed existing "SS" namespace to "SS_". The affected classes are: HTTPRequest, HTTPResponse, Query, Database, SSBacktrace, SSCli, SSDatetime, SSDatetimeTest, SSLog, SSLogTest, SSLogEmailWriter, SSLogErrorEmailFormatter, SSLogErrorFileFormatter, SSLogFileWriter and SSZendLog.
MINOR: Replaced usage of renamed classes with the new namespaced name.
From: Andrew Short <andrewjshort@gmail.com>
git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@90075 467b73ca-7a2a-4603-9d3b-597d59a354a9
2009-10-26 04:06:31 +01:00
|
|
|
SS_Datetime::set_mock_now($mockDate);
|
2012-03-27 06:57:42 +02:00
|
|
|
$systemDatetime = DBField::create_field('SS_Datetime', date('Y-m-d H:i:s'));
|
API CHANGE: Renamed conflicting classes to have an "SS_" namespace, and renamed existing "SS" namespace to "SS_". The affected classes are: HTTPRequest, HTTPResponse, Query, Database, SSBacktrace, SSCli, SSDatetime, SSDatetimeTest, SSLog, SSLogTest, SSLogEmailWriter, SSLogErrorEmailFormatter, SSLogErrorFileFormatter, SSLogFileWriter and SSZendLog.
MINOR: Replaced usage of renamed classes with the new namespaced name.
From: Andrew Short <andrewjshort@gmail.com>
git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@90075 467b73ca-7a2a-4603-9d3b-597d59a354a9
2009-10-26 04:06:31 +01:00
|
|
|
$nowDatetime = SS_Datetime::now();
|
2009-06-18 02:10:02 +02:00
|
|
|
$this->assertNotEquals($systemDatetime->Date(), $nowDatetime->Date());
|
|
|
|
$this->assertEquals($nowDatetime->getValue(), $mockDate);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-06-18 02:10:02 +02:00
|
|
|
// Test clearing
|
API CHANGE: Renamed conflicting classes to have an "SS_" namespace, and renamed existing "SS" namespace to "SS_". The affected classes are: HTTPRequest, HTTPResponse, Query, Database, SSBacktrace, SSCli, SSDatetime, SSDatetimeTest, SSLog, SSLogTest, SSLogEmailWriter, SSLogErrorEmailFormatter, SSLogErrorFileFormatter, SSLogFileWriter and SSZendLog.
MINOR: Replaced usage of renamed classes with the new namespaced name.
From: Andrew Short <andrewjshort@gmail.com>
git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@90075 467b73ca-7a2a-4603-9d3b-597d59a354a9
2009-10-26 04:06:31 +01:00
|
|
|
SS_Datetime::clear_mock_now();
|
2012-03-27 06:57:42 +02:00
|
|
|
$systemDatetime = DBField::create_field('SS_Datetime', date('Y-m-d H:i:s'));
|
API CHANGE: Renamed conflicting classes to have an "SS_" namespace, and renamed existing "SS" namespace to "SS_". The affected classes are: HTTPRequest, HTTPResponse, Query, Database, SSBacktrace, SSCli, SSDatetime, SSDatetimeTest, SSLog, SSLogTest, SSLogEmailWriter, SSLogErrorEmailFormatter, SSLogErrorFileFormatter, SSLogFileWriter and SSZendLog.
MINOR: Replaced usage of renamed classes with the new namespaced name.
From: Andrew Short <andrewjshort@gmail.com>
git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@90075 467b73ca-7a2a-4603-9d3b-597d59a354a9
2009-10-26 04:06:31 +01:00
|
|
|
$nowDatetime = SS_Datetime::now();
|
2009-06-18 02:10:02 +02:00
|
|
|
$this->assertEquals($systemDatetime->Date(), $nowDatetime->Date());
|
|
|
|
}
|
2012-02-24 04:18:36 +01:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testSetNullAndZeroValues() {
|
2012-03-27 06:57:42 +02:00
|
|
|
$date = DBField::create_field('SS_Datetime', '');
|
2012-02-24 04:18:36 +01:00
|
|
|
$this->assertNull($date->getValue(), 'Empty string evaluates to NULL');
|
|
|
|
|
2012-03-27 06:57:42 +02:00
|
|
|
$date = DBField::create_field('SS_Datetime', null);
|
2012-02-24 04:18:36 +01:00
|
|
|
$this->assertNull($date->getValue(), 'NULL is set as NULL');
|
|
|
|
|
2012-03-27 06:57:42 +02:00
|
|
|
$date = DBField::create_field('SS_Datetime', false);
|
2012-02-24 04:18:36 +01:00
|
|
|
$this->assertNull($date->getValue(), 'Boolean FALSE evaluates to NULL');
|
|
|
|
|
2012-03-27 06:57:42 +02:00
|
|
|
$date = DBField::create_field('SS_Datetime', '0');
|
2012-03-10 00:34:16 +01:00
|
|
|
$this->assertEquals('1970-01-01 00:00:00', $date->getValue(), 'String zero is UNIX epoch time');
|
2012-02-24 04:18:36 +01:00
|
|
|
|
2012-03-27 06:57:42 +02:00
|
|
|
$date = DBField::create_field('SS_Datetime', 0);
|
2012-03-10 00:34:16 +01:00
|
|
|
$this->assertEquals('1970-01-01 00:00:00', $date->getValue(), 'Numeric zero is UNIX epoch time');
|
2012-02-24 04:18:36 +01:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testExtendedDateTimes() {
|
2012-05-15 17:36:24 +02:00
|
|
|
$date = DBField::create_field('SS_Datetime', '1500-10-10 15:32:24');
|
|
|
|
$this->assertEquals('10 Oct 1500 15 32 24', $date->Format('d M Y H i s'));
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-05-15 17:36:24 +02:00
|
|
|
$date = DBField::create_field('SS_Datetime', '3000-10-10 15:32:24');
|
|
|
|
$this->assertEquals('10 Oct 3000 15 32 24', $date->Format('d M Y H i s'));
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testNice() {
|
2012-05-15 17:36:24 +02:00
|
|
|
$date = DBField::create_field('SS_Datetime', '2001-12-31 22:10:59');
|
|
|
|
$this->assertEquals('31/12/2001 10:10pm', $date->Nice());
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testNice24() {
|
2012-05-15 17:36:24 +02:00
|
|
|
$date = DBField::create_field('SS_Datetime', '2001-12-31 22:10:59');
|
|
|
|
$this->assertEquals('31/12/2001 22:10', $date->Nice24());
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testDate() {
|
2012-05-15 17:36:24 +02:00
|
|
|
$date = DBField::create_field('SS_Datetime', '2001-12-31 22:10:59');
|
|
|
|
$this->assertEquals('31/12/2001', $date->Date());
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testTime() {
|
2012-05-15 17:36:24 +02:00
|
|
|
$date = DBField::create_field('SS_Datetime', '2001-12-31 22:10:59');
|
|
|
|
$this->assertEquals('10:10pm', $date->Time());
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testTime24() {
|
2012-05-15 17:36:24 +02:00
|
|
|
$date = DBField::create_field('SS_Datetime', '2001-12-31 22:10:59');
|
|
|
|
$this->assertEquals('22:10', $date->Time24());
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testURLDateTime(){
|
2012-05-15 17:36:24 +02:00
|
|
|
$date = DBField::create_field('SS_Datetime', '2001-12-31 22:10:59');
|
|
|
|
$this->assertEquals('2001-12-31%2022:10:59', $date->URLDateTime());
|
|
|
|
}
|
2012-02-24 04:18:36 +01:00
|
|
|
|
2012-12-13 19:01:27 +01:00
|
|
|
public function testAgoInPast() {
|
|
|
|
SS_Datetime::set_mock_now('2000-12-31 12:00:00');
|
|
|
|
|
|
|
|
$this->assertEquals(
|
2014-08-15 08:53:05 +02:00
|
|
|
'10 years ago',
|
2012-12-13 19:01:27 +01:00
|
|
|
DBField::create_field('SS_Datetime', '1990-12-31 12:00:00')->Ago(),
|
|
|
|
'Exact past match on years'
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertEquals(
|
2014-08-15 08:53:05 +02:00
|
|
|
'10 years ago',
|
2012-12-13 19:01:27 +01:00
|
|
|
DBField::create_field('SS_Datetime', '1990-12-30 12:00:00')->Ago(),
|
|
|
|
'Approximate past match on years'
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertEquals(
|
2014-08-15 08:53:05 +02:00
|
|
|
'1 year ago',
|
2012-12-13 19:01:27 +01:00
|
|
|
DBField::create_field('SS_Datetime', '1999-12-30 12:00:12')->Ago(),
|
|
|
|
'Approximate past match in singular'
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertEquals(
|
2014-08-15 08:53:05 +02:00
|
|
|
'50 mins ago',
|
2012-12-13 19:01:27 +01:00
|
|
|
DBField::create_field('SS_Datetime', '2000-12-31 11:10:11')->Ago(),
|
|
|
|
'Approximate past match on minutes'
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertEquals(
|
2014-08-15 08:53:05 +02:00
|
|
|
'59 secs ago',
|
2012-12-13 19:01:27 +01:00
|
|
|
DBField::create_field('SS_Datetime', '2000-12-31 11:59:01')->Ago(),
|
|
|
|
'Approximate past match on seconds'
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertEquals(
|
2014-08-15 08:53:05 +02:00
|
|
|
'less than a minute ago',
|
2012-12-13 19:01:27 +01:00
|
|
|
DBField::create_field('SS_Datetime', '2000-12-31 11:59:01')->Ago(false),
|
|
|
|
'Approximate past match on seconds with $includeSeconds=false'
|
|
|
|
);
|
|
|
|
|
|
|
|
SS_Datetime::clear_mock_now();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testAgoInFuture() {
|
|
|
|
SS_Datetime::set_mock_now('2000-12-31 00:00:00');
|
|
|
|
|
|
|
|
$this->assertEquals(
|
2014-08-15 08:53:05 +02:00
|
|
|
'in 10 years',
|
2012-12-14 11:25:24 +01:00
|
|
|
DBField::create_field('SS_Datetime', '2010-12-31 12:00:00')->Ago(),
|
2012-12-13 19:01:27 +01:00
|
|
|
'Exact past match on years'
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertEquals(
|
2014-08-15 08:53:05 +02:00
|
|
|
'in 1 hour',
|
2012-12-13 19:01:27 +01:00
|
|
|
DBField::create_field('SS_Datetime', '2000-12-31 1:01:05')->Ago(),
|
|
|
|
'Approximate past match on minutes'
|
|
|
|
);
|
|
|
|
|
|
|
|
SS_Datetime::clear_mock_now();
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
|
|
|
public function testFormatFromSettings() {
|
|
|
|
|
2014-03-11 03:42:11 +01:00
|
|
|
$memberID = $this->logInWithPermission();
|
|
|
|
$member = DataObject::get_by_id('Member', $memberID);
|
|
|
|
$member->DateFormat = 'dd/MM/YYYY';
|
|
|
|
$member->TimeFormat = 'hh:mm:ss';
|
|
|
|
$member->write();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2014-03-11 03:42:11 +01:00
|
|
|
$fixtures = array(
|
|
|
|
'2000-12-31 10:11:01' => '31/12/2000 10:11:01',
|
|
|
|
'2000-12-31 1:11:01' => '31/12/2000 01:11:01',
|
|
|
|
'12/12/2000 1:11:01' => '12/12/2000 01:11:01',
|
|
|
|
'2000-12-31' => '31/12/2000 12:00:00',
|
2014-04-01 06:03:10 +02:00
|
|
|
'2014-04-01 10:11:01' => '01/04/2014 10:11:01',
|
2014-03-11 03:42:11 +01:00
|
|
|
'10:11:01' => date('d/m/Y').' 10:11:01'
|
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2014-03-11 03:42:11 +01:00
|
|
|
foreach($fixtures as $from => $to) {
|
2014-04-01 06:03:10 +02:00
|
|
|
$date = DBField::create_field('SS_Datetime', $from);
|
2014-03-11 03:42:11 +01:00
|
|
|
// With member
|
|
|
|
$this->assertEquals($to, $date->FormatFromSettings($member));
|
|
|
|
// Without member
|
|
|
|
$this->assertEquals($to, $date->FormatFromSettings());
|
2014-08-15 08:53:05 +02:00
|
|
|
}
|
|
|
|
}
|
2012-12-13 19:01:27 +01:00
|
|
|
|
2012-02-24 04:18:36 +01:00
|
|
|
}
|