PrepareExecutionMetadataStateTest.php in Rules 8.3
File
tests/src/Unit/Integration/Engine/PrepareExecutionMetadataStateTest.php
View source
<?php
namespace Drupal\Tests\rules\Unit\Integration\Engine;
use Drupal\rules\Context\ContextConfig;
use Drupal\rules\Context\ContextDefinition;
use Drupal\rules\Context\ExecutionMetadataState;
use Drupal\rules\Engine\RulesComponent;
use Drupal\Tests\rules\Unit\Integration\RulesEntityIntegrationTestBase;
class PrepareExecutionMetadataStateTest extends RulesEntityIntegrationTestBase {
public function testAddingVariable() {
$rule = $this->rulesExpressionManager
->createRule();
$rule
->addAction('rules_variable_add', ContextConfig::create()
->setValue('type', 'string')
->setValue('value', '')
->provideAs('variable_added', 'result'));
$state = ExecutionMetadataState::create();
$found = $rule
->prepareExecutionMetadataState($state);
$this
->assertTrue($state
->hasDataDefinition('result'));
$this
->assertNull($found);
}
public function testPreparingUntil() {
$rule = $this->rulesExpressionManager
->createRule();
$rule
->addAction('rules_variable_add', ContextConfig::create()
->setValue('type', 'string')
->setValue('value', '')
->provideAs('variable_added', 'result1'));
$second_action = $this->rulesExpressionManager
->createAction('rules_variable_add')
->setConfiguration(ContextConfig::create()
->setValue('type', 'string')
->setValue('value', '')
->provideAs('variable_added', 'result2')
->toArray());
$rule
->addExpressionObject($second_action);
$state = ExecutionMetadataState::create();
$found = $rule
->prepareExecutionMetadataState($state, $second_action);
$this
->assertTrue($state
->hasDataDefinition('result1'));
$this
->assertFalse($state
->hasDataDefinition('result2'));
$this
->assertTrue($found);
}
public function testPrepareInLoop() {
$rule = $this->rulesExpressionManager
->createRule();
$loop = $this->rulesExpressionManager
->createInstance('rules_loop', [
'list' => 'string_list',
]);
$action = $this->rulesExpressionManager
->createAction('rules_test_string')
->setConfiguration(ContextConfig::create()
->setValue('text', 'x')
->toArray());
$loop
->addExpressionObject($action);
$rule
->addExpressionObject($loop);
$state = RulesComponent::create($rule)
->addContextDefinition('string_list', ContextDefinition::create('string')
->setMultiple())
->getMetadataState();
$found = $rule
->prepareExecutionMetadataState($state, $action);
$this
->assertTrue($state
->hasDataDefinition('list_item'));
$this
->assertTrue($found);
}
public function testPrepareAfterLoop() {
$rule = $this->rulesExpressionManager
->createRule();
$loop = $this->rulesExpressionManager
->createInstance('rules_loop', [
'list' => 'string_list',
]);
$action = $this->rulesExpressionManager
->createAction('rules_test_string')
->setConfiguration(ContextConfig::create()
->setValue('text', 'x')
->toArray());
$loop
->addExpressionObject($action);
$rule
->addExpressionObject($loop);
$state = RulesComponent::create($rule)
->addContextDefinition('string_list', ContextDefinition::create('string')
->setMultiple())
->getMetadataState();
$found = $rule
->prepareExecutionMetadataState($state);
$this
->assertFalse($state
->hasDataDefinition('list_item'));
$this
->assertNull($found);
}
}