Skip to content

Commit

Permalink
MDL-72179 behat: Add page resolver for activity names
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewnicols committed Jul 23, 2021
1 parent 9f428f6 commit 950c7ad
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 3 deletions.
4 changes: 4 additions & 0 deletions admin/tool/behat/tests/behat/i_am_on_page.feature
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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"
Expand Down
42 changes: 42 additions & 0 deletions lib/behat/classes/behat_session_trait.php
Original file line number Diff line number Diff line change
Expand Up @@ -1534,4 +1534,46 @@ protected function get_course_module_for_identifier(string $identifier): ?cm_inf

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);
}
}
23 changes: 20 additions & 3 deletions lib/tests/behat/behat_navigation.php
Original file line number Diff line number Diff line change
Expand Up @@ -735,7 +735,9 @@ protected function resolve_core_page_url(string $name): moodle_url {
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) {
Expand Down Expand Up @@ -766,10 +768,25 @@ protected function resolve_core_page_instance_url(string $type, string $identifi
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 . '."');
}

/**
Expand Down

0 comments on commit 950c7ad

Please sign in to comment.