From c6babbb64e8b2f9d6adb15c3499ec4597ee81dd6 Mon Sep 17 00:00:00 2001
From: catch <catch@35733.no-reply.drupal.org>
Date: Fri, 6 Jan 2023 11:14:45 +0000
Subject: [PATCH] Issue #3324384 by Spokje, joachim, elber, znerol, longwave,
 mondrake: resolve differing createUser() test methods

---
 .../CommentDefaultFormatterCacheTagsTest.php  |  4 +-
 .../src/Kernel/CommentFieldAccessTest.php     | 15 ++++----
 .../tests/src/Kernel/MessageEntityTest.php    |  6 +--
 .../filter/tests/src/Kernel/FilterAPITest.php |  9 +++--
 .../LayoutBuilderCompatibilityTestBase.php    |  2 +-
 .../tests/src/Kernel/NodeFieldAccessTest.php  |  8 ++--
 .../src/Kernel/UserEntityReferenceTest.php    |  6 +--
 core/phpstan-baseline.neon                    |  5 ---
 .../EntityAutocompleteElementFormTest.php     |  2 +-
 .../Entity/EntityAccessControlHandlerTest.php | 12 +++---
 .../Core/Entity/EntityDeprecationTest.php     | 23 ++++++++++++
 .../Core/Entity/EntityKernelTestBase.php      | 37 ++++++++++++++++---
 .../EntityReferenceSelectionSortTest.php      |  2 +-
 13 files changed, 90 insertions(+), 41 deletions(-)
 create mode 100644 core/tests/Drupal/KernelTests/Core/Entity/EntityDeprecationTest.php

diff --git a/core/modules/comment/tests/src/Kernel/CommentDefaultFormatterCacheTagsTest.php b/core/modules/comment/tests/src/Kernel/CommentDefaultFormatterCacheTagsTest.php
index 1997a843bbf3..4df8d4cd51fc 100644
--- a/core/modules/comment/tests/src/Kernel/CommentDefaultFormatterCacheTagsTest.php
+++ b/core/modules/comment/tests/src/Kernel/CommentDefaultFormatterCacheTagsTest.php
@@ -37,7 +37,7 @@ protected function setUp(): void {
     // Create user 1 so that the user created later in the test has a different
     // user ID.
     // @todo Remove in https://www.drupal.org/node/540008.
-    $this->createUser(['uid' => 1, 'name' => 'user1'])->save();
+    $this->createUser([], NULL, FALSE, ['uid' => 1, 'name' => 'user1'])->save();
 
     $this->container->get('module_handler')->loadInclude('comment', 'install');
     comment_install();
@@ -56,7 +56,7 @@ protected function setUp(): void {
     // user does not have access to the 'administer comments' permission, to
     // ensure only published comments are visible to the end user.
     $current_user = $this->container->get('current_user');
-    $current_user->setAccount($this->createUser([], ['access comments', 'post comments']));
+    $current_user->setAccount($this->createUser(['access comments', 'post comments']));
 
     // Install tables and config needed to render comments.
     $this->installSchema('comment', ['comment_entity_statistics']);
diff --git a/core/modules/comment/tests/src/Kernel/CommentFieldAccessTest.php b/core/modules/comment/tests/src/Kernel/CommentFieldAccessTest.php
index 414eb9a79cee..0fb3401efdb3 100644
--- a/core/modules/comment/tests/src/Kernel/CommentFieldAccessTest.php
+++ b/core/modules/comment/tests/src/Kernel/CommentFieldAccessTest.php
@@ -109,26 +109,27 @@ public function testAccessToAdministrativeFields() {
 
     // An administrator user. No user exists yet, ensure that the first user
     // does not have UID 1.
-    $comment_admin_user = $this->createUser(['uid' => 2, 'name' => 'admin'], [
+    $comment_admin_user = $this->createUser([
       'administer comments',
       'access comments',
-    ]);
+    ], 'admin', FALSE, ['uid' => 2]);
 
     // Two comment enabled users, one with edit access.
-    $comment_enabled_user = $this->createUser(['name' => 'enabled'], [
+    $comment_enabled_user = $this->createUser([
       'post comments',
       'skip comment approval',
       'edit own comments',
       'access comments',
-    ]);
-    $comment_no_edit_user = $this->createUser(['name' => 'no edit'], [
+    ], 'enabled');
+
+    $comment_no_edit_user = $this->createUser([
       'post comments',
       'skip comment approval',
       'access comments',
-    ]);
+    ], 'no edit');
 
     // An unprivileged user.
-    $comment_disabled_user = $this->createUser(['name' => 'disabled'], ['access content']);
+    $comment_disabled_user = $this->createUser(['access content'], 'disabled');
 
     $role = Role::load(RoleInterface::ANONYMOUS_ID);
     $role->grantPermission('post comments')
diff --git a/core/modules/contact/tests/src/Kernel/MessageEntityTest.php b/core/modules/contact/tests/src/Kernel/MessageEntityTest.php
index 0c094cc61297..47944211eb4d 100644
--- a/core/modules/contact/tests/src/Kernel/MessageEntityTest.php
+++ b/core/modules/contact/tests/src/Kernel/MessageEntityTest.php
@@ -61,9 +61,9 @@ public function testMessageMethods() {
     $this->assertEquals('sender_mail', $message->getSenderMail());
     $this->assertTrue($message->copySender());
 
-    $no_access_user = $this->createUser(['uid' => 2]);
-    $access_user = $this->createUser(['uid' => 3], ['access site-wide contact form']);
-    $admin = $this->createUser(['uid' => 4], ['administer contact forms']);
+    $no_access_user = $this->createUser([], NULL, FALSE, ['uid' => 2]);
+    $access_user = $this->createUser(['access site-wide contact form'], NULL, FALSE, ['uid' => 3]);
+    $admin = $this->createUser(['administer contact forms'], NULL, FALSE, ['uid' => 4]);
 
     $this->assertFalse(\Drupal::entityTypeManager()->getAccessControlHandler('contact_message')->createAccess(NULL, $no_access_user));
     $this->assertTrue(\Drupal::entityTypeManager()->getAccessControlHandler('contact_message')->createAccess(NULL, $access_user));
diff --git a/core/modules/filter/tests/src/Kernel/FilterAPITest.php b/core/modules/filter/tests/src/Kernel/FilterAPITest.php
index cc0e8613a172..3181ba9be74e 100644
--- a/core/modules/filter/tests/src/Kernel/FilterAPITest.php
+++ b/core/modules/filter/tests/src/Kernel/FilterAPITest.php
@@ -329,9 +329,12 @@ public function testTypedDataAPI() {
 
     $this->assertInstanceOf(OptionsProviderInterface::class, $data);
 
-    $filtered_html_user = $this->createUser(['uid' => 2], [
-      FilterFormat::load('filtered_html')->getPermissionName(),
-    ]);
+    $filtered_html_user = $this->createUser(
+      [FilterFormat::load('filtered_html')->getPermissionName()],
+      NULL,
+      FALSE,
+      ['uid' => 2]
+    );
 
     // Test with anonymous user.
     $user = new AnonymousUserSession();
diff --git a/core/modules/layout_builder/tests/src/Kernel/LayoutBuilderCompatibilityTestBase.php b/core/modules/layout_builder/tests/src/Kernel/LayoutBuilderCompatibilityTestBase.php
index f4dab45b3277..4e6f76aafa37 100644
--- a/core/modules/layout_builder/tests/src/Kernel/LayoutBuilderCompatibilityTestBase.php
+++ b/core/modules/layout_builder/tests/src/Kernel/LayoutBuilderCompatibilityTestBase.php
@@ -44,7 +44,7 @@ protected function setUp(): void {
     $this->installConfig(['filter']);
 
     // Set up a non-admin user that is allowed to view test entities.
-    \Drupal::currentUser()->setAccount($this->createUser(['uid' => 2], ['view test entity']));
+    \Drupal::currentUser()->setAccount($this->createUser(['view test entity'], NULL, FALSE, ['uid' => 2]));
 
     \Drupal::service('theme_installer')->install(['starterkit_theme']);
     $this->config('system.theme')->set('default', 'starterkit_theme')->save();
diff --git a/core/modules/node/tests/src/Kernel/NodeFieldAccessTest.php b/core/modules/node/tests/src/Kernel/NodeFieldAccessTest.php
index b19c6bea2f88..d82b9a63b3cc 100644
--- a/core/modules/node/tests/src/Kernel/NodeFieldAccessTest.php
+++ b/core/modules/node/tests/src/Kernel/NodeFieldAccessTest.php
@@ -62,14 +62,14 @@ public function testAccessToAdministrativeFields() {
 
     // An administrator user. No user exists yet, ensure that the first user
     // does not have UID 1.
-    $content_admin_user = $this->createUser(['uid' => 2], ['administer nodes']);
+    $content_admin_user = $this->createUser(['administer nodes'], NULL, FALSE, ['uid' => 2]);
 
     // Two different editor users.
-    $page_creator_user = $this->createUser([], ['create page content', 'edit own page content', 'delete own page content']);
-    $page_manager_user = $this->createUser([], ['create page content', 'edit any page content', 'delete any page content']);
+    $page_creator_user = $this->createUser(['create page content', 'edit own page content', 'delete own page content']);
+    $page_manager_user = $this->createUser(['create page content', 'edit any page content', 'delete any page content']);
 
     // An unprivileged user.
-    $page_unrelated_user = $this->createUser([], ['access content']);
+    $page_unrelated_user = $this->createUser(['access content']);
 
     // List of all users
     $test_users = [
diff --git a/core/modules/user/tests/src/Kernel/UserEntityReferenceTest.php b/core/modules/user/tests/src/Kernel/UserEntityReferenceTest.php
index efbda8709153..696fc75193fc 100644
--- a/core/modules/user/tests/src/Kernel/UserEntityReferenceTest.php
+++ b/core/modules/user/tests/src/Kernel/UserEntityReferenceTest.php
@@ -66,15 +66,15 @@ public function testUserSelectionByRole() {
     $field_definition->save();
 
     // cspell:ignore aabb aabbb aabbbb aabbbb
-    $user1 = $this->createUser(['name' => 'aabb']);
+    $user1 = $this->createUser([], 'aabb');
     $user1->addRole($this->role1->id());
     $user1->save();
 
-    $user2 = $this->createUser(['name' => 'aabbb']);
+    $user2 = $this->createUser([], 'aabbb');
     $user2->addRole($this->role1->id());
     $user2->save();
 
-    $user3 = $this->createUser(['name' => 'aabbbb']);
+    $user3 = $this->createUser([], 'aabbbb');
     $user3->addRole($this->role2->id());
     $user3->save();
 
diff --git a/core/phpstan-baseline.neon b/core/phpstan-baseline.neon
index 4d6463ab2121..f26c4db4b389 100644
--- a/core/phpstan-baseline.neon
+++ b/core/phpstan-baseline.neon
@@ -3050,11 +3050,6 @@ parameters:
 			count: 2
 			path: tests/Drupal/KernelTests/Core/Entity/EntityDecoupledTranslationRevisionsTest.php
 
-		-
-			message: "#^Method Drupal\\\\KernelTests\\\\Core\\\\Entity\\\\EntityKernelTestBase\\:\\:createUser\\(\\) invoked with 4 parameters, 0\\-2 required\\.$#"
-			count: 3
-			path: tests/Drupal/KernelTests/Core/Entity/EntityKernelTestBase.php
-
 		-
 			message: "#^Variable \\$field might not be defined\\.$#"
 			count: 9
diff --git a/core/tests/Drupal/KernelTests/Core/Entity/Element/EntityAutocompleteElementFormTest.php b/core/tests/Drupal/KernelTests/Core/Entity/Element/EntityAutocompleteElementFormTest.php
index 70ff047b621b..f9ee22074dab 100644
--- a/core/tests/Drupal/KernelTests/Core/Entity/Element/EntityAutocompleteElementFormTest.php
+++ b/core/tests/Drupal/KernelTests/Core/Entity/Element/EntityAutocompleteElementFormTest.php
@@ -338,7 +338,7 @@ public function testEntityAutocompleteAccess() {
     $this->assertEquals($expected, $form['tags_access']['#value']);
 
     // Set up a non-admin user that is *not* allowed to view test entities.
-    \Drupal::currentUser()->setAccount($this->createUser([], []));
+    \Drupal::currentUser()->setAccount($this->createUser());
 
     // Rebuild the form.
     $form = $form_builder->getForm($this);
diff --git a/core/tests/Drupal/KernelTests/Core/Entity/EntityAccessControlHandlerTest.php b/core/tests/Drupal/KernelTests/Core/Entity/EntityAccessControlHandlerTest.php
index 3f303dc90fc8..ccbc1ef101bf 100644
--- a/core/tests/Drupal/KernelTests/Core/Entity/EntityAccessControlHandlerTest.php
+++ b/core/tests/Drupal/KernelTests/Core/Entity/EntityAccessControlHandlerTest.php
@@ -57,7 +57,7 @@ public function assertEntityAccess(array $ops, AccessibleInterface $object, Acco
    */
   public function testUserLabelAccess() {
     // Set up a non-admin user.
-    \Drupal::currentUser()->setAccount($this->createUser(['uid' => 2]));
+    \Drupal::currentUser()->setAccount($this->createUser([], NULL, FALSE, ['uid' => 2]));
 
     $anonymous_user = User::getAnonymousUser();
     $user = $this->createUser();
@@ -111,7 +111,7 @@ public function testUserLabelAccess() {
    */
   public function testEntityAccess() {
     // Set up a non-admin user that is allowed to view test entities.
-    \Drupal::currentUser()->setAccount($this->createUser(['uid' => 2], ['view test entity']));
+    \Drupal::currentUser()->setAccount($this->createUser(['view test entity'], NULL, FALSE, ['uid' => 2]));
 
     // Use the 'entity_test_label' entity type in order to test the 'view label'
     // access operation.
@@ -154,7 +154,7 @@ public function testEntityAccess() {
    */
   public function testDefaultEntityAccess() {
     // Set up a non-admin user that is allowed to view test entities.
-    \Drupal::currentUser()->setAccount($this->createUser(['uid' => 2], ['view test entity']));
+    \Drupal::currentUser()->setAccount($this->createUser(['view test entity'], NULL, FALSE, ['uid' => 2]));
     $entity = EntityTest::create([
       'name' => 'forbid_access',
     ]);
@@ -173,7 +173,7 @@ public function testDefaultEntityAccess() {
    */
   public function testEntityAccessDefaultController() {
     // The implementation requires that the global user id can be loaded.
-    \Drupal::currentUser()->setAccount($this->createUser(['uid' => 2]));
+    \Drupal::currentUser()->setAccount($this->createUser([], NULL, FALSE, ['uid' => 2]));
 
     // Check that the default access control handler is used for entities that don't
     // have a specific access control handler defined.
@@ -195,7 +195,7 @@ public function testEntityAccessDefaultController() {
   public function testEntityTranslationAccess() {
 
     // Set up a non-admin user that is allowed to view test entity translations.
-    \Drupal::currentUser()->setAccount($this->createUser(['uid' => 2], ['view test entity translations']));
+    \Drupal::currentUser()->setAccount($this->createUser(['view test entity translations'], NULL, FALSE, ['uid' => 2]));
 
     // Create two test languages.
     foreach (['foo', 'bar'] as $langcode) {
@@ -305,7 +305,7 @@ public function testHooks() {
   public function testFieldAccess($entity_class, array $entity_create_values, $expected_id_create_access) {
     // Set up a non-admin user that is allowed to create and update test
     // entities.
-    \Drupal::currentUser()->setAccount($this->createUser(['uid' => 2], ['administer entity_test content']));
+    \Drupal::currentUser()->setAccount($this->createUser(['administer entity_test content'], NULL, FALSE, ['uid' => 2]));
 
     // Create the entity to test field access with.
     $entity = $entity_class::create($entity_create_values);
diff --git a/core/tests/Drupal/KernelTests/Core/Entity/EntityDeprecationTest.php b/core/tests/Drupal/KernelTests/Core/Entity/EntityDeprecationTest.php
new file mode 100644
index 000000000000..5d15114a669e
--- /dev/null
+++ b/core/tests/Drupal/KernelTests/Core/Entity/EntityDeprecationTest.php
@@ -0,0 +1,23 @@
+<?php
+
+namespace Drupal\KernelTests\Core\Entity;
+
+/**
+ * Tests the deprecations for the \Drupal\KernelTests\Core\Entity namespace.
+ *
+ * @group Entity
+ * @group legacy
+ */
+class EntityDeprecationTest extends EntityKernelTestBase {
+
+  /**
+   * Tests deprecation of \Drupal\KernelTests\Core\Entity\EntityKernelTestBase::createUser().
+   */
+  public function testCreateUserDeprecation(): void {
+    $this->expectDeprecation('Calling createUser() with $values as the first parameter is deprecated in drupal:10.1.0 and will be removed from drupal:11.0.0. Use createUser(array $permissions = [], $name = NULL, $admin = FALSE, array $values = []) instead. See https://www.drupal.org/node/3330762');
+    $this->createUser(['uid' => 2]);
+    $this->expectDeprecation('Calling createUser() with $permissions as the second parameter is deprecated in drupal:10.1.0 and will be removed from drupal:11.0.0. Use createUser(array $permissions = [], $name = NULL, $admin = FALSE, array $values = []) instead. See https://www.drupal.org/node/3330762');
+    $this->createUser([], ['administer entity_test content']);
+  }
+
+}
diff --git a/core/tests/Drupal/KernelTests/Core/Entity/EntityKernelTestBase.php b/core/tests/Drupal/KernelTests/Core/Entity/EntityKernelTestBase.php
index 1b83fee07d56..3bd7a1b1c9c3 100644
--- a/core/tests/Drupal/KernelTests/Core/Entity/EntityKernelTestBase.php
+++ b/core/tests/Drupal/KernelTests/Core/Entity/EntityKernelTestBase.php
@@ -103,16 +103,43 @@ protected function setUp(): void {
   /**
    * Creates a user.
    *
-   * @param array $values
-   *   (optional) The values used to create the entity.
    * @param array $permissions
-   *   (optional) Array of permission names to assign to user.
+   *   Array of permission names to assign to user. Note that the user always
+   *   has the default permissions derived from the "authenticated users" role.
+   * @param string $name
+   *   The user name.
+   * @param bool $admin
+   *   (optional) Whether the user should be an administrator
+   *   with all the available permissions.
+   * @param array $values
+   *   (optional) An array of initial user field values.
    *
    * @return \Drupal\user\Entity\User
    *   The created user entity.
    */
-  protected function createUser($values = [], $permissions = []) {
-    return $this->drupalCreateUser($permissions ?: [], NULL, FALSE, $values ?: []);
+  protected function createUser(array $permissions = [], $name = NULL, bool $admin = FALSE, array $values = []) {
+    // Allow for the old signature of this method:
+    // createUser($values = [], $permissions = [])
+    if (!array_is_list($permissions)) {
+      // An array with keys is assumed to be entity values rather than
+      // permissions, since there is no point in an array of permissions having
+      // keys.
+      @trigger_error('Calling createUser() with $values as the first parameter is deprecated in drupal:10.1.0 and will be removed from drupal:11.0.0. Use createUser(array $permissions = [], $name = NULL, $admin = FALSE, array $values = []) instead. See https://www.drupal.org/node/3330762', E_USER_DEPRECATED);
+
+      $values = $permissions;
+      $permissions = [];
+    }
+
+    if (is_array($name)) {
+      // If $name is an array rather than a string, then the caller is intending
+      // to pass in $permissions.
+      @trigger_error('Calling createUser() with $permissions as the second parameter is deprecated in drupal:10.1.0 and will be removed from drupal:11.0.0. Use createUser(array $permissions = [], $name = NULL, $admin = FALSE, array $values = []) instead. See https://www.drupal.org/node/3330762', E_USER_DEPRECATED);
+
+      $permissions = $name;
+      $name = NULL;
+    }
+
+    return $this->drupalCreateUser($permissions, $name, $admin, $values);
   }
 
   /**
diff --git a/core/tests/Drupal/KernelTests/Core/Entity/EntityReferenceSelection/EntityReferenceSelectionSortTest.php b/core/tests/Drupal/KernelTests/Core/Entity/EntityReferenceSelection/EntityReferenceSelectionSortTest.php
index 62a81d3b8bde..55e33a73fae6 100644
--- a/core/tests/Drupal/KernelTests/Core/Entity/EntityReferenceSelection/EntityReferenceSelectionSortTest.php
+++ b/core/tests/Drupal/KernelTests/Core/Entity/EntityReferenceSelection/EntityReferenceSelectionSortTest.php
@@ -36,7 +36,7 @@ protected function setUp(): void {
     $article->save();
 
     // Test as a non-admin.
-    $normal_user = $this->createUser([], ['access content']);
+    $normal_user = $this->createUser(['access content']);
     \Drupal::currentUser()->setAccount($normal_user);
   }
 
-- 
GitLab