2525use Config \Security as SecurityConfig ;
2626use PHPUnit \Framework \Attributes \BackupGlobals ;
2727use PHPUnit \Framework \Attributes \Group ;
28+ use ReflectionClass ;
29+ use ReflectionMethod ;
2830
2931/**
3032 * @internal
@@ -49,6 +51,16 @@ private function createMockSecurity(?SecurityConfig $config = null): MockSecurit
4951 return new MockSecurity ($ config );
5052 }
5153
54+ private function getPostedTokenMethod (): ReflectionMethod
55+ {
56+ $ reflection = new ReflectionClass (Security::class);
57+ $ method = $ reflection ->getMethod ('getPostedToken ' );
58+
59+ $ method ->setAccessible (true );
60+
61+ return $ method ;
62+ }
63+
5264 public function testBasicConfigIsSaved (): void
5365 {
5466 $ security = $ this ->createMockSecurity ();
@@ -315,4 +327,37 @@ public function testGetters(): void
315327 $ this ->assertIsString ($ security ->getCookieName ());
316328 $ this ->assertIsBool ($ security ->shouldRedirect ());
317329 }
330+
331+ public function testGetPostedTokenReturnsTokenWhenValid (): void
332+ {
333+ $ method = $ this ->getPostedTokenMethod ();
334+ $ security = $ this ->createMockSecurity ();
335+
336+ $ _POST ['csrf_test_name ' ] = '8b9218a55906f9dcc1dc263dce7f005a ' ;
337+ $ request = $ this ->createIncomingRequest ();
338+
339+ $ this ->assertSame ('8b9218a55906f9dcc1dc263dce7f005a ' , $ method ->invoke ($ security , $ request ));
340+ }
341+
342+ public function testGetPostedTokenReturnsNullWhenEmpty (): void
343+ {
344+ $ method = $ this ->getPostedTokenMethod ();
345+ $ security = $ this ->createMockSecurity ();
346+
347+ $ _POST = [];
348+ $ request = $ this ->createIncomingRequest ();
349+
350+ $ this ->assertNull ($ method ->invoke ($ security , $ request ));
351+ }
352+
353+ public function testGetPostedTokenReturnsNullWhenMaliciousData (): void
354+ {
355+ $ method = $ this ->getPostedTokenMethod ();
356+ $ security = $ this ->createMockSecurity ();
357+
358+ $ _POST ['csrf_test_name ' ] = ['malicious ' => 'data ' ];
359+ $ request = $ this ->createIncomingRequest ();
360+
361+ $ this ->assertNull ($ method ->invoke ($ security , $ request ));
362+ }
318363}
0 commit comments