You are here

public function DisplayTest::testOutputIsEmpty in Drupal 10

Same name and namespace in other branches
  1. 8 core/modules/views/tests/src/Functional/Plugin/DisplayTest.php \Drupal\Tests\views\Functional\Plugin\DisplayTest::testOutputIsEmpty()
  2. 9 core/modules/views/tests/src/Functional/Plugin/DisplayTest.php \Drupal\Tests\views\Functional\Plugin\DisplayTest::testOutputIsEmpty()

Tests the outputIsEmpty method on the display.

File

core/modules/views/tests/src/Functional/Plugin/DisplayTest.php, line 373

Class

DisplayTest
Tests the basic display plugin.

Namespace

Drupal\Tests\views\Functional\Plugin

Code

public function testOutputIsEmpty() {
  $view = Views::getView('test_display_empty');
  $this
    ->executeView($view);
  $this
    ->assertNotEmpty($view->result);
  $this
    ->assertFalse($view->display_handler
    ->outputIsEmpty(), 'Ensure the view output is marked as not empty.');
  $view
    ->destroy();

  // Add a filter, so the view result is empty.
  $view
    ->setDisplay('default');
  $item = [
    'table' => 'views_test_data',
    'field' => 'id',
    'id' => 'id',
    'value' => [
      'value' => 7297,
    ],
  ];
  $view
    ->setHandler('default', 'filter', 'id', $item);
  $this
    ->executeView($view);
  $this
    ->assertEmpty($view->result, 'Ensure the result of the view is empty.');
  $this
    ->assertFalse($view->display_handler
    ->outputIsEmpty(), 'Ensure the view output is marked as not empty, because the empty text still appears.');
  $view
    ->destroy();

  // Remove the empty area, but mark the header area to still appear.
  $view
    ->removeHandler('default', 'empty', 'area');
  $item = $view
    ->getHandler('default', 'header', 'area');
  $item['empty'] = TRUE;
  $view
    ->setHandler('default', 'header', 'area', $item);
  $this
    ->executeView($view);
  $this
    ->assertEmpty($view->result, 'Ensure the result of the view is empty.');
  $this
    ->assertFalse($view->display_handler
    ->outputIsEmpty(), 'Ensure the view output is marked as not empty, because the header text still appears.');
  $view
    ->destroy();

  // Hide the header on empty results.
  $item = $view
    ->getHandler('default', 'header', 'area');
  $item['empty'] = FALSE;
  $view
    ->setHandler('default', 'header', 'area', $item);
  $this
    ->executeView($view);
  $this
    ->assertEmpty($view->result, 'Ensure the result of the view is empty.');
  $this
    ->assertTrue($view->display_handler
    ->outputIsEmpty(), 'Ensure the view output is marked as empty.');
}