Problem/Motivation

This is a follow-on from #3224276: Move useful helper methods for working with entities from EntityKernelTestBase to a trait, in the interest of keeping that issue simple.

There is a confusing mishmash of test methods called createUser():

- Drupal\Tests\user\Traits\UserCreationTrait has createUser() with 4 params, which we'll refer to as createUser4: createUser(array $permissions = [], $name = NULL, $admin = FALSE, array $values = [])
- EntityKernelTestBase has createUser() with 2 params, which we'll refer to as createUser2: createUser($values = [], $permissions = [])
- EntityKernelTestBase imports UserCreationTrait, aliasing createUser as drupalCreateUser
- some child classes of KernelTestBase import UserCreationTrait, and some of them do the same aliasing, and some don't

Proposed resolution

Figure out how to end up in a situation where KernelTestBase imports UserCreationTrait, including createUser() without aliasing.

This is going to be difficult because the methods are incompatible. See https://www.drupal.org/project/drupal/issues/3224276#comment-14681959 for detatils.

Remaining tasks

Figure out the minimum of breakage that can happen.

User interface changes

API changes

Data model changes

Release notes snippet

Issue fork drupal-3324384

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

joachim created an issue. See original summary.

longwave’s picture

longwave’s picture

Wow, this is confusing. If only we hadn't done this!

  protected function createUser($values = [], $permissions = []) {
    return $this->drupalCreateUser($permissions ?: [], NULL, FALSE, $values ?: []);
  }
joachim’s picture

Issue summary: View changes

Is there a way could remove createUser2, and have createUser4 sniff its passed parameter values to figure out which one the caller means, and trigger a deprecation if it detected that the caller passed in createUser2 parameters?

I'm not sure whether that's possible, based on what the caller might be assuming the default values of parameters do.

joachim’s picture

Ok, we'd basically have to detect whether param1 is an array of entity value or an array of permissions:

  protected function createUser($values = [], $permissions = []) {

  protected function createUser(array $permissions = [], $name = NULL, $admin = FALSE, array $values = []) {

We can PROBABLY assume that $permissions is a numeric array? If so, then this would maybe work:

function createUser(array $permissions = [], $name = NULL, $admin = FALSE, array $values = []) {
  // if $permissions has string keys -> it's actually $values!

  // if $name is an array, it's actually $permissions!

  // now carry on as before!

joachim’s picture

Status: Active » Needs review

Let's see if this works...

joachim’s picture

Status: Needs review » Needs work
Issue tags: +Novice

Tagging as novice in case someone can get to this before I do :)

The MR needs all the calls to createUser() that are failing in the test to be changed to the new parameter order.

elber’s picture

ok, I wil try to work on it

elber’s picture

Status: Needs work » Needs review
mondrake’s picture

Issue tags: +PHPStan-1
joachim’s picture

What's the purpose of these lines:

> use phpDocumentor\Reflection\Types\Null_;

elber’s picture

ok sorry it was a mistake. I fixed it in the next commit

Spokje made their first commit to this issue’s fork.

spokje’s picture

Fixed test failures.

elber’s picture

Status: Needs review » Reviewed & tested by the community

I revised the last commit. Tests are passing, coding standards are ok, scope of the issue was fixed and MR is applied without issues. Moving to RTBC

znerol’s picture

Status: Reviewed & tested by the community » Needs work
spokje’s picture

Status: Needs work » Needs review

(Tried to) Address(ed) all threads brought up by @znerol.

znerol’s picture

Status: Needs review » Needs work
spokje’s picture

Status: Needs work » Needs review
longwave’s picture

Status: Needs review » Needs work

Three more minor comments on the MR.

Is this a good place to start using PHP 8 named arguments? Then we can say

$this->createUser(values: ['uid' => 1, 'name' => 'user1'])->save();

or

\Drupal::currentUser()->setAccount($this->createUser(['view test entity'], values: ['uid' => 2]));

instead of repeating NULL, FALSE everywhere.

spokje’s picture

Is this a good place to start using PHP 8 named arguments?

For me it is, but I would like a wider consensus, since I've seen other code style changes being rolled back because of the lack of it.

znerol’s picture

Named arguments would simplify many calls here.

mondrake’s picture

-1 on named arguments in THIS issue. It’s a coding standard issue first, and once approved we will be able to use that.

That will require time (the coding standard queue is pretty slow atm), there’s not even an issue for named arguments yet AFAICS.

Let’s not slow down this issue on that.

mondrake’s picture

see #3323994-13: Fix PHPStan L1 errors "Constructor of class Foo has an unused parameter $bar" and following comments for a similar case where coding standards are slowing down an issue.

joachim’s picture

> Is this a good place to start using PHP 8 named arguments? Then we can say

I have been a bit concerned that calls to createUser() that only care about entity values are a bit ugly. The order of parameters makes sense if you are creating test users to test access and permissions, which I assume is the most common case.

But switching to named function arguments in the future would help that.

Another possibility for the future would be add a createEntity($entity_type_id, $values) method to KernelTestBase, and then tests that only care about the entity values of a user can call that instead.

joachim’s picture

Status: Needs work » Needs review

Dealt with @longwave's comments on the MR.

znerol’s picture

Status: Needs review » Reviewed & tested by the community

Thanks @joachim. This is ready if tests pass.

  • catch committed c6babbb6 on 10.1.x
    Issue #3324384 by Spokje, joachim, elber, znerol, longwave, mondrake:...
catch’s picture

Status: Reviewed & tested by the community » Fixed

Committed c6babbb and pushed to 10.1.x. Thanks!

joachim’s picture

Should we file an 11.x follow-up to remove the deprecation handling?

catch’s picture

It should be covered by the generic 'remove deprecated code from core' issues when they're opened, we generally remove deprecations at a larger scale than they're added.

joachim’s picture

We also need to deprecate drupalCreateUser() - that needs an issue.

quietone’s picture

Issue tags: +Needs followup

Adding tag for a followup.

spokje’s picture

Issue tags: -Needs followup

We're heading into unknown territory with deprecating an "aliased" function, but here we go: #3331682: Deprecate drupalCreateUser().

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.