2009-02-02 00:49:53 +01:00
|
|
|
<?php
|
|
|
|
/**
|
2012-04-12 08:02:46 +02:00
|
|
|
* @package framework
|
2009-02-02 00:49:53 +01:00
|
|
|
* @subpackage tests
|
|
|
|
*/
|
|
|
|
class DateTest extends SapphireTest {
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-04-02 19:16:34 +02:00
|
|
|
protected $originalTZ;
|
2012-03-31 09:08:25 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function setUp() {
|
2009-04-02 19:16:34 +02:00
|
|
|
// Set timezone to support timestamp->date conversion.
|
2012-03-31 09:08:25 +02:00
|
|
|
$this->originalTZ = date_default_timezone_get();
|
|
|
|
date_default_timezone_set('Pacific/Auckland');
|
2009-04-02 19:16:34 +02:00
|
|
|
parent::setUp();
|
|
|
|
}
|
2012-03-31 09:08:25 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function tearDown() {
|
2012-03-31 09:08:25 +02:00
|
|
|
date_default_timezone_set($this->originalTZ);
|
2009-04-02 19:16:34 +02:00
|
|
|
parent::tearDown();
|
|
|
|
}
|
2012-03-31 09:08:25 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testNiceDate() {
|
2012-04-04 16:48:16 +02:00
|
|
|
$this->assertEquals('31/03/2008', DBField::create_field('Date', 1206968400)->Nice(),
|
2009-04-02 18:45:10 +02:00
|
|
|
"Date->Nice() works with timestamp integers"
|
|
|
|
);
|
2012-04-04 16:48:16 +02:00
|
|
|
$this->assertEquals('30/03/2008', DBField::create_field('Date', 1206882000)->Nice(),
|
2009-04-02 18:45:10 +02:00
|
|
|
"Date->Nice() works with timestamp integers"
|
|
|
|
);
|
2012-04-04 16:48:16 +02:00
|
|
|
$this->assertEquals('31/03/2008', DBField::create_field('Date', '1206968400')->Nice(),
|
2009-04-02 18:45:10 +02:00
|
|
|
"Date->Nice() works with timestamp strings"
|
|
|
|
);
|
2012-04-04 16:48:16 +02:00
|
|
|
$this->assertEquals('30/03/2008', DBField::create_field('Date', '1206882000')->Nice(),
|
2009-04-02 18:45:10 +02:00
|
|
|
"Date->Nice() works with timestamp strings"
|
|
|
|
);
|
2012-03-27 06:57:42 +02:00
|
|
|
$this->assertEquals('04/03/2003', DBField::create_field('Date', '4/3/03')->Nice(),
|
2009-04-02 18:45:10 +02:00
|
|
|
"Date->Nice() works with D/M/YY format"
|
|
|
|
);
|
2012-03-27 06:57:42 +02:00
|
|
|
$this->assertEquals('04/03/2003', DBField::create_field('Date', '04/03/03')->Nice(),
|
2009-04-02 18:45:10 +02:00
|
|
|
"Date->Nice() works with DD/MM/YY format"
|
|
|
|
);
|
2012-03-27 06:57:42 +02:00
|
|
|
$this->assertEquals('04/03/2003', DBField::create_field('Date', '4/3/03')->Nice(),
|
2009-04-02 18:45:10 +02:00
|
|
|
"Date->Nice() works with D/M/YY format"
|
|
|
|
);
|
2012-03-27 06:57:42 +02:00
|
|
|
$this->assertEquals('04/03/2003', DBField::create_field('Date', '4/03/03')->Nice(),
|
2009-04-02 18:45:10 +02:00
|
|
|
"Date->Nice() works with D/M/YY format"
|
|
|
|
);
|
2012-03-27 06:57:42 +02:00
|
|
|
$this->assertEquals('04/03/2003', DBField::create_field('Date', '4/3/2003')->Nice(),
|
2009-04-02 18:45:10 +02:00
|
|
|
"Date->Nice() works with D/M/YYYY format"
|
|
|
|
);
|
2012-03-27 06:57:42 +02:00
|
|
|
$this->assertEquals('04/03/2003', DBField::create_field('Date', '4-3-2003')->Nice(),
|
2009-04-02 18:45:10 +02:00
|
|
|
"Date->Nice() works with D-M-YYYY format"
|
|
|
|
);
|
2012-03-27 06:57:42 +02:00
|
|
|
$this->assertEquals('04/03/2003', DBField::create_field('Date', '2003-03-04')->Nice(),
|
2009-04-02 18:45:10 +02:00
|
|
|
"Date->Nice() works with YYYY-MM-DD format"
|
|
|
|
);
|
2012-03-27 06:57:42 +02:00
|
|
|
$this->assertEquals('04/03/2003', DBField::create_field('Date', '04/03/2003')->Nice(),
|
2009-04-02 18:45:10 +02:00
|
|
|
"Date->Nice() works with DD/MM/YYYY format"
|
|
|
|
);
|
2012-03-27 06:57:42 +02:00
|
|
|
$this->assertEquals('04/03/2003', DBField::create_field('Date', '04-03-2003')->Nice(),
|
2009-04-02 18:45:10 +02:00
|
|
|
"Date->Nice() works with DD/MM/YYYY format"
|
|
|
|
);
|
2009-02-02 00:49:53 +01:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testNiceUS(){
|
2012-05-15 17:36:24 +02:00
|
|
|
$this->assertEquals('03/31/2008', DBField::create_field('Date', 1206968400)->NiceUs(),
|
|
|
|
"Date->NiceUs() works with timestamp integers"
|
|
|
|
);
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testYear(){
|
2012-05-15 17:36:24 +02:00
|
|
|
$this->assertEquals('2008', DBField::create_field('Date', 1206968400)->Year(),
|
|
|
|
"Date->Year() works with timestamp integers"
|
|
|
|
);
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testDay(){
|
2012-05-15 17:36:24 +02:00
|
|
|
$this->assertEquals('Monday', DBField::create_field('Date', 1206968400)->Day(),
|
|
|
|
"Date->Day() works with timestamp integers"
|
|
|
|
);
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testMonth(){
|
2012-05-15 17:36:24 +02:00
|
|
|
$this->assertEquals('March', DBField::create_field('Date', 1206968400)->Month(),
|
|
|
|
"Date->Month() works with timestamp integers"
|
|
|
|
);
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testShortMonth(){
|
2012-05-15 17:36:24 +02:00
|
|
|
$this->assertEquals('Mar', DBField::create_field('Date', 1206968400)->ShortMonth(),
|
|
|
|
"Date->ShortMonth() works with timestamp integers"
|
|
|
|
);
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testLongDate() {
|
2012-04-04 16:48:16 +02:00
|
|
|
$this->assertEquals('31 March 2008', DBField::create_field('Date', 1206968400)->Long(),
|
2009-04-02 18:45:10 +02:00
|
|
|
"Date->Long() works with numeric timestamp"
|
|
|
|
);
|
2012-04-04 16:48:16 +02:00
|
|
|
$this->assertEquals('31 March 2008', DBField::create_field('Date', '1206968400')->Long(),
|
2009-04-02 18:45:10 +02:00
|
|
|
"Date->Long() works with string timestamp"
|
|
|
|
);
|
2012-04-04 16:48:16 +02:00
|
|
|
$this->assertEquals('30 March 2008', DBField::create_field('Date', 1206882000)->Long(),
|
2009-04-02 18:45:10 +02:00
|
|
|
"Date->Long() works with numeric timestamp"
|
|
|
|
);
|
2012-04-04 16:48:16 +02:00
|
|
|
$this->assertEquals('30 March 2008', DBField::create_field('Date', '1206882000')->Long(),
|
2009-04-02 18:45:10 +02:00
|
|
|
"Date->Long() works with numeric timestamp"
|
|
|
|
);
|
2012-03-27 06:57:42 +02:00
|
|
|
$this->assertEquals('3 April 2003', DBField::create_field('Date', '2003-4-3')->Long(),
|
2009-04-02 18:45:10 +02:00
|
|
|
"Date->Long() works with YYYY-M-D"
|
|
|
|
);
|
2012-03-27 06:57:42 +02:00
|
|
|
$this->assertEquals('3 April 2003', DBField::create_field('Date', '3/4/2003')->Long(),
|
2009-04-02 18:45:10 +02:00
|
|
|
"Date->Long() works with D/M/YYYY"
|
|
|
|
);
|
2009-02-02 00:49:53 +01:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testFull(){
|
2012-05-15 17:36:24 +02:00
|
|
|
$this->assertEquals('31 Mar 2008', DBField::create_field('Date', 1206968400)->Full(),
|
|
|
|
"Date->Full() works with timestamp integers"
|
|
|
|
);
|
|
|
|
}
|
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('Date', '');
|
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('Date', 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('Date', 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('Date', array());
|
2012-02-24 04:18:36 +01:00
|
|
|
$this->assertNull($date->getValue(), 'Empty array evaluates to NULL');
|
|
|
|
|
2012-03-27 06:57:42 +02:00
|
|
|
$date = DBField::create_field('Date', '0');
|
2012-02-24 04:18:36 +01:00
|
|
|
$this->assertEquals('1970-01-01', $date->getValue(), 'Zero is UNIX epoch date');
|
|
|
|
|
2012-03-27 06:57:42 +02:00
|
|
|
$date = DBField::create_field('Date', 0);
|
2012-02-24 04:18:36 +01:00
|
|
|
$this->assertEquals('1970-01-01', $date->getValue(), 'Zero is UNIX epoch date');
|
|
|
|
}
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testDayOfMonth() {
|
2012-03-27 06:57:42 +02:00
|
|
|
$date = DBField::create_field('Date', '2000-10-10');
|
2012-02-24 04:49:41 +01:00
|
|
|
$this->assertEquals('10', $date->DayOfMonth());
|
|
|
|
$this->assertEquals('10th', $date->DayOfMonth(true));
|
|
|
|
|
2012-03-27 06:57:42 +02:00
|
|
|
$range = $date->RangeString(DBField::create_field('Date', '2000-10-20'));
|
2012-02-24 04:49:41 +01:00
|
|
|
$this->assertEquals('10 - 20 Oct 2000', $range);
|
2012-03-27 06:57:42 +02:00
|
|
|
$range = $date->RangeString(DBField::create_field('Date', '2000-10-20'), true);
|
2012-02-24 04:49:41 +01:00
|
|
|
$this->assertEquals('10th - 20th Oct 2000', $range);
|
|
|
|
}
|
2012-04-12 06:38:03 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testExtendedDates() {
|
2012-04-12 06:38:03 +02:00
|
|
|
$date = DBField::create_field('Date', '1800-10-10');
|
|
|
|
$this->assertEquals('10 Oct 1800', $date->Format('d M Y'));
|
|
|
|
|
|
|
|
$date = DBField::create_field('Date', '1500-10-10');
|
|
|
|
$this->assertEquals('10 Oct 1500', $date->Format('d M Y'));
|
|
|
|
|
|
|
|
$date = DBField::create_field('Date', '3000-4-3');
|
|
|
|
$this->assertEquals('03 Apr 3000', $date->Format('d M Y'));
|
|
|
|
}
|
|
|
|
|
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('Date', '1990-12-31')->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('Date', '1990-12-30')->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('Date', '1999-12-30')->Ago(),
|
|
|
|
'Approximate past match in singular'
|
|
|
|
);
|
|
|
|
|
|
|
|
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('Date', '2010-12-31')->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 day',
|
2012-12-13 19:01:27 +01:00
|
|
|
DBField::create_field('Date', '2001-01-01')->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->write();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2014-03-11 03:42:11 +01:00
|
|
|
$fixtures = array(
|
|
|
|
'2000-12-31' => '31/12/2000',
|
|
|
|
'31-12-2000' => '31/12/2000',
|
2014-04-01 06:03:10 +02:00
|
|
|
'31/12/2000' => '31/12/2000',
|
|
|
|
'2014-04-01' => '01/04/2014'
|
2014-03-11 03:42:11 +01:00
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2014-03-11 03:42:11 +01:00
|
|
|
foreach($fixtures as $from => $to) {
|
|
|
|
$date = DBField::create_field('Date', $from);
|
|
|
|
// 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
|
|
|
|
2009-02-02 00:49:53 +01:00
|
|
|
}
|