You are here

public function DisplayBlockTest::testBlockEmptyRendering in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/block/tests/src/Functional/Views/DisplayBlockTest.php \Drupal\Tests\block\Functional\Views\DisplayBlockTest::testBlockEmptyRendering()

Tests the various testcases of empty block rendering.

File

core/modules/block/tests/src/Functional/Views/DisplayBlockTest.php, line 293

Class

DisplayBlockTest
Tests the block display plugin.

Namespace

Drupal\Tests\block\Functional\Views

Code

public function testBlockEmptyRendering() {
  $url = new Url('test_page_test.test_page');

  // Remove all views_test_data entries.
  \Drupal::database()
    ->truncate('views_test_data')
    ->execute();

  /** @var \Drupal\views\ViewEntityInterface $view */
  $view = View::load('test_view_block');
  $view
    ->invalidateCaches();
  $block = $this
    ->drupalPlaceBlock('views_block:test_view_block-block_1', [
    'label' => 'test_view_block-block_1:1',
    'views_label' => 'Custom title',
  ]);
  $this
    ->drupalGet('');
  $this
    ->assertSession()
    ->elementsCount('xpath', '//div[contains(@class, "block-views-blocktest-view-block-block-1")]', 1);
  $display =& $view
    ->getDisplay('block_1');
  $display['display_options']['block_hide_empty'] = TRUE;
  $view
    ->save();
  $this
    ->drupalGet($url);
  $this
    ->assertSession()
    ->elementNotExists('xpath', '//div[contains(@class, "block-views-blocktest-view-block-block-1")]');

  // Ensure that the view cacheability metadata is propagated even, for an
  // empty block.
  $this
    ->assertCacheTags(array_merge($block
    ->getCacheTags(), [
    'block_view',
    'config:block_list',
    'config:views.view.test_view_block',
    'http_response',
    'rendered',
  ]));
  $this
    ->assertCacheContexts([
    'url.query_args:_wrapper_format',
  ]);

  // Add a header displayed on empty result.
  $display =& $view
    ->getDisplay('block_1');
  $display['display_options']['defaults']['header'] = FALSE;
  $display['display_options']['header']['example'] = [
    'field' => 'area_text_custom',
    'id' => 'area_text_custom',
    'table' => 'views',
    'plugin_id' => 'text_custom',
    'content' => 'test header',
    'empty' => TRUE,
  ];
  $view
    ->save();
  $this
    ->drupalGet($url);
  $this
    ->assertSession()
    ->elementsCount('xpath', '//div[contains(@class, "block-views-blocktest-view-block-block-1")]', 1);
  $this
    ->assertCacheTags(array_merge($block
    ->getCacheTags(), [
    'block_view',
    'config:block_list',
    'config:views.view.test_view_block',
    'http_response',
    'rendered',
  ]));
  $this
    ->assertCacheContexts([
    'url.query_args:_wrapper_format',
  ]);

  // Hide the header on empty results.
  $display =& $view
    ->getDisplay('block_1');
  $display['display_options']['defaults']['header'] = FALSE;
  $display['display_options']['header']['example'] = [
    'field' => 'area_text_custom',
    'id' => 'area_text_custom',
    'table' => 'views',
    'plugin_id' => 'text_custom',
    'content' => 'test header',
    'empty' => FALSE,
  ];
  $view
    ->save();
  $this
    ->drupalGet($url);
  $this
    ->assertSession()
    ->elementNotExists('xpath', '//div[contains(@class, "block-views-blocktest-view-block-block-1")]');
  $this
    ->assertCacheTags(array_merge($block
    ->getCacheTags(), [
    'block_view',
    'config:block_list',
    'config:views.view.test_view_block',
    'http_response',
    'rendered',
  ]));
  $this
    ->assertCacheContexts([
    'url.query_args:_wrapper_format',
  ]);

  // Add an empty text.
  $display =& $view
    ->getDisplay('block_1');
  $display['display_options']['defaults']['empty'] = FALSE;
  $display['display_options']['empty']['example'] = [
    'field' => 'area_text_custom',
    'id' => 'area_text_custom',
    'table' => 'views',
    'plugin_id' => 'text_custom',
    'content' => 'test empty',
  ];
  $view
    ->save();
  $this
    ->drupalGet($url);
  $this
    ->assertSession()
    ->elementsCount('xpath', '//div[contains(@class, "block-views-blocktest-view-block-block-1")]', 1);
  $this
    ->assertCacheTags(array_merge($block
    ->getCacheTags(), [
    'block_view',
    'config:block_list',
    'config:views.view.test_view_block',
    'http_response',
    'rendered',
  ]));
  $this
    ->assertCacheContexts([
    'url.query_args:_wrapper_format',
  ]);
}