You are here

public function DynamicEntityReferenceBaseFieldRevisionTest::testRevisionableMultipleEntityReference in Dynamic Entity Reference 8

Same name and namespace in other branches
  1. 8.2 tests/src/Kernel/DynamicEntityReferenceBaseFieldRevisionTest.php \Drupal\Tests\dynamic_entity_reference\Kernel\DynamicEntityReferenceBaseFieldRevisionTest::testRevisionableMultipleEntityReference()

Tests the multiple non-revisionable basefields.

File

tests/src/Kernel/DynamicEntityReferenceBaseFieldRevisionTest.php, line 470

Class

DynamicEntityReferenceBaseFieldRevisionTest
Tests for the dynamic entity reference base field for revisionable entities.

Namespace

Drupal\Tests\dynamic_entity_reference\Kernel

Code

public function testRevisionableMultipleEntityReference() {
  \Drupal::state()
    ->set('dynamic_entity_reference_entity_test_entities', [
    $this->entityType,
    $this->referencedEntityType,
  ]);
  \Drupal::state()
    ->set('dynamic_entity_reference_entity_test_with_two_base_fields', TRUE);
  \Drupal::state()
    ->set('dynamic_entity_reference_entity_test_revisionable', TRUE);
  $this
    ->enableModules([
    'dynamic_entity_reference_entity_test',
  ]);
  $this
    ->installEntitySchema($this->entityType);
  $this
    ->installEntitySchema($this->referencedEntityType);

  // Create some test entities which link each other.
  $referenced_entity = $this->container
    ->get('entity_type.manager')
    ->getStorage($this->entityType)
    ->create();
  $referenced_entity
    ->save();
  $referenced_entity_mul = $this->container
    ->get('entity_type.manager')
    ->getStorage($this->referencedEntityType)
    ->create();
  $referenced_entity_mul
    ->save();
  $entity = $this->container
    ->get('entity_type.manager')
    ->getStorage($this->entityType)
    ->create();
  $entity->dynamic_references[] = $referenced_entity;
  $entity->der[] = $referenced_entity_mul;
  $entity
    ->save();

  // Loads an unchanged entity from the database.
  $entity = $this->container
    ->get('entity_type.manager')
    ->getStorage($this->entityType)
    ->loadUnchanged($entity
    ->id());

  // Check references correctly for dynamic_references field.
  $this
    ->assertEquals($entity->dynamic_references[0]->target_id, $referenced_entity
    ->id());
  $this
    ->assertEquals($entity->dynamic_references[0]->target_type, $referenced_entity
    ->getEntityTypeId());
  $this
    ->assertEquals($entity->dynamic_references[0]->entity
    ->getName(), $referenced_entity
    ->getName());
  $this
    ->assertEquals($entity->dynamic_references[0]->entity
    ->id(), $referenced_entity
    ->id());
  $this
    ->assertEquals($entity->dynamic_references[0]->entity
    ->uuid(), $referenced_entity
    ->uuid());

  // Check references correctly for der field.
  $this
    ->assertEquals($entity->der[0]->target_id, $referenced_entity_mul
    ->id());
  $this
    ->assertEquals($entity->der[0]->target_type, $referenced_entity_mul
    ->getEntityTypeId());
  $this
    ->assertEquals($entity->der[0]->entity
    ->getName(), $referenced_entity_mul
    ->getName());
  $this
    ->assertEquals($entity->der[0]->entity
    ->id(), $referenced_entity_mul
    ->id());
  $this
    ->assertEquals($entity->der[0]->entity
    ->uuid(), $referenced_entity_mul
    ->uuid());
  $entity = $this->container
    ->get('entity_type.manager')
    ->getStorage($this->referencedEntityType)
    ->create();
  $entity->der[] = $referenced_entity;
  $entity->dynamic_references[] = $referenced_entity_mul;
  $entity
    ->save();

  // Loads an unchanged entity from the database.
  $entity = $this->container
    ->get('entity_type.manager')
    ->getStorage($this->referencedEntityType)
    ->loadUnchanged($entity
    ->id());

  // Check references correctly for dynamic_references field.
  $this
    ->assertEquals($entity->dynamic_references[0]->target_id, $referenced_entity_mul
    ->id());
  $this
    ->assertEquals($entity->dynamic_references[0]->target_type, $referenced_entity_mul
    ->getEntityTypeId());
  $this
    ->assertEquals($entity->dynamic_references[0]->entity
    ->getName(), $referenced_entity_mul
    ->getName());
  $this
    ->assertEquals($entity->dynamic_references[0]->entity
    ->id(), $referenced_entity_mul
    ->id());
  $this
    ->assertEquals($entity->dynamic_references[0]->entity
    ->uuid(), $referenced_entity_mul
    ->uuid());

  // Check references correctly for der field.
  $this
    ->assertEquals($entity->der[0]->target_id, $referenced_entity
    ->id());
  $this
    ->assertEquals($entity->der[0]->target_type, $referenced_entity
    ->getEntityTypeId());
  $this
    ->assertEquals($entity->der[0]->entity
    ->getName(), $referenced_entity
    ->getName());
  $this
    ->assertEquals($entity->der[0]->entity
    ->id(), $referenced_entity
    ->id());
  $this
    ->assertEquals($entity->der[0]->entity
    ->uuid(), $referenced_entity
    ->uuid());
}