diff --git a/core/modules/user/tests/src/Functional/UserRequirementsTest.php b/core/modules/user/tests/src/Functional/UserRequirementsTest.php new file mode 100644 index 0000000000000000000000000000000000000000..5e5abbe3af8011b50d83e23023365e8dd7df4bff --- /dev/null +++ b/core/modules/user/tests/src/Functional/UserRequirementsTest.php @@ -0,0 +1,37 @@ +<?php + +declare(strict_types=1); + +namespace Drupal\Tests\user\Functional; + +use Drupal\Tests\BrowserTestBase; + +/** + * Tests the requirements checks of the User module. + * + * @group user + */ +class UserRequirementsTest extends BrowserTestBase { + + /** + * {@inheritdoc} + */ + protected $defaultTheme = 'stark'; + + /** + * Tests that the requirements check can detect a missing anonymous user. + */ + public function testAnonymousUser(): void { + // Remove the anonymous user. + \Drupal::database() + ->delete('users') + ->condition('uid', 0) + ->execute(); + + $this->drupalLogin($this->rootUser); + $this->drupalGet('/admin/reports/status'); + $this->assertSession()->statusCodeEquals(200); + $this->assertSession()->pageTextContains("The anonymous user does not exist."); + } + +} diff --git a/core/modules/user/user.install b/core/modules/user/user.install index ae27e3bbf5ac4048d66e449db258d0ca714f333d..73fb2947aa3e0095c2300d81d4152ec0b06a6850 100644 --- a/core/modules/user/user.install +++ b/core/modules/user/user.install @@ -93,6 +93,34 @@ function user_install() { ->save(); } +/** + * Implements hook_requirements(). + */ +function user_requirements($phase): array { + if ($phase !== 'runtime') { + return []; + } + + $result = (bool) \Drupal::entityQuery('user') + ->accessCheck(FALSE) + ->condition('uid', 0) + ->range(0, 1) + ->execute(); + + if ($result === FALSE) { + return [ + 'anonymous user' => [ + 'title' => t('Anonymous user'), + 'description' => t('The anonymous user does not exist. See the <a href=":url">restore the anonymous (user ID 0) user record</a> for more information', [ + ':url' => 'https://www.drupal.org/node/1029506', + ]), + 'severity' => REQUIREMENT_WARNING, + ], + ]; + } + return []; +} + /** * Implements hook_update_last_removed(). */