3 namespace Drupal\Tests\block\Kernel;
5 use Drupal\Core\Block\BlockPluginInterface;
6 use Drupal\Core\Config\Entity\ConfigEntityStorage;
7 use Drupal\KernelTests\KernelTestBase;
8 use Drupal\block_test\Plugin\Block\TestHtmlBlock;
9 use Drupal\Component\Plugin\Exception\PluginException;
10 use Drupal\block\Entity\Block;
13 * Tests the storage of blocks.
17 class BlockStorageUnitTest extends KernelTestBase {
24 public static $modules = ['block', 'block_test', 'system'];
29 * @var \Drupal\Core\Config\Entity\ConfigEntityStorageInterface
31 protected $controller;
33 protected function setUp() {
36 $this->controller = $this->container->get('entity_type.manager')->getStorage('block');
38 $this->container->get('theme_installer')->install(['stark']);
42 * Tests CRUD operations.
44 public function testBlockCRUD() {
45 $this->assertTrue($this->controller instanceof ConfigEntityStorage, 'The block storage is loaded.');
47 // Run each test method in the same installation.
54 * Tests the creation of blocks.
56 protected function createTests() {
57 // Attempt to create a block without a plugin.
59 $entity = $this->controller->create([]);
61 $this->fail('A block without a plugin was created with no exception thrown.');
63 catch (PluginException $e) {
64 $this->assertEqual('The block \'\' did not specify a plugin.', $e->getMessage(), 'An exception was thrown when a block was created without a plugin.');
67 // Create a block with only required values.
68 $entity = $this->controller->create([
71 'region' => 'content',
72 'plugin' => 'test_html',
76 $this->assertTrue($entity instanceof Block, 'The newly created entity is a Block.');
78 // Verify all of the block properties.
79 $actual_properties = $this->config('block.block.test_block')->get();
80 $this->assertTrue(!empty($actual_properties['uuid']), 'The block UUID is set.');
81 unset($actual_properties['uuid']);
83 // Ensure that default values are filled in.
84 $expected_properties = [
85 'langcode' => \Drupal::languageManager()->getDefaultLanguage()->getId(),
87 'dependencies' => ['module' => ['block_test'], 'theme' => ['stark']],
90 'region' => 'content',
93 'plugin' => 'test_html',
97 'provider' => 'block_test',
98 'label_display' => BlockPluginInterface::BLOCK_LABEL_VISIBLE,
103 $this->assertIdentical($actual_properties, $expected_properties);
105 $this->assertTrue($entity->getPlugin() instanceof TestHtmlBlock, 'The entity has an instance of the correct block plugin.');
109 * Tests the loading of blocks.
111 protected function loadTests() {
112 $entity = $this->controller->load('test_block');
114 $this->assertTrue($entity instanceof Block, 'The loaded entity is a Block.');
116 // Verify several properties of the block.
117 $this->assertSame('content', $entity->getRegion());
118 $this->assertTrue($entity->status());
119 $this->assertEqual($entity->getTheme(), 'stark');
120 $this->assertTrue($entity->uuid());
124 * Tests the deleting of blocks.
126 protected function deleteTests() {
127 $entity = $this->controller->load('test_block');
129 // Ensure that the storage isn't currently empty.
130 $config_storage = $this->container->get('config.storage');
131 $config = $config_storage->listAll('block.block.');
132 $this->assertFalse(empty($config), 'There are blocks in config storage.');
137 // Ensure that the storage is now empty.
138 $config = $config_storage->listAll('block.block.');
139 $this->assertTrue(empty($config), 'There are no blocks in config storage.');
143 * Tests the installation of default blocks.
145 public function testDefaultBlocks() {
146 \Drupal::service('theme_handler')->install(['classy']);
147 $entities = $this->controller->loadMultiple();
148 $this->assertTrue(empty($entities), 'There are no blocks initially.');
150 // Install the block_test.module, so that its default config is installed.
151 $this->installConfig(['block_test']);
153 $entities = $this->controller->loadMultiple();
154 $entity = reset($entities);
155 $this->assertEqual($entity->id(), 'test_block', 'The default test block was loaded.');