You are here

public function CoreIntegrationTest::testEntityAutoSave in Rules 8.3

Tests that an entity is automatically saved after being changed.

File

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

Class

CoreIntegrationTest
Test using Drupal core integration of Rules API.

Namespace

Drupal\Tests\rules\Kernel

Code

public function testEntityAutoSave() {
  $entity_type_manager = $this->container
    ->get('entity_type.manager');
  $entity_type_manager
    ->getStorage('node_type')
    ->create([
    'type' => 'page',
  ])
    ->save();
  $node = $entity_type_manager
    ->getStorage('node')
    ->create([
    'title' => 'test',
    'type' => 'page',
  ]);

  // We use the rules_test_node action plugin which marks its node context for
  // auto saving.
  // @see \Drupal\rules_test\Plugin\RulesAction\TestNodeAction
  $action = $this->expressionManager
    ->createAction('rules_test_node')
    ->setConfiguration(ContextConfig::create()
    ->map('node', 'node')
    ->map('title', 'title')
    ->toArray());
  RulesComponent::create($action)
    ->addContextDefinition('node', ContextDefinition::create('entity:node'))
    ->addContextDefinition('title', ContextDefinition::create('string'))
    ->setContextValue('node', $node)
    ->setContextValue('title', 'new title')
    ->execute();
  $this
    ->assertNotNull($node
    ->id(), 'Node ID is set, which means that the node has been saved.');
}