You are here

public function ContextItemTest::testField in Core Context 8

Tests attaching contexts to a content entity in a field.

File

tests/src/Kernel/ContextItemTest.php, line 58

Class

ContextItemTest
@group core_context

Namespace

Drupal\Tests\core_context\Kernel

Code

public function testField() {
  EntityTestBundle::create([
    'id' => 'foobaz',
  ])
    ->save();
  $storage = FieldStorageConfig::create([
    'entity_type' => 'entity_test',
    'field_name' => 'context',
    'type' => 'context',
    'cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED,
  ]);
  $storage
    ->save();
  FieldConfig::create([
    'field_storage' => $storage,
    'bundle' => 'foobaz',
  ])
    ->save();

  /** @var EntityTest $entity */
  $entity = EntityTest::create([
    'type' => 'foobaz',
  ])
    ->set('context', [
    'id' => 'wambooli',
    'type' => 'string',
    'label' => 'Pastafazoul!',
    'description' => 'I would try to wow you, but I cannot be arsed.',
    'value' => 'Behold!',
  ]);
  $entity
    ->save();

  // Reload the entity so we can assert that all the values were saved.
  $entity = EntityTest::load($entity
    ->id());
  $this
    ->assertSame('wambooli', $entity->context->id);
  $this
    ->assertSame('string', $entity->context->type);
  $this
    ->assertSame('Pastafazoul!', $entity->context->label);
  $this
    ->assertSame('I would try to wow you, but I cannot be arsed.', $entity->context->description);
  $this
    ->assertSame('Behold!', $entity->context->value);
}