1111
1212namespace Symfony \Component \Security \Http \Tests \EventListener ;
1313
14+ use PHPUnit \Framework \Attributes \DataProvider ;
1415use PHPUnit \Framework \TestCase ;
1516use Symfony \Component \ExpressionLanguage \Expression ;
1617use Symfony \Component \ExpressionLanguage \ExpressionLanguage ;
@@ -90,7 +91,7 @@ public function testIsCsrfTokenValidCalledCorrectly()
9091
9192 public function testIsCsrfTokenValidCalledCorrectlyInPayload ()
9293 {
93- $ request = new Request (server: ['headers ' => [ ' content-type ' => ' application/json '] ], content: json_encode (['_token ' => 'bar ' ]));
94+ $ request = new Request (server: ['CONTENT_TYPE ' => ' application/json ' ], content: json_encode (['_token ' => 'bar ' ]));
9495
9596 $ csrfTokenManager = $ this ->createMock (CsrfTokenManagerInterface::class);
9697 $ csrfTokenManager ->expects ($ this ->once ())
@@ -163,15 +164,15 @@ public function testIsCsrfTokenValidCalledCorrectlyWithCustomTokenKey()
163164 $ listener ->onKernelControllerArguments ($ event );
164165 }
165166
166- public function testIsCsrfTokenValidCalledCorrectlyWithInvalidTokenKey ()
167+ public function testIsCsrfTokenValidThrowExceptionWhenInvalidMatchingToken ()
167168 {
169+ $ this ->expectException (InvalidCsrfTokenException::class);
170+
168171 $ request = new Request (request: ['_token ' => 'bar ' ]);
169172
170173 $ csrfTokenManager = $ this ->createMock (CsrfTokenManagerInterface::class);
171- $ csrfTokenManager ->expects ($ this ->once ())
172- ->method ('isTokenValid ' )
173- ->with (new CsrfToken ('foo ' , '' ))
174- ->willReturn (true );
174+ $ csrfTokenManager ->expects ($ this ->never ())
175+ ->method ('isTokenValid ' );
175176
176177 $ event = new ControllerArgumentsEvent (
177178 $ this ->createMock (HttpKernelInterface::class),
@@ -185,15 +186,13 @@ public function testIsCsrfTokenValidCalledCorrectlyWithInvalidTokenKey()
185186 $ listener ->onKernelControllerArguments ($ event );
186187 }
187188
188- public function testExceptionWhenInvalidToken ()
189+ public function testIsCsrfTokenValidThrowExceptionWhenMissingRequestToken ()
189190 {
190191 $ this ->expectException (InvalidCsrfTokenException::class);
191192
192193 $ csrfTokenManager = $ this ->createMock (CsrfTokenManagerInterface::class);
193- $ csrfTokenManager ->expects ($ this ->once ())
194- ->method ('isTokenValid ' )
195- ->withAnyParameters ()
196- ->willReturn (false );
194+ $ csrfTokenManager ->expects ($ this ->never ())
195+ ->method ('isTokenValid ' );
197196
198197 $ event = new ControllerArgumentsEvent (
199198 $ this ->createMock (HttpKernelInterface::class),
@@ -237,8 +236,7 @@ public function testIsCsrfTokenValidIgnoredWithNonMatchingMethod()
237236
238237 $ csrfTokenManager = $ this ->createMock (CsrfTokenManagerInterface::class);
239238 $ csrfTokenManager ->expects ($ this ->never ())
240- ->method ('isTokenValid ' )
241- ->with (new CsrfToken ('foo ' , 'bar ' ));
239+ ->method ('isTokenValid ' );
242240
243241 $ event = new ControllerArgumentsEvent (
244242 $ this ->createMock (HttpKernelInterface::class),
@@ -275,15 +273,14 @@ public function testIsCsrfTokenValidCalledCorrectlyWithGetOrPostMethodWithGetMet
275273 $ listener ->onKernelControllerArguments ($ event );
276274 }
277275
278- public function testIsCsrfTokenValidNoIgnoredWithGetOrPostMethodWithPutMethod ()
276+ public function testIsCsrfTokenValidIgnoredWithGetOrPostMethodWithPutMethod ()
279277 {
280278 $ request = new Request (request: ['_token ' => 'bar ' ]);
281279 $ request ->setMethod ('PUT ' );
282280
283281 $ csrfTokenManager = $ this ->createMock (CsrfTokenManagerInterface::class);
284282 $ csrfTokenManager ->expects ($ this ->never ())
285- ->method ('isTokenValid ' )
286- ->with (new CsrfToken ('foo ' , 'bar ' ));
283+ ->method ('isTokenValid ' );
287284
288285 $ event = new ControllerArgumentsEvent (
289286 $ this ->createMock (HttpKernelInterface::class),
@@ -297,18 +294,16 @@ public function testIsCsrfTokenValidNoIgnoredWithGetOrPostMethodWithPutMethod()
297294 $ listener ->onKernelControllerArguments ($ event );
298295 }
299296
300- public function testIsCsrfTokenValidCalledCorrectlyWithInvalidTokenKeyAndPostMethod ()
297+ public function testIsCsrfTokenValidThrowExceptionWithInvalidTokenKeyAndPostMethod ()
301298 {
302299 $ this ->expectException (InvalidCsrfTokenException::class);
303300
304301 $ request = new Request (request: ['_token ' => 'bar ' ]);
305302 $ request ->setMethod ('POST ' );
306303
307304 $ csrfTokenManager = $ this ->createMock (CsrfTokenManagerInterface::class);
308- $ csrfTokenManager ->expects ($ this ->once ())
309- ->method ('isTokenValid ' )
310- ->withAnyParameters ()
311- ->willReturn (false );
305+ $ csrfTokenManager ->expects ($ this ->never ())
306+ ->method ('isTokenValid ' );
312307
313308 $ event = new ControllerArgumentsEvent (
314309 $ this ->createMock (HttpKernelInterface::class),
@@ -329,8 +324,7 @@ public function testIsCsrfTokenValidIgnoredWithInvalidTokenKeyAndUnavailableMeth
329324
330325 $ csrfTokenManager = $ this ->createMock (CsrfTokenManagerInterface::class);
331326 $ csrfTokenManager ->expects ($ this ->never ())
332- ->method ('isTokenValid ' )
333- ->withAnyParameters ();
327+ ->method ('isTokenValid ' );
334328
335329 $ event = new ControllerArgumentsEvent (
336330 $ this ->createMock (HttpKernelInterface::class),
@@ -343,4 +337,63 @@ public function testIsCsrfTokenValidIgnoredWithInvalidTokenKeyAndUnavailableMeth
343337 $ listener = new IsCsrfTokenValidAttributeListener ($ csrfTokenManager );
344338 $ listener ->onKernelControllerArguments ($ event );
345339 }
340+
341+ #[DataProvider('provideTokenSourceScenarios ' )]
342+ public function testIsCsrfTokenValidCalledCorrectlyWithCustomTokenSource (Request $ request , string $ attributeMethod , string $ expectedTokenValue )
343+ {
344+ $ csrfTokenManager = $ this ->createMock (CsrfTokenManagerInterface::class);
345+ $ csrfTokenManager ->expects ($ this ->once ())
346+ ->method ('isTokenValid ' )
347+ ->with (new CsrfToken ('foo ' , $ expectedTokenValue ))
348+ ->willReturn (true );
349+
350+ $ event = new ControllerArgumentsEvent (
351+ $ this ->createMock (HttpKernelInterface::class),
352+ [new IsCsrfTokenValidAttributeMethodsController (), $ attributeMethod ],
353+ [],
354+ $ request ,
355+ null
356+ );
357+
358+ $ listener = new IsCsrfTokenValidAttributeListener ($ csrfTokenManager );
359+ $ listener ->onKernelControllerArguments ($ event );
360+ }
361+
362+ public static function provideTokenSourceScenarios (): \Generator
363+ {
364+ yield 'tokenSource Payload (default) ' => [
365+ new Request (
366+ request: ['_token ' => 'bar_payload ' ],
367+ query: ['_token ' => 'bar_query ' ]
368+ ),
369+ 'withDefaultTokenKey ' ,
370+ 'bar_payload ' ,
371+ ];
372+ yield 'tokenSource Query ' => [
373+ new Request (
374+ request: ['_token ' => 'bar_payload ' ],
375+ query: ['_token ' => 'bar_query ' ]
376+ ),
377+ 'withCustomTokenSourceQuery ' ,
378+ 'bar_query ' ,
379+ ];
380+ yield 'tokenSource Query|Payload ' => [
381+ new Request (
382+ server: ['CONTENT_TYPE ' => 'application/json ' ],
383+ content: json_encode (['_token ' => 'bar_payload ' ]),
384+ query: ['_token ' => 'bar_query ' ]
385+ ),
386+ 'withCustomTokenSourceQueryPayload ' ,
387+ 'bar_payload ' ,
388+ ];
389+ yield 'tokenSource Header and custom sourceToken ' => [
390+ new Request (
391+ server: ['HTTP_MY_TOKEN_KEY ' => 'bar_header ' ],
392+ request: ['my_token_key ' => 'bar_payload ' ],
393+ query: ['my_token_key ' => 'bar_query ' ]
394+ ),
395+ 'withCustomTokenSourceHeaderAndCustomSourceToken ' ,
396+ 'bar_header ' ,
397+ ];
398+ }
346399}
0 commit comments