12
12
namespace Symfony \Component \Security \Http \Tests \Authentication ;
13
13
14
14
use PHPUnit \Framework \TestCase ;
15
+ use Symfony \Component \HttpFoundation \Request ;
15
16
use Symfony \Component \HttpFoundation \Response ;
16
17
use Symfony \Component \Security \Http \Authentication \DefaultAuthenticationSuccessHandler ;
17
18
18
19
class DefaultAuthenticationSuccessHandlerTest extends TestCase
19
20
{
20
21
private $ httpUtils = null ;
21
-
22
- private $ request = null ;
23
-
24
22
private $ token = null ;
25
23
26
24
protected function setUp ()
27
25
{
28
26
$ this ->httpUtils = $ this ->getMockBuilder ('Symfony\Component\Security\Http\HttpUtils ' )->getMock ();
29
- $ this ->request = $ this ->getMockBuilder ('Symfony\Component\HttpFoundation\Request ' )->getMock ();
30
- $ this ->request ->headers = $ this ->getMockBuilder ('Symfony\Component\HttpFoundation\HeaderBag ' )->getMock ();
31
27
$ this ->token = $ this ->getMockBuilder ('Symfony\Component\Security\Core\Authentication\Token\TokenInterface ' )->getMock ();
32
28
}
33
29
34
30
public function testRequestIsRedirected ()
35
31
{
36
- $ response = $ this ->expectRedirectResponse ('/ ' );
32
+ $ request = Request::create ('/ ' );
33
+ $ response = $ this ->expectRedirectResponse ($ request , '/ ' );
37
34
38
35
$ handler = new DefaultAuthenticationSuccessHandler ($ this ->httpUtils , array ());
39
- $ result = $ handler ->onAuthenticationSuccess ($ this -> request , $ this ->token );
36
+ $ result = $ handler ->onAuthenticationSuccess ($ request , $ this ->token );
40
37
41
38
$ this ->assertSame ($ response , $ result );
42
39
}
@@ -48,40 +45,34 @@ public function testDefaultTargetPathCanBeForced()
48
45
'default_target_path ' => '/dashboard ' ,
49
46
);
50
47
51
- $ response = $ this ->expectRedirectResponse ('/dashboard ' );
48
+ $ request = Request::create ('/ ' );
49
+ $ response = $ this ->expectRedirectResponse ($ request , '/dashboard ' );
52
50
53
51
$ handler = new DefaultAuthenticationSuccessHandler ($ this ->httpUtils , $ options );
54
- $ result = $ handler ->onAuthenticationSuccess ($ this -> request , $ this ->token );
52
+ $ result = $ handler ->onAuthenticationSuccess ($ request , $ this ->token );
55
53
56
54
$ this ->assertSame ($ response , $ result );
57
55
}
58
56
59
57
public function testTargetPathIsPassedWithRequest ()
60
58
{
61
- $ this ->request ->expects ($ this ->once ())
62
- ->method ('get ' )->with ('_target_path ' )
63
- ->will ($ this ->returnValue ('/dashboard ' ));
64
-
65
- $ response = $ this ->expectRedirectResponse ('/dashboard ' );
59
+ $ request = Request::create ('/?_target_path=/dashboard ' );
60
+ $ response = $ this ->expectRedirectResponse ($ request , '/dashboard ' );
66
61
67
62
$ handler = new DefaultAuthenticationSuccessHandler ($ this ->httpUtils , array ());
68
- $ result = $ handler ->onAuthenticationSuccess ($ this -> request , $ this ->token );
63
+ $ result = $ handler ->onAuthenticationSuccess ($ request , $ this ->token );
69
64
70
65
$ this ->assertSame ($ response , $ result );
71
66
}
72
67
73
68
public function testTargetPathParameterIsCustomised ()
74
69
{
75
70
$ options = array ('target_path_parameter ' => '_my_target_path ' );
76
-
77
- $ this ->request ->expects ($ this ->once ())
78
- ->method ('get ' )->with ('_my_target_path ' )
79
- ->will ($ this ->returnValue ('/dashboard ' ));
80
-
81
- $ response = $ this ->expectRedirectResponse ('/dashboard ' );
71
+ $ request = Request::create ('/?_my_target_path=/dashboard ' );
72
+ $ response = $ this ->expectRedirectResponse ($ request , '/dashboard ' );
82
73
83
74
$ handler = new DefaultAuthenticationSuccessHandler ($ this ->httpUtils , $ options );
84
- $ result = $ handler ->onAuthenticationSuccess ($ this -> request , $ this ->token );
75
+ $ result = $ handler ->onAuthenticationSuccess ($ request , $ this ->token );
85
76
86
77
$ this ->assertSame ($ response , $ result );
87
78
}
@@ -95,74 +86,65 @@ public function testTargetPathIsTakenFromTheSession()
95
86
$ session ->expects ($ this ->once ())
96
87
->method ('remove ' )->with ('_security.admin.target_path ' );
97
88
98
- $ this ->request ->expects ($ this ->any ())
99
- ->method ('getSession ' )
100
- ->will ($ this ->returnValue ($ session ));
101
-
102
- $ response = $ this ->expectRedirectResponse ('/admin/dashboard ' );
89
+ $ request = Request::create ('/?_my_target_path=/dashboard ' );
90
+ $ request ->setSession ($ session );
91
+ $ response = $ this ->expectRedirectResponse ($ request , '/admin/dashboard ' );
103
92
104
93
$ handler = new DefaultAuthenticationSuccessHandler ($ this ->httpUtils , array ());
105
94
$ handler ->setProviderKey ('admin ' );
106
95
107
- $ result = $ handler ->onAuthenticationSuccess ($ this -> request , $ this ->token );
96
+ $ result = $ handler ->onAuthenticationSuccess ($ request , $ this ->token );
108
97
109
98
$ this ->assertSame ($ response , $ result );
110
99
}
111
100
112
101
public function testTargetPathIsPassedAsReferer ()
113
102
{
114
103
$ options = array ('use_referer ' => true );
115
-
116
- $ this ->request ->headers ->expects ($ this ->once ())
117
- ->method ('get ' )->with ('Referer ' )
118
- ->will ($ this ->returnValue ('/dashboard ' ));
119
-
120
- $ response = $ this ->expectRedirectResponse ('/dashboard ' );
104
+ $ request = Request::create ('/ ' );
105
+ $ request ->headers ->set ('Referer ' , '/dashboard ' );
106
+ $ response = $ this ->expectRedirectResponse ($ request , '/dashboard ' );
121
107
122
108
$ handler = new DefaultAuthenticationSuccessHandler ($ this ->httpUtils , $ options );
123
- $ result = $ handler ->onAuthenticationSuccess ($ this -> request , $ this ->token );
109
+ $ result = $ handler ->onAuthenticationSuccess ($ request , $ this ->token );
124
110
125
111
$ this ->assertSame ($ response , $ result );
126
112
}
127
113
128
114
public function testRefererHasToBeDifferentThatLoginUrl ()
129
115
{
130
116
$ options = array ('use_referer ' => true );
131
-
132
- $ this ->request ->headers ->expects ($ this ->any ())
133
- ->method ('get ' )->with ('Referer ' )
134
- ->will ($ this ->returnValue ('/login ' ));
135
-
117
+ $ request = Request::create ('/ ' );
118
+ $ request ->headers ->set ('Referer ' , '/login ' );
136
119
$ this ->httpUtils ->expects ($ this ->once ())
137
- ->method ('generateUri ' )->with ($ this -> request , '/login ' )
120
+ ->method ('generateUri ' )->with ($ request , '/login ' )
138
121
->will ($ this ->returnValue ('/login ' ));
139
122
140
- $ response = $ this ->expectRedirectResponse ('/ ' );
123
+ $ response = $ this ->expectRedirectResponse ($ request , '/ ' );
141
124
142
125
$ handler = new DefaultAuthenticationSuccessHandler ($ this ->httpUtils , $ options );
143
- $ result = $ handler ->onAuthenticationSuccess ($ this -> request , $ this ->token );
126
+ $ result = $ handler ->onAuthenticationSuccess ($ request , $ this ->token );
144
127
145
128
$ this ->assertSame ($ response , $ result );
146
129
}
147
130
148
131
public function testRefererTargetPathIsIgnoredByDefault ()
149
132
{
150
- $ this ->request ->headers ->expects ($ this ->never ())->method ('get ' );
151
-
152
- $ response = $ this ->expectRedirectResponse ('/ ' );
133
+ $ request = Request::create ('/ ' );
134
+ $ response = $ this ->expectRedirectResponse ($ request , '/ ' );
153
135
154
136
$ handler = new DefaultAuthenticationSuccessHandler ($ this ->httpUtils , array ());
155
- $ result = $ handler ->onAuthenticationSuccess ($ this -> request , $ this ->token );
137
+ $ result = $ handler ->onAuthenticationSuccess ($ request , $ this ->token );
156
138
157
139
$ this ->assertSame ($ response , $ result );
158
140
}
159
141
160
- private function expectRedirectResponse ($ path )
142
+ private function expectRedirectResponse (Request $ request , $ path )
161
143
{
162
144
$ response = new Response ();
163
145
$ this ->httpUtils ->expects ($ this ->once ())
164
146
->method ('createRedirectResponse ' )
165
- ->with ($ this -> request , $ path )
147
+ ->with ($ request , $ path )
166
148
->will ($ this ->returnValue ($ response ));
167
149
168
150
return $ response ;
0 commit comments