Chris@0: $this->fieldName, Chris@0: 'entity_type' => 'entity_test', Chris@0: 'type' => 'shape', Chris@0: ])->save(); Chris@0: FieldConfig::create([ Chris@0: 'entity_type' => 'entity_test', Chris@0: 'field_name' => $this->fieldName, Chris@0: 'bundle' => 'entity_test', Chris@0: ])->save(); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests using entity fields of the field field type. Chris@0: */ Chris@0: public function testShapeItem() { Chris@0: // Verify entity creation. Chris@0: $entity = EntityTest::create(); Chris@0: $shape = 'cube'; Chris@0: $color = 'blue'; Chris@0: $entity->{$this->fieldName}->shape = $shape; Chris@0: $entity->{$this->fieldName}->color = $color; Chris@0: $entity->name->value = $this->randomMachineName(); Chris@0: $entity->save(); Chris@0: Chris@0: // Verify entity has been created properly. Chris@0: $id = $entity->id(); Chris@0: $entity = EntityTest::load($id); Chris@0: $this->assertTrue($entity->{$this->fieldName} instanceof FieldItemListInterface, 'Field implements interface.'); Chris@0: $this->assertTrue($entity->{$this->fieldName}[0] instanceof FieldItemInterface, 'Field item implements interface.'); Chris@0: $this->assertEqual($entity->{$this->fieldName}->shape, $shape); Chris@0: $this->assertEqual($entity->{$this->fieldName}->color, $color); Chris@0: $this->assertEqual($entity->{$this->fieldName}[0]->shape, $shape); Chris@0: $this->assertEqual($entity->{$this->fieldName}[0]->color, $color); Chris@0: Chris@0: // Verify changing the field value. Chris@0: $new_shape = 'circle'; Chris@0: $new_color = 'red'; Chris@0: $entity->{$this->fieldName}->shape = $new_shape; Chris@0: $entity->{$this->fieldName}->color = $new_color; Chris@0: $this->assertEqual($entity->{$this->fieldName}->shape, $new_shape); Chris@0: $this->assertEqual($entity->{$this->fieldName}->color, $new_color); Chris@0: Chris@0: // Read changed entity and assert changed values. Chris@0: $entity->save(); Chris@0: $entity = EntityTest::load($id); Chris@0: $this->assertEqual($entity->{$this->fieldName}->shape, $new_shape); Chris@0: $this->assertEqual($entity->{$this->fieldName}->color, $new_color); Chris@0: } Chris@0: Chris@0: }