function EntityCacheTagsTestBase::createReferenceTestEntities

Same name in other branches
  1. 8.9.x core/modules/system/src/Tests/Entity/EntityCacheTagsTestBase.php \Drupal\system\Tests\Entity\EntityCacheTagsTestBase::createReferenceTestEntities()
  2. 8.9.x core/modules/system/tests/src/Functional/Entity/EntityCacheTagsTestBase.php \Drupal\Tests\system\Functional\Entity\EntityCacheTagsTestBase::createReferenceTestEntities()
  3. 10 core/modules/system/tests/src/Functional/Entity/EntityCacheTagsTestBase.php \Drupal\Tests\system\Functional\Entity\EntityCacheTagsTestBase::createReferenceTestEntities()
  4. 11.x core/modules/system/tests/src/Functional/Entity/EntityCacheTagsTestBase.php \Drupal\Tests\system\Functional\Entity\EntityCacheTagsTestBase::createReferenceTestEntities()

Creates a referencing and a non-referencing entity for testing purposes.

Parameters

\Drupal\Core\Entity\EntityInterface $referenced_entity: The entity that the referencing entity should reference.

Return value

\Drupal\Core\Entity\EntityInterface[] An array containing a referencing entity and a non-referencing entity.

1 call to EntityCacheTagsTestBase::createReferenceTestEntities()
EntityCacheTagsTestBase::setUp in core/modules/system/tests/src/Functional/Entity/EntityCacheTagsTestBase.php

File

core/modules/system/tests/src/Functional/Entity/EntityCacheTagsTestBase.php, line 235

Class

EntityCacheTagsTestBase
Provides helper methods for Entity cache tags tests.

Namespace

Drupal\Tests\system\Functional\Entity

Code

protected function createReferenceTestEntities($referenced_entity) {
    // All referencing entities should be of the type 'entity_test'.
    $entity_type = 'entity_test';
    // Create a "foo" bundle for the given entity type.
    $bundle = 'foo';
    entity_test_create_bundle($bundle, NULL, $entity_type);
    // Add a field of the given type to the given entity type's "foo" bundle.
    $field_name = $referenced_entity->getEntityTypeId() . '_reference';
    FieldStorageConfig::create([
        'field_name' => $field_name,
        'entity_type' => $entity_type,
        'type' => 'entity_reference',
        'cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED,
        'settings' => [
            'target_type' => $referenced_entity->getEntityTypeId(),
        ],
    ])
        ->save();
    FieldConfig::create([
        'field_name' => $field_name,
        'entity_type' => $entity_type,
        'bundle' => $bundle,
        'settings' => [
            'handler' => 'default',
            'handler_settings' => [
                'target_bundles' => [
                    $referenced_entity->bundle() => $referenced_entity->bundle(),
                ],
                'sort' => [
                    'field' => '_none',
                ],
                'auto_create' => FALSE,
            ],
        ],
    ])
        ->save();
    
    /** @var \Drupal\Core\Entity\EntityDisplayRepositoryInterface $display_repository */
    $display_repository = \Drupal::service('entity_display.repository');
    if (!$this->entity
        ->getEntityType()
        ->hasHandlerClass('view_builder')) {
        $display_repository->getViewDisplay($entity_type, $bundle, 'full')
            ->setComponent($field_name, [
            'type' => 'entity_reference_label',
        ])
            ->save();
    }
    else {
        $referenced_entity_view_mode = $this->selectViewMode($this->entity
            ->getEntityTypeId());
        $display_repository->getViewDisplay($entity_type, $bundle, 'full')
            ->setComponent($field_name, [
            'type' => 'entity_reference_entity_view',
            'settings' => [
                'view_mode' => $referenced_entity_view_mode,
            ],
        ])
            ->save();
    }
    // Create an entity that does reference the entity being tested.
    $label_key = \Drupal::entityTypeManager()->getDefinition($entity_type)
        ->getKey('label');
    $referencing_entity = $this->container
        ->get('entity_type.manager')
        ->getStorage($entity_type)
        ->create([
        $label_key => 'Referencing ' . $entity_type,
        'status' => 1,
        'type' => $bundle,
        $field_name => [
            'target_id' => $referenced_entity->id(),
        ],
    ]);
    $referencing_entity->save();
    // Create an entity that does not reference the entity being tested.
    $non_referencing_entity = $this->container
        ->get('entity_type.manager')
        ->getStorage($entity_type)
        ->create([
        $label_key => 'Non-referencing ' . $entity_type,
        'status' => 1,
        'type' => $bundle,
    ]);
    $non_referencing_entity->save();
    return [
        $referencing_entity,
        $non_referencing_entity,
    ];
}

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