The documentation for \Drupal\user\UserInterface::setExistingPassword states that the $this is the return value:

  /**
   * Sets the existing plain text password.
   *
   * Required for validation when changing the password, name or email fields.
   *
   * @param string $password
   *   The existing plain text password of the user.
   *
   * @return $this
   */
  public function setExistingPassword($password);

However, \Drupal\user\Entity\User::setExistingPassword does not return a value:

  /**
   * {@inheritdoc}
   */
  public function setExistingPassword($password) {
    $this->get('pass')->existing = $password;
  }

I'll add a patch to the comments.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

Jaypan created an issue. See original summary.

Jaypan’s picture

jungle’s picture

Status: Needs review » Reviewed & tested by the community

Thanks, @Jaypan! The fix is straight. No need to add a test, no CS violation.

init90’s picture

Related issues: +#3048638: Chain File setters

Added relevant issue.

xjm’s picture

Status: Reviewed & tested by the community » Needs work
Issue tags: +Needs tests

This is an interesting case for BC. Technically, returning something different is a BC break. However, since it only ever returned NULL before, there's no reason a caller would do anything with the return value. So, I think this is OK.

However, we should add a unit test for this. Thanks!

munish.kumar’s picture

Status: Needs work » Needs review
FileSize
1.38 KB
848 bytes

Hi @xjm,

I have updated the patch with the test cases, Please review the patch.

Jaypan’s picture

Status: Needs review » Needs work

I don't believe that test matches the issue. The test should be confirming that $user->setExistingPassword() returns a Drupal\user\Entity\User object.

munish.kumar’s picture

Status: Needs work » Needs review
FileSize
1.32 KB
803 bytes

Hi @jaypan,

Thanks for your valuable feedback. I have made changes in the test cases as mentioned in #7. Please review.

munish.kumar’s picture

FileSize
1.32 KB
897 bytes

Please ignore the previous patch, Please review the latest one in which #7 is addressed.

Jaypan’s picture

Status: Needs review » Reviewed & tested by the community

Looks good to me.

jungle’s picture

BTW, would be better to alter an existed test to demonstrate the usage of chaining with setExistingPassword(), however, no such alterable test found.

+++ b/core/modules/user/tests/src/Functional/UserPasswordResetTest.php
@@ -458,4 +458,17 @@ public function testResetImpersonation() {
+    $user = $user->setExistingPassword('existing_pass');
+    $this->assertInstanceOf(User::class, $user);

Prefer one-line $this->assertInstanceOf(User::class, $user->setExistingPassword('existing_pass')); but it really does not matter.

The test added in #9 is good and enough. So RTBC +1.

johnwebdev’s picture

Issue tags: -Needs tests
catch’s picture

Status: Reviewed & tested by the community » Needs work
+++ b/core/modules/user/tests/src/Functional/UserPasswordResetTest.php
@@ -458,4 +458,17 @@ public function testResetImpersonation() {
 
+  /**
+   * Tests that the user entity is created.
+   */
+  public function testExistingPassword() {
+    /** @var \Drupal\user\Entity\User $user */
+    $user = User::create([
+      'name' => $this->randomMachineName(),
+    ]);
+    $user->save();
+    $user = $user->setExistingPassword('existing_pass');
+    $this->assertInstanceOf(User::class, $user);
+  }
+

This could be done in a new kernel test - doing it in a new method on a functional test means we have to do a full install of Drupal.

johnwebdev’s picture

Status: Needs work » Needs review
FileSize
1.16 KB
1.33 KB

Moved the test to UserEntityTest instead. Removed the ::save() call because it's not needed, also renamed the test case to be more descriptive of what we're actually testing.

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.

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.

smustgrave’s picture

Status: Needs review » Reviewed & tested by the community

Looks good to me.

alexpott’s picture

Version: 9.5.x-dev » 9.4.x-dev
Status: Reviewed & tested by the community » Fixed

Committed and pushed 51e7a08352 to 10.1.x and 0763535604 to 10.0.x and 6764848b9a to 9.5.x. Thanks!
Committed 844acce and pushed to 9.4.x. Thanks!

  • alexpott committed 51e7a08 on 10.1.x
    Issue #3137119 by munish.kumar, johnwebdev, Jaypan, jungle, xjm, catch:...

  • alexpott committed 0763535 on 10.0.x
    Issue #3137119 by munish.kumar, johnwebdev, Jaypan, jungle, xjm, catch:...

  • alexpott committed 6764848 on 9.5.x
    Issue #3137119 by munish.kumar, johnwebdev, Jaypan, jungle, xjm, catch:...
mondrake’s picture

Priority: Normal » Critical
Status: Fixed » Needs work

This broke PHPStan testing on 10+, please revert

  • xjm committed 72330ce on 9.5.x
    Revert "Issue #3137119 by munish.kumar, johnwebdev, Jaypan, jungle, xjm...

  • xjm committed dbd151b on 10.0.x
    Revert "Issue #3137119 by munish.kumar, johnwebdev, Jaypan, jungle, xjm...

  • xjm committed 5681b9c on 10.1.x
    Revert "Issue #3137119 by munish.kumar, johnwebdev, Jaypan, jungle, xjm...
xjm’s picture

Priority: Critical » Normal

Reverted from 9.5.x, 10.0.x, and 10.1.x.

I think the 9.5.x patch was fine, but the D10 patches need to do something with the phpstan baseline or something. https://www.drupal.org/pift-ci-job/2422757

  • alexpott committed 98a9766 on 10.1.x
    Revert "Revert "Issue #3137119 by munish.kumar, johnwebdev, Jaypan,...
  • alexpott committed d440b3c on 10.1.x
    Issue #3137119 follow-up: User::setExistingPassword() does not return $...

  • alexpott committed 5387577 on 10.0.x
    Revert "Revert "Issue #3137119 by munish.kumar, johnwebdev, Jaypan,...
  • alexpott committed 9064e58 on 10.0.x
    Issue #3137119 follow-up: User::setExistingPassword() does not return $...

  • alexpott committed f04ef57 on 9.5.x
    Revert "Revert "Issue #3137119 by munish.kumar, johnwebdev, Jaypan,...

  • alexpott committed 9966afc on 9.4.x
    Issue #3137119 by munish.kumar, johnwebdev, Jaypan, jungle, xjm, catch:...
mondrake’s picture

This is the error on D10+ branches:

 ------ --------------------------------------------------------------------- 
  Line   core/modules/user/src/Entity/User.php                                
 ------ --------------------------------------------------------------------- 
         Ignored error pattern #^Method                                       
         Drupal\\user\\Entity\\User\:\:setExistingPassword\(\) should return  
         \$this\(Drupal\\user\\Entity\\User\) but return statement is         
         missing\.$# in path                                                  
         /var/www/html/core/modules/user/src/Entity/User.php was not matched  
         in reported errors.                                                  
 ------ --------------------------------------------------------------------- 


 [ERROR] Found 1 error                                                          

That means that the pattern indicated, that can be found in phpstan-baseline.json in the core directory for the 10+ branches, should be removed from that file since the error is not occurring anymore.

mondrake’s picture

Status: Needs work » Fixed

@alexpott fixed this.

Status: Fixed » Closed (fixed)

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