You are here

public function DynamicEntityReferenceFieldTest::testReferencedEntitiesMultipleLoad in Dynamic Entity Reference 8

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

Tests the multiple target entities loader.

File

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

Class

DynamicEntityReferenceFieldTest
Tests for the dynamic entity reference field.

Namespace

Drupal\Tests\dynamic_entity_reference\Kernel

Code

public function testReferencedEntitiesMultipleLoad() {
  $entity_type_manager = \Drupal::entityTypeManager();

  // Create the parent entity.
  $entity = $entity_type_manager
    ->getStorage($this->entityType)
    ->create([
    'type' => $this->bundle,
  ]);

  // Create three target entities and attach them to parent field.
  $target_entities = [];
  $reference_field = [];
  for ($i = 0; $i < 3; $i++) {
    $target_entity = $entity_type_manager
      ->getStorage($this->referencedEntityType)
      ->create([
      'type' => $this->bundle,
    ]);
    $target_entity
      ->save();
    $target_entities[] = $target_entity;
    $reference_field[] = [
      'target_id' => $target_entity
        ->id(),
      'target_type' => $this->referencedEntityType,
    ];
  }

  // Also attach a non-existent entity and a NULL target id.
  $reference_field[3]['target_id'] = 99999;
  $reference_field[3]['target_type'] = $this->referencedEntityType;
  $target_entities[3] = NULL;
  $reference_field[4]['target_id'] = NULL;
  $reference_field[4]['target_type'] = $this->referencedEntityType;
  $target_entities[4] = NULL;

  // Also attach a non-existent entity and a NULL target id.
  $reference_field[5]['target_id'] = 99999;
  $reference_field[5]['target_type'] = $this->entityType;
  $target_entities[5] = NULL;
  $reference_field[6]['target_id'] = NULL;
  $reference_field[6]['target_type'] = NULL;
  $target_entities[6] = NULL;

  // Attach the first created target entity as the eighth item ($delta == 7)
  // of the parent entity field. We want to test the case when the same target
  // entity is referenced twice (or more times) in the same dynamic entity
  // reference field.
  $reference_field[7] = $reference_field[0];
  $target_entities[7] = $target_entities[0];

  // Create a new target entity that is not saved, thus testing the
  // "autocreate" feature.
  $target_entity_unsaved = $entity_type_manager
    ->getStorage($this->referencedEntityType)
    ->create([
    'type' => $this->bundle,
    'name' => $this
      ->randomString(),
  ]);
  $reference_field[8]['entity'] = $target_entity_unsaved;
  $target_entities[8] = $target_entity_unsaved;

  // Set the field value.
  $entity->{$this->fieldName}
    ->setValue($reference_field);

  // Load the target entities using
  // DynamicEntityReferenceField::referencedEntities().
  $entities = $entity->{$this->fieldName}
    ->referencedEntities();

  // Test returned entities:
  // - Deltas must be preserved.
  // - Non-existent entities must not be retrieved in target entities result.
  foreach ($target_entities as $delta => $target_entity) {
    if (!empty($target_entity)) {
      if (!$target_entity
        ->isNew()) {

        // There must be an entity in the loaded set having the same id for
        // the same delta.
        $this
          ->assertEquals($target_entity
          ->id(), $entities[$delta]
          ->id());
      }
      else {

        // For entities that were not yet saved, there must an entity in the
        // loaded set having the same label for the same delta.
        $this
          ->assertEquals($target_entity
          ->label(), $entities[$delta]
          ->label());
      }
    }
    else {

      // A non-existent or NULL entity target id must not return any item in
      // the target entities set.
      $this
        ->assertFalse(isset($entities[$delta]));
    }
  }
}