function DrupalDateTimeTest::testDateTimezone

Same name in other branches
  1. 8.9.x core/modules/system/tests/src/Functional/Datetime/DrupalDateTimeTest.php \Drupal\Tests\system\Functional\Datetime\DrupalDateTimeTest::testDateTimezone()
  2. 10 core/modules/system/tests/src/Functional/Datetime/DrupalDateTimeTest.php \Drupal\Tests\system\Functional\Datetime\DrupalDateTimeTest::testDateTimezone()
  3. 11.x core/modules/system/tests/src/Functional/Datetime/DrupalDateTimeTest.php \Drupal\Tests\system\Functional\Datetime\DrupalDateTimeTest::testDateTimezone()

Tests that DrupalDateTime can detect the right timezone to use.

Test with a variety of less commonly used timezone names to help ensure that the system timezone will be different than the stated timezones.

File

core/modules/system/tests/src/Functional/Datetime/DrupalDateTimeTest.php, line 41

Class

DrupalDateTimeTest
Tests DrupalDateTime functionality.

Namespace

Drupal\Tests\system\Functional\Datetime

Code

public function testDateTimezone() {
    $date_string = '2007-01-31 21:00:00';
    // Make sure no site timezone has been set.
    $this->config('system.date')
        ->set('timezone.user.configurable', 0)
        ->set('timezone.default', NULL)
        ->save();
    // Detect the system timezone.
    $system_timezone = date_default_timezone_get();
    // Create a date object with an unspecified timezone, which should
    // end up using the system timezone.
    $date = new DrupalDateTime($date_string);
    $timezone = $date->getTimezone()
        ->getName();
    $this->assertSame($system_timezone, $timezone, 'DrupalDateTime uses the system timezone when there is no site timezone.');
    // Create a date object with a specified timezone.
    $date = new DrupalDateTime($date_string, 'America/Yellowknife');
    $timezone = $date->getTimezone()
        ->getName();
    $this->assertSame('America/Yellowknife', $timezone, 'DrupalDateTime uses the specified timezone if provided.');
    // Set a site timezone.
    $this->config('system.date')
        ->set('timezone.default', 'Europe/Warsaw')
        ->save();
    // Create a date object with an unspecified timezone, which should
    // end up using the site timezone.
    $date = new DrupalDateTime($date_string);
    $timezone = $date->getTimezone()
        ->getName();
    $this->assertSame('Europe/Warsaw', $timezone, 'DrupalDateTime uses the site timezone if provided.');
    // Create user.
    $this->config('system.date')
        ->set('timezone.user.configurable', 1)
        ->save();
    $test_user = $this->drupalCreateUser([]);
    $this->drupalLogin($test_user);
    // Set up the user with a different timezone than the site.
    $edit = [
        'mail' => $test_user->getEmail(),
        'timezone' => 'Asia/Manila',
    ];
    $this->drupalGet('user/' . $test_user->id() . '/edit');
    $this->submitForm($edit, 'Save');
    // Reload the user and reset the timezone in AccountProxy::setAccount().
    \Drupal::entityTypeManager()->getStorage('user')
        ->resetCache();
    $this->container
        ->get('current_user')
        ->setAccount(User::load($test_user->id()));
    // Create a date object with an unspecified timezone, which should
    // end up using the user timezone.
    $date = new DrupalDateTime($date_string);
    $timezone = $date->getTimezone()
        ->getName();
    $this->assertSame('Asia/Manila', $timezone, 'DrupalDateTime uses the user timezone, if configurable timezones are used and it is set.');
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.