Commit 136dd9ed authored by alexpott's avatar alexpott

Issue #3014851 by tim.plunkett: ContextAwarePluginBase::setContextValue()...

Issue #3014851 by tim.plunkett: ContextAwarePluginBase::setContextValue() should delegate to ::setContext()

(cherry picked from commit 7dcb9273)
parent e297dace
......@@ -70,7 +70,7 @@ public function setContext($name, ComponentContextInterface $context) {
* {@inheritdoc}
*/
public function setContextValue($name, $value) {
$this->context[$name] = Context::createFromContext($this->getContext($name), $value);
$this->setContext($name, Context::createFromContext($this->getContext($name), $value));
return $this;
}
......
......@@ -2,11 +2,17 @@
namespace Drupal\Tests\Core\Plugin\Context;
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\ContextAwarePluginBase;
use Drupal\Core\TypedData\DataDefinition;
use Drupal\Core\TypedData\Plugin\DataType\StringData;
use Drupal\Core\TypedData\TypedDataManagerInterface;
use Drupal\Tests\UnitTestCase;
/**
......@@ -46,6 +52,22 @@ public function testGetContextDefinition() {
$this->plugin->getContextDefinition('person');
}
/**
* @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);
}
}
class TestPluginDefinition extends PluginDefinition implements ContextAwarePluginDefinitionInterface {
......@@ -54,4 +76,21 @@ class TestPluginDefinition extends PluginDefinition implements ContextAwarePlugi
}
class TestContextAwarePlugin extends ContextAwarePluginBase {}
class TestContextAwarePlugin extends ContextAwarePluginBase {
/**
* Indicates if ::setContext() has been called or not.
*
* @var bool
*/
public $setContextCalled = FALSE;
/**
* {@inheritdoc}
*/
public function setContext($name, ComponentContextInterface $context) {
parent::setContext($name, $context);
$this->setContextCalled = TRUE;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment