TimestampItemTest.php in Drupal 8
File
core/modules/field/tests/src/Kernel/Timestamp/TimestampItemTest.php
View source
<?php
namespace Drupal\Tests\field\Kernel\Timestamp;
use Drupal\Core\Field\FieldItemInterface;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\entity_test\Entity\EntityTest;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\Tests\field\Kernel\FieldKernelTestBase;
class TimestampItemTest extends FieldKernelTestBase {
protected $fieldStorage;
protected $field;
protected function setUp() {
parent::setUp();
$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();
}
public function testDateTime() {
$entity = EntityTest::create();
$value = 1488914208;
$entity->field_timestamp = $value;
$entity->name->value = $this
->randomMachineName();
$this
->entityValidateAndSave($entity);
$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);
$new_value = 1488914000;
$entity->field_timestamp->value = $new_value;
$this
->assertEquals($entity->field_timestamp->value, $new_value);
$this
->entityValidateAndSave($entity);
$entity = EntityTest::load($id);
$this
->assertEquals($entity->field_timestamp->value, $new_value);
$entity = EntityTest::create();
$entity->field_timestamp
->generateSampleItems();
$this
->entityValidateAndSave($entity);
$this
->assertNotNull($entity->field_timestamp->value);
}
}