From b6a4ce3eb70329016d1c56f0eefed41ab84369dd Mon Sep 17 00:00:00 2001 From: catch <catch@35733.no-reply.drupal.org> Date: Tue, 12 Mar 2024 10:51:45 +0000 Subject: [PATCH] Issue #3057399 by quietone, Akhil Babu, kristiaanvandeneynde, alexpott, pradhumanjain2311, smustgrave, catch, poker10, longwave, jimafisk, Newb_Druper, robpowell, dpi, tomdewild: Add user_requirements to check for missing anonymous user (cherry picked from commit 9a6e38c000c21f23b6d3be0844f07dddb5b36fe7) --- .../src/Functional/UserRequirementsTest.php | 37 +++++++++++++++++++ core/modules/user/user.install | 28 ++++++++++++++ 2 files changed, 65 insertions(+) create mode 100644 core/modules/user/tests/src/Functional/UserRequirementsTest.php 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 000000000000..5e5abbe3af80 --- /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 ae27e3bbf5ac..73fb2947aa3e 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(). */ -- GitLab