Skip to content

Commit

Permalink
MDL-46778 behat: Allow different database for behat
Browse files Browse the repository at this point in the history
  • Loading branch information
odeialba committed Sep 2, 2021
1 parent 1464843 commit 15c7a4b
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 12 deletions.
3 changes: 3 additions & 0 deletions admin/tool/behat/cli/util.php
Expand Up @@ -19,6 +19,9 @@
*
* All CLI utilities uses $CFG->behat_dataroot and $CFG->prefix_dataroot as
* $CFG->dataroot and $CFG->prefix
* Same applies for $CFG->behat_dbname, $CFG->behat_dbuser, $CFG->behat_dbpass
* and $CFG->behat_dbhost. But if any of those is not defined $CFG->dbname,
* $CFG->dbuser, $CFG->dbpass and/or $CFG->dbhost will be used.
*
* @package tool_behat
* @copyright 2012 David Monllaó
Expand Down
3 changes: 3 additions & 0 deletions admin/tool/behat/cli/util_single_run.php
Expand Up @@ -19,6 +19,9 @@
*
* All CLI utilities uses $CFG->behat_dataroot and $CFG->prefix_dataroot as
* $CFG->dataroot and $CFG->prefix
* Same applies for $CFG->behat_dbname, $CFG->behat_dbuser, $CFG->behat_dbpass
* and $CFG->behat_dbhost. But if any of those is not defined $CFG->dbname,
* $CFG->dbuser, $CFG->dbpass and/or $CFG->dbhost will be used.
*
* @package tool_behat
* @copyright 2012 David Monllaó
Expand Down
2 changes: 1 addition & 1 deletion admin/tool/behat/lang/en/tool_behat.php
Expand Up @@ -29,7 +29,7 @@
$string['errorcomposer'] = 'Composer dependencies are not installed.';
$string['errordataroot'] = '$CFG->behat_dataroot is not set or is invalid.';
$string['errorsetconfig'] = '$CFG->behat_dataroot, $CFG->behat_prefix and $CFG->behat_wwwroot need to be set in config.php.';
$string['erroruniqueconfig'] = '$CFG->behat_dataroot, $CFG->behat_prefix and $CFG->behat_wwwroot values need to be different than $CFG->dataroot, $CFG->prefix, $CFG->wwwroot, $CFG->phpunit_dataroot and $CFG->phpunit_prefix values.';
$string['erroruniqueconfig'] = '$CFG->behat_dataroot, $CFG->behat_prefix and $CFG->behat_wwwroot values need to be different than $CFG->dataroot, $CFG->prefix, $CFG->wwwroot, $CFG->phpunit_dataroot and $CFG->phpunit_prefix values.<br/>Or, if $CFG->behat_prefix is the same, $CFG->behat_dbname or $CFG->behat_dbhost need to be different from $CFG->phpunit_dbname and $CFG->phpunit_dbhost and from $CFG->dbname and $CFG->dbhost.';
$string['fieldvalueargument'] = 'Field value arguments';
$string['fieldvalueargument_help'] = 'This argument should be completed by a field value. There are many field types, including simple ones like checkboxes, selects or textareas, or complex ones like date selectors. See the dev documentation <a href="https://docs.moodle.org/dev/Acceptance_testing" target="_blank">Acceptance_testing</a> for details of expected field values.';
$string['giveninfo'] = 'Given. Processes to set up the environment';
Expand Down
4 changes: 4 additions & 0 deletions config-dist.php
Expand Up @@ -880,6 +880,10 @@
// $CFG->behat_wwwroot = 'http://127.0.0.1/moodle';
// $CFG->behat_prefix = 'bht_';
// $CFG->behat_dataroot = '/home/example/bht_moodledata';
// $CFG->behat_dbname = 'behat'; // optional
// $CFG->behat_dbuser = 'username'; // optional
// $CFG->behat_dbpass = 'password'; // optional
// $CFG->behat_dbhost = 'localhost'; // optional
//
// You can override default Moodle configuration for Behat and add your own
// params; here you can add more profiles, use different Mink drivers than Selenium...
Expand Down
20 changes: 13 additions & 7 deletions lib/behat/classes/behat_command.php
Expand Up @@ -203,13 +203,19 @@ public static function behat_setup_problem() {
// We only need to check this when the behat site is not running as
// at this point, when it is running, all $CFG->behat_* vars have
// already been copied to $CFG->dataroot, $CFG->prefix and $CFG->wwwroot.
if (!defined('BEHAT_SITE_RUNNING') &&
($CFG->behat_prefix == $CFG->prefix ||
$CFG->behat_dataroot == $CFG->dataroot ||
$CFG->behat_wwwroot == $CFG->wwwroot ||
(!empty($CFG->phpunit_prefix) && $CFG->phpunit_prefix == $CFG->behat_prefix) ||
(!empty($CFG->phpunit_dataroot) && $CFG->phpunit_dataroot == $CFG->behat_dataroot)
)) {
$phpunitprefix = empty($CFG->phpunit_prefix) ? '' : $CFG->phpunit_prefix;
$behatdbname = empty($CFG->behat_dbname) ? $CFG->dbname : $CFG->behat_dbname;
$phpunitdbname = empty($CFG->phpunit_dbname) ? $CFG->dbname : $CFG->phpunit_dbname;
$behatdbhost = empty($CFG->behat_dbhost) ? $CFG->dbhost : $CFG->behat_dbhost;
$phpunitdbhost = empty($CFG->phpunit_dbhost) ? $CFG->dbhost : $CFG->phpunit_dbhost;

$samedataroot = $CFG->behat_dataroot == $CFG->dataroot;
$samedataroot = $samedataroot || (!empty($CFG->phpunit_dataroot) && $CFG->phpunit_dataroot == $CFG->behat_dataroot);
$samewwwroot = $CFG->behat_wwwroot == $CFG->wwwroot;
$sameprefix = ($CFG->behat_prefix == $CFG->prefix && $behatdbname == $CFG->dbname && $behatdbhost == $CFG->dbhost);
$sameprefix = $sameprefix || ($CFG->behat_prefix == $phpunitprefix && $behatdbname == $phpunitdbname &&
$behatdbhost == $phpunitdbhost);
if (!defined('BEHAT_SITE_RUNNING') && ($samedataroot || $samewwwroot || $sameprefix)) {
self::output_msg(get_string('erroruniqueconfig', 'tool_behat'));
return BEHAT_EXITCODE_CONFIG;
}
Expand Down
21 changes: 17 additions & 4 deletions lib/behat/lib.php
Expand Up @@ -243,18 +243,31 @@ function behat_clean_init_config() {
function behat_check_config_vars() {
global $CFG;

$moodleprefix = empty($CFG->prefix) ? '' : $CFG->prefix;
$behatprefix = empty($CFG->behat_prefix) ? '' : $CFG->behat_prefix;
$phpunitprefix = empty($CFG->phpunit_prefix) ? '' : $CFG->phpunit_prefix;
$behatdbname = empty($CFG->behat_dbname) ? $CFG->dbname : $CFG->behat_dbname;
$phpunitdbname = empty($CFG->phpunit_dbname) ? $CFG->dbname : $CFG->phpunit_dbname;
$behatdbhost = empty($CFG->behat_dbhost) ? $CFG->dbhost : $CFG->behat_dbhost;
$phpunitdbhost = empty($CFG->phpunit_dbhost) ? $CFG->dbhost : $CFG->phpunit_dbhost;

// Verify prefix value.
if (empty($CFG->behat_prefix)) {
behat_error(BEHAT_EXITCODE_CONFIG,
'Define $CFG->behat_prefix in config.php');
}
if (!empty($CFG->prefix) and $CFG->behat_prefix == $CFG->prefix) {
if ($behatprefix == $moodleprefix && $behatdbname == $CFG->dbname && $behatdbhost == $CFG->dbhost) {
behat_error(BEHAT_EXITCODE_CONFIG,
'$CFG->behat_prefix in config.php must be different from $CFG->prefix');
'$CFG->behat_prefix in config.php must be different from $CFG->prefix' .
' when $CFG->behat_dbname and $CFG->behat_host are not set or when $CFG->behat_dbname equals $CFG->dbname' .
' and $CFG->behat_dbhost equals $CFG->dbhost');
}
if (!empty($CFG->phpunit_prefix) and $CFG->behat_prefix == $CFG->phpunit_prefix) {
if ($phpunitprefix !== '' && $behatprefix == $phpunitprefix && $behatdbname == $phpunitdbname &&
$behatdbhost == $phpunitdbhost) {
behat_error(BEHAT_EXITCODE_CONFIG,
'$CFG->behat_prefix in config.php must be different from $CFG->phpunit_prefix');
'$CFG->behat_prefix in config.php must be different from $CFG->phpunit_prefix' .
' when $CFG->behat_dbname equals $CFG->phpunit_dbname' .
' and $CFG->behat_dbhost equals $CFG->phpunit_dbhost');
}

// Verify behat wwwroot value.
Expand Down
10 changes: 10 additions & 0 deletions lib/setup.php
Expand Up @@ -141,6 +141,16 @@
$CFG->wwwroot = $CFG->behat_wwwroot;
$CFG->prefix = $CFG->behat_prefix;
$CFG->dataroot = $CFG->behat_dataroot;

// And we do the same with the optional ones.
$allowedconfigoverride = ['dbname', 'dbuser', 'dbpass', 'dbhost'];
foreach ($allowedconfigoverride as $config) {
$behatconfig = 'behat_' . $config;
if (!isset($CFG->$behatconfig)) {
continue;
}
$CFG->$config = $CFG->$behatconfig;
}
}
}

Expand Down

0 comments on commit 15c7a4b

Please sign in to comment.