5 * Helper module for the Batch API tests.
8 use Drupal\Core\Form\FormState;
11 * Batch operation: Submits form_test_mock_form().
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);
20 * Batch 0: Does nothing.
22 function _batch_test_batch_0() {
25 'finished' => '_batch_test_finished_0',
26 'file' => drupal_get_path('module', 'batch_test') . '/batch_test.callbacks.inc',
32 * Batch 1: Repeats a simple operation.
34 * Operations: op 1 from 1 to 10.
36 function _batch_test_batch_1() {
37 // Ensure the batch takes at least two iterations.
39 $sleep = (1000000 / $total) * 2;
42 for ($i = 1; $i <= $total; $i++) {
43 $operations[] = ['_batch_test_callback_1', [$i, $sleep]];
46 'operations' => $operations,
47 'finished' => '_batch_test_finished_1',
48 'file' => drupal_get_path('module', 'batch_test') . '/batch_test.callbacks.inc',
54 * Batch 2: Performs a single multistep operation.
56 * Operations: op 2 from 1 to 10.
58 function _batch_test_batch_2() {
59 // Ensure the batch takes at least two iterations.
61 $sleep = (1000000 / $total) * 2;
64 ['_batch_test_callback_2', [1, $total, $sleep]],
67 'operations' => $operations,
68 'finished' => '_batch_test_finished_2',
69 'file' => drupal_get_path('module', 'batch_test') . '/batch_test.callbacks.inc',
75 * Batch 3: Performs both single and multistep operations.
80 * - op 1 from 6 to 10,
81 * - op 2 from 6 to 10.
83 function _batch_test_batch_3() {
84 // Ensure the batch takes at least two iterations.
86 $sleep = (1000000 / $total) * 2;
89 for ($i = 1; $i <= round($total / 2); $i++) {
90 $operations[] = ['_batch_test_callback_1', [$i, $sleep]];
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]];
96 $operations[] = ['_batch_test_callback_2', [6, $total / 2, $sleep]];
98 'operations' => $operations,
99 'finished' => '_batch_test_finished_3',
100 'file' => drupal_get_path('module', 'batch_test') . '/batch_test.callbacks.inc',
106 * Batch 4: Performs a batch within a batch.
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,
113 function _batch_test_batch_4() {
114 // Ensure the batch takes at least two iterations.
116 $sleep = (1000000 / $total) * 2;
119 for ($i = 1; $i <= round($total / 2); $i++) {
120 $operations[] = ['_batch_test_callback_1', [$i, $sleep]];
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]];
127 'operations' => $operations,
128 'finished' => '_batch_test_finished_4',
129 'file' => drupal_get_path('module', 'batch_test') . '/batch_test.callbacks.inc',
135 * Batch 5: Repeats a simple operation.
137 * Operations: op 1 from 1 to 10.
139 function _batch_test_batch_5() {
140 // Ensure the batch takes at least two iterations.
142 $sleep = (1000000 / $total) * 2;
145 for ($i = 1; $i <= $total; $i++) {
146 $operations[] = ['_batch_test_callback_5', [$i, $sleep]];
149 'operations' => $operations,
150 'finished' => '_batch_test_finished_5',
151 'file' => drupal_get_path('module', 'batch_test') . '/batch_test.callbacks.inc',
157 * Implements callback_batch_operation().
159 * Tests the progress page theme.
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);
172 * Tests the title on the progress page by performing a batch callback.
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);
187 * Helper function: Stores or retrieves traced execution data.
189 function batch_test_stack($data = NULL, $reset = FALSE) {
191 \Drupal::state()->delete('batch_test.stack');
194 return \Drupal::state()->get('batch_test.stack');
196 $stack = \Drupal::state()->get('batch_test.stack');
198 \Drupal::state()->set('batch_test.stack', $stack);