fix(Session): clear session data on destroy in testing environment#10342
fix(Session): clear session data on destroy in testing environment#10342gr8man wants to merge 2 commits into
Conversation
michalsn
left a comment
There was a problem hiding this comment.
Sorry, but no. session_destroy() does not unset $_SESSION: https://www.php.net/manual/en/function.session-destroy.php
Clearing it only in the testing environment makes Session::destroy() behave differently depending on the environment.
Additionally, CIUnitTestCase::mockSession() already resets $_SESSION during test setup.
|
Thanks for the review! I believe we should keep this change for the following reasons: Immediate Assertions: Without clearing $_SESSION immediately, we cannot easily assert if the logout operation was actually successful within the same test method. Simulating Real Flow: In production, destroy() is usually followed by a redirect, meaning the next request's $_SESSION is empty. Since CLI tests don't do real redirects, we need to clear it manually to simulate that post-logout state. State Leakage in Complex Tests: Even with mockSession() in setUp(), if a single test method simulates multiple actions (e.g., logging out User A and then logging in User B), failing to clear the array causes state leakage between those actions. Mock Usability: A test mock should help us verify application logic (that a session was successfully destroyed) rather than strictly mimicking the annoying limitations of PHP's native session_destroy(). |
In production,
A redirect does not start the next request. It only tells the browser where to go.
Logging out one user and logging in another is authentication-level behavior, not merely session destruction. For example, Shield's logout implementation removes authentication data, regenerates the session ID, clears remember-me tokens, resets its internal authentication state, and intentionally keeps the session available for flash messages. An empty
Test doubles should avoid external side effects, but their observable behavior should still match production. Otherwise, tests can pass for behavior that does not work in the application. Also, this changes |
Description
In the
testingenvironment,Session::destroy()returned early without callingsession_destroy(). While avoiding raw PHP session functions in CLI/testing is expected, doing nothing meant that the mock session data stored in the$_SESSIONsuperglobal remained completely intact. This caused session state to leak between test cases, leading to test pollution.This PR addresses the issue by resetting the
$_SESSIONarray ($_SESSION = []) whendestroy()is called under thetestingenvironment. A unit test has also been added to verify this behavior.Checklist: