function FunctionalTestSetupTrait::setupBaseUrl

Same name in other branches
  1. 8.9.x core/lib/Drupal/Core/Test/FunctionalTestSetupTrait.php \Drupal\Core\Test\FunctionalTestSetupTrait::setupBaseUrl()
  2. 10 core/lib/Drupal/Core/Test/FunctionalTestSetupTrait.php \Drupal\Core\Test\FunctionalTestSetupTrait::setupBaseUrl()
  3. 11.x core/lib/Drupal/Core/Test/FunctionalTestSetupTrait.php \Drupal\Core\Test\FunctionalTestSetupTrait::setupBaseUrl()

Sets up the base URL based upon the environment variable.

Throws

\Exception Thrown when no SIMPLETEST_BASE_URL environment variable is provided or uses an invalid scheme.

5 calls to FunctionalTestSetupTrait::setupBaseUrl()
BrowserTestBase::setUp in core/tests/Drupal/Tests/BrowserTestBase.php
BrowserTestBaseTest::testSimpleTestBaseUrlValidation in core/tests/Drupal/FunctionalTests/BrowserTestBaseTest.php
Test if setting an invalid scheme in SIMPLETEST_BASE_URL throws an exception.
InstallerTestBase::setUp in core/tests/Drupal/FunctionalTests/Installer/InstallerTestBase.php
TestSiteInstallCommand::setup in core/tests/Drupal/TestSite/Commands/TestSiteInstallCommand.php
Creates a test drupal installation.
UpdatePathTestBase::setUp in core/tests/Drupal/FunctionalTests/Update/UpdatePathTestBase.php
Overrides BrowserTestBase::setUp() for update testing.

File

core/lib/Drupal/Core/Test/FunctionalTestSetupTrait.php, line 574

Class

FunctionalTestSetupTrait
Defines a trait for shared functional test setup functionality.

Namespace

Drupal\Core\Test

Code

protected function setupBaseUrl() {
    global $base_url;
    // Get and set the domain of the environment we are running our test
    // coverage against.
    $base_url = getenv('SIMPLETEST_BASE_URL');
    if (!$base_url) {
        throw new \Exception('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 = $parsed_url['port'] ?? 80;
    $valid_url_schemes = [
        'http',
        'https',
    ];
    if (!in_array(strtolower($parsed_url['scheme']), $valid_url_schemes, TRUE)) {
        throw new \Exception('You must provide valid scheme for the SIMPLETEST_BASE_URL environment variable. Valid schema are: http, https.');
    }
    $this->baseUrl = $base_url;
    // 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';
}

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