rules_test.rules.inc
8.9 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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
<?php
/**
* @file Includes any rules integration provided by the module.
*/
/**
* Implements hook_rules_event_info().
*/
function rules_test_rules_event_info() {
return array('rules_test_event' => array(
'label' => t('Test event'),
'class' => 'RulesTestEventHandler',
));
}
/**
* Implements hook_rules_file_info().
*/
function rules_test_rules_file_info() {
return array('rules_test.test');
}
/**
* Implements hook_rules_condition_info().
*/
function rules_test_rules_condition_info() {
$items = array();
$defaults = array(
'parameter' => array(
'node' => array('type' => 'node', 'label' => t('Content')),
),
'group' => t('Node'),
);
$items['rules_condition_content_is_type'] = array(
'label' => t('Content has type'),
'parameter' => array(
'node' => array('type' => 'node', 'label' => t('Content')),
'type' => array('type' => 'list<text>', 'label' => t('Content types')),
),
'help' => t('Evaluates to TRUE, if the given content has one of the selected content types.'),
) + $defaults;
$items['rules_condition_content_is_published'] = $defaults + array(
'label' => t('Content is published'),
);
$items['rules_test_condition_true'] = array(
'label' => t('Test condition returning true'),
'group' => t('Rules test'),
);
$items['rules_test_condition_false'] = array(
'label' => t('Test condition returning false'),
'group' => t('Rules test'),
);
// A condition for testing passing entities wrapped.
$items['rules_test_condition_node_wrapped'] = array(
'label' => t('Content is published'),
'parameter' => array(
'node' => array(
'type' => 'node',
'label' => t('Content'),
'wrapped' => TRUE,
),
),
'group' => t('Node'),
);
return $items;
}
/**
* Condition implementation returning true.
*/
function rules_test_condition_true($settings, $state, $element) {
if (!$element instanceof RulesCondition) {
throw new Exception('Rules element has not been passed to condition.');
}
rules_log('condition true called');
return TRUE;
}
/**
* Condition implementation returning false.
*/
function rules_test_condition_false() {
rules_log('condition false called');
return FALSE;
}
/**
* Condition implementation receiving the node wrapped.
*/
function rules_test_condition_node_wrapped($wrapper) {
return $wrapper instanceof EntityMetadataWrapper;
}
/**
* Implements hook_rules_action_info().
*/
function rules_test_rules_action_info() {
$items['rules_test_action'] = array(
'label' => t('Test action'),
'group' => t('Rules test'),
);
return $items + array(
'rules_node_publish_action' => array(
'label' => t('Publish content, but do not save'),
'parameter' => array(
'node' => array('type' => 'node', 'label' => t('Content')),
),
'callbacks' => array(
'help' => 'rules_test_custom_help',
),
'base' => 'node_publish_action',
),
'rules_node_publish_action_save' => array(
'label' => t('Publish content'),
'parameter' => array(
'node' => array(
'type' => 'node',
'label' => t('Content'),
'save' => TRUE,
),
),
'base' => 'node_publish_action',
),
'rules_node_make_sticky_action' => array(
'label' => t('Make content sticky'),
'parameter' => array(
'node' => array(
'type' => 'node',
'label' => t('Content'),
'save' => TRUE,
),
),
'base' => 'node_make_sticky_action',
),
// The same action again requiring a 'page' node.
'rules_node_page_make_sticky_action' => array(
'label' => t('Mage page content sticky'),
'parameter' => array(
'node' => array(
'type' => 'node',
'label' => t('Content'),
'save' => TRUE,
'bundles' => array('page'),
),
),
'base' => 'node_make_sticky_action',
),
'rules_action_test_reference' => array(
'label' => t('Change argument passed by reference'),
'parameter' => array(
// For references working right, we need a data type with a wrapper.
'arg' => array('type' => 'test'),
),
),
'rules_action_load_node' => array(
'label' => t('Fetch content by id'),
'parameter' => array(
'nid' => array('type' => 'integer', 'label' => t('Content ID')),
'vid' => array(
'type' => 'integer',
'label' => t('Content Revision ID'),
'description' => t("If you want to fetch a specific revision, specify it's revision id. Else leave it empty to fetch the currently active revision."),
'optional' => TRUE,
),
),
'provides' => array(
'node_loaded' => array(
'type' => 'node',
'label' => t('Loaded content'),
'label callback' => 'rules_action_load_node_variable_label',
),
),
'group' => t('Node'),
'access callback' => 'rules_node_integration_access',
),
'rules_action_delete_node' => array(
'label' => t('Delete content'),
'parameter' => array(
'node' => array('type' => 'node', 'label' => t('Content')),
),
'group' => t('Node'),
'access callback' => 'rules_node_integration_access',
),
// An action for testing named parameters.
'rules_action_node_set_title' => array(
'label' => t('Content'),
'parameter' => array(
'node' => array('type' => 'node', 'label' => t('Content')),
'title' => array('type' => 'text', 'label' => t('Text')),
),
'named parameter' => TRUE,
'group' => t('Node'),
'access callback' => 'rules_node_integration_access',
),
// Tests automatic saving with a non-entity data type.
'test_type_save' => array(
'base' => 'rules_test_type_save',
'label' => t('Save test type'),
'parameter' => array(
'node' => array('type' => 'rules_test_type', 'label' => t('Test content'), 'save' => TRUE),
),
'group' => t('Node'),
),
);
}
/**
* Test action doing nothing exception logging it has been called.
*/
function rules_test_action() {
rules_log('action called');
}
/**
* Action for testing writing class-based actions.
*/
class RulesTestClassAction extends RulesActionHandlerBase {
/**
* Defines the action.
*/
public static function getInfo() {
return array(
'name' => 'rules_test_class_action',
'label' => t('Test class based action'),
'group' => t('Node'),
'parameter' => array(
'node' => array(
'type' => 'node',
'label' => t('Node'),
),
),
);
}
/**
* Executes the action.
*/
public function execute($node) {
rules_log('Action called with node ' . $node->nid);
}
}
/**
* Implements hook_rules_data_info().
*/
function rules_test_rules_data_info() {
return array(
'rules_test_type' => array(
'label' => t('test type'),
'wrap' => TRUE,
'wrapper class' => 'RulesTestTypeWrapper',
),
);
}
/**
* Implements hook_rules_data_info_alter().
*/
function rules_test_rules_data_info_alter(&$data_info) {
$data_info['log_entry']['creation callback'] = 'rules_action_data_create_array';
}
/**
* The custom wrapper class for the rules test type.
*
* For testing we internally just make use of nodes.
*/
class RulesTestTypeWrapper extends RulesIdentifiableDataWrapper implements RulesDataWrapperSavableInterface {
protected function extractIdentifier($data) {
return $data->nid;
}
protected function load($id) {
return node_load($id);
}
public function save() {
node_save($this->value());
}
}
/**
* Implements hook_rules_plugin_info().
*/
function rules_test_rules_plugin_info() {
return array(
'rules test container' => array(
'label' => t('Test container'),
'class' => 'RulesTestContainer',
'embeddable' => 'RulesActionContainer',
),
);
}
/**
* Test container plugin.
*/
class RulesTestContainer extends RulesContainerPlugin {
protected $itemName = 'rules test container';
/**
* Evaluate the element on a given rules evaluation state.
*/
public function evaluate(RulesState $state) {
// Do nothing.
}
}
/**
* Test event handler class.
*/
class RulesTestEventHandler extends RulesEventDefaultHandler implements RulesEventDispatcherInterface {
/**
* Name of the variable in which to store the state of the event handler.
*
* @var string
*/
protected $variableName = 'rules_test_event_handler_watch';
/**
* Implements RulesEventDispatcherInterface::startWatching().
*/
public function startWatching() {
variable_set($this->variableName, TRUE);
}
/**
* Implements RulesEventDispatcherInterface::stopWatching().
*/
public function stopWatching() {
variable_set($this->variableName, FALSE);
}
/**
* Implements RulesEventDispatcherInterface::isWatching().
*/
public function isWatching() {
return (bool) variable_get($this->variableName);
}
}