You are here

protected function TimezoneTest::setUp in Drupal 8

Same name in this branch
  1. 8 core/modules/simpletest/src/Tests/TimeZoneTest.php \Drupal\simpletest\Tests\TimeZoneTest::setUp()
  2. 8 core/tests/Drupal/KernelTests/Core/Datetime/Element/TimezoneTest.php \Drupal\KernelTests\Core\Datetime\Element\TimezoneTest::setUp()
Same name and namespace in other branches
  1. 9 core/tests/Drupal/KernelTests/Core/Datetime/Element/TimezoneTest.php \Drupal\KernelTests\Core\Datetime\Element\TimezoneTest::setUp()
  2. 10 core/tests/Drupal/KernelTests/Core/Datetime/Element/TimezoneTest.php \Drupal\KernelTests\Core\Datetime\Element\TimezoneTest::setUp()

Overrides EntityKernelTestBase::setUp

File

core/tests/Drupal/KernelTests/Core/Datetime/Element/TimezoneTest.php, line 161

Class

TimezoneTest
Tests the timezone handling of datetime and datelist element types.

Namespace

Drupal\KernelTests\Core\Datetime\Element

Code

protected function setUp() {
  parent::setUp();
  $this
    ->installConfig([
    'system',
  ]);

  // Setup the background time zones.
  $this->timezones['php initial'] = date_default_timezone_get();
  $user = $this
    ->createUser();
  $user
    ->set('timezone', $this->timezones['user'])
    ->save();

  // This also sets PHP's assumed time.
  \Drupal::currentUser()
    ->setAccount($user);

  // Set a reference date to use in tests.
  $this->date = new DrupalDatetime('2000-01-01 12:00', NULL);

  // Create arrays listing the dates and times of $this->date formatted
  // according to the various timezones of $this->timezones.
  $this->dateFormat = DateFormat::load('html_date')
    ->getPattern();
  $this->timeFormat = DateFormat::load('html_time')
    ->getPattern();
  $date = clone $this->date;
  foreach ($this->timezones as $label => $timezone) {
    $date
      ->setTimezone(new \DateTimeZone($timezone));
    $this->formattedDates['date'][$label] = $date
      ->format($this->dateFormat);
    $this->formattedDates['time'][$label] = $date
      ->format($this->timeFormat);
    $this->formattedDates['day'][$label] = $date
      ->format('j');
    $this->formattedDates['month'][$label] = $date
      ->format('n');
    $this->formattedDates['year'][$label] = $date
      ->format('Y');
    $this->formattedDates['hour'][$label] = $date
      ->format('G');
    $this->formattedDates['minute'][$label] = $date
      ->format('i');
    $this->formattedDates['second'][$label] = $date
      ->format('s');
  }

  // Validate the timezone setup.
  $this
    ->assertEquals($this->timezones['user'], date_default_timezone_get(), 'Subsequent tests assume specific value for date_default_timezone_get().');
  $this
    ->assertEquals(date_default_timezone_get(), $this->date
    ->getTimezone()
    ->getName(), 'Subsequent tests assume DrupalDateTime objects default to Drupal user time zone if none specified');
}