Problem/Motivation

BrowserTestBase has its own method for creating users (as well as implementations of the other methods provided by the UserCreationTrait). The method is incompatible with the one used in WebTestBase, since it is missing the third parameter that allows the user to be created with an admin role.

Proposed resolution

Remove the separate methods, and use the UserCreationTrait instead. This will allow the eventual conversion of WebTestBase without having to rework admin users.

Remaining tasks

User interface changes

API changes

Data model changes

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

jhedstrom created an issue. See original summary.

jhedstrom’s picture

Issue summary: View changes
Status: Active » Needs review
FileSize
5.99 KB
pfrenssen’s picture

Status: Needs review » Reviewed & tested by the community

Great job, simplifies the code base and leverages an existing trait.

+++ b/core/modules/simpletest/src/UserCreationTrait.php
@@ -77,6 +77,8 @@ protected function createUser(array $permissions = array(), $name = NULL, $admin
+    // Support BrowserTestBase as well.
+    $account->passRaw = $account->pass_raw;

+++ b/core/tests/Drupal/Tests/BrowserTestBase.php
@@ -56,6 +57,10 @@
 

This is not the most elegant thing, but for BC reasons it's fine to do it like this. We cannot rename this property any more at this point.

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 2: 2758067-02.patch, failed testing.

jhedstrom’s picture

Status: Needs work » Reviewed & tested by the community

Looks to be a random fail, back to RTBC.

  • xjm committed c572b92 on 8.2.x
    Issue #2758067 by jhedstrom, pfrenssen: BrowserTestBase::...
xjm’s picture

Status: Reviewed & tested by the community » Fixed

This is not the most elegant thing, but for BC reasons it's fine to do it like this. We cannot rename this property any more at this point.

Could we add $passRaw to the testing frameworks and deprecate $pass_raw? Since our coding standards do say class members should be camelCase. That could be a followup; not in scope here. Then we could simply remove the old, inconsistently named properties in 9.x.

+++ b/core/tests/Drupal/Tests/BrowserTestBase.php
@@ -56,6 +57,10 @@
+  use UserCreationTrait {
+    createRole as drupalCreateRole;
+    createUser as drupalCreateUser;
+  }

This is also something that might benefit from a followup for method addition/deprecation to later rename these (for both web and browser tests). (Could be part of the same followup.)

I read over the removed code and confirmed it's pretty much pure duplicated code. Diffstats like this make me happy! And it's completely BC as a bonus. Nice work.

Committed c572b92 and pushed to 8.2.x. Thanks!

Removed now-unused use statements on commit:

diff --git a/core/tests/Drupal/Tests/BrowserTestBase.php b/core/tests/Drupal/Tests/BrowserTestBase.php
index 4a8ac3b..1da673a 100644
--- a/core/tests/Drupal/Tests/BrowserTestBase.php
+++ b/core/tests/Drupal/Tests/BrowserTestBase.php
@@ -28,8 +28,6 @@
 use Drupal\simpletest\BlockCreationTrait;
 use Drupal\simpletest\NodeCreationTrait;
 use Drupal\simpletest\UserCreationTrait;
-use Drupal\user\Entity\Role;
-use Drupal\user\Entity\User;
 use Symfony\Component\CssSelector\CssSelectorConverter;
 use Symfony\Component\HttpFoundation\Request;
jhedstrom’s picture

Could we add $passRaw to the testing frameworks and deprecate $pass_raw?

I'm not sure how we'd denote this as deprecated, since the variable on $account isn't declared anywhere besides in documentation. However, I did take an initial approach here: #2768765: Deprecate $pass_raw in UserCreationTrait and DrupalLogin documentation.

Eric_A’s picture

Can we have this in 8.1.x just like we did for the previous BrowserTestBase changes? The touched files are identical in 8.2.x and 8.1.x (core/tests/Drupal/Tests/BrowserTestBase.php and core/modules/simpletest/src/UserCreationTrait.php)

Because everything is identical git cherry-pick c572b92 -X theirs does the job and is as effective as resolving the conflict. (git cherry-pick c572b92 doesn't automatically resolve the "conflict". BrowserTestBase has followed a different route in 8.1.x than in 8.2.x. A couple of the earlier commits weren't cherry-picked but applied separately.)

Therefor committing this one with git cherry-pick c572b92 -X theirs is preferable to doing a completely separate commit.

jhedstrom’s picture

Version: 8.2.x-dev » 8.1.x-dev
Status: Fixed » Needs review

@Eric_A I think this would qualify for a backport, but since it doesn't apply cleanly, a patch would need to be uploaded here (using the method you describe in #9) to verify that all the tests still pass in 8.1.x.

Status: Needs review » Needs work

The last submitted patch, 2: 2758067-02.patch, failed testing.

Eric_A’s picture

Status: Needs work » Needs review
FileSize
6.49 KB

Here is a patch created after git cherry-picking!

As noted in #9: the touched files are actually identical in 8.1.x.
To avoid possible future BrowserTestBase backport problems with git cherry-pick it would certainly help if core committers would actually cherry-pick this one, either with conflict resolution or in this special case with git cherry-pick c572b92 -X theirs.

Thanks, @jhedstrom for pointing out some stuff.

Eric_A’s picture

Hmm, guess I missed something earlier when i said the touched files were identical.

8.2.x - 2758067-02.patch:
- $assigned_permissions = Role::load($role->id())->getPermissions();

8.1.x - 2758067-12.patch:
- $assigned_permissions = entity_load('user_role', $role->id())->getPermissions();

The git cherry-pick c572b92 -X theirs trick worked out fine here, bit my earlier reasoning doesn't apply.

dawehner’s picture

Status: Needs review » Reviewed & tested by the community

What happens when an existing subclass uses this trait already. Doesn't that cause some issues? Nope, it doesn't see https://3v4l.org/lsIgN

Given that we continue to work on those tests issues having something more in sync is not a bad idea in the first place.

xjm’s picture

So I'm not sure about backporting this change to 8.1.x. If it were production code, I probably would not consider it, despite @dawehner's reassurance about it being non-disruptive above. Our allowed changes policy considers internal refactoring (for which this might qualify) for patch releases at committer discretion only.

Other improvements intended to make the test conversions easier have been going into 8.2.x only, so the branches have already diverged. What is the case for backporting this?

xjm’s picture

Eric_A’s picture

Eric_A’s picture

Other improvements intended to make the test conversions easier have been going into 8.2.x only, so the branches have already diverged.

At this moment only the last two changes to BrowserTestBase did not make it into 8.1.x. The entity_load() conversion (unrelated to test conversion) was responsible for divergence. But if we backport here that implementation divergence will be moved into a trait, which would be really nice.

xjm’s picture

Based on git diff 8.2.x -- core/tests/Drupal/Tests/BrowserTestBase.php as well as feedback from @Eric_A, @dawehner, and @Berdir on #2773087: [policy, maybe patch] Clarify backport policy for testing framework changes, including new BTB tests, I will backport this to 8.1.x. However, none of the suggested cherry-picks or uploaded patches cleanly fixed the use statement issue I previously resolved, so to be on the safe side, here's a clean backport patch so we can ensure tests pass with it.

xjm’s picture

Status: Reviewed & tested by the community » Fixed

Committed 4775a28 and pushed to 8.1.x. Thanks!

  • xjm committed 4775a28 on 8.1.x
    Issue #2758067 by jhedstrom, Eric_A, xjm, dawehner, pfrenssen:...
xjm’s picture

Status: Reviewed & tested by the community » Fixed

  • xjm committed c572b92 on 8.3.x
    Issue #2758067 by jhedstrom, pfrenssen: BrowserTestBase::...

  • xjm committed c572b92 on 8.3.x
    Issue #2758067 by jhedstrom, pfrenssen: BrowserTestBase::...

Status: Fixed » Closed (fixed)

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