You are here

public function ShapeItemTest::testShapeItem in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/field/tests/src/Kernel/ShapeItemTest.php \Drupal\Tests\field\Kernel\ShapeItemTest::testShapeItem()
  2. 10 core/modules/field/tests/src/Kernel/ShapeItemTest.php \Drupal\Tests\field\Kernel\ShapeItemTest::testShapeItem()

Tests using entity fields of the field field type.

File

core/modules/field/tests/src/Kernel/ShapeItemTest.php, line 51

Class

ShapeItemTest
Tests the new entity API for the shape field type.

Namespace

Drupal\Tests\field\Kernel

Code

public function testShapeItem() {

  // Verify entity creation.
  $entity = EntityTest::create();
  $shape = 'cube';
  $color = 'blue';
  $entity->{$this->fieldName}->shape = $shape;
  $entity->{$this->fieldName}->color = $color;
  $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
    ->assertEqual($entity->{$this->fieldName}->shape, $shape);
  $this
    ->assertEqual($entity->{$this->fieldName}->color, $color);
  $this
    ->assertEqual($entity->{$this->fieldName}[0]->shape, $shape);
  $this
    ->assertEqual($entity->{$this->fieldName}[0]->color, $color);

  // Verify changing the field value.
  $new_shape = 'circle';
  $new_color = 'red';
  $entity->{$this->fieldName}->shape = $new_shape;
  $entity->{$this->fieldName}->color = $new_color;
  $this
    ->assertEqual($entity->{$this->fieldName}->shape, $new_shape);
  $this
    ->assertEqual($entity->{$this->fieldName}->color, $new_color);

  // Read changed entity and assert changed values.
  $entity
    ->save();
  $entity = EntityTest::load($id);
  $this
    ->assertEqual($entity->{$this->fieldName}->shape, $new_shape);
  $this
    ->assertEqual($entity->{$this->fieldName}->color, $new_color);
}