3 namespace Drupal\Tests\field\Kernel;
5 use Drupal\Core\Field\BaseFieldDefinition;
6 use Drupal\Core\Field\FieldItemInterface;
7 use Drupal\Core\Field\FieldItemListInterface;
8 use Drupal\entity_test\Entity\EntityTest;
9 use Drupal\field\Entity\FieldConfig;
10 use Drupal\field\Entity\FieldStorageConfig;
13 * Tests the new entity API for the test field type.
17 class TestItemTest extends FieldKernelTestBase {
24 public static $modules = ['field_test'];
27 * The name of the field to use in this test.
31 protected $fieldName = 'field_test';
33 protected function setUp() {
36 // Create a 'test_field' field and storage for validation.
37 FieldStorageConfig::create([
38 'field_name' => $this->fieldName,
39 'entity_type' => 'entity_test',
40 'type' => 'test_field',
43 'entity_type' => 'entity_test',
44 'field_name' => $this->fieldName,
45 'bundle' => 'entity_test',
50 * Tests using entity fields of the field field type.
52 public function testTestItem() {
53 // Verify entity creation.
54 $entity = EntityTest::create();
56 $entity->field_test = $value;
57 $entity->name->value = $this->randomMachineName();
60 // Verify entity has been created properly.
62 $entity = EntityTest::load($id);
63 $this->assertTrue($entity->{$this->fieldName} instanceof FieldItemListInterface, 'Field implements interface.');
64 $this->assertTrue($entity->{$this->fieldName}[0] instanceof FieldItemInterface, 'Field item implements interface.');
65 $this->assertEqual($entity->{$this->fieldName}->value, $value);
66 $this->assertEqual($entity->{$this->fieldName}[0]->value, $value);
68 // Verify changing the field value.
69 $new_value = rand(1, 10);
70 $entity->field_test->value = $new_value;
71 $this->assertEqual($entity->{$this->fieldName}->value, $new_value);
73 // Read changed entity and assert changed values.
75 $entity = EntityTest::load($id);
76 $this->assertEqual($entity->{$this->fieldName}->value, $new_value);
78 // Test the schema for this field type.
92 $field_schema = BaseFieldDefinition::create('test_field')->getSchema();
93 $this->assertEqual($field_schema, $expected_schema);