From 743c2e68f70d1337d5204b2a0041f12ea5577a97 Mon Sep 17 00:00:00 2001
From: Alex Pott <alex.a.pott@googlemail.com>
Date: Thu, 14 Nov 2024 13:01:59 +0000
Subject: [PATCH] Issue #3439832 by pooja_sharma, solimanharkas, smustgrave,
 quietone, vensires: Fix Content Moderation tests that rely on UID1's super
 user behavior

(cherry picked from commit 56186f9070bad3416da675871c5211f44aafa4bd)
---
 .../ModerationContentTranslationTest.php      | 18 ++++++++---------
 .../src/Functional/ModerationFormTest.php     | 20 ++++++++++++-------
 .../src/Functional/ModerationLocaleTest.php   | 17 +++++++++-------
 .../Functional/ModerationStateBlockTest.php   | 15 ++++++++------
 ...kspaceContentModerationIntegrationTest.php | 15 ++++++++------
 .../EntityStateChangeValidationTest.php       | 16 ++++-----------
 6 files changed, 53 insertions(+), 48 deletions(-)

diff --git a/core/modules/content_moderation/tests/src/Functional/ModerationContentTranslationTest.php b/core/modules/content_moderation/tests/src/Functional/ModerationContentTranslationTest.php
index 039556243921..ea6d277506f7 100644
--- a/core/modules/content_moderation/tests/src/Functional/ModerationContentTranslationTest.php
+++ b/core/modules/content_moderation/tests/src/Functional/ModerationContentTranslationTest.php
@@ -7,6 +7,7 @@
 use Drupal\Tests\BrowserTestBase;
 use Drupal\Tests\content_moderation\Traits\ContentModerationTestTrait;
 use Drupal\Tests\content_translation\Traits\ContentTranslationTestTrait;
+use Drupal\user\Entity\Role;
 
 /**
  * Test content_moderation functionality with content_translation.
@@ -34,14 +35,6 @@ class ModerationContentTranslationTest extends BrowserTestBase {
     'content_translation',
   ];
 
-  /**
-   * {@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;
-
   /**
    * {@inheritdoc}
    */
@@ -52,7 +45,12 @@ class ModerationContentTranslationTest extends BrowserTestBase {
    */
   protected function setUp(): void {
     parent::setUp();
-    $this->drupalLogin($this->rootUser);
+    $this->adminUser = $this->drupalCreateUser([
+      'bypass node access',
+      'create content translations',
+      'translate any entity',
+    ]);
+    $this->drupalLogin($this->adminUser);
     // Create an Article content type.
     $this->drupalCreateContentType(['type' => 'article', 'name' => 'Article'])->save();
     static::createLanguageFromLangcode('fr');
@@ -91,7 +89,7 @@ public function testModerationWithExistingContent(): void {
     $workflow = $this->createEditorialWorkflow();
     $workflow->getTypePlugin()->addEntityTypeAndBundle('node', 'article');
     $workflow->save();
-    $this->drupalLogin($this->rootUser);
+    $this->grantPermissions(Role::load(Role::AUTHENTICATED_ID), ['use editorial transition publish']);
 
     // Edit the English node.
     $this->drupalGet('node/' . $english_node->id() . '/edit');
diff --git a/core/modules/content_moderation/tests/src/Functional/ModerationFormTest.php b/core/modules/content_moderation/tests/src/Functional/ModerationFormTest.php
index f9a0a753ee0b..8688e5ad034c 100644
--- a/core/modules/content_moderation/tests/src/Functional/ModerationFormTest.php
+++ b/core/modules/content_moderation/tests/src/Functional/ModerationFormTest.php
@@ -29,16 +29,20 @@ class ModerationFormTest extends ModerationStateTestBase {
 
   /**
    * {@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 $defaultTheme = 'stark';
 
   /**
    * {@inheritdoc}
    */
-  protected $defaultTheme = 'stark';
+  protected function getAdministratorPermissions(): array {
+    return array_merge($this->permissions, [
+      'administer entity_test content',
+      'view test entity',
+      'translate any entity',
+      'bypass node access',
+    ]);
+  }
 
   /**
    * {@inheritdoc}
@@ -196,7 +200,8 @@ public function testModerationForm(): void {
    * Tests moderation non-bundle entity type.
    */
   public function testNonBundleModerationForm(): void {
-    $this->drupalLogin($this->rootUser);
+    $this->adminUser = $this->drupalCreateUser($this->getAdministratorPermissions());
+    $this->drupalLogin($this->adminUser);
     $this->workflow->getTypePlugin()->addEntityTypeAndBundle('entity_test_mulrevpub', 'entity_test_mulrevpub');
     $this->workflow->save();
 
@@ -290,7 +295,8 @@ public function testModerationFormSetsRevisionAuthor(): void {
    * Tests translated and moderated nodes.
    */
   public function testContentTranslationNodeForm(): void {
-    $this->drupalLogin($this->rootUser);
+    $this->adminUser = $this->drupalCreateUser($this->getAdministratorPermissions());
+    $this->drupalLogin($this->adminUser);
 
     // Add French language.
     static::createLanguageFromLangcode('fr');
diff --git a/core/modules/content_moderation/tests/src/Functional/ModerationLocaleTest.php b/core/modules/content_moderation/tests/src/Functional/ModerationLocaleTest.php
index 1710c550f76a..a3b8abe12361 100644
--- a/core/modules/content_moderation/tests/src/Functional/ModerationLocaleTest.php
+++ b/core/modules/content_moderation/tests/src/Functional/ModerationLocaleTest.php
@@ -29,16 +29,19 @@ class ModerationLocaleTest extends ModerationStateTestBase {
 
   /**
    * {@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 $defaultTheme = 'stark';
 
   /**
    * {@inheritdoc}
    */
-  protected $defaultTheme = 'stark';
+  protected function getAdministratorPermissions(): array {
+    return array_merge($this->permissions, [
+      'create content translations',
+      'bypass node access',
+      'translate any entity',
+    ]);
+  }
 
   /**
    * {@inheritdoc}
@@ -46,8 +49,8 @@ class ModerationLocaleTest extends ModerationStateTestBase {
   protected function setUp(): void {
     parent::setUp();
 
-    $this->drupalLogin($this->rootUser);
-
+    $this->adminUser = $this->drupalCreateUser($this->getAdministratorPermissions());
+    $this->drupalLogin($this->adminUser);
     // Enable moderation on Article node type.
     $this->createContentTypeFromUi('Article', 'article', TRUE);
 
diff --git a/core/modules/content_moderation/tests/src/Functional/ModerationStateBlockTest.php b/core/modules/content_moderation/tests/src/Functional/ModerationStateBlockTest.php
index 5880df23bd67..4ab3497c058f 100644
--- a/core/modules/content_moderation/tests/src/Functional/ModerationStateBlockTest.php
+++ b/core/modules/content_moderation/tests/src/Functional/ModerationStateBlockTest.php
@@ -16,16 +16,18 @@ class ModerationStateBlockTest extends ModerationStateTestBase {
 
   /**
    * {@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 $defaultTheme = 'stark';
 
   /**
    * {@inheritdoc}
    */
-  protected $defaultTheme = 'stark';
+  protected function getAdministratorPermissions(): array {
+    return array_merge($this->permissions, [
+      'administer blocks',
+      'administer block content',
+    ]);
+  }
 
   /**
    * {@inheritdoc}
@@ -64,7 +66,8 @@ protected function setUp(): void {
    * @see \Drupal\content_moderation\Tests\ModerationFormTest::testModerationForm
    */
   public function testCustomBlockModeration(): void {
-    $this->drupalLogin($this->rootUser);
+    $this->adminUser = $this->drupalCreateUser($this->getAdministratorPermissions());
+    $this->drupalLogin($this->adminUser);
 
     // Enable moderation for content blocks.
     $edit['bundles[basic]'] = TRUE;
diff --git a/core/modules/content_moderation/tests/src/Functional/WorkspaceContentModerationIntegrationTest.php b/core/modules/content_moderation/tests/src/Functional/WorkspaceContentModerationIntegrationTest.php
index 5829390ecc6b..5446bf267544 100644
--- a/core/modules/content_moderation/tests/src/Functional/WorkspaceContentModerationIntegrationTest.php
+++ b/core/modules/content_moderation/tests/src/Functional/WorkspaceContentModerationIntegrationTest.php
@@ -24,16 +24,18 @@ class WorkspaceContentModerationIntegrationTest extends ModerationStateTestBase
 
   /**
    * {@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 $defaultTheme = 'stark';
 
   /**
    * {@inheritdoc}
    */
-  protected $defaultTheme = 'stark';
+  protected function getAdministratorPermissions(): array {
+    return array_merge($this->permissions, [
+      'bypass node access',
+      'view any workspace',
+    ]);
+  }
 
   /**
    * {@inheritdoc}
@@ -41,7 +43,8 @@ class WorkspaceContentModerationIntegrationTest extends ModerationStateTestBase
   protected function setUp(): void {
     parent::setUp();
 
-    $this->drupalLogin($this->rootUser);
+    $this->adminUser = $this->drupalCreateUser($this->getAdministratorPermissions());
+    $this->drupalLogin($this->adminUser);
 
     // Enable moderation on Article node type.
     $this->createContentTypeFromUi('Article', 'article', TRUE);
diff --git a/core/modules/content_moderation/tests/src/Kernel/EntityStateChangeValidationTest.php b/core/modules/content_moderation/tests/src/Kernel/EntityStateChangeValidationTest.php
index e769818a9cb2..3b74d1afe382 100644
--- a/core/modules/content_moderation/tests/src/Kernel/EntityStateChangeValidationTest.php
+++ b/core/modules/content_moderation/tests/src/Kernel/EntityStateChangeValidationTest.php
@@ -33,14 +33,6 @@ class EntityStateChangeValidationTest extends KernelTestBase {
     'workflows',
   ];
 
-  /**
-   * {@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;
-
   /**
    * An admin user.
    *
@@ -69,7 +61,6 @@ protected function setUp(): void {
    * @covers ::validate
    */
   public function testValidTransition(): void {
-    $this->setCurrentUser($this->adminUser);
 
     $node_type = NodeType::create([
       'type' => 'example',
@@ -87,6 +78,7 @@ public function testValidTransition(): void {
     $node->moderation_state->value = 'draft';
     $node->save();
 
+    $this->setCurrentUser($this->createUser(['use editorial transition publish']));
     $node->moderation_state->value = 'published';
     $this->assertCount(0, $node->validate());
     $node->save();
@@ -153,7 +145,6 @@ public function testInvalidState(): void {
    * Tests validation with no initial state or an invalid state.
    */
   public function testInvalidStateWithoutExisting(): void {
-    $this->setCurrentUser($this->adminUser);
     // Create content without moderation enabled for the content type.
     $node_type = NodeType::create([
       'type' => 'example',
@@ -173,6 +164,7 @@ public function testInvalidStateWithoutExisting(): void {
     $workflow->getTypePlugin()->addEntityTypeAndBundle('node', 'example');
     $workflow->save();
 
+    $this->setCurrentUser($this->createUser(['use editorial transition create_new_draft']));
     // Validate the invalid state.
     $node = Node::load($node->id());
     $node->moderation_state->value = 'invalid_state';
@@ -203,7 +195,6 @@ public function testInvalidStateWithoutExisting(): void {
    * Tests state transition validation with multiple languages.
    */
   public function testInvalidStateMultilingual(): void {
-    $this->setCurrentUser($this->adminUser);
 
     ConfigurableLanguage::createFromLangcode('fr')->save();
     $node_type = NodeType::create([
@@ -216,6 +207,7 @@ public function testInvalidStateMultilingual(): void {
     $workflow->getTypePlugin()->addEntityTypeAndBundle('node', 'example');
     $workflow->save();
 
+    $this->setCurrentUser($this->createUser(['use editorial transition archive']));
     $node = Node::create([
       'type' => 'example',
       'title' => 'English Published Node',
@@ -261,7 +253,6 @@ public function testInvalidStateMultilingual(): void {
    * Tests that content without prior moderation information can be moderated.
    */
   public function testExistingContentWithNoModeration(): void {
-    $this->setCurrentUser($this->adminUser);
 
     $node_type = NodeType::create([
       'type' => 'example',
@@ -282,6 +273,7 @@ public function testExistingContentWithNoModeration(): void {
     $workflow->getTypePlugin()->addEntityTypeAndBundle('node', 'example');
     $workflow->save();
 
+    $this->setCurrentUser($this->createUser(['use editorial transition publish']));
     $node = Node::load($nid);
 
     // Having no previous state should not break validation.
-- 
GitLab