diff --git a/auth/tests/behat/behat_auth.php b/auth/tests/behat/behat_auth.php index e6fcadb71f4..08d75997f31 100644 --- a/auth/tests/behat/behat_auth.php +++ b/auth/tests/behat/behat_auth.php @@ -68,6 +68,7 @@ class behat_auth extends behat_base { * Logs out of the system. * * @Given /^I log out$/ + * @Given I am not logged in */ public function i_log_out() { $this->execute('behat_general::i_visit', [new moodle_url('/auth/tests/behat/logout.php')]); diff --git a/auth/tests/behat/login.php b/auth/tests/behat/login.php index d9d06b47d71..2f67eb1c98e 100644 --- a/auth/tests/behat/login.php +++ b/auth/tests/behat/login.php @@ -35,6 +35,15 @@ if (!$behatrunning) { $username = required_param('username', PARAM_ALPHANUMEXT); $wantsurl = new moodle_url(optional_param('wantsurl', '/', PARAM_URL)); +if (isloggedin()) { + // If the user is already logged in, log them out and redirect them back to login again. + require_logout(); + redirect(new moodle_url('/auth/tests/behat/login.php', [ + 'username' => $username, + 'wantsurl' => $wantsurl->out(false), + ])); +} + // Note - with behat, the password is always the same as the username. $password = $username;