View source
<?php
namespace Drupal\Tests\block\Kernel;
use Drupal\Core\Block\BlockPluginInterface;
use Drupal\Core\Config\Entity\ConfigEntityStorage;
use Drupal\KernelTests\KernelTestBase;
use Drupal\block_test\Plugin\Block\TestHtmlBlock;
use Drupal\Component\Plugin\Exception\PluginException;
use Drupal\block\Entity\Block;
class BlockStorageUnitTest extends KernelTestBase {
protected static $modules = [
'block',
'block_test',
'system',
];
protected $controller;
protected function setUp() : void {
parent::setUp();
$this->controller = $this->container
->get('entity_type.manager')
->getStorage('block');
$this->container
->get('theme_installer')
->install([
'stark',
]);
}
public function testBlockCRUD() {
$this
->assertInstanceOf(ConfigEntityStorage::class, $this->controller);
$this
->createTests();
$this
->loadTests();
$this
->deleteTests();
}
protected function createTests() {
try {
$entity = $this->controller
->create([]);
$entity
->getPlugin();
$this
->fail('A block without a plugin was created with no exception thrown.');
} catch (PluginException $e) {
$this
->assertEquals('The block \'\' did not specify a plugin.', $e
->getMessage(), 'An exception was thrown when a block was created without a plugin.');
}
$entity = $this->controller
->create([
'id' => 'test_block',
'theme' => 'stark',
'region' => 'content',
'plugin' => 'test_html',
]);
$entity
->save();
$this
->assertInstanceOf(Block::class, $entity);
$actual_properties = $this
->config('block.block.test_block')
->get();
$this
->assertNotEmpty($actual_properties['uuid'], 'The block UUID is set.');
unset($actual_properties['uuid']);
$expected_properties = [
'langcode' => \Drupal::languageManager()
->getDefaultLanguage()
->getId(),
'status' => TRUE,
'dependencies' => [
'module' => [
'block_test',
],
'theme' => [
'stark',
],
],
'id' => 'test_block',
'theme' => 'stark',
'region' => 'content',
'weight' => NULL,
'provider' => NULL,
'plugin' => 'test_html',
'settings' => [
'id' => 'test_html',
'label' => '',
'label_display' => BlockPluginInterface::BLOCK_LABEL_VISIBLE,
'provider' => 'block_test',
],
'visibility' => [],
];
$this
->assertSame($expected_properties, $actual_properties);
$this
->assertInstanceOf(TestHtmlBlock::class, $entity
->getPlugin());
}
protected function loadTests() {
$entity = $this->controller
->load('test_block');
$this
->assertInstanceOf(Block::class, $entity);
$this
->assertSame('content', $entity
->getRegion());
$this
->assertTrue($entity
->status());
$this
->assertEquals('stark', $entity
->getTheme());
$this
->assertNotEmpty($entity
->uuid());
}
protected function deleteTests() {
$entity = $this->controller
->load('test_block');
$config_storage = $this->container
->get('config.storage');
$config = $config_storage
->listAll('block.block.');
$this
->assertNotEmpty($config, 'There are blocks in config storage.');
$entity
->delete();
$config = $config_storage
->listAll('block.block.');
$this
->assertEmpty($config, 'There are no blocks in config storage.');
}
public function testDefaultBlocks() {
\Drupal::service('theme_installer')
->install([
'stark',
]);
$entities = $this->controller
->loadMultiple();
$this
->assertEmpty($entities, 'There are no blocks initially.');
$this
->installConfig([
'block_test',
]);
$entities = $this->controller
->loadMultiple();
$entity = reset($entities);
$this
->assertEquals('test_block', $entity
->id(), 'The default test block was loaded.');
}
}