BlockStorageUnitTest.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. <?php
  2. namespace Drupal\Tests\block\Kernel;
  3. use Drupal\Core\Block\BlockPluginInterface;
  4. use Drupal\Core\Config\Entity\ConfigEntityStorage;
  5. use Drupal\KernelTests\KernelTestBase;
  6. use Drupal\block_test\Plugin\Block\TestHtmlBlock;
  7. use Drupal\Component\Plugin\Exception\PluginException;
  8. use Drupal\block\Entity\Block;
  9. /**
  10. * Tests the storage of blocks.
  11. *
  12. * @group block
  13. */
  14. class BlockStorageUnitTest extends KernelTestBase {
  15. /**
  16. * Modules to install.
  17. *
  18. * @var array
  19. */
  20. public static $modules = ['block', 'block_test', 'system'];
  21. /**
  22. * The block storage.
  23. *
  24. * @var \Drupal\Core\Config\Entity\ConfigEntityStorageInterface
  25. */
  26. protected $controller;
  27. protected function setUp() {
  28. parent::setUp();
  29. $this->controller = $this->container->get('entity_type.manager')->getStorage('block');
  30. $this->container->get('theme_installer')->install(['stark']);
  31. }
  32. /**
  33. * Tests CRUD operations.
  34. */
  35. public function testBlockCRUD() {
  36. $this->assertTrue($this->controller instanceof ConfigEntityStorage, 'The block storage is loaded.');
  37. // Run each test method in the same installation.
  38. $this->createTests();
  39. $this->loadTests();
  40. $this->deleteTests();
  41. }
  42. /**
  43. * Tests the creation of blocks.
  44. */
  45. protected function createTests() {
  46. // Attempt to create a block without a plugin.
  47. try {
  48. $entity = $this->controller->create([]);
  49. $entity->getPlugin();
  50. $this->fail('A block without a plugin was created with no exception thrown.');
  51. }
  52. catch (PluginException $e) {
  53. $this->assertEqual('The block \'\' did not specify a plugin.', $e->getMessage(), 'An exception was thrown when a block was created without a plugin.');
  54. }
  55. // Create a block with only required values.
  56. $entity = $this->controller->create([
  57. 'id' => 'test_block',
  58. 'theme' => 'stark',
  59. 'region' => 'content',
  60. 'plugin' => 'test_html',
  61. ]);
  62. $entity->save();
  63. $this->assertTrue($entity instanceof Block, 'The newly created entity is a Block.');
  64. // Verify all of the block properties.
  65. $actual_properties = $this->config('block.block.test_block')->get();
  66. $this->assertTrue(!empty($actual_properties['uuid']), 'The block UUID is set.');
  67. unset($actual_properties['uuid']);
  68. // Ensure that default values are filled in.
  69. $expected_properties = [
  70. 'langcode' => \Drupal::languageManager()->getDefaultLanguage()->getId(),
  71. 'status' => TRUE,
  72. 'dependencies' => ['module' => ['block_test'], 'theme' => ['stark']],
  73. 'id' => 'test_block',
  74. 'theme' => 'stark',
  75. 'region' => 'content',
  76. 'weight' => NULL,
  77. 'provider' => NULL,
  78. 'plugin' => 'test_html',
  79. 'settings' => [
  80. 'id' => 'test_html',
  81. 'label' => '',
  82. 'provider' => 'block_test',
  83. 'label_display' => BlockPluginInterface::BLOCK_LABEL_VISIBLE,
  84. ],
  85. 'visibility' => [],
  86. ];
  87. $this->assertIdentical($actual_properties, $expected_properties);
  88. $this->assertTrue($entity->getPlugin() instanceof TestHtmlBlock, 'The entity has an instance of the correct block plugin.');
  89. }
  90. /**
  91. * Tests the loading of blocks.
  92. */
  93. protected function loadTests() {
  94. $entity = $this->controller->load('test_block');
  95. $this->assertTrue($entity instanceof Block, 'The loaded entity is a Block.');
  96. // Verify several properties of the block.
  97. $this->assertSame('content', $entity->getRegion());
  98. $this->assertTrue($entity->status());
  99. $this->assertEqual($entity->getTheme(), 'stark');
  100. $this->assertTrue($entity->uuid());
  101. }
  102. /**
  103. * Tests the deleting of blocks.
  104. */
  105. protected function deleteTests() {
  106. $entity = $this->controller->load('test_block');
  107. // Ensure that the storage isn't currently empty.
  108. $config_storage = $this->container->get('config.storage');
  109. $config = $config_storage->listAll('block.block.');
  110. $this->assertFalse(empty($config), 'There are blocks in config storage.');
  111. // Delete the block.
  112. $entity->delete();
  113. // Ensure that the storage is now empty.
  114. $config = $config_storage->listAll('block.block.');
  115. $this->assertTrue(empty($config), 'There are no blocks in config storage.');
  116. }
  117. /**
  118. * Tests the installation of default blocks.
  119. */
  120. public function testDefaultBlocks() {
  121. \Drupal::service('theme_handler')->install(['classy']);
  122. $entities = $this->controller->loadMultiple();
  123. $this->assertTrue(empty($entities), 'There are no blocks initially.');
  124. // Install the block_test.module, so that its default config is installed.
  125. $this->installConfig(['block_test']);
  126. $entities = $this->controller->loadMultiple();
  127. $entity = reset($entities);
  128. $this->assertEqual($entity->id(), 'test_block', 'The default test block was loaded.');
  129. }
  130. }