You are here

protected function EntityReferenceFormatterTest::setUp in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/field/tests/src/Kernel/EntityReference/EntityReferenceFormatterTest.php \Drupal\Tests\field\Kernel\EntityReference\EntityReferenceFormatterTest::setUp()

Overrides EntityKernelTestBase::setUp

File

core/modules/field/tests/src/Kernel/EntityReference/EntityReferenceFormatterTest.php, line 64

Class

EntityReferenceFormatterTest
Tests the formatters functionality.

Namespace

Drupal\Tests\field\Kernel\EntityReference

Code

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

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

  // 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();
  $this
    ->createEntityReferenceField($this->entityType, $this->bundle, $this->fieldName, 'Field test', $this->entityType, 'default', [], FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED);

  // 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();
  \Drupal::service('entity_display.repository')
    ->getViewDisplay($this->entityType, $this->bundle)
    ->setComponent('body', [
    'type' => 'text_default',
    'settings' => [],
  ])
    ->save();
  FilterFormat::create([
    'format' => 'full_html',
    'name' => 'Full HTML',
  ])
    ->save();

  // Create the entity to be referenced.
  $this->referencedEntity = $this->container
    ->get('entity_type.manager')
    ->getStorage($this->entityType)
    ->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 = $this->container
    ->get('entity_type.manager')
    ->getStorage($this->entityType)
    ->create([
    'name' => $this
      ->randomMachineName(),
  ]);
  $this->unsavedReferencedEntity->body = [
    'value' => '<p>Hello, unsaved world!</p>',
    'format' => 'full_html',
  ];
}