diff --git a/admin/tool/behat/cli/util.php b/admin/tool/behat/cli/util.php
index e5f5adeafd3..8b39c7ce22e 100644
--- a/admin/tool/behat/cli/util.php
+++ b/admin/tool/behat/cli/util.php
@@ -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ó
diff --git a/admin/tool/behat/cli/util_single_run.php b/admin/tool/behat/cli/util_single_run.php
index 868dd84b3e5..5727e5b8f12 100644
--- a/admin/tool/behat/cli/util_single_run.php
+++ b/admin/tool/behat/cli/util_single_run.php
@@ -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ó
diff --git a/admin/tool/behat/lang/en/tool_behat.php b/admin/tool/behat/lang/en/tool_behat.php
index 01c607d11dd..f78896a0b0b 100644
--- a/admin/tool/behat/lang/en/tool_behat.php
+++ b/admin/tool/behat/lang/en/tool_behat.php
@@ -29,7 +29,7 @@ $string['errorbehatcommand'] = 'Error running behat CLI command. Try running "{$
$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.
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 Acceptance_testing for details of expected field values.';
$string['giveninfo'] = 'Given. Processes to set up the environment';
diff --git a/config-dist.php b/config-dist.php
index 70af810e684..317fd9543ca 100644
--- a/config-dist.php
+++ b/config-dist.php
@@ -880,6 +880,10 @@ $CFG->admin = 'admin';
// $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...
diff --git a/lib/behat/classes/behat_command.php b/lib/behat/classes/behat_command.php
index 64ce8f6de77..d653b4a5a28 100644
--- a/lib/behat/classes/behat_command.php
+++ b/lib/behat/classes/behat_command.php
@@ -203,13 +203,19 @@ class behat_command {
// 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;
}
diff --git a/lib/behat/lib.php b/lib/behat/lib.php
index 75654a43d14..53ea59da0fb 100644
--- a/lib/behat/lib.php
+++ b/lib/behat/lib.php
@@ -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.
diff --git a/lib/setup.php b/lib/setup.php
index dc9d1970a4a..79852725837 100644
--- a/lib/setup.php
+++ b/lib/setup.php
@@ -141,6 +141,16 @@ if (defined('BEHAT_SITE_RUNNING')) {
$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;
+ }
}
}