You are here

protected function EntityCacheTagsTestBase::createReferenceTestEntities in Zircon Profile 8.0

Same name and namespace in other branches
  1. 8 core/modules/system/src/Tests/Entity/EntityCacheTagsTestBase.php \Drupal\system\Tests\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/src/Tests/Entity/EntityCacheTagsTestBase.php
Sets up a Drupal site for running functional and integration tests.

File

core/modules/system/src/Tests/Entity/EntityCacheTagsTestBase.php, line 239
Contains \Drupal\system\Tests\Entity\EntityCacheTagsTestBase.

Class

EntityCacheTagsTestBase
Provides helper methods for Entity cache tags tests.

Namespace

Drupal\system\Tests\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';
  entity_create('field_storage_config', array(
    'field_name' => $field_name,
    'entity_type' => $entity_type,
    'type' => 'entity_reference',
    'cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED,
    'settings' => array(
      'target_type' => $referenced_entity
        ->getEntityTypeId(),
    ),
  ))
    ->save();
  entity_create('field_config', array(
    'field_name' => $field_name,
    'entity_type' => $entity_type,
    'bundle' => $bundle,
    'settings' => array(
      'handler' => 'default',
      'handler_settings' => array(
        'target_bundles' => array(
          $referenced_entity
            ->bundle() => $referenced_entity
            ->bundle(),
        ),
        'sort' => array(
          'field' => '_none',
        ),
        'auto_create' => FALSE,
      ),
    ),
  ))
    ->save();
  if (!$this->entity
    ->getEntityType()
    ->hasHandlerClass('view_builder')) {
    entity_get_display($entity_type, $bundle, 'full')
      ->setComponent($field_name, array(
      'type' => 'entity_reference_label',
    ))
      ->save();
  }
  else {
    $referenced_entity_view_mode = $this
      ->selectViewMode($this->entity
      ->getEntityTypeId());
    entity_get_display($entity_type, $bundle, 'full')
      ->setComponent($field_name, array(
      'type' => 'entity_reference_entity_view',
      'settings' => array(
        'view_mode' => $referenced_entity_view_mode,
      ),
    ))
      ->save();
  }

  // Create an entity that does reference the entity being tested.
  $label_key = \Drupal::entityManager()
    ->getDefinition($entity_type)
    ->getKey('label');
  $referencing_entity = entity_create($entity_type, array(
    $label_key => 'Referencing ' . $entity_type,
    'status' => 1,
    'type' => $bundle,
    $field_name => array(
      'target_id' => $referenced_entity
        ->id(),
    ),
  ));
  $referencing_entity
    ->save();

  // Create an entity that does not reference the entity being tested.
  $non_referencing_entity = entity_create($entity_type, array(
    $label_key => 'Non-referencing ' . $entity_type,
    'status' => 1,
    'type' => $bundle,
  ));
  $non_referencing_entity
    ->save();
  return array(
    $referencing_entity,
    $non_referencing_entity,
  );
}