You are here

public function RulesComponentActionTest::testAutosaveOnlyOnce in Rules 8.3

Tests that auto saving is only triggered once with nested components.

File

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

Class

RulesComponentActionTest
Tests for exposing Rules components as action plugins.

Namespace

Drupal\Tests\rules\Unit\Integration\RulesAction

Code

public function testAutosaveOnlyOnce() {
  $entity = $this
    ->prophesizeEntity(EntityInterface::class);
  $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,
  ]);

  // Create a rule with a nested rule. Overall there are 2 actions to set the
  // entity then.
  $rule = $this->rulesExpressionManager
    ->createRule();
  $rule
    ->addAction('rules_component:test_rule', ContextConfig::create()
    ->map('entity', 'entity'));
  $rule
    ->addAction('rules_entity_save', ContextConfig::create()
    ->map('entity', 'entity'));

  // Auto-saving should only be triggered once on the entity.
  $entity
    ->save()
    ->shouldBeCalledTimes(1);
  RulesComponent::create($rule)
    ->addContextDefinition('entity', ContextDefinition::create('entity'))
    ->setContextValue('entity', $entity
    ->reveal())
    ->execute();
}