View source
<?php
namespace Drupal\Tests\rules\Kernel;
use Drupal\rules\Context\ContextConfig;
use Drupal\user\Entity\User;
use Symfony\Component\HttpKernel\KernelEvents;
class EventIntegrationTest extends RulesKernelTestBase {
protected $storage;
protected static $modules = [
'field',
'node',
'text',
'user',
];
protected function setUp() : void {
parent::setUp();
$this->storage = $this->container
->get('entity_type.manager')
->getStorage('rules_reaction_rule');
$this
->installEntitySchema('user');
$this
->installEntitySchema('node');
$this
->installConfig([
'system',
]);
$this
->installConfig([
'field',
]);
$this
->installConfig([
'node',
]);
$this
->installSchema('node', [
'node_access',
]);
$this
->installSchema('system', [
'sequences',
]);
}
public function testUserLoginEvent() {
$rule = $this->expressionManager
->createRule();
$rule
->addCondition('rules_test_true');
$rule
->addAction('rules_test_debug_log', ContextConfig::create()
->map('message', 'account.name.0.value'));
$config_entity = $this->storage
->create([
'id' => 'test_rule',
'events' => [
[
'event_name' => 'rules_user_login',
],
],
'expression' => $rule
->getConfiguration(),
]);
$config_entity
->save();
$this->logger = $this->container
->get('logger.channel.rules_debug');
$this->logger
->addLogger($this->debugLog);
$account = User::create([
'name' => 'test_user',
]);
rules_user_login($account);
$this
->assertRulesDebugLogEntryExists('test_user');
}
public function testUserLogoutEvent() {
$rule = $this->expressionManager
->createRule();
$rule
->addCondition('rules_test_true');
$rule
->addAction('rules_test_debug_log');
$config_entity = $this->storage
->create([
'id' => 'test_rule',
'events' => [
[
'event_name' => 'rules_user_logout',
],
],
'expression' => $rule
->getConfiguration(),
]);
$config_entity
->save();
$this->logger = $this->container
->get('logger.channel.rules_debug');
$this->logger
->addLogger($this->debugLog);
$account = $this->container
->get('current_user');
rules_user_logout($account);
$this
->assertRulesDebugLogEntryExists('action called');
}
public function testCronEvent() {
$rule = $this->expressionManager
->createRule();
$rule
->addCondition('rules_test_true');
$rule
->addAction('rules_test_debug_log');
$config_entity = $this->storage
->create([
'id' => 'test_rule',
'events' => [
[
'event_name' => 'rules_system_cron',
],
],
'expression' => $rule
->getConfiguration(),
]);
$config_entity
->save();
$this->logger = $this->container
->get('logger.channel.rules_debug');
$this->logger
->addLogger($this->debugLog);
$this->container
->get('cron')
->run();
$this
->assertRulesDebugLogEntryExists('action called');
}
public function testSystemLoggerEvent() {
$rule = $this->expressionManager
->createRule();
$rule
->addCondition('rules_test_true');
$rule
->addAction('rules_test_debug_log');
$config_entity = $this->storage
->create([
'id' => 'test_rule',
'events' => [
[
'event_name' => 'rules_system_logger_event',
],
],
'expression' => $rule
->getConfiguration(),
]);
$config_entity
->save();
$this->logger = $this->container
->get('logger.channel.rules_debug');
$this->logger
->addLogger($this->debugLog);
$this->container
->get('logger.factory')
->get('rules_test')
->notice("This message must get logged and dispatched as rules_system_logger_event");
$this
->assertRulesDebugLogEntryExists('action called');
}
public function testInitEvent() {
$rule = $this->expressionManager
->createRule();
$rule
->addCondition('rules_test_true');
$rule
->addAction('rules_test_debug_log');
$config_entity = $this->storage
->create([
'id' => 'test_rule',
'events' => [
[
'event_name' => KernelEvents::REQUEST,
],
],
'expression' => $rule
->getConfiguration(),
]);
$config_entity
->save();
$this->logger = $this->container
->get('logger.channel.rules_debug');
$this->logger
->addLogger($this->debugLog);
$dispatcher = $this->container
->get('event_dispatcher');
$listeners = $dispatcher
->getListeners(KernelEvents::REQUEST);
foreach ($listeners as $listener) {
if (empty($listener[1]) || $listener[1] != 'onRulesEvent') {
$dispatcher
->removeListener(KernelEvents::REQUEST, $listener);
}
}
$dispatcher
->dispatch(KernelEvents::REQUEST);
$this
->assertRulesDebugLogEntryExists('action called');
}
public function testTerminateEvent() {
$rule = $this->expressionManager
->createRule();
$rule
->addCondition('rules_test_true');
$rule
->addAction('rules_test_debug_log');
$config_entity = $this->storage
->create([
'id' => 'test_rule',
'events' => [
[
'event_name' => KernelEvents::TERMINATE,
],
],
'expression' => $rule
->getConfiguration(),
]);
$config_entity
->save();
$this->logger = $this->container
->get('logger.channel.rules_debug');
$this->logger
->addLogger($this->debugLog);
$dispatcher = $this->container
->get('event_dispatcher');
$listeners = $dispatcher
->getListeners(KernelEvents::TERMINATE);
foreach ($listeners as $listener) {
if (empty($listener[1]) || $listener[1] != 'onRulesEvent') {
$dispatcher
->removeListener(KernelEvents::TERMINATE, $listener);
}
}
$dispatcher
->dispatch(KernelEvents::TERMINATE);
$this
->assertRulesDebugLogEntryExists('action called');
}
public function testMultipleEvents() {
$rule = $this->expressionManager
->createRule();
$rule
->addCondition('rules_test_true');
$rule
->addAction('rules_test_debug_log');
$config_entity = $this->storage
->create([
'id' => 'test_rule',
]);
$config_entity
->set('events', [
[
'event_name' => 'rules_user_login',
],
[
'event_name' => 'rules_user_logout',
],
]);
$config_entity
->set('expression', $rule
->getConfiguration());
$config_entity
->save();
$this->logger = $this->container
->get('logger.channel.rules_debug');
$this->logger
->addLogger($this->debugLog);
$account = User::create([
'name' => 'test_user',
]);
rules_user_login($account);
rules_user_logout($account);
$this
->assertRulesDebugLogEntryExists('action called');
$this
->assertRulesDebugLogEntryExists('action called', 1);
}
public function testEntityOriginal($event_name) {
$entity_type_manager = $this->container
->get('entity_type.manager');
$entity_type_manager
->getStorage('node_type')
->create([
'type' => 'page',
'display_submitted' => FALSE,
])
->save();
$node = $entity_type_manager
->getStorage('node')
->create([
'title' => 'test',
'type' => 'page',
]);
$node
->save();
$rule = $this->expressionManager
->createRule();
$rule
->addCondition('rules_data_comparison', ContextConfig::create()
->map('data', 'node.title.value')
->map('value', 'node_unchanged.title.value')
->negateResult());
$rule
->addAction('rules_test_debug_log');
$config_entity = $this->storage
->create([
'id' => 'test_rule',
'events' => [
[
'event_name' => $event_name,
],
],
'expression' => $rule
->getConfiguration(),
]);
$config_entity
->save();
$this->logger = $this->container
->get('logger.channel.rules_debug');
$this->logger
->addLogger($this->debugLog);
$node
->setTitle('new title');
$node
->save();
$this
->assertRulesDebugLogEntryExists('action called');
}
public function providerTestEntityOriginal() {
return [
[
'rules_entity_presave:node',
],
[
'rules_entity_update:node',
],
];
}
public function testBundleQualifiedEvents() {
$entity_type_manager = $this->container
->get('entity_type.manager');
$entity_type_manager
->getStorage('node_type')
->create([
'type' => 'article',
'title' => 'Article',
])
->save();
$entity_type_manager
->getStorage('node_type')
->create([
'type' => 'page',
'title' => 'Page',
])
->save();
$rule = $this->expressionManager
->createRule();
$rule
->addAction('rules_test_debug_log', ContextConfig::create()
->map('message', 'node.title.value'));
$rule = $this->expressionManager
->createRule();
$rule
->addAction('rules_test_debug_log', ContextConfig::create()
->map('message', 'node.title.value'));
$config_entity = $this->storage
->create([
'id' => 'test_article_rule',
'events' => [
[
'event_name' => 'rules_entity_insert:node--article',
],
],
'expression' => $rule
->getConfiguration(),
]);
$config_entity
->save();
$config_entity = $this->storage
->create([
'id' => 'test_page_rule',
'events' => [
[
'event_name' => 'rules_entity_insert:node--page',
],
],
'expression' => $rule
->getConfiguration(),
]);
$config_entity
->save();
$this->logger = $this->container
->get('logger.channel.rules_debug');
$this->logger
->addLogger($this->debugLog);
$node = $entity_type_manager
->getStorage('node')
->create([
'title' => 'Test page entity bundle event',
'type' => 'page',
]);
$node
->save();
$this
->assertRulesDebugLogEntryExists('Test page entity bundle event');
$this
->assertRulesDebugLogEntryNotExists('Test article entity bundle event');
}
}