TokenIntegrationTest.php in Rules 8.3
File
tests/src/Kernel/TokenIntegrationTest.php
View source
<?php
namespace Drupal\Tests\rules\Kernel;
use Drupal\Core\Messenger\MessengerInterface;
use Drupal\rules\Context\ContextConfig;
use Drupal\rules\Context\ContextDefinition;
use Drupal\rules\Engine\RulesComponent;
class TokenIntegrationTest extends RulesKernelTestBase {
public function testSystemDateToken() {
$action = $this->expressionManager
->createInstance('rules_action', ContextConfig::create()
->setValue('message', "The date is {{ 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)
->addContextDefinition('date', ContextDefinition::create('timestamp'))
->setContextValue('date', $this->time
->getRequestTime())
->execute();
$messages = $this->messenger
->all();
$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]);
}
public function testGlobalContextVariableTokens() {
$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();
$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]);
}
}