|
14 | 14 | use PHPUnit\Framework\TestCase;
|
15 | 15 | use Psr\Log\LoggerInterface;
|
16 | 16 | use Symfony\Component\DependencyInjection\Container;
|
| 17 | +use Symfony\Component\HttpFoundation\Cookie; |
17 | 18 | use Symfony\Component\HttpFoundation\Request;
|
18 | 19 | use Symfony\Component\HttpFoundation\RequestStack;
|
19 | 20 | use Symfony\Component\HttpFoundation\Response;
|
|
31 | 32 |
|
32 | 33 | class SessionListenerTest extends TestCase
|
33 | 34 | {
|
| 35 | + /** |
| 36 | + * @dataProvider provideSessionOptions |
| 37 | + * @runInSeparateProcess |
| 38 | + */ |
| 39 | + public function testSessionCookieOptions(array $phpSessionOptions, array $sessionOptions, array $expectedSessionOptions) |
| 40 | + { |
| 41 | + $session = $this->createMock(Session::class); |
| 42 | + $session->method('getUsageIndex')->will($this->onConsecutiveCalls(0, 1)); |
| 43 | + $session->method('getId')->willReturn('123456'); |
| 44 | + $session->method('getName')->willReturn('PHPSESSID'); |
| 45 | + $session->method('save'); |
| 46 | + $session->method('isStarted')->willReturn(true); |
| 47 | + |
| 48 | + if (isset($phpSessionOptions['samesite'])) { |
| 49 | + ini_set('session.cookie_samesite', $phpSessionOptions['samesite']); |
| 50 | + } |
| 51 | + session_set_cookie_params(0, $phpSessionOptions['path'] ?? null, $phpSessionOptions['domain'] ?? null, $phpSessionOptions['secure'] ?? null, $phpSessionOptions['httponly'] ?? null); |
| 52 | + |
| 53 | + $listener = new SessionListener(new Container(), false, $sessionOptions); |
| 54 | + $kernel = $this->createMock(HttpKernelInterface::class); |
| 55 | + |
| 56 | + $request = new Request(); |
| 57 | + $listener->onKernelRequest(new RequestEvent($kernel, $request, HttpKernelInterface::MAIN_REQUEST)); |
| 58 | + |
| 59 | + $request->setSession($session); |
| 60 | + $response = new Response(); |
| 61 | + $listener->onKernelResponse(new ResponseEvent($kernel, $request, HttpKernelInterface::MAIN_REQUEST, $response)); |
| 62 | + |
| 63 | + $cookies = $response->headers->getCookies(); |
| 64 | + $this->assertSame('PHPSESSID', $cookies[0]->getName()); |
| 65 | + $this->assertSame('123456', $cookies[0]->getValue()); |
| 66 | + $this->assertSame($expectedSessionOptions['cookie_path'], $cookies[0]->getPath()); |
| 67 | + $this->assertSame($expectedSessionOptions['cookie_domain'], $cookies[0]->getDomain()); |
| 68 | + $this->assertSame($expectedSessionOptions['cookie_secure'], $cookies[0]->isSecure()); |
| 69 | + $this->assertSame($expectedSessionOptions['cookie_httponly'], $cookies[0]->isHttpOnly()); |
| 70 | + $this->assertSame($expectedSessionOptions['cookie_samesite'], $cookies[0]->getSameSite()); |
| 71 | + } |
| 72 | + |
| 73 | + public function provideSessionOptions(): \Generator |
| 74 | + { |
| 75 | + if (\PHP_VERSION_ID > 70300) { |
| 76 | + yield 'set_samesite_by_php' => [ |
| 77 | + 'phpSessionOptions' => ['samesite' => Cookie::SAMESITE_STRICT], |
| 78 | + 'sessionOptions' => ['cookie_path' => '/test/', 'cookie_domain' => '', 'cookie_secure' => true, 'cookie_httponly' => true], |
| 79 | + 'expectedSessionOptions' => ['cookie_path' => '/test/', 'cookie_domain' => '', 'cookie_secure' => true, 'cookie_httponly' => true, 'cookie_samesite' => Cookie::SAMESITE_STRICT], |
| 80 | + ]; |
| 81 | + } |
| 82 | + |
| 83 | + yield 'set_cookie_path_by_php' => [ |
| 84 | + 'phpSessionOptions' => ['path' => '/prod/'], |
| 85 | + 'sessionOptions' => ['cookie_domain' => '', 'cookie_secure' => true, 'cookie_httponly' => true, 'cookie_samesite' => Cookie::SAMESITE_LAX], |
| 86 | + 'expectedSessionOptions' => ['cookie_path' => '/prod/', 'cookie_domain' => '', 'cookie_secure' => true, 'cookie_httponly' => true, 'cookie_samesite' => Cookie::SAMESITE_LAX], |
| 87 | + ]; |
| 88 | + |
| 89 | + yield 'set_cookie_secure_by_php' => [ |
| 90 | + 'phpSessionOptions' => ['secure' => true], |
| 91 | + 'sessionOptions' => ['cookie_path' => '/test/', 'cookie_domain' => '', 'cookie_httponly' => true, 'cookie_samesite' => Cookie::SAMESITE_LAX], |
| 92 | + 'expectedSessionOptions' => ['cookie_path' => '/test/', 'cookie_domain' => '', 'cookie_secure' => true, 'cookie_httponly' => true, 'cookie_samesite' => Cookie::SAMESITE_LAX], |
| 93 | + ]; |
| 94 | + |
| 95 | + yield 'set_cookiesecure_auto_by_symfony_false_by_php' => [ |
| 96 | + 'phpSessionOptions' => ['secure' => false], |
| 97 | + 'sessionOptions' => ['cookie_path' => '/test/', 'cookie_httponly' => 'auto', 'cookie_secure' => 'auto', 'cookie_samesite' => Cookie::SAMESITE_LAX], |
| 98 | + 'expectedSessionOptions' => ['cookie_path' => '/test/', 'cookie_domain' => '', 'cookie_secure' => false, 'cookie_httponly' => true, 'cookie_samesite' => Cookie::SAMESITE_LAX], |
| 99 | + ]; |
| 100 | + |
| 101 | + yield 'set_cookiesecure_auto_by_symfony_true_by_php' => [ |
| 102 | + 'phpSessionOptions' => ['secure' => true], |
| 103 | + 'sessionOptions' => ['cookie_path' => '/test/', 'cookie_httponly' => 'auto', 'cookie_secure' => 'auto', 'cookie_samesite' => Cookie::SAMESITE_LAX], |
| 104 | + 'expectedSessionOptions' => ['cookie_path' => '/test/', 'cookie_domain' => '', 'cookie_secure' => true, 'cookie_httponly' => true, 'cookie_samesite' => Cookie::SAMESITE_LAX], |
| 105 | + ]; |
| 106 | + |
| 107 | + yield 'set_cookie_httponly_by_php' => [ |
| 108 | + 'phpSessionOptions' => ['httponly' => true], |
| 109 | + 'sessionOptions' => ['cookie_path' => '/test/', 'cookie_domain' => '', 'cookie_secure' => true, 'cookie_samesite' => Cookie::SAMESITE_LAX], |
| 110 | + 'expectedSessionOptions' => ['cookie_path' => '/test/', 'cookie_domain' => '', 'cookie_secure' => true, 'cookie_httponly' => true, 'cookie_samesite' => Cookie::SAMESITE_LAX], |
| 111 | + ]; |
| 112 | + |
| 113 | + yield 'set_cookie_domain_by_php' => [ |
| 114 | + 'phpSessionOptions' => ['domain' => 'test.symfony'], |
| 115 | + 'sessionOptions' => ['cookie_path' => '/test/', 'cookie_httponly' => true, 'cookie_secure' => true, 'cookie_samesite' => Cookie::SAMESITE_LAX], |
| 116 | + 'expectedSessionOptions' => ['cookie_path' => '/test/', 'cookie_domain' => 'test.symfony', 'cookie_secure' => true, 'cookie_httponly' => true, 'cookie_samesite' => Cookie::SAMESITE_LAX], |
| 117 | + ]; |
| 118 | + |
| 119 | + yield 'set_samesite_by_symfony' => [ |
| 120 | + 'phpSessionOptions' => ['samesite' => Cookie::SAMESITE_STRICT], |
| 121 | + 'sessionOptions' => ['cookie_path' => '/test/', 'cookie_httponly' => true, 'cookie_secure' => true, 'cookie_samesite' => Cookie::SAMESITE_LAX], |
| 122 | + 'expectedSessionOptions' => ['cookie_path' => '/test/', 'cookie_domain' => '', 'cookie_secure' => true, 'cookie_httponly' => true, 'cookie_samesite' => Cookie::SAMESITE_LAX], |
| 123 | + ]; |
| 124 | + } |
| 125 | + |
34 | 126 | public function testOnlyTriggeredOnMainRequest()
|
35 | 127 | {
|
36 | 128 | $listener = $this->getMockForAbstractClass(AbstractSessionListener::class);
|
|
0 commit comments