From a15f0a857b87f52cdac068eec729aac0ef4d8364 Mon Sep 17 00:00:00 2001 From: Ferran Recio Date: Thu, 24 Aug 2023 20:58:28 +0200 Subject: [PATCH] MDL-79029 behat: new add activity steps --- course/tests/behat/behat_course.php | 70 +++++++++++++++++------- course/upgrade.txt | 7 +++ lib/tests/behat/behat_deprecated.php | 81 ++++++++++++++++++++++++++++ 3 files changed, 138 insertions(+), 20 deletions(-) diff --git a/course/tests/behat/behat_course.php b/course/tests/behat/behat_course.php index 7c3f6ab6646..7c6ac3a5356 100644 --- a/course/tests/behat/behat_course.php +++ b/course/tests/behat/behat_course.php @@ -162,18 +162,23 @@ class behat_course extends behat_base { } /** - * Adds the selected activity/resource filling the form data with the specified field/value pairs. Sections 0 and 1 are also allowed on frontpage. + * Adds the selected activity/resource filling the form data with the specified field/value pairs. * - * @When /^I add a "(?P(?:[^"]|\\")*)" to section "(?P\d+)" and I fill the form with:$/ + * 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_section_and_i_fill_the_form_with($activity, $section, TableNode $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_section", - array($this->escape($activity), $this->escape($section)) + $this->execute( + "behat_course::i_add_to_course_section", + [$this->escape($activity), $this->escape($coursefullname), $this->escape($section)] ); // Wait to be redirected. @@ -187,29 +192,54 @@ class behat_course extends behat_base { } /** - * Opens the activity chooser and opens the activity/resource form page. Sections 0 and 1 are also allowed on frontpage. + * Open a add activity form page. * - * @Given /^I add a "(?P(?:[^"]|\\")*)" to section "(?P\d+)"$/ - * @throws ElementNotFoundException Thrown by behat_base::find - * @param string $activity - * @param int $section + * @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_section($activity, $section) { + 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) $section <= 1) { + if ($this->getSession()->getPage()->find('css', 'body#page-site-index') && (int) $sectionnum <= 1) { // We are on the frontpage. - if ($section) { + if ($sectionnum) { // 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 ')]"; } } else { // We are inside the course. - $sectionxpath = "//li[@id='section-" . $section . "']"; + $sectionxpath = "//li[@id='section-" . $sectionnum . "']"; } // Clicks add activity or resource section link. @@ -222,12 +252,12 @@ class behat_course extends behat_base { ]); // Clicks the selected activity if it exists. - $activityliteral = behat_context_helper::escape(ucfirst($activity)); + $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"; + "/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 4b0b4831dac..8b906375d1c 100644 --- a/course/upgrade.txt +++ b/course/upgrade.txt @@ -2,6 +2,13 @@ This files describes API changes in /course/*, information provided here is intended especially for developers. === 4.4 === +* The following behat steps are now deprecated and replaced by new ones with no Javascript dependencies: + - I add a ":activityname" to section ":sectionnum" + - I add a ":activityname" to section ":sectionnum and I fill the form with:" + The new steps are: + - 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. diff --git a/lib/tests/behat/behat_deprecated.php b/lib/tests/behat/behat_deprecated.php index a8573b9165c..62d655fe468 100644 --- a/lib/tests/behat/behat_deprecated.php +++ b/lib/tests/behat/behat_deprecated.php @@ -17,6 +17,7 @@ // NOTE: no MOODLE_INTERNAL test here, this file may be required by behat before including /config.php. require_once(__DIR__ . '/../../../lib/behat/behat_deprecated_base.php'); +use Behat\Gherkin\Node\TableNode as TableNode; /** * Steps definitions that are now deprecated and will be removed in the next releases. @@ -31,5 +32,85 @@ require_once(__DIR__ . '/../../../lib/behat/behat_deprecated_base.php'); * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ class behat_deprecated extends behat_deprecated_base { + /** + * Opens the activity chooser and opens the activity/resource form page. Sections 0 and 1 are also allowed on frontpage. + * + * @Given /^I add a "(?P(?:[^"]|\\")*)" to section "(?P\d+)"$/ + * @throws \Behat\Mink\Exception\ElementNotFoundException Thrown by behat_base::find + * @param string $activity + * @param int $section + * @deprecated Since Moodle 4.4 + */ + public function i_add_to_section($activity, $section) { + $this->deprecated_message([ + 'behat_course::i_add_to_course_section', + 'behat_course::i_add_to_section_using_the_activity_chooser', + ]); + $this->require_javascript('Please use the \'the following "activity" exists:\' data generator instead.'); + + if ($this->getSession()->getPage()->find('css', 'body#page-site-index') && (int) $section <= 1) { + // We are on the frontpage. + if ($section) { + // 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-" . $section . "']"; + } + + // 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($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"; + + $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. + * + * @When /^I add a "(?P(?:[^"]|\\")*)" to section "(?P\d+)" and I fill the form with:$/ + * @param string $activity The activity name + * @param int $section The section number + * @param TableNode $data The activity field/value data + * @deprecated Since Moodle 4.4 + */ + public function i_add_to_section_and_i_fill_the_form_with($activity, $section, TableNode $data) { + $this->deprecated_message(['behat_course::i_add_to_course_section_and_i_fill_the_form_with']); + + // Add activity to section. + $this->execute( + "behat_course::i_add_to_section", + [$this->escape($activity), $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')); + } }