summaryrefslogtreecommitdiff
path: root/modules/simpletest/tests/batch_test.callbacks.inc
blob: 8e03c0ba6cf40f3976d04373fb955d1cf3cd2904 (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
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
<?php

// $Id$

/**
 * @file
 * Batch callbacks for the Batch API tests.
 */

/**
 * Simple batch operation.
 */
function _batch_test_callback_1($id, $sleep, &$context) {
  // No-op, but ensure the batch take a couple iterations.
  // Batch needs time to run for the test, so sleep a bit.
  usleep($sleep);
  // Track execution, and store some result for post-processing in the
  // 'finished' callback.
  batch_test_stack("op 1 id $id");
  $context['results'][1][] = $id;
}

/**
 * Multistep batch operation.
 */
function _batch_test_callback_2($start, $total, $sleep, &$context) {
  // Initialize context with progress information.
  if (!isset($context['sandbox']['current'])) {
    $context['sandbox']['current'] = $start;
    $context['sandbox']['count'] = 0;
  }

  // Process by groups of 5 (arbitrary value).
  $limit = 5;
  for ($i = 0; $i < $limit && $context['sandbox']['count'] < $total; $i++) {
    // No-op, but ensure the batch take a couple iterations.
    // Batch needs time to run for the test, so sleep a bit.
    usleep($sleep);
    // Track execution, and store some result for post-processing in the
    // 'finished' callback.
    $id = $context['sandbox']['current'] + $i;
    batch_test_stack("op 2 id $id");
    $context['results'][2][] = $id;

    // Update progress information.
    $context['sandbox']['count']++;
  }
  $context['sandbox']['current'] += $i;

  // Inform batch engine about progress.
  if ($context['sandbox']['count'] != $total) {
    $context['finished'] = $context['sandbox']['count'] / $total;
  }
}

/**
 * Simple batch operation.
 */
function _batch_test_callback_5($id, $sleep, &$context) {
  // No-op, but ensure the batch take a couple iterations.
  // Batch needs time to run for the test, so sleep a bit.
  usleep($sleep);
  // Track execution, and store some result for post-processing in the
  // 'finished' callback.
  batch_test_stack("op 5 id $id");
  $context['results'][5][] = $id;
  // This test is to test finished > 1
  $context['finished'] = 3.14;
}

/**
 * Batch operation setting up its own batch.
 */
function _batch_test_nested_batch_callback() {
  batch_test_stack('setting up batch 2');
  batch_set(_batch_test_batch_2());
}

/**
 * Common 'finished' callbacks for batches 1 to 4.
 */
function _batch_test_finished_helper($batch_id, $success, $results, $operations) {
  $messages = array("results for batch $batch_id");
  if ($results) {
    foreach ($results as $op => $op_results) {
      $messages[] = 'op '. $op . ': processed ' . count($op_results) . ' elements';
    }
  }
  else {
    $messages[] = 'none';
  }

  if (!$success) {
    // A fatal error occurred during the processing.
    $error_operation = reset($operations);
    $messages[] = t('An error occurred while processing @op with arguments:<br/>@args', array('@op' => $error_operation[0], '@args' => print_r($error_operation[1], TRUE)));
  }

  drupal_set_message(implode('<br />', $messages));
}

/**
 * 'finished' callback for batch 0.
 */
function _batch_test_finished_0($success, $results, $operations) {
  _batch_test_finished_helper(0, $success, $results, $operations);
}

/**
 * 'finished' callback for batch 1.
 */
function _batch_test_finished_1($success, $results, $operations) {
  _batch_test_finished_helper(1, $success, $results, $operations);
}

/**
 * 'finished' callback for batch 2.
 */
function _batch_test_finished_2($success, $results, $operations) {
  _batch_test_finished_helper(2, $success, $results, $operations);
}

/**
 * 'finished' callback for batch 3.
 */
function _batch_test_finished_3($success, $results, $operations) {
  _batch_test_finished_helper(3, $success, $results, $operations);
}

/**
 * 'finished' callback for batch 4.
 */
function _batch_test_finished_4($success, $results, $operations) {
  _batch_test_finished_helper(4, $success, $results, $operations);
}

/**
 * 'finished' callback for batch 5.
 */
function _batch_test_finished_5($success, $results, $operations) {
  _batch_test_finished_helper(5, $success, $results, $operations);
}