Skip to content

Commit

Permalink
MDL-66836 Behat: Reset unknown config variables between scenarios
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
sammarshallou committed Oct 9, 2019
1 parent 31f8f75 commit 3816bd8
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
18 changes: 18 additions & 0 deletions lib/behat/classes/util.php
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,23 @@ public final static function get_test_file_path() {
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.
*/
Expand Down Expand Up @@ -375,6 +392,7 @@ public static function reset_all_data() {

// 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();
}

Expand Down
8 changes: 8 additions & 0 deletions lib/moodlelib.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 3816bd8

Please sign in to comment.