summaryrefslogtreecommitdiff
path: root/modules/simpletest/tests/actions_loop_test.module
blob: 77764907b9e6d6204c8c5221a4f5fd6f24dc8ee4 (plain)
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
<?php

/**
 * Implements hook_trigger_info().
 */
function actions_loop_test_trigger_info() {
  return array(
    'actions_loop_test' => array(
      'watchdog' => array(
        'label' => t('When a message is logged'),
      ),
    ),
  );
}

/**
 * Implements hook_watchdog().
 */
function actions_loop_test_watchdog(array $log_entry) {
  // If the triggering actions are not explicitly enabled, abort.
  if (empty($_GET['trigger_actions_on_watchdog'])) {
    return;
  }
  // Get all the action ids assigned to the trigger on the watchdog hook's
  // "run" event.
  $aids = trigger_get_assigned_actions('watchdog');
  // We can pass in any applicable information in $context. There isn't much in
  // this case, but we'll pass in the hook name as the bare minimum.
  $context = array(
    'hook' => 'watchdog',
  );
  // Fire the actions on the associated object ($log_entry) and the context
  // variable.
  actions_do(array_keys($aids), $log_entry, $context);
}

/**
 * Implements hook_init().
 */
function actions_loop_test_init() {
  if (!empty($_GET['trigger_actions_on_watchdog'])) {
    watchdog_skip_semaphore('actions_loop_test', 'Triggering action loop');
  }
}

/**
 * Implements hook_action_info().
 */
function actions_loop_test_action_info() {
  return array(
    'actions_loop_test_log' => array(
      'label' => t('Write a message to the log.'),
      'type' => 'system',
      'configurable' => FALSE,
      'triggers' => array('any'),
    ),
  );
}

/**
 * Write a message to the log.
 */
function actions_loop_test_log() {
  $count = &drupal_static(__FUNCTION__, 0);
  $count++;
  watchdog_skip_semaphore('actions_loop_test', "Test log #$count");
}

/**
 * Replacement of the watchdog() function that eliminates the use of semaphores
 * so that we can test the abortion of an action loop.
 */
function watchdog_skip_semaphore($type, $message, $variables = array(), $severity = WATCHDOG_NOTICE, $link = NULL) {
  global $user, $base_root;

  // Prepare the fields to be logged
  $log_entry = array(
    'type'        => $type,
    'message'     => $message,
    'variables'   => $variables,
    'severity'    => $severity,
    'link'        => $link,
    'user'        => $user,
    'request_uri' => $base_root . request_uri(),
    'referer'     => $_SERVER['HTTP_REFERER'],
    'ip'          => ip_address(),
    'timestamp'   => REQUEST_TIME,
  );

  // Call the logging hooks to log/process the message
  foreach (module_implements('watchdog') as $module) {
    module_invoke($module, 'watchdog', $log_entry);
  }
}