13
13
14
14
use Symfony \Component \Security \Core \Authentication \Provider \UserAuthenticationProvider ;
15
15
use Symfony \Component \Security \Core \Role \Role ;
16
+ use Symfony \Component \Security \Core \Role \SwitchUserRole ;
16
17
use Symfony \Component \Security \Core \Exception \BadCredentialsException ;
17
18
18
19
class UserAuthenticationProviderTest extends \PHPUnit_Framework_TestCase
@@ -172,6 +173,11 @@ public function testAuthenticate()
172
173
->will ($ this ->returnValue ('foo ' ))
173
174
;
174
175
176
+ $ token ->expects ($ this ->once ())
177
+ ->method ('getRoles ' )
178
+ ->will ($ this ->returnValue (array ()))
179
+ ;
180
+
175
181
$ authToken = $ provider ->authenticate ($ token );
176
182
177
183
$ this ->assertInstanceOf ('Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken ' , $ authToken );
@@ -181,9 +187,45 @@ public function testAuthenticate()
181
187
$ this ->assertEquals (array ('foo ' => 'bar ' ), $ authToken ->getAttributes (), '->authenticate() copies token attributes ' );
182
188
}
183
189
190
+ public function testAuthenticateWithPreservingRoleSwitchUserRole ()
191
+ {
192
+ $ user = $ this ->getMock ('Symfony\Component\Security\Core\User\UserInterface ' );
193
+ $ user ->expects ($ this ->once ())
194
+ ->method ('getRoles ' )
195
+ ->will ($ this ->returnValue (array ('ROLE_FOO ' )))
196
+ ;
197
+
198
+ $ provider = $ this ->getProvider ();
199
+ $ provider ->expects ($ this ->once ())
200
+ ->method ('retrieveUser ' )
201
+ ->will ($ this ->returnValue ($ user ))
202
+ ;
203
+
204
+ $ token = $ this ->getSupportedToken ();
205
+ $ token ->expects ($ this ->once ())
206
+ ->method ('getCredentials ' )
207
+ ->will ($ this ->returnValue ('foo ' ))
208
+ ;
209
+
210
+ $ switchUserRole = new SwitchUserRole ('foo ' , $ this ->getMock ('Symfony\Component\Security\Core\Authentication\Token\TokenInterface ' ));
211
+ $ token ->expects ($ this ->once ())
212
+ ->method ('getRoles ' )
213
+ ->will ($ this ->returnValue (array ($ switchUserRole )))
214
+ ;
215
+
216
+ $ authToken = $ provider ->authenticate ($ token );
217
+
218
+ $ this ->assertInstanceOf ('Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken ' , $ authToken );
219
+ $ this ->assertSame ($ user , $ authToken ->getUser ());
220
+ $ this ->assertContains (new Role ('ROLE_FOO ' ), $ authToken ->getRoles (), '' , false , false );
221
+ $ this ->assertContains ($ switchUserRole , $ authToken ->getRoles ());
222
+ $ this ->assertEquals ('foo ' , $ authToken ->getCredentials ());
223
+ $ this ->assertEquals (array ('foo ' => 'bar ' ), $ authToken ->getAttributes (), '->authenticate() copies token attributes ' );
224
+ }
225
+
184
226
protected function getSupportedToken ()
185
227
{
186
- $ mock = $ this ->getMock ('Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken ' , array ('getCredentials ' , 'getProviderKey ' ), array (), '' , false );
228
+ $ mock = $ this ->getMock ('Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken ' , array ('getCredentials ' , 'getProviderKey ' , ' getRoles ' ), array (), '' , false );
187
229
$ mock
188
230
->expects ($ this ->any ())
189
231
->method ('getProviderKey ' )
0 commit comments