function DateTimeItemTest::testDateOnly
Same name in other branches
- 8.9.x core/modules/datetime/tests/src/Kernel/DateTimeItemTest.php \Drupal\Tests\datetime\Kernel\DateTimeItemTest::testDateOnly()
- 10 core/modules/datetime/tests/src/Kernel/DateTimeItemTest.php \Drupal\Tests\datetime\Kernel\DateTimeItemTest::testDateOnly()
- 11.x core/modules/datetime/tests/src/Kernel/DateTimeItemTest.php \Drupal\Tests\datetime\Kernel\DateTimeItemTest::testDateOnly()
Tests using entity fields of the date field type.
File
-
core/
modules/ datetime/ tests/ src/ Kernel/ DateTimeItemTest.php, line 114
Class
- DateTimeItemTest
- Tests the new entity API for the date field type.
Namespace
Drupal\Tests\datetime\KernelCode
public function testDateOnly() {
$this->fieldStorage
->setSetting('datetime_type', DateTimeItem::DATETIME_TYPE_DATE);
$this->fieldStorage
->save();
// Verify entity creation.
$entity = EntityTest::create();
$value = '2014-01-01';
$entity->field_datetime = $value;
$entity->name->value = $this->randomMachineName();
$this->entityValidateAndSave($entity);
// Verify entity has been created properly.
$id = $entity->id();
$entity = EntityTest::load($id);
$this->assertInstanceOf(FieldItemListInterface::class, $entity->field_datetime);
$this->assertInstanceOf(FieldItemInterface::class, $entity->field_datetime[0]);
$this->assertEquals($value, $entity->field_datetime->value);
$this->assertEquals($value, $entity->field_datetime[0]->value);
$this->assertEquals(DateTimeItemInterface::STORAGE_TIMEZONE, $entity->field_datetime->date
->getTimeZone()
->getName());
$this->assertEquals('12:00:00', $entity->field_datetime->date
->format('H:i:s'));
$entity->field_datetime->date
->setDefaultDateTime();
$this->assertEquals('12:00:00', $entity->field_datetime->date
->format('H:i:s'));
// Verify changing the date value.
$new_value = '2016-11-04';
$entity->field_datetime->value = $new_value;
$this->assertEquals($new_value, $entity->field_datetime->value);
$this->assertEquals(DateTimeItemInterface::STORAGE_TIMEZONE, $entity->field_datetime->date
->getTimeZone()
->getName());
$this->assertEquals('12:00:00', $entity->field_datetime->date
->format('H:i:s'));
$entity->field_datetime->date
->setDefaultDateTime();
$this->assertEquals('12:00:00', $entity->field_datetime->date
->format('H:i:s'));
// Read changed entity and assert changed values.
$this->entityValidateAndSave($entity);
$entity = EntityTest::load($id);
$this->assertEquals($new_value, $entity->field_datetime->value);
$this->assertEquals(DateTimeItemInterface::STORAGE_TIMEZONE, $entity->field_datetime->date
->getTimeZone()
->getName());
$this->assertEquals('12:00:00', $entity->field_datetime->date
->format('H:i:s'));
$entity->field_datetime->date
->setDefaultDateTime();
$this->assertEquals('12:00:00', $entity->field_datetime->date
->format('H:i:s'));
// Test the generateSampleValue() method.
$entity = EntityTest::create();
$entity->field_datetime
->generateSampleItems();
$this->assertEquals('12:00:00', $entity->field_datetime->date
->format('H:i:s'));
$entity->field_datetime->date
->setDefaultDateTime();
$this->assertEquals('12:00:00', $entity->field_datetime->date
->format('H:i:s'));
$this->entityValidateAndSave($entity);
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.