3 namespace Drupal\Tests\Core\Plugin\Context;
5 use Drupal\Component\Plugin\Context\ContextInterface as ComponentContextInterface;
6 use Drupal\Component\Plugin\Definition\ContextAwarePluginDefinitionInterface;
7 use Drupal\Component\Plugin\Definition\ContextAwarePluginDefinitionTrait;
8 use Drupal\Component\Plugin\Definition\PluginDefinition;
9 use Drupal\Component\Plugin\Exception\ContextException;
10 use Drupal\Core\DependencyInjection\ContainerBuilder;
11 use Drupal\Core\Plugin\Context\ContextDefinition;
12 use Drupal\Core\Plugin\ContextAwarePluginBase;
13 use Drupal\Core\TypedData\DataDefinition;
14 use Drupal\Core\TypedData\Plugin\DataType\StringData;
15 use Drupal\Core\TypedData\TypedDataManagerInterface;
16 use Drupal\Tests\UnitTestCase;
19 * @coversDefaultClass \Drupal\Core\Plugin\ContextAwarePluginBase
22 class ContextAwarePluginBaseTest extends UnitTestCase {
25 * The plugin instance under test.
27 * @var \Drupal\Core\Plugin\ContextAwarePluginBase
34 public function setUp() {
36 $this->plugin = new TestContextAwarePlugin([], 'the_sisko', new TestPluginDefinition());
40 * @covers ::getContextDefinitions
42 public function testGetContextDefinitions() {
43 $this->assertInternalType('array', $this->plugin->getContextDefinitions());
47 * @covers ::getContextDefinition
49 public function testGetContextDefinition() {
50 // The context is not defined, so an exception will be thrown.
51 $this->setExpectedException(ContextException::class, 'The person context is not a valid context.');
52 $this->plugin->getContextDefinition('person');
56 * @covers ::setContextValue
58 public function testSetContextValue() {
59 $typed_data_manager = $this->prophesize(TypedDataManagerInterface::class);
60 $container = new ContainerBuilder();
61 $container->set('typed_data_manager', $typed_data_manager->reveal());
62 \Drupal::setContainer($container);
64 $this->plugin->getPluginDefinition()->addContextDefinition('foo', new ContextDefinition('string'));
66 $this->assertFalse($this->plugin->setContextCalled);
67 $this->plugin->setContextValue('foo', new StringData(new DataDefinition(), 'bar'));
68 $this->assertTrue($this->plugin->setContextCalled);
73 class TestPluginDefinition extends PluginDefinition implements ContextAwarePluginDefinitionInterface {
75 use ContextAwarePluginDefinitionTrait;
79 class TestContextAwarePlugin extends ContextAwarePluginBase {
82 * Indicates if ::setContext() has been called or not.
86 public $setContextCalled = FALSE;
91 public function setContext($name, ComponentContextInterface $context) {
92 parent::setContext($name, $context);
93 $this->setContextCalled = TRUE;