You are here

public function DisplayBlockTest::testDeleteBlockDisplay in Drupal 8

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

Tests removing a block display.

File

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

Class

DisplayBlockTest
Tests the block display plugin.

Namespace

Drupal\Tests\block\Functional\Views

Code

public function testDeleteBlockDisplay() {

  // To test all combinations possible we first place create two instances
  // of the block display of the first view.
  $block_1 = $this
    ->drupalPlaceBlock('views_block:test_view_block-block_1', [
    'label' => 'test_view_block-block_1:1',
  ]);
  $block_2 = $this
    ->drupalPlaceBlock('views_block:test_view_block-block_1', [
    'label' => 'test_view_block-block_1:2',
  ]);

  // Then we add one instance of blocks for each of the two displays of the
  // second view.
  $block_3 = $this
    ->drupalPlaceBlock('views_block:test_view_block2-block_1', [
    'label' => 'test_view_block2-block_1',
  ]);
  $block_4 = $this
    ->drupalPlaceBlock('views_block:test_view_block2-block_2', [
    'label' => 'test_view_block2-block_2',
  ]);
  $this
    ->drupalGet('test-page');
  $this
    ->assertBlockAppears($block_1);
  $this
    ->assertBlockAppears($block_2);
  $this
    ->assertBlockAppears($block_3);
  $this
    ->assertBlockAppears($block_4);
  $block_storage = $this->container
    ->get('entity_type.manager')
    ->getStorage('block');

  // Remove the block display, so both block entities from the first view
  // should both disappear.
  $view = Views::getView('test_view_block');
  $view
    ->initDisplay();
  $view->displayHandlers
    ->remove('block_1');
  $view->storage
    ->save();
  $this
    ->assertNull($block_storage
    ->load($block_1
    ->id()), 'The block for this display was removed.');
  $this
    ->assertNull($block_storage
    ->load($block_2
    ->id()), 'The block for this display was removed.');
  $this
    ->assertNotEmpty($block_storage
    ->load($block_3
    ->id()), 'A block from another view was unaffected.');
  $this
    ->assertNotEmpty($block_storage
    ->load($block_4
    ->id()), 'A block from another view was unaffected.');
  $this
    ->drupalGet('test-page');
  $this
    ->assertNoBlockAppears($block_1);
  $this
    ->assertNoBlockAppears($block_2);
  $this
    ->assertBlockAppears($block_3);
  $this
    ->assertBlockAppears($block_4);

  // Remove the first block display of the second view and ensure the block
  // instance of the second block display still exists.
  $view = Views::getView('test_view_block2');
  $view
    ->initDisplay();
  $view->displayHandlers
    ->remove('block_1');
  $view->storage
    ->save();
  $this
    ->assertNull($block_storage
    ->load($block_3
    ->id()), 'The block for this display was removed.');
  $this
    ->assertNotEmpty($block_storage
    ->load($block_4
    ->id()), 'A block from another display on the same view was unaffected.');
  $this
    ->drupalGet('test-page');
  $this
    ->assertNoBlockAppears($block_3);
  $this
    ->assertBlockAppears($block_4);
}