Issue #3324384 by Spokje, joachim, elber, znerol, longwave, mondrake: resolve differing createUser() test methods

merge-requests/2994/head^2
catch 2023-01-06 11:14:45 +00:00
parent d496172480
commit c6babbb64e
13 changed files with 90 additions and 41 deletions

View File

@ -37,7 +37,7 @@ class CommentDefaultFormatterCacheTagsTest extends EntityKernelTestBase {
// Create user 1 so that the user created later in the test has a different // Create user 1 so that the user created later in the test has a different
// user ID. // user ID.
// @todo Remove in https://www.drupal.org/node/540008. // @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'); $this->container->get('module_handler')->loadInclude('comment', 'install');
comment_install(); comment_install();
@ -56,7 +56,7 @@ class CommentDefaultFormatterCacheTagsTest extends EntityKernelTestBase {
// user does not have access to the 'administer comments' permission, to // user does not have access to the 'administer comments' permission, to
// ensure only published comments are visible to the end user. // ensure only published comments are visible to the end user.
$current_user = $this->container->get('current_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. // Install tables and config needed to render comments.
$this->installSchema('comment', ['comment_entity_statistics']); $this->installSchema('comment', ['comment_entity_statistics']);

View File

@ -109,26 +109,27 @@ class CommentFieldAccessTest extends EntityKernelTestBase {
// An administrator user. No user exists yet, ensure that the first user // An administrator user. No user exists yet, ensure that the first user
// does not have UID 1. // does not have UID 1.
$comment_admin_user = $this->createUser(['uid' => 2, 'name' => 'admin'], [ $comment_admin_user = $this->createUser([
'administer comments', 'administer comments',
'access comments', 'access comments',
]); ], 'admin', FALSE, ['uid' => 2]);
// Two comment enabled users, one with edit access. // Two comment enabled users, one with edit access.
$comment_enabled_user = $this->createUser(['name' => 'enabled'], [ $comment_enabled_user = $this->createUser([
'post comments', 'post comments',
'skip comment approval', 'skip comment approval',
'edit own comments', 'edit own comments',
'access comments', 'access comments',
]); ], 'enabled');
$comment_no_edit_user = $this->createUser(['name' => 'no edit'], [
$comment_no_edit_user = $this->createUser([
'post comments', 'post comments',
'skip comment approval', 'skip comment approval',
'access comments', 'access comments',
]); ], 'no edit');
// An unprivileged user. // 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 = Role::load(RoleInterface::ANONYMOUS_ID);
$role->grantPermission('post comments') $role->grantPermission('post comments')

View File

@ -61,9 +61,9 @@ class MessageEntityTest extends EntityKernelTestBase {
$this->assertEquals('sender_mail', $message->getSenderMail()); $this->assertEquals('sender_mail', $message->getSenderMail());
$this->assertTrue($message->copySender()); $this->assertTrue($message->copySender());
$no_access_user = $this->createUser(['uid' => 2]); $no_access_user = $this->createUser([], NULL, FALSE, ['uid' => 2]);
$access_user = $this->createUser(['uid' => 3], ['access site-wide contact form']); $access_user = $this->createUser(['access site-wide contact form'], NULL, FALSE, ['uid' => 3]);
$admin = $this->createUser(['uid' => 4], ['administer contact forms']); $admin = $this->createUser(['administer contact forms'], NULL, FALSE, ['uid' => 4]);
$this->assertFalse(\Drupal::entityTypeManager()->getAccessControlHandler('contact_message')->createAccess(NULL, $no_access_user)); $this->assertFalse(\Drupal::entityTypeManager()->getAccessControlHandler('contact_message')->createAccess(NULL, $no_access_user));
$this->assertTrue(\Drupal::entityTypeManager()->getAccessControlHandler('contact_message')->createAccess(NULL, $access_user)); $this->assertTrue(\Drupal::entityTypeManager()->getAccessControlHandler('contact_message')->createAccess(NULL, $access_user));

View File

@ -329,9 +329,12 @@ class FilterAPITest extends EntityKernelTestBase {
$this->assertInstanceOf(OptionsProviderInterface::class, $data); $this->assertInstanceOf(OptionsProviderInterface::class, $data);
$filtered_html_user = $this->createUser(['uid' => 2], [ $filtered_html_user = $this->createUser(
FilterFormat::load('filtered_html')->getPermissionName(), [FilterFormat::load('filtered_html')->getPermissionName()],
]); NULL,
FALSE,
['uid' => 2]
);
// Test with anonymous user. // Test with anonymous user.
$user = new AnonymousUserSession(); $user = new AnonymousUserSession();

View File

@ -44,7 +44,7 @@ abstract class LayoutBuilderCompatibilityTestBase extends EntityKernelTestBase {
$this->installConfig(['filter']); $this->installConfig(['filter']);
// Set up a non-admin user that is allowed to view test entities. // 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']); \Drupal::service('theme_installer')->install(['starterkit_theme']);
$this->config('system.theme')->set('default', 'starterkit_theme')->save(); $this->config('system.theme')->set('default', 'starterkit_theme')->save();

View File

@ -62,14 +62,14 @@ class NodeFieldAccessTest extends EntityKernelTestBase {
// An administrator user. No user exists yet, ensure that the first user // An administrator user. No user exists yet, ensure that the first user
// does not have UID 1. // 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. // Two different editor users.
$page_creator_user = $this->createUser([], ['create page content', 'edit own page content', 'delete own 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']); $page_manager_user = $this->createUser(['create page content', 'edit any page content', 'delete any page content']);
// An unprivileged user. // An unprivileged user.
$page_unrelated_user = $this->createUser([], ['access content']); $page_unrelated_user = $this->createUser(['access content']);
// List of all users // List of all users
$test_users = [ $test_users = [

View File

@ -66,15 +66,15 @@ class UserEntityReferenceTest extends EntityKernelTestBase {
$field_definition->save(); $field_definition->save();
// cspell:ignore aabb aabbb aabbbb aabbbb // cspell:ignore aabb aabbb aabbbb aabbbb
$user1 = $this->createUser(['name' => 'aabb']); $user1 = $this->createUser([], 'aabb');
$user1->addRole($this->role1->id()); $user1->addRole($this->role1->id());
$user1->save(); $user1->save();
$user2 = $this->createUser(['name' => 'aabbb']); $user2 = $this->createUser([], 'aabbb');
$user2->addRole($this->role1->id()); $user2->addRole($this->role1->id());
$user2->save(); $user2->save();
$user3 = $this->createUser(['name' => 'aabbbb']); $user3 = $this->createUser([], 'aabbbb');
$user3->addRole($this->role2->id()); $user3->addRole($this->role2->id());
$user3->save(); $user3->save();

View File

@ -3050,11 +3050,6 @@ parameters:
count: 2 count: 2
path: tests/Drupal/KernelTests/Core/Entity/EntityDecoupledTranslationRevisionsTest.php 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\\.$#" message: "#^Variable \\$field might not be defined\\.$#"
count: 9 count: 9

View File

@ -338,7 +338,7 @@ class EntityAutocompleteElementFormTest extends EntityKernelTestBase implements
$this->assertEquals($expected, $form['tags_access']['#value']); $this->assertEquals($expected, $form['tags_access']['#value']);
// Set up a non-admin user that is *not* allowed to view test entities. // 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. // Rebuild the form.
$form = $form_builder->getForm($this); $form = $form_builder->getForm($this);

View File

@ -57,7 +57,7 @@ class EntityAccessControlHandlerTest extends EntityLanguageTestBase {
*/ */
public function testUserLabelAccess() { public function testUserLabelAccess() {
// Set up a non-admin user. // 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(); $anonymous_user = User::getAnonymousUser();
$user = $this->createUser(); $user = $this->createUser();
@ -111,7 +111,7 @@ class EntityAccessControlHandlerTest extends EntityLanguageTestBase {
*/ */
public function testEntityAccess() { public function testEntityAccess() {
// Set up a non-admin user that is allowed to view test entities. // 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' // Use the 'entity_test_label' entity type in order to test the 'view label'
// access operation. // access operation.
@ -154,7 +154,7 @@ class EntityAccessControlHandlerTest extends EntityLanguageTestBase {
*/ */
public function testDefaultEntityAccess() { public function testDefaultEntityAccess() {
// Set up a non-admin user that is allowed to view test entities. // 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([ $entity = EntityTest::create([
'name' => 'forbid_access', 'name' => 'forbid_access',
]); ]);
@ -173,7 +173,7 @@ class EntityAccessControlHandlerTest extends EntityLanguageTestBase {
*/ */
public function testEntityAccessDefaultController() { public function testEntityAccessDefaultController() {
// The implementation requires that the global user id can be loaded. // 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 // Check that the default access control handler is used for entities that don't
// have a specific access control handler defined. // have a specific access control handler defined.
@ -195,7 +195,7 @@ class EntityAccessControlHandlerTest extends EntityLanguageTestBase {
public function testEntityTranslationAccess() { public function testEntityTranslationAccess() {
// Set up a non-admin user that is allowed to view test entity translations. // 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. // Create two test languages.
foreach (['foo', 'bar'] as $langcode) { foreach (['foo', 'bar'] as $langcode) {
@ -305,7 +305,7 @@ class EntityAccessControlHandlerTest extends EntityLanguageTestBase {
public function testFieldAccess($entity_class, array $entity_create_values, $expected_id_create_access) { 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 // Set up a non-admin user that is allowed to create and update test
// entities. // 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. // Create the entity to test field access with.
$entity = $entity_class::create($entity_create_values); $entity = $entity_class::create($entity_create_values);

View File

@ -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']);
}
}

View File

@ -103,16 +103,43 @@ abstract class EntityKernelTestBase extends KernelTestBase {
/** /**
* Creates a user. * Creates a user.
* *
* @param array $values
* (optional) The values used to create the entity.
* @param array $permissions * @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 * @return \Drupal\user\Entity\User
* The created user entity. * The created user entity.
*/ */
protected function createUser($values = [], $permissions = []) { protected function createUser(array $permissions = [], $name = NULL, bool $admin = FALSE, array $values = []) {
return $this->drupalCreateUser($permissions ?: [], NULL, FALSE, $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);
} }
/** /**

View File

@ -36,7 +36,7 @@ class EntityReferenceSelectionSortTest extends EntityKernelTestBase {
$article->save(); $article->save();
// Test as a non-admin. // Test as a non-admin.
$normal_user = $this->createUser([], ['access content']); $normal_user = $this->createUser(['access content']);
\Drupal::currentUser()->setAccount($normal_user); \Drupal::currentUser()->setAccount($normal_user);
} }