97dc6e7a5f692beae7367c1dba49a75d30fe3b07
[yaffs-website] / 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    * Triggers a pass if the texts were found in order in the raw content.
180    *
181    * @param $texts
182    *   Array of raw strings to look for .
183    * @param $message
184    *   Message to display.
185    *
186    * @return
187    *   TRUE on pass, FALSE on fail.
188    */
189   public function assertBatchMessages($texts, $message) {
190     $pattern = '|' . implode('.*', $texts) . '|s';
191     return $this->assertPattern($pattern, $message);
192   }
193
194   /**
195    * Returns expected execution stacks for the test batches.
196    */
197   public function _resultStack($id, $value = 0) {
198     $stack = [];
199     switch ($id) {
200       case 'batch_1':
201         for ($i = 1; $i <= 10; $i++) {
202           $stack[] = "op 1 id $i";
203         }
204         break;
205
206       case 'batch_2':
207         for ($i = 1; $i <= 10; $i++) {
208           $stack[] = "op 2 id $i";
209         }
210         break;
211
212       case 'batch_3':
213         for ($i = 1; $i <= 5; $i++) {
214           $stack[] = "op 1 id $i";
215         }
216         for ($i = 1; $i <= 5; $i++) {
217           $stack[] = "op 2 id $i";
218         }
219         for ($i = 6; $i <= 10; $i++) {
220           $stack[] = "op 1 id $i";
221         }
222         for ($i = 6; $i <= 10; $i++) {
223           $stack[] = "op 2 id $i";
224         }
225         break;
226
227       case 'batch_4':
228         for ($i = 1; $i <= 5; $i++) {
229           $stack[] = "op 1 id $i";
230         }
231         $stack[] = 'setting up batch 2';
232         for ($i = 6; $i <= 10; $i++) {
233           $stack[] = "op 1 id $i";
234         }
235         $stack = array_merge($stack, $this->_resultStack('batch_2'));
236         break;
237
238       case 'batch_5':
239         for ($i = 1; $i <= 10; $i++) {
240           $stack[] = "op 5 id $i";
241         }
242         break;
243
244       case 'chained':
245         $stack[] = 'submit handler 1';
246         $stack[] = 'value = ' . $value;
247         $stack = array_merge($stack, $this->_resultStack('batch_1'));
248         $stack[] = 'submit handler 2';
249         $stack[] = 'value = ' . ($value + 1);
250         $stack = array_merge($stack, $this->_resultStack('batch_2'));
251         $stack[] = 'submit handler 3';
252         $stack[] = 'value = ' . ($value + 2);
253         $stack[] = 'submit handler 4';
254         $stack[] = 'value = ' . ($value + 3);
255         $stack = array_merge($stack, $this->_resultStack('batch_3'));
256         break;
257     }
258     return $stack;
259   }
260
261   /**
262    * Returns expected result messages for the test batches.
263    */
264   public function _resultMessages($id) {
265     $messages = [];
266
267     switch ($id) {
268       case 'batch_0':
269         $messages[] = 'results for batch 0<div class="item-list"><ul><li>none</li></ul></div>';
270         break;
271
272       case 'batch_1':
273         $messages[] = 'results for batch 1<div class="item-list"><ul><li>op 1: processed 10 elements</li></ul></div>';
274         break;
275
276       case 'batch_2':
277         $messages[] = 'results for batch 2<div class="item-list"><ul><li>op 2: processed 10 elements</li></ul></div>';
278         break;
279
280       case 'batch_3':
281         $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>';
282         break;
283
284       case 'batch_4':
285         $messages[] = 'results for batch 4<div class="item-list"><ul><li>op 1: processed 10 elements</li></ul></div>';
286         $messages = array_merge($messages, $this->_resultMessages('batch_2'));
287         break;
288
289       case 'batch_5':
290         $messages[] = 'results for batch 5<div class="item-list"><ul><li>op 5: processed 10 elements</li></ul></div>';
291         break;
292
293       case 'chained':
294         $messages = array_merge($messages, $this->_resultMessages('batch_1'));
295         $messages = array_merge($messages, $this->_resultMessages('batch_2'));
296         $messages = array_merge($messages, $this->_resultMessages('batch_3'));
297         break;
298     }
299     return $messages;
300   }
301
302 }