2d7c7ae01c527f6fcafa63bc528a94128b9e31e9
[yaffs-website] / web / core / modules / system / tests / src / Functional / Batch / ProcessingTest.php
1 <?php
2
3 namespace Drupal\Tests\system\Functional\Batch;
4
5 use Drupal\Core\Url;
6 use Drupal\Tests\BrowserTestBase;
7
8 /**
9  * Tests batch processing in form and non-form workflow.
10  *
11  * @group Batch
12  */
13 class ProcessingTest extends BrowserTestBase {
14
15   /**
16    * Modules to enable.
17    *
18    * @var array
19    */
20   public static $modules = ['batch_test', 'test_page_test'];
21
22   /**
23    * Tests batches triggered outside of form submission.
24    */
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.');
31   }
32
33   /**
34    * Tests batches that redirect in the batch finished callback.
35    */
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'));
43   }
44
45   /**
46    * Tests batches defined in a form submit handler.
47    */
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.');
55
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.');
63
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.');
71
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.');
79
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.');
87   }
88
89   /**
90    * Tests batches defined in a multistep form.
91    */
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.');
96
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.');
103
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.');
110
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.');
117   }
118
119   /**
120    * Tests batches defined in different submit handlers on the same form.
121    */
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.');
134   }
135
136   /**
137    * Tests batches defined in a programmatically submitted form.
138    *
139    * Same as above, but the form is submitted through drupal_form_execute().
140    */
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.');
152   }
153
154   /**
155    * Test form submission during a batch operation.
156    */
157   public function testDrupalFormSubmitInBatch() {
158     // Displaying the page triggers a batch that programmatically submits a
159     // form.
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.');
163   }
164
165   /**
166    * Tests batches that return $context['finished'] > 1 do in fact complete.
167    *
168    * @see https://www.drupal.org/node/600836
169    */
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.');
176   }
177
178
179   /**
180    * Triggers a pass if the texts were found in order in the raw content.
181    *
182    * @param $texts
183    *   Array of raw strings to look for .
184    * @param $message
185    *   Message to display.
186    *
187    * @return
188    *   TRUE on pass, FALSE on fail.
189    */
190   public function assertBatchMessages($texts, $message) {
191     $pattern = '|' . implode('.*', $texts) . '|s';
192     return $this->assertPattern($pattern, $message);
193   }
194
195   /**
196    * Returns expected execution stacks for the test batches.
197    */
198   public function _resultStack($id, $value = 0) {
199     $stack = [];
200     switch ($id) {
201       case 'batch_1':
202         for ($i = 1; $i <= 10; $i++) {
203           $stack[] = "op 1 id $i";
204         }
205         break;
206
207       case 'batch_2':
208         for ($i = 1; $i <= 10; $i++) {
209           $stack[] = "op 2 id $i";
210         }
211         break;
212
213       case 'batch_3':
214         for ($i = 1; $i <= 5; $i++) {
215           $stack[] = "op 1 id $i";
216         }
217         for ($i = 1; $i <= 5; $i++) {
218           $stack[] = "op 2 id $i";
219         }
220         for ($i = 6; $i <= 10; $i++) {
221           $stack[] = "op 1 id $i";
222         }
223         for ($i = 6; $i <= 10; $i++) {
224           $stack[] = "op 2 id $i";
225         }
226         break;
227
228       case 'batch_4':
229         for ($i = 1; $i <= 5; $i++) {
230           $stack[] = "op 1 id $i";
231         }
232         $stack[] = 'setting up batch 2';
233         for ($i = 6; $i <= 10; $i++) {
234           $stack[] = "op 1 id $i";
235         }
236         $stack = array_merge($stack, $this->_resultStack('batch_2'));
237         break;
238
239       case 'batch_5':
240         for ($i = 1; $i <= 10; $i++) {
241           $stack[] = "op 5 id $i";
242         }
243         break;
244
245       case 'chained':
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'));
257         break;
258     }
259     return $stack;
260   }
261
262   /**
263    * Returns expected result messages for the test batches.
264    */
265   public function _resultMessages($id) {
266     $messages = [];
267
268     switch ($id) {
269       case 'batch_0':
270         $messages[] = 'results for batch 0<div class="item-list"><ul><li>none</li></ul></div>';
271         break;
272
273       case 'batch_1':
274         $messages[] = 'results for batch 1<div class="item-list"><ul><li>op 1: processed 10 elements</li></ul></div>';
275         break;
276
277       case 'batch_2':
278         $messages[] = 'results for batch 2<div class="item-list"><ul><li>op 2: processed 10 elements</li></ul></div>';
279         break;
280
281       case 'batch_3':
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>';
283         break;
284
285       case 'batch_4':
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'));
288         break;
289
290       case 'batch_5':
291         $messages[] = 'results for batch 5<div class="item-list"><ul><li>op 5: processed 10 elements</li></ul></div>';
292         break;
293
294       case 'chained':
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'));
298         break;
299     }
300     return $messages;
301   }
302
303 }