You are here

public function AreaEntityTest::doTestRender in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/views/tests/src/Kernel/Handler/AreaEntityTest.php \Drupal\Tests\views\Kernel\Handler\AreaEntityTest::doTestRender()

Tests rendering the entity area handler.

Parameters

\Drupal\Core\Entity\EntityInterface[] $entities: The entities.

1 call to AreaEntityTest::doTestRender()
AreaEntityTest::testEntityArea in core/modules/views/tests/src/Kernel/Handler/AreaEntityTest.php
Tests the area handler.

File

core/modules/views/tests/src/Kernel/Handler/AreaEntityTest.php, line 123

Class

AreaEntityTest
Tests the generic entity area handler.

Namespace

Drupal\Tests\views\Kernel\Handler

Code

public function doTestRender($entities) {

  /** @var \Drupal\Core\Render\RendererInterface $renderer */
  $renderer = $this->container
    ->get('renderer');
  $view = Views::getView('test_entity_area');
  $preview = $view
    ->preview('default', [
    $entities[1]
      ->id(),
  ]);
  $this
    ->setRawContent(\Drupal::service('renderer')
    ->renderRoot($preview));
  $view_class = 'js-view-dom-id-' . $view->dom_id;
  $header_xpath = '//div[@class = "' . $view_class . '"]/header[1]';
  $footer_xpath = '//div[@class = "' . $view_class . '"]/footer[1]';
  $result = $this
    ->xpath($header_xpath);
  $this
    ->assertStringContainsString($entities[0]
    ->label(), (string) $result[0], 'The rendered entity appears in the header of the view.');
  $this
    ->assertStringContainsString('full', (string) $result[0], 'The rendered entity appeared in the right view mode.');
  $result = $this
    ->xpath($footer_xpath);
  $this
    ->assertStringContainsString($entities[1]
    ->label(), (string) $result[0], 'The rendered entity appears in the footer of the view.');
  $this
    ->assertStringContainsString('full', (string) $result[0], 'The rendered entity appeared in the right view mode.');
  $preview = $view
    ->preview('default', [
    $entities[1]
      ->id(),
  ]);
  $this
    ->setRawContent($renderer
    ->renderRoot($preview));
  $result = $this
    ->xpath($header_xpath);
  $this
    ->assertStringContainsString($entities[0]
    ->label(), (string) $result[0], 'The rendered entity appears in the header of the view.');
  $this
    ->assertStringContainsString('full', (string) $result[0], 'The rendered entity appeared in the right view mode.');
  $result = $this
    ->xpath($footer_xpath);
  $this
    ->assertStringContainsString($entities[1]
    ->label(), (string) $result[0], 'The rendered entity appears in the footer of the view.');
  $this
    ->assertStringContainsString('full', (string) $result[0], 'The rendered entity appeared in the right view mode.');

  // Mark entity_test test view_mode as customizable.
  $entity_view_mode = \Drupal::entityTypeManager()
    ->getStorage('entity_view_mode')
    ->load('entity_test.test');
  $entity_view_mode
    ->enable();
  $entity_view_mode
    ->save();

  // Change the view mode of the area handler.
  $view = Views::getView('test_entity_area');
  $item = $view
    ->getHandler('default', 'header', 'entity_entity_test');
  $item['view_mode'] = 'test';
  $view
    ->setHandler('default', 'header', 'entity_entity_test', $item);
  $preview = $view
    ->preview('default', [
    $entities[1]
      ->id(),
  ]);
  $this
    ->setRawContent($renderer
    ->renderRoot($preview));
  $view_class = 'js-view-dom-id-' . $view->dom_id;
  $result = $this
    ->xpath('//div[@class = "' . $view_class . '"]/header[1]');
  $this
    ->assertStringContainsString($entities[0]
    ->label(), (string) $result[0], 'The rendered entity appears in the header of the view.');
  $this
    ->assertStringContainsString('test', (string) $result[0], 'The rendered entity appeared in the right view mode.');

  // Test entity access.
  $view = Views::getView('test_entity_area');
  $preview = $view
    ->preview('default', [
    $entities[2]
      ->id(),
  ]);
  $this
    ->setRawContent($renderer
    ->renderRoot($preview));
  $view_class = 'js-view-dom-id-' . $view->dom_id;
  $result = $this
    ->xpath('//div[@class = "' . $view_class . '"]/footer[1]');
  $this
    ->assertStringNotContainsString($entities[2]
    ->label(), $result[0], 'The rendered entity does not appear in the footer of the view.');

  // Test the available view mode options.
  $form = [];
  $form_state = (new FormState())
    ->set('type', 'header');
  $view->display_handler
    ->getHandler('header', 'entity_entity_test')
    ->buildOptionsForm($form, $form_state);
  $this
    ->assertTrue(isset($form['view_mode']['#options']['test']), 'Ensure that the test view mode is available.');
  $this
    ->assertTrue(isset($form['view_mode']['#options']['default']), 'Ensure that the default view mode is available.');
}