You are here

public function ProcessingTest::testBatchForm in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/system/tests/src/Functional/Batch/ProcessingTest.php \Drupal\Tests\system\Functional\Batch\ProcessingTest::testBatchForm()

Tests batches defined in a form submit handler.

File

core/modules/system/tests/src/Functional/Batch/ProcessingTest.php, line 53

Class

ProcessingTest
Tests batch processing in form and non-form workflow.

Namespace

Drupal\Tests\system\Functional\Batch

Code

public function testBatchForm() {

  // Batch 0: no operation.
  $edit = [
    'batch' => 'batch_0',
  ];
  $this
    ->drupalPostForm('batch-test', $edit, 'Submit');

  // If there is any escaped markup it will include at least an escaped '<'
  // character, so assert on each page that there is no escaped '<' as a way
  // of verifying that no markup is incorrectly escaped.
  $this
    ->assertNoEscaped('<');
  $this
    ->assertBatchMessages($this
    ->_resultMessages('batch_0'), 'Batch with no operation performed successfully.');
  $this
    ->assertText('Redirection successful.', 'Redirection after batch execution is correct.');

  // Batch 1: several simple operations.
  $edit = [
    'batch' => 'batch_1',
  ];
  $this
    ->drupalPostForm('batch-test', $edit, 'Submit');
  $this
    ->assertNoEscaped('<');
  $this
    ->assertBatchMessages($this
    ->_resultMessages('batch_1'), 'Batch with simple operations performed successfully.');
  $this
    ->assertEqual(batch_test_stack(), $this
    ->_resultStack('batch_1'), 'Execution order was correct.');
  $this
    ->assertText('Redirection successful.', 'Redirection after batch execution is correct.');

  // Batch 2: one multistep operation.
  $edit = [
    'batch' => 'batch_2',
  ];
  $this
    ->drupalPostForm('batch-test', $edit, 'Submit');
  $this
    ->assertNoEscaped('<');
  $this
    ->assertBatchMessages($this
    ->_resultMessages('batch_2'), 'Batch with multistep operation performed successfully.');
  $this
    ->assertEqual(batch_test_stack(), $this
    ->_resultStack('batch_2'), 'Execution order was correct.');
  $this
    ->assertText('Redirection successful.', 'Redirection after batch execution is correct.');

  // Batch 3: simple + multistep combined.
  $edit = [
    'batch' => 'batch_3',
  ];
  $this
    ->drupalPostForm('batch-test', $edit, 'Submit');
  $this
    ->assertNoEscaped('<');
  $this
    ->assertBatchMessages($this
    ->_resultMessages('batch_3'), 'Batch with simple and multistep operations performed successfully.');
  $this
    ->assertEqual(batch_test_stack(), $this
    ->_resultStack('batch_3'), 'Execution order was correct.');
  $this
    ->assertText('Redirection successful.', 'Redirection after batch execution is correct.');

  // Batch 4: nested batch.
  $edit = [
    'batch' => 'batch_4',
  ];
  $this
    ->drupalPostForm('batch-test', $edit, 'Submit');
  $this
    ->assertNoEscaped('<');
  $this
    ->assertBatchMessages($this
    ->_resultMessages('batch_4'), 'Nested batch performed successfully.');
  $this
    ->assertEqual(batch_test_stack(), $this
    ->_resultStack('batch_4'), 'Execution order was correct.');
  $this
    ->assertText('Redirection successful.', 'Redirection after batch execution is correct.');

  // Submit batches 4 and 7. Batch 4 will trigger batch 2. Batch 7 will
  // trigger batches 6 and 5.
  $edit = [
    'batch' => [
      'batch_4',
      'batch_7',
    ],
  ];
  $this
    ->drupalPostForm('batch-test', $edit, 'Submit');
  $this
    ->assertSession()
    ->assertNoEscaped('<');
  $this
    ->assertSession()
    ->responseContains('Redirection successful.');
  $this
    ->assertBatchMessages($this
    ->_resultMessages('batch_4'), 'Nested batch performed successfully.');
  $this
    ->assertBatchMessages($this
    ->_resultMessages('batch_7'), 'Nested batch performed successfully.');
  $expected_stack = array_merge($this
    ->_resultStack('batch_4'), $this
    ->_resultStack('batch_7'));
  $this
    ->assertEquals($expected_stack, batch_test_stack(), 'Execution order was correct.');
  $batch = \Drupal::state()
    ->get('batch_test_nested_order_multiple_batches');
  $this
    ->assertCount(5, $batch['sets']);

  // Ensure correct queue mapping.
  foreach ($batch['sets'] as $index => $batch_set) {
    $this
      ->assertEquals('drupal_batch:' . $batch['id'] . ':' . $index, $batch_set['queue']['name']);
  }

  // Ensure correct order of the nested batches. We reset the indexes in
  // order to directly access the batches by their order.
  $batch_sets = array_values($batch['sets']);
  $this
    ->assertEquals('batch_4', $batch_sets[0]['batch_test_id']);
  $this
    ->assertEquals('batch_2', $batch_sets[1]['batch_test_id']);
  $this
    ->assertEquals('batch_7', $batch_sets[2]['batch_test_id']);
  $this
    ->assertEquals('batch_6', $batch_sets[3]['batch_test_id']);
  $this
    ->assertEquals('batch_5', $batch_sets[4]['batch_test_id']);
}