function TimezoneResolverTest::testGetTimeZone

Same name and namespace in other branches
  1. 9 core/modules/system/tests/src/Kernel/TimezoneResolverTest.php \Drupal\Tests\system\Kernel\TimezoneResolverTest::testGetTimeZone()
  2. 10 core/modules/system/tests/src/Kernel/TimezoneResolverTest.php \Drupal\Tests\system\Kernel\TimezoneResolverTest::testGetTimeZone()
  3. 11.x core/modules/system/tests/src/Kernel/TimezoneResolverTest.php \Drupal\Tests\system\Kernel\TimezoneResolverTest::testGetTimeZone()

Tests time zone resolution.

File

core/modules/system/tests/src/Kernel/TimezoneResolverTest.php, line 31

Class

TimezoneResolverTest
@coversDefaultClass <a href="/api/drupal/core%21modules%21system%21src%21TimeZoneResolver.php/class/TimeZoneResolver/8.9.x" title="Event handler that resolves time zone based on site and user configuration." class="local">\Drupal\system\TimeZoneResolver</a> @group system

Namespace

Drupal\Tests\system\Kernel

Code

public function testGetTimeZone() {
    $this->installEntitySchema('user');
    $this->installSchema('system', [
        'sequences',
    ]);
    $this->installConfig([
        'system',
    ]);
    // Check the default test timezone.
    $this->assertEquals('Australia/Sydney', date_default_timezone_get());
    // Test the configured system timezone.
    $configFactory = $this->container
        ->get('config.factory');
    $timeZoneConfig = $configFactory->getEditable('system.date');
    $timeZoneConfig->set('timezone.default', 'Australia/Adelaide');
    $timeZoneConfig->save();
    $eventDispatcher = $this->container
        ->get('event_dispatcher');
    $kernel = $this->container
        ->get('kernel');
    $eventDispatcher->dispatch(KernelEvents::REQUEST, new GetResponseEvent($kernel, Request::create('http://www.example.com'), HttpKernelInterface::MASTER_REQUEST));
    $this->assertEquals('Australia/Adelaide', date_default_timezone_get());
    $user = $this->createUser([]);
    $user->set('timezone', 'Australia/Lord_Howe');
    $user->save();
    $this->setCurrentUser($user);
    $this->assertEquals('Australia/Lord_Howe', date_default_timezone_get());
}

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