3 namespace Drupal\Tests\system\Functional\Batch;
6 use Drupal\Tests\BrowserTestBase;
9 * Tests batch processing in form and non-form workflow.
13 class ProcessingTest extends BrowserTestBase {
20 public static $modules = ['batch_test', 'test_page_test'];
23 * Tests batches triggered outside of form submission.
25 public function testBatchNoForm() {
26 // Displaying the page triggers batch 1.
27 $this->drupalGet('batch-test/no-form');
28 $this->assertBatchMessages($this->_resultMessages('batch_1'), 'Batch for step 2 performed successfully.');
29 $this->assertEqual(batch_test_stack(), $this->_resultStack('batch_1'), 'Execution order was correct.');
30 $this->assertText('Redirection successful.', 'Redirection after batch execution is correct.');
34 * Tests batches that redirect in the batch finished callback.
36 public function testBatchRedirectFinishedCallback() {
37 // Displaying the page triggers batch 1.
38 $this->drupalGet('batch-test/finish-redirect');
39 $this->assertBatchMessages($this->_resultMessages('batch_1'), 'Batch for step 2 performed successfully.');
40 $this->assertEqual(batch_test_stack(), $this->_resultStack('batch_1'), 'Execution order was correct.');
41 $this->assertText('Test page text.', 'Custom redirection after batch execution displays the correct page.');
42 $this->assertUrl(Url::fromRoute('test_page_test.test_page'));
46 * Tests batches defined in a form submit handler.
48 public function testBatchForm() {
49 // Batch 0: no operation.
50 $edit = ['batch' => 'batch_0'];
51 $this->drupalPostForm('batch-test', $edit, 'Submit');
52 $this->assertNoEscaped('<', 'No escaped markup is present.');
53 $this->assertBatchMessages($this->_resultMessages('batch_0'), 'Batch with no operation performed successfully.');
54 $this->assertText('Redirection successful.', 'Redirection after batch execution is correct.');
56 // Batch 1: several simple operations.
57 $edit = ['batch' => 'batch_1'];
58 $this->drupalPostForm('batch-test', $edit, 'Submit');
59 $this->assertNoEscaped('<', 'No escaped markup is present.');
60 $this->assertBatchMessages($this->_resultMessages('batch_1'), 'Batch with simple operations performed successfully.');
61 $this->assertEqual(batch_test_stack(), $this->_resultStack('batch_1'), 'Execution order was correct.');
62 $this->assertText('Redirection successful.', 'Redirection after batch execution is correct.');
64 // Batch 2: one multistep operation.
65 $edit = ['batch' => 'batch_2'];
66 $this->drupalPostForm('batch-test', $edit, 'Submit');
67 $this->assertNoEscaped('<', 'No escaped markup is present.');
68 $this->assertBatchMessages($this->_resultMessages('batch_2'), 'Batch with multistep operation performed successfully.');
69 $this->assertEqual(batch_test_stack(), $this->_resultStack('batch_2'), 'Execution order was correct.');
70 $this->assertText('Redirection successful.', 'Redirection after batch execution is correct.');
72 // Batch 3: simple + multistep combined.
73 $edit = ['batch' => 'batch_3'];
74 $this->drupalPostForm('batch-test', $edit, 'Submit');
75 $this->assertNoEscaped('<', 'No escaped markup is present.');
76 $this->assertBatchMessages($this->_resultMessages('batch_3'), 'Batch with simple and multistep operations performed successfully.');
77 $this->assertEqual(batch_test_stack(), $this->_resultStack('batch_3'), 'Execution order was correct.');
78 $this->assertText('Redirection successful.', 'Redirection after batch execution is correct.');
80 // Batch 4: nested batch.
81 $edit = ['batch' => 'batch_4'];
82 $this->drupalPostForm('batch-test', $edit, 'Submit');
83 $this->assertNoEscaped('<', 'No escaped markup is present.');
84 $this->assertBatchMessages($this->_resultMessages('batch_4'), 'Nested batch performed successfully.');
85 $this->assertEqual(batch_test_stack(), $this->_resultStack('batch_4'), 'Execution order was correct.');
86 $this->assertText('Redirection successful.', 'Redirection after batch execution is correct.');
90 * Tests batches defined in a multistep form.
92 public function testBatchFormMultistep() {
93 $this->drupalGet('batch-test/multistep');
94 $this->assertNoEscaped('<', 'No escaped markup is present.');
95 $this->assertText('step 1', 'Form is displayed in step 1.');
97 // First step triggers batch 1.
98 $this->drupalPostForm(NULL, [], 'Submit');
99 $this->assertBatchMessages($this->_resultMessages('batch_1'), 'Batch for step 1 performed successfully.');
100 $this->assertEqual(batch_test_stack(), $this->_resultStack('batch_1'), 'Execution order was correct.');
101 $this->assertText('step 2', 'Form is displayed in step 2.');
102 $this->assertNoEscaped('<', 'No escaped markup is present.');
104 // Second step triggers batch 2.
105 $this->drupalPostForm(NULL, [], 'Submit');
106 $this->assertBatchMessages($this->_resultMessages('batch_2'), 'Batch for step 2 performed successfully.');
107 $this->assertEqual(batch_test_stack(), $this->_resultStack('batch_2'), 'Execution order was correct.');
108 $this->assertText('Redirection successful.', 'Redirection after batch execution is correct.');
109 $this->assertNoEscaped('<', 'No escaped markup is present.');
111 // Extra query arguments will trigger logic that will add them to the
112 // redirect URL. Make sure they are persisted.
113 $this->drupalGet('batch-test/multistep', ['query' => ['big_tree' => 'small_axe']]);
114 $this->drupalPostForm(NULL, [], 'Submit');
115 $this->assertText('step 2', 'Form is displayed in step 2.');
116 $this->assertTrue(strpos($this->getUrl(), 'batch-test/multistep?big_tree=small_axe'), 'Query argument was persisted and another extra argument was added.');
120 * Tests batches defined in different submit handlers on the same form.
122 public function testBatchFormMultipleBatches() {
123 // Batches 1, 2 and 3 are triggered in sequence by different submit
124 // handlers. Each submit handler modify the submitted 'value'.
125 $value = rand(0, 255);
126 $edit = ['value' => $value];
127 $this->drupalPostForm('batch-test/chained', $edit, 'Submit');
128 // Check that result messages are present and in the correct order.
129 $this->assertBatchMessages($this->_resultMessages('chained'), 'Batches defined in separate submit handlers performed successfully.');
130 // The stack contains execution order of batch callbacks and submit
131 // handlers and logging of corresponding $form_state->getValues().
132 $this->assertEqual(batch_test_stack(), $this->_resultStack('chained', $value), 'Execution order was correct, and $form_state is correctly persisted.');
133 $this->assertText('Redirection successful.', 'Redirection after batch execution is correct.');
137 * Tests batches defined in a programmatically submitted form.
139 * Same as above, but the form is submitted through drupal_form_execute().
141 public function testBatchFormProgrammatic() {
142 // Batches 1, 2 and 3 are triggered in sequence by different submit
143 // handlers. Each submit handler modify the submitted 'value'.
144 $value = rand(0, 255);
145 $this->drupalGet('batch-test/programmatic/' . $value);
146 // Check that result messages are present and in the correct order.
147 $this->assertBatchMessages($this->_resultMessages('chained'), 'Batches defined in separate submit handlers performed successfully.');
148 // The stack contains execution order of batch callbacks and submit
149 // handlers and logging of corresponding $form_state->getValues().
150 $this->assertEqual(batch_test_stack(), $this->_resultStack('chained', $value), 'Execution order was correct, and $form_state is correctly persisted.');
151 $this->assertText('Got out of a programmatic batched form.', 'Page execution continues normally.');
155 * Test form submission during a batch operation.
157 public function testDrupalFormSubmitInBatch() {
158 // Displaying the page triggers a batch that programmatically submits a
160 $value = rand(0, 255);
161 $this->drupalGet('batch-test/nested-programmatic/' . $value);
162 $this->assertEqual(batch_test_stack(), ['mock form submitted with value = ' . $value], '\Drupal::formBuilder()->submitForm() ran successfully within a batch operation.');
166 * Tests batches that return $context['finished'] > 1 do in fact complete.
168 * @see https://www.drupal.org/node/600836
170 public function testBatchLargePercentage() {
171 // Displaying the page triggers batch 5.
172 $this->drupalGet('batch-test/large-percentage');
173 $this->assertBatchMessages($this->_resultMessages('batch_5'), 'Batch for step 2 performed successfully.');
174 $this->assertEqual(batch_test_stack(), $this->_resultStack('batch_5'), 'Execution order was correct.');
175 $this->assertText('Redirection successful.', 'Redirection after batch execution is correct.');
180 * Triggers a pass if the texts were found in order in the raw content.
183 * Array of raw strings to look for .
185 * Message to display.
188 * TRUE on pass, FALSE on fail.
190 public function assertBatchMessages($texts, $message) {
191 $pattern = '|' . implode('.*', $texts) . '|s';
192 return $this->assertPattern($pattern, $message);
196 * Returns expected execution stacks for the test batches.
198 public function _resultStack($id, $value = 0) {
202 for ($i = 1; $i <= 10; $i++) {
203 $stack[] = "op 1 id $i";
208 for ($i = 1; $i <= 10; $i++) {
209 $stack[] = "op 2 id $i";
214 for ($i = 1; $i <= 5; $i++) {
215 $stack[] = "op 1 id $i";
217 for ($i = 1; $i <= 5; $i++) {
218 $stack[] = "op 2 id $i";
220 for ($i = 6; $i <= 10; $i++) {
221 $stack[] = "op 1 id $i";
223 for ($i = 6; $i <= 10; $i++) {
224 $stack[] = "op 2 id $i";
229 for ($i = 1; $i <= 5; $i++) {
230 $stack[] = "op 1 id $i";
232 $stack[] = 'setting up batch 2';
233 for ($i = 6; $i <= 10; $i++) {
234 $stack[] = "op 1 id $i";
236 $stack = array_merge($stack, $this->_resultStack('batch_2'));
240 for ($i = 1; $i <= 10; $i++) {
241 $stack[] = "op 5 id $i";
246 $stack[] = 'submit handler 1';
247 $stack[] = 'value = ' . $value;
248 $stack = array_merge($stack, $this->_resultStack('batch_1'));
249 $stack[] = 'submit handler 2';
250 $stack[] = 'value = ' . ($value + 1);
251 $stack = array_merge($stack, $this->_resultStack('batch_2'));
252 $stack[] = 'submit handler 3';
253 $stack[] = 'value = ' . ($value + 2);
254 $stack[] = 'submit handler 4';
255 $stack[] = 'value = ' . ($value + 3);
256 $stack = array_merge($stack, $this->_resultStack('batch_3'));
263 * Returns expected result messages for the test batches.
265 public function _resultMessages($id) {
270 $messages[] = 'results for batch 0<div class="item-list"><ul><li>none</li></ul></div>';
274 $messages[] = 'results for batch 1<div class="item-list"><ul><li>op 1: processed 10 elements</li></ul></div>';
278 $messages[] = 'results for batch 2<div class="item-list"><ul><li>op 2: processed 10 elements</li></ul></div>';
282 $messages[] = 'results for batch 3<div class="item-list"><ul><li>op 1: processed 10 elements</li><li>op 2: processed 10 elements</li></ul></div>';
286 $messages[] = 'results for batch 4<div class="item-list"><ul><li>op 1: processed 10 elements</li></ul></div>';
287 $messages = array_merge($messages, $this->_resultMessages('batch_2'));
291 $messages[] = 'results for batch 5<div class="item-list"><ul><li>op 5: processed 10 elements</li></ul></div>';
295 $messages = array_merge($messages, $this->_resultMessages('batch_1'));
296 $messages = array_merge($messages, $this->_resultMessages('batch_2'));
297 $messages = array_merge($messages, $this->_resultMessages('batch_3'));