function ProcessingTest::testBatchForm

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()
  2. 8.9.x core/modules/system/tests/src/Functional/Batch/ProcessingTest.php \Drupal\Tests\system\Functional\Batch\ProcessingTest::testBatchForm()
  3. 11.x 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 56

Class

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

Namespace

Drupal\Tests\system\Functional\Batch

Code

public function testBatchForm() : void {
  // Batch 0: no operation.
  $edit = [
    'batch' => 'batch_0',
  ];
  $this->drupalGet('batch-test');
  $this->submitForm($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->assertSession()
    ->assertNoEscaped('<');
  $this->assertBatchMessages($this->_resultMessages('batch_0'));
  $this->assertSession()
    ->pageTextContains('Redirection successful.');
  // Batch 1: several simple operations.
  $edit = [
    'batch' => 'batch_1',
  ];
  $this->drupalGet('batch-test');
  $this->submitForm($edit, 'Submit');
  $this->assertSession()
    ->assertNoEscaped('<');
  $this->assertBatchMessages($this->_resultMessages('batch_1'));
  $this->assertEquals($this->_resultStack('batch_1'), batch_test_stack(), 'Execution order was correct.');
  $this->assertSession()
    ->pageTextContains('Redirection successful.');
  // Batch 2: one multistep operation.
  $edit = [
    'batch' => 'batch_2',
  ];
  $this->drupalGet('batch-test');
  $this->submitForm($edit, 'Submit');
  $this->assertSession()
    ->assertNoEscaped('<');
  $this->assertBatchMessages($this->_resultMessages('batch_2'));
  $this->assertEquals($this->_resultStack('batch_2'), batch_test_stack(), 'Execution order was correct.');
  $this->assertSession()
    ->pageTextContains('Redirection successful.');
  // Batch 3: simple + multistep combined.
  $edit = [
    'batch' => 'batch_3',
  ];
  $this->drupalGet('batch-test');
  $this->submitForm($edit, 'Submit');
  $this->assertSession()
    ->assertNoEscaped('<');
  $this->assertBatchMessages($this->_resultMessages('batch_3'));
  $this->assertEquals($this->_resultStack('batch_3'), batch_test_stack(), 'Execution order was correct.');
  $this->assertSession()
    ->pageTextContains('Redirection successful.');
  // Batch 4: nested batch.
  $edit = [
    'batch' => 'batch_4',
  ];
  $this->drupalGet('batch-test');
  $this->submitForm($edit, 'Submit');
  $this->assertSession()
    ->assertNoEscaped('<');
  $this->assertBatchMessages($this->_resultMessages('batch_4'));
  $this->assertEquals($this->_resultStack('batch_4'), batch_test_stack(), 'Execution order was correct.');
  $this->assertSession()
    ->pageTextContains('Redirection successful.');
  // 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->drupalGet('batch-test');
  $this->submitForm($edit, 'Submit');
  $this->assertSession()
    ->assertNoEscaped('<');
  $this->assertSession()
    ->responseContains('Redirection successful.');
  $this->assertBatchMessages($this->_resultMessages('batch_4'));
  $this->assertBatchMessages($this->_resultMessages('batch_7'));
  $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']);
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.