You are here

public function DynamicEntityReferenceConfigEntityBaseFieldTest::testRevisionableBaseField in Dynamic Entity Reference 8.2

Config entity only revisionable base DER field.

File

tests/src/Kernel/DynamicEntityReferenceConfigEntityBaseFieldTest.php, line 115

Class

DynamicEntityReferenceConfigEntityBaseFieldTest
Base field tests for referencing config entities.

Namespace

Drupal\Tests\dynamic_entity_reference\Kernel

Code

public function testRevisionableBaseField() {

  // Make this base field revisionable.
  $this->state
    ->set('dynamic_entity_reference_entity_test_revisionable', TRUE);

  // @see dynamic_entity_reference_entity_test_entity_base_field_info()
  $this
    ->enableModules([
    'dynamic_entity_reference_entity_test',
  ]);

  // Update entity_test schema.
  $entity_definition_update_manager = \Drupal::entityDefinitionUpdateManager();
  $mock_entity_type = $this
    ->prophesize(EntityTypeInterface::class);
  $mock_entity_type
    ->id()
    ->willReturn('entity_test');
  $field_storage_definitions = dynamic_entity_reference_entity_test_entity_base_field_info($mock_entity_type
    ->reveal());
  foreach ($field_storage_definitions as $field_name => $field_storage_definition) {
    $entity_definition_update_manager
      ->installFieldStorageDefinition($field_name, 'entity_test', 'dynamic_entity_reference_entity_test', $field_storage_definition);
  }
  $entity = $this->container
    ->get('entity_type.manager')
    ->getStorage($this->entityType)
    ->create([
    'type' => $this->bundle,
  ]);
  $entity->{$this->fieldName}->target_type = $this->configTestReference
    ->getEntityTypeId();
  $entity->{$this->fieldName}->target_id = $this->configTestReference
    ->id();
  $violations = $entity->{$this->fieldName}
    ->validate();
  $this
    ->assertEmpty($violations
    ->count(), 'Validation passes.');

  // Save the entity and update.
  $entity
    ->save();
  $referenced_entity = $this->container
    ->get('entity_type.manager')
    ->getStorage('config_test')
    ->create([
    'id' => 'bar',
    'label' => 'Bar',
    'style' => 'foo',
  ]);
  $referenced_entity
    ->save();
  $entity->{$this->fieldName}->target_type = $referenced_entity
    ->getEntityTypeId();
  $entity->{$this->fieldName}->target_id = $referenced_entity
    ->id();
  $violations = $entity->{$this->fieldName}
    ->validate();
  $this
    ->assertEmpty($violations
    ->count(), 'Validation passes.');
  $entity
    ->save();
  $entity = $this->container
    ->get('entity_type.manager')
    ->getStorage($this->entityType)
    ->load($entity
    ->id());
  $referenced = $entity->{$this->fieldName}
    ->referencedEntities();
  $this
    ->assertEquals(1, count($referenced));
  $this
    ->assertEquals('bar', $referenced[0]
    ->id());
}