class ContextAwarePluginTraitTest
Same name and namespace in other branches
- 11.x core/tests/Drupal/KernelTests/Core/Plugin/Context/ContextAwarePluginTraitTest.php \Drupal\KernelTests\Core\Plugin\Context\ContextAwarePluginTraitTest
- 10 core/tests/Drupal/KernelTests/Core/Plugin/Context/ContextAwarePluginTraitTest.php \Drupal\KernelTests\Core\Plugin\Context\ContextAwarePluginTraitTest
@coversDefaultClass \Drupal\Core\Plugin\ContextAwarePluginTrait
@group Plugin
Hierarchy
- class \Drupal\KernelTests\KernelTestBase implements \Drupal\Core\DependencyInjection\ServiceProviderInterface uses \Drupal\KernelTests\AssertLegacyTrait, \Drupal\KernelTests\AssertContentTrait, \Drupal\Tests\RandomGeneratorTrait, \Drupal\Tests\ConfigTestTrait, \Drupal\Tests\ExtensionListTestTrait, \Drupal\Tests\TestRequirementsTrait, \Drupal\Tests\Traits\PhpUnitWarnings, \Drupal\Tests\PhpUnitCompatibilityTrait, \Symfony\Bridge\PhpUnit\ExpectDeprecationTrait extends \PHPUnit\Framework\TestCase
- class \Drupal\KernelTests\Core\Plugin\Context\ContextAwarePluginTraitTest uses \Drupal\Tests\Traits\ExpectDeprecationTrait extends \Drupal\KernelTests\KernelTestBase
Expanded class hierarchy of ContextAwarePluginTraitTest
File
-
core/
tests/ Drupal/ KernelTests/ Core/ Plugin/ Context/ ContextAwarePluginTraitTest.php, line 27
Namespace
Drupal\KernelTests\Core\Plugin\ContextView source
class ContextAwarePluginTraitTest extends KernelTestBase {
use ExpectDeprecationTrait;
/**
* The plugin instance under test.
*
* @var \Drupal\Core\Plugin\ContextAwarePluginTrait
*/
private $plugin;
/**
* The configurable plugin instance under test.
*
* @var \Drupal\Core\Plugin\ContextAwarePluginTrait
*/
private $configurablePlugin;
/**
* {@inheritdoc}
*/
public function setUp() : void {
parent::setUp();
$plugin_definition = new TestContextAwarePluginDefinition();
$plugin_definition->addContextDefinition('nato_letter', ContextDefinition::create('string'));
$this->plugin = new TestContextAwarePlugin([], 'the_sisko', $plugin_definition);
$this->configurablePlugin = new TestConfigurableContextAwarePlugin([], 'the_sisko', $plugin_definition);
}
/**
* @covers ::getContextDefinitions
*/
public function testGetContextDefinitions() {
$this->assertIsArray($this->plugin
->getContextDefinitions());
}
/**
* @covers ::getContextDefinition
*/
public function testGetContextDefinition() {
// The context is not defined, so an exception will be thrown.
$this->expectException(ContextException::class);
$this->expectExceptionMessage('The person context is not a valid context.');
$this->plugin
->getContextDefinition('person');
}
/**
* @covers ::getContextValue
*/
public function testGetContextValue() {
$this->plugin
->setContextValue('nato_letter', 'Alpha');
$this->assertSame('Alpha', $this->plugin
->getContextValue('nato_letter'));
}
/**
* @covers ::getContextValue
* @group legacy
*/
public function testGetContextValueFromConfiguration() {
$this->expectDeprecation('Passing context values to plugins via configuration is deprecated in drupal:9.1.0 and will be removed before drupal:10.0.0. Instead, call ::setContextValue() on the plugin itself. See https://www.drupal.org/node/3120980');
$configuration = [
'context' => [
'nato_letter' => 'Alpha',
],
];
$this->plugin = new TestContextAwarePlugin($configuration, 'the_sisko', $this->plugin
->getPluginDefinition());
// Assert that the context value passed in the plugin configuration is
// available.
$this->assertSame('Alpha', $this->plugin
->getContextValue('nato_letter'));
}
/**
* @covers ::getContextValue
* @group legacy
*/
public function testConfigurableGetContextValueFromConfiguration() {
$this->expectDeprecation('Passing context values to plugins via configuration is deprecated in drupal:9.1.0 and will be removed before drupal:10.0.0. Instead, call ::setContextValue() on the plugin itself. See https://www.drupal.org/node/3120980');
// Assert that the context value passed in the plugin configuration is
// available.
$this->assertSame('Alpha', $this->configurablePlugin
->getContextValue('nato_letter'));
}
/**
* @covers ::setContextValue
*/
public function testSetContextValue() {
$typed_data_manager = $this->prophesize(TypedDataManagerInterface::class);
$container = new ContainerBuilder();
$container->set('typed_data_manager', $typed_data_manager->reveal());
\Drupal::setContainer($container);
$this->plugin
->getPluginDefinition()
->addContextDefinition('foo', new ContextDefinition('string'));
$this->assertFalse($this->plugin->setContextCalled);
$this->plugin
->setContextValue('foo', new StringData(new DataDefinition(), 'bar'));
$this->assertTrue($this->plugin->setContextCalled);
}
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.