1 common.test CommonFormatDateTestCase::testFormatDate()

Tests for the format_date() function.

File

core/modules/simpletest/tests/common.test, line 2845
Tests for common.inc functionality.

Class

CommonFormatDateTestCase
Tests the format_date() function.

Code

function testFormatDate() {
  global $user, $language;

  $timestamp = strtotime('2007-03-26T00:00:00+00:00');
  $this->assertIdentical(format_date($timestamp, 'custom', 'l, d-M-y H:i:s T', 'America/Los_Angeles', 'en'), 'Sunday, 25-Mar-07 17:00:00 PDT', 'Test all parameters.');
  $this->assertIdentical(format_date($timestamp, 'custom', 'l, d-M-y H:i:s T', 'America/Los_Angeles', self::LANGCODE), 'domingo, 25-Mar-07 17:00:00 PDT', 'Test translated format.');
  $this->assertIdentical(format_date($timestamp, 'custom', '\\l, d-M-y H:i:s T', 'America/Los_Angeles', self::LANGCODE), 'l, 25-Mar-07 17:00:00 PDT', 'Test an escaped format string.');
  $this->assertIdentical(format_date($timestamp, 'custom', '\\\\l, d-M-y H:i:s T', 'America/Los_Angeles', self::LANGCODE), '\\domingo, 25-Mar-07 17:00:00 PDT', 'Test format containing backslash character.');
  $this->assertIdentical(format_date($timestamp, 'custom', '\\\\\\l, d-M-y H:i:s T', 'America/Los_Angeles', self::LANGCODE), '\\l, 25-Mar-07 17:00:00 PDT', 'Test format containing backslash followed by escaped format string.');
  $this->assertIdentical(format_date($timestamp, 'custom', 'l, d-M-y H:i:s T', 'Europe/London', 'en'), 'Monday, 26-Mar-07 01:00:00 BST', 'Test a different time zone.');

  // Create an admin user and add Spanish language.
  $admin_user = $this->backdropCreateUser(array('administer languages'));
  $this->backdropLogin($admin_user);
  $edit = array(
    'predefined_langcode' => 'custom',
    'langcode' => self::LANGCODE,
    'name' => self::LANGCODE,
    'native' => self::LANGCODE,
    'direction' => LANGUAGE_LTR,
  );
  $this->backdropPost('admin/config/regional/language/add', $edit, t('Add custom language'));

  // Set language prefix.
  $edit = array('prefix[' . self::LANGCODE . ']' => self::LANGCODE);
  $this->backdropPost('admin/config/regional/language/detection/url', $edit, t('Save configuration'));

  // Create a test user to carry out the tests.
  $test_user = $this->backdropCreateUser();
  $this->backdropLogin($test_user);
  $edit = array('language' => self::LANGCODE, 'mail' => $test_user->mail, 'timezone' => 'America/Los_Angeles');
  $this->backdropPost('user/' . $test_user->uid . '/edit', $edit, t('Save'));

  // Disable session saving as we are about to modify the global $user.
  backdrop_save_session(FALSE);
  // Save the original user and language and then replace it with the test user and language.
  $real_user = $user;
  $user = user_load($test_user->uid, TRUE);
  $real_language = $language->langcode;
  $language->langcode = $user->language;
  // Simulate a Backdrop bootstrap with the logged-in user.
  date_default_timezone_set(backdrop_get_user_timezone());

  $this->assertIdentical(format_date($timestamp, 'custom', 'l, d-M-y H:i:s T', 'America/Los_Angeles', 'en'), 'Sunday, 25-Mar-07 17:00:00 PDT', 'Test a different language.');
  $this->assertIdentical(format_date($timestamp, 'custom', 'l, d-M-y H:i:s T', 'Europe/London', 'en'), 'Monday, 26-Mar-07 01:00:00 BST', 'Test a different time zone.');
  $this->assertIdentical(format_date($timestamp, 'custom', 'l, d-M-y H:i:s T'), 'domingo, 25-Mar-07 17:00:00 PDT', 'Test custom date format.');
  $this->assertIdentical(format_date($timestamp, 'long'), 'domingo, 25. marzo 2007 - 17:00', 'Test long date format.');
  $this->assertIdentical(format_date($timestamp, 'medium'), '25. marzo 2007 - 17:00', 'Test medium date format.');
  $this->assertIdentical(format_date($timestamp, 'short'), '2007 Mar 25 - 5:00pm', 'Test short date format.');
  $this->assertIdentical(format_date($timestamp), '25. marzo 2007 - 17:00', 'Test default date format.');
  // Test HTML time element formats.
  $this->assertIdentical(format_date($timestamp, 'html_datetime'), '2007-03-25T17:00:00-0700', 'Test html_datetime date format.');
  $this->assertIdentical(format_date($timestamp, 'html_date'), '2007-03-25', 'Test html_date date format.');
  $this->assertIdentical(format_date($timestamp, 'html_time'), '17:00:00', 'Test html_time date format.');
  $this->assertIdentical(format_date($timestamp, 'html_yearless_date'), '03-25', 'Test html_yearless_date date format.');
  $this->assertIdentical(format_date($timestamp, 'html_week'), '2007-W12', 'Test html_week date format.');
  $this->assertIdentical(format_date($timestamp, 'html_month'), '2007-03', 'Test html_month date format.');
  $this->assertIdentical(format_date($timestamp, 'html_year'), '2007', 'Test html_year date format.');

  // Restore the original user and language, and enable session saving.
  $user = $real_user;
  $language->langcode = $real_language;
  // Restore default time zone.
  date_default_timezone_set(backdrop_get_user_timezone());
  backdrop_save_session(TRUE);
}