MDL-65032 mod_forum: Behat test for locking
[moodle.git] / mod / forum / tests / behat / behat_mod_forum.php
CommitLineData
279d8694
DM
1<?php
2// This file is part of Moodle - http://moodle.org/
3//
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.
8//
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.
13//
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/>.
16
17/**
18 * Steps definitions related with the forum activity.
19 *
20 * @package mod_forum
21 * @category test
22 * @copyright 2013 David MonllaĆ³
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 */
25
26// NOTE: no MOODLE_INTERNAL test here, this file may be required by behat before including /config.php.
27
28require_once(__DIR__ . '/../../../../lib/behat/behat_base.php');
29
eb9ca848 30use Behat\Gherkin\Node\TableNode as TableNode;
279d8694
DM
31/**
32 * Forum-related steps definitions.
33 *
34 * @package mod_forum
35 * @category test
36 * @copyright 2013 David MonllaĆ³
37 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
38 */
39class behat_mod_forum extends behat_base {
40
ee9f6db7 41 /**
265a9bf7 42 * Adds a topic to the forum specified by it's name. Useful for the Announcements and blog-style forums.
ee9f6db7
DM
43 *
44 * @Given /^I add a new topic to "(?P<forum_name_string>(?:[^"]|\\")*)" forum with:$/
45 * @param string $forumname
46 * @param TableNode $table
47 */
48 public function i_add_a_new_topic_to_forum_with($forumname, TableNode $table) {
eb9ca848 49 $this->add_new_discussion($forumname, $table, get_string('addanewtopic', 'forum'));
ee9f6db7
DM
50 }
51
279d8694
DM
52 /**
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.
54 *
55 * @Given /^I add a new discussion to "(?P<forum_name_string>(?:[^"]|\\")*)" forum with:$/
56 * @param string $forumname
57 * @param TableNode $table
58 */
59 public function i_add_a_forum_discussion_to_forum_with($forumname, TableNode $table) {
eb9ca848 60 $this->add_new_discussion($forumname, $table, get_string('addanewdiscussion', 'forum'));
279d8694
DM
61 }
62
68834d4a
DM
63 /**
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.
65 *
66 * @Given /^I reply "(?P<post_subject_string>(?:[^"]|\\")*)" post from "(?P<forum_name_string>(?:[^"]|\\")*)" forum with:$/
d0a9a29b
DM
67 * @param string $postname The subject of the post
68 * @param string $forumname The forum name
68834d4a
DM
69 * @param TableNode $table
70 */
71 public function i_reply_post_from_forum_with($postsubject, $forumname, TableNode $table) {
72
eb9ca848 73 // Navigate to forum.
a0c9b6af 74 $this->goto_main_post_reply($postsubject);
d0a9a29b 75
eb9ca848
RT
76 // Fill form and post.
77 $this->execute('behat_forms::i_set_the_following_fields_to_these_values', $table);
78
79 $this->execute('behat_forms::press_button', get_string('posttoforum', 'forum'));
80 $this->execute('behat_general::i_wait_to_be_redirected');
68834d4a 81 }
ee9f6db7 82
9b4f09ba
P
83 /**
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.
85 *
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
90 */
91 public function i_reply_post_from_forum_using_an_inpage_reply_with($postsubject, $forumname, TableNode $table) {
92
93 // Navigate to forum.
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'));
97
98 // Fill form and post.
99 $this->execute('behat_forms::i_set_the_following_fields_to_these_values', $table);
100
101 $this->execute('behat_forms::press_button', get_string('submit', 'core'));
102 }
103
f5b4320e
P
104 /**
105 * Navigates to a particular discussion page
106 *
107 * @Given /^I navigate to post "(?P<post_subject_string>(?:[^"]|\\")*)" in "(?P<forum_name_string>(?:[^"]|\\")*)" forum$/
108 * @param string $postsubject The subject of the post
109 * @param string $forumname The forum name
110 */
111 public function i_navigate_to_post_in_forum($postsubject, $forumname) {
112
113 // Navigate to forum discussion.
114 $this->execute('behat_general::click_link', $this->escape($forumname));
115 $this->execute('behat_general::click_link', $this->escape($postsubject));
116 }
117
118
ee9f6db7
DM
119 /**
120 * Returns the steps list to add a new discussion to a forum.
121 *
122 * Abstracts add a new topic and add a new discussion, as depending
123 * on the forum type the button string changes.
124 *
125 * @param string $forumname
126 * @param TableNode $table
127 * @param string $buttonstr
ee9f6db7
DM
128 */
129 protected function add_new_discussion($forumname, TableNode $table, $buttonstr) {
130
eb9ca848
RT
131 // Navigate to forum.
132 $this->execute('behat_general::click_link', $this->escape($forumname));
69aa0362 133 $this->execute('behat_general::click_link', $buttonstr);
ee9f6db7 134
eb9ca848
RT
135 // Fill form and post.
136 $this->execute('behat_forms::i_set_the_following_fields_to_these_values', $table);
137 $this->execute('behat_forms::press_button', get_string('posttoforum', 'forum'));
138 $this->execute('behat_general::i_wait_to_be_redirected');
ee9f6db7
DM
139 }
140
a0c9b6af
P
141 /**
142 * Go to the default reply to post page.
143 * This is used instead of navigating through 4-5 different steps and to solve issues where JS would be required to click
144 * on the advanced button
145 *
146 * @param $postsubject
147 * @throws coding_exception
148 * @throws dml_exception
149 * @throws moodle_exception
150 */
151 protected function goto_main_post_reply($postsubject) {
152 global $DB;
153 $post = $DB->get_record("forum_posts", array("subject" => $postsubject), 'id', MUST_EXIST);
154 $url = new moodle_url('/mod/forum/post.php', ['reply' => $post->id]);
155 $this->getSession()->visit($this->locate_path($url->out_as_local_url(false)));
156 }
279d8694 157}