RulesEngineTest.php in Rules 8.3
File
tests/src/Kernel/RulesEngineTest.php
View source
<?php
namespace Drupal\Tests\rules\Kernel;
use Drupal\rules\Core\ConditionManager;
use Drupal\rules\Context\ContextConfig;
use Drupal\rules\Context\ContextDefinition;
use Drupal\rules\Context\ExecutionState;
use Drupal\rules\Engine\RulesComponent;
class RulesEngineTest extends RulesKernelTestBase {
protected function setUp() : void {
parent::setUp();
$this
->installEntitySchema('user');
$this->container
->get('module_handler')
->loadInclude('user', 'install');
user_install();
}
public function testRuleCreation() {
$and = $this->expressionManager
->createAnd()
->addCondition('rules_test_false')
->addCondition('rules_test_true', ContextConfig::create()
->negateResult())
->negate();
$this
->assertTrue($and
->execute());
$or = $this->expressionManager
->createOr()
->addCondition('rules_test_true', ContextConfig::create()
->negateResult())
->addCondition('rules_test_false')
->addExpressionObject($and);
$this
->assertTrue($or
->execute());
$rule = $this->expressionManager
->createRule();
$rule
->addCondition('rules_test_true')
->addCondition('rules_test_true')
->addExpressionObject($or);
$this
->assertTrue($rule
->getConditions()
->execute());
$rule
->addAction('rules_test_debug_log');
$rule
->execute();
$this
->assertRulesDebugLogEntryExists('action called');
}
public function testContextPassing() {
$rule = $this->expressionManager
->createRule();
$rule
->addCondition('rules_test_string_condition', ContextConfig::create()
->map('text', 'test'));
$rule
->addAction('rules_test_debug_log');
RulesComponent::create($rule)
->addContextDefinition('test', ContextDefinition::create('string'))
->setContextValue('test', 'test value')
->execute();
$this
->assertRulesDebugLogEntryExists('action called');
}
public function testProvidedVariables() {
$rule = $this->expressionManager
->createRule();
$rule
->addCondition('rules_test_provider');
$rule
->addCondition('rules_test_string_condition', ContextConfig::create()
->map('text', 'provided_text'));
$rule
->addAction('rules_test_debug_log');
$component = RulesComponent::create($rule);
$violations = $component
->checkIntegrity();
$this
->assertEquals(0, iterator_count($violations));
$component
->execute();
$this
->assertRulesDebugLogEntryExists('action called');
}
public function testRenamingOfProvidedVariables() {
$rule = $this->expressionManager
->createRule();
$rule
->addCondition('rules_test_provider', ContextConfig::create()
->provideAs('provided_text', 'newname'));
$state = ExecutionState::create();
$rule
->executeWithState($state);
$variable = $state
->getVariable('newname');
$this
->assertEquals($variable
->getValue(), 'test value');
}
public function testActionProvidedContext() {
$rule = $this->expressionManager
->createRule();
$rule
->addCondition('rules_test_provider');
$rule
->addAction('rules_test_string', ContextConfig::create()
->map('text', 'provided_text'));
$rule
->addAction('rules_test_string', ContextConfig::create()
->map('text', 'concatenated')
->provideAs('concatenated', 'concatenated2'));
$state = ExecutionState::create();
$rule
->executeWithState($state);
$concatenated = $state
->getVariable('concatenated');
$this
->assertEquals($concatenated
->getValue(), 'test valuetest value');
$concatenated2 = $state
->getVariable('concatenated2');
$this
->assertEquals($concatenated2
->getValue(), 'test valuetest valuetest valuetest value');
}
public function testSwappedCoreServices() {
$condition_manager = $this->container
->get('plugin.manager.condition');
$this
->assertTrue($condition_manager instanceof ConditionManager);
}
}