ContextAwarePluginTraitTest.php in Drupal 9
File
core/tests/Drupal/KernelTests/Core/Plugin/Context/ContextAwarePluginTraitTest.php
View source
<?php
namespace Drupal\KernelTests\Core\Plugin\Context;
use Drupal\Component\Plugin\ConfigurableInterface;
use Drupal\Component\Plugin\Context\ContextInterface as ComponentContextInterface;
use Drupal\Component\Plugin\Definition\ContextAwarePluginDefinitionInterface;
use Drupal\Component\Plugin\Definition\ContextAwarePluginDefinitionTrait;
use Drupal\Component\Plugin\Definition\PluginDefinition;
use Drupal\Component\Plugin\Exception\ContextException;
use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\Core\Plugin\Context\ContextDefinition;
use Drupal\Core\Plugin\ContextAwarePluginInterface;
use Drupal\Core\Plugin\ContextAwarePluginTrait;
use Drupal\Core\Plugin\PluginBase;
use Drupal\Core\TypedData\DataDefinition;
use Drupal\Core\TypedData\Plugin\DataType\StringData;
use Drupal\Core\TypedData\TypedDataManagerInterface;
use Drupal\KernelTests\KernelTestBase;
use Drupal\Tests\Traits\ExpectDeprecationTrait;
class ContextAwarePluginTraitTest extends KernelTestBase {
use ExpectDeprecationTrait;
private $plugin;
private $configurablePlugin;
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);
}
public function testGetContextDefinitions() {
$this
->assertIsArray($this->plugin
->getContextDefinitions());
}
public function testGetContextDefinition() {
$this
->expectException(ContextException::class);
$this
->expectExceptionMessage('The person context is not a valid context.');
$this->plugin
->getContextDefinition('person');
}
public function testGetContextValue() {
$this->plugin
->setContextValue('nato_letter', 'Alpha');
$this
->assertSame('Alpha', $this->plugin
->getContextValue('nato_letter'));
}
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());
$this
->assertSame('Alpha', $this->plugin
->getContextValue('nato_letter'));
}
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');
$this
->assertSame('Alpha', $this->configurablePlugin
->getContextValue('nato_letter'));
}
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);
}
}
class TestContextAwarePluginDefinition extends PluginDefinition implements ContextAwarePluginDefinitionInterface {
use ContextAwarePluginDefinitionTrait;
}
class TestContextAwarePlugin extends PluginBase implements ContextAwarePluginInterface {
use ContextAwarePluginTrait {
setContext as setContextTrait;
}
public $setContextCalled = FALSE;
public function setContext($name, ComponentContextInterface $context) {
$this
->setContextTrait($name, $context);
$this->setContextCalled = TRUE;
}
}
class TestConfigurableContextAwarePlugin extends PluginBase implements ConfigurableInterface, ContextAwarePluginInterface {
use ContextAwarePluginTrait;
public function defaultConfiguration() {
return [];
}
public function getConfiguration() {
return [
'context' => [
'nato_letter' => 'Alpha',
],
];
}
public function setConfiguration(array $configuration) {
}
}