8000 Fix same problem in isPasswordValid · symfony/symfony@7d23db8 · GitHub
[go: up one dir, main page]

Skip to content

Commit 7d23db8

Browse files
committed
Fix same problem in isPasswordValid
1 parent c92dfc5 commit 7d23db8

File tree

2 files changed

+29
-4
lines changed

2 files changed

+29
-4
lines changed

src/Symfony/Component/PasswordHasher/Hasher/UserPasswordHasher.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,12 @@ public function isPasswordValid($user, string $plainPassword): bool
6868
trigger_deprecation('symfony/password-hasher', '5.3', 'The "%s()" method expects a "%s" instance as first argument. Not implementing it in class "%s" is deprecated.', __METHOD__, PasswordAuthenticatedUserInterface::class, get_debug_type($user));
6969
}
7070

71-
$salt = $user->getSalt();
72-
if ($salt && !$user instanceof LegacyPasswordAuthenticatedUserInterface) {
73-
trigger_deprecation('symfony/password-hasher', '5.3', 'Returning a string from "getSalt()" without implementing the "%s" interface is deprecated, the "%s" class should implement it.', LegacyPasswordAuthenticatedUserInterface::class, get_debug_type($user));
71+
$salt = null;
72+
if (method_exists($user, 'getSalt')) {
73+
$salt = $user->getSalt();
74+
if ($salt && !$user instanceof LegacyPasswordAuthenticatedUserInterface) {
75+
trigger_deprecation('symfony/password-hasher', '5.3', 'Returning a string from "getSalt()" without implementing the "%s" interface is deprecated, the "%s" class should implement it.', LegacyPasswordAuthenticatedUserInterface::class, get_debug_type($user));
76+
}
7477
}
7578

7679
if (null === $user->getPassword()) {

src/Symfony/Component/PasswordHasher/Tests/Hasher/UserPasswordHasherTest.php

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ public function testHashWithPasswordAuthenticatedUser()
101101
$this->assertSame('hash', $hashedPassword);
102102
}
103103

104-
public function testVerify()
104+
public function testVerifyWithLegacyUser()
105105
{
106106
$user = new TestLegacyPasswordAuthenticatedUser('user', 'hash', 'userSalt');
107107

@@ -123,6 +123,28 @@ public function testVerify()
123123
$this->assertTrue($isValid);
124124
}
125125

126+
public function testVerify()
127+
{
128+
$user = new TestPasswordAuthenticatedUser('hash');
129+
130+
$mockHasher = $this->createMock(PasswordHasherInterface::class);
131+
$mockHasher->expects($this->any())
132+
->method('verify')
133+
->with($this->equalTo('hash'), $this->equalTo('plainPassword'), $this->equalTo(null))
134+
->willReturn(true);
135+
136+
$mockPasswordHasherFactory = $this->createMock(PasswordHasherFactoryInterface::class);
137+
$mockPasswordHasherFactory->expects($this->any())
138+
->method('getPasswordHasher')
139+
->with($user)
140+
->willReturn($mockHasher);
141+
142+
$passwordHasher = new UserPasswordHasher($mockPasswordHasherFactory);
143+
144+
$isValid = $passwordHasher->isPasswordValid($user, 'plainPassword');
145+
$this->assertTrue($isValid);
146+
}
147+
126148
public function testNeedsRehash()
127149
{
128150
$user = new InMemoryUser('username', null);

0 commit comments

Comments
 (0)
0