From 6be8f3deaf5bbd42d873bc6a0b50bb90cad852e9 Mon Sep 17 00:00:00 2001
From: catch <6915-catch@users.noreply.drupalcode.org>
Date: Fri, 7 Feb 2025 16:29:19 +0000
Subject: [PATCH] Issue #3501727 by sayco, oily, berdir, catch, borisson_,
 longwave: Try to simplify  checks in AdminNegotiator

---
 .../user/src/Theme/AdminNegotiator.php        | 34 +++++++++++--------
 .../src/Unit/Theme/AdminNegotiatorTest.php    |  4 +--
 core/modules/user/user.services.yml           |  2 +-
 .../StandardPerformanceTest.php               |  2 +-
 4 files changed, 23 insertions(+), 19 deletions(-)

diff --git a/core/modules/user/src/Theme/AdminNegotiator.php b/core/modules/user/src/Theme/AdminNegotiator.php
index 398acbd20138..d8c495d2350e 100644
--- a/core/modules/user/src/Theme/AdminNegotiator.php
+++ b/core/modules/user/src/Theme/AdminNegotiator.php
@@ -3,6 +3,7 @@
 namespace Drupal\user\Theme;
 
 use Drupal\Core\Config\ConfigFactoryInterface;
+use Drupal\Core\DependencyInjection\DeprecatedServicePropertyTrait;
 use Drupal\Core\Entity\EntityTypeManagerInterface;
 use Drupal\Core\Routing\AdminContext;
 use Drupal\Core\Routing\RouteMatchInterface;
@@ -13,6 +14,12 @@
  * Sets the active theme on admin pages.
  */
 class AdminNegotiator implements ThemeNegotiatorInterface {
+  use DeprecatedServicePropertyTrait;
+
+  /**
+   * The service properties that should raise a deprecation error.
+   */
+  private array $deprecatedProperties = ['entityTypeManager' => 'entity_type.manager'];
 
   /**
    * The current user.
@@ -28,13 +35,6 @@ class AdminNegotiator implements ThemeNegotiatorInterface {
    */
   protected $configFactory;
 
-  /**
-   * The entity type manager.
-   *
-   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
-   */
-  protected $entityTypeManager;
-
   /**
    * The route admin context to determine whether a route is an admin one.
    *
@@ -49,23 +49,29 @@ class AdminNegotiator implements ThemeNegotiatorInterface {
    *   The current user.
    * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
    *   The config factory.
-   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
-   *   The entity type manager.
-   * @param \Drupal\Core\Routing\AdminContext $admin_context
+   * @param \Drupal\Core\Routing\AdminContext|EntityTypeManagerInterface $admin_context
    *   The route admin context to determine whether the route is an admin one.
    */
-  public function __construct(AccountInterface $user, ConfigFactoryInterface $config_factory, EntityTypeManagerInterface $entity_type_manager, AdminContext $admin_context) {
+  public function __construct(AccountInterface $user, ConfigFactoryInterface $config_factory, AdminContext|EntityTypeManagerInterface $admin_context) {
     $this->user = $user;
     $this->configFactory = $config_factory;
-    $this->entityTypeManager = $entity_type_manager;
-    $this->adminContext = $admin_context;
+
+    if ($admin_context instanceof EntityTypeManagerInterface) {
+      $deprecated_service_name = EntityTypeManagerInterface::class;
+      @trigger_error("Passing the $deprecated_service_name (entity_type.manager service) to AdminNegotiator is deprecated in drupal:11.2.0 and will be removed in drupal:12.0.0. There is no replacement for this service, as it is not used. See https://www.drupal.org/project/drupal/issues/3501727", E_USER_DEPRECATED);
+      $this->adminContext = \Drupal::service('router.admin_context');
+    }
+    else {
+      $this->adminContext = $admin_context;
+    }
   }
 
   /**
    * {@inheritdoc}
    */
   public function applies(RouteMatchInterface $route_match) {
-    return ($this->entityTypeManager->hasHandler('user_role', 'storage') && $this->user->hasPermission('view the administration theme') && $this->adminContext->isAdminRoute($route_match->getRouteObject()));
+    $is_admin_route = $this->adminContext->isAdminRoute($route_match->getRouteObject());
+    return $is_admin_route && $this->user->hasPermission('view the administration theme');
   }
 
   /**
diff --git a/core/modules/user/tests/src/Unit/Theme/AdminNegotiatorTest.php b/core/modules/user/tests/src/Unit/Theme/AdminNegotiatorTest.php
index cb95afcd05c7..7a34a967e478 100644
--- a/core/modules/user/tests/src/Unit/Theme/AdminNegotiatorTest.php
+++ b/core/modules/user/tests/src/Unit/Theme/AdminNegotiatorTest.php
@@ -4,7 +4,6 @@
 
 namespace Drupal\Tests\user\Unit\Theme;
 
-use Drupal\Core\Entity\EntityTypeManagerInterface;
 use Drupal\Core\Routing\AdminContext;
 use Drupal\Core\Routing\RouteMatch;
 use Drupal\Core\Session\AccountInterface;
@@ -25,9 +24,8 @@ class AdminNegotiatorTest extends UnitTestCase {
   public function testDetermineActiveTheme($admin_theme, $expected): void {
     $user = $this->prophesize(AccountInterface::class);
     $config_factory = $this->getConfigFactoryStub(['system.theme' => ['admin' => $admin_theme]]);
-    $entity_type_manager = $this->prophesize(EntityTypeManagerInterface::class);
     $admin_context = $this->prophesize(AdminContext::class);
-    $negotiator = new AdminNegotiator($user->reveal(), $config_factory, $entity_type_manager->reveal(), $admin_context->reveal());
+    $negotiator = new AdminNegotiator($user->reveal(), $config_factory, $admin_context->reveal());
     $route_match = $this->prophesize(RouteMatch::class);
     $this->assertSame($expected, $negotiator->determineActiveTheme($route_match->reveal()));
   }
diff --git a/core/modules/user/user.services.yml b/core/modules/user/user.services.yml
index ec34738d992e..d7ad70852f6e 100644
--- a/core/modules/user/user.services.yml
+++ b/core/modules/user/user.services.yml
@@ -39,7 +39,7 @@ services:
     arguments: ['@current_user', '@entity_type.manager', '@datetime.time']
   theme.negotiator.admin_theme:
     class: Drupal\user\Theme\AdminNegotiator
-    arguments: ['@current_user', '@config.factory', '@entity_type.manager', '@router.admin_context']
+    arguments: ['@current_user', '@config.factory', '@router.admin_context']
     tags:
       - { name: theme_negotiator, priority: -40 }
   user.auth:
diff --git a/core/profiles/standard/tests/src/FunctionalJavascript/StandardPerformanceTest.php b/core/profiles/standard/tests/src/FunctionalJavascript/StandardPerformanceTest.php
index a43ba4a03db8..3c045918b6af 100644
--- a/core/profiles/standard/tests/src/FunctionalJavascript/StandardPerformanceTest.php
+++ b/core/profiles/standard/tests/src/FunctionalJavascript/StandardPerformanceTest.php
@@ -335,7 +335,7 @@ protected function testLogin(): void {
     $this->assertSame($expected_queries, $recorded_queries);
     $expected = [
       'QueryCount' => 17,
-      'CacheGetCount' => 85,
+      'CacheGetCount' => 84,
       'CacheSetCount' => 1,
       'CacheDeleteCount' => 1,
       'CacheTagChecksumCount' => 1,
-- 
GitLab