/** * {@inheritdoc} */ public function setUp() { parent::setUp(); $mock_data_definition = $this->getMock('Drupal\\Core\\TypedData\\DataDefinitionInterface'); $this->contextDefinition = $this->getMockBuilder('Drupal\\Core\\Plugin\\Context\\ContextDefinitionInterface')->setMethods(array('getDefaultValue', 'getDataDefinition'))->getMockForAbstractClass(); $this->contextDefinition->expects($this->once())->method('getDefaultValue')->willReturn('test'); $this->contextDefinition->expects($this->once())->method('getDataDefinition')->willReturn($mock_data_definition); $this->typedData = $this->getMock('Drupal\\Core\\TypedData\\TypedDataInterface'); $this->typedDataManager = $this->getMockBuilder('Drupal\\Core\\TypedData\\TypedDataManager')->disableOriginalConstructor()->setMethods(array('create'))->getMock(); $this->typedDataManager->expects($this->once())->method('create')->with($mock_data_definition, 'test')->willReturn($this->typedData); }
/** * Set up mocks for the getDefaultValue() method call. * * @param mixed $default_value * The default value to assign to the mock context definition. */ protected function setUpDefaultValue($default_value = NULL) { $mock_data_definition = $this->getMock('Drupal\\Core\\TypedData\\DataDefinitionInterface'); $this->contextDefinition = $this->getMockBuilder('Drupal\\Core\\Plugin\\Context\\ContextDefinitionInterface')->setMethods(array('getDefaultValue', 'getDataDefinition'))->getMockForAbstractClass(); $this->contextDefinition->expects($this->once())->method('getDefaultValue')->willReturn($default_value); $this->contextDefinition->expects($this->once())->method('getDataDefinition')->willReturn($mock_data_definition); $this->typedData = $this->getMock('Drupal\\Core\\TypedData\\TypedDataInterface'); $this->typedDataManager->expects($this->once())->method('create')->with($mock_data_definition, $default_value)->willReturn($this->typedData); }
/** * {@inheritdoc} */ public function getMatchingContexts(array $contexts, ContextDefinitionInterface $definition) { return array_filter($contexts, function (ContextInterface $context) use($definition) { $context_definition = $context->getContextDefinition(); // If the data types do not match, this context is invalid unless the // expected data type is any, which means all data types are supported. if ($definition->getDataType() != 'any' && $definition->getDataType() != $context_definition->getDataType()) { return FALSE; } // If any constraint does not match, this context is invalid. foreach ($definition->getConstraints() as $constraint_name => $constraint) { if ($context_definition->getConstraint($constraint_name) != $constraint) { return FALSE; } } // All contexts with matching data type and contexts are valid. return TRUE; }); }
/** * {@inheritdoc} */ public function getConstraints() { return $this->contextDefinition->getConstraints(); }
/** * Checks that the data type of a mapped variable matches the expectation. * * @param \Drupal\Core\Plugin\Context\ContextDefinitionInterface $context_definition * The context definition of the context on the plugin. * @param \Drupal\Core\TypedData\DataDefinitionInterface $provided * The data definition of the mapped variable to the context. * @param string $context_name * The name of the context on the plugin. * @param \Drupal\rules\Engine\IntegrityViolationList $violation_list * The list of violations where new ones will be added. */ protected function checkDataTypeCompatible(ContextDefinitionInterface $context_definition, DataDefinitionInterface $provided, $context_name, IntegrityViolationList $violation_list) { $expected_class = $context_definition->getDataDefinition()->getClass(); $provided_class = $provided->getClass(); $expected_type_problem = NULL; if (is_subclass_of($expected_class, PrimitiveInterface::class) && !is_subclass_of($provided_class, PrimitiveInterface::class)) { $expected_type_problem = $this->t('primitive'); } elseif (is_subclass_of($expected_class, ListInterface::class) && !is_subclass_of($provided_class, ListInterface::class)) { $expected_type_problem = $this->t('list'); } elseif (is_subclass_of($expected_class, ComplexDataInterface::class) && !is_subclass_of($provided_class, ComplexDataInterface::class)) { $expected_type_problem = $this->t('complex'); } if ($expected_type_problem) { $violation = new IntegrityViolation(); $violation->setMessage($this->t('Expected a @expected_type data type for context %context_name but got a @provided_type data type instead.', ['@expected_type' => $expected_type_problem, '%context_name' => $context_definition->getLabel(), '@provided_type' => $provided->getDataType()])); $violation->setContextName($context_name); $violation_list->add($violation); } }
/** * Checks that the data type of a mapped variable matches the expectation. * * @param \Drupal\Core\Plugin\Context\ContextDefinitionInterface $context_definition * The context definition of the context on the plugin. * @param \Drupal\Core\TypedData\DataDefinitionInterface $provided * The data definition of the mapped variable to the context. * @param string $context_name * The name of the context on the plugin. * @param \Drupal\rules\Engine\IntegrityViolationList $violation_list * The list of violations where new ones will be added. */ protected function checkDataTypeCompatible(CoreContextDefinitionInterface $context_definition, DataDefinitionInterface $provided, $context_name, IntegrityViolationList $violation_list) { // Compare data types. For now, fail if they are not equal. // @todo: Add support for matching based upon type-inheritance. $target_type = $context_definition->getDataDefinition()->getDataType(); // Special case any and entity target types for now. if ($target_type == 'any' || $target_type == 'entity' && strpos($provided->getDataType(), 'entity:') !== FALSE) { return; } if ($target_type != $provided->getDataType()) { $expected_type_problem = $context_definition->getDataDefinition()->getDataType(); $violation = new IntegrityViolation(); $violation->setMessage($this->t('Expected a @expected_type data type for context %context_name but got a @provided_type data type instead.', ['@expected_type' => $expected_type_problem, '%context_name' => $context_definition->getLabel(), '@provided_type' => $provided->getDataType()])); $violation->setContextName($context_name); $violation->setUuid($this->getUuid()); $violation_list->add($violation); } }