Skip to content

Commit

Permalink
MOBILE-4028 behat: Add behats to test logout and redirects
Browse files Browse the repository at this point in the history
  • Loading branch information
dpalou committed Nov 21, 2024
1 parent e241918 commit ca7434d
Show file tree
Hide file tree
Showing 6 changed files with 343 additions and 31 deletions.
3 changes: 2 additions & 1 deletion local_moodleappbehat/tests/behat/behat_app.php
Original file line number Diff line number Diff line change
Expand Up @@ -604,6 +604,7 @@ public function i_open_a_custom_link(TableNode $data) {
$data = $data->getColumnsHash()[0];
$title = array_keys($data)[0];
$data = (object) $data;
$username = $data->user ?? '';

switch ($title) {
case 'discussion':
Expand Down Expand Up @@ -645,7 +646,7 @@ public function i_open_a_custom_link(TableNode $data) {
throw new DriverException('Invalid custom link title - ' . $title);
}

$this->open_moodleapp_custom_url($pageurl);
$this->open_moodleapp_custom_url($pageurl, '', $username);
}

/**
Expand Down
77 changes: 60 additions & 17 deletions local_moodleappbehat/tests/behat/behat_app_helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -366,12 +366,15 @@ protected function runtime_js(string $script) {
*
* @param string $script
* @param bool $blocking
* @param string $texttofind If set, when this text is found the operation is considered finished. This is useful for
* operations that might expect user input before finishing, like a confirm modal.
* @return mixed Result.
*/
protected function zone_js(string $script, bool $blocking = false) {
protected function zone_js(string $script, bool $blocking = false, string $texttofind = '') {
$blockingjson = json_encode($blocking);
$locatortofind = !empty($texttofind) ? json_encode((object) ['text' => $texttofind]) : null;

return $this->runtime_js("runInZone(() => window.behat.$script, $blockingjson)");
return $this->runtime_js("runInZone(() => window.behat.$script, $blockingjson, $locatortofind)");
}

/**
Expand Down Expand Up @@ -411,16 +414,14 @@ protected function open_moodleapp_custom_login_url($username, $path = '', string
$privatetoken = $usertoken->privatetoken;
}

// Generate custom URL.
$parsed_url = parse_url($CFG->behat_wwwroot);
$site = $parsed_url['host'] ?? '';
$site .= isset($parsed_url['port']) ? ':' . $parsed_url['port'] : '';
$site .= $parsed_url['path'] ?? '';
$url = $this->get_mobile_url_scheme() . "://$username@$site?token=$token&privatetoken=$privatetoken";
$url = $this->generate_custom_url([
'username' => $username,
'token' => $token,
'privatetoken' => $privatetoken,
'redirect' => $path,
]);

if (!empty($path)) {
$url .= '&redirect='.urlencode($CFG->behat_wwwroot.$path);
} else {
if (empty($path)) {
$successXPath = '//page-core-mainmenu';
}

Expand All @@ -434,24 +435,66 @@ protected function open_moodleapp_custom_login_url($username, $path = '', string
*
* @param string $path To navigate.
* @param string $successXPath The XPath of the element to lookat after navigation.
* @param string $username The username to use.
*/
protected function open_moodleapp_custom_url(string $path, string $successXPath = '', string $username = '') {
global $CFG;

$url = $this->generate_custom_url([
'username' => $username,
'redirect' => $path,
]);

$this->handle_url($url, $successXPath, $username ? 'This link belongs to another site' : '');
}

/**
* Generates a custom URL to be treated by the app.
*
* @param array $data Data to generate the URL.
*/
protected function open_moodleapp_custom_url(string $path, string $successXPath = '') {
protected function generate_custom_url(array $data): string {
global $CFG;

$urlscheme = $this->get_mobile_url_scheme();
$url = "$urlscheme://link=" . urlencode($CFG->behat_wwwroot.$path);
$parsed_url = parse_url($CFG->behat_wwwroot);
$parameters = [];

$url = $this->get_mobile_url_scheme() . '://' . ($parsed_url['scheme'] ?? 'http') . '://';
if (!empty($data['username'])) {
$url .= $data['username'] . '@';
}
$url .= $parsed_url['host'] ?? '';
$url .= isset($parsed_url['port']) ? ':' . $parsed_url['port'] : '';
$url .= $parsed_url['path'] ?? '';

if (!empty($data['token'])) {
$parameters[] = 'token=' . $data['token'];
if (!empty($data['privatetoken'])) {
$parameters[] = 'privatetoken=' . $data['privatetoken'];
}
}

if (!empty($data['redirect'])) {
$parameters[] = 'redirect=' . urlencode($data['redirect']);
}

if (!empty($parameters)) {
$url .= '?' . implode('&', $parameters);
}

$this->handle_url($url);
return $url;
}

/**
* Handles the custom URL on the Moodle App (and waits to finish.)
*
* @param string $customurl To navigate.
* @param string $successXPath The XPath of the element to lookat after navigation.
* @param string $texttofind If set, when this text is found the operation is considered finished. This is useful for
* operations that might expect user input before finishing, like a confirm modal.
*/
protected function handle_url(string $customurl, string $successXPath = '') {
$result = $this->zone_js("customUrlSchemes.handleCustomURL('$customurl')");
protected function handle_url(string $customurl, string $successXPath = '', string $texttofind = '') {
$result = $this->zone_js("customUrlSchemes.handleCustomURL('$customurl')", false, $texttofind);

if ($result !== 'OK') {
throw new DriverException('Error handling url - ' . $customurl . ' - '.$result);
Expand Down
7 changes: 0 additions & 7 deletions src/core/features/login/tests/behat/basic_usage.feature
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,6 @@ Feature: Test basic usage of login in app
And I press "Connect to your site" in the app
Then I should find "Can't connect to site" in the app

Scenario: Log out from the app
Given I entered the app as "student1"
And I press the user menu button in the app
When I press "Log out" in the app
And I wait the app to restart
Then the header should be "Accounts" in the app

Scenario: Delete an account
Given I entered the app as "student1"
When I log out in the app
Expand Down
161 changes: 161 additions & 0 deletions src/core/features/login/tests/behat/logout.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
@core_login @app @javascript
Feature: Test different cases of logout and switch account
I need different logout use cases to work

Background:
Given the following "users" exist:
| username | firstname | lastname |
| student1 | david | student |
| student2 | pau | student2 |

Scenario: Log out and re-login
Given I entered the app as "student1"
When I press the user menu button in the app
And I press "Log out" in the app
And I wait the app to restart
Then the header should be "Accounts" in the app

When I press "david student" in the app
Then the header should be "Reconnect" in the app
And I should find "david student" in the app

When I set the following fields to these values in the app:
| Password | student1 |
And I press "Log in" near "Lost password?" in the app
Then the header should be "Acceptance test site" in the app

Scenario: Exit account using switch account and re-enter
Given I entered the app as "student1"
When I press the user menu button in the app
And I press "Switch account" in the app
And I press "Add" in the app
And I wait the app to restart
Then I should find "Connect to Moodle" in the app

When I go back in the app
And I press "david student" in the app
Then the header should be "Acceptance test site" in the app

Scenario: Exit account using switch account and re-enter when forcelogout is enabled
Given the following config values are set as admin:
| forcelogout | 1 | tool_mobile |
| defaulthomepage | 0 | |
And I entered the app as "student1"
When I press the user menu button in the app
And I press "Switch account" in the app
And I press "Add" in the app
And I wait the app to restart
And I go back in the app
And I press "david student" in the app
Then the header should be "Reconnect" in the app
And I should find "david student" in the app

Scenario: Switch to a different account
Given I entered the app as "student1"
And I entered the app as "student2"
When I press the user menu button in the app
Then I should find "pau student2" in the app

When I press "Switch account" in the app
And I press "david student" in the app
And I wait the app to restart
Then the header should be "Acceptance test site" in the app

When I press the user menu button in the app
Then I should find "david student" in the app

Scenario: Logout when there is unsaved data
Given the following "courses" exist:
| fullname | shortname |
| Course 1 | C1 |
And the following "course enrolments" exist:
| user | course | role |
| student1 | C1 | student |
And the following "activities" exist:
| activity | name | intro | course | idnumber |
| forum | Test forum | Test forum | C1 | forum |
And the following forum discussions exist in course "Course 1":
| forum | user | name | message |
| Test forum | student1 | Forum topic 1 | Forum message 1 |
| Test forum | student1 | Forum topic 2 | Forum message 2 |
And I entered the course "Course 1" as "student1" in the app
And I change viewport size to "1200x640" in the app

When I press "Test forum" in the app
And I press "Add discussion topic" in the app
And I set the following fields to these values in the app:
| Subject | My happy subject |
| Message | An awesome message |
And I press the user menu button in the app
And I press "Log out" in the app
Then I should find "Are you sure you want to leave this page?" in the app

# Check that the app continues working fine if the user cancels the logout.
When I press "Cancel" in the app
And I press "Forum topic 1" in the app
And I press "OK" in the app
Then I should find "Forum message 1" in the app

When I press "Forum topic 2" in the app
Then I should find "Forum message 2" in the app

# Now confirm the logout.
When I press "Add discussion topic" in the app
And I set the following fields to these values in the app:
| Subject | My happy subject |
| Message | An awesome message |
And I press the user menu button in the app
And I press "Log out" in the app
And I press "OK" in the app
And I wait the app to restart
Then the header should be "Accounts" in the app

Scenario: Switch account when there is unsaved data
Given the following "courses" exist:
| fullname | shortname |
| Course 1 | C1 |
And the following "course enrolments" exist:
| user | course | role |
| student1 | C1 | student |
And the following "activities" exist:
| activity | name | intro | course | idnumber |
| forum | Test forum | Test forum | C1 | forum |
And the following forum discussions exist in course "Course 1":
| forum | user | name | message |
| Test forum | student1 | Forum topic 1 | Forum message 1 |
| Test forum | student1 | Forum topic 2 | Forum message 2 |
And I entered the app as "student2"
And I entered the course "Course 1" as "student1" in the app
And I change viewport size to "1200x640" in the app

When I press "Test forum" in the app
And I press "Add discussion topic" in the app
And I set the following fields to these values in the app:
| Subject | My happy subject |
| Message | An awesome message |
And I press the user menu button in the app
And I press "Switch account" in the app
And I press "pau student2" in the app
Then I should find "Are you sure you want to leave this page?" in the app

# Check that the app continues working fine if the user cancels the switch account.
When I press "Cancel" in the app
And I press "Forum topic 1" in the app
And I press "OK" in the app
Then I should find "Forum message 1" in the app

When I press "Forum topic 2" in the app
Then I should find "Forum message 2" in the app

# Now confirm the switch account.
When I press "Add discussion topic" in the app
And I set the following fields to these values in the app:
| Subject | My happy subject |
| Message | An awesome message |
And I press the user menu button in the app
And I press "Switch account" in the app
And I press "pau student2" in the app
And I press "OK" in the app
And I wait the app to restart
And I press the user menu button in the app
Then I should find "pau student2" in the app
Loading

0 comments on commit ca7434d

Please sign in to comment.