Add the session to the request in KernelTestBase, BrowserTestBase, and drush

Created on 7 May 2015, over 9 years ago
Updated 11 January 2024, 11 months ago

Problem/Motivation

Currently the session is only available from the request in tests running in the child site. Neither BrowserTestBase nor KernelTestBase add a session to the request when populating the request stack in preparation of a test-run.

As a result, production code has to guard access to the session using $request->hasSession(), but only in some cases. This leads to an inconsistent call patterns into the session API throughout the code base.

If production code could rely on the presence of some session on ever request, issues like πŸ“Œ Convert uses of $_SESSION in language module Needs review and πŸ“Œ Use $this->request in ViewsExecutable::getExposedInput() consistently Needs review could be implemented in a simple and straight forward way.

Proposed resolution

Add a session backed by symfony MockArraySessionStorage unconditionally to every request in DrupalKernel::preHandle()

As a result, a session is automatically available on every request in the following environments:

  1. All tests inheriting from KernelTestBase.
  2. All tests using FunctionalTestSetupTrait::initKernel().
  3. All drush commands (since bootstrapDrupalFull() calls DrupalKernel::preHandle().
  4. All drupal console commands (since boot() calls DrupalKernel::preHandle().

With MR 3320, the following drush script snippet prints bool(TRUE):

echo 'var_dump(\Drupal::request()->getSession()->isStarted());' | drush php:script -

Hence, it is now possible to invoke code from drush, from drupal console, from kernel tests and from the parent process in browser tests which is relying on the presence of a session somewhere deeply nested inside the call graph.

Remaining tasks

  • Review
  • Commit

User interface changes

None.

API changes

  1. A session should be added to every Request - whether it is constructed by Drupal core, inside some test case or by contrib / custom code. In order to communicate that requirement, the request_stack is examined during tearDown() and violations of that requirement are reported via a deprecation warning.
πŸ“Œ Task
Status

Fixed

Version

11.0 πŸ”₯

Component
BaseΒ  β†’

Last updated about 1 hour ago

Created by

πŸ‡¨πŸ‡­Switzerland znerol

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

Production build 0.71.5 2024