From c56c08e9ce6758c1197ba40e0c868cdcfeeb2286 Mon Sep 17 00:00:00 2001 From: sam marshall Date: Wed, 7 May 2025 15:18:51 +0100 Subject: [PATCH] MDL-85400 PHPunit: Hard to test code that uses encryption This change stops the encryption code setting created key files to read-only during PHPunit runs, which means they will be safely deleted when the system resets after a test. --- lib/classes/encryption.php | 4 ++-- lib/tests/encryption_test.php | 18 ------------------ 2 files changed, 2 insertions(+), 20 deletions(-) diff --git a/lib/classes/encryption.php b/lib/classes/encryption.php index aff8599e4e5..436d5e2ac5a 100644 --- a/lib/classes/encryption.php +++ b/lib/classes/encryption.php @@ -84,8 +84,8 @@ class encryption { throw new \moodle_exception('encryption_keyalreadyexists', 'error'); } - // Don't make it read-only in Behat or it will fail to clear for future runs. - if (defined('BEHAT_SITE_RUNNING')) { + // Don't make it read-only in tests or it will fail to clear for future runs. + if (defined('BEHAT_SITE_RUNNING') || PHPUNIT_TEST) { $chmod = false; } diff --git a/lib/tests/encryption_test.php b/lib/tests/encryption_test.php index 290a8f0413e..8a0932624c8 100644 --- a/lib/tests/encryption_test.php +++ b/lib/tests/encryption_test.php @@ -32,26 +32,8 @@ namespace core; * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ final class encryption_test extends \advanced_testcase { - /** - * Clear junk created by tests. - */ - protected function tearDown(): void { - global $CFG; - $keyfile = encryption::get_key_file(encryption::METHOD_OPENSSL); - if (file_exists($keyfile)) { - chmod($keyfile, 0700); - } - $keyfile = encryption::get_key_file(encryption::METHOD_SODIUM); - if (file_exists($keyfile)) { - chmod($keyfile, 0700); - } - remove_dir($CFG->dataroot . '/secret'); - unset($CFG->nokeygeneration); - } protected function setUp(): void { - $this->tearDown(); - require_once(__DIR__ . '/fixtures/testable_encryption.php'); }