From 68959e336d60a2bb5e91bda6521a32bfc903fa50 Mon Sep 17 00:00:00 2001 From: djarrancotleanu Date: Fri, 22 Nov 2024 15:36:27 +1000 Subject: [PATCH 1/2] Fix #350: Do not setcookie during unit tests --- classes/local/outagelib.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/classes/local/outagelib.php b/classes/local/outagelib.php index 94638fb..5386859 100644 --- a/classes/local/outagelib.php +++ b/classes/local/outagelib.php @@ -311,8 +311,9 @@ public static function create_climaintenancephp_code($starttime, $stoptime, $all } // Put access key as a cookie if given. This stops the need to put it as a url param on every request. $urlaccesskey = optional_param('accesskey', null, PARAM_TEXT); + $isphpunit = defined('PHPUNIT_TEST'); - if (!empty($urlaccesskey)) { + if (!empty($urlaccesskey) && !$isphpunit) { setcookie('auth_outage_accesskey', $urlaccesskey, time() + 86400, '/', '', {{COOKIESECURE}}, {{COOKIEHTTPONLY}}); } @@ -322,7 +323,6 @@ public static function create_climaintenancephp_code($starttime, $stoptime, $all $ipblocked = !remoteip_in_list('{{ALLOWEDIPS}}'); $accesskeyblocked = $useraccesskey != '{{ACCESSKEY}}'; $allowed = ({{USEACCESSKEY}} && !$accesskeyblocked) || ({{USEALLOWEDIPS}} && !$ipblocked); - $isphpunit = defined('PHPUNIT_TEST'); if (!$allowed) { if (!$isphpunit) { From 2a55769a860de9db01be5f8831724e89dd436c8e Mon Sep 17 00:00:00 2001 From: djarrancotleanu Date: Wed, 27 Nov 2024 09:55:51 +1000 Subject: [PATCH 2/2] Fix #350: Update unit tests with changes made to climaintenance.php --- tests/local/outagelib_test.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/local/outagelib_test.php b/tests/local/outagelib_test.php index 9337cec..73b536b 100644 --- a/tests/local/outagelib_test.php +++ b/tests/local/outagelib_test.php @@ -323,8 +323,9 @@ public function test_createmaintenancephpcode() { } // Put access key as a cookie if given. This stops the need to put it as a url param on every request. $urlaccesskey = optional_param('accesskey', null, PARAM_TEXT); + $isphpunit = defined('PHPUNIT_TEST'); - if (!empty($urlaccesskey)) { + if (!empty($urlaccesskey) && !$isphpunit) { setcookie('auth_outage_accesskey', $urlaccesskey, time() + 86400, '/', '', true, false); } @@ -336,7 +337,6 @@ public function test_createmaintenancephpcode() { e.e.e.e/20'); $accesskeyblocked = $useraccesskey != '12345'; $allowed = (true && !$accesskeyblocked) || (true && !$ipblocked); - $isphpunit = defined('PHPUNIT_TEST'); if (!$allowed) { if (!$isphpunit) { @@ -404,8 +404,9 @@ public function test_createmaintenancephpcode_withoutage($configkey) { } // Put access key as a cookie if given. This stops the need to put it as a url param on every request. $urlaccesskey = optional_param('accesskey', null, PARAM_TEXT); + $isphpunit = defined('PHPUNIT_TEST'); - if (!empty($urlaccesskey)) { + if (!empty($urlaccesskey) && !$isphpunit) { setcookie('auth_outage_accesskey', $urlaccesskey, time() + 86400, '/', '', true, false); } @@ -415,7 +416,6 @@ public function test_createmaintenancephpcode_withoutage($configkey) { $ipblocked = !remoteip_in_list('127.0.0.1'); $accesskeyblocked = $useraccesskey != '5678'; $allowed = (true && !$accesskeyblocked) || (true && !$ipblocked); - $isphpunit = defined('PHPUNIT_TEST'); if (!$allowed) { if (!$isphpunit) {