You are here

public function CoreIntegrationTest::testComponentActionAutoSave in Rules 8.3

Tests that auto saving in a component executed as action works.

File

tests/src/Kernel/CoreIntegrationTest.php, line 251

Class

CoreIntegrationTest
Test using Drupal core integration of Rules API.

Namespace

Drupal\Tests\rules\Kernel

Code

public function testComponentActionAutoSave() {
  $entity_type_manager = $this->container
    ->get('entity_type.manager');
  $entity_type_manager
    ->getStorage('node_type')
    ->create([
    'type' => 'page',
  ])
    ->save();
  $nested_rule = $this->expressionManager
    ->createRule();

  // Create a node entity with the action.
  $nested_rule
    ->addAction('rules_entity_create:node', ContextConfig::create()
    ->setValue('type', 'page'));

  // Set the title of the new node so that it is marked for auto-saving.
  $nested_rule
    ->addAction('rules_data_set', ContextConfig::create()
    ->map('data', 'node_created.title')
    ->setValue('value', 'new title'));
  $rules_config = new RulesComponentConfig([
    'id' => 'test_rule',
    'label' => 'Test rule',
  ], 'rules_component');
  $rules_config
    ->setExpression($nested_rule);
  $rules_config
    ->save();

  // Invoke the rules component in another rule.
  $rule = $this->expressionManager
    ->createRule();
  $rule
    ->addAction('rules_component:test_rule');
  RulesComponent::create($rule)
    ->execute();
  $nodes = Node::loadMultiple();
  $node = reset($nodes);
  $this
    ->assertEquals('new title', $node
    ->getTitle());
  $this
    ->assertNotNull($node
    ->id(), 'Node ID is set, which means that the node has been auto-saved.');
}