/** * @see \Drupal\rules\Context\ContextProviderInterface */ public function setProvidedValue($name, $value) { $context = $this->getProvidedContext($name); $new_context = Context::createFromContext($context, $value); $this->providedContext[$name] = $new_context; return $this; }
/** * Maps variables from rules state into the plugin context. * * @param \Drupal\Core\Plugin\ContextAwarePluginInterface $plugin * The plugin that is populated with context values. * @param \Drupal\rules\Engine\ExecutionStateInterface $state * The Rules state containing available variables. * * @throws \Drupal\rules\Exception\RulesEvaluationException * In case a required context is missing for the plugin. */ protected function mapContext(CoreContextAwarePluginInterface $plugin, ExecutionStateInterface $state) { $context_definitions = $plugin->getContextDefinitions(); foreach ($context_definitions as $name => $definition) { // Check if a data selector is configured that maps to the state. if (isset($this->configuration['context_mapping'][$name])) { $typed_data = $state->fetchDataByPropertyPath($this->configuration['context_mapping'][$name]); if ($typed_data->getValue() === NULL && !$definition->isAllowedNull()) { throw new RulesEvaluationException('The value of data selector ' . $this->configuration['context_mapping'][$name] . " is NULL, but the context {$name} in " . $plugin->getPluginId() . ' requires a value.'); } $context = $plugin->getContext($name); $new_context = Context::createFromContext($context, $typed_data); $plugin->setContext($name, $new_context); } elseif (isset($this->configuration['context_values']) && array_key_exists($name, $this->configuration['context_values'])) { if ($this->configuration['context_values'][$name] === NULL && !$definition->isAllowedNull()) { throw new RulesEvaluationException("The context value for {$name} is NULL, but the context {$name} in " . $plugin->getPluginId() . ' requires a value.'); } $context = $plugin->getContext($name); $new_context = Context::createFromContext($context, $this->configuration['context_values'][$name]); $plugin->setContext($name, $new_context); } elseif ($definition->isRequired()) { throw new RulesEvaluationException("Required context {$name} is missing for plugin " . $plugin->getPluginId() . '.'); } } }
/** * @covers ::setContextValue */ public function testSetContextValueCacheableDependency() { $container = new Container(); $cache_context_manager = $this->getMockBuilder('Drupal\\Core\\Cache\\CacheContextsManager')->disableOriginalConstructor()->getMock(); $container->set('cache_contexts_manager', $cache_context_manager); $cache_context_manager->expects($this->any())->method('validateTokens')->with(['route'])->willReturn(['route']); \Drupal::setContainer($container); $this->contextDefinition = $this->getMock('Drupal\\Core\\Plugin\\Context\\ContextDefinitionInterface'); $context = new Context($this->contextDefinition); $context->setTypedDataManager($this->typedDataManager); $cacheable_dependency = $this->getMock('Drupal\\Tests\\Core\\Plugin\\Context\\TypedDataCacheableDependencyInterface'); $cacheable_dependency->expects($this->once())->method('getCacheTags')->willReturn(['node:1']); $cacheable_dependency->expects($this->once())->method('getCacheContexts')->willReturn(['route']); $cacheable_dependency->expects($this->once())->method('getCacheMaxAge')->willReturn(60); $context = Context::createFromContext($context, $cacheable_dependency); $this->assertSame($cacheable_dependency, $context->getContextData()); $this->assertEquals(['node:1'], $context->getCacheTags()); $this->assertEquals(['route'], $context->getCacheContexts()); $this->assertEquals(60, $context->getCacheMaxAge()); }
/** * {@inheritdoc} */ public function setContextValue($name, $value) { $this->context[$name] = Context::createFromContext($this->getContext($name), $value); return $this; }
/** * Tests evaluating the condition. * * @covers ::evaluate */ public function testConditionEvaluation() { // Test a ComplexDataInterface object. $entity_adapter_empty = $this->prophesize(ComplexDataInterface::class); $entity_adapter_empty->isEmpty()->willReturn(TRUE)->shouldBeCalledTimes(1); $context = $this->condition->getContext('data'); $context = Context::createFromContext($context, $entity_adapter_empty->reveal()); $this->condition->setContext('data', $context); $this->assertTrue($this->condition->evaluate()); $entity_adapter_full = $this->prophesize(ComplexDataInterface::class); $entity_adapter_full->isEmpty()->willReturn(FALSE)->shouldBeCalledTimes(1); $context = Context::createFromContext($context, $entity_adapter_full->reveal()); $this->condition->setContext('data', $context); $this->assertFalse($this->condition->evaluate()); // These should all return FALSE. // A non-empty array. $context = Context::createFromContext($context, $this->getTypedData('list', [1, 2, 3])); $this->condition->setContext('data', $context); $this->assertFalse($this->condition->evaluate()); // An array containing an empty list. $context = Context::createFromContext($context, $this->getTypedData('list', [[]])); $this->condition->setContext('data', $context); $this->assertFalse($this->condition->evaluate()); // An array with a zero-value element. $context = Context::createFromContext($context, $this->getTypedData('list', [0])); $this->condition->setContext('data', $context); $this->assertFalse($this->condition->evaluate()); // A scalar value. $context = Context::createFromContext($context, $this->getTypedData('integer', 1)); $this->condition->setContext('data', $context); $this->assertFalse($this->condition->evaluate()); $context = Context::createFromContext($context, $this->getTypedData('string', 'short string')); $this->condition->setContext('data', $context); $this->assertFalse($this->condition->evaluate()); // These should all return TRUE. // An empty array. $context = Context::createFromContext($context, $this->getTypedData('list', [])); $this->condition->setContext('data', $context); $this->assertTrue($this->condition->evaluate()); // The false/zero/NULL values. $context = Context::createFromContext($context, $this->getTypedData('boolean', FALSE)); $this->condition->setContext('data', $context); $this->assertTrue($this->condition->evaluate()); $context = Context::createFromContext($context, $this->getTypedData('integer', 0)); $this->condition->setContext('data', $context); $this->assertTrue($this->condition->evaluate()); $context = Context::createFromContext($context, $this->getTypedData('string', NULL)); $this->condition->setContext('data', $context); $this->assertTrue($this->condition->evaluate()); // An empty string. $context = Context::createFromContext($context, $this->getTypedData('string', '')); $this->condition->setContext('data', $context); $this->assertTrue($this->condition->evaluate()); }