ContextIntegrationTest.php in Rules 8.3
File
tests/src/Kernel/ContextIntegrationTest.php
View source
<?php
namespace Drupal\Tests\rules\Kernel;
use Drupal\rules\Context\ContextConfig;
use Drupal\rules\Context\ContextDefinition;
use Drupal\rules\Engine\RulesComponent;
use Drupal\rules\Exception\EvaluationException;
class ContextIntegrationTest extends RulesKernelTestBase {
public function testRequiredNullMapping() {
$action = $this->expressionManager
->createInstance('rules_action', ContextConfig::create()
->setConfigKey('action_id', 'rules_test_string')
->map('text', 'null_context')
->toArray());
$rule = $this->expressionManager
->createRule()
->addExpressionObject($action);
$component = RulesComponent::create($rule)
->addContextDefinition('null_context', ContextDefinition::create('string'))
->setContextValue('null_context', NULL);
try {
$component
->execute();
$this
->fail('No exception thrown when required context value is NULL');
} catch (EvaluationException $e) {
$this
->assertTrue(TRUE, 'Exception thrown as expected when a required context is NULL');
}
}
public function testRequiredNullValue() {
$action = $this->expressionManager
->createInstance('rules_action', ContextConfig::create()
->setConfigKey('action_id', 'rules_test_string')
->setValue('text', NULL)
->toArray());
$rule = $this->expressionManager
->createRule();
$rule
->addExpressionObject($action);
try {
$rule
->execute();
$this
->fail('No exception thrown when required context value is NULL');
} catch (EvaluationException $e) {
$this
->assertTrue(TRUE, 'Exception thrown as expected when a required context is NULL');
}
}
public function testAllowNullValue() {
$action = $this->expressionManager
->createInstance('rules_action', ContextConfig::create()
->setConfigKey('action_id', 'rules_data_set')
->map('data', 'null_variable')
->map('value', 'new_value')
->toArray());
$rule = $this->expressionManager
->createRule()
->addExpressionObject($action);
$component = RulesComponent::create($rule)
->addContextDefinition('null_variable', ContextDefinition::create('string'))
->addContextDefinition('new_value', ContextDefinition::create('string'))
->setContextValue('null_variable', NULL)
->setContextValue('new_value', 'new value');
$component
->execute();
$this
->assertEquals('new value', $component
->getState()
->getVariableValue('null_variable'));
}
public function testAssignmentRestriction() {
$action_manager = $this->container
->get('plugin.manager.rules_action');
$entity_fetch_action = $action_manager
->createInstance('rules_entity_fetch_by_id');
$context_definition = $entity_fetch_action
->getContextDefinition('type');
$this
->assertEquals(ContextDefinition::ASSIGNMENT_RESTRICTION_INPUT, $context_definition
->getAssignmentRestriction());
$entity_delete_action = $action_manager
->createInstance('rules_entity_delete');
$context_definition = $entity_delete_action
->getContextDefinition('entity');
$this
->assertEquals(ContextDefinition::ASSIGNMENT_RESTRICTION_SELECTOR, $context_definition
->getAssignmentRestriction());
}
}