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 #3109600: Convert uses of $_SESSION in language module and #3109971: Convert uses of $_SESSION in ViewsExecutable 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:
- All tests inheriting from KernelTestBase.
- All tests using FunctionalTestSetupTrait::initKernel().
- All drush commands (since bootstrapDrupalFull() calls
DrupalKernel::preHandle()
. - 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
Implement- Review
- Commit
User interface changes
None.
API changes
- 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, therequest_stack
is examined duringtearDown()
and violations of that requirement are reported via a deprecation warning.