18
18
use Symfony \Component \Security \Core \Encoder \PasswordEncoderInterface ;
19
19
use Symfony \Component \Security \Core \User \PasswordUpgraderInterface ;
20
20
use Symfony \Component \Security \Core \User \UserInterface ;
21
+ use Symfony \Component \Security \Core \User \UserProviderInterface ;
21
22
use Symfony \Component \Security \Http \Authenticator \AuthenticatorInterface ;
22
23
use Symfony \Component \Security \Http \Authenticator \Passport \Badge \PasswordUpgradeBadge ;
23
24
use Symfony \Component \Security \Http \Authenticator \Passport \Badge \UserBadge ;
@@ -34,9 +35,14 @@ class PasswordMigratingListenerTest extends TestCase
34
35
35
36
protected function setUp (): void
36
37
{
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 ' );
37
43
$ this ->encoderFactory = $ this ->createMock (EncoderFactoryInterface::class);
44
+ $ this ->encoderFactory ->expects ($ this ->any ())->method ('getEncoder ' )->with ($ this ->user )->willReturn ($ encoder );
38
45
$ this ->listener = new PasswordMigratingListener ($ this ->encoderFactory );
39
- $ this ->user = $ this ->createMock (UserInterface::class);
40
46
}
41
47
42
48
/**
@@ -61,16 +67,8 @@ public function provideUnsupportedEvents()
61
67
yield [$ this ->createEvent ($ this ->createMock (PassportInterface::class))];
62
68
}
63
69
64
- public function testUpgrade ()
70
+ public function testUpgradeWithUpgrader ()
65
71
{
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
-
74
72
$ passwordUpgrader = $ this ->createPasswordUpgrader ();
75
73
$ passwordUpgrader ->expects ($ this ->once ())
76
74
->method ('upgradePassword ' )
@@ -81,6 +79,20 @@ public function testUpgrade()
81
79
$ this ->listener ->onLoginSuccess ($ event );
82
80
}
83
81
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
+
84
96
private function createPasswordUpgrader ()
85
97
{
86
98
return $ this ->createMock (PasswordUpgraderInterface::class);
@@ -91,3 +103,22 @@ private function createEvent(PassportInterface $passport)
91
103
return new LoginSuccessEvent ($ this ->createMock (AuthenticatorInterface::class), $ passport , $ this ->createMock (TokenInterface::class), new Request (), null , 'main ' );
92
104
}
93
105
}
106
+
107
+ class MigratingUserProvider implements UserProviderInterface, PasswordUpgraderInterface
108
+ {
109
+ public function upgradePassword (UserInterface $ user , string $ newEncodedPassword ): void
110
+ {
111
+ }
112
+
113
+ public function loadUserByUsername (string $ username )
114
+ {
115
+ }
116
+
117
+ public function refreshUser (UserInterface $ user )
118
+ {
119
+ }
120
+
121
+ public function supportsClass (string $ class )
122
+ {
123
+ }
124
+ }
0 commit comments