@@ -34,6 +34,7 @@ protected function setUp()
34
34
$ this ->userChecker = $ this ->getMock ('Symfony\Component\Security\Core\User\UserCheckerInterface ' );
35
35
$ this ->accessDecisionManager = $ this ->getMock ('Symfony\Component\Security\Core\Authorization\AccessDecisionManagerInterface ' );
36
36
$ this ->request = $ this ->getMock ('Symfony\Component\HttpFoundation\Request ' );
37
+ $ this ->request ->query = $ this ->getMock ('Symfony\Component\HttpFoundation\ParameterBag ' );
37
38
$ this ->request ->server = $ this ->getMock ('Symfony\Component\HttpFoundation\ServerBag ' );
38
39
$ this ->event = $ this ->getEvent ($ this ->request );
39
40
}
@@ -86,6 +87,8 @@ public function testExitUserUpdatesToken()
86
87
87
88
$ this ->request ->expects ($ this ->any ())->method ('get ' )->with ('_switch_user ' )->will ($ this ->returnValue ('_exit ' ));
88
89
$ this ->request ->expects ($ this ->any ())->method ('getUri ' )->will ($ this ->returnValue ('/ ' ));
90
+ $ this ->request ->query ->expects ($ this ->once ())->method ('remove ' ,'_switch_user ' );
91
+ $ this ->request ->query ->expects ($ this ->any ())->method ('all ' )->will ($ this ->returnValue (array ()));
89
92
$ this ->request ->server ->expects ($ this ->once ())->method ('set ' )->with ('QUERY_STRING ' , '' );
90
93
91
94
$ this ->securityContext ->expects ($ this ->once ())
@@ -123,6 +126,9 @@ public function testSwitchUser()
123
126
124
127
$ this ->securityContext ->expects ($ this ->any ())->method ('getToken ' )->will ($ this ->returnValue ($ token ));
125
128
$ this ->request ->expects ($ this ->any ())->method ('get ' )->with ('_switch_user ' )->will ($ this ->returnValue ('kuba ' ));
129
+ $ this ->request ->query ->expects ($ this ->once ())->method ('remove ' ,'_switch_user ' );
130
+ $ this ->request ->query ->expects ($ this ->any ())->method ('all ' )->will ($ this ->returnValue (array ()));
131
+
126
132
$ this ->request ->expects ($ this ->any ())->method ('getUri ' )->will ($ this ->returnValue ('/ ' ));
127
133
$ this ->request ->server ->expects ($ this ->once ())->method ('set ' )->with ('QUERY_STRING ' , '' );
128
134
@@ -142,6 +148,35 @@ public function testSwitchUser()
142
148
$ listener ->handle ($ this ->event );
143
149
}
144
150
151
+ public function testSwitchUserKeepsOtherQueryStringParameters ()
152
+ {
153
+ $ token = $ this ->getToken (array ($ this ->getMock ('Symfony\Component\Security\Core\Role\RoleInterface ' )));
154
+ $ user = $ this ->getMock ('Symfony\Component\Security\Core\User\UserInterface ' );
155
+ $ user ->expects ($ this ->any ())->method ('getRoles ' )->will ($ this ->returnValue (array ()));
156
+
157
+ $ this ->securityContext ->expects ($ this ->any ())->method ('getToken ' )->will ($ this ->returnValue ($ token ));
158
+ $ this ->request ->expects ($ this ->any ())->method ('get ' )->with ('_switch_user ' )->will ($ this ->returnValue ('kuba ' ));
159
+ $ this ->request ->query ->expects ($ this ->once ())->method ('remove ' ,'_switch_user ' );
160
+ $ this ->request ->query ->expects ($ this ->any ())->method ('all ' )->will ($ this ->returnValue (array ('page ' =>3 ,'section ' =>2 )));
161
+ $ this ->request ->expects ($ this ->any ())->method ('getUri ' )->will ($ this ->returnValue ('/ ' ));
162
+ $ this ->request ->server ->expects ($ this ->once ())->method ('set ' )->with ('QUERY_STRING ' , 'page=3§ion=2 ' );
163
+
164
+ $ this ->accessDecisionManager ->expects ($ this ->once ())
165
+ ->method ('decide ' )->with ($ token , array ('ROLE_ALLOWED_TO_SWITCH ' ))
166
+ ->will ($ this ->returnValue (true ));
167
+
168
+ $ this ->userProvider ->expects ($ this ->once ())
169
+ ->method ('loadUserByUsername ' )->with ('kuba ' )
170
+ ->will ($ this ->returnValue ($ user ));
171
+ $ this ->userChecker ->expects ($ this ->once ())
172
+ ->method ('checkPostAuth ' )->with ($ user );
173
+ $ this ->securityContext ->expects ($ this ->once ())
174
+ ->method ('setToken ' )->with ($ this ->isInstanceOf ('Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken ' ));
175
+
176
+ $ listener = new SwitchUserListener ($ this ->securityContext , $ this ->userProvider , $ this ->userChecker , 'provider123 ' , $ this ->accessDecisionManager );
177
+ $ listener ->handle ($ this ->event );
178
+ }
179
+
145
180
private function getEvent ($ request )
146
181
{
147
182
$ event = $ this ->getMockBuilder ('Symfony\Component\HttpKernel\Event\GetResponseEvent ' )
0 commit comments