From f284c45b3fdb1bec3bb5d13af10fe591eadd42b5 Mon Sep 17 00:00:00 2001
From: "omkar.podey" <omkar.podey@3685158.no-reply.drupal.org>
Date: Tue, 18 Oct 2022 17:55:03 +0000
Subject: [PATCH] Issue #3302527 by omkar.podey: Updater::dispatch() doesn't
 need to handle a StageValidationException with no message

---
 .../src/ExtensionUpdater.php                  |  2 +-
 .../tests/src/Kernel/ExtensionUpdaterTest.php | 56 ++++++++++++++++++-
 src/Updater.php                               |  2 +-
 tests/src/Kernel/UpdaterTest.php              | 51 +++++++++++++++++
 4 files changed, 107 insertions(+), 4 deletions(-)

diff --git a/automatic_updates_extensions/src/ExtensionUpdater.php b/automatic_updates_extensions/src/ExtensionUpdater.php
index 7f8a217fa2..167529455a 100644
--- a/automatic_updates_extensions/src/ExtensionUpdater.php
+++ b/automatic_updates_extensions/src/ExtensionUpdater.php
@@ -108,7 +108,7 @@ class ExtensionUpdater extends Stage {
       parent::dispatch($event, $on_error);
     }
     catch (StageValidationException $e) {
-      throw new UpdateException($e->getResults(), $e->getMessage() ?: "Unable to complete the update because of errors.", $e->getCode(), $e);
+      throw new UpdateException($e->getResults(), $e->getMessage(), $e->getCode(), $e);
     }
   }
 
diff --git a/automatic_updates_extensions/tests/src/Kernel/ExtensionUpdaterTest.php b/automatic_updates_extensions/tests/src/Kernel/ExtensionUpdaterTest.php
index 3b74b08ecf..d9d52e72d0 100644
--- a/automatic_updates_extensions/tests/src/Kernel/ExtensionUpdaterTest.php
+++ b/automatic_updates_extensions/tests/src/Kernel/ExtensionUpdaterTest.php
@@ -2,7 +2,12 @@
 
 namespace Drupal\Tests\automatic_updates_extensions\Kernel;
 
-use Drupal\Tests\automatic_updates\Kernel\AutomaticUpdatesKernelTestBase;
+use Drupal\automatic_updates\Exception\UpdateException;
+use Drupal\package_manager\Event\PreApplyEvent;
+use Drupal\package_manager\Event\PreCreateEvent;
+use Drupal\package_manager\Event\PreRequireEvent;
+use Drupal\package_manager\ValidationResult;
+use Drupal\package_manager_test_validation\EventSubscriber\TestSubscriber;
 use Drupal\Tests\user\Traits\UserCreationTrait;
 
 /**
@@ -10,7 +15,7 @@ use Drupal\Tests\user\Traits\UserCreationTrait;
  *
  * @group automatic_updates_extensions
  */
-class ExtensionUpdaterTest extends AutomaticUpdatesKernelTestBase {
+class ExtensionUpdaterTest extends AutomaticUpdatesExtensionsKernelTestBase {
 
   use UserCreationTrait;
 
@@ -146,4 +151,51 @@ class ExtensionUpdaterTest extends AutomaticUpdatesKernelTestBase {
     ]);
   }
 
+  /**
+   * Tests UpdateException handling.
+   *
+   * @param string $event_class
+   *   The stage life cycle event which should raise an error.
+   *
+   * @dataProvider providerUpdateException
+   */
+  public function testUpdateException(string $event_class): void {
+    $this->createVirtualProject(__DIR__ . '/../../fixtures/fake-site');
+    $extension_updater = $this->container->get('automatic_updates_extensions.updater');
+    $results = [
+      ValidationResult::createError(['An error of some sorts.']),
+    ];
+    TestSubscriber::setTestResult($results, $event_class);
+    try {
+      $extension_updater->begin(['my_module' => '9.8.1']);
+      $extension_updater->stage();
+      $extension_updater->apply();
+      $this->fail('Expected an exception, but none was raised.');
+    }
+    catch (UpdateException $e) {
+      $this->assertStringStartsWith('An error of some sorts.', $e->getMessage());
+      $this->assertInstanceOf($event_class, $e->event);
+    }
+  }
+
+  /**
+   * Data provider for testUpdateException().
+   *
+   * @return string[][]
+   *   The test cases.
+   */
+  public function providerUpdateException(): array {
+    return [
+      'pre-create exception' => [
+        PreCreateEvent::class,
+      ],
+      'pre-require exception' => [
+        PreRequireEvent::class,
+      ],
+      'pre-apply exception' => [
+        PreApplyEvent::class,
+      ],
+    ];
+  }
+
 }
diff --git a/src/Updater.php b/src/Updater.php
index 3f25f17e4f..c0ab67cec0 100644
--- a/src/Updater.php
+++ b/src/Updater.php
@@ -102,7 +102,7 @@ class Updater extends Stage {
       parent::dispatch($event, $on_error);
     }
     catch (StageValidationException $e) {
-      throw new UpdateException($e->getResults(), $e->getMessage() ?: "Unable to complete the update because of errors.", $e->getCode(), $e);
+      throw new UpdateException($e->getResults(), $e->getMessage(), $e->getCode(), $e);
     }
   }
 
diff --git a/tests/src/Kernel/UpdaterTest.php b/tests/src/Kernel/UpdaterTest.php
index 916cf0315e..bcd4c684c8 100644
--- a/tests/src/Kernel/UpdaterTest.php
+++ b/tests/src/Kernel/UpdaterTest.php
@@ -3,8 +3,13 @@
 namespace Drupal\Tests\automatic_updates\Kernel;
 
 use Drupal\automatic_updates\Exception\UpdateException;
+use Drupal\package_manager\Event\PreApplyEvent;
+use Drupal\package_manager\Event\PreCreateEvent;
+use Drupal\package_manager\Event\PreRequireEvent;
 use Drupal\package_manager\Exception\StageException;
+use Drupal\package_manager\ValidationResult;
 use Drupal\package_manager_bypass\Committer;
+use Drupal\package_manager_test_validation\EventSubscriber\TestSubscriber;
 use Drupal\Tests\user\Traits\UserCreationTrait;
 use PhpTuf\ComposerStager\Domain\Exception\InvalidArgumentException;
 
@@ -196,4 +201,50 @@ class UpdaterTest extends AutomaticUpdatesKernelTestBase {
     $this->assertSame('setLogger', $updater_method_calls[0][0]);
   }
 
+  /**
+   * Tests UpdateException handling.
+   *
+   * @param string $event_class
+   *   The stage life cycle event which should raise an error.
+   *
+   * @dataProvider providerUpdateException
+   */
+  public function testUpdateException(string $event_class) {
+    $updater = $this->container->get('automatic_updates.updater');
+    $results = [
+      ValidationResult::createError(['An error of some sorts.']),
+    ];
+    TestSubscriber::setTestResult($results, $event_class);
+    try {
+      $updater->begin(['drupal' => '9.8.1']);
+      $updater->stage();
+      $updater->apply();
+      $this->fail('Expected an exception, but none was raised.');
+    }
+    catch (UpdateException $e) {
+      $this->assertStringStartsWith('An error of some sorts.', $e->getMessage());
+      $this->assertInstanceOf($event_class, $e->event);
+    }
+  }
+
+  /**
+   * Data provider for testUpdateException().
+   *
+   * @return string[][]
+   *   The test cases.
+   */
+  public function providerUpdateException(): array {
+    return [
+      'pre-create exception' => [
+        PreCreateEvent::class,
+      ],
+      'pre-require exception' => [
+        PreRequireEvent::class,
+      ],
+      'pre-apply exception' => [
+        PreApplyEvent::class,
+      ],
+    ];
+  }
+
 }
-- 
GitLab