Projet

Général

Profil

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

root / drupal7 / modules / simpletest / tests / batch_test.callbacks.inc @ db2d93dd

1
<?php
2

    
3

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

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

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

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

    
44
    // Update progress information.
45
    $context['sandbox']['count']++;
46
  }
47
  $context['sandbox']['current'] += $i;
48

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

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

    
70
/**
71
 * Batch operation setting up its own batch.
72
 */
73
function _batch_test_nested_batch_callback() {
74
  batch_test_stack('setting up batch 2');
75
  batch_set(_batch_test_batch_2());
76
}
77

    
78
/**
79
 * Common 'finished' callbacks for batches 1 to 4.
80
 */
81
function _batch_test_finished_helper($batch_id, $success, $results, $operations) {
82
  $messages = array("results for batch $batch_id");
83
  if ($results) {
84
    foreach ($results as $op => $op_results) {
85
      $messages[] = 'op '. $op . ': processed ' . count($op_results) . ' elements';
86
    }
87
  }
88
  else {
89
    $messages[] = 'none';
90
  }
91

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

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

    
101
/**
102
 * 'finished' callback for batch 0.
103
 */
104
function _batch_test_finished_0($success, $results, $operations) {
105
  _batch_test_finished_helper(0, $success, $results, $operations);
106
}
107

    
108
/**
109
 * 'finished' callback for batch 1.
110
 */
111
function _batch_test_finished_1($success, $results, $operations) {
112
  _batch_test_finished_helper(1, $success, $results, $operations);
113
}
114

    
115
/**
116
 * 'finished' callback for batch 2.
117
 */
118
function _batch_test_finished_2($success, $results, $operations) {
119
  _batch_test_finished_helper(2, $success, $results, $operations);
120
}
121

    
122
/**
123
 * 'finished' callback for batch 3.
124
 */
125
function _batch_test_finished_3($success, $results, $operations) {
126
  _batch_test_finished_helper(3, $success, $results, $operations);
127
}
128

    
129
/**
130
 * 'finished' callback for batch 4.
131
 */
132
function _batch_test_finished_4($success, $results, $operations) {
133
  _batch_test_finished_helper(4, $success, $results, $operations);
134
}
135

    
136
/**
137
 * 'finished' callback for batch 5.
138
 */
139
function _batch_test_finished_5($success, $results, $operations) {
140
  _batch_test_finished_helper(5, $success, $results, $operations);
141
}