414 lines
18 KiB
Text
414 lines
18 KiB
Text
<?php
|
|
/**
|
|
* Test Date API functions
|
|
*/
|
|
class DateAPITestCase extends DrupalWebTestCase {
|
|
function getInfo() {
|
|
return array(
|
|
'name' => t('Date API'),
|
|
'description' => t('Test Date API functions.') ,
|
|
'group' => t('Date'),
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Implementation of setUp().
|
|
*/
|
|
public function setUp() {
|
|
// Load the date_api module.
|
|
parent::setUp('date_api', 'date_timezone');
|
|
variable_set('date_api_use_iso8601', FALSE);
|
|
variable_set('date_first_day', 1);
|
|
}
|
|
|
|
function testDateAPI() {
|
|
|
|
$value = '2007-12-05 23:59';
|
|
$this->assertEqual(TRUE, date_part_extract($value, 'year'), "Test date_part_extract(". $value .", year), results ". date_part_extract($value, 'year'));
|
|
$this->assertEqual(TRUE, date_part_extract($value, 'month'), "Test date_part_extract(". $value .", mon), results ". date_part_extract($value, 'month'));
|
|
$this->assertEqual(TRUE, date_part_extract($value, 'day'), "Test date_part_extract(". $value .", mday), results ". date_part_extract($value, 'day'));
|
|
|
|
$this->assertEqual(TRUE, date_is_valid($value), "Test date_is_valid(". $value .")");
|
|
$value = '2007-00-00 00:00';
|
|
$this->assertNotEqual(TRUE, date_is_valid($value), "Test for invalid date_is_valid(". $value .")");
|
|
$value = '0000-00-00 00:00';
|
|
$this->assertNotEqual(TRUE, date_is_valid($value), "Test for invalid date_is_valid(". $value .")");
|
|
$value = '-100';
|
|
$this->assertNotEqual(TRUE, date_is_valid($value), "Test for invalid date_is_valid(". $value .")");
|
|
$value = '2007-00-01T00:00';
|
|
$this->assertEqual(TRUE, date_is_valid($value, DATE_ISO), "Test ISO exception to date_is_valid(". $value .", DATE_ISO)");
|
|
|
|
$dates = array(
|
|
'2007-01-01 00:00:00',
|
|
'1970-01-01 00:00:00',
|
|
'1900-01-01 00:00:00',
|
|
'1600-01-01 00:00:00',
|
|
'0100-01-01 00:00:00');
|
|
foreach ($dates as $date) {
|
|
$unix = date_convert($date, DATE_DATETIME, DATE_UNIX);
|
|
$datetime = date_convert($unix, DATE_UNIX, DATE_DATETIME);
|
|
$this->assertEqual($date, $datetime, 'Test roundtrip using date_convert() from DATE_DATETIME to DATE_UNIX back to DATE_DATETIME, results '. $date .' >> '. $unix .' >> '. $datetime);
|
|
}
|
|
|
|
// Test date_format_date().
|
|
$formatters = array(
|
|
'a',
|
|
'A',
|
|
'B',
|
|
'c',
|
|
'd',
|
|
'D',
|
|
'e',
|
|
'F',
|
|
'g',
|
|
'G',
|
|
'h',
|
|
'H',
|
|
'i',
|
|
'I',
|
|
'j',
|
|
'l',
|
|
'L',
|
|
'm',
|
|
'M',
|
|
'n',
|
|
'N',
|
|
'o',
|
|
'O',
|
|
'P',
|
|
'r',
|
|
'R',
|
|
's',
|
|
'S',
|
|
't',
|
|
'T',
|
|
'u',
|
|
'U',
|
|
'w',
|
|
'W',
|
|
'y',
|
|
'Y',
|
|
'z',
|
|
'Z',
|
|
);
|
|
foreach ($formatters as $formatter) {
|
|
$date_api_format = date_format_date(date_now(), 'custom', $formatter);
|
|
$php_format = date_format(date_now(), $formatter);
|
|
$this->assertEqual($date_api_format, $php_format, 'Test that the "' . $formatter . '" formatter is formatted correctly by date_format_date()');
|
|
}
|
|
|
|
// Test the order of the weeks days for a calendar that starts on Monday and one that starts on Sunday.
|
|
variable_set('date_first_day', 1);
|
|
$expected = array( 0 => t('Mon'), 1 => t('Tue'), 2 => t('Wed'), 3 => t('Thu'), 4 => t('Fri'), 5 => t('Sat'), 6 => t('Sun'), );
|
|
$days = date_week_days_ordered(date_week_days_abbr(1));
|
|
$this->assertEqual($expected, $days, 'Test that date_week_days_ordered() array starts on Monday when the site first day is on Monday.');
|
|
variable_set('date_first_day', 0);
|
|
$expected = array( 0 => t('Sun'), 1 => t('Mon'), 2 => t('Tue'), 3 => t('Wed'), 4 => t('Thu'), 5 => t('Fri'), 6 => t('Sat'), );
|
|
$days = date_week_days_ordered(date_week_days_abbr(1));
|
|
$this->assertEqual($expected, $days, 'Test that date_week_days_ordered() array starts on Sunday when the site first day is on Sunday.');
|
|
|
|
// Test days in February for a leap year and a non-leap year.
|
|
$expected = 28;
|
|
$value = date_days_in_month(2005, 2);
|
|
$this->assertEqual($expected, $value, "Test date_days_in_month(2, 2005): should be $expected, found $value.");
|
|
$expected = 29;
|
|
$value = date_days_in_month(2004, 2);
|
|
$this->assertEqual($expected, $value, "Test date_days_in_month(2, 2004): should be $expected, found $value.");
|
|
|
|
// Test days in year for a leap year and a non-leap year.
|
|
$expected = 365;
|
|
$value = date_days_in_year('2005-06-01 00:00:00', DATE_DATETIME);
|
|
$this->assertEqual($expected, $value, "Test date_days_in_year(2005-06-01, DATE_DATETIME): should be $expected, found $value.");
|
|
$expected = 366;
|
|
$value = date_days_in_year('2004-06-01 00:00:00', DATE_DATETIME);
|
|
$this->assertEqual($expected, $value, "Test date_days_in_year(2004-06-01, DATE_DATETIME): should be $expected, found $value.");
|
|
|
|
// Test ISO weeks for a leap year and a non-leap year.
|
|
$expected = 52;
|
|
$value = date_iso_weeks_in_year('2008-06-01 00:00:00', DATE_DATETIME);
|
|
$this->assertEqual($expected, $value, "Test date_iso_weeks_in_year(2008-06-01, DATE_DATETIME): should be $expected, found $value.");
|
|
$expected = 53;
|
|
$value = date_iso_weeks_in_year('2009-06-01 00:00:00', DATE_DATETIME);
|
|
$this->assertEqual($expected, $value, "Test date_iso_weeks_in_year(2009-06-01, DATE_DATETIME): should be $expected, found $value.");
|
|
|
|
// Test day of week for March 1, the day after leap day.
|
|
$expected = 6;
|
|
$value = date_day_of_week('2008-03-01 00:00:00', DATE_DATETIME);
|
|
$this->assertEqual($expected, $value, "Test date_day_of_week(2008-03-01, DATE_DATETIME): should be $expected, found $value.");
|
|
$expected = 0;
|
|
$value = date_day_of_week('2009-03-01 00:00:00', DATE_DATETIME);
|
|
$this->assertEqual($expected, $value, "Test date_day_of_week(2009-03-01, DATE_DATETIME): should be $expected, found $value.");
|
|
|
|
// Test day of week name for March 1, the day after leap day.
|
|
$expected = 'Sat';
|
|
$value = date_day_of_week_name('2008-03-01 00:00:00', DATE_DATETIME);
|
|
$this->assertEqual($expected, $value, "Test date_day_of_week_name(2008-03-01, DATE_DATETIME): should be $expected, found $value.");
|
|
$expected = 'Sun';
|
|
$value = date_day_of_week_name('2009-03-01 00:00:00', DATE_DATETIME);
|
|
$this->assertEqual($expected, $value, "Test date_day_of_week_name(2009-03-01, DATE_DATETIME): should be $expected, found $value.");
|
|
|
|
// Test week range with calendar weeks.
|
|
variable_set('date_first_day', 0);
|
|
variable_set('date_api_use_iso8601', FALSE);
|
|
$expected = '2008-01-27 to 2008-02-03';
|
|
$result = date_week_range(5, 2008);
|
|
$value = $result[0]->format(DATE_FORMAT_DATE) .' to '. $result[1]->format(DATE_FORMAT_DATE);
|
|
$this->assertEqual($expected, $value, "Test calendar date_week_range(5, 2008): should be $expected, found $value.");
|
|
$expected = '2009-01-25 to 2009-02-01';
|
|
$result = date_week_range(5, 2009);
|
|
$value = $result[0]->format(DATE_FORMAT_DATE) .' to '. $result[1]->format(DATE_FORMAT_DATE);
|
|
$this->assertEqual($expected, $value, "Test calendar date_week_range(5, 2009): should be $expected, found $value.");
|
|
|
|
// And now with ISO weeks.
|
|
variable_set('date_first_day', 1);
|
|
variable_set('date_api_use_iso8601', TRUE);
|
|
$expected = '2008-01-28 to 2008-02-04';
|
|
$result = date_week_range(5, 2008);
|
|
$value = $result[0]->format(DATE_FORMAT_DATE) .' to '. $result[1]->format(DATE_FORMAT_DATE);
|
|
$this->assertEqual($expected, $value, "Test ISO date_week_range(5, 2008): should be $expected, found $value.");
|
|
$expected = '2009-01-26 to 2009-02-02';
|
|
$result = date_week_range(5, 2009);
|
|
$value = $result[0]->format(DATE_FORMAT_DATE) .' to '. $result[1]->format(DATE_FORMAT_DATE);
|
|
$this->assertEqual($expected, $value, "Test ISO date_week_range(5, 2009): should be $expected, found $value.");
|
|
variable_set('date_api_use_iso8601', FALSE);
|
|
|
|
// Find calendar week for a date.
|
|
variable_set('date_first_day', 0);
|
|
$expected = '09';
|
|
$value = date_week('2008-03-01');
|
|
$this->assertEqual($expected, $value, "Test date_week(2008-03-01): should be $expected, found $value.");
|
|
$expected = '10';
|
|
$value = date_week('2009-03-01');
|
|
$this->assertEqual($expected, $value, "Test date_week(2009-03-01): should be $expected, found $value.");
|
|
|
|
// Create date object from datetime string.
|
|
$input = '2009-03-07 10:30';
|
|
$timezone = 'America/Chicago';
|
|
$date = date_make_date($input, $timezone);
|
|
$value = date_format($date, 'c');
|
|
$expected = '2009-03-07T10:30:00-06:00';
|
|
$this->assertEqual($expected, $value, "Test date_make_date($input, $timezone): should be $expected, found $value.");
|
|
|
|
// Same during daylight savings time.
|
|
$input = '2009-06-07 10:30';
|
|
$timezone = 'America/Chicago';
|
|
$date = date_make_date($input, $timezone);
|
|
$value = date_format($date, 'c');
|
|
$expected = '2009-06-07T10:30:00-05:00';
|
|
$this->assertEqual($expected, $value, "Test date_make_date($input, $timezone): should be $expected, found $value.");
|
|
|
|
// Create date object from date string.
|
|
$input = '2009-03-07';
|
|
$timezone = 'America/Chicago';
|
|
$date = date_make_date($input, $timezone);
|
|
$value = date_format($date, 'c');
|
|
$expected = '2009-03-07T00:00:00-06:00';
|
|
$this->assertEqual($expected, $value, "Test date_make_date($input, $timezone): should be $expected, found $value.");
|
|
|
|
// Same during daylight savings time.
|
|
$input = '2009-06-07';
|
|
$timezone = 'America/Chicago';
|
|
$date = date_make_date($input, $timezone);
|
|
$value = date_format($date, 'c');
|
|
$expected = '2009-06-07T00:00:00-05:00';
|
|
$this->assertEqual($expected, $value, "Test date_make_date($input, $timezone): should be $expected, found $value.");
|
|
|
|
// Create date object from date array, date only.
|
|
$input = array('year' => 2010, 'month' => 2, 'day' => 28);
|
|
$timezone = 'America/Chicago';
|
|
$granularity = array('year', 'month', 'day');
|
|
$date = date_make_date($input, $timezone, DATE_ARRAY, $granularity);
|
|
$value = date_format($date, 'c');
|
|
$expected = '2010-02-28T00:00:00-06:00';
|
|
$this->assertEqual($expected, $value, "Test date_make_date(array('year' => 2010, 'month' => 2, 'day' => 28), $timezone, DATE_ARRAY, array('year', 'month', 'day')): should be $expected, found $value.");
|
|
|
|
// Create date object from date array with hour.
|
|
$input = array('year' => 2010, 'month' => 2, 'day' => 28, 'hour' => 10);
|
|
$timezone = 'America/Chicago';
|
|
$granularity = array('year', 'month', 'day', 'hour');
|
|
$date = date_make_date($input, $timezone, DATE_ARRAY, $granularity);
|
|
$value = date_format($date, 'c');
|
|
$expected = '2010-02-28T10:00:00-06:00';
|
|
$this->assertEqual($expected, $value, "Test date_make_date(array('year' => 2010, 'month' => 2, 'day' => 28, 'hour' => 10), $timezone, DATE_ARRAY, array('year', 'month', 'day', 'hour')): should be $expected, found $value.");
|
|
|
|
// 0 = January 1, 1970 00:00:00 (UTC);
|
|
// 1000000000 = September 9, 2001 01:46:40 (UTC);
|
|
|
|
// Create date object from unix timestamp and convert it to a local date.
|
|
$input = 0;
|
|
$timezone = 'UTC';
|
|
$date = date_make_date($input, $timezone, DATE_UNIX);
|
|
$value = date_format($date, 'c');
|
|
$expected = '1970-01-01T00:00:00+00:00';
|
|
$this->assertEqual($expected, $value, "Test date_make_date($input, $timezone, DATE_UNIX): should be $expected, found $value.");
|
|
|
|
$expected = 'UTC';
|
|
$value = timezone_name_get(date_timezone_get($date));
|
|
$this->assertEqual($expected, $value, "The current timezone is $value: should be $expected.");
|
|
$expected = 0;
|
|
$value = date_offset_get($date);
|
|
$this->assertEqual($expected, $value, "The current offset is $value: should be $expected.");
|
|
|
|
$timezone = 'America/Los_Angeles';
|
|
date_timezone_set($date, timezone_open($timezone));
|
|
$value = date_format($date, 'c');
|
|
$expected = '1969-12-31T16:00:00-08:00';
|
|
$this->assertEqual($expected, $value, "Test date_timezone_set(\$date, timezone_open($timezone)): should be $expected, found $value.");
|
|
|
|
$expected = 'America/Los_Angeles';
|
|
$value = timezone_name_get(date_timezone_get($date));
|
|
$this->assertEqual($expected, $value, "The current timezone should be $expected, found $value.");
|
|
$expected = '-28800';
|
|
$value = date_offset_get($date);
|
|
$this->assertEqual($expected, $value, "The current offset should be $expected, found $value.");
|
|
|
|
// Convert the local version of a timestamp to UTC.
|
|
$input = 0;
|
|
$timezone = 'America/Los_Angeles';
|
|
$date = date_make_date($input, $timezone, DATE_UNIX);
|
|
$offset = date_offset_get($date);
|
|
$value = date_format($date, 'c');
|
|
$expected = '1969-12-31T16:00:00-08:00';
|
|
$this->assertEqual($expected, $value, "Test date_make_date($input, $timezone, DATE_UNIX): should be $expected, found $value.");
|
|
|
|
$expected = 'America/Los_Angeles';
|
|
$value = timezone_name_get(date_timezone_get($date));
|
|
$this->assertEqual($expected, $value, "The current timezone should be $expected, found $value.");
|
|
$expected = '-28800';
|
|
$value = date_offset_get($date);
|
|
$this->assertEqual($expected, $value, "The current offset should be $expected, found $value.");
|
|
|
|
$timezone = 'UTC';
|
|
date_timezone_set($date, timezone_open($timezone));
|
|
$value = date_format($date, 'c');
|
|
$expected = '1970-01-01T00:00:00+00:00';
|
|
$this->assertEqual($expected, $value, "Test date_timezone_set(\$date, timezone_open($timezone)): should be $expected, found $value.");
|
|
|
|
$expected = 'UTC';
|
|
$value = timezone_name_get(date_timezone_get($date));
|
|
$this->assertEqual($expected, $value, "The current timezone should be $expected, found $value.");
|
|
$expected = '0';
|
|
$value = date_offset_get($date);
|
|
$this->assertEqual($expected, $value, "The current offset should be $expected, found $value.");
|
|
|
|
// Create date object from datetime string and convert it to a local date.
|
|
$input = '1970-01-01 00:00:00';
|
|
$timezone = 'UTC';
|
|
$date = date_make_date($input, $timezone);
|
|
$value = date_format($date, 'c');
|
|
$expected = '1970-01-01T00:00:00+00:00';
|
|
$this->assertEqual($expected, $value, "Test date_make_date('$input', '$timezone'): should be $expected, found $value.");
|
|
|
|
$expected = 'UTC';
|
|
$value = timezone_name_get(date_timezone_get($date));
|
|
$this->assertEqual($expected, $value, "The current timezone is $value: should be $expected.");
|
|
$expected = 0;
|
|
$value = date_offset_get($date);
|
|
$this->assertEqual($expected, $value, "The current offset is $value: should be $expected.");
|
|
|
|
$timezone = 'America/Los_Angeles';
|
|
date_timezone_set($date, timezone_open($timezone));
|
|
$value = date_format($date, 'c');
|
|
$expected = '1969-12-31T16:00:00-08:00';
|
|
$this->assertEqual($expected, $value, "Test date_timezone_set(timezone_open($timezone)): should be $expected, found $value.");
|
|
|
|
$expected = 'America/Los_Angeles';
|
|
$value = timezone_name_get(date_timezone_get($date));
|
|
$this->assertEqual($expected, $value, "The current timezone should be $expected, found $value.");
|
|
$expected = '-28800';
|
|
$value = date_offset_get($date);
|
|
$this->assertEqual($expected, $value, "The current offset should be $expected, found $value.");
|
|
|
|
// Convert the local version of a datetime string to UTC.
|
|
$input = '1969-12-31 16:00:00';
|
|
$timezone = 'America/Los_Angeles';
|
|
$date = date_make_date($input, $timezone);
|
|
$offset = date_offset_get($date);
|
|
$value = date_format($date, 'c');
|
|
$expected = '1969-12-31T16:00:00-08:00';
|
|
$this->assertEqual($expected, $value, "Test date_make_date('$input', '$timezone'): should be $expected, found $value.");
|
|
|
|
$expected = 'America/Los_Angeles';
|
|
$value = timezone_name_get(date_timezone_get($date));
|
|
$this->assertEqual($expected, $value, "The current timezone should be $expected, found $value.");
|
|
$expected = '-28800';
|
|
$value = date_offset_get($date);
|
|
$this->assertEqual($expected, $value, "The current offset should be $expected, found $value.");
|
|
|
|
$timezone = 'UTC';
|
|
date_timezone_set($date, timezone_open($timezone));
|
|
$value = date_format($date, 'c');
|
|
$expected = '1970-01-01T00:00:00+00:00';
|
|
$this->assertEqual($expected, $value, "Test date_timezone_set(\$date, timezone_open($timezone)): should be $expected, found $value.");
|
|
|
|
$expected = 'UTC';
|
|
$value = timezone_name_get(date_timezone_get($date));
|
|
$this->assertEqual($expected, $value, "The current timezone should be $expected, found $value.");
|
|
$expected = '0';
|
|
$value = date_offset_get($date);
|
|
$this->assertEqual($expected, $value, "The current offset should be $expected, found $value.");
|
|
|
|
// Create year-only date.
|
|
$input = '2009-00-00T00:00:00';
|
|
$timezone = NULL;
|
|
$granularity = array('year');
|
|
$date = date_make_date($input, $timezone, DATE_DATETIME, $granularity);
|
|
$value = date_format($date, 'Y');
|
|
$expected = '2009';
|
|
$this->assertEqual($expected, $value, "Test date_make_date($input, $timezone, DATE_DATETIME, array('year')): should be $expected, found $value.");
|
|
|
|
// Create month and year-only date.
|
|
$input = '2009-10-00T00:00:00';
|
|
$timezone = NULL;
|
|
$granularity = array('year', 'month');
|
|
$date = date_make_date($input, $timezone, DATE_DATETIME, $granularity);
|
|
$value = date_format($date, 'Y-m');
|
|
$expected = '2009-10';
|
|
$this->assertEqual($expected, $value, "Test date_make_date($input, $timezone, DATE_DATETIME, array('year', 'month')): should be $expected, found $value.");
|
|
|
|
// Create time-only date.
|
|
$input = '0000-00-00T10:30:00';
|
|
$timezone = NULL;
|
|
$granularity = array('hour', 'minute', 'second');
|
|
$date = date_make_date($input, $timezone, DATE_DATETIME, $granularity);
|
|
$value = date_format($date, 'H:i:s');
|
|
$expected = '10:30:00';
|
|
$this->assertEqual($expected, $value, "Test date_make_date($input, $timezone, DATE_DATETIME, array('hour', 'minute', 'second')): should be $expected, found $value.");
|
|
|
|
// Test date ranges.
|
|
$valid = array(
|
|
'-20:+20',
|
|
'-1:+0',
|
|
'-10:-5',
|
|
'2000:2020',
|
|
'-10:2010',
|
|
'1980:-10',
|
|
'1920:+20',
|
|
);
|
|
$invalid = array(
|
|
'abc',
|
|
'abc:+20',
|
|
'1920:+20a',
|
|
'+-20:+-30',
|
|
'12:12',
|
|
'0:+20',
|
|
'-20:0',
|
|
);
|
|
foreach ($valid as $range) {
|
|
$this->assertTrue(date_range_valid($range), "$range recognized as a valid date range.");
|
|
}
|
|
foreach ($invalid as $range) {
|
|
$this->assertFalse(date_range_valid($range), "$range recognized as an invalid date range.");
|
|
}
|
|
|
|
}
|
|
|
|
/**
|
|
* Implementation of tearDown().
|
|
*/
|
|
function tearDown() {
|
|
variable_del('date_first_day');
|
|
variable_del('date_api_use_iso8601');
|
|
parent::tearDown();
|
|
}
|
|
}
|