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