You are here

public function FieldAttachOtherTest::testEntityDisplayViewMultiple in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/field/tests/src/Kernel/FieldAttachOtherTest.php \Drupal\Tests\field\Kernel\FieldAttachOtherTest::testEntityDisplayViewMultiple()
  2. 10 core/modules/field/tests/src/Kernel/FieldAttachOtherTest.php \Drupal\Tests\field\Kernel\FieldAttachOtherTest::testEntityDisplayViewMultiple()

Tests rendering fields with EntityDisplay::buildMultiple().

File

core/modules/field/tests/src/Kernel/FieldAttachOtherTest.php, line 136

Class

FieldAttachOtherTest
Tests other Field API functions.

Namespace

Drupal\Tests\field\Kernel

Code

public function testEntityDisplayViewMultiple() {

  // Use a formatter that has a prepareView() step.
  $display = \Drupal::service('entity_display.repository')
    ->getViewDisplay('entity_test', 'entity_test', 'full')
    ->setComponent($this->fieldTestData->field_name, [
    'type' => 'field_test_with_prepare_view',
  ]);

  // Create two entities.
  $entity1 = EntityTest::create([
    'id' => 1,
    'type' => 'entity_test',
  ]);
  $entity1->{$this->fieldTestData->field_name}
    ->setValue($this
    ->_generateTestFieldValues(1));
  $entity2 = EntityTest::create([
    'id' => 2,
    'type' => 'entity_test',
  ]);
  $entity2->{$this->fieldTestData->field_name}
    ->setValue($this
    ->_generateTestFieldValues(1));

  // Run buildMultiple(), and check that the entities come out as expected.
  $display
    ->buildMultiple([
    $entity1,
    $entity2,
  ]);
  $item1 = $entity1->{$this->fieldTestData->field_name}[0];
  $this
    ->assertEquals($item1->value + 1, $item1->additional_formatter_value, 'Entity 1 ran through the prepareView() formatter method.');
  $item2 = $entity2->{$this->fieldTestData->field_name}[0];
  $this
    ->assertEquals($item2->value + 1, $item2->additional_formatter_value, 'Entity 2 ran through the prepareView() formatter method.');
}