diff --git a/admin/tool/behat/tests/behat/loginout.feature b/admin/tool/behat/tests/behat/loginout.feature new file mode 100644 index 00000000000..5fe3a8ff021 --- /dev/null +++ b/admin/tool/behat/tests/behat/loginout.feature @@ -0,0 +1,110 @@ +@tool_behat +Feature: Verify that the behat login and logout steps work as expected + In order to use behat login and log out steps + As a test writer + I need to verify that login and logout happen when the steps are used + + Scenario: Log in as a user using the step + Given the following "users" exist: + | username | firstname | lastname | + | traverst1 | Thomas | Travers | + When I log in as "traverst1" + Then I should see "Thomas Travers" + + @javascript + Scenario: Log in as a user using the step (javascript) + Given the following "users" exist: + | username | firstname | lastname | + | traverst1 | Thomas | Travers | + When I log in as "traverst1" + Then I should see "Thomas Travers" + + Scenario: Log out using the log out step + Given the following "users" exist: + | username | firstname | lastname | + | traverst1 | Thomas | Travers | + And I am logged in as traverst1 + When I log out + Then I should not see "Thomas Travers" + And I should see "You are not logged in" + + @javascript + Scenario: Log out using the log out step (javascript) + Given the following "users" exist: + | username | firstname | lastname | + | traverst1 | Thomas | Travers | + And I am logged in as traverst1 + When I log out + Then I should not see "Thomas Travers" + And I should see "You are not logged in" + + Scenario: Log in step should automatically log user out if already logged in + Given the following "users" exist: + | username | firstname | lastname | + | traverst1 | Thomas | Travers | + | emeryj | Jane | Emery | + And I am logged in as traverst1 + When I log in as "emeryj" + Then I should not see "Thomas Travers" + And I should see "Jane Emery" + + @javascript + Scenario: Log in step should automatically log user out if already logged in (javascript) + Given the following "users" exist: + | username | firstname | lastname | + | traverst1 | Thomas | Travers | + | emeryj | Jane | Emery | + And I am logged in as traverst1 + When I log in as "emeryj" + Then I should not see "Thomas Travers" + And I should see "Jane Emery" + + Scenario: I am on page logged in as should redirect to correct page + Given the following "users" exist: + | username | firstname | lastname | + | traverst1 | Thomas | Travers | + And the following "course" exists: + | fullname | Life, the Universe, and Everything | + | shortname | hhgttg | + When I am on the hhgttg Course page logged in as traverst1 + Then I should see "Thomas Travers" + And I should see "Life, the Universe, and Everything" + + @javascript + Scenario: I am on page logged in as should redirect to correct page (javascript) + Given the following "users" exist: + | username | firstname | lastname | + | traverst1 | Thomas | Travers | + And the following "course" exists: + | fullname | Life, the Universe, and Everything | + | shortname | hhgttg | + When I am on the hhgttg Course page logged in as traverst1 + Then I should see "Thomas Travers" + And I should see "Life, the Universe, and Everything" + + Scenario: I am on page logged in as should redirect to correct page when automatically logging a user out + Given the following "users" exist: + | username | firstname | lastname | + | traverst1 | Thomas | Travers | + | emeryj | Jane | Emery | + And the following "course" exists: + | fullname | Life, the Universe, and Everything | + | shortname | hhgttg | + And I am logged in as emeryj + When I am on the hhgttg Course page logged in as traverst1 + Then I should see "Thomas Travers" + And I should see "Life, the Universe, and Everything" + + @javascript + Scenario: I am on page logged in as should redirect to correct page when automatically logging a user out (javacript) + Given the following "users" exist: + | username | firstname | lastname | + | traverst1 | Thomas | Travers | + | emeryj | Jane | Emery | + And the following "course" exists: + | fullname | Life, the Universe, and Everything | + | shortname | hhgttg | + And I am logged in as emeryj + When I am on the hhgttg Course page logged in as traverst1 + Then I should see "Thomas Travers" + And I should see "Life, the Universe, and Everything" diff --git a/auth/tests/behat/behat_auth.php b/auth/tests/behat/behat_auth.php index 6233454f99f..08d75997f31 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,33 +53,24 @@ class behat_auth extends behat_base { return; } - $loginurl = new moodle_url('/login/index.php'); + $loginurl = new moodle_url('/auth/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')); } /** * Logs out of the system. * * @Given /^I log out$/ + * @Given I am not logged in */ public function i_log_out() { - - // Wait for page to be loaded. - $this->wait_for_pending_js(); - - // Click on logout link in footer, as it's much faster. - $this->execute('behat_general::i_click_on_in_the', array(get_string('logout'), 'link', '#page-footer', "css_element")); + $this->execute('behat_general::i_visit', [new moodle_url('/auth/tests/behat/logout.php')]); } } diff --git a/auth/tests/behat/login.feature b/auth/tests/behat/login.feature index cdc5976666e..12cb5da1f6e 100644 --- a/auth/tests/behat/login.feature +++ b/auth/tests/behat/login.feature @@ -35,9 +35,9 @@ Feature: Authentication And I press "Log in" Then I should see "Invalid login, please try again" - Scenario: Log out + Scenario: Log out using the Log out link Given I log in as "admin" - When I log out + When I click on "Logout" "link" in the "#page-footer" "css_element" Then I should see "You are not logged in" in the "page-footer" "region" Scenario Outline: Checking the display of the Remember username checkbox diff --git a/auth/tests/behat/login.php b/auth/tests/behat/login.php new file mode 100644 index 00000000000..2f67eb1c98e --- /dev/null +++ b/auth/tests/behat/login.php @@ -0,0 +1,85 @@ +. +// phpcs:disable moodle.Files.RequireLogin.Missing +// phpcs:disable moodle.PHP.ForbiddenFunctions.Found + +/** + * Login end point for Behat tests only. + * + * @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 + */ +require(__DIR__.'/../../../config.php'); + +$behatrunning = defined('BEHAT_SITE_RUNNING') && BEHAT_SITE_RUNNING; +if (!$behatrunning) { + redirect(new moodle_url('/')); +} + +$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; + +$failurereason = null; +$user = authenticate_user_login($username, $password, true, $failurereason, false); +if ($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: '{$failurereason}'"; + 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"); +} + +redirect($wantsurl); diff --git a/auth/tests/behat/logout.php b/auth/tests/behat/logout.php new file mode 100644 index 00000000000..d2d7a69ade0 --- /dev/null +++ b/auth/tests/behat/logout.php @@ -0,0 +1,41 @@ +. +// phpcs:disable moodle.Files.RequireLogin.Missing +// phpcs:disable moodle.PHP.ForbiddenFunctions.Found + +/** + * Login end point for Behat tests only. + * + * @package core_auth + * @category test + * @copyright Andrew Lyons + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +require(__DIR__.'/../../../config.php'); + +$behatrunning = defined('BEHAT_SITE_RUNNING') && BEHAT_SITE_RUNNING; +if (!$behatrunning) { + redirect(new moodle_url('/login/logout.php')); +} + +require_logout(); + +$login = optional_param('loginpage', 0, PARAM_BOOL); +if ($login) { + redirect(get_login_url()); +} else { + redirect(new moodle_url('/')); +}