class BlockBaseTest
Same name in this branch
- 11.x core/tests/Drupal/Tests/Core/Block/BlockBaseTest.php \Drupal\Tests\Core\Block\BlockBaseTest
Same name in other branches
- 9 core/tests/Drupal/Tests/Core/Block/BlockBaseTest.php \Drupal\Tests\Core\Block\BlockBaseTest
- 8.9.x core/tests/Drupal/Tests/Core/Block/BlockBaseTest.php \Drupal\Tests\Core\Block\BlockBaseTest
- 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
- class \Drupal\KernelTests\KernelTestBase extends \PHPUnit\Framework\TestCase implements \Drupal\Core\DependencyInjection\ServiceProviderInterface uses \Drupal\KernelTests\AssertContentTrait, \Drupal\Tests\RandomGeneratorTrait, \Drupal\Tests\ConfigTestTrait, \Drupal\Tests\ExtensionListTestTrait, \Drupal\Tests\TestRequirementsTrait, \Drupal\Tests\PhpUnitCompatibilityTrait, \Prophecy\PhpUnit\ProphecyTrait, \Drupal\TestTools\Extension\DeprecationBridge\ExpectDeprecationTrait
- class \Drupal\KernelTests\Core\Block\BlockBaseTest extends \Drupal\KernelTests\KernelTestBase
Expanded class hierarchy of BlockBaseTest
File
-
core/
tests/ Drupal/ KernelTests/ Core/ Block/ BlockBaseTest.php, line 15
Namespace
Drupal\KernelTests\Core\BlockView 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.