MDL-52165 mod_forum: New Web Service mod_forum_can_add_discussion
[moodle.git] / mod / forum / tests / lib_test.php
CommitLineData
62b3b997
FM
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 * The module forums tests
19 *
20 * @package mod_forum
21 * @copyright 2013 Frédéric Massart
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
24
25defined('MOODLE_INTERNAL') || die();
26
e3bbfb52
AN
27global $CFG;
28require_once($CFG->dirroot . '/mod/forum/lib.php');
731c2712 29require_once($CFG->dirroot . '/rating/lib.php');
e3bbfb52 30
62b3b997
FM
31class mod_forum_lib_testcase extends advanced_testcase {
32
59075a43
AN
33 public function setUp() {
34 // We must clear the subscription caches. This has to be done both before each test, and after in case of other
35 // tests using these functions.
36 \mod_forum\subscriptions::reset_forum_cache();
37 }
38
39 public function tearDown() {
40 // We must clear the subscription caches. This has to be done both before each test, and after in case of other
41 // tests using these functions.
42 \mod_forum\subscriptions::reset_forum_cache();
43 }
44
62b3b997
FM
45 public function test_forum_trigger_content_uploaded_event() {
46 $this->resetAfterTest();
47
48 $user = $this->getDataGenerator()->create_user();
49 $course = $this->getDataGenerator()->create_course();
50 $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
51 $context = context_module::instance($forum->cmid);
52
53 $this->setUser($user->id);
54 $fakepost = (object) array('id' => 123, 'message' => 'Yay!', 'discussion' => 100);
74b63eae 55 $cm = get_coursemodule_from_instance('forum', $forum->id);
62b3b997
FM
56
57 $fs = get_file_storage();
58 $dummy = (object) array(
59 'contextid' => $context->id,
60 'component' => 'mod_forum',
61 'filearea' => 'attachment',
62 'itemid' => $fakepost->id,
63 'filepath' => '/',
64 'filename' => 'myassignmnent.pdf'
65 );
66 $fi = $fs->create_file_from_string($dummy, 'Content of ' . $dummy->filename);
67
68 $data = new stdClass();
69 $sink = $this->redirectEvents();
70 forum_trigger_content_uploaded_event($fakepost, $cm, 'some triggered from value');
71 $events = $sink->get_events();
72
73 $this->assertCount(1, $events);
74 $event = reset($events);
75 $this->assertInstanceOf('\mod_forum\event\assessable_uploaded', $event);
76 $this->assertEquals($context->id, $event->contextid);
77 $this->assertEquals($fakepost->id, $event->objectid);
78 $this->assertEquals($fakepost->message, $event->other['content']);
79 $this->assertEquals($fakepost->discussion, $event->other['discussionid']);
80 $this->assertCount(1, $event->other['pathnamehashes']);
81 $this->assertEquals($fi->get_pathnamehash(), $event->other['pathnamehashes'][0]);
82 $expected = new stdClass();
83 $expected->modulename = 'forum';
84 $expected->name = 'some triggered from value';
85 $expected->cmid = $forum->cmid;
86 $expected->itemid = $fakepost->id;
87 $expected->courseid = $course->id;
88 $expected->userid = $user->id;
89 $expected->content = $fakepost->message;
90 $expected->pathnamehashes = array($fi->get_pathnamehash());
91 $this->assertEventLegacyData($expected, $event);
623a32e5 92 $this->assertEventContextNotUsed($event);
62b3b997
FM
93 }
94
de316687
DP
95 public function test_forum_get_courses_user_posted_in() {
96 $this->resetAfterTest();
97
98 $user1 = $this->getDataGenerator()->create_user();
99 $user2 = $this->getDataGenerator()->create_user();
100 $user3 = $this->getDataGenerator()->create_user();
101
102 $course1 = $this->getDataGenerator()->create_course();
103 $course2 = $this->getDataGenerator()->create_course();
104 $course3 = $this->getDataGenerator()->create_course();
105
106 // Create 3 forums, one in each course.
107 $record = new stdClass();
108 $record->course = $course1->id;
109 $forum1 = $this->getDataGenerator()->create_module('forum', $record);
110
111 $record = new stdClass();
112 $record->course = $course2->id;
113 $forum2 = $this->getDataGenerator()->create_module('forum', $record);
114
115 $record = new stdClass();
116 $record->course = $course3->id;
117 $forum3 = $this->getDataGenerator()->create_module('forum', $record);
118
72c2c0bb
DP
119 // Add a second forum in course 1.
120 $record = new stdClass();
121 $record->course = $course1->id;
122 $forum4 = $this->getDataGenerator()->create_module('forum', $record);
123
124 // Add discussions to course 1 started by user1.
de316687
DP
125 $record = new stdClass();
126 $record->course = $course1->id;
127 $record->userid = $user1->id;
128 $record->forum = $forum1->id;
129 $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
130
72c2c0bb
DP
131 $record = new stdClass();
132 $record->course = $course1->id;
133 $record->userid = $user1->id;
134 $record->forum = $forum4->id;
135 $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
136
137 // Add discussions to course2 started by user1.
de316687
DP
138 $record = new stdClass();
139 $record->course = $course2->id;
140 $record->userid = $user1->id;
141 $record->forum = $forum2->id;
142 $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
143
144 // Add discussions to course 3 started by user2.
145 $record = new stdClass();
146 $record->course = $course3->id;
147 $record->userid = $user2->id;
148 $record->forum = $forum3->id;
149 $discussion3 = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
150
151 // Add post to course 3 by user1.
152 $record = new stdClass();
153 $record->course = $course3->id;
154 $record->userid = $user1->id;
155 $record->forum = $forum3->id;
156 $record->discussion = $discussion3->id;
157 $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
158
159 // User 3 hasn't posted anything, so shouldn't get any results.
160 $user3courses = forum_get_courses_user_posted_in($user3);
161 $this->assertEmpty($user3courses);
162
163 // User 2 has only posted in course3.
164 $user2courses = forum_get_courses_user_posted_in($user2);
165 $this->assertCount(1, $user2courses);
166 $user2course = array_shift($user2courses);
167 $this->assertEquals($course3->id, $user2course->id);
168 $this->assertEquals($course3->shortname, $user2course->shortname);
169
170 // User 1 has posted in all 3 courses.
171 $user1courses = forum_get_courses_user_posted_in($user1);
172 $this->assertCount(3, $user1courses);
173 foreach ($user1courses as $course) {
174 $this->assertContains($course->id, array($course1->id, $course2->id, $course3->id));
175 $this->assertContains($course->shortname, array($course1->shortname, $course2->shortname,
176 $course3->shortname));
177
178 }
179
180 // User 1 has only started a discussion in course 1 and 2 though.
181 $user1courses = forum_get_courses_user_posted_in($user1, true);
182 $this->assertCount(2, $user1courses);
183 foreach ($user1courses as $course) {
184 $this->assertContains($course->id, array($course1->id, $course2->id));
185 $this->assertContains($course->shortname, array($course1->shortname, $course2->shortname));
186 }
187 }
bb78e249 188
20e6782f
EM
189 /**
190 * Test the logic in the forum_tp_can_track_forums() function.
191 */
192 public function test_forum_tp_can_track_forums() {
193 global $CFG;
194
195 $this->resetAfterTest();
196
197 $useron = $this->getDataGenerator()->create_user(array('trackforums' => 1));
198 $useroff = $this->getDataGenerator()->create_user(array('trackforums' => 0));
199 $course = $this->getDataGenerator()->create_course();
200 $options = array('course' => $course->id, 'trackingtype' => FORUM_TRACKING_OFF); // Off.
201 $forumoff = $this->getDataGenerator()->create_module('forum', $options);
202
bd8f5d45 203 $options = array('course' => $course->id, 'trackingtype' => FORUM_TRACKING_FORCED); // On.
20e6782f
EM
204 $forumforce = $this->getDataGenerator()->create_module('forum', $options);
205
206 $options = array('course' => $course->id, 'trackingtype' => FORUM_TRACKING_OPTIONAL); // Optional.
207 $forumoptional = $this->getDataGenerator()->create_module('forum', $options);
208
209 // Allow force.
210 $CFG->forum_allowforcedreadtracking = 1;
211
212 // User on, forum off, should be off.
213 $result = forum_tp_can_track_forums($forumoff, $useron);
214 $this->assertEquals(false, $result);
215
216 // User on, forum on, should be on.
217 $result = forum_tp_can_track_forums($forumforce, $useron);
218 $this->assertEquals(true, $result);
219
220 // User on, forum optional, should be on.
221 $result = forum_tp_can_track_forums($forumoptional, $useron);
222 $this->assertEquals(true, $result);
223
224 // User off, forum off, should be off.
225 $result = forum_tp_can_track_forums($forumoff, $useroff);
226 $this->assertEquals(false, $result);
227
228 // User off, forum force, should be on.
229 $result = forum_tp_can_track_forums($forumforce, $useroff);
230 $this->assertEquals(true, $result);
231
232 // User off, forum optional, should be off.
233 $result = forum_tp_can_track_forums($forumoptional, $useroff);
234 $this->assertEquals(false, $result);
235
236 // Don't allow force.
237 $CFG->forum_allowforcedreadtracking = 0;
238
239 // User on, forum off, should be off.
240 $result = forum_tp_can_track_forums($forumoff, $useron);
241 $this->assertEquals(false, $result);
242
243 // User on, forum on, should be on.
244 $result = forum_tp_can_track_forums($forumforce, $useron);
245 $this->assertEquals(true, $result);
246
247 // User on, forum optional, should be on.
248 $result = forum_tp_can_track_forums($forumoptional, $useron);
249 $this->assertEquals(true, $result);
250
251 // User off, forum off, should be off.
252 $result = forum_tp_can_track_forums($forumoff, $useroff);
253 $this->assertEquals(false, $result);
254
255 // User off, forum force, should be off.
256 $result = forum_tp_can_track_forums($forumforce, $useroff);
257 $this->assertEquals(false, $result);
258
259 // User off, forum optional, should be off.
260 $result = forum_tp_can_track_forums($forumoptional, $useroff);
261 $this->assertEquals(false, $result);
262
263 }
264
265 /**
266 * Test the logic in the test_forum_tp_is_tracked() function.
267 */
268 public function test_forum_tp_is_tracked() {
269 global $CFG;
270
271 $this->resetAfterTest();
272
273 $useron = $this->getDataGenerator()->create_user(array('trackforums' => 1));
274 $useroff = $this->getDataGenerator()->create_user(array('trackforums' => 0));
275 $course = $this->getDataGenerator()->create_course();
276 $options = array('course' => $course->id, 'trackingtype' => FORUM_TRACKING_OFF); // Off.
277 $forumoff = $this->getDataGenerator()->create_module('forum', $options);
278
bd8f5d45 279 $options = array('course' => $course->id, 'trackingtype' => FORUM_TRACKING_FORCED); // On.
20e6782f
EM
280 $forumforce = $this->getDataGenerator()->create_module('forum', $options);
281
282 $options = array('course' => $course->id, 'trackingtype' => FORUM_TRACKING_OPTIONAL); // Optional.
283 $forumoptional = $this->getDataGenerator()->create_module('forum', $options);
284
285 // Allow force.
286 $CFG->forum_allowforcedreadtracking = 1;
287
288 // User on, forum off, should be off.
289 $result = forum_tp_is_tracked($forumoff, $useron);
290 $this->assertEquals(false, $result);
291
292 // User on, forum force, should be on.
293 $result = forum_tp_is_tracked($forumforce, $useron);
294 $this->assertEquals(true, $result);
295
296 // User on, forum optional, should be on.
297 $result = forum_tp_is_tracked($forumoptional, $useron);
298 $this->assertEquals(true, $result);
299
300 // User off, forum off, should be off.
301 $result = forum_tp_is_tracked($forumoff, $useroff);
302 $this->assertEquals(false, $result);
303
304 // User off, forum force, should be on.
305 $result = forum_tp_is_tracked($forumforce, $useroff);
306 $this->assertEquals(true, $result);
307
308 // User off, forum optional, should be off.
309 $result = forum_tp_is_tracked($forumoptional, $useroff);
310 $this->assertEquals(false, $result);
311
312 // Don't allow force.
313 $CFG->forum_allowforcedreadtracking = 0;
314
315 // User on, forum off, should be off.
316 $result = forum_tp_is_tracked($forumoff, $useron);
317 $this->assertEquals(false, $result);
318
319 // User on, forum force, should be on.
320 $result = forum_tp_is_tracked($forumforce, $useron);
321 $this->assertEquals(true, $result);
322
323 // User on, forum optional, should be on.
324 $result = forum_tp_is_tracked($forumoptional, $useron);
325 $this->assertEquals(true, $result);
326
327 // User off, forum off, should be off.
328 $result = forum_tp_is_tracked($forumoff, $useroff);
329 $this->assertEquals(false, $result);
330
331 // User off, forum force, should be off.
332 $result = forum_tp_is_tracked($forumforce, $useroff);
333 $this->assertEquals(false, $result);
334
335 // User off, forum optional, should be off.
336 $result = forum_tp_is_tracked($forumoptional, $useroff);
337 $this->assertEquals(false, $result);
338
339 // Stop tracking so we can test again.
340 forum_tp_stop_tracking($forumforce->id, $useron->id);
341 forum_tp_stop_tracking($forumoptional->id, $useron->id);
342 forum_tp_stop_tracking($forumforce->id, $useroff->id);
343 forum_tp_stop_tracking($forumoptional->id, $useroff->id);
344
345 // Allow force.
346 $CFG->forum_allowforcedreadtracking = 1;
347
348 // User on, preference off, forum force, should be on.
349 $result = forum_tp_is_tracked($forumforce, $useron);
350 $this->assertEquals(true, $result);
351
352 // User on, preference off, forum optional, should be on.
353 $result = forum_tp_is_tracked($forumoptional, $useron);
354 $this->assertEquals(false, $result);
355
356 // User off, preference off, forum force, should be on.
357 $result = forum_tp_is_tracked($forumforce, $useroff);
358 $this->assertEquals(true, $result);
359
360 // User off, preference off, forum optional, should be off.
361 $result = forum_tp_is_tracked($forumoptional, $useroff);
362 $this->assertEquals(false, $result);
363
364 // Don't allow force.
365 $CFG->forum_allowforcedreadtracking = 0;
366
367 // User on, preference off, forum force, should be on.
368 $result = forum_tp_is_tracked($forumforce, $useron);
369 $this->assertEquals(false, $result);
370
371 // User on, preference off, forum optional, should be on.
372 $result = forum_tp_is_tracked($forumoptional, $useron);
373 $this->assertEquals(false, $result);
374
375 // User off, preference off, forum force, should be off.
376 $result = forum_tp_is_tracked($forumforce, $useroff);
377 $this->assertEquals(false, $result);
378
379 // User off, preference off, forum optional, should be off.
380 $result = forum_tp_is_tracked($forumoptional, $useroff);
381 $this->assertEquals(false, $result);
382 }
383
384 /**
385 * Test the logic in the forum_tp_get_course_unread_posts() function.
386 */
387 public function test_forum_tp_get_course_unread_posts() {
388 global $CFG;
389
390 $this->resetAfterTest();
391
392 $useron = $this->getDataGenerator()->create_user(array('trackforums' => 1));
393 $useroff = $this->getDataGenerator()->create_user(array('trackforums' => 0));
394 $course = $this->getDataGenerator()->create_course();
395 $options = array('course' => $course->id, 'trackingtype' => FORUM_TRACKING_OFF); // Off.
396 $forumoff = $this->getDataGenerator()->create_module('forum', $options);
397
bd8f5d45 398 $options = array('course' => $course->id, 'trackingtype' => FORUM_TRACKING_FORCED); // On.
20e6782f
EM
399 $forumforce = $this->getDataGenerator()->create_module('forum', $options);
400
401 $options = array('course' => $course->id, 'trackingtype' => FORUM_TRACKING_OPTIONAL); // Optional.
402 $forumoptional = $this->getDataGenerator()->create_module('forum', $options);
403
404 // Add discussions to the tracking off forum.
405 $record = new stdClass();
406 $record->course = $course->id;
407 $record->userid = $useron->id;
408 $record->forum = $forumoff->id;
409 $discussionoff = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
410
411 // Add discussions to the tracking forced forum.
412 $record = new stdClass();
413 $record->course = $course->id;
414 $record->userid = $useron->id;
415 $record->forum = $forumforce->id;
416 $discussionforce = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
417
418 // Add post to the tracking forced discussion.
419 $record = new stdClass();
420 $record->course = $course->id;
421 $record->userid = $useroff->id;
422 $record->forum = $forumforce->id;
423 $record->discussion = $discussionforce->id;
424 $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
425
426 // Add discussions to the tracking optional forum.
427 $record = new stdClass();
428 $record->course = $course->id;
429 $record->userid = $useron->id;
430 $record->forum = $forumoptional->id;
431 $discussionoptional = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
432
433 // Allow force.
434 $CFG->forum_allowforcedreadtracking = 1;
435
436 $result = forum_tp_get_course_unread_posts($useron->id, $course->id);
437 $this->assertEquals(2, count($result));
438 $this->assertEquals(false, isset($result[$forumoff->id]));
439 $this->assertEquals(true, isset($result[$forumforce->id]));
440 $this->assertEquals(2, $result[$forumforce->id]->unread);
441 $this->assertEquals(true, isset($result[$forumoptional->id]));
442 $this->assertEquals(1, $result[$forumoptional->id]->unread);
443
444 $result = forum_tp_get_course_unread_posts($useroff->id, $course->id);
445 $this->assertEquals(1, count($result));
446 $this->assertEquals(false, isset($result[$forumoff->id]));
447 $this->assertEquals(true, isset($result[$forumforce->id]));
448 $this->assertEquals(2, $result[$forumforce->id]->unread);
449 $this->assertEquals(false, isset($result[$forumoptional->id]));
450
451 // Don't allow force.
452 $CFG->forum_allowforcedreadtracking = 0;
453
454 $result = forum_tp_get_course_unread_posts($useron->id, $course->id);
455 $this->assertEquals(2, count($result));
456 $this->assertEquals(false, isset($result[$forumoff->id]));
457 $this->assertEquals(true, isset($result[$forumforce->id]));
458 $this->assertEquals(2, $result[$forumforce->id]->unread);
459 $this->assertEquals(true, isset($result[$forumoptional->id]));
460 $this->assertEquals(1, $result[$forumoptional->id]->unread);
461
462 $result = forum_tp_get_course_unread_posts($useroff->id, $course->id);
463 $this->assertEquals(0, count($result));
464 $this->assertEquals(false, isset($result[$forumoff->id]));
465 $this->assertEquals(false, isset($result[$forumforce->id]));
466 $this->assertEquals(false, isset($result[$forumoptional->id]));
467
468 // Stop tracking so we can test again.
469 forum_tp_stop_tracking($forumforce->id, $useron->id);
470 forum_tp_stop_tracking($forumoptional->id, $useron->id);
471 forum_tp_stop_tracking($forumforce->id, $useroff->id);
472 forum_tp_stop_tracking($forumoptional->id, $useroff->id);
473
474 // Allow force.
475 $CFG->forum_allowforcedreadtracking = 1;
476
477 $result = forum_tp_get_course_unread_posts($useron->id, $course->id);
478 $this->assertEquals(1, count($result));
479 $this->assertEquals(false, isset($result[$forumoff->id]));
480 $this->assertEquals(true, isset($result[$forumforce->id]));
481 $this->assertEquals(2, $result[$forumforce->id]->unread);
482 $this->assertEquals(false, isset($result[$forumoptional->id]));
483
484 $result = forum_tp_get_course_unread_posts($useroff->id, $course->id);
485 $this->assertEquals(1, count($result));
486 $this->assertEquals(false, isset($result[$forumoff->id]));
487 $this->assertEquals(true, isset($result[$forumforce->id]));
488 $this->assertEquals(2, $result[$forumforce->id]->unread);
489 $this->assertEquals(false, isset($result[$forumoptional->id]));
490
491 // Don't allow force.
492 $CFG->forum_allowforcedreadtracking = 0;
493
494 $result = forum_tp_get_course_unread_posts($useron->id, $course->id);
495 $this->assertEquals(0, count($result));
496 $this->assertEquals(false, isset($result[$forumoff->id]));
497 $this->assertEquals(false, isset($result[$forumforce->id]));
498 $this->assertEquals(false, isset($result[$forumoptional->id]));
499
500 $result = forum_tp_get_course_unread_posts($useroff->id, $course->id);
501 $this->assertEquals(0, count($result));
502 $this->assertEquals(false, isset($result[$forumoff->id]));
503 $this->assertEquals(false, isset($result[$forumforce->id]));
504 $this->assertEquals(false, isset($result[$forumoptional->id]));
505 }
506
507 /**
508 * Test the logic in the test_forum_tp_get_untracked_forums() function.
509 */
510 public function test_forum_tp_get_untracked_forums() {
511 global $CFG;
512
513 $this->resetAfterTest();
514
515 $useron = $this->getDataGenerator()->create_user(array('trackforums' => 1));
516 $useroff = $this->getDataGenerator()->create_user(array('trackforums' => 0));
517 $course = $this->getDataGenerator()->create_course();
518 $options = array('course' => $course->id, 'trackingtype' => FORUM_TRACKING_OFF); // Off.
519 $forumoff = $this->getDataGenerator()->create_module('forum', $options);
520
bd8f5d45 521 $options = array('course' => $course->id, 'trackingtype' => FORUM_TRACKING_FORCED); // On.
20e6782f
EM
522 $forumforce = $this->getDataGenerator()->create_module('forum', $options);
523
524 $options = array('course' => $course->id, 'trackingtype' => FORUM_TRACKING_OPTIONAL); // Optional.
525 $forumoptional = $this->getDataGenerator()->create_module('forum', $options);
526
527 // Allow force.
528 $CFG->forum_allowforcedreadtracking = 1;
529
530 // On user with force on.
531 $result = forum_tp_get_untracked_forums($useron->id, $course->id);
532 $this->assertEquals(1, count($result));
533 $this->assertEquals(true, isset($result[$forumoff->id]));
534
535 // Off user with force on.
536 $result = forum_tp_get_untracked_forums($useroff->id, $course->id);
537 $this->assertEquals(2, count($result));
538 $this->assertEquals(true, isset($result[$forumoff->id]));
539 $this->assertEquals(true, isset($result[$forumoptional->id]));
540
541 // Don't allow force.
542 $CFG->forum_allowforcedreadtracking = 0;
543
544 // On user with force off.
545 $result = forum_tp_get_untracked_forums($useron->id, $course->id);
546 $this->assertEquals(1, count($result));
547 $this->assertEquals(true, isset($result[$forumoff->id]));
548
549 // Off user with force off.
550 $result = forum_tp_get_untracked_forums($useroff->id, $course->id);
551 $this->assertEquals(3, count($result));
552 $this->assertEquals(true, isset($result[$forumoff->id]));
553 $this->assertEquals(true, isset($result[$forumoptional->id]));
554 $this->assertEquals(true, isset($result[$forumforce->id]));
555
556 // Stop tracking so we can test again.
557 forum_tp_stop_tracking($forumforce->id, $useron->id);
558 forum_tp_stop_tracking($forumoptional->id, $useron->id);
559 forum_tp_stop_tracking($forumforce->id, $useroff->id);
560 forum_tp_stop_tracking($forumoptional->id, $useroff->id);
561
562 // Allow force.
563 $CFG->forum_allowforcedreadtracking = 1;
564
565 // On user with force on.
566 $result = forum_tp_get_untracked_forums($useron->id, $course->id);
567 $this->assertEquals(2, count($result));
568 $this->assertEquals(true, isset($result[$forumoff->id]));
569 $this->assertEquals(true, isset($result[$forumoptional->id]));
570
571 // Off user with force on.
572 $result = forum_tp_get_untracked_forums($useroff->id, $course->id);
573 $this->assertEquals(2, count($result));
574 $this->assertEquals(true, isset($result[$forumoff->id]));
575 $this->assertEquals(true, isset($result[$forumoptional->id]));
576
577 // Don't allow force.
578 $CFG->forum_allowforcedreadtracking = 0;
579
580 // On user with force off.
581 $result = forum_tp_get_untracked_forums($useron->id, $course->id);
582 $this->assertEquals(3, count($result));
583 $this->assertEquals(true, isset($result[$forumoff->id]));
584 $this->assertEquals(true, isset($result[$forumoptional->id]));
585 $this->assertEquals(true, isset($result[$forumforce->id]));
586
587 // Off user with force off.
588 $result = forum_tp_get_untracked_forums($useroff->id, $course->id);
589 $this->assertEquals(3, count($result));
590 $this->assertEquals(true, isset($result[$forumoff->id]));
591 $this->assertEquals(true, isset($result[$forumoptional->id]));
592 $this->assertEquals(true, isset($result[$forumforce->id]));
593 }
7bb7de44
AN
594
595 /**
596 * Test subscription using automatic subscription on create.
597 */
598 public function test_forum_auto_subscribe_on_create() {
599 global $CFG;
600
601 $this->resetAfterTest();
602
603 $usercount = 5;
604 $course = $this->getDataGenerator()->create_course();
605 $users = array();
606
607 for ($i = 0; $i < $usercount; $i++) {
608 $user = $this->getDataGenerator()->create_user();
609 $users[] = $user;
610 $this->getDataGenerator()->enrol_user($user->id, $course->id);
611 }
612
613 $options = array('course' => $course->id, 'forcesubscribe' => FORUM_INITIALSUBSCRIBE); // Automatic Subscription.
614 $forum = $this->getDataGenerator()->create_module('forum', $options);
615
59075a43 616 $result = \mod_forum\subscriptions::fetch_subscribed_users($forum);
7bb7de44
AN
617 $this->assertEquals($usercount, count($result));
618 foreach ($users as $user) {
59075a43 619 $this->assertTrue(\mod_forum\subscriptions::is_subscribed($user->id, $forum));
7bb7de44
AN
620 }
621 }
622
623 /**
624 * Test subscription using forced subscription on create.
625 */
626 public function test_forum_forced_subscribe_on_create() {
627 global $CFG;
628
629 $this->resetAfterTest();
630
631 $usercount = 5;
632 $course = $this->getDataGenerator()->create_course();
633 $users = array();
634
635 for ($i = 0; $i < $usercount; $i++) {
636 $user = $this->getDataGenerator()->create_user();
637 $users[] = $user;
638 $this->getDataGenerator()->enrol_user($user->id, $course->id);
639 }
640
641 $options = array('course' => $course->id, 'forcesubscribe' => FORUM_FORCESUBSCRIBE); // Forced subscription.
642 $forum = $this->getDataGenerator()->create_module('forum', $options);
643
59075a43 644 $result = \mod_forum\subscriptions::fetch_subscribed_users($forum);
7bb7de44
AN
645 $this->assertEquals($usercount, count($result));
646 foreach ($users as $user) {
59075a43 647 $this->assertTrue(\mod_forum\subscriptions::is_subscribed($user->id, $forum));
7bb7de44
AN
648 }
649 }
650
651 /**
652 * Test subscription using optional subscription on create.
653 */
654 public function test_forum_optional_subscribe_on_create() {
655 global $CFG;
656
657 $this->resetAfterTest();
658
659 $usercount = 5;
660 $course = $this->getDataGenerator()->create_course();
661 $users = array();
662
663 for ($i = 0; $i < $usercount; $i++) {
664 $user = $this->getDataGenerator()->create_user();
665 $users[] = $user;
666 $this->getDataGenerator()->enrol_user($user->id, $course->id);
667 }
668
669 $options = array('course' => $course->id, 'forcesubscribe' => FORUM_CHOOSESUBSCRIBE); // Subscription optional.
670 $forum = $this->getDataGenerator()->create_module('forum', $options);
671
59075a43 672 $result = \mod_forum\subscriptions::fetch_subscribed_users($forum);
7bb7de44
AN
673 // No subscriptions by default.
674 $this->assertEquals(0, count($result));
675 foreach ($users as $user) {
59075a43 676 $this->assertFalse(\mod_forum\subscriptions::is_subscribed($user->id, $forum));
7bb7de44
AN
677 }
678 }
679
680 /**
681 * Test subscription using disallow subscription on create.
682 */
683 public function test_forum_disallow_subscribe_on_create() {
684 global $CFG;
685
686 $this->resetAfterTest();
687
688 $usercount = 5;
689 $course = $this->getDataGenerator()->create_course();
690 $users = array();
691
692 for ($i = 0; $i < $usercount; $i++) {
693 $user = $this->getDataGenerator()->create_user();
694 $users[] = $user;
695 $this->getDataGenerator()->enrol_user($user->id, $course->id);
696 }
697
698 $options = array('course' => $course->id, 'forcesubscribe' => FORUM_DISALLOWSUBSCRIBE); // Subscription prevented.
699 $forum = $this->getDataGenerator()->create_module('forum', $options);
700
59075a43 701 $result = \mod_forum\subscriptions::fetch_subscribed_users($forum);
7bb7de44
AN
702 // No subscriptions by default.
703 $this->assertEquals(0, count($result));
704 foreach ($users as $user) {
59075a43 705 $this->assertFalse(\mod_forum\subscriptions::is_subscribed($user->id, $forum));
7bb7de44
AN
706 }
707 }
59075a43
AN
708
709 /**
710 * Test that context fetching returns the appropriate context.
711 */
712 public function test_forum_get_context() {
713 global $DB, $PAGE;
714
715 $this->resetAfterTest();
716
717 // Setup test data.
718 $course = $this->getDataGenerator()->create_course();
719 $coursecontext = \context_course::instance($course->id);
720
721 $options = array('course' => $course->id, 'forcesubscribe' => FORUM_CHOOSESUBSCRIBE);
722 $forum = $this->getDataGenerator()->create_module('forum', $options);
723 $forumcm = get_coursemodule_from_instance('forum', $forum->id);
724 $forumcontext = \context_module::instance($forumcm->id);
725
726 // First check that specifying the context results in the correct context being returned.
727 // Do this before we set up the page object and we should return from the coursemodule record.
728 // There should be no DB queries here because the context type was correct.
729 $startcount = $DB->perf_get_reads();
730 $result = forum_get_context($forum->id, $forumcontext);
731 $aftercount = $DB->perf_get_reads();
732 $this->assertEquals($forumcontext, $result);
733 $this->assertEquals(0, $aftercount - $startcount);
734
735 // And a context which is not the correct type.
736 // This tests will result in a DB query to fetch the course_module.
737 $startcount = $DB->perf_get_reads();
738 $result = forum_get_context($forum->id, $coursecontext);
739 $aftercount = $DB->perf_get_reads();
740 $this->assertEquals($forumcontext, $result);
741 $this->assertEquals(1, $aftercount - $startcount);
742
743 // Now do not specify a context at all.
744 // This tests will result in a DB query to fetch the course_module.
745 $startcount = $DB->perf_get_reads();
746 $result = forum_get_context($forum->id);
747 $aftercount = $DB->perf_get_reads();
748 $this->assertEquals($forumcontext, $result);
749 $this->assertEquals(1, $aftercount - $startcount);
750
751 // Set up the default page event to use the forum.
752 $PAGE = new moodle_page();
753 $PAGE->set_context($forumcontext);
754 $PAGE->set_cm($forumcm, $course, $forum);
755
756 // Now specify a context which is not a context_module.
757 // There should be no DB queries here because we use the PAGE.
758 $startcount = $DB->perf_get_reads();
759 $result = forum_get_context($forum->id, $coursecontext);
760 $aftercount = $DB->perf_get_reads();
761 $this->assertEquals($forumcontext, $result);
762 $this->assertEquals(0, $aftercount - $startcount);
763
764 // Now do not specify a context at all.
765 // There should be no DB queries here because we use the PAGE.
766 $startcount = $DB->perf_get_reads();
767 $result = forum_get_context($forum->id);
768 $aftercount = $DB->perf_get_reads();
769 $this->assertEquals($forumcontext, $result);
770 $this->assertEquals(0, $aftercount - $startcount);
771
772 // Now specify the page context of the course instead..
773 $PAGE = new moodle_page();
774 $PAGE->set_context($coursecontext);
775
776 // Now specify a context which is not a context_module.
777 // This tests will result in a DB query to fetch the course_module.
778 $startcount = $DB->perf_get_reads();
779 $result = forum_get_context($forum->id, $coursecontext);
780 $aftercount = $DB->perf_get_reads();
781 $this->assertEquals($forumcontext, $result);
782 $this->assertEquals(1, $aftercount - $startcount);
783
784 // Now do not specify a context at all.
785 // This tests will result in a DB query to fetch the course_module.
786 $startcount = $DB->perf_get_reads();
787 $result = forum_get_context($forum->id);
788 $aftercount = $DB->perf_get_reads();
789 $this->assertEquals($forumcontext, $result);
790 $this->assertEquals(1, $aftercount - $startcount);
791 }
826cb6f6
FM
792
793 /**
794 * Test getting the neighbour threads of a discussion.
795 */
796 public function test_forum_get_neighbours() {
797 global $CFG, $DB;
798 $this->resetAfterTest();
799
800 // Setup test data.
801 $forumgen = $this->getDataGenerator()->get_plugin_generator('mod_forum');
802 $course = $this->getDataGenerator()->create_course();
803 $user = $this->getDataGenerator()->create_user();
804 $user2 = $this->getDataGenerator()->create_user();
805
806 $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
807 $cm = get_coursemodule_from_instance('forum', $forum->id);
808 $context = context_module::instance($cm->id);
809
810 $record = new stdClass();
811 $record->course = $course->id;
812 $record->userid = $user->id;
813 $record->forum = $forum->id;
814 $disc1 = $forumgen->create_discussion($record);
815 sleep(1);
816 $disc2 = $forumgen->create_discussion($record);
817 sleep(1);
818 $disc3 = $forumgen->create_discussion($record);
819 sleep(1);
820 $disc4 = $forumgen->create_discussion($record);
821 sleep(1);
822 $disc5 = $forumgen->create_discussion($record);
823
824 // Getting the neighbours.
9e381efd 825 $neighbours = forum_get_discussion_neighbours($cm, $disc1, $forum);
826cb6f6
FM
826 $this->assertEmpty($neighbours['prev']);
827 $this->assertEquals($disc2->id, $neighbours['next']->id);
828
9e381efd 829 $neighbours = forum_get_discussion_neighbours($cm, $disc2, $forum);
826cb6f6
FM
830 $this->assertEquals($disc1->id, $neighbours['prev']->id);
831 $this->assertEquals($disc3->id, $neighbours['next']->id);
832
9e381efd 833 $neighbours = forum_get_discussion_neighbours($cm, $disc3, $forum);
826cb6f6
FM
834 $this->assertEquals($disc2->id, $neighbours['prev']->id);
835 $this->assertEquals($disc4->id, $neighbours['next']->id);
836
9e381efd 837 $neighbours = forum_get_discussion_neighbours($cm, $disc4, $forum);
826cb6f6
FM
838 $this->assertEquals($disc3->id, $neighbours['prev']->id);
839 $this->assertEquals($disc5->id, $neighbours['next']->id);
840
9e381efd 841 $neighbours = forum_get_discussion_neighbours($cm, $disc5, $forum);
826cb6f6
FM
842 $this->assertEquals($disc4->id, $neighbours['prev']->id);
843 $this->assertEmpty($neighbours['next']);
844
845 // Post in some discussions. We manually update the discussion record because
846 // the data generator plays with timemodified in a way that would break this test.
847 sleep(1);
848 $disc1->timemodified = time();
849 $DB->update_record('forum_discussions', $disc1);
850
9e381efd 851 $neighbours = forum_get_discussion_neighbours($cm, $disc5, $forum);
826cb6f6
FM
852 $this->assertEquals($disc4->id, $neighbours['prev']->id);
853 $this->assertEquals($disc1->id, $neighbours['next']->id);
854
9e381efd 855 $neighbours = forum_get_discussion_neighbours($cm, $disc2, $forum);
826cb6f6
FM
856 $this->assertEmpty($neighbours['prev']);
857 $this->assertEquals($disc3->id, $neighbours['next']->id);
858
9e381efd 859 $neighbours = forum_get_discussion_neighbours($cm, $disc1, $forum);
826cb6f6
FM
860 $this->assertEquals($disc5->id, $neighbours['prev']->id);
861 $this->assertEmpty($neighbours['next']);
862
863 // After some discussions were created.
864 sleep(1);
865 $disc6 = $forumgen->create_discussion($record);
9e381efd 866 $neighbours = forum_get_discussion_neighbours($cm, $disc6, $forum);
826cb6f6
FM
867 $this->assertEquals($disc1->id, $neighbours['prev']->id);
868 $this->assertEmpty($neighbours['next']);
869
870 sleep(1);
871 $disc7 = $forumgen->create_discussion($record);
9e381efd 872 $neighbours = forum_get_discussion_neighbours($cm, $disc7, $forum);
826cb6f6
FM
873 $this->assertEquals($disc6->id, $neighbours['prev']->id);
874 $this->assertEmpty($neighbours['next']);
875
876 // Adding timed discussions.
877 $CFG->forum_enabletimedposts = true;
878 $now = time();
879 $past = $now - 60;
880 $future = $now + 60;
881
882 $record = new stdClass();
883 $record->course = $course->id;
884 $record->userid = $user->id;
885 $record->forum = $forum->id;
886 $record->timestart = $past;
887 $record->timeend = $future;
888 sleep(1);
889 $disc8 = $forumgen->create_discussion($record);
890 sleep(1);
891 $record->timestart = $future;
892 $record->timeend = 0;
893 $disc9 = $forumgen->create_discussion($record);
894 sleep(1);
895 $record->timestart = 0;
896 $record->timeend = 0;
897 $disc10 = $forumgen->create_discussion($record);
898 sleep(1);
899 $record->timestart = 0;
900 $record->timeend = $past;
901 $disc11 = $forumgen->create_discussion($record);
902 sleep(1);
903 $record->timestart = $past;
904 $record->timeend = $future;
905 $disc12 = $forumgen->create_discussion($record);
1e366657
AO
906 sleep(1);
907 $record->timestart = $future + 1; // Should be last post for those that can see it.
908 $record->timeend = 0;
909 $disc13 = $forumgen->create_discussion($record);
826cb6f6
FM
910
911 // Admin user ignores the timed settings of discussions.
1e366657
AO
912 // Post ordering taking into account timestart:
913 // 8 = t
914 // 10 = t+3
915 // 11 = t+4
916 // 12 = t+5
917 // 9 = t+60
918 // 13 = t+61.
826cb6f6 919 $this->setAdminUser();
9e381efd 920 $neighbours = forum_get_discussion_neighbours($cm, $disc8, $forum);
826cb6f6 921 $this->assertEquals($disc7->id, $neighbours['prev']->id);
1e366657 922 $this->assertEquals($disc10->id, $neighbours['next']->id);
826cb6f6 923
9e381efd 924 $neighbours = forum_get_discussion_neighbours($cm, $disc9, $forum);
1e366657
AO
925 $this->assertEquals($disc12->id, $neighbours['prev']->id);
926 $this->assertEquals($disc13->id, $neighbours['next']->id);
826cb6f6 927
9e381efd 928 $neighbours = forum_get_discussion_neighbours($cm, $disc10, $forum);
1e366657 929 $this->assertEquals($disc8->id, $neighbours['prev']->id);
826cb6f6
FM
930 $this->assertEquals($disc11->id, $neighbours['next']->id);
931
9e381efd 932 $neighbours = forum_get_discussion_neighbours($cm, $disc11, $forum);
826cb6f6
FM
933 $this->assertEquals($disc10->id, $neighbours['prev']->id);
934 $this->assertEquals($disc12->id, $neighbours['next']->id);
935
9e381efd 936 $neighbours = forum_get_discussion_neighbours($cm, $disc12, $forum);
826cb6f6 937 $this->assertEquals($disc11->id, $neighbours['prev']->id);
1e366657
AO
938 $this->assertEquals($disc9->id, $neighbours['next']->id);
939
940 $neighbours = forum_get_discussion_neighbours($cm, $disc13, $forum);
941 $this->assertEquals($disc9->id, $neighbours['prev']->id);
826cb6f6
FM
942 $this->assertEmpty($neighbours['next']);
943
944 // Normal user can see their own timed discussions.
945 $this->setUser($user);
9e381efd 946 $neighbours = forum_get_discussion_neighbours($cm, $disc8, $forum);
826cb6f6 947 $this->assertEquals($disc7->id, $neighbours['prev']->id);
1e366657 948 $this->assertEquals($disc10->id, $neighbours['next']->id);
826cb6f6 949
9e381efd 950 $neighbours = forum_get_discussion_neighbours($cm, $disc9, $forum);
1e366657
AO
951 $this->assertEquals($disc12->id, $neighbours['prev']->id);
952 $this->assertEquals($disc13->id, $neighbours['next']->id);
826cb6f6 953
9e381efd 954 $neighbours = forum_get_discussion_neighbours($cm, $disc10, $forum);
1e366657 955 $this->assertEquals($disc8->id, $neighbours['prev']->id);
826cb6f6
FM
956 $this->assertEquals($disc11->id, $neighbours['next']->id);
957
9e381efd 958 $neighbours = forum_get_discussion_neighbours($cm, $disc11, $forum);
826cb6f6
FM
959 $this->assertEquals($disc10->id, $neighbours['prev']->id);
960 $this->assertEquals($disc12->id, $neighbours['next']->id);
961
9e381efd 962 $neighbours = forum_get_discussion_neighbours($cm, $disc12, $forum);
826cb6f6 963 $this->assertEquals($disc11->id, $neighbours['prev']->id);
1e366657
AO
964 $this->assertEquals($disc9->id, $neighbours['next']->id);
965
966 $neighbours = forum_get_discussion_neighbours($cm, $disc13, $forum);
967 $this->assertEquals($disc9->id, $neighbours['prev']->id);
826cb6f6
FM
968 $this->assertEmpty($neighbours['next']);
969
970 // Normal user does not ignore timed settings.
971 $this->setUser($user2);
9e381efd 972 $neighbours = forum_get_discussion_neighbours($cm, $disc8, $forum);
826cb6f6
FM
973 $this->assertEquals($disc7->id, $neighbours['prev']->id);
974 $this->assertEquals($disc10->id, $neighbours['next']->id);
975
9e381efd 976 $neighbours = forum_get_discussion_neighbours($cm, $disc10, $forum);
826cb6f6
FM
977 $this->assertEquals($disc8->id, $neighbours['prev']->id);
978 $this->assertEquals($disc12->id, $neighbours['next']->id);
979
9e381efd 980 $neighbours = forum_get_discussion_neighbours($cm, $disc12, $forum);
826cb6f6
FM
981 $this->assertEquals($disc10->id, $neighbours['prev']->id);
982 $this->assertEmpty($neighbours['next']);
983
984 // Reset to normal mode.
985 $CFG->forum_enabletimedposts = false;
986 $this->setAdminUser();
987
988 // Two discussions with identical timemodified ignore each other.
989 sleep(1);
990 $now = time();
991 $DB->update_record('forum_discussions', (object) array('id' => $disc3->id, 'timemodified' => $now));
992 $DB->update_record('forum_discussions', (object) array('id' => $disc2->id, 'timemodified' => $now));
993 $disc2 = $DB->get_record('forum_discussions', array('id' => $disc2->id));
994 $disc3 = $DB->get_record('forum_discussions', array('id' => $disc3->id));
995
9e381efd 996 $neighbours = forum_get_discussion_neighbours($cm, $disc2, $forum);
1e366657 997 $this->assertEquals($disc13->id, $neighbours['prev']->id);
826cb6f6
FM
998 $this->assertEmpty($neighbours['next']);
999
9e381efd 1000 $neighbours = forum_get_discussion_neighbours($cm, $disc3, $forum);
1e366657 1001 $this->assertEquals($disc13->id, $neighbours['prev']->id);
826cb6f6
FM
1002 $this->assertEmpty($neighbours['next']);
1003 }
1004
8d960707
DM
1005 /**
1006 * Test getting the neighbour threads of a blog-like forum.
1007 */
1008 public function test_forum_get_neighbours_blog() {
1009 global $CFG, $DB;
1010 $this->resetAfterTest();
1011
1012 // Setup test data.
1013 $forumgen = $this->getDataGenerator()->get_plugin_generator('mod_forum');
1014 $course = $this->getDataGenerator()->create_course();
1015 $user = $this->getDataGenerator()->create_user();
1016 $user2 = $this->getDataGenerator()->create_user();
1017
1018 $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id, 'type' => 'blog'));
1019 $cm = get_coursemodule_from_instance('forum', $forum->id);
1020 $context = context_module::instance($cm->id);
1021
1022 $record = new stdClass();
1023 $record->course = $course->id;
1024 $record->userid = $user->id;
1025 $record->forum = $forum->id;
1026 $disc1 = $forumgen->create_discussion($record);
1027 sleep(1);
1028 $disc2 = $forumgen->create_discussion($record);
1029 sleep(1);
1030 $disc3 = $forumgen->create_discussion($record);
1031 sleep(1);
1032 $disc4 = $forumgen->create_discussion($record);
1033 sleep(1);
1034 $disc5 = $forumgen->create_discussion($record);
1035
1036 // Getting the neighbours.
1037 $neighbours = forum_get_discussion_neighbours($cm, $disc1, $forum);
1038 $this->assertEmpty($neighbours['prev']);
1039 $this->assertEquals($disc2->id, $neighbours['next']->id);
1040
1041 $neighbours = forum_get_discussion_neighbours($cm, $disc2, $forum);
1042 $this->assertEquals($disc1->id, $neighbours['prev']->id);
1043 $this->assertEquals($disc3->id, $neighbours['next']->id);
1044
1045 $neighbours = forum_get_discussion_neighbours($cm, $disc3, $forum);
1046 $this->assertEquals($disc2->id, $neighbours['prev']->id);
1047 $this->assertEquals($disc4->id, $neighbours['next']->id);
1048
1049 $neighbours = forum_get_discussion_neighbours($cm, $disc4, $forum);
1050 $this->assertEquals($disc3->id, $neighbours['prev']->id);
1051 $this->assertEquals($disc5->id, $neighbours['next']->id);
1052
1053 $neighbours = forum_get_discussion_neighbours($cm, $disc5, $forum);
1054 $this->assertEquals($disc4->id, $neighbours['prev']->id);
1055 $this->assertEmpty($neighbours['next']);
1056
1057 // Make sure that the thread's timemodified does not affect the order.
1058 sleep(1);
1059 $disc1->timemodified = time();
1060 $DB->update_record('forum_discussions', $disc1);
1061
1062 $neighbours = forum_get_discussion_neighbours($cm, $disc1, $forum);
1063 $this->assertEmpty($neighbours['prev']);
1064 $this->assertEquals($disc2->id, $neighbours['next']->id);
1065
1066 $neighbours = forum_get_discussion_neighbours($cm, $disc2, $forum);
1067 $this->assertEquals($disc1->id, $neighbours['prev']->id);
1068 $this->assertEquals($disc3->id, $neighbours['next']->id);
1069
1070 // Add another blog post.
1071 sleep(1);
1072 $disc6 = $forumgen->create_discussion($record);
1073 $neighbours = forum_get_discussion_neighbours($cm, $disc6, $forum);
1074 $this->assertEquals($disc5->id, $neighbours['prev']->id);
1075 $this->assertEmpty($neighbours['next']);
1076
1077 sleep(1);
1078 $disc7 = $forumgen->create_discussion($record);
1079 $neighbours = forum_get_discussion_neighbours($cm, $disc7, $forum);
1080 $this->assertEquals($disc6->id, $neighbours['prev']->id);
1081 $this->assertEmpty($neighbours['next']);
1082
1083 // Adding timed discussions.
1084 $CFG->forum_enabletimedposts = true;
1085 $now = time();
1086 $past = $now - 60;
1087 $future = $now + 60;
1088
1089 $record = new stdClass();
1090 $record->course = $course->id;
1091 $record->userid = $user->id;
1092 $record->forum = $forum->id;
1093 $record->timestart = $past;
1094 $record->timeend = $future;
1095 sleep(1);
1096 $disc8 = $forumgen->create_discussion($record);
1097 sleep(1);
1098 $record->timestart = $future;
1099 $record->timeend = 0;
1100 $disc9 = $forumgen->create_discussion($record);
1101 sleep(1);
1102 $record->timestart = 0;
1103 $record->timeend = 0;
1104 $disc10 = $forumgen->create_discussion($record);
1105 sleep(1);
1106 $record->timestart = 0;
1107 $record->timeend = $past;
1108 $disc11 = $forumgen->create_discussion($record);
1109 sleep(1);
1110 $record->timestart = $past;
1111 $record->timeend = $future;
1112 $disc12 = $forumgen->create_discussion($record);
1113
1114 // Admin user ignores the timed settings of discussions.
1115 $this->setAdminUser();
1116 $neighbours = forum_get_discussion_neighbours($cm, $disc8, $forum);
1117 $this->assertEquals($disc7->id, $neighbours['prev']->id);
1118 $this->assertEquals($disc9->id, $neighbours['next']->id);
1119
1120 $neighbours = forum_get_discussion_neighbours($cm, $disc9, $forum);
1121 $this->assertEquals($disc8->id, $neighbours['prev']->id);
1122 $this->assertEquals($disc10->id, $neighbours['next']->id);
1123
1124 $neighbours = forum_get_discussion_neighbours($cm, $disc10, $forum);
1125 $this->assertEquals($disc9->id, $neighbours['prev']->id);
1126 $this->assertEquals($disc11->id, $neighbours['next']->id);
1127
1128 $neighbours = forum_get_discussion_neighbours($cm, $disc11, $forum);
1129 $this->assertEquals($disc10->id, $neighbours['prev']->id);
1130 $this->assertEquals($disc12->id, $neighbours['next']->id);
1131
1132 $neighbours = forum_get_discussion_neighbours($cm, $disc12, $forum);
1133 $this->assertEquals($disc11->id, $neighbours['prev']->id);
1134 $this->assertEmpty($neighbours['next']);
1135
1136 // Normal user can see their own timed discussions.
1137 $this->setUser($user);
1138 $neighbours = forum_get_discussion_neighbours($cm, $disc8, $forum);
1139 $this->assertEquals($disc7->id, $neighbours['prev']->id);
1140 $this->assertEquals($disc9->id, $neighbours['next']->id);
1141
1142 $neighbours = forum_get_discussion_neighbours($cm, $disc9, $forum);
1143 $this->assertEquals($disc8->id, $neighbours['prev']->id);
1144 $this->assertEquals($disc10->id, $neighbours['next']->id);
1145
1146 $neighbours = forum_get_discussion_neighbours($cm, $disc10, $forum);
1147 $this->assertEquals($disc9->id, $neighbours['prev']->id);
1148 $this->assertEquals($disc11->id, $neighbours['next']->id);
1149
1150 $neighbours = forum_get_discussion_neighbours($cm, $disc11, $forum);
1151 $this->assertEquals($disc10->id, $neighbours['prev']->id);
1152 $this->assertEquals($disc12->id, $neighbours['next']->id);
1153
1154 $neighbours = forum_get_discussion_neighbours($cm, $disc12, $forum);
1155 $this->assertEquals($disc11->id, $neighbours['prev']->id);
1156 $this->assertEmpty($neighbours['next']);
1157
1158 // Normal user does not ignore timed settings.
1159 $this->setUser($user2);
1160 $neighbours = forum_get_discussion_neighbours($cm, $disc8, $forum);
1161 $this->assertEquals($disc7->id, $neighbours['prev']->id);
1162 $this->assertEquals($disc10->id, $neighbours['next']->id);
1163
1164 $neighbours = forum_get_discussion_neighbours($cm, $disc10, $forum);
1165 $this->assertEquals($disc8->id, $neighbours['prev']->id);
1166 $this->assertEquals($disc12->id, $neighbours['next']->id);
1167
1168 $neighbours = forum_get_discussion_neighbours($cm, $disc12, $forum);
1169 $this->assertEquals($disc10->id, $neighbours['prev']->id);
1170 $this->assertEmpty($neighbours['next']);
1171
1172 // Reset to normal mode.
1173 $CFG->forum_enabletimedposts = false;
1174 $this->setAdminUser();
1175
1176 // Two blog posts with identical creation time ignore each other.
1177 sleep(1);
1178 $now = time();
1179 $DB->update_record('forum_posts', (object) array('id' => $disc2->firstpost, 'created' => $now));
1180 $DB->update_record('forum_posts', (object) array('id' => $disc3->firstpost, 'created' => $now));
1181
1182 $neighbours = forum_get_discussion_neighbours($cm, $disc2, $forum);
1183 $this->assertEquals($disc12->id, $neighbours['prev']->id);
1184 $this->assertEmpty($neighbours['next']);
1185
1186 $neighbours = forum_get_discussion_neighbours($cm, $disc3, $forum);
826cb6f6
FM
1187 $this->assertEquals($disc12->id, $neighbours['prev']->id);
1188 $this->assertEmpty($neighbours['next']);
1189 }
1190
1191 /**
1192 * Test getting the neighbour threads of a discussion.
1193 */
1194 public function test_forum_get_neighbours_with_groups() {
1195 $this->resetAfterTest();
1196
1197 // Setup test data.
1198 $forumgen = $this->getDataGenerator()->get_plugin_generator('mod_forum');
1199 $course = $this->getDataGenerator()->create_course();
1200 $group1 = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
1201 $group2 = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
1202 $user1 = $this->getDataGenerator()->create_user();
1203 $user2 = $this->getDataGenerator()->create_user();
1204 $this->getDataGenerator()->enrol_user($user1->id, $course->id);
1205 $this->getDataGenerator()->enrol_user($user2->id, $course->id);
1206 $this->getDataGenerator()->create_group_member(array('userid' => $user1->id, 'groupid' => $group1->id));
1207
1208 $forum1 = $this->getDataGenerator()->create_module('forum', array('course' => $course->id, 'groupmode' => VISIBLEGROUPS));
1209 $forum2 = $this->getDataGenerator()->create_module('forum', array('course' => $course->id, 'groupmode' => SEPARATEGROUPS));
1210 $cm1 = get_coursemodule_from_instance('forum', $forum1->id);
1211 $cm2 = get_coursemodule_from_instance('forum', $forum2->id);
1212 $context1 = context_module::instance($cm1->id);
1213 $context2 = context_module::instance($cm2->id);
1214
1215 // Creating discussions in both forums.
1216 $record = new stdClass();
1217 $record->course = $course->id;
1218 $record->userid = $user1->id;
1219 $record->forum = $forum1->id;
1220 $record->groupid = $group1->id;
1221 $disc11 = $forumgen->create_discussion($record);
1222 $record->forum = $forum2->id;
1223 $disc21 = $forumgen->create_discussion($record);
1224
1225 sleep(1);
1226 $record->userid = $user2->id;
1227 $record->forum = $forum1->id;
1228 $record->groupid = $group2->id;
1229 $disc12 = $forumgen->create_discussion($record);
1230 $record->forum = $forum2->id;
1231 $disc22 = $forumgen->create_discussion($record);
1232
1233 sleep(1);
1234 $record->userid = $user1->id;
1235 $record->forum = $forum1->id;
1236 $record->groupid = null;
1237 $disc13 = $forumgen->create_discussion($record);
1238 $record->forum = $forum2->id;
1239 $disc23 = $forumgen->create_discussion($record);
1240
1241 sleep(1);
1242 $record->userid = $user2->id;
1243 $record->forum = $forum1->id;
1244 $record->groupid = $group2->id;
1245 $disc14 = $forumgen->create_discussion($record);
1246 $record->forum = $forum2->id;
1247 $disc24 = $forumgen->create_discussion($record);
1248
1249 sleep(1);
1250 $record->userid = $user1->id;
1251 $record->forum = $forum1->id;
1252 $record->groupid = $group1->id;
1253 $disc15 = $forumgen->create_discussion($record);
1254 $record->forum = $forum2->id;
1255 $disc25 = $forumgen->create_discussion($record);
1256
1257 // Admin user can see all groups.
1258 $this->setAdminUser();
8d960707
DM
1259 $neighbours = forum_get_discussion_neighbours($cm1, $disc11, $forum1);
1260 $this->assertEmpty($neighbours['prev']);
1261 $this->assertEquals($disc12->id, $neighbours['next']->id);
1262 $neighbours = forum_get_discussion_neighbours($cm2, $disc21, $forum2);
1263 $this->assertEmpty($neighbours['prev']);
1264 $this->assertEquals($disc22->id, $neighbours['next']->id);
1265
1266 $neighbours = forum_get_discussion_neighbours($cm1, $disc12, $forum1);
1267 $this->assertEquals($disc11->id, $neighbours['prev']->id);
1268 $this->assertEquals($disc13->id, $neighbours['next']->id);
1269 $neighbours = forum_get_discussion_neighbours($cm2, $disc22, $forum2);
1270 $this->assertEquals($disc21->id, $neighbours['prev']->id);
1271 $this->assertEquals($disc23->id, $neighbours['next']->id);
1272
1273 $neighbours = forum_get_discussion_neighbours($cm1, $disc13, $forum1);
1274 $this->assertEquals($disc12->id, $neighbours['prev']->id);
1275 $this->assertEquals($disc14->id, $neighbours['next']->id);
1276 $neighbours = forum_get_discussion_neighbours($cm2, $disc23, $forum2);
1277 $this->assertEquals($disc22->id, $neighbours['prev']->id);
1278 $this->assertEquals($disc24->id, $neighbours['next']->id);
1279
1280 $neighbours = forum_get_discussion_neighbours($cm1, $disc14, $forum1);
1281 $this->assertEquals($disc13->id, $neighbours['prev']->id);
1282 $this->assertEquals($disc15->id, $neighbours['next']->id);
1283 $neighbours = forum_get_discussion_neighbours($cm2, $disc24, $forum2);
1284 $this->assertEquals($disc23->id, $neighbours['prev']->id);
1285 $this->assertEquals($disc25->id, $neighbours['next']->id);
1286
1287 $neighbours = forum_get_discussion_neighbours($cm1, $disc15, $forum1);
1288 $this->assertEquals($disc14->id, $neighbours['prev']->id);
1289 $this->assertEmpty($neighbours['next']);
1290 $neighbours = forum_get_discussion_neighbours($cm2, $disc25, $forum2);
1291 $this->assertEquals($disc24->id, $neighbours['prev']->id);
1292 $this->assertEmpty($neighbours['next']);
1293
1294 // Admin user is only viewing group 1.
1295 $_POST['group'] = $group1->id;
1296 $this->assertEquals($group1->id, groups_get_activity_group($cm1, true));
1297 $this->assertEquals($group1->id, groups_get_activity_group($cm2, true));
1298
1299 $neighbours = forum_get_discussion_neighbours($cm1, $disc11, $forum1);
1300 $this->assertEmpty($neighbours['prev']);
1301 $this->assertEquals($disc13->id, $neighbours['next']->id);
1302 $neighbours = forum_get_discussion_neighbours($cm2, $disc21, $forum2);
1303 $this->assertEmpty($neighbours['prev']);
1304 $this->assertEquals($disc23->id, $neighbours['next']->id);
1305
1306 $neighbours = forum_get_discussion_neighbours($cm1, $disc13, $forum1);
1307 $this->assertEquals($disc11->id, $neighbours['prev']->id);
1308 $this->assertEquals($disc15->id, $neighbours['next']->id);
1309 $neighbours = forum_get_discussion_neighbours($cm2, $disc23, $forum2);
1310 $this->assertEquals($disc21->id, $neighbours['prev']->id);
1311 $this->assertEquals($disc25->id, $neighbours['next']->id);
1312
1313 $neighbours = forum_get_discussion_neighbours($cm1, $disc15, $forum1);
1314 $this->assertEquals($disc13->id, $neighbours['prev']->id);
1315 $this->assertEmpty($neighbours['next']);
1316 $neighbours = forum_get_discussion_neighbours($cm2, $disc25, $forum2);
1317 $this->assertEquals($disc23->id, $neighbours['prev']->id);
1318 $this->assertEmpty($neighbours['next']);
1319
1320 // Normal user viewing non-grouped posts (this is only possible in visible groups).
1321 $this->setUser($user1);
1322 $_POST['group'] = 0;
1323 $this->assertEquals(0, groups_get_activity_group($cm1, true));
1324
1325 // They can see anything in visible groups.
1326 $neighbours = forum_get_discussion_neighbours($cm1, $disc12, $forum1);
1327 $this->assertEquals($disc11->id, $neighbours['prev']->id);
1328 $this->assertEquals($disc13->id, $neighbours['next']->id);
1329 $neighbours = forum_get_discussion_neighbours($cm1, $disc13, $forum1);
1330 $this->assertEquals($disc12->id, $neighbours['prev']->id);
1331 $this->assertEquals($disc14->id, $neighbours['next']->id);
1332
1333 // Normal user, orphan of groups, can only see non-grouped posts in separate groups.
1334 $this->setUser($user2);
1335 $_POST['group'] = 0;
1336 $this->assertEquals(0, groups_get_activity_group($cm2, true));
1337
1338 $neighbours = forum_get_discussion_neighbours($cm2, $disc23, $forum2);
1339 $this->assertEmpty($neighbours['prev']);
1340 $this->assertEmpty($neighbours['next']);
1341
1342 $neighbours = forum_get_discussion_neighbours($cm2, $disc22, $forum2);
1343 $this->assertEmpty($neighbours['prev']);
1344 $this->assertEquals($disc23->id, $neighbours['next']->id);
1345
1346 $neighbours = forum_get_discussion_neighbours($cm2, $disc24, $forum2);
1347 $this->assertEquals($disc23->id, $neighbours['prev']->id);
1348 $this->assertEmpty($neighbours['next']);
1349
1350 // Switching to viewing group 1.
1351 $this->setUser($user1);
1352 $_POST['group'] = $group1->id;
1353 $this->assertEquals($group1->id, groups_get_activity_group($cm1, true));
1354 $this->assertEquals($group1->id, groups_get_activity_group($cm2, true));
1355
1356 // They can see non-grouped or same group.
1357 $neighbours = forum_get_discussion_neighbours($cm1, $disc11, $forum1);
1358 $this->assertEmpty($neighbours['prev']);
1359 $this->assertEquals($disc13->id, $neighbours['next']->id);
1360 $neighbours = forum_get_discussion_neighbours($cm2, $disc21, $forum2);
1361 $this->assertEmpty($neighbours['prev']);
1362 $this->assertEquals($disc23->id, $neighbours['next']->id);
1363
1364 $neighbours = forum_get_discussion_neighbours($cm1, $disc13, $forum1);
1365 $this->assertEquals($disc11->id, $neighbours['prev']->id);
1366 $this->assertEquals($disc15->id, $neighbours['next']->id);
1367 $neighbours = forum_get_discussion_neighbours($cm2, $disc23, $forum2);
1368 $this->assertEquals($disc21->id, $neighbours['prev']->id);
1369 $this->assertEquals($disc25->id, $neighbours['next']->id);
1370
1371 $neighbours = forum_get_discussion_neighbours($cm1, $disc15, $forum1);
1372 $this->assertEquals($disc13->id, $neighbours['prev']->id);
1373 $this->assertEmpty($neighbours['next']);
1374 $neighbours = forum_get_discussion_neighbours($cm2, $disc25, $forum2);
1375 $this->assertEquals($disc23->id, $neighbours['prev']->id);
1376 $this->assertEmpty($neighbours['next']);
1377
1378 // Querying the neighbours of a discussion passing the wrong CM.
1379 $this->setExpectedException('coding_exception');
1380 forum_get_discussion_neighbours($cm2, $disc11, $forum2);
1381 }
1382
1383 /**
1384 * Test getting the neighbour threads of a blog-like forum with groups involved.
1385 */
1386 public function test_forum_get_neighbours_with_groups_blog() {
1387 $this->resetAfterTest();
1388
1389 // Setup test data.
1390 $forumgen = $this->getDataGenerator()->get_plugin_generator('mod_forum');
1391 $course = $this->getDataGenerator()->create_course();
1392 $group1 = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
1393 $group2 = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
1394 $user1 = $this->getDataGenerator()->create_user();
1395 $user2 = $this->getDataGenerator()->create_user();
1396 $this->getDataGenerator()->enrol_user($user1->id, $course->id);
1397 $this->getDataGenerator()->enrol_user($user2->id, $course->id);
1398 $this->getDataGenerator()->create_group_member(array('userid' => $user1->id, 'groupid' => $group1->id));
1399
1400 $forum1 = $this->getDataGenerator()->create_module('forum', array('course' => $course->id, 'type' => 'blog',
1401 'groupmode' => VISIBLEGROUPS));
1402 $forum2 = $this->getDataGenerator()->create_module('forum', array('course' => $course->id, 'type' => 'blog',
1403 'groupmode' => SEPARATEGROUPS));
1404 $cm1 = get_coursemodule_from_instance('forum', $forum1->id);
1405 $cm2 = get_coursemodule_from_instance('forum', $forum2->id);
1406 $context1 = context_module::instance($cm1->id);
1407 $context2 = context_module::instance($cm2->id);
1408
1409 // Creating blog posts in both forums.
1410 $record = new stdClass();
1411 $record->course = $course->id;
1412 $record->userid = $user1->id;
1413 $record->forum = $forum1->id;
826cb6f6
FM
1414 $record->groupid = $group1->id;
1415 $disc11 = $forumgen->create_discussion($record);
1416 $record->forum = $forum2->id;
1417 $disc21 = $forumgen->create_discussion($record);
1418
1419 sleep(1);
1420 $record->userid = $user2->id;
1421 $record->forum = $forum1->id;
1422 $record->groupid = $group2->id;
1423 $disc12 = $forumgen->create_discussion($record);
1424 $record->forum = $forum2->id;
1425 $disc22 = $forumgen->create_discussion($record);
1426
1427 sleep(1);
1428 $record->userid = $user1->id;
1429 $record->forum = $forum1->id;
1430 $record->groupid = null;
1431 $disc13 = $forumgen->create_discussion($record);
1432 $record->forum = $forum2->id;
1433 $disc23 = $forumgen->create_discussion($record);
1434
1435 sleep(1);
1436 $record->userid = $user2->id;
1437 $record->forum = $forum1->id;
1438 $record->groupid = $group2->id;
1439 $disc14 = $forumgen->create_discussion($record);
1440 $record->forum = $forum2->id;
1441 $disc24 = $forumgen->create_discussion($record);
1442
1443 sleep(1);
1444 $record->userid = $user1->id;
1445 $record->forum = $forum1->id;
1446 $record->groupid = $group1->id;
1447 $disc15 = $forumgen->create_discussion($record);
1448 $record->forum = $forum2->id;
1449 $disc25 = $forumgen->create_discussion($record);
1450
1451 // Admin user can see all groups.
1452 $this->setAdminUser();
9e381efd 1453 $neighbours = forum_get_discussion_neighbours($cm1, $disc11, $forum1);
826cb6f6
FM
1454 $this->assertEmpty($neighbours['prev']);
1455 $this->assertEquals($disc12->id, $neighbours['next']->id);
9e381efd 1456 $neighbours = forum_get_discussion_neighbours($cm2, $disc21, $forum2);
826cb6f6
FM
1457 $this->assertEmpty($neighbours['prev']);
1458 $this->assertEquals($disc22->id, $neighbours['next']->id);
1459
9e381efd 1460 $neighbours = forum_get_discussion_neighbours($cm1, $disc12, $forum1);
826cb6f6
FM
1461 $this->assertEquals($disc11->id, $neighbours['prev']->id);
1462 $this->assertEquals($disc13->id, $neighbours['next']->id);
9e381efd 1463 $neighbours = forum_get_discussion_neighbours($cm2, $disc22, $forum2);
826cb6f6
FM
1464 $this->assertEquals($disc21->id, $neighbours['prev']->id);
1465 $this->assertEquals($disc23->id, $neighbours['next']->id);
1466
9e381efd 1467 $neighbours = forum_get_discussion_neighbours($cm1, $disc13, $forum1);
826cb6f6
FM
1468 $this->assertEquals($disc12->id, $neighbours['prev']->id);
1469 $this->assertEquals($disc14->id, $neighbours['next']->id);
9e381efd 1470 $neighbours = forum_get_discussion_neighbours($cm2, $disc23, $forum2);
826cb6f6
FM
1471 $this->assertEquals($disc22->id, $neighbours['prev']->id);
1472 $this->assertEquals($disc24->id, $neighbours['next']->id);
1473
9e381efd 1474 $neighbours = forum_get_discussion_neighbours($cm1, $disc14, $forum1);
826cb6f6
FM
1475 $this->assertEquals($disc13->id, $neighbours['prev']->id);
1476 $this->assertEquals($disc15->id, $neighbours['next']->id);
9e381efd 1477 $neighbours = forum_get_discussion_neighbours($cm2, $disc24, $forum2);
826cb6f6
FM
1478 $this->assertEquals($disc23->id, $neighbours['prev']->id);
1479 $this->assertEquals($disc25->id, $neighbours['next']->id);
1480
9e381efd 1481 $neighbours = forum_get_discussion_neighbours($cm1, $disc15, $forum1);
826cb6f6
FM
1482 $this->assertEquals($disc14->id, $neighbours['prev']->id);
1483 $this->assertEmpty($neighbours['next']);
9e381efd 1484 $neighbours = forum_get_discussion_neighbours($cm2, $disc25, $forum2);
826cb6f6
FM
1485 $this->assertEquals($disc24->id, $neighbours['prev']->id);
1486 $this->assertEmpty($neighbours['next']);
1487
1488 // Admin user is only viewing group 1.
1489 $_POST['group'] = $group1->id;
1490 $this->assertEquals($group1->id, groups_get_activity_group($cm1, true));
1491 $this->assertEquals($group1->id, groups_get_activity_group($cm2, true));
1492
9e381efd 1493 $neighbours = forum_get_discussion_neighbours($cm1, $disc11, $forum1);
826cb6f6
FM
1494 $this->assertEmpty($neighbours['prev']);
1495 $this->assertEquals($disc13->id, $neighbours['next']->id);
9e381efd 1496 $neighbours = forum_get_discussion_neighbours($cm2, $disc21, $forum2);
826cb6f6
FM
1497 $this->assertEmpty($neighbours['prev']);
1498 $this->assertEquals($disc23->id, $neighbours['next']->id);
1499
9e381efd 1500 $neighbours = forum_get_discussion_neighbours($cm1, $disc13, $forum1);
826cb6f6
FM
1501 $this->assertEquals($disc11->id, $neighbours['prev']->id);
1502 $this->assertEquals($disc15->id, $neighbours['next']->id);
9e381efd 1503 $neighbours = forum_get_discussion_neighbours($cm2, $disc23, $forum2);
826cb6f6
FM
1504 $this->assertEquals($disc21->id, $neighbours['prev']->id);
1505 $this->assertEquals($disc25->id, $neighbours['next']->id);
1506
9e381efd 1507 $neighbours = forum_get_discussion_neighbours($cm1, $disc15, $forum1);
826cb6f6
FM
1508 $this->assertEquals($disc13->id, $neighbours['prev']->id);
1509 $this->assertEmpty($neighbours['next']);
9e381efd 1510 $neighbours = forum_get_discussion_neighbours($cm2, $disc25, $forum2);
826cb6f6
FM
1511 $this->assertEquals($disc23->id, $neighbours['prev']->id);
1512 $this->assertEmpty($neighbours['next']);
1513
1514 // Normal user viewing non-grouped posts (this is only possible in visible groups).
1515 $this->setUser($user1);
1516 $_POST['group'] = 0;
1517 $this->assertEquals(0, groups_get_activity_group($cm1, true));
1518
1519 // They can see anything in visible groups.
9e381efd 1520 $neighbours = forum_get_discussion_neighbours($cm1, $disc12, $forum1);
826cb6f6
FM
1521 $this->assertEquals($disc11->id, $neighbours['prev']->id);
1522 $this->assertEquals($disc13->id, $neighbours['next']->id);
9e381efd 1523 $neighbours = forum_get_discussion_neighbours($cm1, $disc13, $forum1);
826cb6f6
FM
1524 $this->assertEquals($disc12->id, $neighbours['prev']->id);
1525 $this->assertEquals($disc14->id, $neighbours['next']->id);
1526
1527 // Normal user, orphan of groups, can only see non-grouped posts in separate groups.
1528 $this->setUser($user2);
1529 $_POST['group'] = 0;
1530 $this->assertEquals(0, groups_get_activity_group($cm2, true));
1531
9e381efd 1532 $neighbours = forum_get_discussion_neighbours($cm2, $disc23, $forum2);
826cb6f6
FM
1533 $this->assertEmpty($neighbours['prev']);
1534 $this->assertEmpty($neighbours['next']);
1535
9e381efd 1536 $neighbours = forum_get_discussion_neighbours($cm2, $disc22, $forum2);
826cb6f6
FM
1537 $this->assertEmpty($neighbours['prev']);
1538 $this->assertEquals($disc23->id, $neighbours['next']->id);
1539
9e381efd 1540 $neighbours = forum_get_discussion_neighbours($cm2, $disc24, $forum2);
826cb6f6
FM
1541 $this->assertEquals($disc23->id, $neighbours['prev']->id);
1542 $this->assertEmpty($neighbours['next']);
1543
1544 // Switching to viewing group 1.
1545 $this->setUser($user1);
1546 $_POST['group'] = $group1->id;
1547 $this->assertEquals($group1->id, groups_get_activity_group($cm1, true));
1548 $this->assertEquals($group1->id, groups_get_activity_group($cm2, true));
1549
1550 // They can see non-grouped or same group.
9e381efd 1551 $neighbours = forum_get_discussion_neighbours($cm1, $disc11, $forum1);
826cb6f6
FM
1552 $this->assertEmpty($neighbours['prev']);
1553 $this->assertEquals($disc13->id, $neighbours['next']->id);
9e381efd 1554 $neighbours = forum_get_discussion_neighbours($cm2, $disc21, $forum2);
826cb6f6
FM
1555 $this->assertEmpty($neighbours['prev']);
1556 $this->assertEquals($disc23->id, $neighbours['next']->id);
1557
9e381efd 1558 $neighbours = forum_get_discussion_neighbours($cm1, $disc13, $forum1);
826cb6f6
FM
1559 $this->assertEquals($disc11->id, $neighbours['prev']->id);
1560 $this->assertEquals($disc15->id, $neighbours['next']->id);
9e381efd 1561 $neighbours = forum_get_discussion_neighbours($cm2, $disc23, $forum2);
826cb6f6
FM
1562 $this->assertEquals($disc21->id, $neighbours['prev']->id);
1563 $this->assertEquals($disc25->id, $neighbours['next']->id);
1564
9e381efd 1565 $neighbours = forum_get_discussion_neighbours($cm1, $disc15, $forum1);
826cb6f6
FM
1566 $this->assertEquals($disc13->id, $neighbours['prev']->id);
1567 $this->assertEmpty($neighbours['next']);
9e381efd 1568 $neighbours = forum_get_discussion_neighbours($cm2, $disc25, $forum2);
826cb6f6
FM
1569 $this->assertEquals($disc23->id, $neighbours['prev']->id);
1570 $this->assertEmpty($neighbours['next']);
1571
1572 // Querying the neighbours of a discussion passing the wrong CM.
1573 $this->setExpectedException('coding_exception');
9e381efd 1574 forum_get_discussion_neighbours($cm2, $disc11, $forum2);
826cb6f6 1575 }
94802d35
AN
1576
1577 public function test_count_discussion_replies_basic() {
1578 list($forum, $discussionids) = $this->create_multiple_discussions_with_replies(10, 5);
1579
1580 // Count the discussion replies in the forum.
1581 $result = forum_count_discussion_replies($forum->id);
1582 $this->assertCount(10, $result);
1583 }
1584
1585 public function test_count_discussion_replies_limited() {
1586 list($forum, $discussionids) = $this->create_multiple_discussions_with_replies(10, 5);
1587 // Adding limits shouldn't make a difference.
1588 $result = forum_count_discussion_replies($forum->id, "", 20);
1589 $this->assertCount(10, $result);
1590 }
1591
1592 public function test_count_discussion_replies_paginated() {
1593 list($forum, $discussionids) = $this->create_multiple_discussions_with_replies(10, 5);
1594 // Adding paging shouldn't make any difference.
1595 $result = forum_count_discussion_replies($forum->id, "", -1, 0, 100);
1596 $this->assertCount(10, $result);
1597 }
1598
1599 public function test_count_discussion_replies_paginated_sorted() {
1600 list($forum, $discussionids) = $this->create_multiple_discussions_with_replies(10, 5);
1601 // Specifying the forumsort should also give a good result. This follows a different path.
1602 $result = forum_count_discussion_replies($forum->id, "d.id asc", -1, 0, 100);
1603 $this->assertCount(10, $result);
1604 foreach ($result as $row) {
1605 // Grab the first discussionid.
1606 $discussionid = array_shift($discussionids);
1607 $this->assertEquals($discussionid, $row->discussion);
1608 }
1609 }
1610
1611 public function test_count_discussion_replies_limited_sorted() {
1612 list($forum, $discussionids) = $this->create_multiple_discussions_with_replies(10, 5);
1613 // Adding limits, and a forumsort shouldn't make a difference.
1614 $result = forum_count_discussion_replies($forum->id, "d.id asc", 20);
1615 $this->assertCount(10, $result);
1616 foreach ($result as $row) {
1617 // Grab the first discussionid.
1618 $discussionid = array_shift($discussionids);
1619 $this->assertEquals($discussionid, $row->discussion);
1620 }
1621 }
1622
1623 public function test_count_discussion_replies_paginated_sorted_small() {
1624 list($forum, $discussionids) = $this->create_multiple_discussions_with_replies(10, 5);
1625 // Grabbing a smaller subset and they should be ordered as expected.
1626 $result = forum_count_discussion_replies($forum->id, "d.id asc", -1, 0, 5);
1627 $this->assertCount(5, $result);
1628 foreach ($result as $row) {
1629 // Grab the first discussionid.
1630 $discussionid = array_shift($discussionids);
1631 $this->assertEquals($discussionid, $row->discussion);
1632 }
1633 }
1634
1635 public function test_count_discussion_replies_paginated_sorted_small_reverse() {
1636 list($forum, $discussionids) = $this->create_multiple_discussions_with_replies(10, 5);
1637 // Grabbing a smaller subset and they should be ordered as expected.
1638 $result = forum_count_discussion_replies($forum->id, "d.id desc", -1, 0, 5);
1639 $this->assertCount(5, $result);
1640 foreach ($result as $row) {
1641 // Grab the last discussionid.
1642 $discussionid = array_pop($discussionids);
1643 $this->assertEquals($discussionid, $row->discussion);
1644 }
1645 }
1646
1647 public function test_count_discussion_replies_limited_sorted_small_reverse() {
1648 list($forum, $discussionids) = $this->create_multiple_discussions_with_replies(10, 5);
1649 // Adding limits, and a forumsort shouldn't make a difference.
1650 $result = forum_count_discussion_replies($forum->id, "d.id desc", 5);
1651 $this->assertCount(5, $result);
1652 foreach ($result as $row) {
1653 // Grab the last discussionid.
1654 $discussionid = array_pop($discussionids);
1655 $this->assertEquals($discussionid, $row->discussion);
1656 }
1657 }
1658
ebfa74c7
JL
1659 public function test_forum_view() {
1660 global $CFG;
1661
1662 $CFG->enablecompletion = 1;
1663 $this->resetAfterTest();
1664
1665 // Setup test data.
1666 $course = $this->getDataGenerator()->create_course(array('enablecompletion' => 1));
1667 $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id),
1668 array('completion' => 2, 'completionview' => 1));
1669 $context = context_module::instance($forum->cmid);
1670 $cm = get_coursemodule_from_instance('forum', $forum->id);
1671
1672 // Trigger and capture the event.
1673 $sink = $this->redirectEvents();
1674
1675 $this->setAdminUser();
1676 forum_view($forum, $course, $cm, $context);
1677
1678 $events = $sink->get_events();
1679 // 2 additional events thanks to completion.
1680 $this->assertCount(3, $events);
1681 $event = array_pop($events);
1682
1683 // Checking that the event contains the expected values.
1684 $this->assertInstanceOf('\mod_forum\event\course_module_viewed', $event);
1685 $this->assertEquals($context, $event->get_context());
1686 $url = new \moodle_url('/mod/forum/view.php', array('f' => $forum->id));
1687 $this->assertEquals($url, $event->get_url());
1688 $this->assertEventContextNotUsed($event);
1689 $this->assertNotEmpty($event->get_name());
1690
1691 // Check completion status.
1692 $completion = new completion_info($course);
1693 $completiondata = $completion->get_data($cm);
1694 $this->assertEquals(1, $completiondata->completionstate);
1695
1696 }
1697
6e5df52c
JL
1698 /**
1699 * Test forum_discussion_view.
1700 */
1701 public function test_forum_discussion_view() {
1702 global $CFG, $USER;
1703
1704 $this->resetAfterTest();
1705
1706 // Setup test data.
1707 $course = $this->getDataGenerator()->create_course();
1708 $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
1709 $discussion = $this->create_single_discussion_with_replies($forum, $USER, 2);
1710
1711 $context = context_module::instance($forum->cmid);
1712 $cm = get_coursemodule_from_instance('forum', $forum->id);
1713
1714 // Trigger and capture the event.
1715 $sink = $this->redirectEvents();
1716
1717 $this->setAdminUser();
1718 forum_discussion_view($context, $forum, $discussion);
1719
1720 $events = $sink->get_events();
1721 $this->assertCount(1, $events);
1722 $event = array_pop($events);
1723
1724 // Checking that the event contains the expected values.
1725 $this->assertInstanceOf('\mod_forum\event\discussion_viewed', $event);
1726 $this->assertEquals($context, $event->get_context());
1727 $expected = array($course->id, 'forum', 'view discussion', "discuss.php?d={$discussion->id}",
1728 $discussion->id, $forum->cmid);
1729 $this->assertEventLegacyLogData($expected, $event);
1730 $this->assertEventContextNotUsed($event);
1731
1732 $this->assertNotEmpty($event->get_name());
1733
1734 }
1735
94802d35
AN
1736 /**
1737 * Create a new course, forum, and user with a number of discussions and replies.
1738 *
1739 * @param int $discussioncount The number of discussions to create
1740 * @param int $replycount The number of replies to create in each discussion
1741 * @return array Containing the created forum object, and the ids of the created discussions.
1742 */
1743 protected function create_multiple_discussions_with_replies($discussioncount, $replycount) {
1744 $this->resetAfterTest();
1745
1746 // Setup the content.
1747 $user = $this->getDataGenerator()->create_user();
1748 $course = $this->getDataGenerator()->create_course();
1749 $record = new stdClass();
1750 $record->course = $course->id;
1751 $forum = $this->getDataGenerator()->create_module('forum', $record);
1752
1753 // Create 10 discussions with replies.
1754 $discussionids = array();
1755 for ($i = 0; $i < $discussioncount; $i++) {
1756 $discussion = $this->create_single_discussion_with_replies($forum, $user, $replycount);
1757 $discussionids[] = $discussion->id;
1758 }
1759 return array($forum, $discussionids);
1760 }
1761
1762 /**
1763 * Create a discussion with a number of replies.
1764 *
1765 * @param object $forum The forum which has been created
1766 * @param object $user The user making the discussion and replies
1767 * @param int $replycount The number of replies
1768 * @return object $discussion
1769 */
1770 protected function create_single_discussion_with_replies($forum, $user, $replycount) {
1771 global $DB;
1772
1773 $generator = self::getDataGenerator()->get_plugin_generator('mod_forum');
1774
1775 $record = new stdClass();
1776 $record->course = $forum->course;
1777 $record->forum = $forum->id;
1778 $record->userid = $user->id;
1779 $discussion = $generator->create_discussion($record);
1780
1781 // Retrieve the first post.
1782 $replyto = $DB->get_record('forum_posts', array('discussion' => $discussion->id));
1783
1784 // Create the replies.
1785 $post = new stdClass();
1786 $post->userid = $user->id;
1787 $post->discussion = $discussion->id;
1788 $post->parent = $replyto->id;
1789
1790 for ($i = 0; $i < $replycount; $i++) {
1791 $generator->create_post($post);
1792 }
1793
1794 return $discussion;
1795 }
1796
731c2712
AA
1797 /**
1798 * Tests for mod_forum_rating_can_see_item_ratings().
1799 *
1800 * @throws coding_exception
1801 * @throws rating_exception
1802 */
1803 public function test_mod_forum_rating_can_see_item_ratings() {
1804 global $DB;
1805
1806 $this->resetAfterTest();
1807
1808 // Setup test data.
1809 $course = new stdClass();
1810 $course->groupmode = SEPARATEGROUPS;
1811 $course->groupmodeforce = true;
1812 $course = $this->getDataGenerator()->create_course($course);
1813 $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
1814 $generator = self::getDataGenerator()->get_plugin_generator('mod_forum');
1815 $cm = get_coursemodule_from_instance('forum', $forum->id);
1816 $context = context_module::instance($cm->id);
1817
1818 // Create users.
1819 $user1 = $this->getDataGenerator()->create_user();
1820 $user2 = $this->getDataGenerator()->create_user();
1821 $user3 = $this->getDataGenerator()->create_user();
1822 $user4 = $this->getDataGenerator()->create_user();
1823
1824 // Groups and stuff.
1825 $role = $DB->get_record('role', array('shortname' => 'teacher'), '*', MUST_EXIST);
1826 $this->getDataGenerator()->enrol_user($user1->id, $course->id, $role->id);
1827 $this->getDataGenerator()->enrol_user($user2->id, $course->id, $role->id);
1828 $this->getDataGenerator()->enrol_user($user3->id, $course->id, $role->id);
1829 $this->getDataGenerator()->enrol_user($user4->id, $course->id, $role->id);
1830
1831 $group1 = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
1832 $group2 = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
1833 groups_add_member($group1, $user1);
1834 groups_add_member($group1, $user2);
1835 groups_add_member($group2, $user3);
1836 groups_add_member($group2, $user4);
1837
1838 $record = new stdClass();
1839 $record->course = $forum->course;
1840 $record->forum = $forum->id;
1841 $record->userid = $user1->id;
1842 $record->groupid = $group1->id;
1843 $discussion = $generator->create_discussion($record);
1844
1845 // Retrieve the first post.
1846 $post = $DB->get_record('forum_posts', array('discussion' => $discussion->id));
1847
1848 $ratingoptions = new stdClass;
1849 $ratingoptions->context = $context;
1850 $ratingoptions->ratingarea = 'post';
1851 $ratingoptions->component = 'mod_forum';
1852 $ratingoptions->itemid = $post->id;
1853 $ratingoptions->scaleid = 2;
1854 $ratingoptions->userid = $user2->id;
1855 $rating = new rating($ratingoptions);
1856 $rating->update_rating(2);
1857
1858 // Now try to access it as various users.
1859 unassign_capability('moodle/site:accessallgroups', $role->id);
1860 $params = array('contextid' => 2,
1861 'component' => 'mod_forum',
1862 'ratingarea' => 'post',
1863 'itemid' => $post->id,
1864 'scaleid' => 2);
1865 $this->setUser($user1);
1866 $this->assertTrue(mod_forum_rating_can_see_item_ratings($params));
1867 $this->setUser($user2);
1868 $this->assertTrue(mod_forum_rating_can_see_item_ratings($params));
1869 $this->setUser($user3);
1870 $this->assertFalse(mod_forum_rating_can_see_item_ratings($params));
1871 $this->setUser($user4);
1872 $this->assertFalse(mod_forum_rating_can_see_item_ratings($params));
1873
1874 // Now try with accessallgroups cap and make sure everything is visible.
1875 assign_capability('moodle/site:accessallgroups', CAP_ALLOW, $role->id, $context->id);
1876 $this->setUser($user1);
1877 $this->assertTrue(mod_forum_rating_can_see_item_ratings($params));
1878 $this->setUser($user2);
1879 $this->assertTrue(mod_forum_rating_can_see_item_ratings($params));
1880 $this->setUser($user3);
1881 $this->assertTrue(mod_forum_rating_can_see_item_ratings($params));
1882 $this->setUser($user4);
1883 $this->assertTrue(mod_forum_rating_can_see_item_ratings($params));
1884
1885 // Change group mode and verify visibility.
1886 $course->groupmode = VISIBLEGROUPS;
1887 $DB->update_record('course', $course);
1888 unassign_capability('moodle/site:accessallgroups', $role->id);
1889 $this->setUser($user1);
1890 $this->assertTrue(mod_forum_rating_can_see_item_ratings($params));
1891 $this->setUser($user2);
1892 $this->assertTrue(mod_forum_rating_can_see_item_ratings($params));
1893 $this->setUser($user3);
1894 $this->assertTrue(mod_forum_rating_can_see_item_ratings($params));
1895 $this->setUser($user4);
1896 $this->assertTrue(mod_forum_rating_can_see_item_ratings($params));
1897
1898 }
1899
4f3a2d21
JL
1900 /**
1901 * Test forum_get_discussions
1902 */
1903 public function test_forum_get_discussions_with_groups() {
1904 global $DB;
1905
1906 $this->resetAfterTest(true);
1907
1908 // Create course to add the module.
1909 $course = self::getDataGenerator()->create_course(array('groupmode' => VISIBLEGROUPS, 'groupmodeforce' => 0));
1910 $user1 = self::getDataGenerator()->create_user();
1911 $user2 = self::getDataGenerator()->create_user();
1912 $user3 = self::getDataGenerator()->create_user();
1913
1914 $role = $DB->get_record('role', array('shortname' => 'student'), '*', MUST_EXIST);
1915 self::getDataGenerator()->enrol_user($user1->id, $course->id, $role->id);
1916 self::getDataGenerator()->enrol_user($user2->id, $course->id, $role->id);
1917 self::getDataGenerator()->enrol_user($user3->id, $course->id, $role->id);
1918
1919 // Forum forcing separate gropus.
1920 $record = new stdClass();
1921 $record->course = $course->id;
1922 $forum = self::getDataGenerator()->create_module('forum', $record, array('groupmode' => SEPARATEGROUPS));
1923 $cm = get_coursemodule_from_instance('forum', $forum->id);
1924
1925 // Create groups.
1926 $group1 = self::getDataGenerator()->create_group(array('courseid' => $course->id));
1927 $group2 = self::getDataGenerator()->create_group(array('courseid' => $course->id));
1928 $group3 = self::getDataGenerator()->create_group(array('courseid' => $course->id));
1929
1930 // Add the user1 to g1 and g2 groups.
1931 groups_add_member($group1->id, $user1->id);
1932 groups_add_member($group2->id, $user1->id);
1933
1934 // Add the user 2 and 3 to only one group.
1935 groups_add_member($group1->id, $user2->id);
1936 groups_add_member($group3->id, $user3->id);
1937
1938 // Add a few discussions.
1939 $record = array();
1940 $record['course'] = $course->id;
1941 $record['forum'] = $forum->id;
1942 $record['userid'] = $user1->id;
1943 $record['groupid'] = $group1->id;
1944 $discussiong1u1 = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1945
1946 $record['groupid'] = $group2->id;
1947 $discussiong2u1 = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1948
1949 $record['userid'] = $user2->id;
1950 $record['groupid'] = $group1->id;
1951 $discussiong1u2 = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1952
1953 $record['userid'] = $user3->id;
1954 $record['groupid'] = $group3->id;
1955 $discussiong3u3 = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1956
1957 self::setUser($user1);
1958 // Test retrieve discussions not passing the groupid parameter. We will receive only first group discussions.
1959 $discussions = forum_get_discussions($cm);
1960 self::assertCount(2, $discussions);
1961 foreach ($discussions as $discussion) {
1962 self::assertEquals($group1->id, $discussion->groupid);
1963 }
1964
1965 // Get all my discussions.
1966 $discussions = forum_get_discussions($cm, '', true, -1, -1, false, -1, 0, 0);
1967 self::assertCount(3, $discussions);
1968
1969 // Get all my g1 discussions.
1970 $discussions = forum_get_discussions($cm, '', true, -1, -1, false, -1, 0, $group1->id);
1971 self::assertCount(2, $discussions);
1972 foreach ($discussions as $discussion) {
1973 self::assertEquals($group1->id, $discussion->groupid);
1974 }
1975
1976 // Get all my g2 discussions.
1977 $discussions = forum_get_discussions($cm, '', true, -1, -1, false, -1, 0, $group2->id);
1978 self::assertCount(1, $discussions);
1979 $discussion = array_shift($discussions);
1980 self::assertEquals($group2->id, $discussion->groupid);
1981 self::assertEquals($user1->id, $discussion->userid);
1982 self::assertEquals($discussiong2u1->id, $discussion->discussion);
1983
1984 // Get all my g3 discussions (I'm not enrolled in that group).
1985 $discussions = forum_get_discussions($cm, '', true, -1, -1, false, -1, 0, $group3->id);
1986 self::assertCount(0, $discussions);
1987
1988 // This group does not exist.
1989 $discussions = forum_get_discussions($cm, '', true, -1, -1, false, -1, 0, $group3->id + 1000);
1990 self::assertCount(0, $discussions);
1991
1992 self::setUser($user2);
1993
1994 // Test retrieve discussions not passing the groupid parameter. We will receive only first group discussions.
1995 $discussions = forum_get_discussions($cm);
1996 self::assertCount(2, $discussions);
1997 foreach ($discussions as $discussion) {
1998 self::assertEquals($group1->id, $discussion->groupid);
1999 }
2000
2001 // Get all my viewable discussions.
2002 $discussions = forum_get_discussions($cm, '', true, -1, -1, false, -1, 0, 0);
2003 self::assertCount(2, $discussions);
2004 foreach ($discussions as $discussion) {
2005 self::assertEquals($group1->id, $discussion->groupid);
2006 }
2007
2008 // Get all my g2 discussions (I'm not enrolled in that group).
2009 $discussions = forum_get_discussions($cm, '', true, -1, -1, false, -1, 0, $group2->id);
2010 self::assertCount(0, $discussions);
2011
2012 // Get all my g3 discussions (I'm not enrolled in that group).
2013 $discussions = forum_get_discussions($cm, '', true, -1, -1, false, -1, 0, $group3->id);
2014 self::assertCount(0, $discussions);
2015
2016 }
04cd8ae3
JL
2017
2018 /**
2019 * Test forum_user_can_post_discussion
2020 */
2021 public function test_forum_user_can_post_discussion() {
2022 global $CFG, $DB;
2023
2024 $this->resetAfterTest(true);
2025
2026 // Create course to add the module.
2027 $course = self::getDataGenerator()->create_course(array('groupmode' => SEPARATEGROUPS, 'groupmodeforce' => 1));
2028 $user = self::getDataGenerator()->create_user();
2029 $this->getDataGenerator()->enrol_user($user->id, $course->id);
2030
2031 // Forum forcing separate gropus.
2032 $record = new stdClass();
2033 $record->course = $course->id;
2034 $forum = self::getDataGenerator()->create_module('forum', $record, array('groupmode' => SEPARATEGROUPS));
2035 $cm = get_coursemodule_from_instance('forum', $forum->id);
2036 $context = context_module::instance($cm->id);
2037
2038 self::setUser($user);
2039
2040 // The user is not enroled in any group, try to post in a forum with separate groups.
2041 $can = forum_user_can_post_discussion($forum, null, -1, $cm, $context);
2042 $this->assertFalse($can);
2043
2044 // Create a group.
2045 $group = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
2046
2047 // Try to post in a group the user is not enrolled.
2048 $can = forum_user_can_post_discussion($forum, $group->id, -1, $cm, $context);
2049 $this->assertFalse($can);
2050
2051 // Add the user to a group.
2052 groups_add_member($group->id, $user->id);
2053
2054 // Try to post in a group the user is not enrolled.
2055 $can = forum_user_can_post_discussion($forum, $group->id + 1, -1, $cm, $context);
2056 $this->assertFalse($can);
2057
2058 // Now try to post in the user group. (null means it will guess the group).
2059 $can = forum_user_can_post_discussion($forum, null, -1, $cm, $context);
2060 $this->assertTrue($can);
2061
2062 $can = forum_user_can_post_discussion($forum, $group->id, -1, $cm, $context);
2063 $this->assertTrue($can);
2064
2065 // Test all groups.
2066 $can = forum_user_can_post_discussion($forum, -1, -1, $cm, $context);
2067 $this->assertFalse($can);
2068
2069 $this->setAdminUser();
2070 $can = forum_user_can_post_discussion($forum, -1, -1, $cm, $context);
2071 $this->assertTrue($can);
2072
2073 // Change forum type.
2074 $forum->type = 'news';
2075 $DB->update_record('forum', $forum);
2076
2077 // Admin can post news.
2078 $can = forum_user_can_post_discussion($forum, null, -1, $cm, $context);
2079 $this->assertTrue($can);
2080
2081 // Normal users don't.
2082 self::setUser($user);
2083 $can = forum_user_can_post_discussion($forum, null, -1, $cm, $context);
2084 $this->assertFalse($can);
2085
2086 // Change forum type.
2087 $forum->type = 'eachuser';
2088 $DB->update_record('forum', $forum);
2089
2090 // I didn't post yet, so I should be able to post.
2091 $can = forum_user_can_post_discussion($forum, null, -1, $cm, $context);
2092 $this->assertTrue($can);
2093
2094 // Post now.
2095 $record = new stdClass();
2096 $record->course = $course->id;
2097 $record->userid = $user->id;
2098 $record->forum = $forum->id;
2099 $discussion = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
2100
2101 // I already posted, I shouldn't be able to post.
2102 $can = forum_user_can_post_discussion($forum, null, -1, $cm, $context);
2103 $this->assertFalse($can);
2104
2105 // Last check with no groups, normal forum and course.
2106 $course->groupmode = NOGROUPS;
2107 $course->groupmodeforce = 0;
2108 $DB->update_record('course', $course);
2109
2110 $forum->type = 'general';
2111 $forum->groupmode = NOGROUPS;
2112 $DB->update_record('forum', $forum);
2113
2114 $can = forum_user_can_post_discussion($forum, null, -1, $cm, $context);
2115 $this->assertTrue($can);
2116 }
62b3b997 2117}