function EventIntegrationTest::testInitEvent
Test that Drupal initializing triggers the Rules debug logger listener.
File
-
tests/
src/ Kernel/ EventIntegrationTest.php, line 159
Class
- EventIntegrationTest
- Test for the Symfony event mapping to Rules events.
Namespace
Drupal\Tests\rules\KernelCode
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();
// The logger instance has changed, refresh it.
$this->logger = $this->container
->get('logger.channel.rules_debug');
$this->logger
->addLogger($this->debugLog);
$dispatcher = $this->container
->get('event_dispatcher');
// Remove all the listeners except Rules before triggering an event.
$listeners = $dispatcher->getListeners(KernelEvents::REQUEST);
foreach ($listeners as $listener) {
if (empty($listener[1]) || $listener[1] != 'onRulesEvent') {
$dispatcher->removeListener(KernelEvents::REQUEST, $listener);
}
}
// Manually trigger the initialization event.
$event = new Event();
$dispatcher->dispatch($event, KernelEvents::REQUEST);
// Test that the action in the rule logged something.
$this->assertRulesDebugLogEntryExists('action called');
}