From c9cb89323cfbb4dbf94abfbae65a8911dd6bec58 Mon Sep 17 00:00:00 2001 From: meirzamoodle Date: Wed, 10 Apr 2024 13:49:55 +0700 Subject: [PATCH] MDL-81480 core: Sets user-level session storage functions Also added @runInSeparateProcess on each session Redis cluster test to let the test run in a separate process to avoid the error "the headers are not already sent" by PHPUnit. --- lang/en/error.php | 1 + lib/classes/session/redis.php | 5 +++++ lib/tests/session_redis_cluster_test.php | 2 ++ 3 files changed, 8 insertions(+) diff --git a/lang/en/error.php b/lang/en/error.php index b571d2851a9..c7b1e12ed88 100644 --- a/lang/en/error.php +++ b/lang/en/error.php @@ -511,6 +511,7 @@ $string['privatefilesupload'] = 'Uploading directly to user private files area i $string['processingstops'] = 'Processing stops here. Remaining records ignored.'; $string['querystringcannotbeempty'] = 'The query string cannot be empty.'; $string['redirecterrordetected'] = 'Unsupported redirect detected, script execution terminated'; +$string['redissessionhandlerproblem'] = 'Setting up of redis session failed. Please notify the server administrator.'; $string['refoundto'] = 'Can be refunded to {$a}'; $string['refoundtoorigi'] = 'Refunded to original amount: {$a}'; $string['registrationdisabled'] = 'Registration is disabled on this site'; diff --git a/lib/classes/session/redis.php b/lib/classes/session/redis.php index c3614a4a113..93930adcbfe 100644 --- a/lib/classes/session/redis.php +++ b/lib/classes/session/redis.php @@ -213,6 +213,11 @@ class redis extends handler implements SessionHandlerInterface { throw new exception('sessionhandlerproblem', 'error', '', null, 'redis extension version must be at least 2.0'); } + $result = session_set_save_handler($this); + if (!$result) { + throw new exception('redissessionhandlerproblem', 'error'); + } + $encrypt = (bool) ($this->sslopts ?? false); // Set Redis server(s). $trimmedservers = []; diff --git a/lib/tests/session_redis_cluster_test.php b/lib/tests/session_redis_cluster_test.php index 515e51ef129..826160d0cfe 100644 --- a/lib/tests/session_redis_cluster_test.php +++ b/lib/tests/session_redis_cluster_test.php @@ -68,6 +68,7 @@ class session_redis_cluster_test extends \advanced_testcase { * * It covers the behavior of session read and write operations under different compression configurations. * + * @runInSeparateProcess * @covers ::read * @covers ::write */ @@ -85,6 +86,7 @@ class session_redis_cluster_test extends \advanced_testcase { * It sets up the environment to simulate multiple failed connection attempts and * checks if the expected exception message is received. * + * @runInSeparateProcess * @covers ::init */ public function test_exception_when_connection_attempts_exceeded(): void { -- 2.11.4.GIT