function BlockContentDeletionTest::testDeletingBlockContentShouldClearPluginCache

Same name in other branches
  1. 8.9.x core/modules/block_content/tests/src/Kernel/BlockContentDeletionTest.php \Drupal\Tests\block_content\Kernel\BlockContentDeletionTest::testDeletingBlockContentShouldClearPluginCache()
  2. 10 core/modules/block_content/tests/src/Kernel/BlockContentDeletionTest.php \Drupal\Tests\block_content\Kernel\BlockContentDeletionTest::testDeletingBlockContentShouldClearPluginCache()
  3. 11.x core/modules/block_content/tests/src/Kernel/BlockContentDeletionTest.php \Drupal\Tests\block_content\Kernel\BlockContentDeletionTest::testDeletingBlockContentShouldClearPluginCache()

Tests deleting a block_content updates the discovered block plugin.

File

core/modules/block_content/tests/src/Kernel/BlockContentDeletionTest.php, line 37

Class

BlockContentDeletionTest
Tests that deleting a block clears the cached definitions.

Namespace

Drupal\Tests\block_content\Kernel

Code

public function testDeletingBlockContentShouldClearPluginCache() {
    // Create a block content type.
    $block_content_type = BlockContentType::create([
        'id' => 'spiffy',
        'label' => 'Mucho spiffy',
        'description' => "Provides a block type that increases your site's spiffiness by upto 11%",
    ]);
    $block_content_type->save();
    // And a block content entity.
    $block_content = BlockContent::create([
        'info' => 'Spiffy prototype',
        'type' => 'spiffy',
    ]);
    $block_content->save();
    // Make sure the block content provides a derivative block plugin in the
    // block repository.
    
    /** @var \Drupal\Core\Block\BlockManagerInterface $block_manager */
    $block_manager = $this->container
        ->get('plugin.manager.block');
    $plugin_id = 'block_content' . PluginBase::DERIVATIVE_SEPARATOR . $block_content->uuid();
    $this->assertTrue($block_manager->hasDefinition($plugin_id));
    // Now delete the block content entity.
    $block_content->delete();
    // The plugin should no longer exist.
    $this->assertFalse($block_manager->hasDefinition($plugin_id));
    // Create another block content entity.
    $block_content = BlockContent::create([
        'info' => 'Spiffy prototype',
        'type' => 'spiffy',
    ]);
    $block_content->save();
    $plugin_id = 'block_content' . PluginBase::DERIVATIVE_SEPARATOR . $block_content->uuid();
    $block = $this->placeBlock($plugin_id, [
        'region' => 'content',
    ]);
    // Delete it via storage.
    $storage = $this->container
        ->get('entity_type.manager')
        ->getStorage('block_content');
    $storage->delete([
        $block_content,
    ]);
    // The plugin should no longer exist.
    $this->assertFalse($block_manager->hasDefinition($plugin_id));
    $this->assertNull($this->container
        ->get('entity_type.manager')
        ->getStorage('block')
        ->loadUnchanged($block->id()));
}

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