Problem/Motivation

This is a follow-up to #3569423: Convert expectation-less test mocks to stubs - User module. While I was updating tests to use stubs instead of mocks, I became concerned that UserAuthenticationTest::testAuthenticateWithZeroPassword() isn't testing things as intended:

  public function testAuthenticateWithZeroPassword(): void {
    $this->testUser->expects($this->once())
      ->method('id')
      ->willReturn(2);

    $this->userStorage->expects($this->once())
      ->method('loadByProperties')
      ->with(['name' => $this->username])
      ->willReturn([$this->testUser]);

    $this->passwordService->expects($this->once())
      ->method('check')
      ->with(0, 0)
      ->willReturn(TRUE);

    $this->assertSame(2, $this->userAuth->authenticate($this->username, 0));
  }

The ->with(0, 0) near the end is a red flag that something is going wrong. The check() function takes two parameters:

  1. The plain-text password as entered into a login form
  2. The user's hashed password as stored in their account

In other words, these two parameters should not be the same! My concern is that this test hasn't been testing what we think it's testing. The function needs to be audited to ensure we have proper test coverage.

Proposed resolution

Delete UserAuthenticationTest::testAuthenticateWithZeroPassword(). A deep dive on the history of the function and the justification for removing it is written in comment #7.

Remaining tasks

User interface changes

Introduced terminology

API changes

Data model changes

Release notes snippet

Issue fork drupal-3570206

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

dcam created an issue. See original summary.

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

danielveza’s picture

Status: Active » Needs review

Went through the report and the tests and question. I put some breakpoints in UserAuthentication while the tests were running and the report is correct. I've updated the tests so they're more accurate to what they're actually trying to test.

This is NR assuming tests come back green, if not I'll revisit tomorrow.

danielveza’s picture

Status: Needs review » Needs work

Looks like something to look into there in the pipeline

danielveza’s picture

Status: Needs work » Needs review

Tests green, ready for review

dcam’s picture

Status: Needs review » Needs work

Thank you for working on this and verifying my claims. Unfortunately, this issue requires more than simply setting up new mocks. Someone needs to do a deep dive on these tests, find out why they were created in the first place, and make certain they are still fulfilling that purpose.

For example, I did the deep dive on testAuthenticateWithZeroPassword(). This test was originally created because UserAuth::authenticate() was responsible for doing user entity lookups. For performance reasons it avoided doing the lookup if the password was empty(). But that meant a person could never log in with a falsey value, '0'. The function would short-circuit.

Fast-forward ten years. UserAuth no longer exists. The functions of UserAuth::authenticate() were broken up and superseded by UserAuthentication::lookupAccount() and UserAuthentication::authenticateAccount. The test was relevant to the part of the process that was taken over by lookupAccount(). So there's two issues with the test now:

  1. It's testing authenticateAccount() which isn't the part of the login process that had the problem.
  2. lookupAccount() doesn't skip the entity query based on the password value. It doesn't receive the password at all.

There's no code that matches what UserAuth::authenticate() did anymore. Through all the changes over the years people have updated testAuthenticateWithZeroPassword() with new services and methods thinking that they were doing the right thing. No one realized that at some point they were were testing nothing at all.

In my opinion we can simply delete testAuthenticateWithZeroPassword(). If someone disagrees, that's fine, but they need to provide suggestions for improving the test to actually check something and make the test relevant again. It was never meant to test UserAuthentication::authenticateAccount. I won't get to the others tonight because it's getting late. Feel free to pick up where I left off.

dcam’s picture

Title: UserAuthTest password test audit » Delete UserAuthenticationTest::testAuthenticateWithZeroPassword()
Issue summary: View changes
Status: Needs work » Needs review

I removed testAuthenticateWithIncorrectPassword() and testAuthenticateWithCorrectPassword() from the scope of the issue. They've both changed since the time this issue was created and the worst of their problems were fixed. They pointlessly set up user storage just to call lookupAccount(), which is tested elsewhere. So the functions could be simplified, but it would require us to do major refactorings throughout the rest of the test. They're "fine" now, so I'll leave them alone.

Given my analysis of testAuthenticateWithZeroPassword() in #7 I am proposing that we delete it. I have changed the issue summary to reflect this.

smustgrave’s picture

Status: Needs review » Reviewed & tested by the community

From the standpoint of just deleting seems pretty straight forward. Nice analysis!

dcam’s picture

I had to update the new baseline that resulted from increasing the PHPStan level to 3. No code was changed.

needs-review-queue-bot’s picture

Status: Reviewed & tested by the community » Needs work
StatusFileSize
new1.25 KB

The Needs Review Queue Bot tested this issue. It fails the Drupal core commit checks. Therefore, this issue status is now "Needs work".

This does not mean that the patch necessarily needs to be re-rolled or the MR rebased. Read the Issue Summary, the issue tags and the latest discussion here to determine what needs to be done.

Consult the Drupal Contributor Guide to find step-by-step guides for working with issues.

dcam’s picture

Status: Needs work » Reviewed & tested by the community

It needed a rebase.

mstrelan’s picture

Status: Reviewed & tested by the community » Needs work

Needs a rebase again. I pressed the button on the MR but it needs to be resolved manually.

dcam’s picture

Status: Needs work » Reviewed & tested by the community

  • catch committed d4804ac2 on 12.0.x
    task: #3570206 Delete UserAuthenticationTest::...

  • catch committed ef9e2b72 on main
    task: #3570206 Delete UserAuthenticationTest::...
catch’s picture

Version: main » 12.0.x-dev
Status: Reviewed & tested by the community » Fixed

Committed/pushed to main and 12.0.x, thanks!

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.

Status: Fixed » Closed (fixed)

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