Pull merge.
[yaffs-website] / web / core / tests / Drupal / Tests / Core / Plugin / Context / ContextAwarePluginBaseTest.php
1 <?php
2
3 namespace Drupal\Tests\Core\Plugin\Context;
4
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;
17
18 /**
19  * @coversDefaultClass \Drupal\Core\Plugin\ContextAwarePluginBase
20  * @group Plugin
21  */
22 class ContextAwarePluginBaseTest extends UnitTestCase {
23
24   /**
25    * The plugin instance under test.
26    *
27    * @var \Drupal\Core\Plugin\ContextAwarePluginBase
28    */
29   private $plugin;
30
31   /**
32    * {@inheritdoc}
33    */
34   public function setUp() {
35     parent::setUp();
36     $this->plugin = new TestContextAwarePlugin([], 'the_sisko', new TestPluginDefinition());
37   }
38
39   /**
40    * @covers ::getContextDefinitions
41    */
42   public function testGetContextDefinitions() {
43     $this->assertInternalType('array', $this->plugin->getContextDefinitions());
44   }
45
46   /**
47    * @covers ::getContextDefinition
48    */
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');
53   }
54
55   /**
56    * @covers ::setContextValue
57    */
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);
63
64     $this->plugin->getPluginDefinition()->addContextDefinition('foo', new ContextDefinition('string'));
65
66     $this->assertFalse($this->plugin->setContextCalled);
67     $this->plugin->setContextValue('foo', new StringData(new DataDefinition(), 'bar'));
68     $this->assertTrue($this->plugin->setContextCalled);
69   }
70
71 }
72
73 class TestPluginDefinition extends PluginDefinition implements ContextAwarePluginDefinitionInterface {
74
75   use ContextAwarePluginDefinitionTrait;
76
77 }
78
79 class TestContextAwarePlugin extends ContextAwarePluginBase {
80
81   /**
82    * Indicates if ::setContext() has been called or not.
83    *
84    * @var bool
85    */
86   public $setContextCalled = FALSE;
87
88   /**
89    * {@inheritdoc}
90    */
91   public function setContext($name, ComponentContextInterface $context) {
92     parent::setContext($name, $context);
93     $this->setContextCalled = TRUE;
94   }
95
96 }