You are here

public function RulesComponentActionTest::testExecute in Rules 8.3

Tests that the execution of the action invokes the Rules component.

File

tests/src/Unit/Integration/RulesAction/RulesComponentActionTest.php, line 42

Class

RulesComponentActionTest
Tests for exposing Rules components as action plugins.

Namespace

Drupal\Tests\rules\Unit\Integration\RulesAction

Code

public function testExecute() {

  // Set up a rules component that will just save an entity.
  $nested_rule = $this->rulesExpressionManager
    ->createRule();
  $nested_rule
    ->addAction('rules_entity_save', ContextConfig::create()
    ->map('entity', 'entity'));
  $rules_config = new RulesComponentConfig([
    'id' => 'test_rule',
    'label' => 'Test rule',
  ], 'rules_component');
  $rules_config
    ->setExpression($nested_rule);
  $rules_config
    ->setContextDefinitions([
    'entity' => ContextDefinition::create('entity'),
  ]);
  $this
    ->prophesizeStorage([
    $rules_config,
  ]);

  // Invoke the rules component in another rule.
  $rule = $this->rulesExpressionManager
    ->createRule();
  $rule
    ->addAction('rules_component:test_rule', ContextConfig::create()
    ->map('entity', 'entity'));

  // The call to save the entity means that the action was executed.
  $entity = $this
    ->prophesizeEntity(EntityInterface::class);
  $entity
    ->save()
    ->shouldBeCalledTimes(1);
  RulesComponent::create($rule)
    ->addContextDefinition('entity', ContextDefinition::create('entity'))
    ->setContextValue('entity', $entity
    ->reveal())
    ->execute();
}