-
Notifications
You must be signed in to change notification settings - Fork 123
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Change reaction-rule config to save the event with settings #405
base: 8.x-3.x
Are you sure you want to change the base?
Changes from 3 commits
b57e33d
7d0d7e2
87dca7d
cb7f762
6fecbad
944aa09
9ead81e
8e0c714
d179fcf
0c297b6
5eea48c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -36,7 +36,7 @@ | |
* config_export = { | ||
* "id", | ||
* "label", | ||
* "event", | ||
* "events", | ||
* "module", | ||
* "description", | ||
* "tag", | ||
|
@@ -121,11 +121,16 @@ class ReactionRuleConfig extends ConfigEntityBase { | |
protected $module = 'rules'; | ||
|
||
/** | ||
* The event name this reaction rule is reacting on. | ||
* The events this reaction rule is reacting on. | ||
* | ||
* @var string | ||
* Events array. The array is numerically indexed and contains arrays with the | ||
* following structure: | ||
* - event_name: string with the event machine name. | ||
* - configuration: an array containing the event configuration. | ||
* | ||
* @var array | ||
*/ | ||
protected $event; | ||
protected $events; | ||
|
||
/** | ||
* Sets a Rules expression instance for this Reaction rule. | ||
|
@@ -208,10 +213,30 @@ public function getTag() { | |
} | ||
|
||
/** | ||
* Returns the event on which this rule will trigger. | ||
* Gets configuration of all events the rule is reacting on. | ||
* | ||
* @return array | ||
* The events array. The array is numerically indexed and contains arrays | ||
* with the following structure: | ||
* - event_name: string with the event machine name. | ||
* - configuration: an array containing the event configuration. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Description sentences should start upper-case |
||
*/ | ||
public function getEvents() { | ||
return $this->events; | ||
} | ||
|
||
/** | ||
* Gets fully qualified names of all events the rule is reacting on. | ||
* | ||
* @return string[] | ||
* The array of fully qualified event names of the rule. | ||
*/ | ||
public function getEvent() { | ||
return $this->event; | ||
public function getEventNames() { | ||
$names = []; | ||
foreach ($this->events as $event) { | ||
$names[] = $event['event_name']; | ||
} | ||
return $names; | ||
} | ||
|
||
/** | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -54,7 +54,7 @@ public function testUserLoginEvent() { | |
$config_entity = $this->storage->create([ | ||
'id' => 'test_rule', | ||
'expression_id' => 'rules_rule', | ||
'event' => 'rules_user_login', | ||
'events' => [['event_name' => 'rules_user_login']], | ||
'configuration' => $rule->getConfiguration(), | ||
]); | ||
$config_entity->save(); | ||
|
@@ -81,7 +81,7 @@ public function testUserLogoutEvent() { | |
$config_entity = $this->storage->create([ | ||
'id' => 'test_rule', | ||
'expression_id' => 'rules_rule', | ||
'event' => 'rules_user_logout', | ||
'events' => [['event_name' => 'rules_user_logout']], | ||
'configuration' => $rule->getConfiguration(), | ||
]); | ||
$config_entity->save(); | ||
|
@@ -108,7 +108,7 @@ public function testCronEvent() { | |
$config_entity = $this->storage->create([ | ||
'id' => 'test_rule', | ||
'expression_id' => 'rules_rule', | ||
'event' => 'rules_system_cron', | ||
'events' => [['event_name' => 'rules_system_cron']], | ||
'configuration' => $rule->getConfiguration(), | ||
]); | ||
$config_entity->save(); | ||
|
@@ -134,7 +134,7 @@ public function testSystemLoggerEvent() { | |
$config_entity = $this->storage->create([ | ||
'id' => 'test_rule', | ||
'expression_id' => 'rules_rule', | ||
'event' => 'rules_system_logger_event', | ||
'events' => [['event_name' => 'rules_system_logger_event']], | ||
'configuration' => $rule->getConfiguration(), | ||
]); | ||
$config_entity->save(); | ||
|
@@ -161,7 +161,7 @@ public function testInitEvent() { | |
$config_entity = $this->storage->create([ | ||
'id' => 'test_rule', | ||
'expression_id' => 'rules_rule', | ||
'event' => KernelEvents::REQUEST, | ||
'events' => [['event_name' => KernelEvents::REQUEST]], | ||
'configuration' => $rule->getConfiguration(), | ||
]); | ||
$config_entity->save(); | ||
|
@@ -185,4 +185,38 @@ public function testInitEvent() { | |
$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', | ||
'expression_id' => 'rules_rule', | ||
]); | ||
$config_entity->set('events', [ | ||
['event_name' => 'rules_user_login'], | ||
['event_name' => 'rules_user_logout'], | ||
]); | ||
$config_entity->set('configuration', $rule->getConfiguration()); | ||
$config_entity->save(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should test using the getter also. |
||
|
||
// 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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice that this works already! |
||
|
||
// Test that the action in the rule logged something. | ||
$this->assertRulesLogEntryExists('action called'); | ||
$this->assertRulesLogEntryExists('action called', 1); | ||
} | ||
|
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should initialize this as empty array with "[]".