From 2aff2c19a990fb11813a58367dfbb8b37344da05 Mon Sep 17 00:00:00 2001
From: Peter <peter@moodle.com>
Date: Wed, 27 Mar 2019 15:42:28 +0800
Subject: [PATCH] MDL-65069 mod_forum: Behat test for the inline form

---
 .../local/renderers/discussion_list.php       |  7 +++-
 mod/forum/classes/post_form.php               | 18 +++++----
 .../tests/behat/add_forum_inline.feature      | 32 +++++++++++++++
 mod/forum/tests/behat/behat_mod_forum.php     | 40 +++++++++++++++++++
 4 files changed, 87 insertions(+), 10 deletions(-)
 create mode 100644 mod/forum/tests/behat/add_forum_inline.feature

diff --git a/mod/forum/classes/local/renderers/discussion_list.php b/mod/forum/classes/local/renderers/discussion_list.php
index 307f40201cf..aef97577d4e 100644
--- a/mod/forum/classes/local/renderers/discussion_list.php
+++ b/mod/forum/classes/local/renderers/discussion_list.php
@@ -223,7 +223,8 @@ class discussion_list {
         ];
         $thresholdwarning = forum_check_throttling($forum, $cm);
 
-        $mformpost = new \mod_forum_post_form('post.php', array('course' => $forum->get_course_record(),
+        $formparams = array(
+            'course' => $forum->get_course_record(),
             'cm' => $cm,
             'coursecontext' => $coursecontext,
             'modcontext' => $modcontext,
@@ -233,7 +234,9 @@ class discussion_list {
                 null, $cm),
             'thresholdwarning' => $thresholdwarning,
             'inpagereply' => true,
-            'edit' => 0), 'post', '', array('id' => 'mformforum'));
+            'edit' => 0
+        );
+        $mformpost = new \mod_forum_post_form('post.php', $formparams, 'post', '', array('id' => 'mformforum'));
 
         $params = array('reply' => 0, 'forum' => $forumrecord->id, 'edit' => 0) +
             (isset($post->groupid) ? array('groupid' => $post->groupid) : array()) +
diff --git a/mod/forum/classes/post_form.php b/mod/forum/classes/post_form.php
index b4933cde2c7..40b421dcb69 100644
--- a/mod/forum/classes/post_form.php
+++ b/mod/forum/classes/post_form.php
@@ -93,11 +93,13 @@ class mod_forum_post_form extends moodleform {
         $subscribe = $this->_customdata['subscribe'];
         $edit = $this->_customdata['edit'];
         $thresholdwarning = $this->_customdata['thresholdwarning'];
-        $canreplyprivately = $this->_customdata['canreplyprivately'];
+        $canreplyprivately = array_key_exists('canreplyprivately', $this->_customdata) ?
+            $this->_customdata['canreplyprivately'] : false;
         $stripped = isset($this->_customdata['inpagereply']) ? $this->_customdata['inpagereply'] : false;
 
         if (!$stripped) {
-            $mform->addElement('header', 'general', '');//fill in the data depending on page params later using set_data
+            // Fill in the data depending on page params later using set_data.
+            $mform->addElement('header', 'general', '');
         }
 
         // If there is a warning message and we are not editing a post we need to handle the warning.
@@ -273,9 +275,9 @@ class mod_forum_post_form extends moodleform {
         //-------------------------------------------------------------------------------
         // buttons
         if (isset($post->edit)) { // hack alert
-            $submit_string = get_string('savechanges');
+            $submitstring = get_string('savechanges');
         } else {
-            $submit_string = get_string('posttoforum', 'forum');
+            $submitstring = get_string('posttoforum', 'forum');
         }
 
         // Always register a no submit button so it can be picked up if redirecting to the original post form.
@@ -284,18 +286,18 @@ class mod_forum_post_form extends moodleform {
         // This is an inpage add discussion which requires custom buttons.
         if ($stripped) {
             $buttonarray = array();
-            $buttonarray[] = &$mform->createElement('submit', 'submitbutton', $submit_string);
+            $buttonarray[] = &$mform->createElement('submit', 'submitbutton', $submitstring);
             $buttonarray[] = &$mform->createElement('button', 'cancelbtn',
                 get_string('cancel', 'core'),
-                // Additional attribs to handle collapsible div
+                // Additional attribs to handle collapsible div.
                 ['data-toggle' => 'collapse', 'data-target' => "#collapseAddForm"]);
             $buttonarray[] = &$mform->createElement('submit', 'advancedadddiscussion',
-                get_string('advanced'), ['customclassoverride'=>'btn-link']);
+                get_string('advanced'), ['customclassoverride' => 'btn-link']);
 
             $mform->addGroup($buttonarray, 'buttonar', '', array(' '), false);
             $mform->closeHeaderBefore('buttonar');
         } else {
-            $this->add_action_buttons(true, $submit_string);
+            $this->add_action_buttons(true, $submitstring);
         }
 
         $mform->addElement('hidden', 'course');
diff --git a/mod/forum/tests/behat/add_forum_inline.feature b/mod/forum/tests/behat/add_forum_inline.feature
new file mode 100644
index 00000000000..b7f5d185cc8
--- /dev/null
+++ b/mod/forum/tests/behat/add_forum_inline.feature
@@ -0,0 +1,32 @@
+@mod @mod_forum @javascript
+Feature: Add forum activities and discussions utilizing the inline add discussion form
+
+  Background: Add a forum and a discussion attaching files
+    Given the following "users" exist:
+      | username | firstname | lastname | email                |
+      | teacher1 | Teacher   | 1        | teacher1@example.com |
+      | student1 | Student   | 1        | student1@example.com |
+    And the following "courses" exist:
+      | fullname | shortname | category |
+      | Course 1 | C1        | 0        |
+    And the following "course enrolments" exist:
+      | user     | course | role           |
+      | teacher1 | C1     | editingteacher |
+      | student1 | C1     | student        |
+    And I log in as "teacher1"
+    And I am on "Course 1" course homepage with editing mode on
+    And I add a "Forum" to section "1" and I fill the form with:
+      | Forum name  | Test forum name                |
+      | Forum type  | Standard forum for general use |
+      | Description | Test forum description         |
+    And I add a new discussion to "Test forum name" forum with:
+      | Subject | Forum post 1     |
+      | Message | This is the body |
+    And I log out
+
+  Scenario: Student can add a discussion via the inline form
+    Given I log in as "student1"
+    And I am on "Course 1" course homepage
+    Then I add a new discussion to "Test forum name" forum inline with:
+      | Subject | Post with attachment |
+      | Message | This is the body     |
\ No newline at end of file
diff --git a/mod/forum/tests/behat/behat_mod_forum.php b/mod/forum/tests/behat/behat_mod_forum.php
index e8af7e42c0e..f754b624d8b 100644
--- a/mod/forum/tests/behat/behat_mod_forum.php
+++ b/mod/forum/tests/behat/behat_mod_forum.php
@@ -60,6 +60,18 @@ class behat_mod_forum extends behat_base {
         $this->add_new_discussion($forumname, $table, get_string('addanewdiscussion', 'forum'));
     }
 
+    /**
+     * Adds a discussion to the forum specified by it's name with the provided table data (usually Subject and Message).
+     * The step begins from the forum's course page.
+     *
+     * @Given /^I add a new discussion to "(?P<forum_name_string>(?:[^"]|\\")*)" forum inline with:$/
+     * @param string $forumname
+     * @param TableNode $table
+     */
+    public function i_add_a_forum_discussion_to_forum_inline_with($forumname, TableNode $table) {
+        $this->add_new_discussion_inline($forumname, $table, get_string('addanewdiscussion', 'forum'));
+    }
+
     /**
      * Adds a reply to the specified post of the specified forum. The step begins from the forum's page or from the forum's course page.
      *
@@ -118,6 +130,34 @@ class behat_mod_forum extends behat_base {
         $this->execute('behat_general::click_link', $buttonstr);
         $this->execute('behat_forms::press_button', get_string('advanced'));
 
+        $this->fill_new_discussion_form($table);
+    }
+
+    /**
+     * Returns the steps list to add a new discussion to a forum inline.
+     *
+     * Abstracts add a new topic and add a new discussion, as depending
+     * on the forum type the button string changes.
+     *
+     * @param string $forumname
+     * @param TableNode $table
+     * @param string $buttonstr
+     */
+    protected function add_new_discussion_inline($forumname, TableNode $table, $buttonstr) {
+
+        // Navigate to forum.
+        $this->execute('behat_general::click_link', $this->escape($forumname));
+        $this->execute('behat_general::click_link', $buttonstr);
+        $this->fill_new_discussion_form($table);
+    }
+
+    /**
+     * Fill in the forum's post form and submit. It assumes you've already navigated and enabled the form for view.
+     *
+     * @param TableNode $table
+     * @throws coding_exception
+     */
+    protected function fill_new_discussion_form(TableNode $table) {
         // Fill form and post.
         $this->execute('behat_forms::i_set_the_following_fields_to_these_values', $table);
         $this->execute('behat_forms::press_button', get_string('posttoforum', 'forum'));