You are here

public function DateRecurFieldTest::testOccurrencesTimezone in Recurring Dates Field 3.1.x

Same name and namespace in other branches
  1. 8.2 tests/src/Kernel/DateRecurFieldTest.php \Drupal\Tests\date_recur\Kernel\DateRecurFieldTest::testOccurrencesTimezone()
  2. 3.x tests/src/Kernel/DateRecurFieldTest.php \Drupal\Tests\date_recur\Kernel\DateRecurFieldTest::testOccurrencesTimezone()
  3. 3.0.x tests/src/Kernel/DateRecurFieldTest.php \Drupal\Tests\date_recur\Kernel\DateRecurFieldTest::testOccurrencesTimezone()

Tests storage timezone is returned.

Not the timezone used for current request, or default to UTC per storage.

File

tests/src/Kernel/DateRecurFieldTest.php, line 61

Class

DateRecurFieldTest
Tests date_recur fields.

Namespace

Drupal\Tests\date_recur\Kernel

Code

public function testOccurrencesTimezone() {

  // Set the timezone to something different than UTC or storage.
  date_default_timezone_set('Pacific/Wake');
  $tzChristmas = new \DateTimeZone('Indian/Christmas');
  $entity = EntityTest::create();
  $entity->foo = [
    'value' => '2014-06-15T23:00:00',
    'end_value' => '2014-06-16T07:00:00',
    'rrule' => 'FREQ=WEEKLY;BYDAY=MO,TU,WE,TH,FR',
    'infinite' => '1',
    'timezone' => $tzChristmas
      ->getName(),
  ];

  /** @var \Drupal\date_recur\Plugin\Field\FieldType\DateRecurItem $item */
  $item = $entity
    ->get('foo')[0];
  $occurrences = $item
    ->getHelper()
    ->getOccurrences(NULL, NULL, 1);

  // Christmas island is UTC+7, so start time will be 6am.
  $assertDateStart = new \DateTime('6am 2014-06-16', $tzChristmas);
  $assertDateEnd = new \DateTime('2pm 2014-06-16', $tzChristmas);
  $this
    ->assertTrue($assertDateStart == $occurrences[0]
    ->getStart());
  $this
    ->assertEquals($tzChristmas
    ->getName(), $occurrences[0]
    ->getStart()
    ->getTimezone()
    ->getName());
  $this
    ->assertTrue($assertDateEnd == $occurrences[0]
    ->getEnd());
  $this
    ->assertEquals($tzChristmas
    ->getName(), $occurrences[0]
    ->getEnd()
    ->getTimezone()
    ->getName());
}