function BigPipeTest::setCsrfTokenSeedInTestEnvironment

Same name in other branches
  1. 9 core/modules/big_pipe/tests/src/Functional/BigPipeTest.php \Drupal\Tests\big_pipe\Functional\BigPipeTest::setCsrfTokenSeedInTestEnvironment()
  2. 8.9.x core/modules/big_pipe/tests/src/Functional/BigPipeTest.php \Drupal\Tests\big_pipe\Functional\BigPipeTest::setCsrfTokenSeedInTestEnvironment()
  3. 10 core/modules/big_pipe/tests/src/Functional/BigPipeTest.php \Drupal\Tests\big_pipe\Functional\BigPipeTest::setCsrfTokenSeedInTestEnvironment()

Ensures CSRF tokens can be generated for the current user's session.

2 calls to BigPipeTest::setCsrfTokenSeedInTestEnvironment()
BigPipeTest::testBigPipe in core/modules/big_pipe/tests/src/Functional/BigPipeTest.php
Tests BigPipe-delivered HTML responses when JavaScript is enabled.
BigPipeTest::testBigPipeNoJs in core/modules/big_pipe/tests/src/Functional/BigPipeTest.php
Tests BigPipe-delivered HTML responses when JavaScript is disabled.

File

core/modules/big_pipe/tests/src/Functional/BigPipeTest.php, line 441

Class

BigPipeTest
Tests BigPipe's no-JS detection & response delivery (with and without JS).

Namespace

Drupal\Tests\big_pipe\Functional

Code

protected function setCsrfTokenSeedInTestEnvironment() : void {
    // Retrieve the CSRF token from the child site from its serialized session
    // record in the database.
    $session_data = $this->container
        ->get('session_handler.write_safe')
        ->read($this->getSession()
        ->getCookie($this->getSessionName()));
    $csrf_token_seed = unserialize(explode('_sf2_meta|', $session_data)[1])['s'];
    // Ensure that the session is started before accessing a session bag.
    // Otherwise the value stored in the bag is lost when subsequent session
    // access triggers a session start automatically.
    
    /** @var \Symfony\Component\HttpFoundation\RequestStack $request_stack */
    $request_stack = $this->container
        ->get('request_stack');
    $session = $request_stack->getSession();
    if (!$session->isStarted()) {
        $session->start();
    }
    // Store the CSRF token in the test runners session metadata bag.
    $this->container
        ->get('session_manager.metadata_bag')
        ->setCsrfTokenSeed($csrf_token_seed);
}

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