function FunctionalTestSetupTrait::prepareRequestForGenerator

Same name in other branches
  1. 9 core/lib/Drupal/Core/Test/FunctionalTestSetupTrait.php \Drupal\Core\Test\FunctionalTestSetupTrait::prepareRequestForGenerator()
  2. 8.9.x core/lib/Drupal/Core/Test/FunctionalTestSetupTrait.php \Drupal\Core\Test\FunctionalTestSetupTrait::prepareRequestForGenerator()
  3. 11.x core/lib/Drupal/Core/Test/FunctionalTestSetupTrait.php \Drupal\Core\Test\FunctionalTestSetupTrait::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.

array $override_server_vars: An array of server variables to override.

Return value

\Symfony\Component\HttpFoundation\Request The mocked request object.

3 calls to FunctionalTestSetupTrait::prepareRequestForGenerator()
DownloadTest::testFileCreateUrl in core/modules/file/tests/src/Functional/DownloadTest.php
Test FileUrlGeneratorInterface::generateString()
ImageStylesPathAndUrlTest::doImageStyleUrlAndPathTests in core/modules/image/tests/src/Functional/ImageStylesPathAndUrlTest.php
Tests building an image style URL.
LanguageUrlRewritingTest::testDomainNameNegotiationPort in core/modules/language/tests/src/Functional/LanguageUrlRewritingTest.php
Check URL rewriting when using a domain name and a non-standard port.

File

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

Class

FunctionalTestSetupTrait
Defines a trait for shared functional test setup functionality.

Namespace

Drupal\Core\Test

Code

protected function prepareRequestForGenerator($clean_urls = TRUE, $override_server_vars = []) {
    $request = Request::createFromGlobals();
    $request->setSession(new Session(new MockArraySessionStorage()));
    $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($request->server
        ->all(), $override_server_vars);
    $request = Request::create($request_path, 'GET', [], [], [], $server);
    $request->setSession(new Session(new MockArraySessionStorage()));
    // Ensure the request time is \Drupal::time()->getRequestTime() to ensure
    // that API calls in the test use the right timestamp.
    $request->server
        ->set('REQUEST_TIME', \Drupal::time()->getRequestTime());
    $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 parent site this event is not
    // fired, therefore it is necessary to update the request context manually
    // here.
    $this->container
        ->get('router.request_context')
        ->fromRequest($request);
    return $request;
}

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