function BlockBaseTest::testContextMapping

Tests that blocks config form have context mapping, and it is stored in configuration.

File

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

Class

BlockBaseTest
Tests the BlockBase class, base for all block plugins.

Namespace

Drupal\KernelTests\Core\Block

Code

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.