From a60977e7665f0d4f35552b99c08be21b6ceede72 Mon Sep 17 00:00:00 2001 From: Simey Lameze Date: Wed, 2 Aug 2023 10:16:00 +0800 Subject: [PATCH] MDL-78843 behat: update get_forum_id to search for idnumber or name This commit also converts manual steps to create discussions on the Annoucements forum to data generator. --- .../tests/behat/display_news.feature | 17 ++++------ .../behat/block_search_forums_course.feature | 15 +++------ mod/forum/tests/behat/advanced_search.feature | 32 +++++++------------ .../generator/behat_mod_forum_generator.php | 14 +++----- 4 files changed, 26 insertions(+), 52 deletions(-) diff --git a/blocks/news_items/tests/behat/display_news.feature b/blocks/news_items/tests/behat/display_news.feature index 0f228736a77..24f426fa96e 100644 --- a/blocks/news_items/tests/behat/display_news.feature +++ b/blocks/news_items/tests/behat/display_news.feature @@ -18,17 +18,12 @@ Feature: Latest announcements block displays the course latest news And the following "blocks" exist: | blockname | contextlevel | reference | pagetypepattern | defaultregion | | news_items | Course | C1 | course-view-* | side-pre | - And I am on the "Course 1" Course page logged in as teacher1 - When I add a new topic to "Announcements" forum with: - | Subject | Discussion One | - | Message | Not important | - And I add a new topic to "Announcements" forum with: - | Subject | Discussion Two | - | Message | Not important | - And I add a new topic to "Announcements" forum with: - | Subject | Discussion Three | - | Message | Not important | - And I am on "Course 1" course homepage + And the following "mod_forum > discussions" exist: + | user | forum | name | message | + | teacher1 | Announcements | Discussion One | Not important | + | teacher1 | Announcements | Discussion Two | Not important | + | teacher1 | Announcements | Discussion Three | Not important | + When I am on the "Course 1" Course page logged in as teacher1 Then I should see "Discussion One" in the "Latest announcements" "block" And I should see "Discussion Two" in the "Latest announcements" "block" And I should see "Discussion Three" in the "Latest announcements" "block" diff --git a/blocks/search_forums/tests/behat/block_search_forums_course.feature b/blocks/search_forums/tests/behat/block_search_forums_course.feature index 253ce444343..40979e39e10 100644 --- a/blocks/search_forums/tests/behat/block_search_forums_course.feature +++ b/blocks/search_forums/tests/behat/block_search_forums_course.feature @@ -23,6 +23,9 @@ Feature: The search forums block allows users to search for forum posts on cours And I am on the "Course 1" "course editing" page logged in as teacher1 And I set the field "id_newsitems" to "1" And I press "Save and display" + And the following "mod_forum > discussions" exist: + | user | forum | name | message | + | teacher1 | Announcements | My subject | My message | Scenario: Use the search forum block in a course without any forum posts Given I am on the "Course 1" course page logged in as student1 @@ -32,12 +35,7 @@ Feature: The search forums block allows users to search for forum posts on cours Then I should see "No posts" Scenario: Use the search forum block in a course with a hidden forum and search for posts - Given I add a new topic to "Announcements" forum with: - | Subject | My subject | - | Message | My message | - And I am on "Course 1" course homepage with editing mode on - And I follow "Announcements" - And I navigate to "Edit settings" in current page administration + Given I am on the "Announcements" "forum activity editing" page logged in as teacher1 And I expand all fieldsets And I set the field "id_visible" to "0" And I press "Save and return to course" @@ -49,10 +47,7 @@ Feature: The search forums block allows users to search for forum posts on cours Then I should see "No posts" Scenario: Use the search forum block in a course and search for posts - Given I add a new topic to "Announcements" forum with: - | Subject | My subject | - | Message | My message | - When I am on the "Course 1" course page logged in as student1 + Given I am on the "Course 1" course page logged in as student1 And "Search forums" "block" should exist And I set the following fields to these values: | searchform_search | message | diff --git a/mod/forum/tests/behat/advanced_search.feature b/mod/forum/tests/behat/advanced_search.feature index d5e9f061bc3..b1ab1e5fbd0 100644 --- a/mod/forum/tests/behat/advanced_search.feature +++ b/mod/forum/tests/behat/advanced_search.feature @@ -11,8 +11,8 @@ Feature: The forum search allows users to perform advanced searches for forum po | teacher2 | Teacher | TWO | teacher2@example.com | T1 | | student1 | Student | 1 | student1@example.com | S1 | And the following "courses" exist: - | fullname | shortname | category | - | Course 1 | C1 | 0 | + | fullname | shortname | category | newsitems | + | Course 1 | C1 | 0 | 1 | And the following "course enrolments" exist: | user | course | role | | teacher1 | C1 | editingteacher | @@ -24,17 +24,10 @@ Feature: The forum search allows users to perform advanced searches for forum po And the following "blocks" exist: | blockname | contextlevel | reference | pagetypepattern | defaultregion | | news_items | Course | C1 | course-view-* | side-pre | - And I am on the "Course 1" "course editing" page logged in as teacher1 - And I expand all fieldsets - And I set the field "id_newsitems" to "1" - And I press "Save and display" - And I add a new topic to "Announcements" forum with: - | Subject | My subject | - | Message | My message | - And I am on "Course 1" course homepage - And I add a new topic to "Announcements" forum with: - | Subject | Your subjective| - | Message | Your long message | + And the following "mod_forum > discussions" exist: + | user | forum | name | subject | message | + | teacher1 | Announcements | My subject | My subject | My message | + | teacher1 | Announcements | Your subjective | Your subjective | Your long message | Scenario: Perform an advanced search using any term Given I am on the "Announcements" "forum activity" page logged in as student1 @@ -77,14 +70,11 @@ Feature: The forum search allows users to perform advanced searches for forum po And I should see "Your subjective" Scenario: Perform an advanced search matching the author - Given I log in as "teacher2" - And I am on "Course 1" course homepage - And I add a new topic to "Announcements" forum with: - | Subject | My Subjects | - | Message | My message | - And I log out - When I am on the "Announcements" "forum activity" page logged in as student1 - And I press "Search forums" + Given the following "mod_forum > discussions" exist: + | user | forum | name | subject | message | + | teacher2 | Announcements | My Subjects | My Subjects | My message | + And I am on the "Announcements" "forum activity" page logged in as student1 + When I press "Search forums" And I should see "Advanced search" And I set the field "user" to "TWO" And I press "Search forums" diff --git a/mod/forum/tests/generator/behat_mod_forum_generator.php b/mod/forum/tests/generator/behat_mod_forum_generator.php index 5c16677796d..0064a315c78 100644 --- a/mod/forum/tests/generator/behat_mod_forum_generator.php +++ b/mod/forum/tests/generator/behat_mod_forum_generator.php @@ -47,19 +47,13 @@ class behat_mod_forum_generator extends behat_generator_base { } /** - * Get the forum id using an activity idnumber. + * Get the forum id using an activity idnumber or name. * - * @param string $idnumber + * @param string $idnumberorname The forum activity idnumber or name. * @return int The forum id */ - protected function get_forum_id(string $idnumber): int { - global $DB; - - if (!$id = $DB->get_field('course_modules', 'instance', ['idnumber' => $idnumber])) { - throw new Exception('The specified activity with idnumber "' . $idnumber . '" could not be found.'); - } - - return $id; + protected function get_forum_id(string $idnumberorname): int { + return $this->get_cm_by_activity_name('forum', $idnumberorname)->instance; } /**