1818use Symfony \Component \Security \Core \Encoder \PasswordEncoderInterface ;
1919use Symfony \Component \Security \Core \User \PasswordUpgraderInterface ;
2020use Symfony \Component \Security \Core \User \UserInterface ;
21+ use Symfony \Component \Security \Core \User \UserProviderInterface ;
2122use Symfony \Component \Security \Http \Authenticator \AuthenticatorInterface ;
2223use Symfony \Component \Security \Http \Authenticator \Passport \Badge \PasswordUpgradeBadge ;
2324use Symfony \Component \Security \Http \Authenticator \Passport \Badge \UserBadge ;
@@ -34,9 +35,14 @@ class PasswordMigratingListenerTest extends TestCase
3435
3536 protected function setUp (): void
3637 {
38+ $ this ->user = $ this ->createMock (UserInterface::class);
39+ $ this ->user ->expects ($ this ->any ())->method ('getPassword ' )->willReturn ('old-encoded-password ' );
40+ $ encoder = $ this ->createMock (PasswordEncoderInterface::class);
41+ $ encoder ->expects ($ this ->any ())->method ('needsRehash ' )->willReturn (true );
42+ $ encoder ->expects ($ this ->any ())->method ('encodePassword ' )->with ('pa$$word ' , null )->willReturn ('new-encoded-password ' );
3743 $ this ->encoderFactory = $ this ->createMock (EncoderFactoryInterface::class);
44+ $ this ->encoderFactory ->expects ($ this ->any ())->method ('getEncoder ' )->with ($ this ->user )->willReturn ($ encoder );
3845 $ this ->listener = new PasswordMigratingListener ($ this ->encoderFactory );
39- $ this ->user = $ this ->createMock (UserInterface::class);
4046 }
4147
4248 /**
@@ -61,16 +67,8 @@ public function provideUnsupportedEvents()
6167 yield [$ this ->createEvent ($ this ->createMock (PassportInterface::class))];
6268 }
6369
64- public function testUpgrade ()
70+ public function testUpgradeWithUpgrader ()
6571 {
66- $ encoder = $ this ->createMock (PasswordEncoderInterface::class);
67- $ encoder ->expects ($ this ->any ())->method ('needsRehash ' )->willReturn (true );
68- $ encoder ->expects ($ this ->any ())->method ('encodePassword ' )->with ('pa$$word ' , null )->willReturn ('new-encoded-password ' );
69-
70- $ this ->encoderFactory ->expects ($ this ->any ())->method ('getEncoder ' )->with ($ this ->user )->willReturn ($ encoder );
71-
72- $ this ->user ->expects ($ this ->any ())->method ('getPassword ' )->willReturn ('old-encoded-password ' );
73-
7472 $ passwordUpgrader = $ this ->createPasswordUpgrader ();
7573 $ passwordUpgrader ->expects ($ this ->once ())
7674 ->method ('upgradePassword ' )
@@ -81,6 +79,20 @@ public function testUpgrade()
8179 $ this ->listener ->onLoginSuccess ($ event );
8280 }
8381
82+ public function testUpgradeWithoutUpgrader ()
83+ {
84+ $ userLoader = $ this ->createMock (MigratingUserProvider::class);
85+ $ userLoader ->expects ($ this ->any ())->method ('loadUserByUsername ' )->willReturn ($ this ->user );
86+
87+ $ userLoader ->expects ($ this ->once ())
88+ ->method ('upgradePassword ' )
89+ ->with ($ this ->user , 'new-encoded-password ' )
90+ ;
91+
92+ $ event = $ this ->createEvent (new SelfValidatingPassport (new UserBadge ('test ' , [$ userLoader , 'loadUserByUsername ' ]), [new PasswordUpgradeBadge ('pa$$word ' )]));
93+ $ this ->listener ->onLoginSuccess ($ event );
94+ }
95+
8496 private function createPasswordUpgrader ()
8597 {
8698 return $ this ->createMock (PasswordUpgraderInterface::class);
@@ -91,3 +103,7 @@ private function createEvent(PassportInterface $passport)
91103 return new LoginSuccessEvent ($ this ->createMock (AuthenticatorInterface::class), $ passport , $ this ->createMock (TokenInterface::class), new Request (), null , 'main ' );
92104 }
93105}
106+
107+ abstract class MigratingUserProvider implements UserProviderInterface, PasswordUpgraderInterface
108+ {
109+ }
0 commit comments