MDL-65032 mod_forum: Behat test for locking
[moodle.git] / mod / forum / tests / generator / lib.php
CommitLineData
f8965a3d
PS
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 * mod_forum data generator
19 *
20 * @package mod_forum
6b219869 21 * @category test
f8965a3d
PS
22 * @copyright 2012 Petr Skoda {@link http://skodak.org}
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 */
25
26defined('MOODLE_INTERNAL') || die();
27
28
29/**
5c3c2c81 30 * Forum module data generator class
f8965a3d
PS
31 *
32 * @package mod_forum
6b219869 33 * @category test
f8965a3d
PS
34 * @copyright 2012 Petr Skoda {@link http://skodak.org}
35 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
36 */
5c3c2c81 37class mod_forum_generator extends testing_module_generator {
f8965a3d 38
c3f31a3d
PS
39 /**
40 * @var int keep track of how many forum discussions have been created.
41 */
42 protected $forumdiscussioncount = 0;
43
44 /**
45 * @var int keep track of how many forum posts have been created.
46 */
47 protected $forumpostcount = 0;
48
b95db263
EL
49 /**
50 * @var int keep track of how many forum subscriptions have been created.
51 */
52 protected $forumsubscriptionscount = 0;
53
c3f31a3d
PS
54 /**
55 * To be called from data reset code only,
56 * do not use in tests.
57 * @return void
58 */
59 public function reset() {
60 $this->forumdiscussioncount = 0;
61 $this->forumpostcount = 0;
b95db263 62 $this->forumsubscriptionscount = 0;
c3f31a3d
PS
63
64 parent::reset();
65 }
66
f8965a3d 67 public function create_instance($record = null, array $options = null) {
6b04fdc0 68 global $CFG;
7fbe33fc 69 require_once($CFG->dirroot.'/mod/forum/lib.php');
f8965a3d 70 $record = (object)(array)$record;
f8965a3d 71
6b04fdc0
PS
72 if (!isset($record->type)) {
73 $record->type = 'general';
74 }
75 if (!isset($record->assessed)) {
76 $record->assessed = 0;
77 }
78 if (!isset($record->scale)) {
79 $record->scale = 0;
80 }
81 if (!isset($record->forcesubscribe)) {
82 $record->forcesubscribe = FORUM_CHOOSESUBSCRIBE;
83 }
f8965a3d 84
7fbe33fc 85 return parent::create_instance($record, (array)$options);
f8965a3d 86 }
c3f31a3d 87
b95db263
EL
88 /**
89 * Function to create a dummy subscription.
90 *
91 * @param array|stdClass $record
92 * @return stdClass the subscription object
93 */
94 public function create_subscription($record = null) {
95 global $DB;
96
97 // Increment the forum subscription count.
98 $this->forumsubscriptionscount++;
99
100 $record = (array)$record;
101
102 if (!isset($record['course'])) {
103 throw new coding_exception('course must be present in phpunit_util::create_subscription() $record');
104 }
105
106 if (!isset($record['forum'])) {
107 throw new coding_exception('forum must be present in phpunit_util::create_subscription() $record');
108 }
109
110 if (!isset($record['userid'])) {
111 throw new coding_exception('userid must be present in phpunit_util::create_subscription() $record');
112 }
113
114 $record = (object)$record;
115
116 // Add the subscription.
117 $record->id = $DB->insert_record('forum_subscriptions', $record);
118
119 return $record;
120 }
121
c3f31a3d
PS
122 /**
123 * Function to create a dummy discussion.
124 *
125 * @param array|stdClass $record
126 * @return stdClass the discussion object
127 */
128 public function create_discussion($record = null) {
129 global $DB;
130
131 // Increment the forum discussion count.
132 $this->forumdiscussioncount++;
133
134 $record = (array) $record;
135
136 if (!isset($record['course'])) {
137 throw new coding_exception('course must be present in phpunit_util::create_discussion() $record');
138 }
139
140 if (!isset($record['forum'])) {
141 throw new coding_exception('forum must be present in phpunit_util::create_discussion() $record');
142 }
143
144 if (!isset($record['userid'])) {
145 throw new coding_exception('userid must be present in phpunit_util::create_discussion() $record');
146 }
147
148 if (!isset($record['name'])) {
149 $record['name'] = "Discussion " . $this->forumdiscussioncount;
150 }
151
152 if (!isset($record['subject'])) {
153 $record['subject'] = "Subject for discussion " . $this->forumdiscussioncount;
154 }
155
156 if (!isset($record['message'])) {
157 $record['message'] = html_writer::tag('p', 'Message for discussion ' . $this->forumdiscussioncount);
158 }
159
160 if (!isset($record['messageformat'])) {
161 $record['messageformat'] = editors_get_preferred_format();
162 }
163
164 if (!isset($record['messagetrust'])) {
165 $record['messagetrust'] = "";
166 }
167
168 if (!isset($record['assessed'])) {
169 $record['assessed'] = '1';
170 }
171
172 if (!isset($record['groupid'])) {
173 $record['groupid'] = "-1";
174 }
175
176 if (!isset($record['timestart'])) {
177 $record['timestart'] = "0";
178 }
179
180 if (!isset($record['timeend'])) {
181 $record['timeend'] = "0";
182 }
183
184 if (!isset($record['mailnow'])) {
185 $record['mailnow'] = "0";
186 }
187
6ec76606
AN
188 if (isset($record['timemodified'])) {
189 $timemodified = $record['timemodified'];
190 }
191
5f219cf1 192 if (!isset($record['pinned'])) {
f9621c56 193 $record['pinned'] = FORUM_DISCUSSION_UNPINNED;
5f219cf1
BK
194 }
195
2893812e 196 if (!isset($record['locked'])) {
f5b4320e 197 $record['locked'] = 0;
2893812e
P
198 }
199
d6ffb027
AN
200 if (isset($record['mailed'])) {
201 $mailed = $record['mailed'];
202 }
203
c3f31a3d
PS
204 $record = (object) $record;
205
206 // Add the discussion.
207 $record->id = forum_add_discussion($record, null, null, $record->userid);
208
d2ba493c 209 $post = $DB->get_record('forum_posts', array('discussion' => $record->id));
6ec76606 210
d2ba493c 211 if (isset($timemodified) || isset($mailed)) {
d6ffb027
AN
212 if (isset($mailed)) {
213 $post->mailed = $mailed;
214 }
215
216 if (isset($timemodified)) {
217 // Enforce the time modified.
218 $record->timemodified = $timemodified;
219 $post->modified = $post->created = $timemodified;
220
221 $DB->update_record('forum_discussions', $record);
222 }
223
6ec76606
AN
224 $DB->update_record('forum_posts', $post);
225 }
226
d2ba493c
AH
227 if (property_exists($record, 'tags')) {
228 $cm = get_coursemodule_from_instance('forum', $record->forum);
229 $tags = is_array($record->tags) ? $record->tags : preg_split('/,/', $record->tags);
230
231 core_tag_tag::set_item_tags('mod_forum', 'forum_posts', $post->id,
232 context_module::instance($cm->id), $tags);
233 }
234
c3f31a3d
PS
235 return $record;
236 }
237
238 /**
239 * Function to create a dummy post.
240 *
241 * @param array|stdClass $record
242 * @return stdClass the post object
243 */
244 public function create_post($record = null) {
245 global $DB;
246
247 // Increment the forum post count.
248 $this->forumpostcount++;
249
250 // Variable to store time.
251 $time = time() + $this->forumpostcount;
252
253 $record = (array) $record;
254
255 if (!isset($record['discussion'])) {
256 throw new coding_exception('discussion must be present in phpunit_util::create_post() $record');
257 }
258
259 if (!isset($record['userid'])) {
260 throw new coding_exception('userid must be present in phpunit_util::create_post() $record');
261 }
262
263 if (!isset($record['parent'])) {
264 $record['parent'] = 0;
265 }
266
267 if (!isset($record['subject'])) {
268 $record['subject'] = 'Forum post subject ' . $this->forumpostcount;
269 }
270
271 if (!isset($record['message'])) {
272 $record['message'] = html_writer::tag('p', 'Forum message post ' . $this->forumpostcount);
273 }
274
275 if (!isset($record['created'])) {
276 $record['created'] = $time;
277 }
278
279 if (!isset($record['modified'])) {
280 $record['modified'] = $time;
281 }
282
e2ede426 283 if (!isset($record['mailed'])) {
fb8840d2 284 $record['mailed'] = 0;
e2ede426
JL
285 }
286
287 if (!isset($record['messageformat'])) {
fb8840d2 288 $record['messageformat'] = 0;
e2ede426
JL
289 }
290
291 if (!isset($record['messagetrust'])) {
fb8840d2 292 $record['messagetrust'] = 0;
e2ede426
JL
293 }
294
295 if (!isset($record['attachment'])) {
296 $record['attachment'] = "";
297 }
298
299 if (!isset($record['totalscore'])) {
300 $record['totalscore'] = 0;
301 }
302
303 if (!isset($record['mailnow'])) {
304 $record['mailnow'] = 0;
305 }
306
47d38303
RW
307 if (!isset($record['deleted'])) {
308 $record['deleted'] = 0;
309 }
310
bc4c7337
AN
311 if (!isset($record['privatereplyto'])) {
312 $record['privatereplyto'] = 0;
313 }
314
c3f31a3d
PS
315 $record = (object) $record;
316
317 // Add the post.
318 $record->id = $DB->insert_record('forum_posts', $record);
319
d2ba493c
AH
320 if (property_exists($record, 'tags')) {
321 $discussion = $DB->get_record('forum_discussions', ['id' => $record->discussion]);
322 $cm = get_coursemodule_from_instance('forum', $discussion->forum);
323 $tags = is_array($record->tags) ? $record->tags : preg_split('/,/', $record->tags);
324
325 core_tag_tag::set_item_tags('mod_forum', 'forum_posts', $record->id,
326 context_module::instance($cm->id), $tags);
327 }
328
c3f31a3d
PS
329 // Update the last post.
330 forum_discussion_update_last_post($record->discussion);
331
332 return $record;
333 }
b6affde2
MG
334
335 public function create_content($instance, $record = array()) {
336 global $USER, $DB;
337 $record = (array)$record + array(
338 'forum' => $instance->id,
339 'userid' => $USER->id,
340 'course' => $instance->course
341 );
342 if (empty($record['discussion']) && empty($record['parent'])) {
343 // Create discussion.
344 $discussion = $this->create_discussion($record);
345 $post = $DB->get_record('forum_posts', array('id' => $discussion->firstpost));
346 } else {
347 // Create post.
348 if (empty($record['parent'])) {
349 $record['parent'] = $DB->get_field('forum_discussions', 'firstpost', array('id' => $record['discussion']), MUST_EXIST);
350 } else if (empty($record['discussion'])) {
351 $record['discussion'] = $DB->get_field('forum_posts', 'discussion', array('id' => $record['parent']), MUST_EXIST);
352 }
353 $post = $this->create_post($record);
354 }
355 return $post;
356 }
8245daba
P
357
358 /**
359 * Extracted from exporter/post.php
360 *
361 * Get the HTML to display as a subheading in a post.
362 *
363 * @param stdClass $exportedauthor The exported author object
364 * @param int $timecreated The post time created timestamp if it's to be displayed
365 * @return string
366 */
367 public function get_author_subheading_html(stdClass $exportedauthor, int $timecreated) : string {
368 $fullname = $exportedauthor->fullname;
369 $profileurl = $exportedauthor->urls['profile'] ?? null;
370 $formatteddate = userdate($timecreated, get_string('strftimedaydatetime', 'core_langconfig'));
371 $name = $profileurl ? "<a href=\"{$profileurl}\">{$fullname}</a>" : $fullname;
372 $date = "<time>{$formatteddate}</time>";
373 return get_string('bynameondate', 'mod_forum', ['name' => $name, 'date' => $date]);
374 }
f8965a3d 375}