Projet

Général

Profil

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

root / drupal7 / modules / simpletest / tests / batch_test.callbacks.inc @ 582db59d

1
<?php
2

    
3

    
4
/**
5
 * @file
6
 * Batch callbacks for the Batch API tests.
7
 */
8

    
9
/**
10
 * Implements callback_batch_operation().
11
 *
12
 * Simple batch operation.
13
 */
14
function _batch_test_callback_1($id, $sleep, &$context) {
15
  // No-op, but ensure the batch take a couple iterations.
16
  // Batch needs time to run for the test, so sleep a bit.
17
  usleep($sleep);
18
  // Track execution, and store some result for post-processing in the
19
  // 'finished' callback.
20
  batch_test_stack("op 1 id $id");
21
  $context['results'][1][] = $id;
22
}
23

    
24
/**
25
 * Implements callback_batch_operation().
26
 *
27
 * Multistep batch operation.
28
 */
29
function _batch_test_callback_2($start, $total, $sleep, &$context) {
30
  // Initialize context with progress information.
31
  if (!isset($context['sandbox']['current'])) {
32
    $context['sandbox']['current'] = $start;
33
    $context['sandbox']['count'] = 0;
34
  }
35

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

    
48
    // Update progress information.
49
    $context['sandbox']['count']++;
50
  }
51
  $context['sandbox']['current'] += $i;
52

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

    
59
/**
60
 * Implements callback_batch_operation().
61
 *
62
 * Simple batch operation.
63
 */
64
function _batch_test_callback_5($id, $sleep, &$context) {
65
  // No-op, but ensure the batch take a couple iterations.
66
  // Batch needs time to run for the test, so sleep a bit.
67
  usleep($sleep);
68
  // Track execution, and store some result for post-processing in the
69
  // 'finished' callback.
70
  batch_test_stack("op 5 id $id");
71
  $context['results'][5][] = $id;
72
  // This test is to test finished > 1
73
  $context['finished'] = 3.14;
74
}
75

    
76
/**
77
 * Implements callback_batch_operation().
78
 *
79
 * Batch operation setting up its own batch.
80
 */
81
function _batch_test_nested_batch_callback() {
82
  batch_test_stack('setting up batch 2');
83
  batch_set(_batch_test_batch_2());
84
}
85

    
86
/**
87
 * Implements callback_batch_finished().
88
 *
89
 * Common 'finished' callbacks for batches 1 to 4.
90
 */
91
function _batch_test_finished_helper($batch_id, $success, $results, $operations) {
92
  $messages = array("results for batch $batch_id");
93
  if ($results) {
94
    foreach ($results as $op => $op_results) {
95
      $messages[] = 'op '. $op . ': processed ' . count($op_results) . ' elements';
96
    }
97
  }
98
  else {
99
    $messages[] = 'none';
100
  }
101

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

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

    
111
/**
112
 * Implements callback_batch_finished().
113
 *
114
 * 'finished' callback for batch 0.
115
 */
116
function _batch_test_finished_0($success, $results, $operations) {
117
  _batch_test_finished_helper(0, $success, $results, $operations);
118
}
119

    
120
/**
121
 * Implements callback_batch_finished().
122
 *
123
 * 'finished' callback for batch 1.
124
 */
125
function _batch_test_finished_1($success, $results, $operations) {
126
  _batch_test_finished_helper(1, $success, $results, $operations);
127
}
128

    
129
/**
130
 * Implements callback_batch_finished().
131
 *
132
 * 'finished' callback for batch 2.
133
 */
134
function _batch_test_finished_2($success, $results, $operations) {
135
  _batch_test_finished_helper(2, $success, $results, $operations);
136
}
137

    
138
/**
139
 * Implements callback_batch_finished().
140
 *
141
 * 'finished' callback for batch 3.
142
 */
143
function _batch_test_finished_3($success, $results, $operations) {
144
  _batch_test_finished_helper(3, $success, $results, $operations);
145
}
146

    
147
/**
148
 * Implements callback_batch_finished().
149
 *
150
 * 'finished' callback for batch 4.
151
 */
152
function _batch_test_finished_4($success, $results, $operations) {
153
  _batch_test_finished_helper(4, $success, $results, $operations);
154
}
155

    
156
/**
157
 * Implements callback_batch_finished().
158
 *
159
 * 'finished' callback for batch 5.
160
 */
161
function _batch_test_finished_5($success, $results, $operations) {
162
  _batch_test_finished_helper(5, $success, $results, $operations);
163
}