From 6d1b7c52ed4ff866f6ee0140b9567381e715d407 Mon Sep 17 00:00:00 2001
From: Noel De Martin <noel@moodle.com>
Date: Thu, 10 Jun 2021 15:14:51 +0200
Subject: [PATCH] MOBILE-3320 behat: Add navigation tests

---
 mod/messages/tests/behat/navigation.feature   | 42 +++++++++++++++++++
 tests/behat/behat_app.php                     | 23 ++++++++--
 ...s.feature => navigation_deeplinks.feature} | 31 +++++++++++---
 3 files changed, 88 insertions(+), 8 deletions(-)
 create mode 100644 mod/messages/tests/behat/navigation.feature
 rename tests/behat/{navigation_pushnotifications.feature => navigation_deeplinks.feature} (58%)

diff --git a/mod/messages/tests/behat/navigation.feature b/mod/messages/tests/behat/navigation.feature
new file mode 100644
index 000000000..15bded770
--- /dev/null
+++ b/mod/messages/tests/behat/navigation.feature
@@ -0,0 +1,42 @@
+@mod @mod_messages @app @javascript
+Feature: Test messages navigation in the app
+
+  Background:
+    Given the following "users" exist:
+      | username | firstname  |
+      | teacher  | Teacher    |
+      | student  | Student    |
+    And the following "courses" exist:
+      | fullname | shortname |
+      | Course 1 | C1        |
+    And the following "course enrolments" exist:
+      | user    | course | role           |
+      | teacher | C1     | editingteacher |
+      | student | C1     | student        |
+
+  Scenario: Avoid recursive links to profile
+    When I enter the app
+    And I log in as "teacher"
+    And I press "Messages" in the app
+    And I press "Contacts" in the app
+    And I press "Search people and messages" in the app
+    And I set the field "Search" to "student" in the app
+    And I press "Search" "button" in the app
+    And I press "Student" in the app
+    And I set the field "New message" to "Hi there" in the app
+    And I press "Send" in the app
+    Then I should find "Hi there" in the app
+
+    When I press "Display options" in the app
+    And I press "User info" in the app
+    Then I should find "Details" in the app
+
+    When I press "Message" in the app
+    Then I should find "Hi there" in the app
+
+    When I press "Display options" in the app
+    Then I should not find "User info" in the app
+
+    When I press the back button in the app
+    And I press the back button in the app
+    Then I should find "Hi there" in the app
diff --git a/tests/behat/behat_app.php b/tests/behat/behat_app.php
index f74f8c888..081f43fba 100644
--- a/tests/behat/behat_app.php
+++ b/tests/behat/behat_app.php
@@ -484,12 +484,12 @@ class behat_app extends behat_base {
     }
 
     /**
-     * Receives push notifications for forum events.
+     * Receives push notifications.
      *
-     * @Given /^I receive a forum push notification for:$/
+     * @Given /^I receive a push notification in the app for:$/
      * @param TableNode $data
      */
-    public function i_receive_a_forum_push_notification(TableNode $data) {
+    public function i_receive_a_push_notification(TableNode $data) {
         global $DB, $CFG;
 
         $data = (object) $data->getColumnsHash()[0];
@@ -513,6 +513,23 @@ class behat_app extends behat_base {
         $this->wait_for_pending_js();
     }
 
+    /**
+     * Opens a custom link.
+     *
+     * @Given /^I open a custom link in the app for:$/
+     */
+    public function i_open_a_custom_link(TableNode $data) {
+        global $DB, $CFG;
+
+        $data = (object) $data->getColumnsHash()[0];
+        $discussion = $DB->get_record('forum_discussions', ['name' => $data->discussion]);
+        $pageurl = "{$CFG->behat_wwwroot}/mod/forum/discuss.php?d={$discussion->id}";
+        $url = "moodlemobile://link=" . urlencode($pageurl);
+
+        $this->evaluate_script("return window.urlSchemes.handleCustomURL('$url')");
+        $this->wait_for_pending_js();
+    }
+
     /**
      * Closes a popup by clicking on the 'backdrop' behind it.
      *
diff --git a/tests/behat/navigation_pushnotifications.feature b/tests/behat/navigation_deeplinks.feature
similarity index 58%
rename from tests/behat/navigation_pushnotifications.feature
rename to tests/behat/navigation_deeplinks.feature
index d36002e0c..051ae52fc 100644
--- a/tests/behat/navigation_pushnotifications.feature
+++ b/tests/behat/navigation_deeplinks.feature
@@ -1,5 +1,5 @@
 @app @javascript
-Feature: It navigates properly after receiving push notifications.
+Feature: It navigates properly using deep links.
 
   Background:
     Given the following "users" exist:
@@ -22,7 +22,7 @@ Feature: It navigates properly after receiving push notifications.
     And the following config values are set as admin:
       | forcelogout | 1 | tool_mobile |
 
-  Scenario: Open a forum push notification
+  Scenario: Receive a push notification
     When I enter the app
     And I log in as "student2"
     And I press the main menu button in the app
@@ -31,12 +31,33 @@ Feature: It navigates properly after receiving push notifications.
     And I set the field "Your site" to "$WWWROOT" in the app
     And I press "Connect to your site" in the app
     And I log in as "student1"
-    And I receive a forum push notification for:
-      | username | course | module | discussion  |
-      | student2 | C1     | forum  | Forum topic |
+    And I receive a push notification in the app for:
+      | username | module | discussion  |
+      | student2 | forum  | Forum topic |
     Then I should find "Reconnect" in the app
 
     When I set the field "Password" to "student2" in the app
     And I press "Log in" in the app
     Then I should find "Forum topic" in the app
     And I should find "Forum message" in the app
+    But I should not find "Site home" in the app
+
+    When I press the back button in the app
+    Then I should find "Site home" in the app
+    But I should not find "Forum topic" in the app
+    And I should not find "Forum message" in the app
+
+  Scenario: Open a link with a custom URL
+    When I launch the app
+    And I open a custom link in the app for:
+      | discussion  |
+      | Forum topic |
+    And I log in as "student1"
+    Then I should find "Forum topic" in the app
+    And I should find "Forum message" in the app
+    But I should not find "Site home" in the app
+
+    When I press the back button in the app
+    Then I should find "Site home" in the app
+    But I should not find "Forum topic" in the app
+    And I should not find "Forum message" in the app