From 11edb986670a0ff577c3ac4ae791b98b4bff85a5 Mon Sep 17 00:00:00 2001
From: catch <catch@35733.no-reply.drupal.org>
Date: Fri, 19 Apr 2024 10:36:54 +0100
Subject: [PATCH] Issue #3439905 by SolimanHarkas, thebumik, smustgrave,
 pradhumanjain2311, vensires, alexpott: Fix Shortcut tests that rely on UID1's
 super user behavior

(cherry picked from commit 46b8545da8f516f5e5466bf685793f9484004a5d)
---
 .../src/Functional/ShortcutCacheTagsTest.php  | 20 +++++++++----
 .../src/Functional/ShortcutLinksTest.php      | 29 ++++++++++++++-----
 2 files changed, 35 insertions(+), 14 deletions(-)

diff --git a/core/modules/shortcut/tests/src/Functional/ShortcutCacheTagsTest.php b/core/modules/shortcut/tests/src/Functional/ShortcutCacheTagsTest.php
index e2e957b0ef1d..6cad565e8fcc 100644
--- a/core/modules/shortcut/tests/src/Functional/ShortcutCacheTagsTest.php
+++ b/core/modules/shortcut/tests/src/Functional/ShortcutCacheTagsTest.php
@@ -12,6 +12,7 @@
 use Drupal\Tests\system\Functional\Entity\EntityCacheTagsTestBase;
 use Drupal\user\Entity\Role;
 use Drupal\user\RoleInterface;
+use Drupal\user\UserInterface;
 
 /**
  * Tests the Shortcut entity's cache tags.
@@ -32,12 +33,11 @@ class ShortcutCacheTagsTest extends EntityCacheTagsTestBase {
   ];
 
   /**
-   * {@inheritdoc}
+   * User with permission to administer shortcuts.
    *
-   * @todo Remove and fix test to not rely on super user.
-   * @see https://www.drupal.org/project/drupal/issues/3437620
+   * @var \Drupal\user\UserInterface
    */
-  protected bool $usesSuperUserAccessPolicy = TRUE;
+  protected UserInterface $adminUser;
 
   /**
    * {@inheritdoc}
@@ -50,6 +50,14 @@ class ShortcutCacheTagsTest extends EntityCacheTagsTestBase {
   protected function setUp(): void {
     parent::setUp();
 
+    $this->adminUser = $this->drupalCreateUser([
+      'access toolbar',
+      'access shortcuts',
+      'administer site configuration',
+      'administer shortcuts',
+      'administer themes',
+    ]);
+
     // Give anonymous users permission to customize shortcut links, so that we
     // can verify the cache tags of cached versions of shortcuts.
     $user_role = Role::load(RoleInterface::ANONYMOUS_ID);
@@ -109,7 +117,7 @@ public function testToolbar() {
 
     // Ensure that without enabling the shortcuts-in-page-title-link feature
     // in the theme, the shortcut_list cache tag is not added to the page.
-    $this->drupalLogin($this->rootUser);
+    $this->drupalLogin($this->adminUser);
     $this->drupalGet('admin/config/system/cron');
     $expected_cache_tags = [
       'block_view',
@@ -286,7 +294,7 @@ public function testBlock(): void {
 
     // Ensure that without enabling the shortcuts-in-page-title-link feature
     // in the theme, the shortcut_list cache tag is not added to the page.
-    $this->drupalLogin($this->rootUser);
+    $this->drupalLogin($this->adminUser);
     $this->drupalGet('admin/config/system/cron');
     $expected_cache_tags = [
       'CACHE_MISS_IF_UNCACHEABLE_HTTP_METHOD:form',
diff --git a/core/modules/shortcut/tests/src/Functional/ShortcutLinksTest.php b/core/modules/shortcut/tests/src/Functional/ShortcutLinksTest.php
index 4880f1907326..b6afa13b53f3 100644
--- a/core/modules/shortcut/tests/src/Functional/ShortcutLinksTest.php
+++ b/core/modules/shortcut/tests/src/Functional/ShortcutLinksTest.php
@@ -33,11 +33,8 @@ class ShortcutLinksTest extends ShortcutTestBase {
 
   /**
    * {@inheritdoc}
-   *
-   * @todo Remove and fix test to not rely on super user.
-   * @see https://www.drupal.org/project/drupal/issues/3437620
    */
-  protected bool $usesSuperUserAccessPolicy = TRUE;
+  protected $adminUser;
 
   /**
    * {@inheritdoc}
@@ -50,6 +47,22 @@ class ShortcutLinksTest extends ShortcutTestBase {
   protected function setUp(): void {
     parent::setUp();
 
+    $this->adminUser = $this->drupalCreateUser([
+      'access toolbar',
+      'administer shortcuts',
+      'view the administration theme',
+      'access content overview',
+      'administer users',
+      'administer site configuration',
+      'administer content types',
+      'create article content',
+      'create page content',
+      'edit any article content',
+      'edit any page content',
+      'administer blocks',
+      'access shortcuts',
+    ]);
+
     $this->drupalPlaceBlock('page_title_block');
   }
 
@@ -164,7 +177,7 @@ public function testShortcutQuickLink() {
     $this->config('node.settings')->set('use_admin_theme', '1')->save();
     $this->container->get('router.builder')->rebuild();
 
-    $this->drupalLogin($this->rootUser);
+    $this->drupalLogin($this->adminUser);
     $this->drupalGet('admin/config/system/cron');
 
     // Test the "Add to shortcuts" link.
@@ -221,8 +234,8 @@ public function testShortcutQuickLink() {
     $this->clickLink('Remove from Default shortcuts');
     $this->assertSession()->pageTextContains("The shortcut $title has been deleted.");
     $this->assertShortcutQuickLink('Add to Default shortcuts');
-
     \Drupal::service('module_installer')->install(['block_content']);
+    $this->adminUser->addRole($this->drupalCreateRole(['administer block types']))->save();
     BlockContentType::create([
       'id' => 'basic',
       'label' => 'Basic block',
@@ -284,7 +297,7 @@ public function testShortcutLinkChangePath() {
    * Tests that changing the route of a shortcut link works.
    */
   public function testShortcutLinkChangeRoute() {
-    $this->drupalLogin($this->rootUser);
+    $this->drupalLogin($this->adminUser);
     $this->drupalGet('admin/content');
     $this->assertSession()->statusCodeEquals(200);
     // Disable the view.
@@ -360,7 +373,7 @@ public function testAccessShortcutsPermission() {
       ->save();
 
     // Add cron to the default shortcut set.
-    $this->drupalLogin($this->rootUser);
+    $this->drupalLogin($this->adminUser);
     $this->drupalGet('admin/config/system/cron');
     $this->clickLink('Add to Default shortcuts');
 
-- 
GitLab