You are here

public function DynamicEntityReferenceConfigEntityTest::testMixedConfigurableFieldMixedIds in Dynamic Entity Reference 8.2

Mixed content entity IDs (string and int) and config entity.

File

tests/src/Kernel/DynamicEntityReferenceConfigEntityTest.php, line 212

Class

DynamicEntityReferenceConfigEntityTest
Tests for referencing configuration entities with configurable fields.

Namespace

Drupal\Tests\dynamic_entity_reference\Kernel

Code

public function testMixedConfigurableFieldMixedIds() {
  $this->referencedEntityTypes[] = 'entity_test_rev';
  $this->referencedEntityTypes[] = 'entity_test_string_id';
  $this
    ->setUpField();

  // Check 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.');

  // Check content entity (string ID).
  $referenced_entity = $this->container
    ->get('entity_type.manager')
    ->getStorage('entity_test_string_id')
    ->create([
    'type' => $this->bundle,
  ]);
  $entity = $this->container
    ->get('entity_type.manager')
    ->getStorage($this->entityType)
    ->create([
    'type' => $this->bundle,
  ]);
  $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.');

  // Check content entity (int ID).
  $entity = $this->container
    ->get('entity_type.manager')
    ->getStorage($this->entityType)
    ->create([
    'type' => $this->bundle,
  ]);
  $entity->{$this->fieldName}->target_type = $this->contentEntityReference
    ->getEntityTypeId();
  $entity->{$this->fieldName}->target_id = $this->contentEntityReference
    ->id();
  $violations = $entity->{$this->fieldName}
    ->validate();
  $this
    ->assertEmpty($violations
    ->count(), 'Validation passes.');
}