From dcce6d99df5e4d5d16db86369e8847fe0bc3d223 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mikel=20Mart=C3=ADn?= Date: Mon, 20 Mar 2023 13:54:47 +0100 Subject: [PATCH] MDL-77691 behat: Add step to navigate to profile page directly Co-authored-by: Simey Lameze --- .../tool/lp/tests/behat/plan_workflow.feature | 12 +++------ .../policy/tests/behat/acceptances.feature | 4 +-- admin/tool/policy/tests/behat/consent.feature | 7 ++--- lib/behat/classes/behat_session_trait.php | 26 +++++++++++++++++++ .../tests/behat/message_preferences.feature | 3 +-- user/tests/behat/behat_user.php | 25 ++++++++++++++++++ .../tests/behat/user_grade_navigation.feature | 3 +-- .../tests/behat/view_preferences_page.feature | 3 +-- 8 files changed, 60 insertions(+), 23 deletions(-) diff --git a/admin/tool/lp/tests/behat/plan_workflow.feature b/admin/tool/lp/tests/behat/plan_workflow.feature index 5410f2ab2e9..1923882699c 100644 --- a/admin/tool/lp/tests/behat/plan_workflow.feature +++ b/admin/tool/lp/tests/behat/plan_workflow.feature @@ -144,9 +144,7 @@ Feature: Manage plan workflow | name | user | description | status | reviewer | | Test-Plan3 | user1 | Description of plan 3 for user 1 | active | manager1 | | Test-Plan4 | user1 | Description of plan 4 for user 1 | active | manager1 | - And I log in as "manager1" - And I navigate to "Users > Accounts > Browse list of users" in site administration - And I follow "User 1" + And I am on the "user1" "user > profile" page logged in as "manager1" And I follow "Learning plans" And I should see "List of learning plans" And I follow "Learning plans" @@ -163,9 +161,7 @@ Feature: Manage plan workflow | name | user | description | status | reviewer | | Test-Plan3 | user1 | Description of plan 3 for user 1 | active | manager1 | | Test-Plan4 | user1 | Description of plan 4 for user 1 | active | manager1 | - And I log in as "manager1" - And I navigate to "Users > Accounts > Browse list of users" in site administration - And I follow "User 1" + And I am on the "user1" "user > profile" page logged in as "manager1" And I follow "Learning plans" And I should see "List of learning plans" And I follow "Learning plans" @@ -185,9 +181,7 @@ Feature: Manage plan workflow | name | user | description | status | reviewer | | Test-Plan3 | user1 | Description of plan 3 for user 1 | complete | manager1 | | Test-Plan4 | user1 | Description of plan 4 for user 1 | complete | manager1 | - And I log in as "manager1" - And I navigate to "Users > Accounts > Browse list of users" in site administration - And I follow "User 1" + And I am on the "user1" "user > profile" page logged in as "manager1" And I follow "Learning plans" And I should see "List of learning plans" And I follow "Learning plans" diff --git a/admin/tool/policy/tests/behat/acceptances.feature b/admin/tool/policy/tests/behat/acceptances.feature index 85df22dbaa9..562c27e80fd 100644 --- a/admin/tool/policy/tests/behat/acceptances.feature +++ b/admin/tool/policy/tests/behat/acceptances.feature @@ -241,9 +241,7 @@ Feature: Viewing acceptances reports and accepting on behalf of other users And I set the field "I agree to the This site policy" to "1" And I press "Next" And I log out - And I log in as "admin" - And I navigate to "Users > Accounts > Browse list of users" in site administration - And I follow "Manager" + And I am on the "manager" "user > profile" page logged in as "admin" And I follow "Log in as" And I press "Continue" And I navigate to "Users > Privacy and policies > Manage policies" in site administration diff --git a/admin/tool/policy/tests/behat/consent.feature b/admin/tool/policy/tests/behat/consent.feature index 88563462dad..336ee1eb398 100644 --- a/admin/tool/policy/tests/behat/consent.feature +++ b/admin/tool/policy/tests/behat/consent.feature @@ -562,9 +562,7 @@ Feature: User must accept policy managed by this plugin when logging in and sign And the following "users" exist: | username | firstname | lastname | email | | user1 | User | 1 | user1@example.com | - When I log in as "admin" - And I navigate to "Users > Accounts > Browse list of users" in site administration - And I follow "User 1" + When I am on the "user1" "user > profile" page logged in as "admin" And I follow "Log in as" Then I should see "You are logged in as User 1" And I press "Continue" @@ -603,8 +601,7 @@ Feature: User must accept policy managed by this plugin when logging in and sign And I set the field "I agree to the This site policy" to "1" And I set the field "I agree to the This privacy policy" to "1" And I press "Next" - And I navigate to "Users > Accounts > Browse list of users" in site administration - And I follow "User 1" + And I am on the "user1" "user > profile" page And I follow "Log in as" Then I should see "You are logged in as User 1" And I press "Continue" diff --git a/lib/behat/classes/behat_session_trait.php b/lib/behat/classes/behat_session_trait.php index 97b42cf5e72..2e2ad48d607 100644 --- a/lib/behat/classes/behat_session_trait.php +++ b/lib/behat/classes/behat_session_trait.php @@ -1685,4 +1685,30 @@ EOF; return !empty($matches); } + + /** + * Get the user id from an identifier. + * + * The user username and email fields are checked. + * + * @param string $identifier The user's username or email. + * @return int|null The user id or null if not found. + */ + protected function get_user_id_by_identifier(string $identifier): ?int { + global $DB; + + $sql = <<get_field_sql($sql, [ + 'username' => $identifier, + 'email' => $identifier, + ]); + + return $result ?: null; + } } diff --git a/message/tests/behat/message_preferences.feature b/message/tests/behat/message_preferences.feature index e12ee118b63..5c9663bfc39 100644 --- a/message/tests/behat/message_preferences.feature +++ b/message/tests/behat/message_preferences.feature @@ -18,8 +18,7 @@ Feature: To be able to see and save user message preferences as admin And I navigate to "Messaging > Notification settings" in site administration And I set the field "email" to "1" And I press "Save changes" - And I navigate to "Users > Accounts > Browse list of users" in site administration - And I click on "Student 1" "link" in the "Student 1" "table_row" + And I am on the "student1" "user > profile" page And I click on "Preferences" "link" in the "#region-main-box" "css_element" And I click on "Message preferences" "link" in the "#region-main-box" "css_element" And I click on "//label[@data-state='loggedoff']" "xpath_element" diff --git a/user/tests/behat/behat_user.php b/user/tests/behat/behat_user.php index eff2a766406..8848d93b353 100644 --- a/user/tests/behat/behat_user.php +++ b/user/tests/behat/behat_user.php @@ -84,4 +84,29 @@ class behat_user extends behat_base { throw new ExpectationException('The "' . $field . '" field does have purpose "' . $purpose . '"', $this->getSession()); } } + + /** + * Convert page names to URLs for steps like 'When I am on the "[identifier]" "[page type]" page'. + * + * Recognised page names are: + * | Page Type | Identifier meaning | Description | + * | profile | username or email | User profile page (/user/profile.php) | + * + * @param string $type identifies which type of page this is, e.g. 'Editing'. + * @param string $identifier identifies the user, e.g. 'student1'. + * @return moodle_url the corresponding URL. + * @throws Exception with a meaningful error message if the specified page cannot be found. + */ + protected function resolve_page_instance_url(string $type, string $identifier): moodle_url { + switch (strtolower($type)) { + case 'profile': + $userid = $this->get_user_id_by_identifier($identifier); + if (!$userid) { + throw new Exception('The specified user with username or email "' . $identifier . '" does not exist'); + } + return new moodle_url('/user/profile.php', ['id' => $userid]); + default: + throw new Exception("Unrecognised page type '{$type}'."); + } + } } diff --git a/user/tests/behat/user_grade_navigation.feature b/user/tests/behat/user_grade_navigation.feature index 040087ac2c2..7eaee807efd 100644 --- a/user/tests/behat/user_grade_navigation.feature +++ b/user/tests/behat/user_grade_navigation.feature @@ -76,8 +76,7 @@ Feature: The student can navigate to their grades page and user grade report. | moodle/user:viewuseractivitiesreport | 1 | | moodle/user:viewdetails | 1 | And I click on "Create this role" "button" - And I navigate to "Users > Accounts > Browse list of users" in site administration - And I follow "Student 1" + And I am on the "student1" "user > profile" page And I click on "Preferences" "link" in the ".profile_tree" "css_element" And I follow "Assign roles relative to this user" And I follow "Parent" diff --git a/user/tests/behat/view_preferences_page.feature b/user/tests/behat/view_preferences_page.feature index 8075576adf8..9e452536e3f 100644 --- a/user/tests/behat/view_preferences_page.feature +++ b/user/tests/behat/view_preferences_page.feature @@ -68,8 +68,7 @@ Feature: Access to preferences page | moodle/user:viewuseractivitiesreport | 1 | | moodle/user:viewdetails | 1 | And I click on "Create this role" "button" - And I navigate to "Users > Accounts > Browse list of users" in site administration - And I follow "Student 1" + And I am on the "student1" "user > profile" page And I click on "Preferences" "link" in the ".profile_tree" "css_element" And I follow "Assign roles relative to this user" And I follow "Parent"