From 142e12bee78ab11c7d67929fcdc286f86f21ae06 Mon Sep 17 00:00:00 2001 From: Andrew Nicols Date: Tue, 20 Jul 2021 17:12:05 +0800 Subject: [PATCH] MDL-72179 behat: Add page resolver for activity names --- .../behat/tests/behat/i_am_on_page.feature | 4 ++ lib/behat/classes/behat_session_trait.php | 42 +++++++++++++++++++ lib/tests/behat/behat_navigation.php | 25 +++++++++-- 3 files changed, 67 insertions(+), 4 deletions(-) diff --git a/admin/tool/behat/tests/behat/i_am_on_page.feature b/admin/tool/behat/tests/behat/i_am_on_page.feature index 43e1869e956..70da5fbc08e 100644 --- a/admin/tool/behat/tests/behat/i_am_on_page.feature +++ b/admin/tool/behat/tests/behat/i_am_on_page.feature @@ -30,6 +30,8 @@ Feature: Use core page resolvers for the I am on the page steps | Course idnumber | "2021-econ101" | Course | Fundamentals of Economics | | Forum idnumber | fundamentalsofeconomics | Activity | Add a new discussion | | Generic activity editing | fundamentalsofeconomics | "Activity editing" | Updating: Forum | + | Forum name | "Fundamentals of Economics" | "Forum activity" | Add a new discussion | + | Forum name editing | "Fundamentals of Economics" | "Forum activity editing" | Updating: Forum | Scenario Outline: When I am on an instance logged in as Given the following "categories" exist: @@ -57,6 +59,8 @@ Feature: Use core page resolvers for the I am on the page steps | Course idnumber | "2021-econ101" | Course | Fundamentals of Economics | | Forum idnumber | fundamentalsofeconomics | Activity | Add a new discussion | | Generic activity editing | fundamentalsofeconomics | "Activity editing" | Updating: Forum | + | Forum name | "Fundamentals of Economics" | "Forum activity" | Add a new discussion | + | Forum name editing | "Fundamentals of Economics" | "Forum activity editing" | Updating: Forum | Scenario Outline: When I am on a named page Given I log in as "admin" diff --git a/lib/behat/classes/behat_session_trait.php b/lib/behat/classes/behat_session_trait.php index 160b6d0acd4..a306a091663 100644 --- a/lib/behat/classes/behat_session_trait.php +++ b/lib/behat/classes/behat_session_trait.php @@ -1528,4 +1528,46 @@ EOF; return null; } + + /** + * Get a coursemodule from an activity name or idnumber. + * + * @param string $activity + * @param string $identifier + * @return cm_info + */ + protected function get_cm_by_activity_name(string $activity, string $identifier): cm_info { + global $DB; + + $coursetable = new \core\dml\table('course', 'c', 'c'); + $courseselect = $coursetable->get_field_select(); + $coursefrom = $coursetable->get_from_sql(); + + $cmtable = new \core\dml\table('course_modules', 'cm', 'cm'); + $cmfrom = $cmtable->get_from_sql(); + + $acttable = new \core\dml\table($activity, 'act', 'act'); + $actselect = $acttable->get_field_select(); + $actfrom = $acttable->get_from_sql(); + + $sql = <<get_record_sql($sql, [ + 'modname' => $activity, + 'idnumber' => $identifier, + 'name' => $identifier, + ], MUST_EXIST); + + $course = $coursetable->extract_from_result($result); + $instancedata = $acttable->extract_from_result($result); + + return get_fast_modinfo($course)->get_cm($result->cmid); + } } diff --git a/lib/tests/behat/behat_navigation.php b/lib/tests/behat/behat_navigation.php index a2abb85a885..e5ff5c725ff 100644 --- a/lib/tests/behat/behat_navigation.php +++ b/lib/tests/behat/behat_navigation.php @@ -735,7 +735,9 @@ class behat_navigation extends behat_base { protected function resolve_core_page_instance_url(string $type, string $identifier): moodle_url { global $DB; - switch (strtolower($type)) { + $type = strtolower($type); + + switch ($type) { case 'category': $categoryid = $this->get_category_id($identifier); if (!$categoryid) { @@ -766,10 +768,25 @@ class behat_navigation extends behat_base { return new moodle_url('/course/modedit.php', [ 'update' => $cm->id, ]); - - default: - throw new Exception('Unrecognised core page type "' . $type . '."'); } + + $parts = explode(' ', $type); + if (count($parts) > 1) { + if ($parts[1] === 'activity') { + $modname = $parts[0]; + $cm = $this->get_cm_by_activity_name($modname, $identifier); + + if (count($parts) == 2) { + return new moodle_url($cm->url); + } + + if ($parts[2] === 'editing') { + return new moodle_url('/course/modedit.php', ['update' => $cm->id]); + } + } + } + + throw new Exception('Unrecognised core page type "' . $type . '."'); } /**