Commit 6a519217 authored by xjm's avatar xjm

Revert "Issue #3016458 by tim.plunkett, xjm: ContextHandler should use...

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

This reverts commit 2b93e802.
parent 698f4703
...@@ -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->setContext($plugin_context_id, $contexts[$context_id]); $plugin->setContextValue($plugin_context_id, $contexts[$context_id]->getContextData());
} }
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,6 +20,8 @@ ...@@ -20,6 +20,8 @@
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;
...@@ -312,11 +314,20 @@ public function providerTestFilterPluginDefinitionsByContexts() { ...@@ -312,11 +314,20 @@ 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,
...@@ -333,8 +344,8 @@ public function testApplyContextMapping() { ...@@ -333,8 +344,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('setContext') ->method('setContextValue')
->with('hit', $context_hit); ->with('hit', $context_hit_data);
// 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');
...@@ -374,7 +385,7 @@ public function testApplyContextMappingMissingRequired() { ...@@ -374,7 +385,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('setContext'); ->method('setContextValue');
// 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())
...@@ -409,7 +420,7 @@ public function testApplyContextMappingMissingNotRequired() { ...@@ -409,7 +420,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('setContext'); ->method('setContextValue');
// 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())
...@@ -446,7 +457,7 @@ public function testApplyContextMappingNoValueRequired() { ...@@ -446,7 +457,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('setContext'); ->method('setContextValue');
$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);
...@@ -480,7 +491,7 @@ public function testApplyContextMappingNoValueNonRequired() { ...@@ -480,7 +491,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('setContext'); ->method('setContextValue');
$this->contextHandler->applyContextMapping($plugin, $contexts); $this->contextHandler->applyContextMapping($plugin, $contexts);
} }
...@@ -489,7 +500,12 @@ public function testApplyContextMappingNoValueNonRequired() { ...@@ -489,7 +500,12 @@ 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);
...@@ -508,8 +524,8 @@ public function testApplyContextMappingConfigurableAssigned() { ...@@ -508,8 +524,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('setContext') ->method('setContextValue')
->with('hit', $context); ->with('hit', $context_data);
// 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');
...@@ -546,7 +562,7 @@ public function testApplyContextMappingConfigurableAssignedMiss() { ...@@ -546,7 +562,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('setContext'); ->method('setContextValue');
$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']);
......
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