Projet

Général

Profil

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

root / drupal7 / sites / all / modules / rules / tests / rules_test.rules.inc @ 76e2e7c3

1
<?php
2

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

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

    
17
/**
18
 * Implements hook_rules_file_info().
19
 */
20
function rules_test_rules_file_info() {
21
  return array('rules_test.test');
22
}
23

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

    
69
/**
70
 * Condition implementation returning true.
71
 */
72
function rules_test_condition_true($settings, $state, $element) {
73
  if (!$element instanceof RulesCondition) {
74
    throw new Exception('Rules element has not been passed to condition.');
75
  }
76
  rules_log('condition true called');
77
  return TRUE;
78
}
79

    
80
/**
81
 * Condition implementation returning false.
82
 */
83
function rules_test_condition_false() {
84
  rules_log('condition false called');
85
  return FALSE;
86
}
87

    
88
/**
89
 * Condition implementation receiving the node wrapped.
90
 */
91
function rules_test_condition_node_wrapped($wrapper) {
92
  return $wrapper instanceof EntityMetadataWrapper;
93
}
94

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

    
208
/**
209
 * Test action doing nothing exception logging it has been called.
210
 */
211
function rules_test_action() {
212
  rules_log('action called');
213
}
214

    
215
/**
216
 * Action for testing writing class-based actions.
217
 */
218
class RulesTestClassAction extends RulesActionHandlerBase {
219

    
220
  /**
221
   * Defines the action.
222
   */
223
  public static function getInfo() {
224
    return array(
225
      'name' => 'rules_test_class_action',
226
      'label' => t('Test class based action'),
227
      'group' => t('Node'),
228
      'parameter' => array(
229
        'node' => array(
230
          'type' => 'node',
231
          'label' => t('Node'),
232
        ),
233
      ),
234
    );
235
  }
236

    
237
  /**
238
   * Executes the action.
239
   */
240
  public function execute($node) {
241
    rules_log('Action called with node ' . $node->nid);
242
  }
243
}
244

    
245
/**
246
 * Implements hook_rules_data_info().
247
 */
248
function rules_test_rules_data_info() {
249
  return array(
250
    'rules_test_type' => array(
251
      'label' => t('test type'),
252
      'wrap' => TRUE,
253
      'wrapper class' => 'RulesTestTypeWrapper',
254
    ),
255
  );
256
}
257

    
258
/**
259
 * Implements hook_rules_data_info_alter().
260
 */
261
function rules_test_rules_data_info_alter(&$data_info) {
262
  $data_info['log_entry']['creation callback'] = 'rules_action_data_create_array';
263
}
264

    
265
/**
266
 * The custom wrapper class for the rules test type.
267
 *
268
 * For testing we internally just make use of nodes.
269
 */
270
class RulesTestTypeWrapper extends RulesIdentifiableDataWrapper implements RulesDataWrapperSavableInterface {
271

    
272
  protected function extractIdentifier($data) {
273
    return $data->nid;
274
  }
275

    
276
  protected function load($id) {
277
    return node_load($id);
278
  }
279

    
280
  public function save() {
281
    node_save($this->value());
282
  }
283
}
284

    
285
/**
286
 * Implements hook_rules_plugin_info().
287
 */
288
function rules_test_rules_plugin_info() {
289
  return array(
290
    'rules test container' => array(
291
      'label' => t('Test container'),
292
      'class' => 'RulesTestContainer',
293
      'embeddable' => 'RulesActionContainer',
294
    ),
295
  );
296
}
297

    
298
/**
299
 * Test container plugin.
300
 */
301
class RulesTestContainer extends RulesContainerPlugin {
302
  protected $itemName = 'rules test container';
303

    
304
  /**
305
   * Evaluate the element on a given rules evaluation state.
306
   */
307
  public function evaluate(RulesState $state) {
308
    // Do nothing.
309
  }
310
}
311

    
312
/**
313
 * Test event handler class.
314
 */
315
class RulesTestEventHandler extends RulesEventDefaultHandler implements RulesEventDispatcherInterface {
316

    
317
  /**
318
   * Name of the variable in which to store the state of the event handler.
319
   *
320
   * @var string
321
   */
322
  protected $variableName = 'rules_test_event_handler_watch';
323

    
324
  /**
325
   * Implements RulesEventDispatcherInterface::startWatching().
326
   */
327
  public function startWatching() {
328
    variable_set($this->variableName, TRUE);
329
  }
330

    
331
  /**
332
   * Implements RulesEventDispatcherInterface::stopWatching().
333
   */
334
  public function stopWatching() {
335
    variable_set($this->variableName, FALSE);
336
  }
337

    
338
  /**
339
   * Implements RulesEventDispatcherInterface::isWatching().
340
   */
341
  public function isWatching() {
342
    return (bool) variable_get($this->variableName);
343
  }
344

    
345
}