You are here

public function DisplayBlockTest::testBlockEmptyRendering in Zircon Profile 8

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

Tests the various testcases of empty block rendering.

File

core/modules/block/src/Tests/Views/DisplayBlockTest.php, line 274
Contains \Drupal\block\Tests\Views\DisplayBlockTest.

Class

DisplayBlockTest
Tests the block display plugin.

Namespace

Drupal\block\Tests\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', array(
    'label' => 'test_view_block-block_1:1',
    'views_label' => 'Custom title',
  ));
  $this
    ->drupalGet('');
  $this
    ->assertEqual(1, count($this
    ->xpath('//div[contains(@class, "block-views-blocktest-view-block-block-1")]')));
  $display =& $view
    ->getDisplay('block_1');
  $display['display_options']['block_hide_empty'] = TRUE;
  $view
    ->save();
  $this
    ->drupalGet($url);
  $this
    ->assertEqual(0, count($this
    ->xpath('//div[contains(@class, "block-views-blocktest-view-block-block-1")]')));

  // Ensure that the view cachability 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',
    '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
    ->assertEqual(1, count($this
    ->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',
    '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
    ->assertEqual(0, count($this
    ->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',
    '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
    ->assertEqual(1, count($this
    ->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',
    'rendered',
  ]));
  $this
    ->assertCacheContexts([
    'url.query_args:_wrapper_format',
  ]);
}