function EntityViewBuilderTest::testBuildComponents

Tests build components using a mocked Iterator.

File

core/tests/Drupal/Tests/Core/Entity/EntityViewBuilderTest.php, line 46

Class

EntityViewBuilderTest
@coversDefaultClass \Drupal\Core\Entity\EntityViewBuilder[[api-linebreak]] @group Entity

Namespace

Drupal\Tests\Core\Entity

Code

public function testBuildComponents() : void {
  $field_name = $this->randomMachineName();
  $bundle = $this->randomMachineName();
  $entity_id = mt_rand(20, 30);
  $field_item_list = $this->createStub(FieldItemListInterface::class);
  $item = new \stdClass();
  $this->setupMockIterator($field_item_list, [
    $item,
  ]);
  $entity = $this->createConfiguredStub(FieldableEntityInterface::class, [
    'bundle' => $bundle,
    'hasField' => TRUE,
    'get' => $field_item_list,
  ]);
  $formatter_result = [
    $entity_id => [
      '#' . $this->randomMachineName() => $this->randomString(),
    ],
  ];
  $display = $this->createConfiguredStub(EntityViewDisplayInterface::class, [
    'getComponents' => [
      $field_name => [],
    ],
    'buildMultiple' => $formatter_result,
  ]);
  $entities = [
    $entity_id => $entity,
  ];
  $displays = [
    $bundle => $display,
  ];
  $build = [
    $entity_id => [],
  ];
  $view_mode = $this->randomMachineName();
  // Assert the hook is invoked.
  $module_handler = $this->createMock(ModuleHandlerInterface::class);
  $module_handler->expects($this->once())
    ->method('invokeAll')
    ->with('entity_prepare_view', [
    self::ENTITY_TYPE_ID,
    $entities,
    $displays,
    $view_mode,
  ]);
  $this->viewBuilder
    ->setModuleHandler($module_handler);
  $this->viewBuilder
    ->buildComponents($build, $entities, $displays, $view_mode);
  $this->assertSame([], $item->_attributes);
  $this->assertSame($formatter_result, $build);
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.