class TimestampItemTest
Same name and namespace in other branches
- 11.x core/modules/field/tests/src/Kernel/Timestamp/TimestampItemTest.php \Drupal\Tests\field\Kernel\Timestamp\TimestampItemTest
- 10 core/modules/field/tests/src/Kernel/Timestamp/TimestampItemTest.php \Drupal\Tests\field\Kernel\Timestamp\TimestampItemTest
- 9 core/modules/field/tests/src/Kernel/Timestamp/TimestampItemTest.php \Drupal\Tests\field\Kernel\Timestamp\TimestampItemTest
Tests the timestamp fields.
@group field
Hierarchy
- class \Drupal\KernelTests\KernelTestBase implements \Drupal\Core\DependencyInjection\ServiceProviderInterface uses \Drupal\KernelTests\AssertLegacyTrait, \Drupal\KernelTests\AssertContentTrait, \Drupal\Tests\AssertHelperTrait, \Drupal\Tests\RandomGeneratorTrait, \Drupal\Tests\ConfigTestTrait, \Drupal\Tests\TestRequirementsTrait, \Drupal\Tests\PhpunitCompatibilityTrait extends \PHPUnit\Framework\TestCase
- class \Drupal\Tests\field\Kernel\FieldKernelTestBase extends \Drupal\KernelTests\KernelTestBase
- class \Drupal\Tests\field\Kernel\Timestamp\TimestampItemTest extends \Drupal\Tests\field\Kernel\FieldKernelTestBase
- class \Drupal\Tests\field\Kernel\FieldKernelTestBase extends \Drupal\KernelTests\KernelTestBase
Expanded class hierarchy of TimestampItemTest
File
-
core/
modules/ field/ tests/ src/ Kernel/ Timestamp/ TimestampItemTest.php, line 17
Namespace
Drupal\Tests\field\Kernel\TimestampView source
class TimestampItemTest extends FieldKernelTestBase {
/**
* A field storage to use in this test class.
*
* @var \Drupal\field\Entity\FieldStorageConfig
*/
protected $fieldStorage;
/**
* The field used in this test class.
*
* @var \Drupal\field\Entity\FieldConfig
*/
protected $field;
/**
* {@inheritdoc}
*/
protected function setUp() {
parent::setUp();
// Create a field with settings to validate.
$this->fieldStorage = FieldStorageConfig::create([
'field_name' => 'field_timestamp',
'type' => 'timestamp',
'entity_type' => 'entity_test',
]);
$this->fieldStorage
->save();
$this->field = FieldConfig::create([
'field_storage' => $this->fieldStorage,
'bundle' => 'entity_test',
]);
$this->field
->save();
}
/**
* Tests using entity fields of the datetime field type.
*/
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);
}
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.