public function TimestampItemTest::testDateTime in Drupal 10
Same name and namespace in other branches
- 8 core/modules/field/tests/src/Kernel/Timestamp/TimestampItemTest.php \Drupal\Tests\field\Kernel\Timestamp\TimestampItemTest::testDateTime()
- 9 core/modules/field/tests/src/Kernel/Timestamp/TimestampItemTest.php \Drupal\Tests\field\Kernel\Timestamp\TimestampItemTest::testDateTime()
Tests using entity fields of the datetime field type.
File
- core/
modules/ field/ tests/ src/ Kernel/ Timestamp/ TimestampItemTest.php, line 56
Class
- TimestampItemTest
- Tests the timestamp fields.
Namespace
Drupal\Tests\field\Kernel\TimestampCode
public function testDateTime() {
// Verify entity creation.
$entity = EntityTest::create();
$value = 1488914208;
$entity->field_timestamp = $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_timestamp);
$this
->assertInstanceOf(FieldItemInterface::class, $entity->field_timestamp[0]);
$this
->assertEquals($entity->field_timestamp->value, $value);
$this
->assertEquals($entity->field_timestamp[0]->value, $value);
// Verify changing the date value.
$new_value = 1488914000;
$entity->field_timestamp->value = $new_value;
$this
->assertEquals($entity->field_timestamp->value, $new_value);
// Read changed entity and assert changed values.
$this
->entityValidateAndSave($entity);
$entity = EntityTest::load($id);
$this
->assertEquals($entity->field_timestamp->value, $new_value);
// Test sample item generation.
$entity = EntityTest::create();
$entity->field_timestamp
->generateSampleItems();
$this
->entityValidateAndSave($entity);
// Ensure there is sample value a generated for the field.
$this
->assertNotNull($entity->field_timestamp->value);
}