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
// 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 @@ class CommentDefaultFormatterCacheTagsTest extends EntityKernelTestBase {
// 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']);

View File

@ -109,26 +109,27 @@ class CommentFieldAccessTest extends EntityKernelTestBase {
// 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')

View File

@ -61,9 +61,9 @@ class MessageEntityTest extends EntityKernelTestBase {
$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));

View File

@ -329,9 +329,12 @@ class FilterAPITest extends EntityKernelTestBase {
$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();

View File

@ -44,7 +44,7 @@ abstract class LayoutBuilderCompatibilityTestBase extends EntityKernelTestBase {
$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();

View File

@ -62,14 +62,14 @@ class NodeFieldAccessTest extends EntityKernelTestBase {
// 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 = [

View File

@ -66,15 +66,15 @@ class UserEntityReferenceTest extends EntityKernelTestBase {
$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();

View File

@ -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

View File

@ -338,7 +338,7 @@ class EntityAutocompleteElementFormTest extends EntityKernelTestBase implements
$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);

View File

@ -57,7 +57,7 @@ class EntityAccessControlHandlerTest extends EntityLanguageTestBase {
*/
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 @@ class EntityAccessControlHandlerTest extends EntityLanguageTestBase {
*/
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 @@ class EntityAccessControlHandlerTest extends EntityLanguageTestBase {
*/
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 @@ class EntityAccessControlHandlerTest extends EntityLanguageTestBase {
*/
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 @@ class EntityAccessControlHandlerTest extends EntityLanguageTestBase {
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 @@ class EntityAccessControlHandlerTest extends EntityLanguageTestBase {
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);

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.
*
* @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);
}
/**

View File

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