function EntityReferenceFieldTest::testReferencedEntitiesMultipleLoad

Same name and namespace in other branches
  1. 9 core/tests/Drupal/KernelTests/Core/Entity/EntityReferenceFieldTest.php \Drupal\KernelTests\Core\Entity\EntityReferenceFieldTest::testReferencedEntitiesMultipleLoad()
  2. 10 core/tests/Drupal/KernelTests/Core/Entity/EntityReferenceFieldTest.php \Drupal\KernelTests\Core\Entity\EntityReferenceFieldTest::testReferencedEntitiesMultipleLoad()
  3. 11.x core/tests/Drupal/KernelTests/Core/Entity/EntityReferenceFieldTest.php \Drupal\KernelTests\Core\Entity\EntityReferenceFieldTest::testReferencedEntitiesMultipleLoad()

Tests the multiple target entities loader.

File

core/tests/Drupal/KernelTests/Core/Entity/EntityReferenceFieldTest.php, line 124

Class

EntityReferenceFieldTest
Tests for the entity reference field.

Namespace

Drupal\KernelTests\Core\Entity

Code

public function testReferencedEntitiesMultipleLoad() {
    // Create the parent entity.
    $entity = $this->container
        ->get('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 = $this->container
            ->get('entity_type.manager')
            ->getStorage($this->referencedEntityType)
            ->create([
            'type' => $this->bundle,
        ]);
        $target_entity->save();
        $target_entities[] = $target_entity;
        $reference_field[]['target_id'] = $target_entity->id();
    }
    // Also attach a non-existent entity and a NULL target id.
    $reference_field[3]['target_id'] = 99999;
    $target_entities[3] = NULL;
    $reference_field[4]['target_id'] = NULL;
    $target_entities[4] = NULL;
    // Attach the first created target entity as the sixth item ($delta == 5) 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 entity reference
    // field.
    $reference_field[5] = $reference_field[0];
    $target_entities[5] = $target_entities[0];
    // Create a new target entity that is not saved, thus testing the
    // "autocreate" feature.
    $target_entity_unsaved = $this->container
        ->get('entity_type.manager')
        ->getStorage($this->referencedEntityType)
        ->create([
        'type' => $this->bundle,
        'name' => $this->randomString(),
    ]);
    $reference_field[6]['entity'] = $target_entity_unsaved;
    $target_entities[6] = $target_entity_unsaved;
    // Set the field value.
    $entity->{$this->fieldName}
        ->setValue($reference_field);
    // Load the target entities using EntityReferenceField::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->assertEqual($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->assertEqual($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]));
        }
    }
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.