From 068825184b9ca0571f99b23da1e5ef7b1ef2f2af Mon Sep 17 00:00:00 2001
From: effulgentsia <alex.bronstein@acquia.com>
Date: Mon, 23 Sep 2019 13:09:51 -0700
Subject: [PATCH] Issue #3080631 by phenaproxima, tim.plunkett: Context values
 passed in configuration to ContextAwarePluginBase are disregarded

---
 .../Plugin/ContextAwarePluginBase.php         | 28 ++++++++++++-
 .../Context/ContextAwarePluginBaseTest.php    | 40 +++++++++++++++++--
 2 files changed, 63 insertions(+), 5 deletions(-)
 rename core/tests/Drupal/{Tests => KernelTests}/Core/Plugin/Context/ContextAwarePluginBaseTest.php (59%)

diff --git a/core/lib/Drupal/Component/Plugin/ContextAwarePluginBase.php b/core/lib/Drupal/Component/Plugin/ContextAwarePluginBase.php
index dadb8b4f9dc7..30d6918c0d04 100644
--- a/core/lib/Drupal/Component/Plugin/ContextAwarePluginBase.php
+++ b/core/lib/Drupal/Component/Plugin/ContextAwarePluginBase.php
@@ -20,6 +20,19 @@ abstract class ContextAwarePluginBase extends PluginBase implements ContextAware
    */
   protected $context = [];
 
+  /**
+   * Data objects representing the contexts passed in the plugin configuration.
+   *
+   * @var \Drupal\Component\Plugin\Context\ContextInterface[]
+   *
+   * @deprecated
+   *   Deprecated in Drupal 8.8.0 and will be removed before Drupal 9.0.0. Use
+   *   \Drupal\Component\Plugin\ContextAwarePluginInterface instead.
+   *
+   * @see https://www.drupal.org/project/drupal/issues/3080631
+   */
+  private $contexts = [];
+
   /**
    * Overrides \Drupal\Component\Plugin\PluginBase::__construct().
    *
@@ -42,7 +55,20 @@ public function __construct(array $configuration, $plugin_id, $plugin_definition
 
     parent::__construct($configuration, $plugin_id, $plugin_definition);
 
-    $this->contexts = $this->createContextFromConfiguration($context_configuration);
+    $this->context = $this->createContextFromConfiguration($context_configuration);
+    // @todo Remove $this->contexts in Drupal 9; see
+    // https://www.drupal.org/project/drupal/issues/3081145
+    $this->contexts = $this->context;
+  }
+
+  /**
+   * Implements magic __get() method.
+   */
+  public function __get($name) {
+    if ($name === 'contexts') {
+      @trigger_error('The $contexts property is deprecated in Drupal 8.8.0 and will be removed before Drupal 9.0.0. Use methods of \Drupal\Component\Plugin\ContextAwarePluginInterface instead. See https://www.drupal.org/project/drupal/issues/3080631 for more information.', E_USER_DEPRECATED);
+      return $this->contexts;
+    }
   }
 
   /**
diff --git a/core/tests/Drupal/Tests/Core/Plugin/Context/ContextAwarePluginBaseTest.php b/core/tests/Drupal/KernelTests/Core/Plugin/Context/ContextAwarePluginBaseTest.php
similarity index 59%
rename from core/tests/Drupal/Tests/Core/Plugin/Context/ContextAwarePluginBaseTest.php
rename to core/tests/Drupal/KernelTests/Core/Plugin/Context/ContextAwarePluginBaseTest.php
index b3c0d694e874..0e1ba5daab0e 100644
--- a/core/tests/Drupal/Tests/Core/Plugin/Context/ContextAwarePluginBaseTest.php
+++ b/core/tests/Drupal/KernelTests/Core/Plugin/Context/ContextAwarePluginBaseTest.php
@@ -1,6 +1,6 @@
 <?php
 
-namespace Drupal\Tests\Core\Plugin\Context;
+namespace Drupal\KernelTests\Core\Plugin\Context;
 
 use Drupal\Component\Plugin\Context\ContextInterface as ComponentContextInterface;
 use Drupal\Component\Plugin\Definition\ContextAwarePluginDefinitionInterface;
@@ -13,13 +13,17 @@
 use Drupal\Core\TypedData\DataDefinition;
 use Drupal\Core\TypedData\Plugin\DataType\StringData;
 use Drupal\Core\TypedData\TypedDataManagerInterface;
-use Drupal\Tests\UnitTestCase;
+use Drupal\KernelTests\KernelTestBase;
+use Drupal\Tests\Traits\ExpectDeprecationTrait;
 
 /**
  * @coversDefaultClass \Drupal\Core\Plugin\ContextAwarePluginBase
+ *
  * @group Plugin
  */
-class ContextAwarePluginBaseTest extends UnitTestCase {
+class ContextAwarePluginBaseTest extends KernelTestBase {
+
+  use ExpectDeprecationTrait;
 
   /**
    * The plugin instance under test.
@@ -33,7 +37,14 @@ class ContextAwarePluginBaseTest extends UnitTestCase {
    */
   public function setUp() {
     parent::setUp();
-    $this->plugin = new TestContextAwarePlugin([], 'the_sisko', new TestPluginDefinition());
+    $configuration = [
+      'context' => [
+        'nato_letter' => 'Alpha',
+      ],
+    ];
+    $plugin_definition = new TestPluginDefinition();
+    $plugin_definition->addContextDefinition('nato_letter', ContextDefinition::create('string'));
+    $this->plugin = new TestContextAwarePlugin($configuration, 'the_sisko', $plugin_definition);
   }
 
   /**
@@ -53,8 +64,24 @@ public function testGetContextDefinition() {
     $this->plugin->getContextDefinition('person');
   }
 
+  /**
+   * @covers ::getContextValue
+   * @group legacy
+   */
+  public function testGetContextValue() {
+    // Assert that the context value passed in the plugin configuration is
+    // available.
+    $this->assertSame('Alpha', $this->plugin->getContextValue('nato_letter'));
+
+    // It should be possible to access the context via the $contexts property,
+    // but it should trigger a deprecation notice.
+    $this->expectDeprecation('The $contexts property is deprecated in Drupal 8.8.0 and will be removed before Drupal 9.0.0. Use methods of \Drupal\Component\Plugin\ContextAwarePluginInterface instead. See https://www.drupal.org/project/drupal/issues/3080631 for more information.');
+    $this->assertSame('Alpha', $this->plugin->contexts['nato_letter']->getContextValue());
+  }
+
   /**
    * @covers ::setContextValue
+   * @group legacy
    */
   public function testSetContextValue() {
     $typed_data_manager = $this->prophesize(TypedDataManagerInterface::class);
@@ -67,6 +94,11 @@ public function testSetContextValue() {
     $this->assertFalse($this->plugin->setContextCalled);
     $this->plugin->setContextValue('foo', new StringData(new DataDefinition(), 'bar'));
     $this->assertTrue($this->plugin->setContextCalled);
+
+    // Assert that setContextValue() did NOT update the deprecated $contexts
+    // property.
+    $this->expectDeprecation('The $contexts property is deprecated in Drupal 8.8.0 and will be removed before Drupal 9.0.0. Use methods of \Drupal\Component\Plugin\ContextAwarePluginInterface instead. See https://www.drupal.org/project/drupal/issues/3080631 for more information.');
+    $this->assertArrayNotHasKey('foo', $this->plugin->contexts);
   }
 
 }
-- 
GitLab