| [ Index ] |
PHP Cross Reference of Drupal 6 (yi-drupal) |
[Summary view] [Print] [Text view]
1 <?php 2 /** 3 * Test Date API functions 4 */ 5 class DateAPITestCase extends DrupalWebTestCase { 6 function getInfo() { 7 return array( 8 'name' => t('Date API'), 9 'description' => t('Test Date API functions.') , 10 'group' => t('Date'), 11 ); 12 } 13 14 /** 15 * Implementation of setUp(). 16 */ 17 public function setUp() { 18 // Load the date_api module. 19 parent::setUp('date_api'); 20 variable_set('date_api_use_iso8601', FALSE); 21 variable_set('date_first_day', 1); 22 } 23 24 function testDateAPI() { 25 26 $value = '2007-12-05 23:59'; 27 $this->assertEqual(TRUE, date_part_extract($value, 'year'), "Test date_part_extract(". $value .", year), results ". date_part_extract($value, 'year')); 28 $this->assertEqual(TRUE, date_part_extract($value, 'month'), "Test date_part_extract(". $value .", mon), results ". date_part_extract($value, 'month')); 29 $this->assertEqual(TRUE, date_part_extract($value, 'day'), "Test date_part_extract(". $value .", mday), results ". date_part_extract($value, 'day')); 30 31 $this->assertEqual(TRUE, date_is_valid($value), "Test date_is_valid(". $value .")"); 32 $value = '2007-00-00 00:00'; 33 $this->assertNotEqual(TRUE, date_is_valid($value), "Test for invalid date_is_valid(". $value .")"); 34 $value = '0000-00-00 00:00'; 35 $this->assertNotEqual(TRUE, date_is_valid($value), "Test for invalid date_is_valid(". $value .")"); 36 $value = '-100'; 37 $this->assertNotEqual(TRUE, date_is_valid($value), "Test for invalid date_is_valid(". $value .")"); 38 $value = '2007-00-01T00:00'; 39 $this->assertEqual(TRUE, date_is_valid($value, DATE_ISO), "Test ISO exception to date_is_valid(". $value .", DATE_ISO)"); 40 41 $dates = array( 42 '2007-01-01 00:00:00', 43 '1970-01-01 00:00:00', 44 '1900-01-01 00:00:00', 45 '1600-01-01 00:00:00', 46 '0100-01-01 00:00:00'); 47 foreach ($dates as $date) { 48 $unix = date_convert($date, DATE_DATETIME, DATE_UNIX); 49 $datetime = date_convert($unix, DATE_UNIX, DATE_DATETIME); 50 $this->assertEqual($date, $datetime, 'Test roundtrip using date_convert() from DATE_DATETIME to DATE_UNIX back to DATE_DATETIME, results '.$date.' >> '.$unix.' >> '.$datetime); 51 } 52 53 // Test date_format_date(). 54 $formatters = array( 55 'a', 56 'A', 57 'B', 58 'c', 59 'd', 60 'D', 61 'e', 62 'F', 63 'g', 64 'G', 65 'h', 66 'H', 67 'i', 68 'I', 69 'j', 70 'l', 71 'L', 72 'm', 73 'M', 74 'n', 75 'N', 76 'o', 77 'O', 78 'P', 79 'r', 80 'R', 81 's', 82 'S', 83 't', 84 'T', 85 'u', 86 'U', 87 'w', 88 'W', 89 'y', 90 'Y', 91 'z', 92 'Z', 93 ); 94 foreach ($formatters as $formatter) { 95 $date_api_format = date_format_date(date_now(), 'custom', $formatter); 96 $php_format = date_format(date_now(), $formatter); 97 $this->assertEqual($date_api_format, $php_format, 'Test that the "' . $formatter . '" formatter is formatted correctly by date_format_date()'); 98 } 99 100 // Test the order of the weeks days for a calendar that starts on Monday and one that starts on Sunday. 101 variable_set('date_first_day', 1); 102 $expected = array ( 0 => t('Mon'), 1 => t('Tue'), 2 => t('Wed'), 3 => t('Thu'), 4 => t('Fri'), 5 => t('Sat'), 6 => t('Sun'), ); 103 $days = date_week_days_ordered(date_week_days_abbr(1)); 104 $this->assertEqual($expected, $days, 'Test that date_week_days_ordered() array starts on Monday when the site first day is on Monday.'); 105 variable_set('date_first_day', 0); 106 $expected = array ( 0 => t('Sun'), 1 => t('Mon'), 2 => t('Tue'), 3 => t('Wed'), 4 => t('Thu'), 5 => t('Fri'), 6 => t('Sat'), ); 107 $days = date_week_days_ordered(date_week_days_abbr(1)); 108 $this->assertEqual($expected, $days, 'Test that date_week_days_ordered() array starts on Sunday when the site first day is on Sunday.'); 109 110 // Test days in February for a leap year and a non-leap year. 111 $expected = 28; 112 $value = date_days_in_month(2005, 2); 113 $this->assertEqual($expected, $value, "Test date_days_in_month(2, 2005): should be $expected, found $value."); 114 $expected = 29; 115 $value = date_days_in_month(2004, 2); 116 $this->assertEqual($expected, $value, "Test date_days_in_month(2, 2004): should be $expected, found $value."); 117 118 // Test days in year for a leap year and a non-leap year. 119 $expected = 365; 120 $value = date_days_in_year('2005-06-01 00:00:00', DATE_DATETIME); 121 $this->assertEqual($expected, $value, "Test date_days_in_year(2005-06-01, DATE_DATETIME): should be $expected, found $value."); 122 $expected = 366; 123 $value = date_days_in_year('2004-06-01 00:00:00', DATE_DATETIME); 124 $this->assertEqual($expected, $value, "Test date_days_in_year(2004-06-01, DATE_DATETIME): should be $expected, found $value."); 125 126 // Test ISO weeks for a leap year and a non-leap year. 127 $expected = 52; 128 $value = date_iso_weeks_in_year('2008-06-01 00:00:00', DATE_DATETIME); 129 $this->assertEqual($expected, $value, "Test date_iso_weeks_in_year(2008-06-01, DATE_DATETIME): should be $expected, found $value."); 130 $expected = 53; 131 $value = date_iso_weeks_in_year('2009-06-01 00:00:00', DATE_DATETIME); 132 $this->assertEqual($expected, $value, "Test date_iso_weeks_in_year(2009-06-01, DATE_DATETIME): should be $expected, found $value."); 133 134 // Test day of week for March 1, the day after leap day. 135 $expected = 6; 136 $value = date_day_of_week('2008-03-01 00:00:00', DATE_DATETIME); 137 $this->assertEqual($expected, $value, "Test date_day_of_week(2008-03-01, DATE_DATETIME): should be $expected, found $value."); 138 $expected = 0; 139 $value = date_day_of_week('2009-03-01 00:00:00', DATE_DATETIME); 140 $this->assertEqual($expected, $value, "Test date_day_of_week(2009-03-01, DATE_DATETIME): should be $expected, found $value."); 141 142 // Test day of week name for March 1, the day after leap day. 143 $expected = 'Sat'; 144 $value = date_day_of_week_name('2008-03-01 00:00:00', DATE_DATETIME); 145 $this->assertEqual($expected, $value, "Test date_day_of_week_name(2008-03-01, DATE_DATETIME): should be $expected, found $value."); 146 $expected = 'Sun'; 147 $value = date_day_of_week_name('2009-03-01 00:00:00', DATE_DATETIME); 148 $this->assertEqual($expected, $value, "Test date_day_of_week_name(2009-03-01, DATE_DATETIME): should be $expected, found $value."); 149 150 // Test week range with calendar weeks. 151 variable_set('date_first_day', 0); 152 variable_set('date_api_use_iso8601', FALSE); 153 $expected = '2008-01-27 to 2008-02-03'; 154 $result = date_week_range(5, 2008); 155 $value = $result[0]->format(DATE_FORMAT_DATE) .' to '. $result[1]->format(DATE_FORMAT_DATE); 156 $this->assertEqual($expected, $value, "Test calendar date_week_range(5, 2008): should be $expected, found $value."); 157 $expected = '2009-01-25 to 2009-02-01'; 158 $result = date_week_range(5, 2009); 159 $value = $result[0]->format(DATE_FORMAT_DATE) .' to '. $result[1]->format(DATE_FORMAT_DATE); 160 $this->assertEqual($expected, $value, "Test calendar date_week_range(5, 2009): should be $expected, found $value."); 161 162 // And now with ISO weeks. 163 variable_set('date_first_day', 1); 164 variable_set('date_api_use_iso8601', TRUE); 165 $expected = '2008-01-28 to 2008-02-04'; 166 $result = date_week_range(5, 2008); 167 $value = $result[0]->format(DATE_FORMAT_DATE) .' to '. $result[1]->format(DATE_FORMAT_DATE); 168 $this->assertEqual($expected, $value, "Test ISO date_week_range(5, 2008): should be $expected, found $value."); 169 $expected = '2009-01-26 to 2009-02-02'; 170 $result = date_week_range(5, 2009); 171 $value = $result[0]->format(DATE_FORMAT_DATE) .' to '. $result[1]->format(DATE_FORMAT_DATE); 172 $this->assertEqual($expected, $value, "Test ISO date_week_range(5, 2009): should be $expected, found $value."); 173 variable_set('date_api_use_iso8601', FALSE); 174 175 // Find calendar week for a date. 176 variable_set('date_first_day', 0); 177 $expected = '09'; 178 $value = date_week('2008-03-01'); 179 $this->assertEqual($expected, $value, "Test date_week(2008-03-01): should be $expected, found $value."); 180 $expected = '10'; 181 $value = date_week('2009-03-01'); 182 $this->assertEqual($expected, $value, "Test date_week(2009-03-01): should be $expected, found $value."); 183 184 // Create date object from datetime string. 185 $input = '2009-03-07 10:30'; 186 $timezone = 'America/Chicago'; 187 $date = date_make_date($input, $timezone); 188 $value = date_format($date, 'c'); 189 $expected = '2009-03-07T10:30:00-06:00'; 190 $this->assertEqual($expected, $value, "Test date_make_date($input, $timezone): should be $expected, found $value."); 191 192 // Same during daylight savings time. 193 $input = '2009-06-07 10:30'; 194 $timezone = 'America/Chicago'; 195 $date = date_make_date($input, $timezone); 196 $value = date_format($date, 'c'); 197 $expected = '2009-06-07T10:30:00-05:00'; 198 $this->assertEqual($expected, $value, "Test date_make_date($input, $timezone): should be $expected, found $value."); 199 200 // Create date object from date string. 201 $input = '2009-03-07'; 202 $timezone = 'America/Chicago'; 203 $date = date_make_date($input, $timezone); 204 $value = date_format($date, 'c'); 205 $expected = '2009-03-07T00:00:00-06:00'; 206 $this->assertEqual($expected, $value, "Test date_make_date($input, $timezone): should be $expected, found $value."); 207 208 // Same during daylight savings time. 209 $input = '2009-06-07'; 210 $timezone = 'America/Chicago'; 211 $date = date_make_date($input, $timezone); 212 $value = date_format($date, 'c'); 213 $expected = '2009-06-07T00:00:00-05:00'; 214 $this->assertEqual($expected, $value, "Test date_make_date($input, $timezone): should be $expected, found $value."); 215 216 // Create date object from date array, date only. 217 $input = array('year' => 2010, 'month' => 2, 'day' => 28); 218 $timezone = 'America/Chicago'; 219 $granularity = array('year', 'month', 'day'); 220 $date = date_make_date($input, $timezone, DATE_ARRAY, $granularity); 221 $value = date_format($date, 'c'); 222 $expected = '2010-02-28T00:00:00-06:00'; 223 $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."); 224 225 // Create date object from date array with hour. 226 $input = array('year' => 2010, 'month' => 2, 'day' => 28, 'hour' => 10); 227 $timezone = 'America/Chicago'; 228 $granularity = array('year', 'month', 'day', 'hour'); 229 $date = date_make_date($input, $timezone, DATE_ARRAY, $granularity); 230 $value = date_format($date, 'c'); 231 $expected = '2010-02-28T10:00:00-06:00'; 232 $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."); 233 234 // 0 = January 1, 1970 00:00:00 (UTC); 235 // 1000000000 = September 9, 2001 01:46:40 (UTC); 236 237 // Create date object from unix timestamp and convert it to a local date. 238 $input = 0; 239 $timezone = 'UTC'; 240 $date = date_make_date($input, $timezone, DATE_UNIX); 241 $value = date_format($date, 'c'); 242 $expected = '1970-01-01T00:00:00+00:00'; 243 $this->assertEqual($expected, $value, "Test date_make_date($input, $timezone, DATE_UNIX): should be $expected, found $value."); 244 245 $expected = 'UTC'; 246 $value = timezone_name_get(date_timezone_get($date)); 247 $this->assertEqual($expected, $value, "The current timezone is $value: should be $expected."); 248 $expected = 0; 249 $value = date_offset_get($date); 250 $this->assertEqual($expected, $value, "The current offset is $value: should be $expected."); 251 252 $timezone = 'America/Los_Angeles'; 253 date_timezone_set($date, timezone_open($timezone)); 254 $value = date_format($date, 'c'); 255 $expected = '1969-12-31T16:00:00-08:00'; 256 $this->assertEqual($expected, $value, "Test date_timezone_set(\$date, timezone_open($timezone)): should be $expected, found $value."); 257 258 $expected = 'America/Los_Angeles'; 259 $value = timezone_name_get(date_timezone_get($date)); 260 $this->assertEqual($expected, $value, "The current timezone should be $expected, found $value."); 261 $expected = '-28800'; 262 $value = date_offset_get($date); 263 $this->assertEqual($expected, $value, "The current offset should be $expected, found $value."); 264 265 // Convert the local version of a timestamp to UTC. 266 $input = 0; 267 $timezone = 'America/Los_Angeles'; 268 $date = date_make_date($input, $timezone, DATE_UNIX); 269 $offset = date_offset_get($date); 270 $value = date_format($date, 'c'); 271 $expected = '1969-12-31T16:00:00-08:00'; 272 $this->assertEqual($expected, $value, "Test date_make_date($input, $timezone, DATE_UNIX): should be $expected, found $value."); 273 274 $expected = 'America/Los_Angeles'; 275 $value = timezone_name_get(date_timezone_get($date)); 276 $this->assertEqual($expected, $value, "The current timezone should be $expected, found $value."); 277 $expected = '-28800'; 278 $value = date_offset_get($date); 279 $this->assertEqual($expected, $value, "The current offset should be $expected, found $value."); 280 281 $timezone = 'UTC'; 282 date_timezone_set($date, timezone_open($timezone)); 283 $value = date_format($date, 'c'); 284 $expected = '1970-01-01T00:00:00+00:00'; 285 $this->assertEqual($expected, $value, "Test date_timezone_set(\$date, timezone_open($timezone)): should be $expected, found $value."); 286 287 $expected = 'UTC'; 288 $value = timezone_name_get(date_timezone_get($date)); 289 $this->assertEqual($expected, $value, "The current timezone should be $expected, found $value."); 290 $expected = '0'; 291 $value = date_offset_get($date); 292 $this->assertEqual($expected, $value, "The current offset should be $expected, found $value."); 293 294 // Create date object from datetime string and convert it to a local date. 295 $input = '1970-01-01 00:00:00'; 296 $timezone = 'UTC'; 297 $date = date_make_date($input, $timezone); 298 $value = date_format($date, 'c'); 299 $expected = '1970-01-01T00:00:00+00:00'; 300 $this->assertEqual($expected, $value, "Test date_make_date('$input', '$timezone'): should be $expected, found $value."); 301 302 $expected = 'UTC'; 303 $value = timezone_name_get(date_timezone_get($date)); 304 $this->assertEqual($expected, $value, "The current timezone is $value: should be $expected."); 305 $expected = 0; 306 $value = date_offset_get($date); 307 $this->assertEqual($expected, $value, "The current offset is $value: should be $expected."); 308 309 $timezone = 'America/Los_Angeles'; 310 date_timezone_set($date, timezone_open($timezone)); 311 $value = date_format($date, 'c'); 312 $expected = '1969-12-31T16:00:00-08:00'; 313 $this->assertEqual($expected, $value, "Test date_timezone_set(timezone_open($timezone)): should be $expected, found $value."); 314 315 $expected = 'America/Los_Angeles'; 316 $value = timezone_name_get(date_timezone_get($date)); 317 $this->assertEqual($expected, $value, "The current timezone should be $expected, found $value."); 318 $expected = '-28800'; 319 $value = date_offset_get($date); 320 $this->assertEqual($expected, $value, "The current offset should be $expected, found $value."); 321 322 // Convert the local version of a datetime string to UTC. 323 $input = '1969-12-31 16:00:00'; 324 $timezone = 'America/Los_Angeles'; 325 $date = date_make_date($input, $timezone); 326 $offset = date_offset_get($date); 327 $value = date_format($date, 'c'); 328 $expected = '1969-12-31T16:00:00-08:00'; 329 $this->assertEqual($expected, $value, "Test date_make_date('$input', '$timezone'): should be $expected, found $value."); 330 331 $expected = 'America/Los_Angeles'; 332 $value = timezone_name_get(date_timezone_get($date)); 333 $this->assertEqual($expected, $value, "The current timezone should be $expected, found $value."); 334 $expected = '-28800'; 335 $value = date_offset_get($date); 336 $this->assertEqual($expected, $value, "The current offset should be $expected, found $value."); 337 338 $timezone = 'UTC'; 339 date_timezone_set($date, timezone_open($timezone)); 340 $value = date_format($date, 'c'); 341 $expected = '1970-01-01T00:00:00+00:00'; 342 $this->assertEqual($expected, $value, "Test date_timezone_set(\$date, timezone_open($timezone)): should be $expected, found $value."); 343 344 $expected = 'UTC'; 345 $value = timezone_name_get(date_timezone_get($date)); 346 $this->assertEqual($expected, $value, "The current timezone should be $expected, found $value."); 347 $expected = '0'; 348 $value = date_offset_get($date); 349 $this->assertEqual($expected, $value, "The current offset should be $expected, found $value."); 350 351 // Create year-only date. 352 $input = '2009-00-00T00:00:00'; 353 $timezone = NULL; 354 $granularity = array('year'); 355 $date = date_make_date($input, $timezone, DATE_DATETIME, $granularity); 356 $value = date_format($date, 'Y'); 357 $expected = '2009'; 358 $this->assertEqual($expected, $value, "Test date_make_date($input, $timezone, DATE_DATETIME, array('year')): should be $expected, found $value."); 359 360 // Create month and year-only date. 361 $input = '2009-10-00T00:00:00'; 362 $timezone = NULL; 363 $granularity = array('year', 'month'); 364 $date = date_make_date($input, $timezone, DATE_DATETIME, $granularity); 365 $value = date_format($date, 'Y-m'); 366 $expected = '2009-10'; 367 $this->assertEqual($expected, $value, "Test date_make_date($input, $timezone, DATE_DATETIME, array('year', 'month')): should be $expected, found $value."); 368 369 // Create time-only date. 370 $input = '0000-00-00T10:30:00'; 371 $timezone = NULL; 372 $granularity = array('hour', 'minute', 'second'); 373 $date = date_make_date($input, $timezone, DATE_DATETIME, $granularity); 374 $value = date_format($date, 'H:i:s'); 375 $expected = '10:30:00'; 376 $this->assertEqual($expected, $value, "Test date_make_date($input, $timezone, DATE_DATETIME, array('hour', 'minute', 'second')): should be $expected, found $value."); 377 378 // Test date ranges. 379 $valid = array( 380 '-20:+20', 381 '-1:+0', 382 '-10:-5', 383 '2000:2020', 384 '-10:2010', 385 '1980:-10', 386 '1920:+20', 387 ); 388 $invalid = array( 389 'abc', 390 'abc:+20', 391 '1920:+20a', 392 '+-20:+-30', 393 '12:12', 394 '0:+20', 395 '-20:0', 396 ); 397 foreach ($valid as $range) { 398 $this->assertTrue(date_range_valid($range), "$range recognized as a valid date range."); 399 } 400 foreach ($invalid as $range) { 401 $this->assertFalse(date_range_valid($range), "$range recognized as an invalid date range."); 402 } 403 404 } 405 406 /** 407 * Implementation of tearDown(). 408 */ 409 function tearDown() { 410 variable_del('date_first_day'); 411 variable_del('date_api_use_iso8601'); 412 parent::tearDown(); 413 } 414 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated: Mon Jul 9 18:01:44 2012 | Cross-referenced by PHPXref 0.7 |