function DisplayBlockTest::testBlockEmptyRendering

Same name and namespace in other branches
  1. 9 core/modules/block/tests/src/Functional/Views/DisplayBlockTest.php \Drupal\Tests\block\Functional\Views\DisplayBlockTest::testBlockEmptyRendering()
  2. 8.9.x core/modules/block/tests/src/Functional/Views/DisplayBlockTest.php \Drupal\Tests\block\Functional\Views\DisplayBlockTest::testBlockEmptyRendering()
  3. 10 core/modules/block/tests/src/Functional/Views/DisplayBlockTest.php \Drupal\Tests\block\Functional\Views\DisplayBlockTest::testBlockEmptyRendering()

Tests the various test cases of empty block rendering.

File

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

Class

DisplayBlockTest
Tests the block display plugin.

Namespace

Drupal\Tests\block\Functional\Views

Code

public function testBlockEmptyRendering() : void {
    $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',
    ]);
    $block_xpath = $this->assertSession()
        ->buildXPathQuery('//div[@id = :id]', [
        ':id' => 'block-' . $block->id(),
    ]);
    $this->drupalGet('');
    $this->assertSession()
        ->elementsCount('xpath', $block_xpath, 1);
    $display =& $view->getDisplay('block_1');
    $display['display_options']['block_hide_empty'] = TRUE;
    $view->save();
    $this->drupalGet($url);
    $this->assertSession()
        ->elementNotExists('xpath', $block_xpath);
    // 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', $block_xpath, 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', $block_xpath);
    $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', $block_xpath, 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',
    ]);
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.