function XdebugRequestTrait::extractCookiesFromRequest

Same name in other branches
  1. 8.9.x core/tests/Drupal/Tests/XdebugRequestTrait.php \Drupal\Tests\XdebugRequestTrait::extractCookiesFromRequest()
  2. 10 core/tests/Drupal/Tests/XdebugRequestTrait.php \Drupal\Tests\XdebugRequestTrait::extractCookiesFromRequest()
  3. 11.x core/tests/Drupal/Tests/XdebugRequestTrait.php \Drupal\Tests\XdebugRequestTrait::extractCookiesFromRequest()

Adds xdebug cookies, from request setup.

In order to debug web tests you need to either set a cookie, have the Xdebug session in the URL or set an environment variable in case of CLI requests. If the developer listens to connection on the parent site, by default the cookie is not forwarded to the client side, so you cannot debug the code running on the child site. In order to make debuggers work this bit of information is forwarded. Make sure that the debugger listens to at least three external connections.

Parameters

\Symfony\Component\HttpFoundation\Request $request: The request.

Return value

array The extracted cookies.

3 calls to XdebugRequestTrait::extractCookiesFromRequest()
BrowserTestBase::initMink in core/tests/Drupal/Tests/BrowserTestBase.php
Initializes Mink sessions.
SessionHttpsTest::getGuzzleCookieJar in core/modules/system/tests/src/Functional/Session/SessionHttpsTest.php
Creates a new Guzzle CookieJar with a Xdebug cookie if necessary.
XdebugRequestTraitTest::testExtractCookiesFromRequest in core/tests/Drupal/Tests/Core/Test/XdebugRequestTraitTest.php
Tests that Xdebug cookies are extracted from a request correctly.

File

core/tests/Drupal/Tests/XdebugRequestTrait.php, line 26

Class

XdebugRequestTrait

Namespace

Drupal\Tests

Code

protected function extractCookiesFromRequest(Request $request) {
    $cookie_params = $request->cookies;
    $cookies = [];
    if ($cookie_params->has('XDEBUG_SESSION')) {
        $cookies['XDEBUG_SESSION'][] = $cookie_params->get('XDEBUG_SESSION');
    }
    // For CLI requests, the information is stored in $_SERVER.
    $server = $request->server;
    if ($server->has('XDEBUG_SESSION')) {
        $cookies['XDEBUG_SESSION'][] = $server->get('XDEBUG_SESSION');
    }
    elseif ($server->has('XDEBUG_CONFIG')) {
        // $_SERVER['XDEBUG_CONFIG'] has the form "key1=value1 key2=value2 ...".
        $pairs = array_filter(explode(' ', $server->get('XDEBUG_CONFIG')), function ($value) {
            return str_contains($value, '=');
        });
        foreach ($pairs as $pair) {
            [
                $key,
                $value,
            ] = explode('=', $pair, 2);
            // Account for key-value pairs being separated by multiple spaces.
            if (trim($key, ' ') == 'idekey') {
                $cookies['XDEBUG_SESSION'][] = trim($value, ' ');
            }
        }
    }
    return $cookies;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.