-
Notifications
You must be signed in to change notification settings - Fork 118
Expand file tree
/
Copy pathEventIntegrationTest.php
More file actions
216 lines (177 loc) · 6.52 KB
/
EventIntegrationTest.php
File metadata and controls
216 lines (177 loc) · 6.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
<?php
/**
* @file
* Contains \Drupal\Tests\rules\Kernel\EventIntegrationTest.
*/
namespace Drupal\Tests\rules\Kernel;
use Drupal\rules\Context\ContextConfig;
use Drupal\user\Entity\User;
use Symfony\Component\HttpKernel\KernelEvents;
/**
* Test for the Symfony event mapping to Rules events.
*
* @group rules
*/
class EventIntegrationTest extends RulesDrupalTestBase {
/**
* The entity storage for Rules config entities.
*
* @var \Drupal\Core\Entity\EntityStorageInterface
*/
protected $storage;
/**
* Modules to enable.
*
* @var array
*/
public static $modules = ['user'];
/**
* {@inheritdoc}
*/
public function setUp() {
parent::setUp();
$this->storage = $this->container->get('entity_type.manager')->getStorage('rules_reaction_rule');
}
/**
* Test that the user login hook triggers the Rules event listener.
*/
public function testUserLoginEvent() {
$rule = $this->expressionManager->createRule();
$rule->addCondition('rules_test_true');
$rule->addAction('rules_test_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();
// 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 rule.
rules_user_login($account);
// Test that the action in the rule logged something.
$this->assertRulesLogEntryExists('test_user');
}
/**
* Test that the user logout hook triggers the Rules event listener.
*/
public function testUserLogoutEvent() {
$rule = $this->expressionManager->createRule();
$rule->addCondition('rules_test_true');
$rule->addAction('rules_test_log');
$config_entity = $this->storage->create([
'id' => 'test_rule',
'events' => [['event_name' => 'rules_user_logout']],
'expression' => $rule->getConfiguration(),
]);
$config_entity->save();
// 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 the cron hook triggers the Rules event listener.
*/
public function testCronEvent() {
$rule = $this->expressionManager->createRule();
$rule->addCondition('rules_test_true');
$rule->addAction('rules_test_log');
$config_entity = $this->storage->create([
'id' => 'test_rule',
'events' => [['event_name' => 'rules_system_cron']],
'expression' => $rule->getConfiguration(),
]);
$config_entity->save();
// The logger instance has changed, refresh it.
$this->logger = $this->container->get('logger.channel.rules');
// Run cron.
$this->container->get('cron')->run();
// Test that the action in the rule logged something.
$this->assertRulesLogEntryExists('action called');
}
/**
* Test that a Logger message trigger the Rules logger listener.
*/
public function testSystemLoggerEvent() {
$rule = $this->expressionManager->createRule();
$rule->addCondition('rules_test_true');
$rule->addAction('rules_test_log');
$config_entity = $this->storage->create([
'id' => 'test_rule',
'events' => [['event_name' => 'rules_system_logger_event']],
'expression' => $rule->getConfiguration(),
]);
$config_entity->save();
// The logger instance has changed, refresh it.
$this->logger = $this->container->get('logger.channel.rules');
// Creates a logger-item, that must be dispatched as event.
$this->container->get('logger.factory')->get('rules_test')
->notice("This message must get logged and dispatched as rules_system_logger_event");
// Test that the action in the rule logged something.
$this->assertRulesLogEntryExists('action called');
}
/**
* Test that Drupal initializing triggers the Rules logger listener.
*/
public function testInitEvent() {
$rule = $this->expressionManager->createRule();
$rule->addCondition('rules_test_true');
$rule->addAction('rules_test_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');
$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.
$dispatcher->dispatch(KernelEvents::REQUEST);
// 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);
}
}