From 8d56ffabdc39cb08257b871b2e943500421d03de Mon Sep 17 00:00:00 2001
From: "kunal.sachdev" <kunal.sachdev@3685163.no-reply.drupal.org>
Date: Thu, 3 Mar 2022 13:37:09 +0000
Subject: [PATCH] Issue #3265873 by kunal.sachdev: Destroy the update stage if
 there is an error requiring a core version

---
 src/BatchProcessor.php                   |  1 +
 tests/src/Functional/UpdaterFormTest.php | 28 ++++++++++++++++++++++++
 2 files changed, 29 insertions(+)

diff --git a/src/BatchProcessor.php b/src/BatchProcessor.php
index 10d8462bd2..4b73583cd1 100644
--- a/src/BatchProcessor.php
+++ b/src/BatchProcessor.php
@@ -95,6 +95,7 @@ class BatchProcessor {
       static::getUpdater()->claim($stage_id)->stage();
     }
     catch (\Throwable $e) {
+      static::clean($stage_id, $context);
       static::handleException($e, $context);
     }
   }
diff --git a/tests/src/Functional/UpdaterFormTest.php b/tests/src/Functional/UpdaterFormTest.php
index d257fe3182..c94abdd82b 100644
--- a/tests/src/Functional/UpdaterFormTest.php
+++ b/tests/src/Functional/UpdaterFormTest.php
@@ -4,6 +4,7 @@ namespace Drupal\Tests\automatic_updates\Functional;
 
 use Drupal\automatic_updates\Event\ReadinessCheckEvent;
 use Drupal\automatic_updates_test\Datetime\TestTime;
+use Drupal\package_manager\Event\PostRequireEvent;
 use Drupal\package_manager\Event\PreApplyEvent;
 use Drupal\package_manager\Event\PreCreateEvent;
 use Drupal\package_manager\ValidationResult;
@@ -413,4 +414,31 @@ class UpdaterFormTest extends AutomaticUpdatesFunctionalTestBase {
     $assert_session->pageTextContainsOnce('Update complete!');
   }
 
+  /**
+   * Tests that the update stage is destroyed if an error occurs during require.
+   */
+  public function testStageDestroyedOnError(): void {
+    $session = $this->getSession();
+    $assert_session = $this->assertSession();
+    $page = $session->getPage();
+    $this->setCoreVersion('9.8.0');
+    $this->checkForUpdates();
+
+    $this->drupalGet('/admin/modules/automatic-update');
+    $error = new \Exception('Some Exception');
+    TestSubscriber1::setException($error, PostRequireEvent::class);
+    $assert_session->pageTextNotContains(static::$errorsExplanation);
+    $assert_session->pageTextNotContains(static::$warningsExplanation);
+    $page->pressButton('Update');
+    $this->checkForMetaRefresh();
+    $this->assertUpdateStagedTimes(1);
+    $assert_session->pageTextContainsOnce('An error has occurred.');
+    $page->clickLink('the error page');
+    $assert_session->addressEquals('/admin/modules/automatic-update');
+    $assert_session->pageTextNotContains('Cannot begin an update because another Composer operation is currently in progress.');
+    $assert_session->buttonNotExists('Delete existing update');
+    $assert_session->pageTextContains('Some Exception');
+    $assert_session->buttonExists('Update');
+  }
+
 }
-- 
GitLab