diff --git a/automatic_updates.module b/automatic_updates.module index 2c798000f84278de19c6351490356771203002db..7e32da52076f4def9dd40dae21c9e1f4ed947e8a 100644 --- a/automatic_updates.module +++ b/automatic_updates.module @@ -196,12 +196,22 @@ function automatic_updates_cron() { /** * Implements hook_modules_installed(). */ -function automatic_updates_modules_installed() { +function automatic_updates_modules_installed($modules) { // Run the status checkers if needed when any modules are installed in // case they provide status checkers. /** @var \Drupal\automatic_updates\Validation\StatusChecker $status_checker */ $status_checker = \Drupal::service('automatic_updates.status_checker'); $status_checker->run(); + /** @var \Drupal\automatic_updates\CronUpdater $cron_updater */ + $cron_updater = \Drupal::service('automatic_updates.cron_updater'); + // If cron updates are disabled status check messages will not be displayed on + // admin pages. Therefore, after installing the module the user will not be + // alerted to any problems until they access the status report page. + if ($cron_updater->getMode() === CronUpdater::DISABLED) { + /** @var \Drupal\automatic_updates\Validation\AdminStatusCheckMessages $status_check_messages */ + $status_check_messages = \Drupal::classResolver(AdminStatusCheckMessages::class); + $status_check_messages->displayResultSummary(); + } } /** diff --git a/src/Validation/AdminStatusCheckMessages.php b/src/Validation/AdminStatusCheckMessages.php index eeadbdcf5c1debe8c9ca0f64abfc67cb7389cc14..b780799ca7cc23e1b28bc0136384c621e9f34fac 100644 --- a/src/Validation/AdminStatusCheckMessages.php +++ b/src/Validation/AdminStatusCheckMessages.php @@ -16,6 +16,7 @@ use Drupal\Core\Session\AccountProxyInterface; use Drupal\Core\StringTranslation\StringTranslationTrait; use Drupal\Core\StringTranslation\TranslationInterface; use Drupal\Core\Url; +use Drupal\package_manager\ValidationResult; use Drupal\system\SystemManager; use Symfony\Component\DependencyInjection\ContainerInterface; @@ -191,4 +192,34 @@ final class AdminStatusCheckMessages implements ContainerInjectionInterface { return TRUE; } + /** + * Displays the result summary. + */ + public function displayResultSummary(): void { + if (!$this->currentUser->hasPermission('administer site configuration')) { + return; + } + $results = $this->statusChecker->getResults(); + if (empty($results)) { + return; + } + // First message: severity. + $overall_severity = ValidationResult::getOverallSeverity($results); + $message = $this->getFailureMessageForSeverity($overall_severity); + $message_type = $overall_severity === SystemManager::REQUIREMENT_ERROR ? MessengerInterface::TYPE_ERROR : MessengerInterface::TYPE_WARNING; + $this->messenger()->addMessage($message, $message_type); + + // Optional second message: more details (for users with sufficient + // permissions). + $status_report_url = Url::fromRoute('system.status'); + if ($status_report_url->access()) { + $this->messenger()->addMessage( + $this->t('<a href=":url">See status report for more details.</a>', [ + ':url' => $status_report_url->toString(), + ]), + $message_type, + ); + } + } + } diff --git a/tests/src/Functional/StatusCheckTest.php b/tests/src/Functional/StatusCheckTest.php index 4f7b778569bf6aa4c7459e3c547bd58dfc9604f1..24e4a6471a8963b62fc7cab2167912200b08db5b 100644 --- a/tests/src/Functional/StatusCheckTest.php +++ b/tests/src/Functional/StatusCheckTest.php @@ -12,6 +12,7 @@ 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\package_manager_test_validation\EventSubscriber\TestSubscriber; use Drupal\system\SystemManager; use Drupal\Tests\automatic_updates\Traits\ValidationTestTrait; use Drupal\Tests\Traits\Core\CronRunTrait; @@ -53,6 +54,13 @@ class StatusCheckTest extends AutomaticUpdatesFunctionalTestBase { */ protected $testChecker; + /** + * {@inheritdoc} + */ + protected static $modules = [ + 'package_manager_test_validation', + ]; + /** * {@inheritdoc} */ @@ -70,10 +78,49 @@ class StatusCheckTest extends AutomaticUpdatesFunctionalTestBase { 'administer software updates', 'access administration pages', 'access site in maintenance mode', + 'administer modules', ]); $this->drupalLogin($this->reportViewerUser); } + /** + * Tests status checks are displayed after Automatic Updates is installed. + * + * @dataProvider providerTestModuleFormInstallDisplay + */ + public function testModuleFormInstallDisplay(int $results_severity): void { + // Uninstall Automatic Updates as it is installed in TestBase setup(). + $this->container->get('module_installer')->uninstall(['automatic_updates']); + $expected_result = $this->createValidationResult($results_severity); + TestSubscriber::setTestResult([$expected_result], StatusCheckEvent::class); + + $this->drupalLogin($this->checkerRunnerUser); + $this->drupalGet('admin/modules'); + $page = $this->getSession()->getPage(); + $page->checkField('modules[automatic_updates][enable]'); + $page->pressButton('Install'); + + // Cron Updates will always be disabled on installation as per + // automatic_updates.settings.yml . + $session = $this->assertSession(); + $session->pageTextNotContains($expected_result->getMessages()[0]); + $session->linkExists('See status report for more details.'); + } + + /** + * Provides data for testModuleFormInstallDisplay. + */ + public function providerTestModuleFormInstallDisplay(): array { + return [ + 'Error' => [ + SystemManager::REQUIREMENT_ERROR, + ], + 'Warning' => [ + SystemManager::REQUIREMENT_WARNING, + ], + ]; + } + /** * Tests status checks on status report page. */