You are here

protected function DynamicEntityReferenceFormatterTest::setUp in Dynamic Entity Reference 8

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

Overrides EntityKernelTestBase::setUp

File

tests/src/Kernel/DynamicEntityReferenceFormatterTest.php, line 71

Class

DynamicEntityReferenceFormatterTest
Tests the formatters functionality.

Namespace

Drupal\Tests\dynamic_entity_reference\Kernel

Code

protected function setUp() : void {
  parent::setUp();

  // Use Classy theme for testing markup output.
  \Drupal::service('theme_installer')
    ->install([
    'classy',
  ]);
  $this
    ->config('system.theme')
    ->set('default', 'classy')
    ->save();

  // Grant the 'view test entity' permission.
  $this
    ->installConfig([
    'user',
  ]);
  Role::load(RoleInterface::ANONYMOUS_ID)
    ->grantPermission('view test entity')
    ->save();

  // The label formatter rendering generates links, so build the router.
  $this->container
    ->get('router.builder')
    ->rebuild();
  FieldStorageConfig::create([
    'field_name' => $this->fieldName,
    'type' => 'dynamic_entity_reference',
    'entity_type' => $this->entityType,
    'cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED,
    'settings' => [
      'exclude_entity_types' => FALSE,
      'entity_type_ids' => [
        $this->entityType,
      ],
    ],
  ])
    ->save();
  FieldConfig::create([
    'field_name' => $this->fieldName,
    'entity_type' => $this->entityType,
    'bundle' => $this->bundle,
    'label' => 'Field test',
    'settings' => [
      'handler' => 'default',
      'handler_settings' => [],
    ],
  ])
    ->save();

  // Set up a field, so that the entity that'll be referenced bubbles up a
  // cache tag when rendering it entirely.
  FieldStorageConfig::create([
    'field_name' => 'body',
    'entity_type' => $this->entityType,
    'type' => 'text',
    'settings' => [],
  ])
    ->save();
  FieldConfig::create([
    'entity_type' => $this->entityType,
    'bundle' => $this->bundle,
    'field_name' => 'body',
    'label' => 'Body',
  ])
    ->save();
  EntityViewDisplay::create([
    'targetEntityType' => $this->entityType,
    'bundle' => $this->bundle,
    'mode' => 'default',
    'status' => TRUE,
  ])
    ->setComponent('body', [
    'type' => 'text_default',
    'settings' => [],
  ])
    ->save();
  FilterFormat::create([
    'format' => 'full_html',
    'name' => 'Full HTML',
  ])
    ->save();

  // Create the entity to be referenced.
  $this->referencedEntity = EntityTest::create([
    'name' => $this
      ->randomMachineName(),
  ]);
  $this->referencedEntity->body = [
    'value' => '<p>Hello, world!</p>',
    'format' => 'full_html',
  ];
  $this->referencedEntity
    ->save();

  // Create another entity to be referenced but do not save it.
  $this->unsavedReferencedEntity = EntityTest::create([
    'name' => $this
      ->randomMachineName(),
  ]);
  $this->unsavedReferencedEntity->body = [
    'value' => '<p>Hello, unsaved world!</p>',
    'format' => 'full_html',
  ];
}