function TokenIntegrationTest::testGlobalContextVariableTokens

Tests that global context variable tokens are replaced correctly.

File

tests/src/Kernel/TokenIntegrationTest.php, line 48

Class

TokenIntegrationTest
Test using the Rules API with the placeholder token replacement system.

Namespace

Drupal\Tests\rules\Kernel

Code

public function testGlobalContextVariableTokens() {
    // Configure a simple rule with one action and token replacements enabled.
    $action = $this->expressionManager
        ->createInstance('rules_action', ContextConfig::create()->setValue('message', "The date is {{ @rules.current_date_context:current_date | format_date('custom', 'Y-m') }}!")
        ->setValue('type', 'status')
        ->process('message', 'rules_tokens')
        ->setConfigKey('action_id', 'rules_system_message')
        ->toArray());
    $rule = $this->expressionManager
        ->createRule();
    $rule->addExpressionObject($action);
    RulesComponent::create($rule)->execute();
    $messages = $this->messenger
        ->all();
    
    /** @var \Drupal\Core\Datetime\DateFormatterInterface $date_formatter */
    $date_formatter = $this->container
        ->get('date.formatter');
    $date = $date_formatter->format($this->time
        ->getRequestTime(), 'custom', 'Y-m');
    $this->assertEquals("The date is {$date}!", (string) $messages[MessengerInterface::TYPE_STATUS][0]);
}