diff --git a/core/modules/system/tests/modules/test_page_test/src/Controller/Test.php b/core/modules/system/tests/modules/test_page_test/src/Controller/Test.php index e754eaf0c2..58797ce2fe 100644 --- a/core/modules/system/tests/modules/test_page_test/src/Controller/Test.php +++ b/core/modules/system/tests/modules/test_page_test/src/Controller/Test.php @@ -4,6 +4,7 @@ use Drupal\Core\Url; use Symfony\Component\HttpFoundation\RedirectResponse; +use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpKernel\Exception\HttpException; /** @@ -111,6 +112,17 @@ public function renderPipeInLink() { return ['#markup' => 'foo|bar|baz']; } + /** + * Renders all cookies from request. + * + * @return \Symfony\Component\HttpFoundation\Response + * The response object. + */ + public function renderRequestCookies() { + $cookies = \Drupal::request()->cookies->all(); + return new Response(json_encode($cookies), 200); + } + /** * Loads a page that does a redirect. * diff --git a/core/modules/system/tests/modules/test_page_test/test_page_test.routing.yml b/core/modules/system/tests/modules/test_page_test/test_page_test.routing.yml index e64a88d003..3dedee720a 100644 --- a/core/modules/system/tests/modules/test_page_test/test_page_test.routing.yml +++ b/core/modules/system/tests/modules/test_page_test/test_page_test.routing.yml @@ -90,3 +90,11 @@ test_page_test.meta_refresh: _controller: '\Drupal\test_page_test\Controller\Test::metaRefresh' requirements: _access: 'TRUE' + +test_page_test.request_cookies: + path: '/test-request-cookies' + defaults: + _title: 'Request Cookies' + _controller: '\Drupal\test_page_test\Controller\Test::renderRequestCookies' + requirements: + _access: 'TRUE' diff --git a/core/tests/Drupal/FunctionalTests/BrowserTestBaseTest.php b/core/tests/Drupal/FunctionalTests/BrowserTestBaseTest.php index 95ee39fd3a..968c60dedf 100644 --- a/core/tests/Drupal/FunctionalTests/BrowserTestBaseTest.php +++ b/core/tests/Drupal/FunctionalTests/BrowserTestBaseTest.php @@ -639,4 +639,41 @@ public function testGetDefaultDriveInstance() { $this->assertEquals([NULL, ['key1' => ['key2' => ['key3' => 3, 'key3.1' => 3.1]]]], $this->minkDefaultDriverArgs); } + /** + * Tests the ::getHttpClient() method with Xdebug mode. + * + * @dataProvider providerTestGetHttpClientXdebugOn + * @dataProvider providerTestGetHttpClientXdebugOff + */ + public function testGetHttpClientWithXdebugMode($expected) { + $url = Url::fromRoute('test_page_test.request_cookies')->setAbsolute()->toString(); + $response = $this->getHttpClient()->request('GET', $url); + $this->assertSame($expected, (string) $response->getBody()); + } + + /** + * Data provider for testGetHttpClientWithXdebugMode. + * + * @return array + * Expected response. + */ + public function providerTestGetHttpClientXdebugOn() { + putenv('XDEBUG_CONFIG=idekey=cake'); + return [ + 'Xdebug On' => [json_encode(['XDEBUG_SESSION' => 'cake'])], + ]; + } + + /** + * Data provider for testGetHttpClientWithXdebugMode. + * + * @return array + * Expected response. + */ + public function providerTestGetHttpClientXdebugOff() { + return [ + 'Xdebug Off' => [''], + ]; + } + } diff --git a/core/tests/Drupal/Tests/BrowserTestBase.php b/core/tests/Drupal/Tests/BrowserTestBase.php index 4a983c05a1..2bef4fa092 100644 --- a/core/tests/Drupal/Tests/BrowserTestBase.php +++ b/core/tests/Drupal/Tests/BrowserTestBase.php @@ -23,6 +23,7 @@ use Drupal\Tests\node\Traits\ContentTypeCreationTrait; use Drupal\Tests\node\Traits\NodeCreationTrait; use Drupal\Tests\user\Traits\UserCreationTrait; +use GuzzleHttp\Cookie\CookieJar; use PHPUnit\Framework\TestCase; use Psr\Http\Message\RequestInterface; use Psr\Http\Message\ResponseInterface; @@ -296,11 +297,24 @@ protected function initMink() { if ($driver instanceof GoutteDriver) { // Turn off curl timeout. Having a timeout is not a problem in a normal // test running, but it is a problem when debugging. Also, disable SSL - // peer verification so that testing under HTTPS always works. + // peer verification so that testing under HTTPS always works. Include + // cookies with XDEBUG_SESSION in the debugging. + $request = \Drupal::request(); + $cookies_params = $this->extractCookiesFromRequest($request); + if (isset($cookies_params['XDEBUG_SESSION'])) { + $cookies = new CookieJar(TRUE, [ + [ + 'Name' => 'XDEBUG_SESSION', + 'Value' => current($cookies_params['XDEBUG_SESSION']), + 'Domain' => $request->getHost(), + ], + ]); + } /** @var \GuzzleHttp\Client $client */ $client = $this->container->get('http_client_factory')->fromOptions([ 'timeout' => NULL, 'verify' => FALSE, + 'cookies' => $cookies ?: FALSE, ]); // Inject a Guzzle middleware to generate debug output for every request