function LoopTest::testPropertyPathList

Tests that a list can be chosen with a property path selector.

File

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

Class

LoopTest
Test Rules execution with the loop plugin.

Namespace

Drupal\Tests\rules\Unit\Integration\Engine

Code

public function testPropertyPathList() {
    $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' => 'node.field_text',
    ]);
    $loop->addAction('rules_data_set', ContextConfig::create()->map('data', 'result')
        ->setValue('value', '{{result}} {{list_item}}')
        ->process('value', 'rules_tokens'));
    $rule->addExpressionObject($loop);
    // Create a fake field for the fake node for testing.
    $list_definition = $this->typedDataManager
        ->createListDataDefinition('string');
    $field_text = new FieldItemList($list_definition);
    $field_text->setValue([
        'Hello',
        'world',
        'this',
        'is',
        'the',
        'loop',
    ]);
    $node = $this->prophesizeEntity(NodeInterface::class);
    $node->get('field_text')
        ->willReturn($field_text);
    // We cannot use EntityDataDefinitionInterface here because the context
    // system in core violates the interface and relies on the actual class.
    // @see https://www.drupal.org/node/2660216
    $node_definition = $this->prophesize(EntityDataDefinition::class);
    $node_definition->getPropertyDefinition("field_text")
        ->willReturn($list_definition);
    $context_definition = $this->getContextDefinitionFor('entity:node', $node_definition);
    $component = RulesComponent::create($rule)->addContextDefinition('node', $context_definition)
        ->provideContext('result')
        ->setContextValue('node', $node->reveal());
    $violations = $component->checkIntegrity();
    $this->assertCount(0, $violations);
    $result = $component->execute();
    $this->assertEquals(' Hello world this is the loop', $result['result']);
}