You are here

protected function EntityReferenceFormatterTest::setUp in Drupal 10

Same name and namespace in other branches
  1. 8 core/modules/field/tests/src/Kernel/EntityReference/EntityReferenceFormatterTest.php \Drupal\Tests\field\Kernel\EntityReference\EntityReferenceFormatterTest::setUp()
  2. 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() : void {
  parent::setUp();

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

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