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 dc8b0b0d547..c8341c5865b 100644 --- a/lib/moodlelib.php +++ b/lib/moodlelib.php @@ -1405,6 +1405,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);