3 namespace Drupal\Tests\field\Kernel;
5 use Drupal\Core\Field\FieldItemInterface;
6 use Drupal\Core\Field\FieldItemListInterface;
7 use Drupal\entity_test\Entity\EntityTest;
8 use Drupal\field\Entity\FieldConfig;
9 use Drupal\field\Entity\FieldStorageConfig;
12 * Tests the new entity API for the shape field type.
16 class ShapeItemTest extends FieldKernelTestBase {
23 public static $modules = ['field_test'];
26 * The name of the field to use in this test.
30 protected $fieldName = 'field_shape';
32 protected function setUp() {
35 // Create a 'shape' field and storage for validation.
36 FieldStorageConfig::create([
37 'field_name' => $this->fieldName,
38 'entity_type' => 'entity_test',
42 'entity_type' => 'entity_test',
43 'field_name' => $this->fieldName,
44 'bundle' => 'entity_test',
49 * Tests using entity fields of the field field type.
51 public function testShapeItem() {
52 // Verify entity creation.
53 $entity = EntityTest::create();
56 $entity->{$this->fieldName}->shape = $shape;
57 $entity->{$this->fieldName}->color = $color;
58 $entity->name->value = $this->randomMachineName();
61 // Verify entity has been created properly.
63 $entity = EntityTest::load($id);
64 $this->assertTrue($entity->{$this->fieldName} instanceof FieldItemListInterface, 'Field implements interface.');
65 $this->assertTrue($entity->{$this->fieldName}[0] instanceof FieldItemInterface, 'Field item implements interface.');
66 $this->assertEqual($entity->{$this->fieldName}->shape, $shape);
67 $this->assertEqual($entity->{$this->fieldName}->color, $color);
68 $this->assertEqual($entity->{$this->fieldName}[0]->shape, $shape);
69 $this->assertEqual($entity->{$this->fieldName}[0]->color, $color);
71 // Verify changing the field value.
72 $new_shape = 'circle';
74 $entity->{$this->fieldName}->shape = $new_shape;
75 $entity->{$this->fieldName}->color = $new_color;
76 $this->assertEqual($entity->{$this->fieldName}->shape, $new_shape);
77 $this->assertEqual($entity->{$this->fieldName}->color, $new_color);
79 // Read changed entity and assert changed values.
81 $entity = EntityTest::load($id);
82 $this->assertEqual($entity->{$this->fieldName}->shape, $new_shape);
83 $this->assertEqual($entity->{$this->fieldName}->color, $new_color);