function CoreIntegrationTest::testDataSetEntities

Tests that the data set action works on entities.

File

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

Class

CoreIntegrationTest
Test using Drupal core integration of Rules API.

Namespace

Drupal\Tests\rules\Kernel

Code

public function testDataSetEntities() {
    $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',
    ]);
    // Configure a simple rule with one action.
    $action = $this->expressionManager
        ->createInstance('rules_action', ContextConfig::create()->setConfigKey('action_id', 'rules_data_set')
        ->map('data', 'node.title')
        ->map('value', 'new_title')
        ->toArray());
    $rule = $this->expressionManager
        ->createRule()
        ->addExpressionObject($action);
    RulesComponent::create($rule)->addContextDefinition('node', ContextDefinition::create('entity:node'))
        ->addContextDefinition('new_title', ContextDefinition::create('string'))
        ->setContextValue('node', $node)
        ->setContextValue('new_title', 'new title')
        ->execute();
    $this->assertEquals('new title', $node->getTitle());
    $this->assertNotNull($node->id(), 'Node ID is set, which means that the node has been auto-saved.');
}