Commit 91b4ae58 authored by alexpott's avatar alexpott

Issue #2508884 by EclipseGc, tim.plunkett, dsnopek, japerry, sravya_12,...

Issue #2508884 by EclipseGc, tim.plunkett, dsnopek, japerry, sravya_12, swathin, Wim Leers, Fabianx, dawehner, yched: Make contexts immutable
parent c7de07a6
...@@ -31,20 +31,16 @@ class Context implements ContextInterface { ...@@ -31,20 +31,16 @@ class Context implements ContextInterface {
protected $contextDefinition; protected $contextDefinition;
/** /**
* Sets the contextDefinition for us without needing to call the setter. * Create a context object.
* *
* @param \Drupal\Component\Plugin\Context\ContextDefinitionInterface $context_definition * @param \Drupal\Component\Plugin\Context\ContextDefinitionInterface $context_definition
* The context definition. * The context definition.
* @param mixed|null $context_value
* The value of the context.
*/ */
public function __construct(ContextDefinitionInterface $context_definition) { public function __construct(ContextDefinitionInterface $context_definition, $context_value = NULL) {
$this->contextDefinition = $context_definition; $this->contextDefinition = $context_definition;
} $this->contextValue = $context_value;
/**
* Implements \Drupal\Component\Plugin\Context\ContextInterface::setContextValue().
*/
public function setContextValue($value) {
$this->contextValue = $value;
} }
/** /**
...@@ -74,13 +70,6 @@ public function hasContextValue() { ...@@ -74,13 +70,6 @@ public function hasContextValue() {
return (bool) $this->contextValue || (bool) $this->getContextDefinition()->getDefaultValue(); return (bool) $this->contextValue || (bool) $this->getContextDefinition()->getDefaultValue();
} }
/**
* {@inheritdoc}
*/
public function setContextDefinition(ContextDefinitionInterface $context_definition) {
$this->contextDefinition = $context_definition;
}
/** /**
* Implements \Drupal\Component\Plugin\Context\ContextInterface::getContextDefinition(). * Implements \Drupal\Component\Plugin\Context\ContextInterface::getContextDefinition().
*/ */
......
...@@ -12,16 +12,6 @@ ...@@ -12,16 +12,6 @@
*/ */
interface ContextInterface { interface ContextInterface {
/**
* Sets the context value.
*
* @param mixed $value
* The value of this context, matching the context definition.
*
* @see \Drupal\Component\Plugin\Context\ContextInterface::setContextDefinition().
*/
public function setContextValue($value);
/** /**
* Gets the context value. * Gets the context value.
* *
...@@ -38,15 +28,6 @@ public function getContextValue(); ...@@ -38,15 +28,6 @@ public function getContextValue();
*/ */
public function hasContextValue(); public function hasContextValue();
/**
* Sets the definition that the context must conform to.
*
* @param \Drupal\Component\Plugin\Context\ContextDefinitionInterface $context_definition
* A defining characteristic representation of the context against which
* that context can be validated.
*/
public function setContextDefinition(ContextDefinitionInterface $context_definition);
/** /**
* Gets the provided definition that the context must conform to. * Gets the provided definition that the context must conform to.
* *
......
...@@ -41,17 +41,30 @@ abstract class ContextAwarePluginBase extends PluginBase implements ContextAware ...@@ -41,17 +41,30 @@ abstract class ContextAwarePluginBase extends PluginBase implements ContextAware
* The plugin implementation definition. * The plugin implementation definition.
*/ */
public function __construct(array $configuration, $plugin_id, $plugin_definition) { public function __construct(array $configuration, $plugin_id, $plugin_definition) {
$context = array(); $context_configuration = isset($configuration['context']) ? $configuration['context'] : [];
if (isset($configuration['context'])) { unset($configuration['context']);
$context = $configuration['context'];
unset($configuration['context']);
}
parent::__construct($configuration, $plugin_id, $plugin_definition); parent::__construct($configuration, $plugin_id, $plugin_definition);
foreach ($context as $key => $value) {
$this->contexts = $this->createContextFromConfiguration($context_configuration);
}
/**
* Creates context objects from any context mappings in configuration.
*
* @param array $context_configuration
* An associative array of context names and values.
*
* @return \Drupal\Component\Plugin\Context\ContextInterface[]
* An array of context objects.
*/
protected function createContextFromConfiguration(array $context_configuration) {
$contexts = [];
foreach ($context_configuration as $key => $value) {
$context_definition = $this->getContextDefinition($key); $context_definition = $this->getContextDefinition($key);
$this->context[$key] = new Context($context_definition); $contexts[$key] = new Context($context_definition, $value);
$this->context[$key]->setContextValue($value);
} }
return $contexts;
} }
/** /**
...@@ -124,7 +137,7 @@ public function getContextValue($name) { ...@@ -124,7 +137,7 @@ public function getContextValue($name) {
* {@inheritdoc} * {@inheritdoc}
*/ */
public function setContextValue($name, $value) { public function setContextValue($name, $value) {
$this->getContext($name)->setContextValue($value); $this->context[$name] = new Context($this->getContextDefinition($name), $value);
return $this; return $this;
} }
......
...@@ -57,8 +57,7 @@ public function getRuntimeContexts(array $unqualified_context_ids) { ...@@ -57,8 +57,7 @@ public function getRuntimeContexts(array $unqualified_context_ids) {
$result = []; $result = [];
foreach ($language_types as $type_key) { foreach ($language_types as $type_key) {
if (isset($info[$type_key]['name'])) { if (isset($info[$type_key]['name'])) {
$context = new Context(new ContextDefinition('language', $info[$type_key]['name'])); $context = new Context(new ContextDefinition('language', $info[$type_key]['name']), $this->languageManager->getCurrentLanguage($type_key));
$context->setContextValue($this->languageManager->getCurrentLanguage($type_key));
$cacheability = new CacheableMetadata(); $cacheability = new CacheableMetadata();
$cacheability->setCacheContexts(['languages:' . $type_key]); $cacheability->setCacheContexts(['languages:' . $type_key]);
......
...@@ -43,11 +43,19 @@ class Context extends ComponentContext implements ContextInterface { ...@@ -43,11 +43,19 @@ class Context extends ComponentContext implements ContextInterface {
protected $cacheabilityMetadata; protected $cacheabilityMetadata;
/** /**
* {@inheritdoc} * Create a context object.
*
* @param \Drupal\Core\Plugin\Context\ContextDefinitionInterface $context_definition
* The context definition.
* @param mixed $context_value|NULL
* The context value object.
*/ */
public function __construct(ContextDefinitionInterface $context_definition) { public function __construct(ContextDefinitionInterface $context_definition, $context_value = NULL) {
parent::__construct($context_definition); parent::__construct($context_definition, NULL);
$this->cacheabilityMetadata = new CacheableMetadata(); $this->cacheabilityMetadata = new CacheableMetadata();
if (!is_null($context_value)) {
$this->setContextValue($context_value);
}
} }
/** /**
...@@ -80,19 +88,22 @@ public function hasContextValue() { ...@@ -80,19 +88,22 @@ public function hasContextValue() {
} }
/** /**
* {@inheritdoc} * Sets the context value.
*
* @param mixed $value
* The value of this context, matching the context definition.
*/ */
public function setContextValue($value) { protected function setContextValue($value) {
// Add the value as a cacheable dependency only if implements the interface // Add the value as a cacheable dependency only if implements the interface
// to prevent it from disabling caching with a max-age 0. // to prevent it from disabling caching with a max-age 0.
if ($value instanceof CacheableDependencyInterface) { if ($value instanceof CacheableDependencyInterface) {
$this->addCacheableDependency($value); $this->addCacheableDependency($value);
} }
if ($value instanceof TypedDataInterface) { if ($value instanceof TypedDataInterface) {
return $this->setContextData($value); $this->contextData = $value;
} }
else { else {
return $this->setContextData($this->getTypedDataManager()->create($this->contextDefinition->getDataDefinition(), $value)); $this->contextData = $this->getTypedDataManager()->create($this->contextDefinition->getDataDefinition(), $value);
} }
} }
...@@ -119,13 +130,6 @@ public function getContextData() { ...@@ -119,13 +130,6 @@ public function getContextData() {
return $this->contextData; return $this->contextData;
} }
/**
* {@inheritdoc}
*/
public function setContextData(TypedDataInterface $data) {
$this->contextData = $data;
return $this;
}
/** /**
* {@inheritdoc} * {@inheritdoc}
...@@ -170,4 +174,16 @@ public function getCacheMaxAge() { ...@@ -170,4 +174,16 @@ public function getCacheMaxAge() {
return $this->cacheabilityMetadata->getCacheMaxAge(); return $this->cacheabilityMetadata->getCacheMaxAge();
} }
/**
* {@inheritdoc}
*/
public static function createFromContext(ContextInterface $old_context, $value) {
$context = new static($old_context->getContextDefinition(), $value);
$context->addCacheableDependency($old_context);
if (method_exists($old_context, 'getTypedDataManager')) {
$context->setTypedDataManager($old_context->getTypedDataManager());
}
return $context;
}
} }
...@@ -72,6 +72,7 @@ public function getMatchingContexts(array $contexts, ContextDefinitionInterface ...@@ -72,6 +72,7 @@ public function getMatchingContexts(array $contexts, ContextDefinitionInterface
* {@inheritdoc} * {@inheritdoc}
*/ */
public function applyContextMapping(ContextAwarePluginInterface $plugin, $contexts, $mappings = array()) { public function applyContextMapping(ContextAwarePluginInterface $plugin, $contexts, $mappings = array()) {
/** @var $contexts \Drupal\Core\Plugin\Context\ContextInterface[] */
$mappings += $plugin->getContextMapping(); $mappings += $plugin->getContextMapping();
// Loop through each of the expected contexts. // Loop through each of the expected contexts.
...@@ -94,7 +95,7 @@ public function applyContextMapping(ContextAwarePluginInterface $plugin, $contex ...@@ -94,7 +95,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]->getContextValue()); $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.
......
...@@ -68,7 +68,7 @@ public function getMatchingContexts(array $contexts, ContextDefinitionInterface ...@@ -68,7 +68,7 @@ public function getMatchingContexts(array $contexts, ContextDefinitionInterface
* *
* @param \Drupal\Core\Plugin\ContextAwarePluginInterface $plugin * @param \Drupal\Core\Plugin\ContextAwarePluginInterface $plugin
* A plugin about to be evaluated. * A plugin about to be evaluated.
* @param \Drupal\Component\Plugin\Context\ContextInterface[] $contexts * @param \Drupal\Core\Plugin\Context\ContextInterface[] $contexts
* An array of contexts to set on the plugin. They will only be set if they * An array of contexts to set on the plugin. They will only be set if they
* match the plugin's context definitions. * match the plugin's context definitions.
* @param array $mappings * @param array $mappings
......
...@@ -9,7 +9,6 @@ ...@@ -9,7 +9,6 @@
use Drupal\Component\Plugin\Context\ContextInterface as ComponentContextInterface; use Drupal\Component\Plugin\Context\ContextInterface as ComponentContextInterface;
use Drupal\Core\Cache\CacheableDependencyInterface; use Drupal\Core\Cache\CacheableDependencyInterface;
use Drupal\Core\TypedData\TypedDataInterface;
/** /**
* Interface for context. * Interface for context.
...@@ -17,21 +16,18 @@ ...@@ -17,21 +16,18 @@
interface ContextInterface extends ComponentContextInterface, CacheableDependencyInterface { interface ContextInterface extends ComponentContextInterface, CacheableDependencyInterface {
/** /**
* Gets the context value as typed data object. * {@inheritdoc}
* *
* @return \Drupal\Core\TypedData\TypedDataInterface * @return \Drupal\Core\Plugin\Context\ContextDefinitionInterface
*/ */
public function getContextData(); public function getContextDefinition();
/** /**
* Sets the context value as typed data object. * Gets the context value as typed data object.
*
* @param \Drupal\Core\TypedData\TypedDataInterface $data
* The context value as a typed data object.
* *
* @return $this * @return \Drupal\Core\TypedData\TypedDataInterface
*/ */
public function setContextData(TypedDataInterface $data); public function getContextData();
/** /**
* Adds a dependency on an object: merges its cacheability metadata. * Adds a dependency on an object: merges its cacheability metadata.
...@@ -51,4 +47,17 @@ public function setContextData(TypedDataInterface $data); ...@@ -51,4 +47,17 @@ public function setContextData(TypedDataInterface $data);
*/ */
public function addCacheableDependency($dependency); public function addCacheableDependency($dependency);
/**
* Creates a new context with a different value.
*
* @param \Drupal\Core\Plugin\Context\ContextInterface $old_context
* The context object used to create a new object. Cacheability metadata
* will be copied over.
* @param mixed $value
* The value of the new context object.
*
* @return static
*/
public static function createFromContext(ContextInterface $old_context, $value);
} }
...@@ -32,8 +32,7 @@ interface ContextProviderInterface { ...@@ -32,8 +32,7 @@ interface ContextProviderInterface {
* $node = ... * $node = ...
* *
* // Set that specific node as the value of the 'node' context. * // Set that specific node as the value of the 'node' context.
* $context = new Context(new ContextDefinition('entity:node')); * $context = new Context(new ContextDefinition('entity:node'), $node);
* $context->setContextValue($node);
* return ['node' => $context]; * return ['node' => $context];
* @endcode * @endcode
* *
......
...@@ -30,6 +30,25 @@ abstract class ContextAwarePluginBase extends ComponentContextAwarePluginBase im ...@@ -30,6 +30,25 @@ abstract class ContextAwarePluginBase extends ComponentContextAwarePluginBase im
/** /**
* {@inheritdoc} * {@inheritdoc}
* *
* @return \Drupal\Core\Plugin\Context\ContextInterface[]
*/
protected function createContextFromConfiguration(array $context_configuration) {
// This method is overridden so that it will use
// \Drupal\Core\Plugin\Context\Context instead.
$contexts = [];
foreach ($context_configuration as $key => $value) {
$context_definition = $this->getContextDefinition($key);
$contexts[$key] = new Context($context_definition, $value);
}
return $contexts;
}
/**
* {@inheritdoc}
*
* @return \Drupal\Core\Plugin\Context\ContextInterface
* The context object.
*
* This code is identical to the Component in order to pick up a different * This code is identical to the Component in order to pick up a different
* Context class. * Context class.
*/ */
...@@ -52,6 +71,14 @@ public function setContext($name, ComponentContextInterface $context) { ...@@ -52,6 +71,14 @@ public function setContext($name, ComponentContextInterface $context) {
parent::setContext($name, $context); parent::setContext($name, $context);
} }
/**
* {@inheritdoc}
*/
public function setContextValue($name, $value) {
$this->context[$name] = Context::createFromContext($this->getContext($name), $value);
return $this;
}
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
...@@ -84,6 +111,15 @@ public function getContextDefinitions() { ...@@ -84,6 +111,15 @@ public function getContextDefinitions() {
return parent::getContextDefinitions(); return parent::getContextDefinitions();
} }
/**
* {@inheritdoc}
*
* @return \Drupal\Core\Plugin\Context\ContextDefinitionInterface
*/
public function getContextDefinition($name) {
return parent::getContextDefinition($name);
}
/** /**
* Wraps the context handler. * Wraps the context handler.
* *
......
...@@ -52,11 +52,9 @@ public function __construct(AccountInterface $account, EntityManagerInterface $e ...@@ -52,11 +52,9 @@ public function __construct(AccountInterface $account, EntityManagerInterface $e
public function getRuntimeContexts(array $unqualified_context_ids) { public function getRuntimeContexts(array $unqualified_context_ids) {
$current_user = $this->userStorage->load($this->account->id()); $current_user = $this->userStorage->load($this->account->id());
$context1 = new Context(new ContextDefinition('entity:user', 'User 1')); $context1 = new Context(new ContextDefinition('entity:user', 'User 1'), $current_user);
$context1->setContextValue($current_user);
$context2 = new Context(new ContextDefinition('entity:user', 'User 2')); $context2 = new Context(new ContextDefinition('entity:user', 'User 2'), $current_user);
$context2->setContextValue($current_user);
$cacheability = new CacheableMetadata(); $cacheability = new CacheableMetadata();
$cacheability->setCacheContexts(['user']); $cacheability->setCacheContexts(['user']);
......
...@@ -44,18 +44,22 @@ public function __construct(RouteMatchInterface $route_match) { ...@@ -44,18 +44,22 @@ public function __construct(RouteMatchInterface $route_match) {
*/ */
public function getRuntimeContexts(array $unqualified_context_ids) { public function getRuntimeContexts(array $unqualified_context_ids) {
$result = []; $result = [];
$context = new Context(new ContextDefinition('entity:node', NULL, FALSE)); $context_definition = new ContextDefinition('entity:node', NULL, FALSE);
$value = NULL;
if (($route_object = $this->routeMatch->getRouteObject()) && ($route_contexts = $route_object->getOption('parameters')) && isset($route_contexts['node'])) { if (($route_object = $this->routeMatch->getRouteObject()) && ($route_contexts = $route_object->getOption('parameters')) && isset($route_contexts['node'])) {
if ($node = $this->routeMatch->getParameter('node')) { if ($node = $this->routeMatch->getParameter('node')) {
$context->setContextValue($node); $value = $node;
} }
} }
elseif ($this->routeMatch->getRouteName() == 'node.add') { elseif ($this->routeMatch->getRouteName() == 'node.add') {
$node_type = $this->routeMatch->getParameter('node_type'); $node_type = $this->routeMatch->getParameter('node_type');
$context->setContextValue(Node::create(array('type' => $node_type->id()))); $value = Node::create(array('type' => $node_type->id()));
} }
$cacheability = new CacheableMetadata(); $cacheability = new CacheableMetadata();
$cacheability->setCacheContexts(['route']); $cacheability->setCacheContexts(['route']);
$context = new Context($context_definition, $value);
$context->addCacheableDependency($cacheability); $context->addCacheableDependency($cacheability);
$result['node'] = $context; $result['node'] = $context;
......
...@@ -9,6 +9,7 @@ ...@@ -9,6 +9,7 @@
use Drupal\Component\Plugin\Exception\ContextException; use Drupal\Component\Plugin\Exception\ContextException;
use Drupal\Core\Plugin\Context\ContextDefinition; use Drupal\Core\Plugin\Context\ContextDefinition;
use Drupal\node\Entity\NodeType;
use Drupal\plugin_test\Plugin\MockBlockManager; use Drupal\plugin_test\Plugin\MockBlockManager;
use Drupal\simpletest\KernelTestBase; use Drupal\simpletest\KernelTestBase;
...@@ -26,6 +27,10 @@ class ContextPluginTest extends KernelTestBase { ...@@ -26,6 +27,10 @@ class ContextPluginTest extends KernelTestBase {
*/ */
function testContext() { function testContext() {
$this->installEntitySchema('user'); $this->installEntitySchema('user');
$this->installEntitySchema('node');
$this->installEntitySchema('node_type');
$type = NodeType::create(['type' => 'page', 'name' => 'Page']);
$type->save();
$name = $this->randomMachineName(); $name = $this->randomMachineName();
$manager = new MockBlockManager(); $manager = new MockBlockManager();
......
...@@ -72,7 +72,7 @@ protected function doTestIdenticalUser() { ...@@ -72,7 +72,7 @@ protected function doTestIdenticalUser() {
'user2' => 'anonymous', 'user2' => 'anonymous',
]); ]);
$definition = new ContextDefinition('entity:user'); $definition = new ContextDefinition('entity:user');
$contexts['anonymous'] = (new Context($definition))->setContextValue($this->anonymous); $contexts['anonymous'] = new Context($definition, $this->anonymous);
\Drupal::service('context.handler')->applyContextMapping($condition, $contexts); \Drupal::service('context.handler')->applyContextMapping($condition, $contexts);
$this->assertTrue($condition->execute()); $this->assertTrue($condition->execute());
} }
...@@ -89,8 +89,8 @@ protected function doTestDifferentUser() { ...@@ -89,8 +89,8 @@ protected function doTestDifferentUser() {
'user2' => 'authenticated', 'user2' => 'authenticated',
]); ]);
$definition = new ContextDefinition('entity:user'); $definition = new ContextDefinition('entity:user');
$contexts['anonymous'] = (new Context($definition))->setContextValue($this->anonymous); $contexts['anonymous'] = new Context($definition, $this->anonymous);
$contexts['authenticated'] = (new Context($definition))->setContextValue($this->authenticated); $contexts['authenticated'] = new Context($definition, $this->authenticated);
\Drupal::service('context.handler')->applyContextMapping($condition, $contexts); \Drupal::service('context.handler')->applyContextMapping($condition, $contexts);
$this->assertFalse($condition->execute()); $this->assertFalse($condition->execute());
} }
......
...@@ -10,6 +10,7 @@ ...@@ -10,6 +10,7 @@
use Drupal\Core\Plugin\Context\Context; use Drupal\Core\Plugin\Context\Context;
use Drupal\Core\Plugin\Context\ContextDefinition; use Drupal\Core\Plugin\Context\ContextDefinition;
use Drupal\node\Entity\Node; use Drupal\node\Entity\Node;
use Drupal\node\Entity\NodeType;
use Drupal\simpletest\KernelTestBase; use Drupal\simpletest\KernelTestBase;
/** /**
...@@ -58,6 +59,7 @@ protected function testContextNoValue() { ...@@ -58,6 +59,7 @@ protected function testContextNoValue() {
* Tests with both contexts mapped to the same user. * Tests with both contexts mapped to the same user.
*/ */
protected function testContextAvailable() { protected function testContextAvailable() {
NodeType::create(['type' => 'example', 'name' => 'Example'])->save();
/** @var \Drupal\Core\Condition\ConditionPluginBase $condition */ /** @var \Drupal\Core\Condition\ConditionPluginBase $condition */
$condition = \Drupal::service('plugin.manager.condition') $condition = \Drupal::service('plugin.manager.condition')
->createInstance('condition_test_optional_context') ->createInstance('condition_test_optional_context')
...@@ -66,7 +68,7 @@ protected function testContextAvailable() { ...@@ -66,7 +68,7 @@ protected function testContextAvailable() {
]); ]);
$definition = new ContextDefinition('entity:node'); $definition = new ContextDefinition('entity:node');
$node = Node::create(['type' => 'example']); $node = Node::create(['type' => 'example']);
$contexts['node'] = (new Context($definition))->setContextValue($node); $contexts['node'] = new Context($definition, $node);
\Drupal::service('context.handler')->applyContextMapping($condition, $contexts); \Drupal::service('context.handler')->applyContextMapping($condition, $contexts);
$this->assertFalse($condition->execute()); $this->assertFalse($condition->execute());
} }
......
...@@ -29,8 +29,7 @@ public function onSelectPageDisplayVariant(PageDisplayVariantSelectionEvent $eve ...@@ -29,8 +29,7 @@ public function onSelectPageDisplayVariant(PageDisplayVariantSelectionEvent $eve
$event->setPluginConfiguration(['required_configuration' => 'A very important, required value.']); $event->setPluginConfiguration(['required_configuration' => 'A very important, required value.']);
$event->addCacheTags(['custom_cache_tag']); $event->addCacheTags(['custom_cache_tag']);
$context = new Context(new ContextDefinition('string', NULL, TRUE)); $context = new Context(new ContextDefinition('string', NULL, TRUE), 'Explicitly passed in context.');
$context->setContextValue('Explicitly passed in context.');
$event->setContexts(['context' => $context]); $event->setContexts(['context' => $context]);
} }
......
...@@ -55,8 +55,7 @@ public function __construct(AccountInterface $account, EntityManagerInterface $e ...@@ -55,8 +55,7 @@ public function __construct(AccountInterface $account, EntityManagerInterface $e
public function getRuntimeContexts(array $unqualified_context_ids) { public function getRuntimeContexts(array $unqualified_context_ids) {
$current_user = $this->userStorage->load($this->account->id()); $current_user = $this->userStorage->load($this->account->id());
$context = new Context(new ContextDefinition('entity:user', $this->t('Current user'))); $context = new Context(new ContextDefinition('entity:user', $this->t('Current user')), $current_user);
$context->setContextValue($current_user);
$cacheability = new CacheableMetadata(); $cacheability = new CacheableMetadata();
$cacheability->setCacheContexts(['user']); $cacheability->setCacheContexts(['user']);
$context->addCacheableDependency($cacheability); $context->addCacheableDependency($cacheability);
......
...@@ -83,10 +83,8 @@ public function testSetContextValueTypedData() { ...@@ -83,10 +83,8 @@ public function testSetContextValueTypedData() {
->setMethods(array('getDefaultValue', 'getDataDefinition')) ->setMethods(array('getDefaultValue', 'getDataDefinition'))
->getMockForAbstractClass(); ->getMockForAbstractClass();
$context = new Context($this->contextDefinition);
$context->setTypedDataManager($this->typedDataManager);
$typed_data = $this->getMock('Drupal\Core\TypedData\TypedDataInterface'); $typed_data = $this->getMock('Drupal\Core\TypedData\TypedDataInterface');
$context->setContextValue($typed_data); $context = new Context($this->contextDefinition, $typed_data);
$this->assertSame($typed_data, $context->getContextData()); $this->assertSame($typed_data, $context->getContextData());
} }
...@@ -120,7 +118,7 @@ public function testSetContextValueCacheableDependency() { ...@@ -120,7 +118,7 @@ public function testSetContextValueCacheableDependency() {
->method('getCacheMaxAge') ->method('getCacheMaxAge')
->willReturn(60); ->willReturn(60);
$context->setContextValue($cacheable_dependency); $context = Context::createFromContext($context, $cacheable_dependency);
$this->assertSame($cacheable_dependency, $context->getContextData()); $this->assertSame($cacheable_dependency, $context->getContextData());
$this->assertEquals(['node:1'], $context->getCacheTags()); $this->assertEquals(['node:1'], $context->getCacheTags());
$this->assertEquals(['route'], $context->getCacheContexts()); $this->assertEquals(['route'], $context->getCacheContexts());
......
...@@ -50,11 +50,10 @@ public function testGetContextValue() { ...@@ -50,11 +50,10 @@ public function testGetContextValue() {
\Drupal::setContainer($container); \Drupal::setContainer($container);