function DateRangeItemTest::testDateOnly

Same name in other branches
  1. 9 core/modules/datetime_range/tests/src/Kernel/DateRangeItemTest.php \Drupal\Tests\datetime_range\Kernel\DateRangeItemTest::testDateOnly()
  2. 8.9.x core/modules/datetime_range/tests/src/Kernel/DateRangeItemTest.php \Drupal\Tests\datetime_range\Kernel\DateRangeItemTest::testDateOnly()
  3. 10 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 85

Class

DateRangeItemTest
Test datetime range field type via API.

Namespace

Drupal\Tests\datetime_range\Kernel

Code

public function testDateOnly() : void {
    $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'));
}

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