Skip to content
Snippets Groups Projects
Unverified Commit 2b93e802 authored by Alex Pott's avatar Alex Pott
Browse files

Issue #3016458 by tim.plunkett, xjm: ContextHandler should use setContext() not setContextValue()

(cherry picked from commit b33232b7)
parent a6005f8f
No related branches found
No related tags found
No related merge requests found
...@@ -97,7 +97,7 @@ public function applyContextMapping(ContextAwarePluginInterface $plugin, $contex ...@@ -97,7 +97,7 @@ public function applyContextMapping(ContextAwarePluginInterface $plugin, $contex
// Pass the value to the plugin if there is one. // Pass the value to the plugin if there is one.
if ($contexts[$context_id]->hasContextValue()) { if ($contexts[$context_id]->hasContextValue()) {
$plugin->setContextValue($plugin_context_id, $contexts[$context_id]->getContextData()); $plugin->setContext($plugin_context_id, $contexts[$context_id]);
} }
elseif ($plugin_context_definition->isRequired()) { elseif ($plugin_context_definition->isRequired()) {
// Collect required contexts that exist but are missing a value. // Collect required contexts that exist but are missing a value.
......
<?php
namespace Drupal\KernelTests\Core\Plugin;
use Drupal\Core\Plugin\Context\ContextHandler;
use Drupal\Core\Plugin\Context\EntityContext;
use Drupal\Core\Plugin\Context\EntityContextDefinition;
use Drupal\Core\Plugin\ContextAwarePluginBase;
use Drupal\entity_test\Entity\EntityTest;
use Drupal\KernelTests\KernelTestBase;
/**
* @coversDefaultClass \Drupal\Core\Plugin\Context\ContextHandler
*
* @group Plugin
*/
class ContextHandlerTest extends KernelTestBase {
/**
* {@inheritdoc}
*/
protected static $modules = [
'entity_test',
'user',
];
/**
* @covers ::applyContextMapping
*/
public function testApplyContextMapping() {
$entity = EntityTest::create([]);
$context_definition = EntityContextDefinition::fromEntity($entity);
$context = EntityContext::fromEntity($entity);
$definition = ['context_definitions' => ['a_context_id' => $context_definition]];
$plugin = new TestContextAwarePlugin([], 'test_plugin_id', $definition);
(new ContextHandler())->applyContextMapping($plugin, ['a_context_id' => $context]);
$result = $plugin->getContext('a_context_id');
$this->assertInstanceOf(EntityContext::class, $result);
$this->assertSame($context, $result);
}
}
/**
* Provides a test implementation of a context-aware plugin.
*/
class TestContextAwarePlugin extends ContextAwarePluginBase {
}
...@@ -20,8 +20,6 @@ ...@@ -20,8 +20,6 @@
use Drupal\Core\Plugin\Context\ContextDefinition; use Drupal\Core\Plugin\Context\ContextDefinition;
use Drupal\Core\Plugin\Context\ContextHandler; use Drupal\Core\Plugin\Context\ContextHandler;
use Drupal\Core\Plugin\ContextAwarePluginInterface; use Drupal\Core\Plugin\ContextAwarePluginInterface;
use Drupal\Core\TypedData\DataDefinition;
use Drupal\Core\TypedData\Plugin\DataType\StringData;
use Drupal\Core\TypedData\TypedDataManager; use Drupal\Core\TypedData\TypedDataManager;
use Drupal\Core\Validation\ConstraintManager; use Drupal\Core\Validation\ConstraintManager;
use Drupal\Tests\UnitTestCase; use Drupal\Tests\UnitTestCase;
...@@ -314,20 +312,11 @@ public function providerTestFilterPluginDefinitionsByContexts() { ...@@ -314,20 +312,11 @@ public function providerTestFilterPluginDefinitionsByContexts() {
* @covers ::applyContextMapping * @covers ::applyContextMapping
*/ */
public function testApplyContextMapping() { public function testApplyContextMapping() {
$context_hit_data = StringData::createInstance(DataDefinition::create('string'));
$context_hit_data->setValue('foo');
$context_hit = $this->getMock('Drupal\Core\Plugin\Context\ContextInterface'); $context_hit = $this->getMock('Drupal\Core\Plugin\Context\ContextInterface');
$context_hit->expects($this->atLeastOnce())
->method('getContextData')
->will($this->returnValue($context_hit_data));
$context_miss_data = StringData::createInstance(DataDefinition::create('string'));
$context_miss_data->setValue('bar');
$context_hit->expects($this->atLeastOnce()) $context_hit->expects($this->atLeastOnce())
->method('hasContextValue') ->method('hasContextValue')
->willReturn(TRUE); ->willReturn(TRUE);
$context_miss = $this->getMock('Drupal\Core\Plugin\Context\ContextInterface'); $context_miss = $this->getMock('Drupal\Core\Plugin\Context\ContextInterface');
$context_miss->expects($this->never())
->method('getContextData');
$contexts = [ $contexts = [
'hit' => $context_hit, 'hit' => $context_hit,
...@@ -344,8 +333,8 @@ public function testApplyContextMapping() { ...@@ -344,8 +333,8 @@ public function testApplyContextMapping() {
->method('getContextDefinitions') ->method('getContextDefinitions')
->will($this->returnValue(['hit' => $context_definition])); ->will($this->returnValue(['hit' => $context_definition]));
$plugin->expects($this->once()) $plugin->expects($this->once())
->method('setContextValue') ->method('setContext')
->with('hit', $context_hit_data); ->with('hit', $context_hit);
// Make sure that the cacheability metadata is passed to the plugin context. // Make sure that the cacheability metadata is passed to the plugin context.
$plugin_context = $this->getMock('Drupal\Core\Plugin\Context\ContextInterface'); $plugin_context = $this->getMock('Drupal\Core\Plugin\Context\ContextInterface');
...@@ -385,7 +374,7 @@ public function testApplyContextMappingMissingRequired() { ...@@ -385,7 +374,7 @@ public function testApplyContextMappingMissingRequired() {
->method('getContextDefinitions') ->method('getContextDefinitions')
->will($this->returnValue(['hit' => $context_definition])); ->will($this->returnValue(['hit' => $context_definition]));
$plugin->expects($this->never()) $plugin->expects($this->never())
->method('setContextValue'); ->method('setContext');
// No context, so no cacheability metadata can be passed along. // No context, so no cacheability metadata can be passed along.
$plugin->expects($this->never()) $plugin->expects($this->never())
...@@ -420,7 +409,7 @@ public function testApplyContextMappingMissingNotRequired() { ...@@ -420,7 +409,7 @@ public function testApplyContextMappingMissingNotRequired() {
->method('getContextDefinitions') ->method('getContextDefinitions')
->will($this->returnValue(['optional' => $context_definition])); ->will($this->returnValue(['optional' => $context_definition]));
$plugin->expects($this->never()) $plugin->expects($this->never())
->method('setContextValue'); ->method('setContext');
// No context, so no cacheability metadata can be passed along. // No context, so no cacheability metadata can be passed along.
$plugin->expects($this->never()) $plugin->expects($this->never())
...@@ -457,7 +446,7 @@ public function testApplyContextMappingNoValueRequired() { ...@@ -457,7 +446,7 @@ public function testApplyContextMappingNoValueRequired() {
->method('getContextDefinitions') ->method('getContextDefinitions')
->will($this->returnValue(['hit' => $context_definition])); ->will($this->returnValue(['hit' => $context_definition]));
$plugin->expects($this->never()) $plugin->expects($this->never())
->method('setContextValue'); ->method('setContext');
$this->setExpectedException(MissingValueContextException::class, 'Required contexts without a value: hit'); $this->setExpectedException(MissingValueContextException::class, 'Required contexts without a value: hit');
$this->contextHandler->applyContextMapping($plugin, $contexts); $this->contextHandler->applyContextMapping($plugin, $contexts);
...@@ -491,7 +480,7 @@ public function testApplyContextMappingNoValueNonRequired() { ...@@ -491,7 +480,7 @@ public function testApplyContextMappingNoValueNonRequired() {
->method('getContextDefinitions') ->method('getContextDefinitions')
->will($this->returnValue(['hit' => $context_definition])); ->will($this->returnValue(['hit' => $context_definition]));
$plugin->expects($this->never()) $plugin->expects($this->never())
->method('setContextValue'); ->method('setContext');
$this->contextHandler->applyContextMapping($plugin, $contexts); $this->contextHandler->applyContextMapping($plugin, $contexts);
} }
...@@ -500,12 +489,7 @@ public function testApplyContextMappingNoValueNonRequired() { ...@@ -500,12 +489,7 @@ public function testApplyContextMappingNoValueNonRequired() {
* @covers ::applyContextMapping * @covers ::applyContextMapping
*/ */
public function testApplyContextMappingConfigurableAssigned() { public function testApplyContextMappingConfigurableAssigned() {
$context_data = StringData::createInstance(DataDefinition::create('string'));
$context_data->setValue('foo');
$context = $this->getMock('Drupal\Core\Plugin\Context\ContextInterface'); $context = $this->getMock('Drupal\Core\Plugin\Context\ContextInterface');
$context->expects($this->atLeastOnce())
->method('getContextData')
->will($this->returnValue($context_data));
$context->expects($this->atLeastOnce()) $context->expects($this->atLeastOnce())
->method('hasContextValue') ->method('hasContextValue')
->willReturn(TRUE); ->willReturn(TRUE);
...@@ -524,8 +508,8 @@ public function testApplyContextMappingConfigurableAssigned() { ...@@ -524,8 +508,8 @@ public function testApplyContextMappingConfigurableAssigned() {
->method('getContextDefinitions') ->method('getContextDefinitions')
->will($this->returnValue(['hit' => $context_definition])); ->will($this->returnValue(['hit' => $context_definition]));
$plugin->expects($this->once()) $plugin->expects($this->once())
->method('setContextValue') ->method('setContext')
->with('hit', $context_data); ->with('hit', $context);
// Make sure that the cacheability metadata is passed to the plugin context. // Make sure that the cacheability metadata is passed to the plugin context.
$plugin_context = $this->getMock('Drupal\Core\Plugin\Context\ContextInterface'); $plugin_context = $this->getMock('Drupal\Core\Plugin\Context\ContextInterface');
...@@ -562,7 +546,7 @@ public function testApplyContextMappingConfigurableAssignedMiss() { ...@@ -562,7 +546,7 @@ public function testApplyContextMappingConfigurableAssignedMiss() {
->method('getContextDefinitions') ->method('getContextDefinitions')
->will($this->returnValue(['hit' => $context_definition])); ->will($this->returnValue(['hit' => $context_definition]));
$plugin->expects($this->never()) $plugin->expects($this->never())
->method('setContextValue'); ->method('setContext');
$this->setExpectedException(ContextException::class, 'Assigned contexts were not satisfied: miss'); $this->setExpectedException(ContextException::class, 'Assigned contexts were not satisfied: miss');
$this->contextHandler->applyContextMapping($plugin, $contexts, ['miss' => 'name']); $this->contextHandler->applyContextMapping($plugin, $contexts, ['miss' => 'name']);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment