You are here

protected function BrowserTestBase::setUp in Zircon Profile 8.0

Same name and namespace in other branches
  1. 8 core/modules/simpletest/src/BrowserTestBase.php \Drupal\simpletest\BrowserTestBase::setUp()

File

core/modules/simpletest/src/BrowserTestBase.php, line 286
Contains \Drupal\simpletest\BrowserTestBase.

Class

BrowserTestBase
Provides a test case for functional Drupal tests.

Namespace

Drupal\simpletest

Code

protected function setUp() {
  global $base_url;
  parent::setUp();

  // Get and set the domain of the environment we are running our test
  // coverage against.
  $base_url = getenv('SIMPLETEST_BASE_URL');
  if (!$base_url) {
    $this
      ->markTestSkipped('You must provide a SIMPLETEST_BASE_URL environment variable to run some PHPUnit based functional tests.');
  }

  // Setup $_SERVER variable.
  $parsed_url = parse_url($base_url);
  $host = $parsed_url['host'] . (isset($parsed_url['port']) ? ':' . $parsed_url['port'] : '');
  $path = isset($parsed_url['path']) ? rtrim(rtrim($parsed_url['path']), '/') : '';
  $port = isset($parsed_url['port']) ? $parsed_url['port'] : 80;

  // If the passed URL schema is 'https' then setup the $_SERVER variables
  // properly so that testing will run under HTTPS.
  if ($parsed_url['scheme'] === 'https') {
    $_SERVER['HTTPS'] = 'on';
  }
  $_SERVER['HTTP_HOST'] = $host;
  $_SERVER['REMOTE_ADDR'] = '127.0.0.1';
  $_SERVER['SERVER_ADDR'] = '127.0.0.1';
  $_SERVER['SERVER_PORT'] = $port;
  $_SERVER['SERVER_SOFTWARE'] = NULL;
  $_SERVER['SERVER_NAME'] = 'localhost';
  $_SERVER['REQUEST_URI'] = $path . '/';
  $_SERVER['REQUEST_METHOD'] = 'GET';
  $_SERVER['SCRIPT_NAME'] = $path . '/index.php';
  $_SERVER['SCRIPT_FILENAME'] = $path . '/index.php';
  $_SERVER['PHP_SELF'] = $path . '/index.php';
  $_SERVER['HTTP_USER_AGENT'] = 'Drupal command line';

  // Install Drupal test site.
  $this
    ->prepareEnvironment();
  $this
    ->installDrupal();

  // Setup Mink.
  $session = $this
    ->initMink();

  // 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 when running tests, by
  // default the cookie is not forwarded to the client side, so you cannot
  // debug the code running on the test 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.
  $request = \Drupal::request();
  $cookie_params = $request->cookies;
  if ($cookie_params
    ->has('XDEBUG_SESSION')) {
    $session
      ->setCookie('XDEBUG_SESSION', $cookie_params
      ->get('XDEBUG_SESSION'));
  }

  // For CLI requests, the information is stored in $_SERVER.
  $server = $request->server;
  if ($server
    ->has('XDEBUG_CONFIG')) {

    // $_SERVER['XDEBUG_CONFIG'] has the form "key1=value1 key2=value2 ...".
    $pairs = explode(' ', $server
      ->get('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') {
        $session
          ->setCookie('XDEBUG_SESSION', trim($value));
      }
    }
  }
}