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
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
Comment #2
longwaveThis is also confusing PHPStan, see #3319582-19: Fix calls to methods with too many parameters passed in
Comment #3
longwaveWow, this is confusing. If only we hadn't done this!
Comment #4
joachim commentedIs 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.
Comment #5
joachim commentedOk, we'd basically have to detect whether param1 is an array of entity value or an array of permissions:
We can PROBABLY assume that $permissions is a numeric array? If so, then this would maybe work:
Comment #6
joachim commentedLet's see if this works...
Comment #8
joachim commentedTagging 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.
Comment #9
elberok, I wil try to work on it
Comment #10
elberComment #11
mondrakeComment #12
joachim commentedWhat's the purpose of these lines:
> use phpDocumentor\Reflection\Types\Null_;
Comment #13
elberok sorry it was a mistake. I fixed it in the next commit
Comment #15
spokjeFixed test failures.
Comment #16
elberI 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
Comment #17
znerol commentedComment #18
spokje(Tried to) Address(ed) all threads brought up by @znerol.
Comment #19
znerol commentedComment #20
spokjeComment #21
longwaveThree more minor comments on the MR.
Is this a good place to start using PHP 8 named arguments? Then we can say
or
instead of repeating NULL, FALSE everywhere.
Comment #22
spokjeFor 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.
Comment #23
znerol commentedNamed arguments would simplify many calls here.
Comment #24
mondrake-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.
Comment #25
mondrakesee #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.
Comment #26
joachim commented> 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.
Comment #27
joachim commentedDealt with @longwave's comments on the MR.
Comment #28
znerol commentedThanks @joachim. This is ready if tests pass.
Comment #30
catchCommitted c6babbb and pushed to 10.1.x. Thanks!
Comment #31
joachim commentedShould we file an 11.x follow-up to remove the deprecation handling?
Comment #32
catchIt 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.
Comment #33
joachim commentedWe also need to deprecate drupalCreateUser() - that needs an issue.
Comment #34
quietone commentedAdding tag for a followup.
Comment #35
spokjeWe're heading into unknown territory with deprecating an "aliased" function, but here we go: #3331682: Deprecate drupalCreateUser().