You are here

public function DynamicEntityReferenceConfigEntityBaseFieldTest::testMixedRevisionableBaseField in Dynamic Entity Reference 8.2

Content entity and config entity revisionable base DER field.

File

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

Class

DynamicEntityReferenceConfigEntityBaseFieldTest
Base field tests for referencing config entities.

Namespace

Drupal\Tests\dynamic_entity_reference\Kernel

Code

public function testMixedRevisionableBaseField() {

  // @see dynamic_entity_reference_entity_test_entity_base_field_info()
  // Make this base field revisionable.
  $this->state
    ->set('dynamic_entity_reference_entity_test_revisionable', TRUE);
  $this->state
    ->set('dynamic_entity_reference_entity_test_entities', [
    $this->entityType,
    'config_test',
    'entity_test_mul',
  ]);
  $this
    ->enableModules([
    'dynamic_entity_reference_entity_test',
  ]);
  $this
    ->installEntitySchema('entity_test_mul');

  // 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);
  }

  // Reference a config entity.
  $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 to use a content entity.
  $entity
    ->save();
  $referenced_entity = $this->container
    ->get('entity_type.manager')
    ->getStorage('entity_test_mul')
    ->create([
    'type' => $this->bundle,
  ]);
  $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($referenced_entity
    ->id(), $referenced[0]
    ->id());
}