You are here

public function LoopTest::testListItemRenaming in Rules 8.3

Tests that list items can be renamed for usage in nested loops.

File

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

Class

LoopTest
Test Rules execution with the loop plugin.

Namespace

Drupal\Tests\rules\Unit\Integration\Engine

Code

public function testListItemRenaming() {

  // 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'));
  $outer_loop = $this->rulesExpressionManager
    ->createInstance('rules_loop', [
    'list' => 'outer_list',
    'list_item' => 'outer_item',
  ]);
  $outer_loop
    ->addAction('rules_data_set', ContextConfig::create()
    ->map('data', 'result')
    ->setValue('value', '{{result}} {{outer_item}}')
    ->process('value', 'rules_tokens'));
  $inner_loop = $this->rulesExpressionManager
    ->createInstance('rules_loop', [
    'list' => 'inner_list',
    'list_item' => 'inner_item',
  ]);
  $inner_loop
    ->addAction('rules_data_set', ContextConfig::create()
    ->map('data', 'result')
    ->setValue('value', '{{result}} {{inner_item}}')
    ->process('value', 'rules_tokens'));
  $outer_loop
    ->addExpressionObject($inner_loop);
  $rule
    ->addExpressionObject($outer_loop);
  $result = RulesComponent::create($rule)
    ->addContextDefinition('outer_list', ContextDefinition::create('string')
    ->setMultiple())
    ->addContextDefinition('inner_list', ContextDefinition::create('string')
    ->setMultiple())
    ->provideContext('result')
    ->setContextValue('outer_list', [
    'Outer 1',
    'Outer 2',
  ])
    ->setContextValue('inner_list', [
    'Inner 1',
    'Inner 2',
    'Inner 3',
  ])
    ->execute();
  $this
    ->assertEquals(' Outer 1 Inner 1 Inner 2 Inner 3 Outer 2 Inner 1 Inner 2 Inner 3', $result['result']);
}