2 // This file is part of Moodle - http://moodle.org/
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
18 * Steps definitions related with the forum activity.
22 * @copyright 2013 David Monllaó
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 // NOTE: no MOODLE_INTERNAL test here, this file may be required by behat before including /config.php.
28 require_once(__DIR__ . '/../../../../lib/behat/behat_base.php');
30 use Behat\Gherkin\Node\TableNode as TableNode;
32 * Forum-related steps definitions.
36 * @copyright 2013 David Monllaó
37 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
39 class behat_mod_forum extends behat_base {
42 * Adds a topic to the forum specified by it's name. Useful for the Announcements and blog-style forums.
44 * @Given /^I add a new topic to "(?P<forum_name_string>(?:[^"]|\\")*)" forum with:$/
45 * @param string $forumname
46 * @param TableNode $table
48 public function i_add_a_new_topic_to_forum_with($forumname, TableNode $table) {
49 $this->add_new_discussion($forumname, $table, get_string('addanewtopic', 'forum'));
53 * 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.
55 * @Given /^I add a new discussion to "(?P<forum_name_string>(?:[^"]|\\")*)" forum with:$/
56 * @param string $forumname
57 * @param TableNode $table
59 public function i_add_a_forum_discussion_to_forum_with($forumname, TableNode $table) {
60 $this->add_new_discussion($forumname, $table, get_string('addanewdiscussion', 'forum'));
64 * 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.
66 * @Given /^I reply "(?P<post_subject_string>(?:[^"]|\\")*)" post from "(?P<forum_name_string>(?:[^"]|\\")*)" forum with:$/
67 * @param string $postname The subject of the post
68 * @param string $forumname The forum name
69 * @param TableNode $table
71 public function i_reply_post_from_forum_with($postsubject, $forumname, TableNode $table) {
74 $this->goto_main_post_reply($postsubject);
76 // Fill form and post.
77 $this->execute('behat_forms::i_set_the_following_fields_to_these_values', $table);
79 $this->execute('behat_forms::press_button', get_string('posttoforum', 'forum'));
80 $this->execute('behat_general::i_wait_to_be_redirected');
84 * Inpage Reply - 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.
86 * @Given /^I reply "(?P<post_subject_string>(?:[^"]|\\")*)" post from "(?P<forum_name_string>(?:[^"]|\\")*)" forum using an inpage reply with:$/
87 * @param string $postsubject The subject of the post
88 * @param string $forumname The forum name
89 * @param TableNode $table
91 public function i_reply_post_from_forum_using_an_inpage_reply_with($postsubject, $forumname, TableNode $table) {
94 $this->execute('behat_general::click_link', $this->escape($forumname));
95 $this->execute('behat_general::click_link', $this->escape($postsubject));
96 $this->execute('behat_general::click_link', get_string('reply', 'forum'));
98 // Fill form and post.
99 $this->execute('behat_forms::i_set_the_following_fields_to_these_values', $table);
101 $this->execute('behat_forms::press_button', get_string('submit', 'core'));
105 * Returns the steps list to add a new discussion to a forum.
107 * Abstracts add a new topic and add a new discussion, as depending
108 * on the forum type the button string changes.
110 * @param string $forumname
111 * @param TableNode $table
112 * @param string $buttonstr
114 protected function add_new_discussion($forumname, TableNode $table, $buttonstr) {
116 // Navigate to forum.
117 $this->execute('behat_general::click_link', $this->escape($forumname));
118 $this->execute('behat_general::click_link', $buttonstr);
119 $this->execute('behat_forms::press_button', get_string('advanced'));
121 // Fill form and post.
122 $this->execute('behat_forms::i_set_the_following_fields_to_these_values', $table);
123 $this->execute('behat_forms::press_button', get_string('posttoforum', 'forum'));
124 $this->execute('behat_general::i_wait_to_be_redirected');
128 * Go to the default reply to post page.
129 * This is used instead of navigating through 4-5 different steps and to solve issues where JS would be required to click
130 * on the advanced button
132 * @param $postsubject
133 * @throws coding_exception
134 * @throws dml_exception
135 * @throws moodle_exception
137 protected function goto_main_post_reply($postsubject) {
139 $post = $DB->get_record("forum_posts", array("subject" => $postsubject), 'id', MUST_EXIST);
140 $url = new moodle_url('/mod/forum/post.php', ['reply' => $post->id]);
141 $this->getSession()->visit($this->locate_path($url->out_as_local_url(false)));