LayoutBuilderInlineBlockStubCleanupTest.php in Acquia Content Hub 8.2
File
tests/src/Kernel/EventSubscriber/CleanUpStubs/LayoutBuilderInlineBlockStubCleanupTest.php
View source
<?php
namespace Drupal\Tests\acquia_contenthub\Kernel\EventSubscriber\CleanUpStubs;
use Drupal\acquia_contenthub\AcquiaContentHubEvents;
use Drupal\acquia_contenthub\Event\CleanUpStubsEvent;
use Drupal\block_content\Entity\BlockContent;
use Drupal\depcalc\DependencyStack;
use Drupal\KernelTests\KernelTestBase;
class LayoutBuilderInlineBlockStubCleanupTest extends KernelTestBase {
public static $modules = [
'acquia_contenthub',
'depcalc',
'block',
'block_content',
'layout_builder',
'user',
'field',
];
protected $dispatcher;
protected function setUp() {
parent::setUp();
$this
->installEntitySchema('block_content');
$this
->installEntitySchema('field_config');
$this
->installSchema('layout_builder', 'inline_block_usage');
$this->dispatcher = $this->container
->get('event_dispatcher');
}
public function testLayoutBuilderInlineBlockStubCleanup() {
$block_content = $this
->createEntity();
$this
->createEntity();
$event = new CleanUpStubsEvent($block_content, new DependencyStack());
$this->dispatcher
->dispatch(AcquiaContentHubEvents::CLEANUP_STUBS, $event);
$this
->assertFalse($event
->isPropagationStopped());
$this
->addInlineBlockUsage($block_content
->id());
$event = new CleanUpStubsEvent($block_content, new DependencyStack());
$this->dispatcher
->dispatch(AcquiaContentHubEvents::CLEANUP_STUBS, $event);
$this
->assertTrue($event
->isPropagationStopped());
$blocks = \Drupal::entityTypeManager()
->getStorage('block_content')
->loadByProperties([
'info' => $block_content
->get('info')->value,
]);
$this
->assertEqual(count($blocks), 1, 'Only 1 block should be after subscriber run');
}
protected function addInlineBlockUsage($block_id) {
\Drupal::database()
->insert('inline_block_usage')
->fields([
'block_content_id' => $block_id,
'layout_entity_type' => 'node',
'layout_entity_id' => 1,
])
->execute();
}
protected function createEntity() {
$block_content = BlockContent::create([
'info' => 'Test',
'type' => 'basic',
'body' => [
'value' => 'Test.',
'format' => 'plain_text',
],
]);
$block_content
->save();
return $block_content;
}
}