ConfigEntityTest.php in Rules 8.3
File
tests/src/Kernel/ConfigEntityTest.php
View source
<?php
namespace Drupal\Tests\rules\Kernel;
use Drupal\rules\Context\ContextDefinition;
use Drupal\rules\Engine\RulesComponent;
use Drupal\rules\Plugin\RulesExpression\RuleExpression;
class ConfigEntityTest extends RulesKernelTestBase {
protected $storage;
protected function setUp() : void {
parent::setUp();
$this->storage = $this->container
->get('entity_type.manager')
->getStorage('rules_component');
}
public function testSavingEmptyRule() {
$this
->addToAssertionCount(1);
$rule = $this->expressionManager
->createRule();
$config_entity = $this->storage
->create([
'id' => 'test_rule',
])
->setExpression($rule);
$config_entity
->save();
}
public function testConfigAction() {
$action = $this->expressionManager
->createAction('rules_test_debug_log');
$config_entity = $this->storage
->create([
'id' => 'test_rule',
])
->setExpression($action);
$config_entity
->save();
$this->logger = $this->container
->get('logger.channel.rules_debug');
$this->logger
->addLogger($this->debugLog);
$loaded_entity = $this->storage
->load('test_rule');
$this
->assertEquals($action
->getConfiguration(), $loaded_entity
->get('component')['expression'], 'Action configuration is the same after loading the config.');
$expression = $loaded_entity
->getExpression();
$expression
->execute();
$this
->assertRulesDebugLogEntryExists('action called');
}
public function testConfigRule() {
$rule = $this->expressionManager
->createRule();
$rule
->addCondition('rules_test_true');
$rule
->addAction('rules_test_debug_log');
$config_entity = $this->storage
->create([
'id' => 'test_rule',
])
->setExpression($rule);
$config_entity
->save();
$loaded_entity = $this->storage
->load('test_rule');
$expression = $loaded_entity
->getExpression();
$expression
->execute();
$this
->assertRulesDebugLogEntryExists('action called');
}
public function testContextDefinitionExport() {
$component = RulesComponent::create($this->expressionManager
->createRule())
->addContextDefinition('test', ContextDefinition::create('string')
->setLabel('Test string'));
$config_entity = $this->storage
->create([
'id' => 'test_rule',
])
->updateFromComponent($component);
$config_entity
->save();
$loaded_entity = $this->storage
->load('test_rule');
$expression = $loaded_entity
->getExpression();
$this
->assertInstanceOf(RuleExpression::class, $expression);
$context_definitions = $loaded_entity
->getContextDefinitions();
$this
->assertEquals($context_definitions['test']
->getDataType(), 'string', 'Data type of context definition is correct.');
$this
->assertEquals($context_definitions['test']
->getLabel(), 'Test string', 'Label of context definition is correct.');
}
public function testReactionRuleSaving() {
$this
->addToAssertionCount(1);
$rule = $this->expressionManager
->createRule();
$storage = $this->container
->get('entity_type.manager')
->getStorage('rules_reaction_rule');
$config_entity = $storage
->create([
'id' => 'test_rule',
])
->setExpression($rule);
$config_entity
->save();
}
}