You are here

public function DateRangeItemTest::testDateOnly in Drupal 10

Same name and namespace in other branches
  1. 8 core/modules/datetime_range/tests/src/Kernel/DateRangeItemTest.php \Drupal\Tests\datetime_range\Kernel\DateRangeItemTest::testDateOnly()
  2. 9 core/modules/datetime_range/tests/src/Kernel/DateRangeItemTest.php \Drupal\Tests\datetime_range\Kernel\DateRangeItemTest::testDateOnly()

Tests the field configured for date-only.

File

core/modules/datetime_range/tests/src/Kernel/DateRangeItemTest.php, line 83

Class

DateRangeItemTest
Test datetime range field type via API.

Namespace

Drupal\Tests\datetime_range\Kernel

Code

public function testDateOnly() {
  $this->fieldStorage
    ->setSetting('datetime_type', DateRangeItem::DATETIME_TYPE_DATE);
  $field_name = $this->fieldStorage
    ->getName();

  // Create an entity.
  $entity = EntityTest::create([
    'name' => $this
      ->randomString(),
    $field_name => [
      'value' => '2016-09-21',
      'end_value' => '2016-09-21',
    ],
  ]);

  // 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_date = $entity->{$field_name}->start_date;
  sleep(1);
  $end_date = $entity->{$field_name}->end_date;
  $this
    ->assertEquals($start_date
    ->getTimestamp(), $end_date
    ->getTimestamp());
  $this
    ->assertEquals('12:00:00', $start_date
    ->format('H:i:s'));
  $this
    ->assertEquals('12:00:00', $end_date
    ->format('H:i:s'));
}