diff --git a/lib/phpunit/bootstrap.php b/lib/phpunit/bootstrap.php index dd15d1bb189..c7cf470686c 100644 --- a/lib/phpunit/bootstrap.php +++ b/lib/phpunit/bootstrap.php @@ -205,6 +205,20 @@ ini_set('log_errors', '1'); $CFG->themerev = 1; $CFG->jsrev = 1; +(function () { + // Determine if this test is being run with isolation. + // This is tricky because neither PHPUnit, nor PHP provide an official way to work this out. + // PHPUnit does set a value, but not until later on and we need this earlier. + // PHPUnit runs isolated tests by creating a class on the fly and running it through proc_open as standard input. + // There is no other legitimate reason to run PHPUnit this way that I'm aware of. + // When run in this way, PHP sets the value of $_SERVER['PHP_SELF'] to "Standard input code". + // It has done this since 2016, and it is unlikely to change. + define( + 'PHPUNIT_ISOLATED_TEST', + $_SERVER['PHP_SELF'] === 'Standard input code', + ); +})(); + // load test case stub classes and other stuff require_once("$CFG->dirroot/lib/phpunit/lib.php"); diff --git a/lib/phpunit/classes/advanced_testcase.php b/lib/phpunit/classes/advanced_testcase.php index 25ceef15107..eb7a9ae1a24 100644 --- a/lib/phpunit/classes/advanced_testcase.php +++ b/lib/phpunit/classes/advanced_testcase.php @@ -60,19 +60,6 @@ abstract class advanced_testcase extends base_testcase { } - /** - * Hook into the setInIsolation method to define an optional constant. - * - * @param bool $inisolation - */ - public function setInIsolation(bool $inisolation): void { - parent::setInIsolation($inisolation); - if ($inisolation) { - // Note: This is safe to do because it will only be set once per test run. - define('PHPUNIT_ISOLATED_TEST', true); - } - } - /** * Runs the bare test sequence. * @return void diff --git a/lib/phpunit/classes/util.php b/lib/phpunit/classes/util.php index fc131dc2614..0b0aec7eae6 100644 --- a/lib/phpunit/classes/util.php +++ b/lib/phpunit/classes/util.php @@ -320,7 +320,13 @@ class phpunit_util extends testing_util { public static function reset_database() { global $DB; - if (!is_null(self::$lastdbwrites) and self::$lastdbwrites == $DB->perf_get_writes()) { + if (defined('PHPUNIT_ISOLATED_TEST') && PHPUNIT_ISOLATED_TEST && self::$lastdbwrites === null) { + // This is an isolated test and the lastdbwrites has not yet been initialised. + // Isolated test runs are reset by the test runner before the run starts. + self::$lastdbwrites = $DB->perf_get_writes(); + } + + if (!is_null(self::$lastdbwrites) && self::$lastdbwrites == $DB->perf_get_writes()) { return false; } diff --git a/lib/setuplib.php b/lib/setuplib.php index 0a5af63d64e..47cc23030a6 100644 --- a/lib/setuplib.php +++ b/lib/setuplib.php @@ -2202,7 +2202,7 @@ function require_phpunit_isolation(): void { return; } - if (defined('PHPUNIT_ISOLATED_TEST')) { + if (defined('PHPUNIT_ISOLATED_TEST') && PHPUNIT_ISOLATED_TEST) { // Already isolated. return; }