You are here

function FormatDateTest::testFormatDate in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/system/src/Tests/Common/FormatDateTest.php \Drupal\system\Tests\Common\FormatDateTest::testFormatDate()

Tests the format_date() function.

File

core/modules/system/src/Tests/Common/FormatDateTest.php, line 88
Contains \Drupal\system\Tests\Common\FormatDateTest.

Class

FormatDateTest
Tests the format_date() function.

Namespace

Drupal\system\Tests\Common

Code

function testFormatDate() {
  $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.');

  // Change the default language and timezone.
  $this
    ->config('system.site')
    ->set('default_langcode', static::LANGCODE)
    ->save();
  date_default_timezone_set('America/Los_Angeles');

  // Reset the language manager so new negotiations attempts will fall back on
  // on the new language.
  $this->container
    ->get('language_manager')
    ->reset();
  $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'), '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.');

  // HTML is not escaped by the date formatter, it must be escaped later.
  $formatter = \Drupal::service('date.formatter');
  $this
    ->assertIdentical($formatter
    ->format($timestamp, 'custom', '\\<\\s\\c\\r\\i\\p\\t\\>\\a\\l\\e\\r\\t\\(\'Y\'\\)\\;\\<\\/\\s\\c\\r\\i\\p\\t\\>'), "<script>alert('2007');</script>", 'Script tags not removed from dates.');
  $this
    ->assertIdentical($formatter
    ->format($timestamp, 'custom', '\\<\\e\\m\\>Y\\<\\/\\e\\m\\>'), '<em>2007</em>', 'Em tags are not removed from dates.');
}