diff --git a/automatic_updates_extensions/src/ExtensionUpdater.php b/automatic_updates_extensions/src/ExtensionUpdater.php index 7f8a217fa2a4be5289a852c5093c4cffce9beb9a..167529455a9a6bedf7caf64a446cdfa1f1c4e930 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 3b74b08ecff0e9c9c3999b97eedcab8a2e913935..d9d52e72d0ef15ee1d93be9fffe1a17aea559580 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 3f25f17e4ff5621cb782412b890abacbe276fe11..c0ab67cec05f49859531759d7f8504ca2e5886ee 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 916cf0315e8ff2c93ec64b95c9917aa08dbf4f5b..bcd4c684c8c60946564332a2df70047d26e65e67 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, + ], + ]; + } + }