MDL-72179 behat: Add page resolver for activity names

This commit is contained in:
Andrew Nicols 2021-07-20 17:12:05 +08:00
parent 9f428f68c7
commit 950c7adb1c
3 changed files with 67 additions and 4 deletions

View File

@ -29,6 +29,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:
@ -55,6 +57,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"

View File

@ -1534,4 +1534,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 = <<<EOF
SELECT cm.id as cmid, {$courseselect}, {$actselect}
FROM {$cmfrom}
INNER JOIN {$coursefrom} ON c.id = cm.course
INNER JOIN {modules} m ON m.id = cm.module AND m.name = :modname
INNER JOIN {$actfrom} ON cm.instance = act.id
WHERE cm.idnumber = :idnumber OR act.name = :name
EOF;
$result = $DB->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);
}
}

View File

@ -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 . '."');
}
/**