Further Drupal 8.6.4 changes. Some core files were not committed before a commit...
[yaffs-website] / web / core / modules / block / tests / src / Kernel / BlockStorageUnitTest.php
1 <?php
2
3 namespace Drupal\Tests\block\Kernel;
4
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;
11
12 /**
13  * Tests the storage of blocks.
14  *
15  * @group block
16  */
17 class BlockStorageUnitTest extends KernelTestBase {
18
19   /**
20    * Modules to install.
21    *
22    * @var array
23    */
24   public static $modules = ['block', 'block_test', 'system'];
25
26   /**
27    * The block storage.
28    *
29    * @var \Drupal\Core\Config\Entity\ConfigEntityStorageInterface
30    */
31   protected $controller;
32
33   protected function setUp() {
34     parent::setUp();
35
36     $this->controller = $this->container->get('entity_type.manager')->getStorage('block');
37
38     $this->container->get('theme_installer')->install(['stark']);
39   }
40
41   /**
42    * Tests CRUD operations.
43    */
44   public function testBlockCRUD() {
45     $this->assertTrue($this->controller instanceof ConfigEntityStorage, 'The block storage is loaded.');
46
47     // Run each test method in the same installation.
48     $this->createTests();
49     $this->loadTests();
50     $this->deleteTests();
51   }
52
53   /**
54    * Tests the creation of blocks.
55    */
56   protected function createTests() {
57     // Attempt to create a block without a plugin.
58     try {
59       $entity = $this->controller->create([]);
60       $entity->getPlugin();
61       $this->fail('A block without a plugin was created with no exception thrown.');
62     }
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.');
65     }
66
67     // Create a block with only required values.
68     $entity = $this->controller->create([
69       'id' => 'test_block',
70       'theme' => 'stark',
71       'region' => 'content',
72       'plugin' => 'test_html',
73     ]);
74     $entity->save();
75
76     $this->assertTrue($entity instanceof Block, 'The newly created entity is a Block.');
77
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']);
82
83     // Ensure that default values are filled in.
84     $expected_properties = [
85       'langcode' => \Drupal::languageManager()->getDefaultLanguage()->getId(),
86       'status' => TRUE,
87       'dependencies' => ['module' => ['block_test'], 'theme' => ['stark']],
88       'id' => 'test_block',
89       'theme' => 'stark',
90       'region' => 'content',
91       'weight' => NULL,
92       'provider' => NULL,
93       'plugin' => 'test_html',
94       'settings' => [
95         'id' => 'test_html',
96         'label' => '',
97         'provider' => 'block_test',
98         'label_display' => BlockPluginInterface::BLOCK_LABEL_VISIBLE,
99       ],
100       'visibility' => [],
101     ];
102
103     $this->assertIdentical($actual_properties, $expected_properties);
104
105     $this->assertTrue($entity->getPlugin() instanceof TestHtmlBlock, 'The entity has an instance of the correct block plugin.');
106   }
107
108   /**
109    * Tests the loading of blocks.
110    */
111   protected function loadTests() {
112     $entity = $this->controller->load('test_block');
113
114     $this->assertTrue($entity instanceof Block, 'The loaded entity is a Block.');
115
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());
121   }
122
123   /**
124    * Tests the deleting of blocks.
125    */
126   protected function deleteTests() {
127     $entity = $this->controller->load('test_block');
128
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.');
133
134     // Delete the block.
135     $entity->delete();
136
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.');
140   }
141
142   /**
143    * Tests the installation of default blocks.
144    */
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.');
149
150     // Install the block_test.module, so that its default config is installed.
151     $this->installConfig(['block_test']);
152
153     $entities = $this->controller->loadMultiple();
154     $entity = reset($entities);
155     $this->assertEqual($entity->id(), 'test_block', 'The default test block was loaded.');
156   }
157
158 }