59327de4fe2cd9998c89e9da34b8feffe2f40329
[yaffs-website] / batch_test / batch_test.module
1 <?php
2
3 /**
4  * @file
5  * Helper module for the Batch API tests.
6  */
7
8 use Drupal\Core\Form\FormState;
9
10 /**
11  * Batch operation: Submits form_test_mock_form().
12  */
13 function _batch_test_nested_drupal_form_submit_callback($value) {
14   $form_state = (new FormState())
15     ->setValue('test_value', $value);
16   \Drupal::formBuilder()->submitForm('Drupal\batch_test\Form\BatchTestMockForm', $form_state);
17 }
18
19 /**
20  * Batch 0: Does nothing.
21  */
22 function _batch_test_batch_0() {
23   $batch = [
24     'operations' => [],
25     'finished' => '_batch_test_finished_0',
26     'file' => drupal_get_path('module', 'batch_test') . '/batch_test.callbacks.inc',
27   ];
28   return $batch;
29 }
30
31 /**
32  * Batch 1: Repeats a simple operation.
33  *
34  * Operations: op 1 from 1 to 10.
35  */
36 function _batch_test_batch_1() {
37   // Ensure the batch takes at least two iterations.
38   $total = 10;
39   $sleep = (1000000 / $total) * 2;
40
41   $operations = [];
42   for ($i = 1; $i <= $total; $i++) {
43     $operations[] = ['_batch_test_callback_1', [$i, $sleep]];
44   }
45   $batch = [
46     'operations' => $operations,
47     'finished' => '_batch_test_finished_1',
48     'file' => drupal_get_path('module', 'batch_test') . '/batch_test.callbacks.inc',
49   ];
50   return $batch;
51 }
52
53 /**
54  * Batch 2: Performs a single multistep operation.
55  *
56  * Operations: op 2 from 1 to 10.
57  */
58 function _batch_test_batch_2() {
59   // Ensure the batch takes at least two iterations.
60   $total = 10;
61   $sleep = (1000000 / $total) * 2;
62
63   $operations = [
64     ['_batch_test_callback_2', [1, $total, $sleep]],
65   ];
66   $batch = [
67     'operations' => $operations,
68     'finished' => '_batch_test_finished_2',
69     'file' => drupal_get_path('module', 'batch_test') . '/batch_test.callbacks.inc',
70   ];
71   return $batch;
72 }
73
74 /**
75  * Batch 3: Performs both single and multistep operations.
76  *
77  * Operations:
78  * - op 1 from 1 to 5,
79  * - op 2 from 1 to 5,
80  * - op 1 from 6 to 10,
81  * - op 2 from 6 to 10.
82  */
83 function _batch_test_batch_3() {
84   // Ensure the batch takes at least two iterations.
85   $total = 10;
86   $sleep = (1000000 / $total) * 2;
87
88   $operations = [];
89   for ($i = 1; $i <= round($total / 2); $i++) {
90     $operations[] = ['_batch_test_callback_1', [$i, $sleep]];
91   }
92   $operations[] = ['_batch_test_callback_2', [1, $total / 2, $sleep]];
93   for ($i = round($total / 2) + 1; $i <= $total; $i++) {
94     $operations[] = ['_batch_test_callback_1', [$i, $sleep]];
95   }
96   $operations[] = ['_batch_test_callback_2', [6, $total / 2, $sleep]];
97   $batch = [
98     'operations' => $operations,
99     'finished' => '_batch_test_finished_3',
100     'file' => drupal_get_path('module', 'batch_test') . '/batch_test.callbacks.inc',
101   ];
102   return $batch;
103 }
104
105 /**
106  * Batch 4: Performs a batch within a batch.
107  *
108  * Operations:
109  * - op 1 from 1 to 5,
110  * - set batch 2 (op 2 from 1 to 10, should run at the end)
111  * - op 1 from 6 to 10,
112  */
113 function _batch_test_batch_4() {
114   // Ensure the batch takes at least two iterations.
115   $total = 10;
116   $sleep = (1000000 / $total) * 2;
117
118   $operations = [];
119   for ($i = 1; $i <= round($total / 2); $i++) {
120     $operations[] = ['_batch_test_callback_1', [$i, $sleep]];
121   }
122   $operations[] = ['_batch_test_nested_batch_callback', []];
123   for ($i = round($total / 2) + 1; $i <= $total; $i++) {
124     $operations[] = ['_batch_test_callback_1', [$i, $sleep]];
125   }
126   $batch = [
127     'operations' => $operations,
128     'finished' => '_batch_test_finished_4',
129     'file' => drupal_get_path('module', 'batch_test') . '/batch_test.callbacks.inc',
130   ];
131   return $batch;
132 }
133
134 /**
135  * Batch 5: Repeats a simple operation.
136  *
137  * Operations: op 1 from 1 to 10.
138  */
139 function _batch_test_batch_5() {
140   // Ensure the batch takes at least two iterations.
141   $total = 10;
142   $sleep = (1000000 / $total) * 2;
143
144   $operations = [];
145   for ($i = 1; $i <= $total; $i++) {
146     $operations[] = ['_batch_test_callback_5', [$i, $sleep]];
147   }
148   $batch = [
149     'operations' => $operations,
150     'finished' => '_batch_test_finished_5',
151     'file' => drupal_get_path('module', 'batch_test') . '/batch_test.callbacks.inc',
152   ];
153   return $batch;
154 }
155
156 /**
157  * Implements callback_batch_operation().
158  *
159  * Tests the progress page theme.
160  */
161 function _batch_test_theme_callback() {
162   // Because drupalGet() steps through the full progressive batch before
163   // returning control to the test function, we cannot test that the correct
164   // theme is being used on the batch processing page by viewing that page
165   // directly. Instead, we save the theme being used in a variable here, so
166   // that it can be loaded and inspected in the thread running the test.
167   $theme = \Drupal::theme()->getActiveTheme()->getName();
168   batch_test_stack($theme);
169 }
170
171 /**
172  * Tests the title on the progress page by performing a batch callback.
173  */
174 function _batch_test_title_callback() {
175   // Because drupalGet() steps through the full progressive batch before
176   // returning control to the test function, we cannot test that the correct
177   // title is being used on the batch processing page by viewing that page
178   // directly. Instead, we save the title being used in a variable here, so
179   // that it can be loaded and inspected in the thread running the test.
180   $request = \Drupal::request();
181   $route_match = \Drupal::routeMatch();
182   $title = \Drupal::service('title_resolver')->getTitle($request, $route_match->getRouteObject());
183   batch_test_stack($title);
184 }
185
186 /**
187  * Helper function: Stores or retrieves traced execution data.
188  */
189 function batch_test_stack($data = NULL, $reset = FALSE) {
190   if ($reset) {
191     \Drupal::state()->delete('batch_test.stack');
192   }
193   if (!isset($data)) {
194     return \Drupal::state()->get('batch_test.stack');
195   }
196   $stack = \Drupal::state()->get('batch_test.stack');
197   $stack[] = $data;
198   \Drupal::state()->set('batch_test.stack', $stack);
199 }