Skip to content

Commit 9af35ae

Browse files
committed
MOBILE-3320 behat: Test activity deep links
1 parent 83eab88 commit 9af35ae

File tree

2 files changed

+77
-0
lines changed

2 files changed

+77
-0
lines changed

tests/behat/behat_app.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -532,6 +532,24 @@ public function i_receive_a_push_notification(TableNode $data) {
532532
$this->wait_for_pending_js();
533533
}
534534

535+
/**
536+
* Replace arguments from the content in the given activity field.
537+
*
538+
* @Given /^I replace the arguments in "([^"]+)" "([^"]+)"$/
539+
*/
540+
public function i_replace_arguments_in_the_activity(string $idnumber, string $field) {
541+
global $DB;
542+
543+
$coursemodule = $DB->get_record('course_modules', compact('idnumber'));
544+
$module = $DB->get_record('modules', ['id' => $coursemodule->module]);
545+
$activity = $DB->get_record($module->name, ['id' => $coursemodule->instance]);
546+
547+
$DB->update_record($module->name, [
548+
'id' => $coursemodule->instance,
549+
$field => $this->replace_arguments($activity->{$field}),
550+
]);
551+
}
552+
535553
/**
536554
* Opens a custom link.
537555
*
@@ -889,4 +907,31 @@ public function replace_wwwroot($text) {
889907
return str_replace('$WWWROOT', $CFG->behat_wwwroot, $text);
890908
}
891909

910+
/**
911+
* Replace arguments with the format "${activity:field}" from a string, where "activity" is
912+
* the idnumber of an activity and "field" is the activity's field to get replacement from.
913+
*
914+
* At the moment, the only field supported is "cmid", the id of the course module for this activity.
915+
*
916+
* @param string $text Original text.
917+
* @return string Text with arguments replaced.
918+
*/
919+
protected function replace_arguments(string $text): string {
920+
global $DB;
921+
922+
preg_match_all("/\\$\\{([^:}]+):([^}]+)\\}/", $text, $matches);
923+
924+
foreach ($matches[0] as $index => $match) {
925+
switch ($matches[2][$index]) {
926+
case 'cmid':
927+
$coursemodule = $DB->get_record('course_modules', ['idnumber' => $matches[1][$index]]);
928+
$text = str_replace($match, $coursemodule->id, $text);
929+
930+
break;
931+
}
932+
}
933+
934+
return $text;
935+
}
936+
892937
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
@app @javascript
2+
Feature: It navigates properly within activities.
3+
4+
Background:
5+
Given the following "users" exist:
6+
| username |
7+
| student |
8+
And the following "courses" exist:
9+
| fullname | shortname |
10+
| Course 1 | C1 |
11+
And the following "course enrolments" exist:
12+
| user | course | role |
13+
| student | C1 | student |
14+
And the following "activities" exist:
15+
| activity | idnumber | course | name | intro | content |
16+
| label | label | C1 | Label | Label description | - |
17+
| page | page | C1 | Page | - | <a href="/mod/label/view.php?id=${label:cmid}">Go to label</a> |
18+
And I replace the arguments in "page" "content"
19+
20+
Scenario: Navigates using deep links
21+
When I enter the app
22+
And I log in as "student"
23+
And I press "Course 1" in the app
24+
And I press "Page" in the app
25+
And I press "Go to label" in the app
26+
Then I should find "Label description" in the app
27+
28+
When I press the back button in the app
29+
Then I should find "Go to label" in the app
30+
31+
When I press the back button in the app
32+
Then I should find "Label description" in the app

0 commit comments

Comments
 (0)