You are here

public function AreaEmptyTest::testRenderEmptyHeaderFooter in Drupal 9

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

Tests that the header and footer areas are not rendered if empty.

File

core/modules/views/tests/src/Kernel/Handler/AreaEmptyTest.php, line 55

Class

AreaEmptyTest
Tests the generic entity area handler.

Namespace

Drupal\Tests\views\Kernel\Handler

Code

public function testRenderEmptyHeaderFooter() {
  $view = Views::getView('test_example_area');
  $view
    ->initHandlers();

  // Set example empty text.
  $empty_text = $this
    ->randomMachineName();
  $empty_header = $this
    ->randomMachineName();
  $empty_footer = $this
    ->randomMachineName();

  // Set empty text.
  $view->empty['test_example']->options['string'] = '<p>' . $empty_text . '</p>';

  // Set example header text.
  $view->header['test_example']->options['string'] = '<p>' . $empty_header . '</p>';

  // Set example footer text.
  $view->footer['test_example']->options['string'] = '<p>' . $empty_footer . '</p>';

  // Verify that the empty header and footer sections have not been rendered.
  $view
    ->setDisplay('default');
  $this
    ->executeView($view);
  $output = $view
    ->render();
  $output = \Drupal::service('renderer')
    ->renderRoot($output);
  $this
    ->setRawContent($output);
  $this
    ->assertText($empty_text);
  $this
    ->assertNoText($empty_header);
  $this
    ->assertNoText($empty_footer);

  // Enable displaying the header and footer when the View is empty.
  $view->header['test_example']->options['empty'] = TRUE;
  $view->footer['test_example']->options['empty'] = TRUE;

  // Verify that the header and footer sections have been rendered.
  $this
    ->executeView($view);
  $output = $view
    ->render();
  $output = \Drupal::service('renderer')
    ->renderRoot($output);
  $this
    ->setRawContent($output);
  $this
    ->assertText($empty_header);
  $this
    ->assertText($empty_footer);
}