Projet

Général

Profil

Paste
Télécharger (9,77 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / rules / tests / rules_test.rules.inc @ 950416da

1
<?php
2

    
3
/**
4
 * @file
5
 * Includes any rules integration provided by the module.
6
 */
7

    
8
/**
9
 * Implements hook_rules_event_info().
10
 */
11
function rules_test_rules_event_info() {
12
  return array(
13
    'rules_test_event' => array(
14
      'label' => t('Test event'),
15
      'class' => 'RulesTestEventHandler',
16
    ),
17
  );
18
}
19

    
20
/**
21
 * Implements hook_rules_file_info().
22
 */
23
function rules_test_rules_file_info() {
24
  return array('rules_test.test');
25
}
26

    
27
/**
28
 * Implements hook_rules_condition_info().
29
 */
30
function rules_test_rules_condition_info() {
31
  $items = array();
32
  $defaults = array(
33
    'parameter' => array(
34
      'node' => array('type' => 'node', 'label' => t('Content')),
35
    ),
36
    'group' => t('Node'),
37
  );
38
  $items['rules_condition_content_is_type'] = array(
39
    'label' => t('Content has type'),
40
    'parameter' => array(
41
      'node' => array('type' => 'node', 'label' => t('Content')),
42
      'type' => array('type' => 'list<text>', 'label' => t('Content types')),
43
    ),
44
    'help' => t('Evaluates to TRUE, if the given content has one of the selected content types.'),
45
  ) + $defaults;
46
  $items['rules_condition_content_is_published'] = $defaults + array(
47
    'label' => t('Content is published'),
48
  );
49
  $items['rules_test_condition_true'] = array(
50
    'label' => t('Test condition returning true'),
51
    'group' => t('Rules test'),
52
  );
53
  $items['rules_test_condition_false'] = array(
54
    'label' => t('Test condition returning false'),
55
    'group' => t('Rules test'),
56
  );
57
  $items['rules_test_condition_apostrophe'] = array(
58
    'label' => t("Test use of an apostrophe (') in a condition label"),
59
    'group' => t('Rules test'),
60
  );
61
  // A condition for testing passing entities wrapped.
62
  $items['rules_test_condition_node_wrapped'] = array(
63
    'label' => t('Content is published'),
64
    'parameter' => array(
65
      'node' => array(
66
        'type' => 'node',
67
        'label' => t('Content'),
68
        'wrapped' => TRUE,
69
      ),
70
    ),
71
    'group' => t('Node'),
72
  );
73
  return $items;
74
}
75

    
76
/**
77
 * Condition implementation returning true.
78
 */
79
function rules_test_condition_true($settings, $state, $element) {
80
  if (!$element instanceof RulesCondition) {
81
    throw new Exception('Rules element has not been passed to condition.');
82
  }
83
  rules_log('condition true called');
84
  return TRUE;
85
}
86

    
87
/**
88
 * Condition implementation returning false.
89
 */
90
function rules_test_condition_false() {
91
  rules_log('condition false called');
92
  return FALSE;
93
}
94

    
95
/**
96
 * Condition testing use of an apostrophe in a condition label.
97
 *
98
 * Specifically, we want to ensure that special characters do not show up as
99
 * HTML-encoded in the user interface.
100
 */
101
function rules_test_condition_apostrophe($settings, $state, $element) {
102
  if (!$element instanceof RulesCondition) {
103
    throw new Exception('Rules element has not been passed to condition.');
104
  }
105
  rules_log('condition apostrophe called');
106
  return TRUE;
107
}
108

    
109
/**
110
 * Condition implementation receiving the node wrapped.
111
 */
112
function rules_test_condition_node_wrapped($wrapper) {
113
  return $wrapper instanceof EntityMetadataWrapper;
114
}
115

    
116
/**
117
 * Implements hook_rules_action_info().
118
 */
119
function rules_test_rules_action_info() {
120
  $items['rules_test_action'] = array(
121
    'label' => t('Test action'),
122
    'group' => t('Rules test'),
123
  );
124
  return $items + array(
125
    'rules_node_publish_action' => array(
126
      'label' => t('Publish content, but do not save'),
127
      'parameter' => array(
128
        'node' => array('type' => 'node', 'label' => t('Content')),
129
      ),
130
      'callbacks' => array(
131
        'help' => 'rules_test_custom_help',
132
      ),
133
      'base' => 'node_publish_action',
134
    ),
135
    'rules_node_publish_action_save' => array(
136
      'label' => t('Publish content'),
137
      'parameter' => array(
138
        'node' => array(
139
          'type' => 'node',
140
          'label' => t('Content'),
141
          'save' => TRUE,
142
        ),
143
      ),
144
      'base' => 'node_publish_action',
145
    ),
146
    'rules_node_make_sticky_action' => array(
147
      'label' => t('Make content sticky'),
148
      'parameter' => array(
149
        'node' => array(
150
          'type' => 'node',
151
          'label' => t('Content'),
152
          'save' => TRUE,
153
        ),
154
      ),
155
      'base' => 'node_make_sticky_action',
156
    ),
157
    // The same action again requiring a 'page' node.
158
    'rules_node_page_make_sticky_action' => array(
159
      'label' => t('Mage page content sticky'),
160
      'parameter' => array(
161
        'node' => array(
162
          'type' => 'node',
163
          'label' => t('Content'),
164
          'save' => TRUE,
165
          'bundles' => array('page'),
166
        ),
167
      ),
168
      'base' => 'node_make_sticky_action',
169
    ),
170
    'rules_action_test_reference' => array(
171
      'label' => t('Change argument passed by reference'),
172
      'parameter' => array(
173
         // For references working right, we need a data type with a wrapper.
174
        'arg' => array('type' => 'test'),
175
      ),
176
    ),
177
    'rules_action_load_node' => array(
178
      'label' => t('Fetch content by id'),
179
      'parameter' => array(
180
        'nid' => array('type' => 'integer', 'label' => t('Content ID')),
181
        'vid' => array(
182
          'type' => 'integer',
183
          'label' => t('Content Revision ID'),
184
          '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."),
185
          'optional' => TRUE,
186
        ),
187
      ),
188
      'provides' => array(
189
        'node_loaded' => array(
190
          'type' => 'node',
191
          'label' => t('Loaded content'),
192
          'label callback' => 'rules_action_load_node_variable_label',
193
        ),
194
      ),
195
      'group' => t('Node'),
196
      'access callback' => 'rules_node_integration_access',
197
    ),
198
    'rules_action_delete_node' => array(
199
      'label' => t('Delete content'),
200
      'parameter' => array(
201
        'node' => array('type' => 'node', 'label' => t('Content')),
202
      ),
203
      'group' => t('Node'),
204
      'access callback' => 'rules_node_integration_access',
205
    ),
206
    // An action for testing named parameters.
207
    'rules_action_node_set_title' => array(
208
      'label' => t('Content'),
209
      'parameter' => array(
210
        'node' => array('type' => 'node', 'label' => t('Content')),
211
        'title' => array('type' => 'text', 'label' => t('Text')),
212
      ),
213
      'named parameter' => TRUE,
214
      'group' => t('Node'),
215
      'access callback' => 'rules_node_integration_access',
216
    ),
217
    // Tests automatic saving with a non-entity data type.
218
    'test_type_save' => array(
219
      'base' => 'rules_test_type_save',
220
      'label' => t('Save test type'),
221
      'parameter' => array(
222
        'node' => array(
223
          'type' => 'rules_test_type',
224
          'label' => t('Test content'),
225
          'save' => TRUE,
226
        ),
227
      ),
228
      'group' => t('Node'),
229
    ),
230
  );
231
}
232

    
233
/**
234
 * Test action doing nothing exception logging it has been called.
235
 */
236
function rules_test_action() {
237
  rules_log('action called');
238
}
239

    
240
/**
241
 * Action for testing writing class-based actions.
242
 */
243
class RulesTestClassAction extends RulesActionHandlerBase {
244

    
245
  /**
246
   * Defines the action.
247
   */
248
  public static function getInfo() {
249
    return array(
250
      'name' => 'rules_test_class_action',
251
      'label' => t('Test class based action'),
252
      'group' => t('Node'),
253
      'parameter' => array(
254
        'node' => array(
255
          'type' => 'node',
256
          'label' => t('Node'),
257
        ),
258
      ),
259
    );
260
  }
261

    
262
  /**
263
   * Executes the action.
264
   */
265
  public function execute($node) {
266
    rules_log('Action called with node ' . $node->nid);
267
  }
268

    
269
}
270

    
271
/**
272
 * Implements hook_rules_data_info().
273
 */
274
function rules_test_rules_data_info() {
275
  return array(
276
    'rules_test_type' => array(
277
      'label' => t('test type'),
278
      'wrap' => TRUE,
279
      'wrapper class' => 'RulesTestTypeWrapper',
280
    ),
281
  );
282
}
283

    
284
/**
285
 * Implements hook_rules_data_info_alter().
286
 */
287
function rules_test_rules_data_info_alter(&$data_info) {
288
  $data_info['log_entry']['creation callback'] = 'rules_action_data_create_array';
289
}
290

    
291
/**
292
 * The custom wrapper class for the rules test type.
293
 *
294
 * For testing we internally just make use of nodes.
295
 */
296
class RulesTestTypeWrapper extends RulesIdentifiableDataWrapper implements RulesDataWrapperSavableInterface {
297

    
298
  /**
299
   * Overrides RulesIdentifiableDataWrapper::extractIdentifier().
300
   */
301
  protected function extractIdentifier($data) {
302
    return $data->nid;
303
  }
304

    
305
  /**
306
   * Overrides RulesIdentifiableDataWrapper::load().
307
   */
308
  protected function load($id) {
309
    return node_load($id);
310
  }
311

    
312
  /**
313
   * Implements RulesDataWrapperSavableInterface::save().
314
   */
315
  public function save() {
316
    node_save($this->value());
317
  }
318

    
319
}
320

    
321
/**
322
 * Implements hook_rules_plugin_info().
323
 */
324
function rules_test_rules_plugin_info() {
325
  return array(
326
    'rules test container' => array(
327
      'label' => t('Test container'),
328
      'class' => 'RulesTestContainer',
329
      'embeddable' => 'RulesActionContainer',
330
    ),
331
  );
332
}
333

    
334
/**
335
 * Test container plugin.
336
 */
337
class RulesTestContainer extends RulesContainerPlugin {
338
  protected $itemName = 'rules test container';
339

    
340
  /**
341
   * Evaluate the element on a given rules evaluation state.
342
   */
343
  public function evaluate(RulesState $state) {
344
    // Do nothing.
345
  }
346

    
347
}
348

    
349
/**
350
 * Test event handler class.
351
 */
352
class RulesTestEventHandler extends RulesEventDefaultHandler implements RulesEventDispatcherInterface {
353

    
354
  /**
355
   * Name of the variable in which to store the state of the event handler.
356
   *
357
   * @var string
358
   */
359
  protected $variableName = 'rules_test_event_handler_watch';
360

    
361
  /**
362
   * Implements RulesEventDispatcherInterface::startWatching().
363
   */
364
  public function startWatching() {
365
    variable_set($this->variableName, TRUE);
366
  }
367

    
368
  /**
369
   * Implements RulesEventDispatcherInterface::stopWatching().
370
   */
371
  public function stopWatching() {
372
    variable_set($this->variableName, FALSE);
373
  }
374

    
375
  /**
376
   * Implements RulesEventDispatcherInterface::isWatching().
377
   */
378
  public function isWatching() {
379
    return (bool) variable_get($this->variableName);
380
  }
381

    
382
}