From bdc38a94015f98eace3e8ba378941b5bef09fdbc Mon Sep 17 00:00:00 2001 From: Kunal Sachdev <57170-kunal.sachdev@users.noreply.drupalcode.org> Date: Wed, 21 Jun 2023 16:50:56 +0000 Subject: [PATCH] Issue #3366499 by kunal.sachdev, phenaproxima: In CronUpdateStage if postApply() fails no update email will be sent --- src/CronUpdateStage.php | 34 ++++++++++++------------ tests/src/Kernel/CronUpdateStageTest.php | 27 +++++++++++++++++++ 2 files changed, 44 insertions(+), 17 deletions(-) diff --git a/src/CronUpdateStage.php b/src/CronUpdateStage.php index 533256f783..15f39ee97d 100644 --- a/src/CronUpdateStage.php +++ b/src/CronUpdateStage.php @@ -355,28 +355,28 @@ class CronUpdateStage extends UpdateStage implements CronInterface { $this->claim($stage_id); + $this->logger->info( + 'Drupal core has been updated from %previous_version to %target_version', + [ + '%previous_version' => $installed_version, + '%target_version' => $target_version, + ] + ); + + // Send notifications about the successful update. + $mail_params = [ + 'previous_version' => $installed_version, + 'updated_version' => $target_version, + ]; + foreach ($this->statusCheckMailer->getRecipients() as $recipient => $langcode) { + $this->mailManager->mail('automatic_updates', 'cron_successful', $recipient, $langcode, $mail_params); + } + // Run post-apply tasks in their own try-catch block so that, if anything // raises an exception, we'll log it and proceed to destroy the stage as // soon as possible (which is also what we do in ::performUpdate()). try { $this->postApply(); - - $this->logger->info( - 'Drupal core has been updated from %previous_version to %target_version', - [ - '%previous_version' => $installed_version, - '%target_version' => $target_version, - ] - ); - - // Send notifications about the successful update. - $mail_params = [ - 'previous_version' => $installed_version, - 'updated_version' => $target_version, - ]; - foreach ($this->statusCheckMailer->getRecipients() as $recipient => $langcode) { - $this->mailManager->mail('automatic_updates', 'cron_successful', $recipient, $langcode, $mail_params); - } } catch (\Throwable $e) { $this->logger->error($e->getMessage()); diff --git a/tests/src/Kernel/CronUpdateStageTest.php b/tests/src/Kernel/CronUpdateStageTest.php index b13e2d0ee3..b94b20086f 100644 --- a/tests/src/Kernel/CronUpdateStageTest.php +++ b/tests/src/Kernel/CronUpdateStageTest.php @@ -462,6 +462,33 @@ END; $this->assertRegularCronRun(FALSE); } + /** + * Tests that a success email is sent even when post-apply tasks fail. + */ + public function testEmailSentIfPostApplyFails(): void { + $this->getStageFixtureManipulator()->setCorePackageVersion('9.8.1'); + + $exception = new \Exception('Error during running post-apply tasks!'); + TestSubscriber1::setException($exception, PostApplyEvent::class); + + $this->container->get('cron')->run(); + $this->assertRegularCronRun(FALSE); + $this->assertTrue($this->logger->hasRecord($exception->getMessage(), (string) RfcLogLevel::ERROR)); + + // Ensure we sent a success email to all recipients, even though post-apply + // tasks failed. + $expected_body = <<<END +Congratulations! + +Drupal core was automatically updated from 9.8.0 to 9.8.1. + +This e-mail was sent by the Automatic Updates module. Unattended updates are not yet fully supported. + +If you are using this feature in production, it is strongly recommended for you to visit your site and ensure that everything still looks good. +END; + $this->assertMessagesSent("Drupal core was successfully updated", $expected_body); + } + /** * Tests that regular cron runs if not update is available. */ -- GitLab