function InlineBlockTest::testDeletion

Same name and namespace in other branches
  1. 9 core/modules/layout_builder/tests/src/FunctionalJavascript/InlineBlockTest.php \Drupal\Tests\layout_builder\FunctionalJavascript\InlineBlockTest::testDeletion()
  2. 8.9.x core/modules/layout_builder/tests/src/FunctionalJavascript/InlineBlockTest.php \Drupal\Tests\layout_builder\FunctionalJavascript\InlineBlockTest::testDeletion()
  3. 11.x core/modules/layout_builder/tests/src/FunctionalJavascript/InlineBlockTest.php \Drupal\Tests\layout_builder\FunctionalJavascript\InlineBlockTest::testDeletion()

Tests that entity blocks deleted correctly.

File

core/modules/layout_builder/tests/src/FunctionalJavascript/InlineBlockTest.php, line 362

Class

InlineBlockTest
Tests that the inline block feature works correctly.

Namespace

Drupal\Tests\layout_builder\FunctionalJavascript

Code

public function testDeletion() : void {
  /** @var \Drupal\Core\Cron $cron */
  $cron = \Drupal::service('cron');
  /** @var \Drupal\layout_builder\InlineBlockUsageInterface $usage */
  $usage = \Drupal::service('inline_block.usage');
  $this->drupalLogin($this->drupalCreateUser([
    'administer content types',
    'access contextual links',
    'configure any layout',
    'administer node display',
    'administer node fields',
    'administer nodes',
    'bypass node access',
    'create and edit custom blocks',
  ]));
  $assert_session = $this->assertSession();
  $page = $this->getSession()
    ->getPage();
  // Enable layout builder.
  $this->drupalGet(static::FIELD_UI_PREFIX . '/display/default');
  $this->submitForm([
    'layout[enabled]' => TRUE,
  ], 'Save');
  // Add a block to default layout.
  $this->drupalGet(static::FIELD_UI_PREFIX . '/display/default');
  $this->clickLink('Manage layout');
  $assert_session->addressEquals(static::FIELD_UI_PREFIX . '/display/default/layout');
  $this->addInlineBlockToLayout('Block title', 'The DEFAULT block body');
  $this->assertSaveLayout();
  $this->assertCount(1, $this->blockStorage
    ->loadMultiple());
  $default_block_id = $this->getLatestBlockEntityId();
  // Ensure the block shows up on node pages.
  $this->drupalGet('node/1');
  $assert_session->pageTextContains('The DEFAULT block body');
  $this->drupalGet('node/2');
  $assert_session->pageTextContains('The DEFAULT block body');
  // Enable overrides.
  $this->drupalGet(static::FIELD_UI_PREFIX . '/display/default');
  $this->submitForm([
    'layout[allow_custom]' => TRUE,
  ], 'Save');
  // Ensure we have 2 copies of the block in node overrides.
  $this->drupalGet('node/1/layout');
  $this->assertSaveLayout();
  $node_1_block_id = $this->getLatestBlockEntityId();
  $this->drupalGet('node/2/layout');
  $this->assertSaveLayout();
  $node_2_block_id = $this->getLatestBlockEntityId();
  $this->assertCount(3, $this->blockStorage
    ->loadMultiple());
  $this->drupalGet(static::FIELD_UI_PREFIX . '/display/default');
  $this->clickLink('Manage layout');
  $assert_session->addressEquals(static::FIELD_UI_PREFIX . '/display/default/layout');
  $this->assertNotEmpty($this->blockStorage
    ->load($default_block_id));
  $this->assertNotEmpty($usage->getUsage($default_block_id));
  // Remove block from default.
  $this->removeInlineBlockFromLayout();
  $this->assertSaveLayout();
  // Ensure the block in the default was deleted.
  $this->blockStorage
    ->resetCache([
    $default_block_id,
  ]);
  $this->assertEmpty($this->blockStorage
    ->load($default_block_id));
  // Ensure other blocks still exist.
  $this->assertCount(2, $this->blockStorage
    ->loadMultiple());
  $this->assertEmpty($usage->getUsage($default_block_id));
  $this->drupalGet('node/1/layout');
  $assert_session->pageTextContains('The DEFAULT block body');
  $this->removeInlineBlockFromLayout();
  $this->assertSaveLayout();
  $cron->run();
  // Ensure entity block is not deleted because it is needed in revision.
  $this->assertNotEmpty($this->blockStorage
    ->load($node_1_block_id));
  $this->assertCount(2, $this->blockStorage
    ->loadMultiple());
  $this->assertNotEmpty($usage->getUsage($node_1_block_id));
  // Ensure entity block is deleted when node is deleted.
  $this->drupalGet('node/1/delete');
  $page->pressButton('Delete');
  $this->assertEmpty(Node::load(1));
  $cron->run();
  $this->assertEmpty($this->blockStorage
    ->load($node_1_block_id));
  $this->assertEmpty($usage->getUsage($node_1_block_id));
  $this->assertCount(1, $this->blockStorage
    ->loadMultiple());
  // Add another block to the default.
  $this->drupalGet(static::FIELD_UI_PREFIX . '/display/default');
  $this->clickLink('Manage layout');
  $assert_session->addressEquals(static::FIELD_UI_PREFIX . '/display/default/layout');
  $this->addInlineBlockToLayout('Title 2', 'Body 2');
  $this->assertSaveLayout();
  $cron->run();
  $default_block2_id = $this->getLatestBlockEntityId();
  $this->assertCount(2, $this->blockStorage
    ->loadMultiple());
  // Delete the other node so bundle can be deleted.
  $this->assertNotEmpty($usage->getUsage($node_2_block_id));
  $this->drupalGet('node/2/delete');
  $page->pressButton('Delete');
  $this->assertEmpty(Node::load(2));
  $cron->run();
  // Ensure entity block was deleted.
  $this->assertEmpty($this->blockStorage
    ->load($node_2_block_id));
  $this->assertEmpty($usage->getUsage($node_2_block_id));
  $this->assertCount(1, $this->blockStorage
    ->loadMultiple());
  // Delete the bundle which has the default layout.
  $this->assertNotEmpty($usage->getUsage($default_block2_id));
  $this->drupalGet(static::FIELD_UI_PREFIX . '/delete');
  $page->pressButton('Delete');
  $cron->run();
  // Ensure the entity block in default is deleted when bundle is deleted.
  $this->assertEmpty($this->blockStorage
    ->load($default_block2_id));
  $this->assertEmpty($usage->getUsage($default_block2_id));
  $this->assertCount(0, $this->blockStorage
    ->loadMultiple());
}

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