From 99a6cd12b2b2bedde24170e217e2987fde211ac8 Mon Sep 17 00:00:00 2001 From: Andrew Nicols Date: Thu, 21 Sep 2023 23:46:22 +0800 Subject: [PATCH] MDL-79637 phpunit: Do not reset DB during init of isolated tests During the bootstrap of PHPUnit we ensure that the database has been reset to its initial state. We do this by checking the internally-stored DB write count between runs. If the count is not yet set (null), or it has been increased, we force a reset. When running an isolated test the test runner resets the database, it then sets up a new isolated test environment by writing a new PHPUnit test case and passing it to a new PHP Process using standard in. As part of this, the bootstrap is run for that process. Because we are in a new process, the db write count is fresh and not yet set. This has been leading to an additional db reset before the isolated test. To handle this we want to _not_ perform a reset during the initialisation for isolated runs. We know that the DB is in a fresh state before we start the run. To support this we need to know whether the test is an isolated test during the bootstrap, which means we cannot use the previous approach to calculating this. Instead we look at the PHP_SELF value. PHP sets this to "Standard input code" when run from stdin, instead of running a file. There should not be any other legitimate reason to run a PHPUnit bootstrap via this stdin approach. Unfortunately this approach is a little bit risky as it depends on the presence of a specific string, however this string has been in place since 2016, and there is no legitimate way of calculating this. I did consider looking at whether the called script included `/vendor/` and `/phpunit`, but this is also likely a risky approach if someone calls PHPUnit in an unexpected way. This approach is itself unit tested so any change to PHP's stdin string before we deprecate this approach entirely in 12 months time will be caught. --- lib/phpunit/bootstrap.php | 14 ++++++++++++++ lib/phpunit/classes/advanced_testcase.php | 13 ------------- lib/phpunit/classes/util.php | 8 +++++++- lib/setuplib.php | 2 +- 4 files changed, 22 insertions(+), 15 deletions(-) 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; }