You are here

protected function WebTestBase::prepareRequestForGenerator in Zircon Profile 8.0

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

Creates a mock request and sets it on the generator.

This is used to manipulate how the generator generates paths during tests. It also ensures that calls to $this->drupalGet() will work when running from run-tests.sh because the url generator no longer looks at the global variables that are set there but relies on getting this information from a request object.

Parameters

bool $clean_urls: Whether to mock the request using clean urls.

$override_server_vars: An array of server variables to override.

Return value

$request The mocked request object.

4 calls to WebTestBase::prepareRequestForGenerator()
DownloadTest::testFileCreateUrl in core/modules/file/src/Tests/DownloadTest.php
Test file_create_url().
ImageStylesPathAndUrlTest::doImageStyleUrlAndPathTests in core/modules/image/src/Tests/ImageStylesPathAndUrlTest.php
Tests building an image style URL.
LanguageUrlRewritingTest::testDomainNameNegotiationPort in core/modules/language/src/Tests/LanguageUrlRewritingTest.php
Check URL rewriting when using a domain name and a non-standard port.
WebTestBase::rebuildContainer in core/modules/simpletest/src/WebTestBase.php
Rebuilds \Drupal::getContainer().

File

core/modules/simpletest/src/WebTestBase.php, line 2916
Contains \Drupal\simpletest\WebTestBase.

Class

WebTestBase
Test case for typical Drupal tests.

Namespace

Drupal\simpletest

Code

protected function prepareRequestForGenerator($clean_urls = TRUE, $override_server_vars = array()) {
  $request = Request::createFromGlobals();
  $server = $request->server
    ->all();
  if (basename($server['SCRIPT_FILENAME']) != basename($server['SCRIPT_NAME'])) {

    // We need this for when the test is executed by run-tests.sh.
    // @todo Remove this once run-tests.sh has been converted to use a Request
    //   object.
    $cwd = getcwd();
    $server['SCRIPT_FILENAME'] = $cwd . '/' . basename($server['SCRIPT_NAME']);
    $base_path = rtrim($server['REQUEST_URI'], '/');
  }
  else {
    $base_path = $request
      ->getBasePath();
  }
  if ($clean_urls) {
    $request_path = $base_path ? $base_path . '/user' : 'user';
  }
  else {
    $request_path = $base_path ? $base_path . '/index.php/user' : '/index.php/user';
  }
  $server = array_merge($server, $override_server_vars);
  $request = Request::create($request_path, 'GET', array(), array(), array(), $server);

  // Ensure the request time is REQUEST_TIME to ensure that API calls
  // in the test use the right timestamp.
  $request->server
    ->set('REQUEST_TIME', REQUEST_TIME);
  $this->container
    ->get('request_stack')
    ->push($request);

  // The request context is normally set by the router_listener from within
  // its KernelEvents::REQUEST listener. In the simpletest parent site this
  // event is not fired, therefore it is necessary to updated the request
  // context manually here.
  $this->container
    ->get('router.request_context')
    ->fromRequest($request);
  return $request;
}