You are here

public function DateTimeDayItemTest::testDateDayTimeOnly in Date time day 8

Tests the field configured for time-only.

File

tests/src/Kernel/DateTimeDayItemTest.php, line 89

Class

DateTimeDayItemTest
Test date_time_day field type via API.

Namespace

Drupal\Tests\date_time_day\Kernel

Code

public function testDateDayTimeOnly() {
  $this->fieldStorage
    ->setSetting('datetime_type', DateTimeItem::DATETIME_TYPE_DATE);
  $this->fieldStorage
    ->setSetting('time_type', DateTimeDayItem::DATEDAY_TIME_DEFAULT_TYPE_FORMAT);
  $field_name = $this->fieldStorage
    ->getName();

  // Create an entity.
  $entity = EntityTest::create([
    'name' => $this
      ->randomString(),
    $field_name => [
      'value' => '2018-02-06',
      'start_time_value' => '10:00',
      'end_time_value' => '10:00',
    ],
  ]);

  // Dates are saved without a time value. When they are converted back into
  // a \Drupal\datetime\DateTimeComputed object they should all have the same
  // time.
  $start_time = $entity->{$field_name}->start_time;
  sleep(1);
  $end_time = $entity->{$field_name}->end_time;
  $this
    ->assertEquals($start_time
    ->getTimestamp(), $end_time
    ->getTimestamp());
}