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
// Pass the value to the plugin if there is one.
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()) {
// 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 @@
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;
......@@ -312,11 +314,20 @@ 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,
......@@ -333,8 +344,8 @@ public function testApplyContextMapping() {
->method('getContextDefinitions')
->will($this->returnValue(['hit' => $context_definition]));
$plugin->expects($this->once())
->method('setContext')
->with('hit', $context_hit);
->method('setContextValue')
->with('hit', $context_hit_data);
// Make sure that the cacheability metadata is passed to the plugin context.
$plugin_context = $this->getMock('Drupal\Core\Plugin\Context\ContextInterface');
......@@ -374,7 +385,7 @@ public function testApplyContextMappingMissingRequired() {
->method('getContextDefinitions')
->will($this->returnValue(['hit' => $context_definition]));
$plugin->expects($this->never())
->method('setContext');
->method('setContextValue');
// No context, so no cacheability metadata can be passed along.
$plugin->expects($this->never())
......@@ -409,7 +420,7 @@ public function testApplyContextMappingMissingNotRequired() {
->method('getContextDefinitions')
->will($this->returnValue(['optional' => $context_definition]));
$plugin->expects($this->never())
->method('setContext');
->method('setContextValue');
// No context, so no cacheability metadata can be passed along.
$plugin->expects($this->never())
......@@ -446,7 +457,7 @@ public function testApplyContextMappingNoValueRequired() {
->method('getContextDefinitions')
->will($this->returnValue(['hit' => $context_definition]));
$plugin->expects($this->never())
->method('setContext');
->method('setContextValue');
$this->setExpectedException(MissingValueContextException::class, 'Required contexts without a value: hit');
$this->contextHandler->applyContextMapping($plugin, $contexts);
......@@ -480,7 +491,7 @@ public function testApplyContextMappingNoValueNonRequired() {
->method('getContextDefinitions')
->will($this->returnValue(['hit' => $context_definition]));
$plugin->expects($this->never())
->method('setContext');
->method('setContextValue');
$this->contextHandler->applyContextMapping($plugin, $contexts);
}
......@@ -489,7 +500,12 @@ 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);
......@@ -508,8 +524,8 @@ public function testApplyContextMappingConfigurableAssigned() {
->method('getContextDefinitions')
->will($this->returnValue(['hit' => $context_definition]));
$plugin->expects($this->once())
->method('setContext')
->with('hit', $context);
->method('setContextValue')
->with('hit', $context_data);
// Make sure that the cacheability metadata is passed to the plugin context.
$plugin_context = $this->getMock('Drupal\Core\Plugin\Context\ContextInterface');
......@@ -546,7 +562,7 @@ public function testApplyContextMappingConfigurableAssignedMiss() {
->method('getContextDefinitions')
->will($this->returnValue(['hit' => $context_definition]));
$plugin->expects($this->never())
->method('setContext');
->method('setContextValue');
$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