Follow-up to #2917584-49: Some tests only go green because they happen to run as UID1

Problem/Motivation

UserViewsFieldAccessTest extends FieldFieldAccessTestBase, where we have $userWithAccess instance with unexpected user/1 mode.

As a result, the UserViewsFieldAccessTest::testUserFields() checks access for user with full access, which is obviously not the goal of this test.

However, it would be good to keep assertions for user with 'administer users' permission too.

If we move this assertions to separate test, than another fun challenge arises. What to do with a commented code?

    // $this->assertFieldAccess('user', 'mail', 'druplicon@drop.org');
    // $this->assertFieldAccess('user', 'created', \Drupal::service('date.formatter')->format(123456));
    // $this->assertFieldAccess('user', 'changed', \Drupal::service('date.formatter')->format(REQUEST_TIME));

Proposed resolution

  1. Decouple test on two tests:
    • for user with 'view test entity field', 'access content' permissions
    • for user with 'administer users' permission
  2. Do not touch the commented code (just duplicate it to both tests), because we already have issue about it (#2464635: Follow-up: Enable additional tests for base entity field access checking in Views)
  3. Fix unexpected user/1 access for $userWithAccess in FieldFieldAccessTestBase, to prevent similar defects in the future.
  4. Override $userWithAccess inside UserViewsFieldAccessTest tests, and fixing $userWithAccess of FieldFieldAccessTestBase in #540008: Add a container parameter that can remove the special behavior of UID#1.

Remaining tasks

To choose the most suitable solution:

  • fix the root of the problem - in the FieldFieldAccessTestBase class (see #2)
  • fix only the effects of the problem in the UserViewsFieldAccessTest (see #4, #5).

User interface changes

API changes

Data model changes

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

Anonymous’s picture

vaplas created an issue. See original summary.

Anonymous’s picture

Status: Active » Needs review
FileSize
5.02 KB

Patch from @kristiaanvandeneynde.

kristiaanvandeneynde’s picture

Awesome, thanks for creating the spin-off @vaplas!

In the other issue, we decided to remove the changes to the FileManagedUnitTestBase class because we could not know who could have extended that. Shouldn't we be doing the same here and fix the base class in the UID 1 patch?

Anonymous’s picture

Thanks, @kristiaanvandeneynde. That's a great question. When I made the IS, I thought about it in passing, but I cast aside doubts after this part of FieldFieldAccessTestBase:

$role_with_access = Role::create([
    'id' => 'with_access',
    'permissions' => ['view test entity field'],
  ]);
$role_with_access->save();
$this->userWithAccess = User::create([
    'name' => $this->randomMachineName()
    'roles' => [$role_with_access->id()],
  ]);

Which clearly indicates that limited permissions are assumed for userWithAccess. Other tests should only rely on 'view test entity field' permission for this user, and their fall will be a fair indication of the flaws in assertions. But for BC, I absolutely agree to transfer this to the main issue.

Anonymous’s picture

Hm.. trying to fix regression, because with #4 we cann't prevent userWithAccess/UID1 for new tests in UserViewsFieldAccessTest.

kristiaanvandeneynde’s picture

We could update UserViewsFieldAccessTest to recreate the user and load the admin role from the parent to add the proper permissions. Maybe it's overkill and we should just leave the test base as is. I'm not sure.

How about we stick with the patch in #2 and see what the core committers think of it?

Anonymous’s picture

Sure :)

kristiaanvandeneynde’s picture

Status: Needs review » Reviewed & tested by the community

Okay, RTBC for #2 then. Let's see what core committers have to say about the base class change. Impact should be minimal to no as it adds the right permission for the base class to still function, but you never know.

Anonymous’s picture

Issue summary: View changes

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 5: 2928648-5.patch, failed testing. View results

Anonymous’s picture

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

Status: Reviewed & tested by the community » Needs review
  1. +++ b/core/modules/user/tests/src/Kernel/Views/UserViewsFieldAccessTest.php
    @@ -46,20 +52,50 @@ public function testUserFields() {
    +    $this->assertFieldAccess('user', 'name', 'test user');
    

    is this correct? I thought user names were only available for those with the 'view user profiles' permission? Apologies if that is already granted but not present in the diff.

  2. +++ b/core/modules/user/tests/src/Kernel/Views/UserViewsFieldAccessTest.php
    @@ -46,20 +52,50 @@ public function testUserFields() {
    +    // $this->assertFieldAccess('user', 'mail', 'druplicon@drop.org');
    +    // $this->assertFieldAccess('user', 'created', \Drupal::service('date.formatter')->format(123456));
    +    // $this->assertFieldAccess('user', 'changed', \Drupal::service('date.formatter')->format(REQUEST_TIME));
    ...
    +    // $this->assertFieldAccess('user', 'mail', 'druplicon@drop.org');
    

    Let's punt this to the followup (existing followup)

  3. +++ b/core/modules/views/tests/src/Kernel/Handler/FieldFieldAccessTestBase.php
    @@ -40,9 +40,14 @@ protected function setUp($import_test_views = TRUE) {
    +    User::create(['uid' => 1, 'name' => 'user1'])->save();
    

    yeah I'm with @kristiaanvandeneynde, lets not do this in the base class as it may lead to unexpected issues

kristiaanvandeneynde’s picture

@larowlan in #12:

  1. Everyone can view it as it's the label. See below, taken from UserAccessControlHandler
    case 'name':
            // Allow view access to anyone with access to the entity. Anonymous
            // users should be able to access the username field during the
            // registration process, otherwise the username and email constraints
            // are not checked.
            if ($operation == 'view' || /* snip */ ) {
              return AccessResult::allowed()->cachePerPermissions();
            }
  2. Okay so which part do we punt? All of it, including the lines that were there (commented out) but got rearranged? Trying to not throw out the rearranged lines is near impossible because they never should have been in the low-access test in the first place. They belong in the admin-access test.
  3. Done

This test might fail as I threw out the 'access content' permission because I don't think we need it in the user test. We might need to add said permission in the node tests in the parent issue that strips UID 1 of its power. I'll add a comment in the parent issue if this one goes green.

The patch also contains the commented out lines still. I'm not sure which ones to remove (as stated in 2.)

Status: Needs review » Needs work

The last submitted patch, 13: drupal-2928648-13.patch, failed testing. View results

kristiaanvandeneynde’s picture

Status: Needs work » Needs review

Unrelated failure

kristiaanvandeneynde’s picture

2018 bump. This is the first domino in a series that might lead to getting #540008: Add a container parameter that can remove the special behavior of UID#1 committed.

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.0-alpha1 will be released the week of January 17, 2018, which means new developments and disruptive changes should now be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.6.x-dev » 8.7.x-dev

Drupal 8.6.0-alpha1 will be released the week of July 16, 2018, which means new developments and disruptive changes should now be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.7.x-dev » 8.8.x-dev

Drupal 8.7.0-alpha1 will be released the week of March 11, 2019, which means new developments and disruptive changes should now be targeted against the 8.8.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.0-alpha1 will be released the week of October 14th, 2019, which means new developments and disruptive changes should now be targeted against the 8.9.x-dev branch. (Any changes to 8.9.x will also be committed to 9.0.x in preparation for Drupal 9’s release, but some changes like significant feature additions will be deferred to 9.1.x.). For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.9.x-dev » 9.1.x-dev

Drupal 8.9.0-beta1 was released on March 20, 2020. 8.9.x is the final, long-term support (LTS) minor release of Drupal 8, which means new developments and disruptive changes should now be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 9.1.x-dev » 9.2.x-dev

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

daffie’s picture

Status: Needs review » Needs work
Issue tags: +Needs reroll
adityasingh’s picture

Status: Needs work » Needs review
Issue tags: -Needs reroll
FileSize
4.35 KB
1.58 KB

Reroll the patch. Please review.

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.5.x-dev » 10.1.x-dev

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

smustgrave’s picture

Status: Needs review » Needs work
Issue tags: +Needs Review Queue Initiative

This issue is being reviewed by the kind folks in Slack, #needs-review-queue-initiative. We are working to keep the size of Needs Review queue [2700+ issues] to around 400 (1 month or less), following Review a patch or merge request as a guide.

+    Role::create([
+      'id' => 'user_admin',
+      'label' => 'User role',
+      'permissions' => ['administer users'],
+    ])->save();

Instead of creating roles and users this way think it should use UserCreationTrait

Version: 10.1.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.