Quantcast
Channel: Issues for Drupal core
Viewing all articles
Browse latest Browse all 295737

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

$
0
0

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:

  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

  • Implement
  • 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.

Viewing all articles
Browse latest Browse all 295737


<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>