From 787cc8ab26cd80acc4142448702127a5333835fe Mon Sep 17 00:00:00 2001
From: Lee Rowlands <lee.rowlands@previousnext.com.au>
Date: Fri, 3 Jan 2025 09:55:16 +1000
Subject: [PATCH] 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

---
 core/includes/batch.inc                       |  5 +-
 .../Functional/Batch/BatchNotFoundTest.php    | 49 +++++++++++++++++++
 2 files changed, 52 insertions(+), 2 deletions(-)
 create mode 100644 core/modules/system/tests/src/Functional/Batch/BatchNotFoundTest.php

diff --git a/core/includes/batch.inc b/core/includes/batch.inc
index 3c80ee1d1db8..080b2867bc51 100644
--- a/core/includes/batch.inc
+++ b/core/includes/batch.inc
@@ -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));
     }
   }
 
diff --git a/core/modules/system/tests/src/Functional/Batch/BatchNotFoundTest.php b/core/modules/system/tests/src/Functional/Batch/BatchNotFoundTest.php
new file mode 100644
index 000000000000..97abd54759f3
--- /dev/null
+++ b/core/modules/system/tests/src/Functional/Batch/BatchNotFoundTest.php
@@ -0,0 +1,49 @@
+<?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);
+  }
+
+}
-- 
GitLab