1111
1212namespace  Symfony \Bundle \SecurityBundle \Tests \Functional ;
1313
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+ 
1420class  CsrfFormLoginTest extends  AbstractWebTestCase
1521{
1622    /** 
@@ -20,6 +26,10 @@ public function testFormLoginAndLogoutWithCsrfTokens($options)
2026    {
2127        $ client  = $ this  ->createClient ($ options );
2228
29+         $ this  ->callInRequestContext ($ client , function  () {
30+             static ::getContainer ()->get ('security.csrf.token_storage ' )->setToken ('foo ' , 'bar ' );
31+         });
32+ 
2333        $ form  = $ client ->request ('GET ' , '/login ' )->selectButton ('login ' )->form ();
2434        $ form ['user_login[username] ' ] = 'johannes ' ;
2535        $ form ['user_login[password] ' ] = 'test ' ;
@@ -40,6 +50,10 @@ public function testFormLoginAndLogoutWithCsrfTokens($options)
4050        $ client ->click ($ logoutLinks [0 ]);
4151
4252        $ this  ->assertRedirect ($ client ->getResponse (), '/ ' );
53+ 
54+         $ this  ->callInRequestContext ($ client , function  () {
55+             $ this  ->assertFalse (static ::getContainer ()->get ('security.csrf.token_storage ' )->hasToken ('foo ' ));
56+         });
4357    }
4458
4559    /** 
@@ -49,6 +63,10 @@ public function testFormLoginWithInvalidCsrfToken($options)
4963    {
5064        $ client  = $ this  ->createClient ($ options );
5165
66+         $ this  ->callInRequestContext ($ client , function  () {
67+             static ::getContainer ()->get ('security.csrf.token_storage ' )->setToken ('foo ' , 'bar ' );
68+         });
69+ 
5270        $ form  = $ client ->request ('GET ' , '/login ' )->selectButton ('login ' )->form ();
5371        $ form ['user_login[_token] ' ] = '' ;
5472        $ client ->submit ($ form );
@@ -57,6 +75,10 @@ public function testFormLoginWithInvalidCsrfToken($options)
5775
5876        $ text  = $ client ->followRedirect ()->text (null , true );
5977        $ 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+         });
6082    }
6183
6284    /** 
@@ -202,4 +224,22 @@ public function provideLegacyClientOptions()
202224        yield  [['test_case '  => 'CsrfFormLogin ' , 'root_config '  => 'legacy_config.yml ' , 'enable_authenticator_manager '  => false ]];
203225        yield  [['test_case '  => 'CsrfFormLogin ' , 'root_config '  => 'legacy_routes_as_path.yml ' , 'enable_authenticator_manager '  => false ]];
204226    }
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+     }
205245}
0 commit comments