11
11
12
12
namespace Symfony \Bundle \SecurityBundle \Tests \Functional ;
13
13
14
+ use Symfony \Bundle \FrameworkBundle \KernelBrowser ;
15
+ use Symfony \Component \EventDispatcher \EventDispatcherInterface ;
16
+ use Symfony \Component \HttpFoundation \Response ;
17
+ use Symfony \Component \HttpKernel \Event \RequestEvent ;
18
+ use Symfony \Component \HttpKernel \KernelEvents ;
19
+
14
20
class CsrfFormLoginTest extends AbstractWebTestCase
15
21
{
16
22
/**
@@ -20,6 +26,10 @@ public function testFormLoginAndLogoutWithCsrfTokens($options)
20
26
{
21
27
$ client = $ this ->createClient ($ options );
22
28
29
+ $ this ->callInRequestContext ($ client , function () {
30
+ static ::getContainer ()->get ('security.csrf.token_storage ' )->setToken ('foo ' , 'bar ' );
31
+ });
32
+
23
33
$ form = $ client ->request ('GET ' , '/login ' )->selectButton ('login ' )->form ();
24
34
$ form ['user_login[username] ' ] = 'johannes ' ;
25
35
$ form ['user_login[password] ' ] = 'test ' ;
@@ -40,6 +50,10 @@ public function testFormLoginAndLogoutWithCsrfTokens($options)
40
50
$ client ->click ($ logoutLinks [0 ]);
41
51
42
52
$ this ->assertRedirect ($ client ->getResponse (), '/ ' );
53
+
54
+ $ this ->callInRequestContext ($ client , function () {
55
+ $ this ->assertFalse (static ::getContainer ()->get ('security.csrf.token_storage ' )->hasToken ('foo ' ));
56
+ });
43
57
}
44
58
45
59
/**
@@ -49,6 +63,10 @@ public function testFormLoginWithInvalidCsrfToken($options)
49
63
{
50
64
$ client = $ this ->createClient ($ options );
51
65
66
+ $ this ->callInRequestContext ($ client , function () {
67
+ static ::getContainer ()->get ('security.csrf.token_storage ' )->setToken ('foo ' , 'bar ' );
68
+ });
69
+
52
70
$ form = $ client ->request ('GET ' , '/login ' )->selectButton ('login ' )->form ();
53
71
$ form ['user_login[_token] ' ] = '' ;
54
72
$ client ->submit ($ form );
@@ -57,6 +75,10 @@ public function testFormLoginWithInvalidCsrfToken($options)
57
75
58
76
$ text = $ client ->followRedirect ()->text (null , true );
59
77
$ this ->assertStringContainsString ('Invalid CSRF token. ' , $ text );
78
+
79
+ $ this ->callInRequestContext ($ client , function () {
80
+ $ this ->assertTrue (static ::getContainer ()->get ('security.csrf.token_storage ' )->hasToken ('foo ' ));
81
+ });
60
82
}
61
83
62
84
/**
@@ -202,4 +224,22 @@ public function provideLegacyClientOptions()
202
224
yield [['test_case ' => 'CsrfFormLogin ' , 'root_config ' => 'legacy_config.yml ' , 'enable_authenticator_manager ' => false ]];
203
225
yield [['test_case ' => 'CsrfFormLogin ' , 'root_config ' => 'legacy_routes_as_path.yml ' , 'enable_authenticator_manager ' => false ]];
204
226
}
227
+
228
+ private function callInRequestContext (KernelBrowser $ client , callable $ callable ): void
229
+ {
230
+ /** @var EventDispatcherInterface $eventDispatcher */
231
+ $ eventDispatcher = static ::getContainer ()->get (EventDispatcherInterface::class);
232
+ $ wrappedCallable = function (RequestEvent $ event ) use (&$ callable ) {
233
+ $ callable ();
234
+ $ event ->setResponse (new Response ('' ));
235
+ $ event ->stopPropagation ();
236
+ };
237
+
238
+ $ eventDispatcher ->addListener (KernelEvents::REQUEST , $ wrappedCallable );
239
+ try {
240
+ $ client ->request ('GET ' , '/ ' .uniqid ('' , true ));
241
+ } finally {
242
+ $ eventDispatcher ->removeListener (KernelEvents::REQUEST , $ wrappedCallable );
243
+ }
244
+ }
205
245
}
0 commit comments