function CoreIntegrationTest::testTokenReplacements
Tests that tokens in action parameters get replaced.
File
-
tests/
src/ Kernel/ CoreIntegrationTest.php, line 121
Class
- CoreIntegrationTest
- Test using Drupal core integration of Rules API.
Namespace
Drupal\Tests\rules\KernelCode
public function testTokenReplacements() {
$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',
]);
$user = $entity_type_manager->getStorage('user')
->create([
'name' => 'klausi',
]);
$user->save();
$node->setOwner($user);
// Configure a simple rule with one action.
$action = $this->expressionManager
->createInstance('rules_action', ContextConfig::create()->map('message', 'message')
->map('type', 'type')
->process('message', 'rules_tokens')
->setConfigKey('action_id', 'rules_system_message')
->toArray());
$rule = $this->expressionManager
->createRule()
->addExpressionObject($action);
RulesComponent::create($rule)->addContextDefinition('node', ContextDefinition::create('entity:node'))
->addContextDefinition('message', ContextDefinition::create('string'))
->addContextDefinition('type', ContextDefinition::create('string'))
->setContextValue('node', $node)
->setContextValue('message', 'Hello {{ node.uid.entity.name.value }}!')
->setContextValue('type', 'status')
->execute();
$messages = $this->messenger
->all();
$this->assertEquals((string) $messages[MessengerInterface::TYPE_STATUS][0], 'Hello klausi!');
}