-
-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Deprecate user plainPassword in favor of using UserManipulator
- Loading branch information
Showing
7 changed files
with
129 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,6 +21,7 @@ | |
use PHPUnit\Framework\MockObject\MockObject; | ||
use PHPUnit\Framework\TestCase; | ||
use Symfony\Component\HttpFoundation\RequestStack; | ||
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface; | ||
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface; | ||
|
||
/** | ||
|
@@ -61,6 +62,43 @@ public function testCreate(): void | |
self::assertFalse($user->isSuperAdmin()); | ||
} | ||
|
||
public function testCreateWithHasher(): void | ||
{ | ||
$userManagerMock = $this->getMockBuilder(UserManager::class)->getMock(); | ||
$user = new TestUser(); | ||
|
||
$username = 'test_username'; | ||
$password = 'test_password'; | ||
$email = '[email protected]'; | ||
|
||
$userManagerMock->expects(self::once()) | ||
->method('createUser') | ||
->willReturn($user) | ||
; | ||
|
||
$userManagerMock->expects(self::once()) | ||
->method('updateUser') | ||
->with(self::isInstanceOf(TestUser::class)) | ||
; | ||
|
||
$eventDispatcherMock = $this->getEventDispatcherMock(NucleosUserEvents::USER_CREATED, true); | ||
|
||
$requestStackMock = $this->getRequestStackMock(true); | ||
|
||
$hasher = $this->createMock(UserPasswordHasherInterface::class); | ||
$hasher->method('hashPassword')->with($user, $password)->willReturn('hashed_password'); | ||
|
||
$manipulator = new SimpleUserManipulator($userManagerMock, $eventDispatcherMock, $requestStackMock, $hasher); | ||
$manipulator->create($username, $password, $email, true, false); | ||
|
||
self::assertSame($username, $user->getUsername()); | ||
self::assertSame($password, $user->getPlainPassword()); | ||
self::assertSame('hashed_password', $user->getPassword()); | ||
self::assertSame($email, $user->getEmail()); | ||
self::assertTrue($user->isEnabled()); | ||
self::assertFalse($user->isSuperAdmin()); | ||
} | ||
|
||
public function testActivateWithValidUsername(): void | ||
{ | ||
$userManagerMock = $this->getMockBuilder(UserManager::class)->getMock(); | ||
|
@@ -319,6 +357,44 @@ public function testChangePasswordWithValidUsername(): void | |
self::assertSame($password, $user->getPlainPassword()); | ||
} | ||
|
||
public function testChangePasswordWithValidUsernameAndHasher(): void | ||
{ | ||
$userManagerMock = $this->getMockBuilder(UserManager::class)->getMock(); | ||
|
||
$user = new TestUser(); | ||
$username = 'test_username'; | ||
$password = 'test_password'; | ||
$oldpassword = 'old_password'; | ||
|
||
$user->setUsername($username); | ||
$user->setPlainPassword($oldpassword); | ||
|
||
$userManagerMock->expects(self::once()) | ||
->method('findUserByUsername') | ||
->willReturn($user) | ||
->with(self::equalTo($username)) | ||
; | ||
|
||
$userManagerMock->expects(self::once()) | ||
->method('updateUser') | ||
->with(self::isInstanceOf(TestUser::class)) | ||
; | ||
|
||
$eventDispatcherMock = $this->getEventDispatcherMock(NucleosUserEvents::USER_PASSWORD_CHANGED, true); | ||
|
||
$requestStackMock = $this->getRequestStackMock(true); | ||
|
||
$hasher = $this->createMock(UserPasswordHasherInterface::class); | ||
$hasher->method('hashPassword')->with($user, $password)->willReturn('hashed_password'); | ||
|
||
$manipulator = new SimpleUserManipulator($userManagerMock, $eventDispatcherMock, $requestStackMock, $hasher); | ||
$manipulator->changePassword($username, $password); | ||
|
||
self::assertSame($username, $user->getUsername()); | ||
self::assertSame($password, $user->getPlainPassword()); | ||
self::assertSame('hashed_password', $user->getPassword()); | ||
} | ||
|
||
public function testChangePasswordWithInvalidUsername(): void | ||
{ | ||
$this->expectException(InvalidArgumentException::class); | ||
|