function DateTimeTest::testTimeZoneHandling

Test time zones and DST handling.

File

drupal/core/modules/system/lib/Drupal/system/Tests/System/DateTimeTest.php, line 44
Definition of Drupal\system\Tests\System\DateTimeTest.

Class

DateTimeTest
Tests generic date and time handling capabilities of Drupal.

Namespace

Drupal\system\Tests\System

Code

function testTimeZoneHandling() {

  // Setup date/time settings for Honolulu time.
  $config = config('system.timezone')
    ->set('default', 'Pacific/Honolulu')
    ->set('user.configurable', 0)
    ->save();
  config('system.date')
    ->set('formats.medium.pattern.php', 'Y-m-d H:i:s O')
    ->save();

  // Create some nodes with different authored-on dates.
  $date1 = '2007-01-31 21:00:00 -1000';
  $date2 = '2007-07-31 21:00:00 -1000';
  $node1 = $this
    ->drupalCreateNode(array(
    'created' => strtotime($date1),
    'type' => 'article',
  ));
  $node2 = $this
    ->drupalCreateNode(array(
    'created' => strtotime($date2),
    'type' => 'article',
  ));

  // Confirm date format and time zone.
  $this
    ->drupalGet("node/{$node1->nid}");
  $this
    ->assertText('2007-01-31 21:00:00 -1000', 'Date should be identical, with GMT offset of -10 hours.');
  $this
    ->drupalGet("node/{$node2->nid}");
  $this
    ->assertText('2007-07-31 21:00:00 -1000', 'Date should be identical, with GMT offset of -10 hours.');

  // Set time zone to Los Angeles time.
  $config
    ->set('default', 'America/Los_Angeles')
    ->save();

  // Confirm date format and time zone.
  $this
    ->drupalGet("node/{$node1->nid}");
  $this
    ->assertText('2007-01-31 23:00:00 -0800', 'Date should be two hours ahead, with GMT offset of -8 hours.');
  $this
    ->drupalGet("node/{$node2->nid}");
  $this
    ->assertText('2007-08-01 00:00:00 -0700', 'Date should be three hours ahead, with GMT offset of -7 hours.');
}