Verified Commit 34fe29fb authored by Lee Rowlands's avatar Lee Rowlands
Browse files

Issue #1986330 by bhanu951, subhojit777, marcelodornelas, ravi.shankar,...

Issue #1986330 by bhanu951, subhojit777, marcelodornelas, ravi.shankar, immaculatexavier, wheatpenny, nikunjkotecha, shashikant_chauhan, vacho, smagdits, kasperg, aron.beal, andriansyah, suresh prabhu parkala, rajeevk, benjifisher, mangy.fox, cebasqueira, stefank, richardcanoe, quietone, yesct, AkshayKalose, drdam, DevElCuy, john cook, moymilo, alexpott, xjm, dawehner, tstoeckler, webchick, akashkumar07: When Batch ID doesn't exist, Drupal should emit a 404

(cherry picked from commit 787cc8ab)
parent c4087dec
Loading
Loading
Loading
Loading
Loading
+3 −2
Original line number Diff line number Diff line
@@ -22,6 +22,7 @@
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;

/**
 * Renders the batch processing page based on the current state of the batch.
@@ -42,8 +43,8 @@ function _batch_page(Request $request) {
  if (!$batch) {
    $batch = \Drupal::service('batch.storage')->load($request_id);
    if (!$batch) {
      \Drupal::messenger()->addError(t('No active batch.'));
      return new RedirectResponse(Url::fromRoute('<front>', [], ['absolute' => TRUE])->toString());
      \Drupal::messenger()->addError(t('No batch with ID @batch exists.', ['@batch' => $request_id]));
      throw new NotFoundHttpException(sprintf('No batch with ID %s exists.', $request_id));
    }
  }

+49 −0
Original line number Diff line number Diff line
<?php

declare(strict_types=1);

namespace Drupal\Tests\system\Functional\Batch;

use Drupal\Core\Batch\BatchStorageInterface;
use Drupal\Tests\BrowserTestBase;

/**
 * Tests if a page not found error is returned when a batch ID does not exist.
 *
 * @group Batch
 */
class BatchNotFoundTest extends BrowserTestBase {

  /**
   * {@inheritdoc}
   */
  protected static $modules = ['batch_test'];

  /**
   * {@inheritdoc}
   */
  protected $defaultTheme = 'stark';

  /**
   * Tests for page not found error if batch ID does not exist.
   */
  public function testBatchNotFound(): void {

    $edit = ['batch' => 'batch_0'];
    $this->drupalGet('batch-test');
    $this->submitForm($edit, 'Submit');
    $this->assertSession()->statusCodeEquals(200);

    $batch_id = \Drupal::service(BatchStorageInterface::class)->getId();

    $this->drupalGet('batch', [
      'query' => [
        'op' => 'start',
        'id' => $batch_id,
      ],
    ]);

    $this->assertSession()->statusCodeEquals(404);
  }

}