/** * Test that the user logout hook triggers the Rules event listener. */ public function testUserLogoutEvent() { $rule = $this->expressionManager->createInstance('rules_reaction_rule', ['event' => 'rules_user_logout']); $rule->addCondition('rules_test_true'); $rule->addAction('rules_test_log'); $config_entity = $this->storage->create(['id' => 'test_rule', 'expression_id' => 'rules_reaction_rule', 'event' => 'rules_user_logout', 'configuration' => $rule->getConfiguration()]); $config_entity->save(); // Rebuild the container so that the newly configured event gets picked up. $this->container->get('kernel')->rebuildContainer(); // The logger instance has changed, refresh it. $this->logger = $this->container->get('logger.channel.rules'); $account = $this->container->get('current_user'); // Invoke the hook manually which should trigger the rule. rules_user_logout($account); // Test that the action in the rule logged something. $this->assertRulesLogEntryExists('action called'); }
/** * Test that rules config supports multiple events. */ public function testMultipleEvents() { $rule = $this->expressionManager->createRule(); $rule->addCondition('rules_test_true'); $rule->addAction('rules_test_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(); // The logger instance has changed, refresh it. $this->logger = $this->container->get('logger.channel.rules'); $account = User::create(['name' => 'test_user']); // Invoke the hook manually which should trigger the rules_user_login event. rules_user_login($account); // Invoke the hook manually which should trigger the rules_user_logout // event. rules_user_logout($account); // Test that the action in the rule logged something. $this->assertRulesLogEntryExists('action called'); $this->assertRulesLogEntryExists('action called', 1); }