Unverified Commit b33232b7 authored by alexpott's avatar alexpott

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

parent 7758b755
......@@ -97,7 +97,7 @@ public function applyContextMapping(ContextAwarePluginInterface $plugin, $contex
// Pass the value to the plugin if there is one.
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()) {
// 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 @@
use Drupal\Core\Plugin\Context\ContextDefinition;
use Drupal\Core\Plugin\Context\ContextHandler;
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\Validation\ConstraintManager;
use Drupal\Tests\UnitTestCase;
......@@ -314,20 +312,11 @@ public function providerTestFilterPluginDefinitionsByContexts() {
* @covers ::applyContextMapping
*/
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->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())
->method('hasContextValue')
->willReturn(TRUE);
$context_miss = $this->getMock('Drupal\Core\Plugin\Context\ContextInterface');
$context_miss->expects($this->never())
->method('getContextData');
$contexts = [
'hit' => $context_hit,
......@@ -344,8 +333,8 @@ public function testApplyContextMapping() {
->method('getContextDefinitions')
->will($this->returnValue(['hit' => $context_definition]));
$plugin->expects($this->once())
->method('setContextValue')
->with('hit', $context_hit_data);
->method('setContext')
->with('hit', $context_hit);
// Make sure that the cacheability metadata is passed to the plugin context.
$plugin_context = $this->getMock('Drupal\Core\Plugin\Context\ContextInterface');
......@@ -385,7 +374,7 @@ public function testApplyContextMappingMissingRequired() {
->method('getContextDefinitions')
->will($this->returnValue(['hit' => $context_definition]));
$plugin->expects($this->never())
->method('setContextValue');
->method('setContext');
// No context, so no cacheability metadata can be passed along.
$plugin->expects($this->never())
......@@ -420,7 +409,7 @@ public function testApplyContextMappingMissingNotRequired() {
->method('getContextDefinitions')
->will($this->returnValue(['optional' => $context_definition]));
$plugin->expects($this->never())
->method('setContextValue');
->method('setContext');
// No context, so no cacheability metadata can be passed along.
$plugin->expects($this->never())
......@@ -457,7 +446,7 @@ public function testApplyContextMappingNoValueRequired() {
->method('getContextDefinitions')
->will($this->returnValue(['hit' => $context_definition]));
$plugin->expects($this->never())
->method('setContextValue');
->method('setContext');
$this->setExpectedException(MissingValueContextException::class, 'Required contexts without a value: hit');
$this->contextHandler->applyContextMapping($plugin, $contexts);
......@@ -491,7 +480,7 @@ public function testApplyContextMappingNoValueNonRequired() {
->method('getContextDefinitions')
->will($this->returnValue(['hit' => $context_definition]));
$plugin->expects($this->never())
->method('setContextValue');
->method('setContext');
$this->contextHandler->applyContextMapping($plugin, $contexts);
}
......@@ -500,12 +489,7 @@ public function testApplyContextMappingNoValueNonRequired() {
* @covers ::applyContextMapping
*/
public function testApplyContextMappingConfigurableAssigned() {
$context_data = StringData::createInstance(DataDefinition::create('string'));
$context_data->setValue('foo');
$context = $this->getMock('Drupal\Core\Plugin\Context\ContextInterface');
$context->expects($this->atLeastOnce())
->method('getContextData')
->will($this->returnValue($context_data));
$context->expects($this->atLeastOnce())
->method('hasContextValue')
->willReturn(TRUE);
......@@ -524,8 +508,8 @@ public function testApplyContextMappingConfigurableAssigned() {
->method('getContextDefinitions')
->will($this->returnValue(['hit' => $context_definition]));
$plugin->expects($this->once())
->method('setContextValue')
->with('hit', $context_data);
->method('setContext')
->with('hit', $context);
// Make sure that the cacheability metadata is passed to the plugin context.
$plugin_context = $this->getMock('Drupal\Core\Plugin\Context\ContextInterface');
......@@ -562,7 +546,7 @@ public function testApplyContextMappingConfigurableAssignedMiss() {
->method('getContextDefinitions')
->will($this->returnValue(['hit' => $context_definition]));
$plugin->expects($this->never())
->method('setContextValue');
->method('setContext');
$this->setExpectedException(ContextException::class, 'Assigned contexts were not satisfied: miss');
$this->contextHandler->applyContextMapping($plugin, $contexts, ['miss' => 'name']);
......
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