You are here

protected function BlockStorageUnitTest::createTests in Drupal 10

Same name and namespace in other branches
  1. 8 core/modules/block/tests/src/Kernel/BlockStorageUnitTest.php \Drupal\Tests\block\Kernel\BlockStorageUnitTest::createTests()
  2. 9 core/modules/block/tests/src/Kernel/BlockStorageUnitTest.php \Drupal\Tests\block\Kernel\BlockStorageUnitTest::createTests()

Tests the creation of blocks.

File

core/modules/block/tests/src/Kernel/BlockStorageUnitTest.php, line 56

Class

BlockStorageUnitTest
Tests the storage of blocks.

Namespace

Drupal\Tests\block\Kernel

Code

protected function createTests() {

  // Attempt to create a block without a plugin.
  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.');
  }

  // Create a block with only required values.
  $entity = $this->controller
    ->create([
    'id' => 'test_block',
    'theme' => 'stark',
    'region' => 'content',
    'plugin' => 'test_html',
  ]);
  $entity
    ->save();
  $this
    ->assertInstanceOf(Block::class, $entity);

  // Verify all of the block properties.
  $actual_properties = $this
    ->config('block.block.test_block')
    ->get();
  $this
    ->assertNotEmpty($actual_properties['uuid'], 'The block UUID is set.');
  unset($actual_properties['uuid']);

  // Ensure that default values are filled in.
  $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());
}