From 3816bd8776e1190b5f2f0dad0bff15ee48c1f1db Mon Sep 17 00:00:00 2001 From: sam marshall Date: Thu, 3 Oct 2019 14:24:11 +0100 Subject: [PATCH] MDL-66836 Behat: Reset unknown config variables between scenarios In the Behat CLI run (not in the individual web requests), if you modified an existing $CFG variable then this would be reset at the start of the next scenario. However, if you added a completely new one, it would not be reset. This change removes those variables. --- lib/behat/classes/util.php | 18 ++++++++++++++++++ lib/moodlelib.php | 8 ++++++++ 2 files changed, 26 insertions(+) diff --git a/lib/behat/classes/util.php b/lib/behat/classes/util.php index 1988e015593..5338be1c7f6 100644 --- a/lib/behat/classes/util.php +++ b/lib/behat/classes/util.php @@ -345,6 +345,23 @@ class behat_util extends testing_util { return behat_command::get_parent_behat_dir() . '/test_environment_enabled.txt'; } + /** + * Removes config settings that were added to the main $CFG config within the Behat CLI + * run. + * + * Database storage is already handled by reset_database and existing config values will + * be reset automatically by initialise_cfg(), so we only need to remove added ones. + */ + public static function remove_added_config() { + global $CFG; + if (!empty($CFG->behat_cli_added_config)) { + foreach ($CFG->behat_cli_added_config as $key => $value) { + unset($CFG->{$key}); + } + unset($CFG->behat_cli_added_config); + } + } + /** * Reset contents of all database tables to initial values, reset caches, etc. */ @@ -375,6 +392,7 @@ class behat_util extends testing_util { // Initialise $CFG with default values. This is needed for behat cli process, so we don't have modified // $CFG values from the old run. @see set_config. + self::remove_added_config(); initialise_cfg(); } diff --git a/lib/moodlelib.php b/lib/moodlelib.php index 2363dcb980f..91f9ce483ec 100644 --- a/lib/moodlelib.php +++ b/lib/moodlelib.php @@ -1412,6 +1412,14 @@ function set_config($name, $value, $plugin=null) { $config->value = $value; $DB->insert_record('config', $config, false); } + // When setting config during a Behat test (in the CLI script, not in the web browser + // requests), remember which ones are set so that we can clear them later. + if (defined('BEHAT_TEST')) { + if (!property_exists($CFG, 'behat_cli_added_config')) { + $CFG->behat_cli_added_config = []; + } + $CFG->behat_cli_added_config[$name] = true; + } } if ($name === 'siteidentifier') { cache_helper::update_site_identifier($value);