-
Notifications
You must be signed in to change notification settings - Fork 118
Expand file tree
/
Copy pathConfigurableEventHandlerTest.php
More file actions
136 lines (116 loc) · 3.95 KB
/
ConfigurableEventHandlerTest.php
File metadata and controls
136 lines (116 loc) · 3.95 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
<?php
/**
* @file
* Contains \Drupal\Tests\rules\Integration\Engine\ConfigurableEventHandlerTest.
*/
namespace Drupal\Tests\rules\Kernel;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\rules\Context\ContextConfig;
use Drupal\rules\Event\EntityEvent;
/**
* Tests events with qualified name.
*
* @group rules
*/
class ConfigurableEventHandlerTest extends RulesDrupalTestBase {
/**
* {@inheritdoc}
*/
public static $modules = ['rules', 'system', 'node', 'field', 'user'];
/**
* The entity storage for Rules config entities.
*
* @var \Drupal\Core\Entity\EntityStorageInterface
*/
protected $storage;
/**
* A node used for testing.
*
* @var \Drupal\node\NodeInterface
*/
protected $node;
/**
* {@inheritdoc}
*/
public function setUp() {
parent::setUp();
$this->installSchema('system', ['sequences']);
$this->installEntitySchema('user');
$this->installEntitySchema('node');
$this->installConfig(['field']);
$this->storage = $this->container->get('entity_type.manager')->getStorage('rules_reaction_rule');
$entity_type_manager = $this->container->get('entity_type.manager');
$entity_type_manager->getStorage('node_type')
->create(['type' => 'page'])
->save();
FieldStorageConfig::create([
'field_name' => 'field_integer',
'type' => 'integer',
'entity_type' => 'node',
'cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED,
])->save();
FieldConfig::create([
'field_name' => 'field_integer',
'entity_type' => 'node',
'bundle' => 'page',
])->save();
$this->node = $entity_type_manager->getStorage('node')
->create([
'title' => 'test',
'type' => 'page',
]);
}
/**
* Tests ConfigurableEventHandlerEntityBundle.
*
* Test that rules are triggered correctly based upon the fully qualified
* event name as well as the base event name.
*
* @todo Add integrity check that node.field_integer is detected by Rules.
*/
public function testConfigurableEventHandler() {
// Create rule1 with the 'rules_entity_presave:node--page' event.
$rule1 = $this->expressionManager->createRule();
$rule1->addAction('rules_test_log',
ContextConfig::create()
->map('message', 'node.field_integer.0.value')
);
$config_entity1 = $this->storage->create([
'id' => 'test_rule1',
]);
$config_entity1->set('events', [
['event_name' => 'rules_entity_presave:node--page'],
]);
$config_entity1->set('expression', $rule1->getConfiguration());
$config_entity1->save();
// Create rule2 with the 'rules_entity_presave:node' event.
$rule2 = $this->expressionManager->createRule();
$rule2->addAction('rules_test_log',
ContextConfig::create()
->map('message', 'node.field_integer.1.value')
);
$config_entity2 = $this->storage->create([
'id' => 'test_rule2',
]);
$config_entity2->set('events', [
['event_name' => 'rules_entity_presave:node'],
]);
$config_entity2->set('expression', $rule2->getConfiguration());
$config_entity2->save();
// The logger instance has changed, refresh it.
$this->logger = $this->container->get('logger.channel.rules');
// Add node.field_integer.0.value to rules log message, read result.
$this->node->field_integer->setValue(['0' => 11, '1' => 22]);
// Trigger node save.
$entity_type_id = $this->node->getEntityTypeId();
$event = new EntityEvent($this->node, [$entity_type_id => $this->node]);
$event_dispatcher = \Drupal::service('event_dispatcher');
$event_dispatcher->dispatch("rules_entity_presave:$entity_type_id", $event);
// Test that the action in the rule1 logged node value.
$this->assertRulesLogEntryExists(11, 1);
// Test that the action in the rule2 logged node value.
$this->assertRulesLogEntryExists(22, 0);
}
}