class BlockBaseTest

Same name in this branch
  1. 11.x core/tests/Drupal/Tests/Core/Block/BlockBaseTest.php \Drupal\Tests\Core\Block\BlockBaseTest
Same name in other branches
  1. 9 core/tests/Drupal/Tests/Core/Block/BlockBaseTest.php \Drupal\Tests\Core\Block\BlockBaseTest
  2. 8.9.x core/tests/Drupal/Tests/Core/Block/BlockBaseTest.php \Drupal\Tests\Core\Block\BlockBaseTest
  3. 10 core/tests/Drupal/Tests/Core/Block/BlockBaseTest.php \Drupal\Tests\Core\Block\BlockBaseTest

Tests the BlockBase class, base for all block plugins.

@group block

Hierarchy

Expanded class hierarchy of BlockBaseTest

File

core/tests/Drupal/KernelTests/Core/Block/BlockBaseTest.php, line 15

Namespace

Drupal\KernelTests\Core\Block
View source
class BlockBaseTest extends KernelTestBase {
    
    /**
     * {@inheritdoc}
     */
    protected static $modules = [
        'system',
        'block',
        'block_test',
    ];
    
    /**
     * Tests that blocks config form have context mapping, and it is stored in configuration.
     */
    public function testContextMapping() : void {
        $configuration = [
            'label' => 'A very cool block',
        ];
        
        /** @var \Drupal\Core\Block\BlockManagerInterface $blockManager */
        $blockManager = \Drupal::service('plugin.manager.block');
        
        /** @var \Drupal\Core\Block\BlockBase $block */
        $block = $blockManager->createInstance('test_block_instantiation', $configuration);
        // Check that context mapping is present in the block config form.
        $form = [];
        $form_state = new FormState();
        $form = $block->buildConfigurationForm($form, $form_state);
        $this->assertArrayHasKey('context_mapping', $form);
        // Check that context mapping is stored in block's configuration.
        $context_mapping = [
            'user' => 'current_user',
        ];
        $form_state->setValue('context_mapping', $context_mapping);
        $block->submitConfigurationForm($form, $form_state);
        $this->assertEquals($context_mapping, $block->getConfiguration()['context_mapping'] ?? NULL);
    }

}

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