Commit dcdf0f81 authored by alexpott's avatar alexpott

Issue #2478167 by andypost, znerol: Generate proper value for sessionName...

Issue #2478167 by andypost, znerol: Generate proper value for sessionName property in BrowserTestBase
parent 673f5b79
...@@ -52,6 +52,8 @@ ...@@ -52,6 +52,8 @@
*/ */
abstract class BrowserTestBase extends \PHPUnit_Framework_TestCase { abstract class BrowserTestBase extends \PHPUnit_Framework_TestCase {
use SessionTestTrait;
/** /**
* Class loader. * Class loader.
* *
...@@ -158,13 +160,6 @@ abstract class BrowserTestBase extends \PHPUnit_Framework_TestCase { ...@@ -158,13 +160,6 @@ abstract class BrowserTestBase extends \PHPUnit_Framework_TestCase {
*/ */
protected $profile = 'testing'; protected $profile = 'testing';
/**
* The current session name, if available.
*
* @var string
*/
protected $sessionName;
/** /**
* The current user logged in using the Mink controlled browser. * The current user logged in using the Mink controlled browser.
* *
...@@ -702,7 +697,7 @@ protected function drupalLogin(AccountInterface $account) { ...@@ -702,7 +697,7 @@ protected function drupalLogin(AccountInterface $account) {
), t('Log in')); ), t('Log in'));
// @see BrowserTestBase::drupalUserIsLoggedIn() // @see BrowserTestBase::drupalUserIsLoggedIn()
$account->sessionId = $this->getSession()->getCookie(session_name()); $account->sessionId = $this->getSession()->getCookie($this->getSessionName());
$this->assertTrue($this->drupalUserIsLoggedIn($account), SafeMarkup::format('User %name successfully logged in.', array('name' => $account->getUsername()))); $this->assertTrue($this->drupalUserIsLoggedIn($account), SafeMarkup::format('User %name successfully logged in.', array('name' => $account->getUsername())));
$this->loggedInUser = $account; $this->loggedInUser = $account;
...@@ -835,11 +830,9 @@ public function installDrupal() { ...@@ -835,11 +830,9 @@ public function installDrupal() {
'passRaw' => $this->randomMachineName(), 'passRaw' => $this->randomMachineName(),
)); ));
// Some tests (SessionTest and SessionHttpsTest) need to examine whether the // The child site derives its session name from the database prefix when
// proper session cookies were set on a response. Because the child site // running web tests.
// uses the same session name as the test runner, it is necessary to make $this->generateSessionName($this->databasePrefix);
// that available to test-methods.
$this->sessionName = session_name();
// Get parameters for install_drupal() before removing global variables. // Get parameters for install_drupal() before removing global variables.
$parameters = $this->installParameters(); $parameters = $this->installParameters();
......
<?php
/**
* @file
* Contains \Drupal\simpletest\SessionTestTrait.
*/
namespace Drupal\simpletest;
use Symfony\Component\HttpFoundation\Request;
/**
* Provides methods to generate and get session name in tests.
*/
trait SessionTestTrait {
/**
* The name of the session cookie.
*
* @var string
*/
protected $sessionName;
/**
* Generates a session cookie name.
*
* @param string $data
* The data to generate session name.
*/
protected function generateSessionName($data) {
$prefix = (Request::createFromGlobals()->isSecure() ? 'SSESS' : 'SESS');
$this->sessionName = $prefix . substr(hash('sha256', $data), 0, 32);
}
/**
* Returns the session name in use on the child site.
*
* @return string
* The name of the session cookie.
*/
protected function getSessionName() {
return $this->sessionName;
}
}
...@@ -13,18 +13,11 @@ ...@@ -13,18 +13,11 @@
use Drupal\Core\Database\Database; use Drupal\Core\Database\Database;
use Drupal\Core\Config\ConfigImporter; use Drupal\Core\Config\ConfigImporter;
use Drupal\Core\Config\StorageComparer; use Drupal\Core\Config\StorageComparer;
use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\Core\Database\ConnectionNotDefinedException; use Drupal\Core\Database\ConnectionNotDefinedException;
use Drupal\Core\Config\StorageInterface; use Drupal\Core\Config\StorageInterface;
use Drupal\Core\Language\Language;
use Drupal\Core\Session\AccountProxy;
use Drupal\Core\Session\AnonymousUserSession;
use Drupal\Core\Site\Settings; use Drupal\Core\Site\Settings;
use Drupal\Core\StreamWrapper\PublicStream; use Drupal\Core\StreamWrapper\PublicStream;
use Drupal\Core\Utility\Error; use Drupal\Core\Utility\Error;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\DependencyInjection\Reference;
/** /**
* Base class for Drupal tests. * Base class for Drupal tests.
...@@ -33,6 +26,9 @@ ...@@ -33,6 +26,9 @@
* \Drupal\simpletest\WebTestBase or \Drupal\simpletest\KernelTestBase. * \Drupal\simpletest\WebTestBase or \Drupal\simpletest\KernelTestBase.
*/ */
abstract class TestBase { abstract class TestBase {
use SessionTestTrait;
/** /**
* The test run ID. * The test run ID.
* *
...@@ -187,7 +183,7 @@ abstract class TestBase { ...@@ -187,7 +183,7 @@ abstract class TestBase {
protected $originalProfile; protected $originalProfile;
/** /**
* The name of the session cookie. * The name of the session cookie of the test-runner.
* *
* @var string * @var string
*/ */
......
...@@ -142,11 +142,6 @@ abstract class WebTestBase extends TestBase { ...@@ -142,11 +142,6 @@ abstract class WebTestBase extends TestBase {
*/ */
protected $originalShutdownCallbacks = array(); protected $originalShutdownCallbacks = array();
/**
* The current session name, if available.
*/
protected $sessionName = NULL;
/** /**
* The current session ID, if available. * The current session ID, if available.
*/ */
...@@ -616,16 +611,6 @@ protected function drupalLogout() { ...@@ -616,16 +611,6 @@ protected function drupalLogout() {
} }
} }
/**
* Returns the session name in use on the child site.
*
* @return string
* The name of the session cookie.
*/
public function getSessionName() {
return $this->sessionName;
}
/** /**
* Sets up a Drupal site for running functional and integration tests. * Sets up a Drupal site for running functional and integration tests.
* *
...@@ -659,8 +644,7 @@ protected function setUp() { ...@@ -659,8 +644,7 @@ protected function setUp() {
// The child site derives its session name from the database prefix when // The child site derives its session name from the database prefix when
// running web tests. // running web tests.
$prefix = (Request::createFromGlobals()->isSecure() ? 'SSESS' : 'SESS'); $this->generateSessionName($this->databasePrefix);
$this->sessionName = $prefix . substr(hash('sha256', $this->databasePrefix), 0, 32);
// Reset the static batch to remove Simpletest's batch operations. // Reset the static batch to remove Simpletest's batch operations.
$batch = &batch_get(); $batch = &batch_get();
...@@ -1319,7 +1303,7 @@ protected function curlHeaderCallback($curlHandler, $header) { ...@@ -1319,7 +1303,7 @@ protected function curlHeaderCallback($curlHandler, $header) {
$parts = array_map('trim', explode(';', $matches[2])); $parts = array_map('trim', explode(';', $matches[2]));
$value = array_shift($parts); $value = array_shift($parts);
$this->cookies[$name] = array('value' => $value, 'secure' => in_array('secure', $parts)); $this->cookies[$name] = array('value' => $value, 'secure' => in_array('secure', $parts));
if ($name == $this->sessionName) { if ($name === $this->getSessionName()) {
if ($value != 'deleted') { if ($value != 'deleted') {
$this->sessionId = $value; $this->sessionId = $value;
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment