function BlockStorageUnitTest::createTests

Same name in other branches
  1. 8.9.x core/modules/block/tests/src/Kernel/BlockStorageUnitTest.php \Drupal\Tests\block\Kernel\BlockStorageUnitTest::createTests()
  2. 10 core/modules/block/tests/src/Kernel/BlockStorageUnitTest.php \Drupal\Tests\block\Kernel\BlockStorageUnitTest::createTests()
  3. 11.x core/modules/block/tests/src/Kernel/BlockStorageUnitTest.php \Drupal\Tests\block\Kernel\BlockStorageUnitTest::createTests()

Tests the creation of blocks.

1 call to BlockStorageUnitTest::createTests()
BlockStorageUnitTest::testBlockCRUD in core/modules/block/tests/src/Kernel/BlockStorageUnitTest.php
Tests CRUD operations.

File

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

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());
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.