function TestItemTest::testTestItem
Same name in other branches
- 8.9.x core/modules/field/tests/src/Kernel/TestItemTest.php \Drupal\Tests\field\Kernel\TestItemTest::testTestItem()
- 10 core/modules/field/tests/src/Kernel/TestItemTest.php \Drupal\Tests\field\Kernel\TestItemTest::testTestItem()
- 11.x core/modules/field/tests/src/Kernel/TestItemTest.php \Drupal\Tests\field\Kernel\TestItemTest::testTestItem()
Tests using entity fields of the test field type.
File
-
core/
modules/ field/ tests/ src/ Kernel/ TestItemTest.php, line 55
Class
- TestItemTest
- Tests the new entity API for the test field type.
Namespace
Drupal\Tests\field\KernelCode
public function testTestItem() {
// Verify entity creation.
$entity = EntityTest::create();
$value = rand(1, 10);
$entity->field_test = $value;
$entity->name->value = $this->randomMachineName();
$entity->save();
// Verify entity has been created properly.
$id = $entity->id();
$entity = EntityTest::load($id);
$this->assertInstanceOf(FieldItemListInterface::class, $entity->{$this->fieldName});
$this->assertInstanceOf(FieldItemInterface::class, $entity->{$this->fieldName}[0]);
$this->assertEquals($value, $entity->{$this->fieldName}->value);
$this->assertEquals($value, $entity->{$this->fieldName}[0]->value);
// Verify changing the field value.
$new_value = rand(1, 10);
$entity->field_test->value = $new_value;
$this->assertEquals($new_value, $entity->{$this->fieldName}->value);
// Read changed entity and assert changed values.
$entity->save();
$entity = EntityTest::load($id);
$this->assertEquals($new_value, $entity->{$this->fieldName}->value);
// Test the schema for this field type.
$expected_schema = [
'columns' => [
'value' => [
'type' => 'int',
'size' => 'medium',
],
],
'unique keys' => [],
'indexes' => [
'value' => [
'value',
],
],
'foreign keys' => [],
];
$field_schema = BaseFieldDefinition::create('test_field')->getSchema();
$this->assertEquals($expected_schema, $field_schema);
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.