Verified Commit ff0d0f48 authored by Lauri Timmanee's avatar Lauri Timmanee
Browse files

Issue #3406612 by tedbow, phenaproxima: Exceptions in batch no longer are...

Issue #3406612 by tedbow, phenaproxima: Exceptions in batch no longer are shown on the page: JavaScript error

(cherry picked from commit d562edaa)
parent 26446bd2
Loading
Loading
Loading
Loading
Loading
+16 −0
Original line number Diff line number Diff line
@@ -4,6 +4,7 @@

use Drupal\Core\Batch\BatchStorageInterface;
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
@@ -14,6 +15,8 @@
 */
class BatchController implements ContainerInjectionInterface {

  use StringTranslationTrait;

  /**
   * Constructs a new BatchController.
   */
@@ -55,6 +58,19 @@ public function batchPage(Request $request) {
      return $output;
    }
    elseif (isset($output)) {
      // Directly render a status message placeholder without any messages.
      // Messages are not intended to be show on the batch page, but in the
      // event an error in a AJAX callback the messages will be displayed.
      // @todo Remove in https://drupal.org/i/3396099.
      $output['batch_messages'] = [
        '#theme' => 'status_messages',
        '#message_list' => [],
        '#status_headings' => [
          'status' => $this->t('Status message'),
          'error' => $this->t('Error message'),
          'warning' => $this->t('Warning message'),
        ],
      ];
      $title = $output['#title'] ?? NULL;
      $page = [
        '#type' => 'page',
+12 −0
Original line number Diff line number Diff line
@@ -104,6 +104,18 @@ function _batch_test_callback_7($id, $sleep, &$context) {
  $context['results'][7][] = $id;
}

/**
 * Implements callback_batch_operation().
 *
 * Performs a simple batch operation that optionally throws an exception.
 */
function _batch_test_callback_8(bool $throw_exception): void {
  usleep(500);
  if ($throw_exception) {
    throw new Exception('Exception in batch');
  }
}

/**
 * Implements callback_batch_operation().
 *
+11 −0
Original line number Diff line number Diff line
@@ -190,6 +190,17 @@ function _batch_test_batch_7() {
  return $batch_builder->toArray() + ['batch_test_id' => 'batch_7'];
}

/**
 * Batch 8: Throws an exception.
 */
function _batch_test_batch_8(): array {
  $batch_builder = (new BatchBuilder())
    ->setFile(\Drupal::service('extension.list.module')->getPath('batch_test') . '/batch_test.callbacks.inc')
    ->addOperation('_batch_test_callback_8', [FALSE])
    ->addOperation('_batch_test_callback_8', [TRUE]);
  return $batch_builder->toArray() + ['batch_test_id' => 'batch_8'];
}

/**
 * Implements callback_batch_operation().
 *
+1 −0
Original line number Diff line number Diff line
@@ -34,6 +34,7 @@ public function buildForm(array $form, FormStateInterface $form_state) {
        'batch_4' => 'batch 4',
        'batch_6' => 'batch 6',
        'batch_7' => 'batch 7',
        'batch_8' => 'batch 8',
      ],
      '#multiple' => TRUE,
    ];
+40 −0
Original line number Diff line number Diff line
<?php

namespace Drupal\Tests\system\FunctionalJavascript\Batch;

use Drupal\FunctionalJavascriptTests\WebDriverTestBase;

/**
 * @group Batch
 */
class ProcessingTest extends WebDriverTestBase {

  /**
   * Modules to enable.
   *
   * @var array
   */
  protected static $modules = ['batch_test', 'test_page_test'];

  /**
   * {@inheritdoc}
   *
   * @todo Use the stark theme in https://drupal.org/i/3407067.
   */
  protected $defaultTheme = 'olivero';

  /**
   * Tests that a link to the error page is shown.
   */
  public function testLinkToErrorPageAppears(): void {
    $edit = ['batch' => 'batch_8'];
    $this->drupalGet('batch-test');
    $this->submitForm($edit, 'Submit');
    $this->assertNotNull($this->assertSession()->waitForLink('the error page'));
    $this->assertSession()->assertNoEscaped('<');
    $this->assertSession()->responseContains('Exception in batch');
    $this->clickLink('the error page');
    $this->assertSession()->pageTextContains('Redirection successful.');
  }

}