You are here

public function LoopTest::testListItemUsage in Rules 8.3

Tests that list items in the loop can be used during execution.

File

tests/src/Unit/Integration/Engine/LoopTest.php, line 32

Class

LoopTest
Test Rules execution with the loop plugin.

Namespace

Drupal\Tests\rules\Unit\Integration\Engine

Code

public function testListItemUsage() {

  // The rule contains a list of strings that will be concatenated into one
  // variable.
  $rule = $this->rulesExpressionManager
    ->createRule();
  $rule
    ->addAction('rules_variable_add', ContextConfig::create()
    ->setValue('type', 'string')
    ->setValue('value', '')
    ->provideAs('variable_added', 'result'));
  $loop = $this->rulesExpressionManager
    ->createInstance('rules_loop', [
    'list' => 'string_list',
  ]);
  $loop
    ->addAction('rules_data_set', ContextConfig::create()
    ->map('data', 'result')
    ->setValue('value', '{{result}} {{list_item}}')
    ->process('value', 'rules_tokens'));
  $rule
    ->addExpressionObject($loop);
  $result = RulesComponent::create($rule)
    ->addContextDefinition('string_list', ContextDefinition::create('string')
    ->setMultiple())
    ->provideContext('result')
    ->setContextValue('string_list', [
    'Hello',
    'world',
    'this',
    'is',
    'the',
    'loop',
  ])
    ->execute();
  $this
    ->assertEquals(' Hello world this is the loop', $result['result']);
}