BlockStorageUnitTest.php 4.7 KB
Newer Older
1 2
<?php

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 23
   *
   * @var array
   */
24
  public static $modules = ['block', 'block_test', 'system'];
25 26

  /**
27
   * The block storage.
28
   *
29
   * @var \Drupal\Core\Config\Entity\ConfigEntityStorageInterface
30 31 32 33 34 35
   */
  protected $controller;

  protected function setUp() {
    parent::setUp();

36
    $this->controller = $this->container->get('entity_type.manager')->getStorage('block');
37 38

    $this->container->get('theme_installer')->install(['stark']);
39 40 41 42 43 44
  }

  /**
   * Tests CRUD operations.
   */
  public function testBlockCRUD() {
45
    $this->assertTrue($this->controller instanceof ConfigEntityStorage, 'The block storage is loaded.');
46 47 48 49 50 51 52 53 54 55 56 57 58

    // Run each test method in the same installation.
    $this->createTests();
    $this->loadTests();
    $this->deleteTests();
  }

  /**
   * Tests the creation of blocks.
   */
  protected function createTests() {
    // Attempt to create a block without a plugin.
    try {
59
      $entity = $this->controller->create([]);
60
      $entity->getPlugin();
61 62 63 64 65 66 67
      $this->fail('A block without a plugin was created with no exception thrown.');
    }
    catch (PluginException $e) {
      $this->assertEqual('The block \'\' did not specify a plugin.', $e->getMessage(), 'An exception was thrown when a block was created without a plugin.');
    }

    // Create a block with only required values.
68
    $entity = $this->controller->create([
69 70
      'id' => 'test_block',
      'theme' => 'stark',
71
      'region' => 'content',
72
      'plugin' => 'test_html',
73
    ]);
74 75 76 77 78
    $entity->save();

    $this->assertTrue($entity instanceof Block, 'The newly created entity is a Block.');

    // Verify all of the block properties.
79
    $actual_properties = $this->config('block.block.test_block')->get();
80 81 82 83
    $this->assertTrue(!empty($actual_properties['uuid']), 'The block UUID is set.');
    unset($actual_properties['uuid']);

    // 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 92
      'weight' => NULL,
      '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 101
      ],
      'visibility' => [],
    ];
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 115 116

    $this->assertTrue($entity instanceof Block, 'The loaded entity is a Block.');

    // Verify several properties of the block.
117
    $this->assertSame('content', $entity->getRegion());
118 119
    $this->assertTrue($entity->status());
    $this->assertEqual($entity->getTheme(), 'stark');
120 121 122 123 124 125 126
    $this->assertTrue($entity->uuid());
  }

  /**
   * Tests the deleting of blocks.
   */
  protected function deleteTests() {
127
    $entity = $this->controller->load('test_block');
128 129 130

    // Ensure that the storage isn't currently empty.
    $config_storage = $this->container->get('config.storage');
131
    $config = $config_storage->listAll('block.block.');
132 133 134 135 136 137
    $this->assertFalse(empty($config), 'There are blocks in config storage.');

    // Delete the block.
    $entity->delete();

    // Ensure that the storage is now empty.
138
    $config = $config_storage->listAll('block.block.');
139 140 141 142 143 144 145
    $this->assertTrue(empty($config), 'There are no blocks in config storage.');
  }

  /**
   * Tests the installation of default blocks.
   */
  public function testDefaultBlocks() {
146
    \Drupal::service('theme_handler')->install(['classy']);
147
    $entities = $this->controller->loadMultiple();
148 149 150
    $this->assertTrue(empty($entities), 'There are no blocks initially.');

    // 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
  }

}