Skip to content
Snippets Groups Projects

Issue #3316611: If unattended updates are enabled send an email when the site stops passing readiness checks

Merged Issue #3316611: If unattended updates are enabled send an email when the site stops passing readiness checks
Compare and Show latest version
3 files
+ 215
73
Compare changes
  • Side-by-side
  • Inline
Files
3
@@ -6,9 +6,11 @@ use Drupal\automatic_updates\CronUpdater;
use Drupal\automatic_updates\Updater;
use Drupal\automatic_updates_test\EventSubscriber\TestSubscriber1;
use Drupal\automatic_updates_test2\EventSubscriber\TestSubscriber2;
use Drupal\Core\Url;
use Drupal\package_manager\Event\StatusCheckEvent;
use Drupal\system\SystemManager;
use Drupal\Tests\automatic_updates\Kernel\AutomaticUpdatesKernelTestBase;
use Drupal\Tests\automatic_updates\Traits\EmailNotificationsTestTrait;
/**
* @coversDefaultClass \Drupal\automatic_updates\Validation\StatusChecker
@@ -17,11 +19,14 @@ use Drupal\Tests\automatic_updates\Kernel\AutomaticUpdatesKernelTestBase;
*/
class StatusCheckerTest extends AutomaticUpdatesKernelTestBase {
use EmailNotificationsTestTrait;
/**
* {@inheritdoc}
*/
protected static $modules = [
'automatic_updates_test',
'package_manager_test_validation',
'user',
];
@@ -278,4 +283,119 @@ class StatusCheckerTest extends AutomaticUpdatesKernelTestBase {
$this->assertNull($this->getResultsFromManager(FALSE));
}
public function testFailureNotifications(): void {
$this->setUpEmailRecipients();
$this->container->get('module_installer')
->install(['automatic_updates']);
// No messages should have been sent yet.
$this->assertEmpty($this->getMails());
$error = $this->createValidationResult(SystemManager::REQUIREMENT_ERROR);
TestSubscriber1::setTestResult([$error], StatusCheckEvent::class);
$this->assertCheckerResultsFromManager([$error], TRUE);
$url = Url::fromRoute('system.status')
->setAbsolute()
->toString();
$expected_body = <<<END
Your site has failed some readiness checks for automatic updates and may not be able to receive automatic updates until further action is taken. Please visit $url for more information.
END;
$this->assertMessagesSent('Automatic updates readiness checks failed', $expected_body);
// Running the status check again should not trigger another e-mail (i.e.,
// each recipient has only been e-mailed once) since the results are
// unchanged.
$recipient_count = count($this->emailRecipients);
$sent_messages_count = $recipient_count;
$this->assertCheckerResultsFromManager([$error], TRUE);
$this->assertCount($sent_messages_count, $this->getMails());
// If a different error is flagged, they should be e-mailed again.
$error = $this->createValidationResult(SystemManager::REQUIREMENT_ERROR);
TestSubscriber1::setTestResult([$error], StatusCheckEvent::class);
$this->assertCheckerResultsFromManager([$error], TRUE);
$sent_messages_count += $recipient_count;
$this->assertCount($sent_messages_count, $this->getMails());
// If a warning is flagged, they should not be e-mailed again since we only
// e-mail for errors by default.
$warning = $this->createValidationResult(SystemManager::REQUIREMENT_WARNING);
TestSubscriber1::setTestResult([$warning], StatusCheckEvent::class);
$this->assertCheckerResultsFromManager([$warning], TRUE);
$this->assertCount($sent_messages_count, $this->getMails());
// If we lower the minimum severity for e-mail notifications, they should
// not be e-mailed again because the results haven't changed since the
// previous status check.
$config = $this->config('automatic_updates.settings')
->set('status_check_notifications.minimum_severity', SystemManager::REQUIREMENT_WARNING);
$config->save();
$this->assertCheckerResultsFromManager([$warning], TRUE);
$this->assertCount($sent_messages_count, $this->getMails());
// If we flag a different warning, they should be e-mailed again.
$warning = $this->createValidationResult(SystemManager::REQUIREMENT_WARNING);
TestSubscriber1::setTestResult([$warning], StatusCheckEvent::class);
$this->assertCheckerResultsFromManager([$warning], TRUE);
$sent_messages_count += $recipient_count;
$this->assertCount($sent_messages_count, $this->getMails());
// If we flag multiple warnings, they should be e-mailed again because the
// number of results has changed, even if the severity hasn't.
$warnings = [
$this->createValidationResult(SystemManager::REQUIREMENT_WARNING),
$this->createValidationResult(SystemManager::REQUIREMENT_WARNING),
];
TestSubscriber1::setTestResult($warnings, StatusCheckEvent::class);
$this->assertCheckerResultsFromManager($warnings, TRUE);
$sent_messages_count += $recipient_count;
$this->assertCount($sent_messages_count, $this->getMails());
// If we flag an error and a warning, they should be e-mailed again because
// the severity has changed, even if the number of results hasn't.
$results = [
$this->createValidationResult(SystemManager::REQUIREMENT_WARNING),
$this->createValidationResult(SystemManager::REQUIREMENT_ERROR),
];
TestSubscriber1::setTestResult($results, StatusCheckEvent::class);
$this->assertCheckerResultsFromManager($results, TRUE);
$sent_messages_count += $recipient_count;
$this->assertCount($sent_messages_count, $this->getMails());
// If we disable notifications entirely, they should not be e-mailed even
// if a different error is flagged.
$config->set('status_check_notifications.enabled', FALSE)->save();
$error = $this->createValidationResult(SystemManager::REQUIREMENT_ERROR);
TestSubscriber1::setTestResult([$error], StatusCheckEvent::class);
$this->assertCheckerResultsFromManager([$error], TRUE);
$this->assertCount($sent_messages_count, $this->getMails());
// If we re-enable notifications and raise the minimum severity, they should
// not be e-mailed if a new warning is flagged.
$config->set('status_check_notifications', [
'enabled' => TRUE,
'minimum_severity' => SystemManager::REQUIREMENT_ERROR,
])->save();
$warning = $this->createValidationResult(SystemManager::REQUIREMENT_WARNING);
TestSubscriber1::setTestResult([$warning], StatusCheckEvent::class);
$this->assertCheckerResultsFromManager([$warning], TRUE);
$this->assertCount($sent_messages_count, $this->getMails());
// Finally, if we disable unattended updates entirely and flag a new error,
// they should not be e-mailed.
$config->set('cron', CronUpdater::DISABLED)->save();
$error = $this->createValidationResult(SystemManager::REQUIREMENT_ERROR);
TestSubscriber1::setTestResult([$error], StatusCheckEvent::class);
$this->assertCheckerResultsFromManager([$error], TRUE);
$this->assertCount($sent_messages_count, $this->getMails());
// As a final sanity check, ensure that every e-mail we've sent has been a
// status check failure notification.
foreach ($this->getMails() as $sent_message) {
$this->assertSame('automatic_updates_status_check_failed', $sent_message['id']);
}
}
}
Loading