From 9107f06db39ca50759b37fa0c142a1e0b3c48911 Mon Sep 17 00:00:00 2001
From: Alex Pott <alex.a.pott@googlemail.com>
Date: Sat, 16 Nov 2013 19:30:41 -0400
Subject: [PATCH] Issue #2134259 by tstoeckler: Make the Simpletest XDebug
 integration work for CLI requests.

---
 .../lib/Drupal/simpletest/WebTestBase.php     | 21 +++++++++++++++----
 1 file changed, 17 insertions(+), 4 deletions(-)

diff --git a/core/modules/simpletest/lib/Drupal/simpletest/WebTestBase.php b/core/modules/simpletest/lib/Drupal/simpletest/WebTestBase.php
index e4182843de9a..284df1d360af 100644
--- a/core/modules/simpletest/lib/Drupal/simpletest/WebTestBase.php
+++ b/core/modules/simpletest/lib/Drupal/simpletest/WebTestBase.php
@@ -1132,15 +1132,28 @@ protected function curlExec($curl_options, $redirect = FALSE) {
     if (!empty($this->curlCookies)) {
       $cookies = $this->curlCookies;
     }
-    // In order to debug webtests you need to either set a cookie or have the
-    // xdebug session in the URL. If the developer listens to connection on the
-    // parent site, by default the cookie is not forwarded to the client side,
-    // so you can't debug actual running code. In order to make debuggers work
+    // 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.
     if (isset($_COOKIE['XDEBUG_SESSION'])) {
       $cookies[] = 'XDEBUG_SESSION=' . $_COOKIE['XDEBUG_SESSION'];
     }
+    // For CLI requests, the information is stored in $_SERVER.
+    if (isset($_SERVER['XDEBUG_CONFIG'])) {
+      // $_SERVER['XDEBUG_CONFIG'] has the form "key1=value1 key2=value2 ...".
+      $pairs = explode(' ', $_SERVER['XDEBUG_CONFIG']);
+      foreach ($pairs as $pair) {
+        list($key, $value) = explode('=', $pair);
+        // Account for key-value pairs being separated by multiple spaces.
+        if (trim($key, ' ') == 'idekey') {
+          $cookies[] = 'XDEBUG_SESSION=' . trim($value, ' ');
+        }
+      }
+    }
 
     // Merge additional cookies in.
     if (!empty($cookies)) {
-- 
GitLab