From 6eec619abc7f8849277a38cdc101dece9adecda3 Mon Sep 17 00:00:00 2001
From: webchick <webchick@24967.no-reply.drupal.org>
Date: Thu, 22 May 2014 10:54:36 -0700
Subject: [PATCH] Revert "Issue #2239969 by znerol, sun: Session of (UI) test
 runner leaks into web tests."

Broke HEAD. Temporary rollback.

This reverts commit 5a3ef306311d78ca39daab6ec52698f8cd562b09.
---
 .../lib/Drupal/simpletest/TestBase.php        | 29 ++-----------------
 .../lib/Drupal/simpletest/WebTestBase.php     | 20 +++----------
 .../system/Tests/Session/SessionHttpsTest.php | 20 ++++++-------
 .../system/Tests/Session/SessionTest.php      |  2 +-
 4 files changed, 18 insertions(+), 53 deletions(-)

diff --git a/core/modules/simpletest/lib/Drupal/simpletest/TestBase.php b/core/modules/simpletest/lib/Drupal/simpletest/TestBase.php
index fe9b62eba166..023cf8e53bcc 100644
--- a/core/modules/simpletest/lib/Drupal/simpletest/TestBase.php
+++ b/core/modules/simpletest/lib/Drupal/simpletest/TestBase.php
@@ -7,7 +7,6 @@
 
 namespace Drupal\simpletest;
 
-use Drupal\Component\Utility\Crypt;
 use Drupal\Component\Utility\Random;
 use Drupal\Core\Database\Database;
 use Drupal\Component\Utility\String;
@@ -190,11 +189,6 @@ abstract class TestBase {
    */
   protected $randomGenerator;
 
-  /**
-   * The name of the session cookie.
-   */
-  protected $originalSessionName;
-
   /**
    * Constructor for Test.
    *
@@ -1035,17 +1029,8 @@ private function prepareEnvironment() {
     $this->originalProfile = drupal_get_profile();
     $this->originalUser = isset($user) ? clone $user : NULL;
 
-    // Prevent that session data is leaked into the UI test runner by closing
-    // the session and then setting the session-name (i.e. the name of the
-    // session cookie) to a random value. If a test starts a new session, then
-    // it will be associated with a different session-name. After the test-run
-    // it can be safely destroyed.
-    // @see TestBase::restoreEnvironment()
-    if (PHP_SAPI != 'cli' && session_status() == PHP_SESSION_ACTIVE) {
-      session_write_close();
-    }
-    $this->originalSessionName = session_name();
-    session_name('SIMPLETEST' . Crypt::randomBytesBase64());
+    // Ensure that the current session is not changed by the new environment.
+    \Drupal::service('session_manager')->disable();
 
     // Save and clean the shutdown callbacks array because it is static cached
     // and will be changed by the test run. Otherwise it will contain callbacks
@@ -1160,15 +1145,6 @@ protected function tearDown() {
    * @see TestBase::prepareEnvironment()
    */
   private function restoreEnvironment() {
-    // Destroy the session if one was started during the test-run.
-    $_SESSION = array();
-    if (PHP_SAPI != 'cli' && session_status() == PHP_SESSION_ACTIVE) {
-      session_destroy();
-      $params = session_get_cookie_params();
-      setcookie(session_name(), '', REQUEST_TIME - 3600, $params['path'], $params['domain'], $params['secure'], $params['httponly']);
-    }
-    session_name($this->originalSessionName);
-
     // Reset all static variables.
     // Unsetting static variables will potentially invoke destruct methods,
     // which might call into functions that prime statics and caches again.
@@ -1254,6 +1230,7 @@ private function restoreEnvironment() {
 
     // Restore original user session.
     $this->container->set('current_user', $this->originalUser);
+    \Drupal::service('session_manager')->enable();
   }
 
   /**
diff --git a/core/modules/simpletest/lib/Drupal/simpletest/WebTestBase.php b/core/modules/simpletest/lib/Drupal/simpletest/WebTestBase.php
index 82e37e44c936..a120883ac2e3 100644
--- a/core/modules/simpletest/lib/Drupal/simpletest/WebTestBase.php
+++ b/core/modules/simpletest/lib/Drupal/simpletest/WebTestBase.php
@@ -776,16 +776,6 @@ protected function drupalLogout() {
     }
   }
 
-  /**
-   * Return the session name in use on the child site.
-   *
-   * @return string
-   *   The name of the session cookie.
-   */
-  public function getSessionName() {
-    return $this->session_name;
-  }
-
   /**
    * Sets up a Drupal site for running functional and integration tests.
    *
@@ -817,12 +807,6 @@ protected function setUp() {
       'pass_raw' => $this->randomName(),
     ));
 
-    // The simpletest child site currently uses the same session name as the
-    // execution environment.
-    // @todo: Introduce a setting such that the session name can be customized
-    // for the child site.
-    $this->session_name = $this->originalSessionName;
-
     // Reset the static batch to remove Simpletest's batch operations.
     $batch = &batch_get();
     $batch = array();
@@ -1123,6 +1107,7 @@ protected function rebuildContainer($environment = 'prod') {
     else {
       $this->container->get('request_stack')->push($request);
     }
+    $this->container->get('current_user')->setAccount(\Drupal::currentUser());
 
     // The request context is normally set by the router_listener from within
     // its KernelEvents::REQUEST listener. In the simpletest parent site this
@@ -1239,6 +1224,9 @@ protected function curlInitialize() {
       if (!$result) {
         throw new \UnexpectedValueException('One or more cURL options could not be set.');
       }
+
+      // By default, the child session name should be the same as the parent.
+      $this->session_name = session_name();
     }
     // We set the user agent header on each request so as to use the current
     // time and a new uniqid.
diff --git a/core/modules/system/lib/Drupal/system/Tests/Session/SessionHttpsTest.php b/core/modules/system/lib/Drupal/system/Tests/Session/SessionHttpsTest.php
index cc753b6e6d09..a9c709c1ae5f 100644
--- a/core/modules/system/lib/Drupal/system/Tests/Session/SessionHttpsTest.php
+++ b/core/modules/system/lib/Drupal/system/Tests/Session/SessionHttpsTest.php
@@ -40,12 +40,12 @@ public function setUp() {
 
   protected function testHttpsSession() {
     if ($this->request->isSecure()) {
-      $secure_session_name = $this->getSessionName();
-      $insecure_session_name = substr($this->getSessionName(), 1);
+      $secure_session_name = session_name();
+      $insecure_session_name = substr(session_name(), 1);
     }
     else {
-      $secure_session_name = 'S' . $this->getSessionName();
-      $insecure_session_name = $this->getSessionName();
+      $secure_session_name = 'S' . session_name();
+      $insecure_session_name = session_name();
     }
 
     $user = $this->drupalCreateUser(array('access administration pages'));
@@ -124,8 +124,8 @@ protected function testMixedModeSslSession() {
       return;
     }
     else {
-      $secure_session_name = 'S' . $this->getSessionName();
-      $insecure_session_name = $this->getSessionName();
+      $secure_session_name = 'S' . session_name();
+      $insecure_session_name = session_name();
     }
 
     // Enable secure pages.
@@ -231,12 +231,12 @@ protected function testMixedModeSslSession() {
    */
   protected function testCsrfTokenWithMixedModeSsl() {
     if ($this->request->isSecure()) {
-      $secure_session_name = $this->getSessionName();
-      $insecure_session_name = substr($this->getSessionName(), 1);
+      $secure_session_name = session_name();
+      $insecure_session_name = substr(session_name(), 1);
     }
     else {
-      $secure_session_name = 'S' . $this->getSessionName();
-      $insecure_session_name = $this->getSessionName();
+      $secure_session_name = 'S' . session_name();
+      $insecure_session_name = session_name();
     }
 
     // Enable mixed mode SSL.
diff --git a/core/modules/system/lib/Drupal/system/Tests/Session/SessionTest.php b/core/modules/system/lib/Drupal/system/Tests/Session/SessionTest.php
index c64d1adae4d4..720f16fcfc8d 100644
--- a/core/modules/system/lib/Drupal/system/Tests/Session/SessionTest.php
+++ b/core/modules/system/lib/Drupal/system/Tests/Session/SessionTest.php
@@ -36,7 +36,7 @@ public static function getInfo() {
    */
   function testSessionSaveRegenerate() {
     $session_manager = $this->container->get('session_manager');
-    $this->assertTrue($session_manager->isEnabled(), 'SessionManager->isEnabled() initially returns TRUE.');
+    $this->assertFalse($session_manager->isEnabled(), 'SessionManager->isEnabled() initially returns FALSE (in testing framework).');
     $this->assertFalse($session_manager->disable()->isEnabled(), 'SessionManager->isEnabled() returns FALSE after disabling.');
     $this->assertTrue($session_manager->enable()->isEnabled(), 'SessionManager->isEnabled() returns TRUE after enabling.');
 
-- 
GitLab