From 9f7163869e941a1e79ba7ac920d45f95823135cc Mon Sep 17 00:00:00 2001 From: Ferran Recio Date: Wed, 13 Dec 2023 09:52:14 +0100 Subject: [PATCH 1/2] MDL-79029 behat: new add activity steps --- course/tests/behat/behat_course.php | 116 ++++++++++++++++++++++++++-- course/upgrade.txt | 4 + 2 files changed, 115 insertions(+), 5 deletions(-) diff --git a/course/tests/behat/behat_course.php b/course/tests/behat/behat_course.php index 7c3f6ab6646..d8fc68695b7 100644 --- a/course/tests/behat/behat_course.php +++ b/course/tests/behat/behat_course.php @@ -202,7 +202,7 @@ class behat_course extends behat_base { if ($section) { // Section 1 represents the contents on the frontpage. $sectionxpath = "//body[@id='page-site-index']" . - "/descendant::div[contains(concat(' ',normalize-space(@class),' '),' sitetopic ')]"; + "/descendant::div[contains(concat(' ',normalize-space(@class),' '),' sitetopic ')]"; } else { // Section 0 represents "Site main menu" block. $sectionxpath = "//*[contains(concat(' ',normalize-space(@class),' '),' block_site_main_menu ')]"; @@ -224,10 +224,116 @@ class behat_course extends behat_base { // Clicks the selected activity if it exists. $activityliteral = behat_context_helper::escape(ucfirst($activity)); $activityxpath = "//div[contains(concat(' ', normalize-space(@class), ' '), ' modchooser ')]" . - "/descendant::div[contains(concat(' ', normalize-space(@class), ' '), ' optioninfo ')]" . - "/descendant::div[contains(concat(' ', normalize-space(@class), ' '), ' optionname ')]" . - "[normalize-space(.)=$activityliteral]" . - "/parent::a"; + "/descendant::div[contains(concat(' ', normalize-space(@class), ' '), ' optioninfo ')]" . + "/descendant::div[contains(concat(' ', normalize-space(@class), ' '), ' optionname ')]" . + "[normalize-space(.)=$activityliteral]" . + "/parent::a"; + + $this->execute('behat_general::i_click_on', [$activityxpath, 'xpath']); + } + + /** + * Adds the selected activity/resource filling the form data with the specified field/value pairs. + * + * Sections 0 and 1 are also allowed on frontpage. + * + * @Given I add a :activity activity to course :coursefullname section :sectionnum and I fill the form with: + * @Given I add an :activity activity to course :coursefullname section :sectionnum and I fill the form with: + * @param string $activity The activity name + * @param string $coursefullname The course full name of the course. + * @param int $section The section number + * @param TableNode $data The activity field/value data + */ + public function i_add_to_course_section_and_i_fill_the_form_with($activity, $coursefullname, $section, TableNode $data) { + + // Add activity to section. + $this->execute( + "behat_course::i_add_to_course_section", + [$this->escape($activity), $this->escape($coursefullname), $this->escape($section)] + ); + + // Wait to be redirected. + $this->execute('behat_general::wait_until_the_page_is_ready'); + + // Set form fields. + $this->execute("behat_forms::i_set_the_following_fields_to_these_values", $data); + + // Save course settings. + $this->execute("behat_forms::press_button", get_string('savechangesandreturntocourse')); + } + + /** + * Open a add activity form page. + * + * @Given I add a :activity activity to course :coursefullname section :sectionnum + * @Given I add an :activity activity to course :coursefullname section :sectionnum + * @throws coding_exception + * @param string $activity The activity name. + * @param string $coursefullname The course full name of the course. + * @param string $sectionnum The section number. + */ + public function i_add_to_course_section(string $activity, string $coursefullname, string $sectionnum): void { + $addurl = new moodle_url('/course/modedit.php', [ + 'add' => $activity, + 'course' => $this->get_course_id($coursefullname), + 'section' => intval($sectionnum), + ]); + $this->execute('behat_general::i_visit', [$addurl]); + } + + /** + * Opens the activity chooser and opens the activity/resource link form page. + * + * Sections 0 and 1 are also allowed on frontpage. + * + * This step require javascript enabled and it is used mainly to click activities or resources by name, + * not by plugin name. Use the standard behat_course::i_add_to_course_section step instead unless the + * plugin create extra entries into the activity chooser (like LTI). + * + * @Given I add a :activityname to section :sectionnum using the activity chooser + * @Given I add an :activityname to section :sectionnum using the activity chooser + * @throws ElementNotFoundException Thrown by behat_base::find + * @param string $activityname + * @param int $sectionnum + */ + public function i_add_to_section_using_the_activity_chooser($activityname, $sectionnum) { + + $this->require_javascript('Please use the \'the following "activity" exists:\' data generator instead.'); + + if ($this->getSession()->getPage()->find('css', 'body#page-site-index') && (int) $sectionnum <= 1) { + // We are on the frontpage. + if ($sectionnum) { + // Section 1 represents the contents on the frontpage. + $sectionxpath = "//body[@id='page-site-index']" . + "/descendant::div[contains(concat(' ',normalize-space(@class),' '),' sitetopic ')]"; + } else { + // Section 0 represents "Site main menu" block. + $sectionxpath = "//*[contains(concat(' ',normalize-space(@class),' '),' block_site_main_menu ')]"; + } + } else { + // We are inside the course. + $sectionxpath = "//li[@id='section-" . $sectionnum . "']"; + } + + // Clicks add activity or resource section link. + $sectionnode = $this->find('xpath', $sectionxpath); + $this->execute( + 'behat_general::i_click_on_in_the', + [ + "//button[@data-action='open-chooser' and not(@data-beforemod)]", + 'xpath', + $sectionnode, + 'NodeElement', + ] + ); + + // Clicks the selected activity if it exists. + $activityliteral = behat_context_helper::escape(ucfirst($activityname)); + $activityxpath = "//div[contains(concat(' ', normalize-space(@class), ' '), ' modchooser ')]" . + "/descendant::div[contains(concat(' ', normalize-space(@class), ' '), ' optioninfo ')]" . + "/descendant::div[contains(concat(' ', normalize-space(@class), ' '), ' optionname ')]" . + "[normalize-space(.)=$activityliteral]" . + "/parent::a"; $this->execute('behat_general::i_click_on', [$activityxpath, 'xpath']); } diff --git a/course/upgrade.txt b/course/upgrade.txt index c3da86e8780..3dbc340a4c1 100644 --- a/course/upgrade.txt +++ b/course/upgrade.txt @@ -2,6 +2,10 @@ This files describes API changes in /course/*, information provided here is intended especially for developers. === 4.4 === +* New behat steps backported to stable versions: + - I add a :activityname to section :sectionnum using the activity chooser + - I add a :activitypluginname activity to course :coursefullname section :sectionnum + - I add a :activitypluginname activity to course :coursefullname section :sectionnum and I fill the form with: * set_coursemodule_visible() has a new $rebuildcache parameter. If this is being called multiple times in the same request, consider passing `false` for this parameter and rebuilding the cache once after all the course modules have been updated. See course_update_section() for an example. From e679f8f11f09d7b29011f7448858041a03d896f0 Mon Sep 17 00:00:00 2001 From: Ferran Recio Date: Wed, 13 Dec 2023 12:38:03 +0100 Subject: [PATCH 2/2] MDL-79029 behat: optimize behat tests Replace some add activity to section steps to the new add activity to course section so they are faster and do not require javascript. --- .../tests/behat/add_filetypes.feature | 3 +-- .../tests/behat/private_ruleset.feature | 3 +-- course/tests/behat/add_activities.feature | 22 ++++++------------- .../videojs/tests/behat/modules.feature | 2 +- 4 files changed, 10 insertions(+), 20 deletions(-) diff --git a/admin/tool/filetypes/tests/behat/add_filetypes.feature b/admin/tool/filetypes/tests/behat/add_filetypes.feature index 13b1e2acba3..870cc8a5579 100644 --- a/admin/tool/filetypes/tests/behat/add_filetypes.feature +++ b/admin/tool/filetypes/tests/behat/add_filetypes.feature @@ -120,8 +120,7 @@ Feature: Add customised file types | Custom description | Froggy file | And I press "Save changes" # Create a resource activity and add it to a course - And I am on "Course 1" course homepage with editing mode on - When I add a "File" to section "1" + When I add a resource activity to course "Course 1" section "1" And I set the following fields to these values: | Name | An example of customised file type | | Description | File description | diff --git a/availability/tests/behat/private_ruleset.feature b/availability/tests/behat/private_ruleset.feature index 142f9858d4a..fa60a6948a4 100644 --- a/availability/tests/behat/private_ruleset.feature +++ b/availability/tests/behat/private_ruleset.feature @@ -21,8 +21,7 @@ Feature: Private rule sets | Group A | C1 | GA | 0 | | Group B | C1 | GB | 1 | And I log in as "teacher1" - And I am on "Course 1" course homepage with editing mode on - And I add a "Page" to section "1" + And I add a page activity to course "Course 1" section "1" And I expand all fieldsets Scenario: Add restriction with visible condition (must match), display option should be active diff --git a/course/tests/behat/add_activities.feature b/course/tests/behat/add_activities.feature index 47d76d7ec0c..08f489dfba9 100644 --- a/course/tests/behat/add_activities.feature +++ b/course/tests/behat/add_activities.feature @@ -11,15 +11,13 @@ Feature: Add activities to courses @javascript Scenario: Add an activity to a course - Given I am on the "Course 1" Course page logged in as admin - And I am on "Course 1" course homepage with editing mode on - When I add a "Database" to section "3" and I fill the form with: + Given I log in as "admin" + When I add a data activity to course "Course 1" section "3" and I fill the form with: | Name | Test name | | Description | Test database description | | ID number | TESTNAME | | Allow comments on entries | Yes | | Force language | English | - And I turn editing mode off Then I should not see "Adding a new" And I turn editing mode on And I open "Test name" actions menu @@ -30,32 +28,26 @@ Feature: Add activities to courses | Allow comments on entries | Yes | | Force language | English ‎(en)‎ | - @javascript Scenario: Add an activity supplying only the name - Given I am on the "Course 1" Course page logged in as admin - And I am on "Course 1" course homepage with editing mode on - When I add a "Database" to section "3" and I fill the form with: + Given I log in as "admin" + When I add a data activity to course "Course 1" section "3" and I fill the form with: | Name | Test name | Then I should see "Test name" - @javascript Scenario: Set activity description to required then add an activity supplying only the name Given the following config values are set as admin: | requiremodintro | 1 | - And I am on the "Course 1" Course page logged in as admin - And I am on "Course 1" course homepage with editing mode on - And I add a "Database" to section "3" and I fill the form with: + And I log in as "admin" + And I add a data activity to course "Course 1" section "3" and I fill the form with: | Name | Test name | Then I should see "Required" - @javascript Scenario: The activity description should use the user's preferred editor on creation Given the following "user preferences" exist: | user | preference | value | | admin | htmleditor | textarea | And I am logged in as admin - And I am on "Course 1" course homepage with editing mode on - When I add a "Database" to section "3" + When I add a data activity to course "Course 1" section "3" Then the field "Description format" matches value "0" @javascript diff --git a/media/player/videojs/tests/behat/modules.feature b/media/player/videojs/tests/behat/modules.feature index 90da7466d20..e02bcd63d25 100644 --- a/media/player/videojs/tests/behat/modules.feature +++ b/media/player/videojs/tests/behat/modules.feature @@ -24,7 +24,7 @@ Feature: Embed videos without the media filter @javascript Scenario: Add a video as a File resource. Make sure media filters work - When I add a "File" to section "1" + When I add a "File" to section "1" using the activity chooser And I set the following fields to these values: | Name | Video File | | Description | Example of a video file |