From 441f3132175e91ab9115ac573ae11036a66ce467 Mon Sep 17 00:00:00 2001 From: Andrew Nicols Date: Fri, 16 Jul 2021 23:58:36 +0800 Subject: [PATCH] MDL-72173 behat: Switch to behat login URL --- auth/tests/behat/behat_auth.php | 12 ++--- lib/tests/behat/{fastlogin.php => login.php} | 51 +++++++++++++------- 2 files changed, 38 insertions(+), 25 deletions(-) rename lib/tests/behat/{fastlogin.php => login.php} (57%) diff --git a/auth/tests/behat/behat_auth.php b/auth/tests/behat/behat_auth.php index 6233454f99f..316cc2579fa 100644 --- a/auth/tests/behat/behat_auth.php +++ b/auth/tests/behat/behat_auth.php @@ -42,6 +42,7 @@ class behat_auth extends behat_base { * Logs in the user. There should exist a user with the same value as username and password. * * @Given /^I log in as "(?P(?:[^"]|\\")*)"$/ + * @Given I am logged in as :username * @param string $username the user to log in as. * @param moodle_url|null $wantsurl optional, URL to go to after logging in. */ @@ -52,20 +53,15 @@ class behat_auth extends behat_base { return; } - $loginurl = new moodle_url('/login/index.php'); + $loginurl = new moodle_url('/lib/tests/behat/login.php', [ + 'username' => $username, + ]); if ($wantsurl !== null) { $loginurl->param('wantsurl', $wantsurl->out_as_local_url()); } // Visit login page. $this->execute('behat_general::i_visit', [$loginurl]); - - // Enter username and password. - $this->execute('behat_forms::i_set_the_field_to', array('Username', $this->escape($username))); - $this->execute('behat_forms::i_set_the_field_to', array('Password', $this->escape($username))); - - // Press log in button, no need to check for exceptions as it will checked after this step execution. - $this->execute('behat_forms::press_button', get_string('login')); } /** diff --git a/lib/tests/behat/fastlogin.php b/lib/tests/behat/login.php similarity index 57% rename from lib/tests/behat/fastlogin.php rename to lib/tests/behat/login.php index d1e6707efeb..8103806d8e3 100644 --- a/lib/tests/behat/fastlogin.php +++ b/lib/tests/behat/login.php @@ -17,9 +17,10 @@ // phpcs:disable moodle.PHP.ForbiddenFunctions.Found /** - * Fast login end point for BEHAT TESTS ONLY. + * Login end point for Behat tests only. * - * @package theme_cfz + * @package core_auth + * @category test * @author Guy Thomas * @copyright 2021 Class Technologies Inc. {@link https://www.class.com/} * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later @@ -28,32 +29,48 @@ require(__DIR__.'/../../../config.php'); $behatrunning = defined('BEHAT_SITE_RUNNING') && BEHAT_SITE_RUNNING; if (!$behatrunning) { - die; + redirect(new moodle_url('/')); } $username = required_param('username', PARAM_ALPHANUMEXT); +$wantsurl = new moodle_url(optional_param('wantsurl', '/', PARAM_URL)); + // Note - with behat, the password is always the same as the username. $password = $username; $failurereason = null; $user = authenticate_user_login($username, $password, true, $failurereason, false); if ($failurereason) { - error_log("Failed to login as behat step for $username with reason: " . $failurereason); - throw new Exception($failurereason); + switch($failurereason) { + case AUTH_LOGIN_NOUSER: + $reason = get_string('invalidlogin'); + break; + case AUTH_LOGIN_SUSPENDED: + $reason = 'User suspended'; + break; + case AUTH_LOGIN_FAILED: + $reason = 'Login failed'; + break; + case AUTH_LOGIN_LOCKOUT: + $reason = 'Account locked'; + break; + case AUTH_LOGIN_UNAUTHORISED: + $reason = get_string('unauthorisedlogin', 'core', $username); + break; + default: + $reason = "Unknown login failure: '{$failureeason}'"; + break; + + } + + // Note: Do not throw an exception here as we sometimes test that login does not work. + // Exceptions are automatic failures in Behat. + \core\notification::add($reason, \core\notification::ERROR); + redirect(new moodle_url('/')); } + if (!complete_user_login($user)) { throw new Exception("Failed to login as behat step for $username"); } -$redirecturl = optional_param('redirecturl', null, PARAM_URL); -$redirecturl = $redirecturl ?? $CFG->wwwroot; - -if (optional_param('forceeditmode', false, PARAM_INT)) { - $sesskey = sesskey(); - $url = new moodle_url($redirecturl); - $url->param('edit', 1); - $url->param('sesskey', $sesskey); - $redirecturl = $url.''; -} - -redirect($redirecturl); +redirect($wantsurl);