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: Use $this->request in ViewsExecutable::getExposedInput() consistently 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.
CommentFileSizeAuthor
#125 2484991-nr-bot.txt90 bytesneeds-review-queue-bot
#119 2484991-nr-bot.txt90 bytesneeds-review-queue-bot
#72 2484991-72.patch20.94 KBznerol
#72 interdiff.txt633 bytesznerol
#70 interdiff.txt1.36 KBznerol
#70 2484991-add-the-session-70.patch20.32 KBznerol
#69 2484991-69.patch18.95 KBandypost
#69 interdiff.txt829 bytesandypost
#68 2484991-68.patch19.76 KBandypost
#68 interdiff.txt869 bytesandypost
#67 2484991-67-10.1.patch20.61 KBandypost
#67 interdiff.txt3.59 KBandypost
#66 2484991-67.patch21.8 KBandypost
#66 interdiff.txt26.96 KBandypost
#65 2484991-65-10.1.patch47.99 KBandypost
#65 interdiff.txt10.79 KBandypost
#52 interdiff.txt2.38 KBznerol
#52 2484991-52.patch54.11 KBznerol
#50 2484991-50.patch52.57 KBznerol
#50 interdiff.txt767 bytesznerol
#48 interdiff.txt1.19 KBznerol
#48 2484991-48.patch52.57 KBznerol
#46 interdiff_43_46.txt751 bytesanmolgoyal74
#46 2484991-46.patch52.76 KBanmolgoyal74
#43 reroll_diff_2484991_41-43.txt9.67 KBankithashetty
#43 2484991-43.patch52.92 KBankithashetty
#41 interdiff-2484991-40-41.txt275 bytesmarkdorison
#41 2484991-41.patch52.89 KBmarkdorison
#40 interdiff_36_40.txt5.41 KBanmolgoyal74
#40 2484991-40.patch52.63 KBanmolgoyal74
#36 2484991-36.patch52.6 KBandypost
#36 interdiff.txt1.33 KBandypost
#34 2484991-32.diff52.56 KBznerol
#33 2484991-32.diff52.56 KBznerol
#29 2484991-29.diff52.57 KBznerol
#29 interdiff-29.txt1.4 KBznerol
#28 2484991-28.diff52.28 KBznerol
#28 interdiff-28.txt1.55 KBznerol
#26 interdiff-26.txt35.39 KBznerol
#26 2484991-26.diff48.41 KBznerol
#25 interdiff-25.txt6.05 KBznerol
#25 2484991.diff14.34 KBznerol
#23 2484991-23.diff12.28 KBznerol
#23 interdiff-23.txt813 bytesznerol
#21 2484991-22.diff12.28 KBznerol
#21 interdiff-22.diff4.96 KBznerol
#19 interdiff-19.txt1.15 KBznerol
#19 2484991-19.diff7.32 KBznerol
#13 2484991-13.patch1.84 KBandypost
#19 interdiff-19.txt1.15 KBznerol
#19 2484991-19.diff7.32 KBznerol
#15 2484991-15.patch6.18 KBznerol

Issue fork drupal-2484991

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

dawehner’s picture

So should we setup a request by default with an empty session object inside $request?

@znerol
It would be great if you could explain why a blocker for a normal is critical.

znerol’s picture

Priority: Critical » Normal

So should we setup a request by default with an empty session object inside $request?

We already populate the request stack by default. We should probably try to just retrieve the session from the container and put it on the request.

why a blocker for a normal is critical.

Oh, that was not intended.

andypost’s picture

debuggin #2478167-18: Generate proper value for sessionName property in BrowserTestBase I found that child site reports incremental amount of cookies which grows with every next test* method

larowlan’s picture

Over in #2421503: SA-CORE-2014-002 forward port only checks internal cache we're trying to replace key/value storage of form state with session based and found that tests run from run-tests.sh don't always have the session on the request object (because php is cli) - would this help that?

andypost’s picture

Crell’s picture

Crazy idea: KeyValue is an interface, so for normal requests have the form state backed by a session-based key-value, while in test use a disk key/value. Interfaces are fun like that.

Version: 8.0.x-dev » 8.1.x-dev

Drupal 8.0.6 was released on April 6 and is the final bugfix release for the Drupal 8.0.x series. Drupal 8.0.x will not receive any further development aside from security fixes. Drupal 8.1.0-rc1 is now available and sites should prepare to update to 8.1.0.

Bug reports should be targeted against the 8.1.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.2.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.1.x-dev » 8.2.x-dev

Drupal 8.1.9 was released on September 7 and is the final bugfix release for the Drupal 8.1.x series. Drupal 8.1.x will not receive any further development aside from security fixes. Drupal 8.2.0-rc1 is now available and sites should prepare to upgrade to 8.2.0.

Bug reports should be targeted against the 8.2.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.3.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.2.x-dev » 8.3.x-dev

Drupal 8.2.6 was released on February 1, 2017 and is the final full bugfix release for the Drupal 8.2.x series. Drupal 8.2.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.3.0 on April 5, 2017. (Drupal 8.3.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.3.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.4.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.3.x-dev » 8.4.x-dev

Drupal 8.3.6 was released on August 2, 2017 and is the final full bugfix release for the Drupal 8.3.x series. Drupal 8.3.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.4.0 on October 4, 2017. (Drupal 8.4.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.4.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.5.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.4.x-dev » 8.5.x-dev

Drupal 8.4.4 was released on January 3, 2018 and is the final full bugfix release for the Drupal 8.4.x series. Drupal 8.4.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.5.0 on March 7, 2018. (Drupal 8.5.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.5.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.6 was released on August 1, 2018 and is the final bugfix release for the Drupal 8.5.x series. Drupal 8.5.x will not receive any further development aside from security fixes. Sites should prepare to update to 8.6.0 on September 5, 2018. (Drupal 8.6.0-rc1 is available for testing.)

Bug reports should be targeted against the 8.6.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

andypost’s picture

Version: 8.6.x-dev » 8.8.x-dev
Status: Active » Needs review
FileSize
1.84 KB

Maybe that will be enough, let's see how many tests will fail

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.0-alpha1 will be released the week of October 14th, 2019, which means new developments and disruptive changes should now be targeted against the 8.9.x-dev branch. (Any changes to 8.9.x will also be committed to 9.0.x in preparation for Drupal 9’s release, but some changes like significant feature additions will be deferred to 9.1.x.). For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

znerol’s picture

andypost’s picture

It looks ready to go!

jibran’s picture

What about WebTestBase as per IS and title? The patch only covers KTB

znerol’s picture

I think we should leave alone WebTestBase (that's the old thing from simpletest). Because that one is deprecated and will be removed from Drupal 9 anyway.

I guess I need to step through BrowserTestBase once in order to figure out how that thing is working. Depending on whether we still have the parent-site (runner) vs child-site (system-under-test) relationship or not, we might need different approaches on how the session needs to be wired into those components.

znerol’s picture

Now that KTB populates its request stack with a request pointing to a session associated with the in memory session manager, that request should now be used in test cases.

Note that it seems to be common practice to fabricate and use a separate request in some test cases. However, this can lead to weird results when some parts of the system-under-test fetch the request from the stack and some other parts work with the one passed in from the test case. This is not something which is changed with this patch. But as we convert more and more of the code base to use the symfony session exclusively and unconditionally, I expect that we will uncover quite some of those cases.

Hunk taken over and adapted from #2473875: Convert uses of $_SESSION to symfony session retrieved from the request, Use \Drupal::request() instead of a custom one in ReplicaKillSwitchTest.

znerol’s picture

If I'm not totally mistaken, browser tests have the same architecture like good old simpletest but with modern tools. The SUT runs as a separate Drupal instance which is newly installed for every test class. A test runner (which is either the browser or a test-script) then executes the test cases by running HTTP requests against the SUT and checking the results.

Since the SUT is a fully blown Drupal instance, the session is already available there. No change necessary.

Now the question is whether we need the session in the test-runner as well. It looks like the old Drupal\simpletest\TestBase had some code in place which prevented $_SESSION data set while running test cases from bleeding over into the test-runner itself. I have not found evidence of such protection anywhere inside core/tests directory. On the other hand I found that Drupal/Tests/UiHelperTrait.php provides a method drupalUserIsLoggedIn(). That one is calling directly into the session_handler.storage in order to determine whether there is an active session for a given user.

From the comments above it looks like people expect that the session is on the request in the test runner. I guess we should try to add a session associated with a mock session manager to the test-runner part of browser tests.

znerol’s picture

In BrowserTestBase the parent (or test-runner) shares the container with the child (SUT). Both parts share the exact same set of services. Hence it is not possible to mock the session_manager in the test runner.

Since SessionManager protects against calls into PHP session subsystem when run under CLI, it should be possible to make sessions available on requests in the test runner nevertheless.

znerol’s picture

Issue summary: View changes
znerol’s picture

Fix wrong variable name.

znerol’s picture

Title: Add the session to the request in KernelTestBase / WebTestBase » Add the session to the request in KernelTestBase / BrowserTestBase
znerol’s picture

Over in #3110291: Test-only issue for session on request in BrowserTestBase I realized, that there are quite some tests which fabricate their own request objects. Some of them put it on the stack others don't. Problem is that we need to populate the session on those requests as well. Thus let's introduce a helper Trait providing a function to retrieve the session from the current request on the request stack. We can reuse this method in KTB and BTB based tests.

znerol’s picture

Ensure that the session is restored in KTB and BTB base and test classes. Not touching unit tests (those should probably better mock the session object if necessary).

Status: Needs review » Needs work

The last submitted patch, 26: 2484991-26.diff, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

znerol’s picture

Status: Needs work » Needs review
FileSize
1.55 KB
52.28 KB

Fix copy paste / spelling problem.

znerol’s picture

Issue summary: View changes
FileSize
1.4 KB
52.57 KB

Cosmetic fixes. Issue summary update.

In my opinion this patch is now ready. I was tempted to also merge #3109600: Convert uses of $_SESSION in language module and #3109971: Use $this->request in ViewsExecutable::getExposedInput() consistently into this issue here. #3110291-12: Test-only issue for session on request in BrowserTestBase shows that this patch here fixes the remaining cases in views and language modules. However, the test-only issue also shows that in the case of the views issue, some non-trivial changes to the AreaDisplayLinkTest are necessary. Thus I think it is better to commit them separately.

jibran’s picture

Great! Let's ping @Berdir or @alexpott to review this.

alexpott’s picture

So there's plenty of change to all the tests here that's not truly necessary.

+++ b/core/modules/block_content/tests/src/Functional/BlockContentCacheTagsTest.php
@@ -83,8 +83,10 @@ public function testBlock() {
     //   https://www.drupal.org/node/2367555 is fixed and the
     //   corresponding $request->isMethodCacheable() checks are removed from
     //   Drupal\Core\Render\Renderer.
+    $request = new Request();
+    $request->setSession($this->getSessionFromRequest());
     $request_stack = $this->container->get('request_stack');
-    $request_stack->push(new Request());
+    $request_stack->push($request);
     $this->container->get('renderer')->renderRoot($build);
     $request_stack->pop();

If I remove this from the patch then the test is not going to fail. I have two concerns here which are kind of in opposition:

  • If the old code doesn't fail how is contrib and custom going to know how to "do it right"
  • If the old code did fail then this would be an extremely disruptive change - given that it doesn't fail is the new code really "doing it right" or is it being done for the appearance of correctness?

I think what I'm driving at is the why of

Problem is that we need to populate the session on those requests as well.

from #25 is not answered.

Also this patch needs to apply to 9.0.x and it doesn't.

znerol’s picture

I share your concerns. If you look at how it is currently used in non-test code, you find occurrences where a call to $request->getSession() is conditional and other parts where the session is assumed to be always on the request.

Example command to asses session use in production code:

find . -type f -a -not -iname '*session*' -a -not -ipath '*test*' -print0 | xargs -0 git grep -C2 -- '->getSession'

Unless someone alters away the Session middleware, the session is always on the request during the whole request-response cycle in production code. However, since this is not the case in KTB and the test-runner part of BTB (and in the early installer), some lower level code still must check for the presence of the session in order to avoid blowing up during test-runs.

If production code needs needs to conditionally check for the session only because it is not available in some parts of the test-system, then I do consider this as bug in the test-system.

Tracking down all cases where Requests are created during KTB und BTB was quite a tedious process. Reviewing these changes ist kind of boring as will. However, I think it is still the right thing to do. It clearly communicates the direction we are moving and shows the pattern we need to follow in the future. Also it shows how the new trait should be used by core and contrib test cases.

The alternative would be to only introduce the infrastructure (changes in KTB and BTB) (something like #23). If we do that, we either have to fix the test-cases in a follow-up or introduce the session only if required by a certain test (e.g., in [#3109971). Both approaches will result in an inconsistent state. Less inconsistent than now though.

Thus this is ultimately a question on whether we want to minimize changes or minimize inconsistency. Both approaches work for me.

znerol’s picture

Reroll.

znerol’s picture

Dear Mr./Ms. Jenkins. Why do try to apply this patch to 8.9.x? I kindly request you to checkout 9.0.x from now on.

Status: Needs review » Needs work

The last submitted patch, 34: 2484991-32.diff, failed testing. View results

Status: Needs review » Needs work

The last submitted patch, 36: 2484991-36.patch, failed testing. View results

Version: 9.1.x-dev » 9.2.x-dev

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

andypost’s picture

Issue tags: +Needs reroll
anmolgoyal74’s picture

Status: Needs work » Needs review
FileSize
52.63 KB
5.41 KB

Re-rolled for 9.2.x

markdorison’s picture

The patch in #40 is failing due to a cspell check on the use of MOCKSESSID in MockSessionManager. This could be resolved by either changin the MOCKSESSID string to pass the checks or by adding MOCKSESSID to cspell's dictionary.txt. I chose the latter since dictionary.txt already contains two similar strings: sess and ssess and modifying the string would require additional changes elsewhere. Patch and inter diff attached.

andypost’s picture

Status: Needs review » Needs work
ankithashetty’s picture

Status: Needs work » Needs review
Issue tags: -Needs reroll
FileSize
52.92 KB
9.67 KB

Rerolled the patch in #41 and fixed custom command error. Thanks!

alexpott’s picture

+++ b/core/tests/Drupal/KernelTests/Core/Session/MockSessionManager.php
@@ -0,0 +1,139 @@
+    $this->storage = new MockArraySessionStorage('MOCKSESSID', $metadata_bag);

How about using proper words here. ie. MOCK_SESSION_ID then we don't need to add something to the dictionary.

Status: Needs review » Needs work

The last submitted patch, 43: 2484991-43.patch, failed testing. View results

anmolgoyal74’s picture

Status: Needs work » Needs review
FileSize
52.76 KB
751 bytes

Changed MOCKSESSID to MOCK_SESSION_ID

Status: Needs review » Needs work

The last submitted patch, 46: 2484991-46.patch, failed testing. View results

znerol’s picture

Assigned: Unassigned » znerol
Status: Needs work » Needs review
FileSize
52.57 KB
1.19 KB

This is a fresh reroll from #29. I suspected that maybe some reroll between the last green patch and now went wrong. The only thing I found is ReplicaKillSwitchTest where a conflict was resolved incorrectly (see interdiff.txt). Other than that the patch is logically equivalent as #46. Thus, going to look at what changed between 8.9.x and 9.0.x wrt to functional tests.

Status: Needs review » Needs work

The last submitted patch, 48: 2484991-48.patch, failed testing. View results

znerol’s picture

Status: Needs work » Needs review
FileSize
767 bytes
52.57 KB

Interestingly I was not able to reproduce the successful run from #29 in 8.9.x. However, since I'm running PHP 7.3 locally, I had to update phpunit to 7 (Drupal 8.9.x at the time of the last successful test run still was on phpunit 6).

Drupal 9 uses phpunit 8. Since there were no other big changes to the test infrastructure on the Drupal side, I assume that changes in phpunit are causing the weird test failures. What I found is that a call @trigger_error("...", E_USER_ERROR) will lead to a situation where the phpunit subprocess fails to report anything (i.e., stdout remains empty).

Hence, let's change the trigger_error call into a E_USER_WARNING for now.

Status: Needs review » Needs work

The last submitted patch, 50: 2484991-50.patch, failed testing. View results

znerol’s picture

Status: Needs work » Needs review
FileSize
54.11 KB
2.38 KB

Fix a typo.

Also fix SessionManagerDestroyNoCliCheckTest. This test needs to operate on the production session_manager. Thus move it to another id before KernelTestBase::register() installs the mock session manager.

andypost’s picture

$this->container->get('session') the only trickery prevents me from rtbc but I see no other way (except some request factory)

znerol’s picture

$this->container->get('session') the only trickery prevents me from rtbc but I see no other way (except some request factory)

Do you mind elaborating on that? Maybe there is a better way.

andypost’s picture

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 52: 2484991-52.patch, failed testing. View results

benjifisher’s picture

Please do not ask the testbot to try again until #3207086: [HEAD BROKEN] Consistent failure in MonthDatePluginTest is fixed.

alexpott’s picture

Status: Needs work » Reviewed & tested by the community
alexpott’s picture

Status: Reviewed & tested by the community » Needs review

I still have the concerns I outlined in #31. IN #32 @znerol addresses this concerns directly and I agree that if production code only has to check for session's existence due to test code then that would be a bug but what about CLIs like drush and console. They don't have a session either so are we in danger of making things more difficult for them?

+++ b/core/tests/Drupal/Tests/RequestSessionTestTrait.php
@@ -0,0 +1,33 @@
+      @trigger_error('Failed to retrieve session from current request, request stack was modified during test.', E_USER_WARNING);

Is this likely to cause contrib / custom tests to start failing? Do we want this or should we deprecate and then start failing in D10?

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

andypost’s picture

Status: Needs review » Needs work

+1 to deprecate it in 9.5 and fail in 10.1

Version: 9.5.x-dev » 10.1.x-dev

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

andypost’s picture

Status: Needs work » Needs review
FileSize
10.79 KB
47.99 KB

Re-roll and looks mostly no changes needed for tests nowadays

PS: see interdiff - few tests removed $request->setSession($this->getSessionFromRequest()); but passed

andypost’s picture

bit more clean-up

andypost’s picture

andypost’s picture

andypost’s picture

znerol’s picture

Re #59

but what about CLIs like drush and console. They don't have a session either so are we in danger of making things more difficult for them?

We could add a session object backed by an in-memory store in DrupalKernel::preHandle(). This is sufficient for command line tools and tests. Web requests will be processed by \Drupal\Core\StackMiddleware\Session where the mock session is replaced by a session object backed with persistent storage and a real session handler.

This works with current drush php:script and the following snipped:

var_dump(\Drupal::request()->hasSession());

Status: Needs review » Needs work

The last submitted patch, 70: 2484991-add-the-session-70.patch, failed testing. View results

znerol’s picture

Status: Needs work » Needs review
FileSize
633 bytes
20.94 KB

Session stack middleware should avoid attempting to save a session it didn't start.

znerol’s picture

Re #65

PS: see interdiff - few tests removed $request->setSession($this->getSessionFromRequest()); but passed

That is expected. All those calls were added for consistency reasons. The goal in this issue is that every Request in core has a Session. We only can hope that people start copying the $request->setSession($this->getSessionFromRequest()); pattern if it is applied consistently throughout core.

znerol’s picture

Switching this issue to MR workflow. I'm probably going to test another idea in a separate branch as well.

Status: Needs review » Needs work

The last submitted patch, 72: 2484991-72.patch, failed testing. View results

andypost’s picture

Left few comments, primary is to fix deprecation message according standard https://www.drupal.org/about/core/policies/core-change-policies/drupal-d...

43 failed tests! Great job!

znerol’s picture

Title: Add the session to the request in KernelTestBase / BrowserTestBase » Add the session to the request in KernelTestBase, BrowserTestBase and drush
Issue summary: View changes
Status: Needs work » Needs review

Updated the issue summary in order to reflect the new approach.

andypost’s picture

Status: Needs review » Reviewed & tested by the community

Thanks, 👍 looks ready

voleger’s picture

Status: Reviewed & tested by the community » Needs work

I set it to Needs work due to unresolved threads in MR.

znerol’s picture

Status: Needs work » Needs review

Thanks. Extracted two methods for session initialization in order to better document their responsibility and purpose:

  • DrupalKernel::initializeEphemeralSession()
  • StackMiddleware\Session::initializePersistentSession()
voleger’s picture

Status: Needs review » Reviewed & tested by the community

All review comments are addressed.

voleger’s picture

voleger’s picture

bump

quietone’s picture

Status: Reviewed & tested by the community » Needs work
Issue tags: +Needs change record updates

The change record for this issue has no text. Adding tag and setting to NW.

znerol’s picture

Issue summary: View changes
Status: Needs work » Needs review
Issue tags: -Needs change record updates

Added the CR.

quietone’s picture

Status: Needs review » Needs work

Apologies to all for my rather terse comment.

@znerol, thanks for updating the CR.

quietone’s picture

Status: Needs work » Needs review

Setting this to NW was unintentional. Sorry for the noise.

andypost’s picture

Status: Needs review » Reviewed & tested by the community

CR looks good, thank you

znerol’s picture

All good @quietone, change records are essential! At least I hardly could keep up with core without them.

dww’s picture

MAINTAINERS.txt has this for the "base system" "subsystem" [sic]:

Base system
- ?
  1. It's weird that the "base system" is considered a "subsystem" (out of scope for this issue, obviously). 😅
  2. There is no dedicated subsystem maintainer for "base system". In this case, I think a "framework manager" review would be most appropriate.
  3. @alexpott already reviewed, but let's be explicit that a framework manager reviews this and signs off before commit.
dww’s picture

Status: Reviewed & tested by the community » Needs work

Doing a review of the MR. Found some issues. Will save that soon so they appear here, but downgrading to NW now...

dww’s picture

Status: Needs work » Needs review

Pushed a commit to fix all my own nits and the deprecation message typo. Also edited the CR to fix the same typo there. 😅 I'd resolve most of the threads I just opened (if I could). The two that should remain open until addressed are:

  1. https://git.drupalcode.org/project/drupal/-/merge_requests/3320#note_168867
  2. https://git.drupalcode.org/project/drupal/-/merge_requests/3320#note_168868
dww’s picture

WHOOPS! Holy cross-posting and cross-pushing, batman. 😢 Sorry, @andypost! Was trying to resolve my own stuff, didn't see you were already responding. Yikes. I'll stop now and let the dust settle before I do anything else... 🙏

andypost’s picture

dww’s picture

Per Slack chat w/ @andypost, we agreed a @todo/@see comment pointing to #2613044: Requests are pushed onto the request stack twice, popped once was worth adding to justify the switch to rebuildContainer()...

All new threads should now be resolved. This is ready for framework manager review, although I can't re-RTBC it now. 😅

dww’s picture

Posted in #needs-review-queue-initiative to solicit the framework manager review this needs, so tagging for that. 😉

dww’s picture

Re-queue'ed PHP 8.1 + MySQL 5.7 - random fail in

Ckeditor5.Drupal\Tests\ckeditor5\FunctionalJavascript\MediaTest

.

dww’s picture

Needs followup: Opened #3355243: cspell does not flag "ist" as a typo to deal with the cspell vs. "ist" weirdness.

andypost’s picture

Status: Needs review » Reviewed & tested by the community

I think it ready and now more polished

Version: 10.1.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

voleger’s picture

Status: Reviewed & tested by the community » Needs work

Needs to change branch in MR, and update versions in deprecation messages 10.1.0 -> 10.2.0

znerol’s picture

Status: Needs work » Needs review

Rebased on 11.x and adapted the deprecation messages.

daffie’s picture

Looks good to me.
The MR does what the IS states.
For me it is RTBC.

daffie’s picture

daffie’s picture

Status: Needs review » Reviewed & tested by the community

I forgot to change the status.

voleger’s picture

Wim Leers’s picture

This looks awesome. 👏 I could only find 3 language nits, for which I left suggestions.

voleger’s picture

applied suggestions. thanks

quietone’s picture

Status: Reviewed & tested by the community » Needs work
Issue tags: +Needs change record updates

Sorry folks, The change record has an an incorrect branch, version and example deprecation message. And the deprecation message in the MR needs a change to conform to coding standards. I have left suggestions in the MR.

voleger’s picture

Status: Needs work » Needs review
Issue tags: -Needs change record updates

Addressed #112

andypost’s picture

Status: Needs review » Reviewed & tested by the community

MR is green, text is fixed

znerol’s picture

Rerolled.

xjm’s picture

Title: Add the session to the request in KernelTestBase, BrowserTestBase and drush » Add the session to the request in KernelTestBase, BrowserTestBase, and drush

Raised this in committer Slack to hopefully get that needed FM feedback.

larowlan’s picture

Status: Reviewed & tested by the community » Needs review
Issue tags: -Needs framework manager review

Both Request::create and Request::createFromGlobals end up calling Request::createFromFactory

I wonder if our test setup trait could call \Symfony\Component\HttpFoundation\Request::setFactory which would add a factory method that could do the session setup for all of those instances. It won't help new Request but those should probably be using ::create or ::createFromGlobals anyway.

It would mean the size of the change here is much smaller, and the need for contrib/custom projects to make chances would be reduced.

We could mention in the CR that in most cases they should switch to ::create or ::createFromGlobals instead of using new Request and setting the session.

znerol’s picture

While maybe convenient, I think that #117 will create more problems than it solves.

In Drupal 7, application code modified the $_SESSION superglobal directly. Every piece of code which accessed it was guaranteed to work with the same data. However, using the $_SESSION directly breaks encapsulation and makes testing difficult. Missing test coverage was probably one of the reasons why #3260652: Feature "Remember the last selection" for views exposed filters doesn't work anymore went unnoticed for quite a long time.

In times where $_SESSION was used exclusively, the responsibility to populate and store its contents was SessionManager.php alone (or session.inc in D7). Now that almost all core code is refactored to use the Request::getSession(), there are additional code paths involved in passing data form the PHP session to the places where its used and back again.

Basically every piece of code which creates, clones, duplicates a request also has the responsibility to copy the session. It is true that many of those are located in test cases, but some can be found in production code paths as well. The latter have the potential to introduce subtle bugs if they do not propagate the session. When everybody used $_SESSION this wasn't an issue. Nowadays it is.

I fear that installing a request factory in the test system will hide those potential bugs. Code which fabricates a request in order to pass that into non-trivial code paths without propagating the session will seemingly behave correctly in tests (because of the request factory) and will produce invalid requests (i.e., lacking a session) in production.

needs-review-queue-bot’s picture

Status: Needs review » Needs work
FileSize
90 bytes

The Needs Review Queue Bot tested this issue. It no longer applies to Drupal core. Therefore, this issue status is now "Needs work".

This does not mean that the patch needs to be re-rolled or the MR rebased. Read the Issue Summary, the issue tags and the latest discussion here to determine what needs to be done.

Consult the Drupal Contributor Guide to find step-by-step guides for working with issues.

znerol’s picture

Status: Needs work » Needs review
larowlan’s picture

Thank you @znerol that is a very thorough and eloquent explanation of why the disruption is actually better.

I think we probably want to target 10.3.0 with this now, which will mean we need to update the deprecation message. I'll confirm with release managers.

daffie’s picture

Status: Needs review » Needs work

The MR needs to be updated for that this will go in 10.3 instead of 10.2

voleger’s picture

Status: Needs work » Needs review

Addressed #122

daffie’s picture

Status: Needs review » Reviewed & tested by the community

Back to RTBC.

needs-review-queue-bot’s picture

Status: Reviewed & tested by the community » Needs work
FileSize
90 bytes

The Needs Review Queue Bot tested this issue. It no longer applies to Drupal core. Therefore, this issue status is now "Needs work".

This does not mean that the patch needs to be re-rolled or the MR rebased. Read the Issue Summary, the issue tags and the latest discussion here to determine what needs to be done.

Consult the Drupal Contributor Guide to find step-by-step guides for working with issues.

dww’s picture

Status: Needs work » Needs review

Hah, whoops, cross rebase. 😅 But no harm done.

smustgrave’s picture

Status: Needs review » Reviewed & tested by the community

Appear all threads have been addressed and no tests appear to be breaking.

CR is full of detail (branch versions need to be bumped)

dww’s picture

Edited the CR to bump the versions to 10.3.0.

  • larowlan committed 48a7cf12 on 11.x
    Issue #2484991 by znerol, voleger, andypost, dww, anmolgoyal74,...
larowlan’s picture

Status: Reviewed & tested by the community » Fixed

Went through this again, only found one out of scope change, that I reverted locally before committing.

@alexpott's concerns in #31, #32 and #59 were addressed by @znerol by a change of approach.

My point in #117 was addressed in #118

We know this could be disruptive so I think getting it in early in the 10.3 cycle is the best approach.

So on that basis, committed and pushed to 11.x

Published the change record.

Thanks everyone for the mammoth effort over a prolonged period here.

Special thanks to @znerol for your patient and thorough explanations when required.

  • larowlan committed 7393eba2 on 11.x
    Revert "Issue #2484991 by znerol, voleger, andypost, dww, anmolgoyal74,...

larowlan’s picture

Status: Fixed » Needs work

Sorry folks, I had to revert this as it caused failure in HEAD

Looks like there's a new test that creates a Request but doesn't push a session

See \Drupal\Tests\file\Kernel\RequirementsTest::setServerSoftware

Fine to self RTBC

I'll keep an eye out for changes

znerol’s picture

Assigned: Unassigned » znerol
Status: Needs work » Reviewed & tested by the community

Fine to self RTBC

Thanks @larowlan. Done.

znerol’s picture

Status: Reviewed & tested by the community » Needs work

Oh, I might have overwritten work done by @voleger and @dww by basing off my changes on an outdated local branch. Let me fix that.

znerol’s picture

Status: Needs work » Reviewed & tested by the community

MR should be up-to-date now.

  • larowlan committed fbee2baa on 11.x
    Issue #2484991 by znerol, voleger, andypost, dww, anmolgoyal74,...
larowlan’s picture

Status: Reviewed & tested by the community » Fixed

Thanks, compared the two patches locally and confirmed the only difference was this

diff --git a/core/modules/file/tests/src/Kernel/RequirementsTest.php b/core/modules/file/tests/src/Kernel/RequirementsTest.php
index 4d472644dc4..224616e089b 100644
--- a/core/modules/file/tests/src/Kernel/RequirementsTest.php
+++ b/core/modules/file/tests/src/Kernel/RequirementsTest.php
@@ -7,6 +7,8 @@
 use Drupal\KernelTests\KernelTestBase;
 use Symfony\Component\HttpFoundation\Request;
 use Symfony\Component\HttpFoundation\RequestStack;
+use Symfony\Component\HttpFoundation\Session\Session;
+use Symfony\Component\HttpFoundation\Session\Storage\MockArraySessionStorage;
 
 /**
  * Tests the file requirements.
@@ -69,6 +71,7 @@ public function testUploadRequirements(): void {
    */
   private function setServerSoftware(?string $software): void {
     $request = new Request();
+    $request->setSession(new Session(new MockArraySessionStorage()));
     if (is_string($software)) {
       $request->server->set('SERVER_SOFTWARE', $software);
     }

Committed and pushed to 11.x, re-published the change record.

Thanks for the quick turnaround

andypost’s picture

Thank you! Removal of global session remains now unblocked #3109970: Convert uses of $_SESSION in forms and batch

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.