MDL-50357 core_group: tidy up the function groups_delete_group_members
[moodle.git] / mod / forum / tests / lib_test.php
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/>.
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  */
25 defined('MOODLE_INTERNAL') || die();
27 global $CFG;
28 require_once($CFG->dirroot . '/mod/forum/lib.php');
29 require_once($CFG->dirroot . '/rating/lib.php');
31 class mod_forum_lib_testcase extends advanced_testcase {
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     }
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     }
45     public function test_forum_trigger_content_uploaded_event() {
46         $this->resetAfterTest();
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);
53         $this->setUser($user->id);
54         $fakepost = (object) array('id' => 123, 'message' => 'Yay!', 'discussion' => 100);
55         $cm = get_coursemodule_from_instance('forum', $forum->id);
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);
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();
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);
92         $this->assertEventContextNotUsed($event);
93     }
95     public function test_forum_get_courses_user_posted_in() {
96         $this->resetAfterTest();
98         $user1 = $this->getDataGenerator()->create_user();
99         $user2 = $this->getDataGenerator()->create_user();
100         $user3 = $this->getDataGenerator()->create_user();
102         $course1 = $this->getDataGenerator()->create_course();
103         $course2 = $this->getDataGenerator()->create_course();
104         $course3 = $this->getDataGenerator()->create_course();
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);
111         $record = new stdClass();
112         $record->course = $course2->id;
113         $forum2 = $this->getDataGenerator()->create_module('forum', $record);
115         $record = new stdClass();
116         $record->course = $course3->id;
117         $forum3 = $this->getDataGenerator()->create_module('forum', $record);
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);
124         // Add discussions to course 1 started by user1.
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);
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);
137         // Add discussions to course2 started by user1.
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);
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);
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);
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);
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);
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));
178         }
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     }
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;
195         $this->resetAfterTest();
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);
203         $options = array('course' => $course->id, 'trackingtype' => FORUM_TRACKING_FORCED); // On.
204         $forumforce = $this->getDataGenerator()->create_module('forum', $options);
206         $options = array('course' => $course->id, 'trackingtype' => FORUM_TRACKING_OPTIONAL); // Optional.
207         $forumoptional = $this->getDataGenerator()->create_module('forum', $options);
209         // Allow force.
210         $CFG->forum_allowforcedreadtracking = 1;
212         // User on, forum off, should be off.
213         $result = forum_tp_can_track_forums($forumoff, $useron);
214         $this->assertEquals(false, $result);
216         // User on, forum on, should be on.
217         $result = forum_tp_can_track_forums($forumforce, $useron);
218         $this->assertEquals(true, $result);
220         // User on, forum optional, should be on.
221         $result = forum_tp_can_track_forums($forumoptional, $useron);
222         $this->assertEquals(true, $result);
224         // User off, forum off, should be off.
225         $result = forum_tp_can_track_forums($forumoff, $useroff);
226         $this->assertEquals(false, $result);
228         // User off, forum force, should be on.
229         $result = forum_tp_can_track_forums($forumforce, $useroff);
230         $this->assertEquals(true, $result);
232         // User off, forum optional, should be off.
233         $result = forum_tp_can_track_forums($forumoptional, $useroff);
234         $this->assertEquals(false, $result);
236         // Don't allow force.
237         $CFG->forum_allowforcedreadtracking = 0;
239         // User on, forum off, should be off.
240         $result = forum_tp_can_track_forums($forumoff, $useron);
241         $this->assertEquals(false, $result);
243         // User on, forum on, should be on.
244         $result = forum_tp_can_track_forums($forumforce, $useron);
245         $this->assertEquals(true, $result);
247         // User on, forum optional, should be on.
248         $result = forum_tp_can_track_forums($forumoptional, $useron);
249         $this->assertEquals(true, $result);
251         // User off, forum off, should be off.
252         $result = forum_tp_can_track_forums($forumoff, $useroff);
253         $this->assertEquals(false, $result);
255         // User off, forum force, should be off.
256         $result = forum_tp_can_track_forums($forumforce, $useroff);
257         $this->assertEquals(false, $result);
259         // User off, forum optional, should be off.
260         $result = forum_tp_can_track_forums($forumoptional, $useroff);
261         $this->assertEquals(false, $result);
263     }
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;
271         $this->resetAfterTest();
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);
279         $options = array('course' => $course->id, 'trackingtype' => FORUM_TRACKING_FORCED); // On.
280         $forumforce = $this->getDataGenerator()->create_module('forum', $options);
282         $options = array('course' => $course->id, 'trackingtype' => FORUM_TRACKING_OPTIONAL); // Optional.
283         $forumoptional = $this->getDataGenerator()->create_module('forum', $options);
285         // Allow force.
286         $CFG->forum_allowforcedreadtracking = 1;
288         // User on, forum off, should be off.
289         $result = forum_tp_is_tracked($forumoff, $useron);
290         $this->assertEquals(false, $result);
292         // User on, forum force, should be on.
293         $result = forum_tp_is_tracked($forumforce, $useron);
294         $this->assertEquals(true, $result);
296         // User on, forum optional, should be on.
297         $result = forum_tp_is_tracked($forumoptional, $useron);
298         $this->assertEquals(true, $result);
300         // User off, forum off, should be off.
301         $result = forum_tp_is_tracked($forumoff, $useroff);
302         $this->assertEquals(false, $result);
304         // User off, forum force, should be on.
305         $result = forum_tp_is_tracked($forumforce, $useroff);
306         $this->assertEquals(true, $result);
308         // User off, forum optional, should be off.
309         $result = forum_tp_is_tracked($forumoptional, $useroff);
310         $this->assertEquals(false, $result);
312         // Don't allow force.
313         $CFG->forum_allowforcedreadtracking = 0;
315         // User on, forum off, should be off.
316         $result = forum_tp_is_tracked($forumoff, $useron);
317         $this->assertEquals(false, $result);
319         // User on, forum force, should be on.
320         $result = forum_tp_is_tracked($forumforce, $useron);
321         $this->assertEquals(true, $result);
323         // User on, forum optional, should be on.
324         $result = forum_tp_is_tracked($forumoptional, $useron);
325         $this->assertEquals(true, $result);
327         // User off, forum off, should be off.
328         $result = forum_tp_is_tracked($forumoff, $useroff);
329         $this->assertEquals(false, $result);
331         // User off, forum force, should be off.
332         $result = forum_tp_is_tracked($forumforce, $useroff);
333         $this->assertEquals(false, $result);
335         // User off, forum optional, should be off.
336         $result = forum_tp_is_tracked($forumoptional, $useroff);
337         $this->assertEquals(false, $result);
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);
345         // Allow force.
346         $CFG->forum_allowforcedreadtracking = 1;
348         // User on, preference off, forum force, should be on.
349         $result = forum_tp_is_tracked($forumforce, $useron);
350         $this->assertEquals(true, $result);
352         // User on, preference off, forum optional, should be on.
353         $result = forum_tp_is_tracked($forumoptional, $useron);
354         $this->assertEquals(false, $result);
356         // User off, preference off, forum force, should be on.
357         $result = forum_tp_is_tracked($forumforce, $useroff);
358         $this->assertEquals(true, $result);
360         // User off, preference off, forum optional, should be off.
361         $result = forum_tp_is_tracked($forumoptional, $useroff);
362         $this->assertEquals(false, $result);
364         // Don't allow force.
365         $CFG->forum_allowforcedreadtracking = 0;
367         // User on, preference off, forum force, should be on.
368         $result = forum_tp_is_tracked($forumforce, $useron);
369         $this->assertEquals(false, $result);
371         // User on, preference off, forum optional, should be on.
372         $result = forum_tp_is_tracked($forumoptional, $useron);
373         $this->assertEquals(false, $result);
375         // User off, preference off, forum force, should be off.
376         $result = forum_tp_is_tracked($forumforce, $useroff);
377         $this->assertEquals(false, $result);
379         // User off, preference off, forum optional, should be off.
380         $result = forum_tp_is_tracked($forumoptional, $useroff);
381         $this->assertEquals(false, $result);
382     }
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;
390         $this->resetAfterTest();
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);
398         $options = array('course' => $course->id, 'trackingtype' => FORUM_TRACKING_FORCED); // On.
399         $forumforce = $this->getDataGenerator()->create_module('forum', $options);
401         $options = array('course' => $course->id, 'trackingtype' => FORUM_TRACKING_OPTIONAL); // Optional.
402         $forumoptional = $this->getDataGenerator()->create_module('forum', $options);
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);
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);
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);
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);
433         // Allow force.
434         $CFG->forum_allowforcedreadtracking = 1;
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);
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]));
451         // Don't allow force.
452         $CFG->forum_allowforcedreadtracking = 0;
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);
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]));
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);
474         // Allow force.
475         $CFG->forum_allowforcedreadtracking = 1;
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]));
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]));
491         // Don't allow force.
492         $CFG->forum_allowforcedreadtracking = 0;
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]));
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     }
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;
513         $this->resetAfterTest();
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);
521         $options = array('course' => $course->id, 'trackingtype' => FORUM_TRACKING_FORCED); // On.
522         $forumforce = $this->getDataGenerator()->create_module('forum', $options);
524         $options = array('course' => $course->id, 'trackingtype' => FORUM_TRACKING_OPTIONAL); // Optional.
525         $forumoptional = $this->getDataGenerator()->create_module('forum', $options);
527         // Allow force.
528         $CFG->forum_allowforcedreadtracking = 1;
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]));
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]));
541         // Don't allow force.
542         $CFG->forum_allowforcedreadtracking = 0;
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]));
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]));
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);
562         // Allow force.
563         $CFG->forum_allowforcedreadtracking = 1;
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]));
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]));
577         // Don't allow force.
578         $CFG->forum_allowforcedreadtracking = 0;
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]));
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     }
595     /**
596      * Test subscription using automatic subscription on create.
597      */
598     public function test_forum_auto_subscribe_on_create() {
599         global $CFG;
601         $this->resetAfterTest();
603         $usercount = 5;
604         $course = $this->getDataGenerator()->create_course();
605         $users = array();
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         }
613         $options = array('course' => $course->id, 'forcesubscribe' => FORUM_INITIALSUBSCRIBE); // Automatic Subscription.
614         $forum = $this->getDataGenerator()->create_module('forum', $options);
616         $result = \mod_forum\subscriptions::fetch_subscribed_users($forum);
617         $this->assertEquals($usercount, count($result));
618         foreach ($users as $user) {
619             $this->assertTrue(\mod_forum\subscriptions::is_subscribed($user->id, $forum));
620         }
621     }
623     /**
624      * Test subscription using forced subscription on create.
625      */
626     public function test_forum_forced_subscribe_on_create() {
627         global $CFG;
629         $this->resetAfterTest();
631         $usercount = 5;
632         $course = $this->getDataGenerator()->create_course();
633         $users = array();
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         }
641         $options = array('course' => $course->id, 'forcesubscribe' => FORUM_FORCESUBSCRIBE); // Forced subscription.
642         $forum = $this->getDataGenerator()->create_module('forum', $options);
644         $result = \mod_forum\subscriptions::fetch_subscribed_users($forum);
645         $this->assertEquals($usercount, count($result));
646         foreach ($users as $user) {
647             $this->assertTrue(\mod_forum\subscriptions::is_subscribed($user->id, $forum));
648         }
649     }
651     /**
652      * Test subscription using optional subscription on create.
653      */
654     public function test_forum_optional_subscribe_on_create() {
655         global $CFG;
657         $this->resetAfterTest();
659         $usercount = 5;
660         $course = $this->getDataGenerator()->create_course();
661         $users = array();
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         }
669         $options = array('course' => $course->id, 'forcesubscribe' => FORUM_CHOOSESUBSCRIBE); // Subscription optional.
670         $forum = $this->getDataGenerator()->create_module('forum', $options);
672         $result = \mod_forum\subscriptions::fetch_subscribed_users($forum);
673         // No subscriptions by default.
674         $this->assertEquals(0, count($result));
675         foreach ($users as $user) {
676             $this->assertFalse(\mod_forum\subscriptions::is_subscribed($user->id, $forum));
677         }
678     }
680     /**
681      * Test subscription using disallow subscription on create.
682      */
683     public function test_forum_disallow_subscribe_on_create() {
684         global $CFG;
686         $this->resetAfterTest();
688         $usercount = 5;
689         $course = $this->getDataGenerator()->create_course();
690         $users = array();
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         }
698         $options = array('course' => $course->id, 'forcesubscribe' => FORUM_DISALLOWSUBSCRIBE); // Subscription prevented.
699         $forum = $this->getDataGenerator()->create_module('forum', $options);
701         $result = \mod_forum\subscriptions::fetch_subscribed_users($forum);
702         // No subscriptions by default.
703         $this->assertEquals(0, count($result));
704         foreach ($users as $user) {
705             $this->assertFalse(\mod_forum\subscriptions::is_subscribed($user->id, $forum));
706         }
707     }
709     /**
710      * Test that context fetching returns the appropriate context.
711      */
712     public function test_forum_get_context() {
713         global $DB, $PAGE;
715         $this->resetAfterTest();
717         // Setup test data.
718         $course = $this->getDataGenerator()->create_course();
719         $coursecontext = \context_course::instance($course->id);
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);
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);
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);
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);
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);
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);
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);
772         // Now specify the page context of the course instead..
773         $PAGE = new moodle_page();
774         $PAGE->set_context($coursecontext);
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);
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     }
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();
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();
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);
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);
824         // Getting the neighbours.
825         $neighbours = forum_get_discussion_neighbours($cm, $disc1, $forum);
826         $this->assertEmpty($neighbours['prev']);
827         $this->assertEquals($disc2->id, $neighbours['next']->id);
829         $neighbours = forum_get_discussion_neighbours($cm, $disc2, $forum);
830         $this->assertEquals($disc1->id, $neighbours['prev']->id);
831         $this->assertEquals($disc3->id, $neighbours['next']->id);
833         $neighbours = forum_get_discussion_neighbours($cm, $disc3, $forum);
834         $this->assertEquals($disc2->id, $neighbours['prev']->id);
835         $this->assertEquals($disc4->id, $neighbours['next']->id);
837         $neighbours = forum_get_discussion_neighbours($cm, $disc4, $forum);
838         $this->assertEquals($disc3->id, $neighbours['prev']->id);
839         $this->assertEquals($disc5->id, $neighbours['next']->id);
841         $neighbours = forum_get_discussion_neighbours($cm, $disc5, $forum);
842         $this->assertEquals($disc4->id, $neighbours['prev']->id);
843         $this->assertEmpty($neighbours['next']);
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);
851         $neighbours = forum_get_discussion_neighbours($cm, $disc5, $forum);
852         $this->assertEquals($disc4->id, $neighbours['prev']->id);
853         $this->assertEquals($disc1->id, $neighbours['next']->id);
855         $neighbours = forum_get_discussion_neighbours($cm, $disc2, $forum);
856         $this->assertEmpty($neighbours['prev']);
857         $this->assertEquals($disc3->id, $neighbours['next']->id);
859         $neighbours = forum_get_discussion_neighbours($cm, $disc1, $forum);
860         $this->assertEquals($disc5->id, $neighbours['prev']->id);
861         $this->assertEmpty($neighbours['next']);
863         // After some discussions were created.
864         sleep(1);
865         $disc6 = $forumgen->create_discussion($record);
866         $neighbours = forum_get_discussion_neighbours($cm, $disc6, $forum);
867         $this->assertEquals($disc1->id, $neighbours['prev']->id);
868         $this->assertEmpty($neighbours['next']);
870         sleep(1);
871         $disc7 = $forumgen->create_discussion($record);
872         $neighbours = forum_get_discussion_neighbours($cm, $disc7, $forum);
873         $this->assertEquals($disc6->id, $neighbours['prev']->id);
874         $this->assertEmpty($neighbours['next']);
876         // Adding timed discussions.
877         $CFG->forum_enabletimedposts = true;
878         $now = time();
879         $past = $now - 60;
880         $future = $now + 60;
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);
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);
911         // Admin user ignores the timed settings of discussions.
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.
919         $this->setAdminUser();
920         $neighbours = forum_get_discussion_neighbours($cm, $disc8, $forum);
921         $this->assertEquals($disc7->id, $neighbours['prev']->id);
922         $this->assertEquals($disc10->id, $neighbours['next']->id);
924         $neighbours = forum_get_discussion_neighbours($cm, $disc9, $forum);
925         $this->assertEquals($disc12->id, $neighbours['prev']->id);
926         $this->assertEquals($disc13->id, $neighbours['next']->id);
928         $neighbours = forum_get_discussion_neighbours($cm, $disc10, $forum);
929         $this->assertEquals($disc8->id, $neighbours['prev']->id);
930         $this->assertEquals($disc11->id, $neighbours['next']->id);
932         $neighbours = forum_get_discussion_neighbours($cm, $disc11, $forum);
933         $this->assertEquals($disc10->id, $neighbours['prev']->id);
934         $this->assertEquals($disc12->id, $neighbours['next']->id);
936         $neighbours = forum_get_discussion_neighbours($cm, $disc12, $forum);
937         $this->assertEquals($disc11->id, $neighbours['prev']->id);
938         $this->assertEquals($disc9->id, $neighbours['next']->id);
940         $neighbours = forum_get_discussion_neighbours($cm, $disc13, $forum);
941         $this->assertEquals($disc9->id, $neighbours['prev']->id);
942         $this->assertEmpty($neighbours['next']);
944         // Normal user can see their own timed discussions.
945         $this->setUser($user);
946         $neighbours = forum_get_discussion_neighbours($cm, $disc8, $forum);
947         $this->assertEquals($disc7->id, $neighbours['prev']->id);
948         $this->assertEquals($disc10->id, $neighbours['next']->id);
950         $neighbours = forum_get_discussion_neighbours($cm, $disc9, $forum);
951         $this->assertEquals($disc12->id, $neighbours['prev']->id);
952         $this->assertEquals($disc13->id, $neighbours['next']->id);
954         $neighbours = forum_get_discussion_neighbours($cm, $disc10, $forum);
955         $this->assertEquals($disc8->id, $neighbours['prev']->id);
956         $this->assertEquals($disc11->id, $neighbours['next']->id);
958         $neighbours = forum_get_discussion_neighbours($cm, $disc11, $forum);
959         $this->assertEquals($disc10->id, $neighbours['prev']->id);
960         $this->assertEquals($disc12->id, $neighbours['next']->id);
962         $neighbours = forum_get_discussion_neighbours($cm, $disc12, $forum);
963         $this->assertEquals($disc11->id, $neighbours['prev']->id);
964         $this->assertEquals($disc9->id, $neighbours['next']->id);
966         $neighbours = forum_get_discussion_neighbours($cm, $disc13, $forum);
967         $this->assertEquals($disc9->id, $neighbours['prev']->id);
968         $this->assertEmpty($neighbours['next']);
970         // Normal user does not ignore timed settings.
971         $this->setUser($user2);
972         $neighbours = forum_get_discussion_neighbours($cm, $disc8, $forum);
973         $this->assertEquals($disc7->id, $neighbours['prev']->id);
974         $this->assertEquals($disc10->id, $neighbours['next']->id);
976         $neighbours = forum_get_discussion_neighbours($cm, $disc10, $forum);
977         $this->assertEquals($disc8->id, $neighbours['prev']->id);
978         $this->assertEquals($disc12->id, $neighbours['next']->id);
980         $neighbours = forum_get_discussion_neighbours($cm, $disc12, $forum);
981         $this->assertEquals($disc10->id, $neighbours['prev']->id);
982         $this->assertEmpty($neighbours['next']);
984         // Reset to normal mode.
985         $CFG->forum_enabletimedposts = false;
986         $this->setAdminUser();
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));
996         $neighbours = forum_get_discussion_neighbours($cm, $disc2, $forum);
997         $this->assertEquals($disc13->id, $neighbours['prev']->id);
998         $this->assertEmpty($neighbours['next']);
1000         $neighbours = forum_get_discussion_neighbours($cm, $disc3, $forum);
1001         $this->assertEquals($disc13->id, $neighbours['prev']->id);
1002         $this->assertEmpty($neighbours['next']);
1003     }
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();
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();
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);
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);
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);
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);
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);
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);
1053         $neighbours = forum_get_discussion_neighbours($cm, $disc5, $forum);
1054         $this->assertEquals($disc4->id, $neighbours['prev']->id);
1055         $this->assertEmpty($neighbours['next']);
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);
1062         $neighbours = forum_get_discussion_neighbours($cm, $disc1, $forum);
1063         $this->assertEmpty($neighbours['prev']);
1064         $this->assertEquals($disc2->id, $neighbours['next']->id);
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);
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']);
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']);
1083         // Adding timed discussions.
1084         $CFG->forum_enabletimedposts = true;
1085         $now = time();
1086         $past = $now - 60;
1087         $future = $now + 60;
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);
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);
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);
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);
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);
1132         $neighbours = forum_get_discussion_neighbours($cm, $disc12, $forum);
1133         $this->assertEquals($disc11->id, $neighbours['prev']->id);
1134         $this->assertEmpty($neighbours['next']);
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);
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);
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);
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);
1154         $neighbours = forum_get_discussion_neighbours($cm, $disc12, $forum);
1155         $this->assertEquals($disc11->id, $neighbours['prev']->id);
1156         $this->assertEmpty($neighbours['next']);
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);
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);
1168         $neighbours = forum_get_discussion_neighbours($cm, $disc12, $forum);
1169         $this->assertEquals($disc10->id, $neighbours['prev']->id);
1170         $this->assertEmpty($neighbours['next']);
1172         // Reset to normal mode.
1173         $CFG->forum_enabletimedposts = false;
1174         $this->setAdminUser();
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));
1182         $neighbours = forum_get_discussion_neighbours($cm, $disc2, $forum);
1183         $this->assertEquals($disc12->id, $neighbours['prev']->id);
1184         $this->assertEmpty($neighbours['next']);
1186         $neighbours = forum_get_discussion_neighbours($cm, $disc3, $forum);
1187         $this->assertEquals($disc12->id, $neighbours['prev']->id);
1188         $this->assertEmpty($neighbours['next']);
1189     }
1191     /**
1192      * Test getting the neighbour threads of a discussion.
1193      */
1194     public function test_forum_get_neighbours_with_groups() {
1195         $this->resetAfterTest();
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));
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);
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);
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);
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);
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);
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);
1257         // Admin user can see all groups.
1258         $this->setAdminUser();
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);
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);
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);
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);
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']);
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));
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);
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);
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']);
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));
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);
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));
1338         $neighbours = forum_get_discussion_neighbours($cm2, $disc23, $forum2);
1339         $this->assertEmpty($neighbours['prev']);
1340         $this->assertEmpty($neighbours['next']);
1342         $neighbours = forum_get_discussion_neighbours($cm2, $disc22, $forum2);
1343         $this->assertEmpty($neighbours['prev']);
1344         $this->assertEquals($disc23->id, $neighbours['next']->id);
1346         $neighbours = forum_get_discussion_neighbours($cm2, $disc24, $forum2);
1347         $this->assertEquals($disc23->id, $neighbours['prev']->id);
1348         $this->assertEmpty($neighbours['next']);
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));
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);
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);
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']);
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     }
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();
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));
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);
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;
1414         $record->groupid = $group1->id;
1415         $disc11 = $forumgen->create_discussion($record);
1416         $record->forum = $forum2->id;
1417         $disc21 = $forumgen->create_discussion($record);
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);
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);
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);
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);
1451         // Admin user can see all groups.
1452         $this->setAdminUser();
1453         $neighbours = forum_get_discussion_neighbours($cm1, $disc11, $forum1);
1454         $this->assertEmpty($neighbours['prev']);
1455         $this->assertEquals($disc12->id, $neighbours['next']->id);
1456         $neighbours = forum_get_discussion_neighbours($cm2, $disc21, $forum2);
1457         $this->assertEmpty($neighbours['prev']);
1458         $this->assertEquals($disc22->id, $neighbours['next']->id);
1460         $neighbours = forum_get_discussion_neighbours($cm1, $disc12, $forum1);
1461         $this->assertEquals($disc11->id, $neighbours['prev']->id);
1462         $this->assertEquals($disc13->id, $neighbours['next']->id);
1463         $neighbours = forum_get_discussion_neighbours($cm2, $disc22, $forum2);
1464         $this->assertEquals($disc21->id, $neighbours['prev']->id);
1465         $this->assertEquals($disc23->id, $neighbours['next']->id);
1467         $neighbours = forum_get_discussion_neighbours($cm1, $disc13, $forum1);
1468         $this->assertEquals($disc12->id, $neighbours['prev']->id);
1469         $this->assertEquals($disc14->id, $neighbours['next']->id);
1470         $neighbours = forum_get_discussion_neighbours($cm2, $disc23, $forum2);
1471         $this->assertEquals($disc22->id, $neighbours['prev']->id);
1472         $this->assertEquals($disc24->id, $neighbours['next']->id);
1474         $neighbours = forum_get_discussion_neighbours($cm1, $disc14, $forum1);
1475         $this->assertEquals($disc13->id, $neighbours['prev']->id);
1476         $this->assertEquals($disc15->id, $neighbours['next']->id);
1477         $neighbours = forum_get_discussion_neighbours($cm2, $disc24, $forum2);
1478         $this->assertEquals($disc23->id, $neighbours['prev']->id);
1479         $this->assertEquals($disc25->id, $neighbours['next']->id);
1481         $neighbours = forum_get_discussion_neighbours($cm1, $disc15, $forum1);
1482         $this->assertEquals($disc14->id, $neighbours['prev']->id);
1483         $this->assertEmpty($neighbours['next']);
1484         $neighbours = forum_get_discussion_neighbours($cm2, $disc25, $forum2);
1485         $this->assertEquals($disc24->id, $neighbours['prev']->id);
1486         $this->assertEmpty($neighbours['next']);
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));
1493         $neighbours = forum_get_discussion_neighbours($cm1, $disc11, $forum1);
1494         $this->assertEmpty($neighbours['prev']);
1495         $this->assertEquals($disc13->id, $neighbours['next']->id);
1496         $neighbours = forum_get_discussion_neighbours($cm2, $disc21, $forum2);
1497         $this->assertEmpty($neighbours['prev']);
1498         $this->assertEquals($disc23->id, $neighbours['next']->id);
1500         $neighbours = forum_get_discussion_neighbours($cm1, $disc13, $forum1);
1501         $this->assertEquals($disc11->id, $neighbours['prev']->id);
1502         $this->assertEquals($disc15->id, $neighbours['next']->id);
1503         $neighbours = forum_get_discussion_neighbours($cm2, $disc23, $forum2);
1504         $this->assertEquals($disc21->id, $neighbours['prev']->id);
1505         $this->assertEquals($disc25->id, $neighbours['next']->id);
1507         $neighbours = forum_get_discussion_neighbours($cm1, $disc15, $forum1);
1508         $this->assertEquals($disc13->id, $neighbours['prev']->id);
1509         $this->assertEmpty($neighbours['next']);
1510         $neighbours = forum_get_discussion_neighbours($cm2, $disc25, $forum2);
1511         $this->assertEquals($disc23->id, $neighbours['prev']->id);
1512         $this->assertEmpty($neighbours['next']);
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));
1519         // They can see anything in visible groups.
1520         $neighbours = forum_get_discussion_neighbours($cm1, $disc12, $forum1);
1521         $this->assertEquals($disc11->id, $neighbours['prev']->id);
1522         $this->assertEquals($disc13->id, $neighbours['next']->id);
1523         $neighbours = forum_get_discussion_neighbours($cm1, $disc13, $forum1);
1524         $this->assertEquals($disc12->id, $neighbours['prev']->id);
1525         $this->assertEquals($disc14->id, $neighbours['next']->id);
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));
1532         $neighbours = forum_get_discussion_neighbours($cm2, $disc23, $forum2);
1533         $this->assertEmpty($neighbours['prev']);
1534         $this->assertEmpty($neighbours['next']);
1536         $neighbours = forum_get_discussion_neighbours($cm2, $disc22, $forum2);
1537         $this->assertEmpty($neighbours['prev']);
1538         $this->assertEquals($disc23->id, $neighbours['next']->id);
1540         $neighbours = forum_get_discussion_neighbours($cm2, $disc24, $forum2);
1541         $this->assertEquals($disc23->id, $neighbours['prev']->id);
1542         $this->assertEmpty($neighbours['next']);
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));
1550         // They can see non-grouped or same group.
1551         $neighbours = forum_get_discussion_neighbours($cm1, $disc11, $forum1);
1552         $this->assertEmpty($neighbours['prev']);
1553         $this->assertEquals($disc13->id, $neighbours['next']->id);
1554         $neighbours = forum_get_discussion_neighbours($cm2, $disc21, $forum2);
1555         $this->assertEmpty($neighbours['prev']);
1556         $this->assertEquals($disc23->id, $neighbours['next']->id);
1558         $neighbours = forum_get_discussion_neighbours($cm1, $disc13, $forum1);
1559         $this->assertEquals($disc11->id, $neighbours['prev']->id);
1560         $this->assertEquals($disc15->id, $neighbours['next']->id);
1561         $neighbours = forum_get_discussion_neighbours($cm2, $disc23, $forum2);
1562         $this->assertEquals($disc21->id, $neighbours['prev']->id);
1563         $this->assertEquals($disc25->id, $neighbours['next']->id);
1565         $neighbours = forum_get_discussion_neighbours($cm1, $disc15, $forum1);
1566         $this->assertEquals($disc13->id, $neighbours['prev']->id);
1567         $this->assertEmpty($neighbours['next']);
1568         $neighbours = forum_get_discussion_neighbours($cm2, $disc25, $forum2);
1569         $this->assertEquals($disc23->id, $neighbours['prev']->id);
1570         $this->assertEmpty($neighbours['next']);
1572         // Querying the neighbours of a discussion passing the wrong CM.
1573         $this->setExpectedException('coding_exception');
1574         forum_get_discussion_neighbours($cm2, $disc11, $forum2);
1575     }
1577     public function test_count_discussion_replies_basic() {
1578         list($forum, $discussionids) = $this->create_multiple_discussions_with_replies(10, 5);
1580         // Count the discussion replies in the forum.
1581         $result = forum_count_discussion_replies($forum->id);
1582         $this->assertCount(10, $result);
1583     }
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     }
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     }
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     }
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     }
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     }
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     }
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     }
1659     public function test_forum_view() {
1660         global $CFG;
1662         $CFG->enablecompletion = 1;
1663         $this->resetAfterTest();
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);
1672         // Trigger and capture the event.
1673         $sink = $this->redirectEvents();
1675         $this->setAdminUser();
1676         forum_view($forum, $course, $cm, $context);
1678         $events = $sink->get_events();
1679         // 2 additional events thanks to completion.
1680         $this->assertCount(3, $events);
1681         $event = array_pop($events);
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());
1691         // Check completion status.
1692         $completion = new completion_info($course);
1693         $completiondata = $completion->get_data($cm);
1694         $this->assertEquals(1, $completiondata->completionstate);
1696     }
1698     /**
1699      * Test forum_discussion_view.
1700      */
1701     public function test_forum_discussion_view() {
1702         global $CFG, $USER;
1704         $this->resetAfterTest();
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);
1711         $context = context_module::instance($forum->cmid);
1712         $cm = get_coursemodule_from_instance('forum', $forum->id);
1714         // Trigger and capture the event.
1715         $sink = $this->redirectEvents();
1717         $this->setAdminUser();
1718         forum_discussion_view($context, $forum, $discussion);
1720         $events = $sink->get_events();
1721         $this->assertCount(1, $events);
1722         $event = array_pop($events);
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);
1732         $this->assertNotEmpty($event->get_name());
1734     }
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();
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);
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     }
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;
1773         $generator = self::getDataGenerator()->get_plugin_generator('mod_forum');
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);
1781         // Retrieve the first post.
1782         $replyto = $DB->get_record('forum_posts', array('discussion' => $discussion->id));
1784         // Create the replies.
1785         $post = new stdClass();
1786         $post->userid = $user->id;
1787         $post->discussion = $discussion->id;
1788         $post->parent = $replyto->id;
1790         for ($i = 0; $i < $replycount; $i++) {
1791             $generator->create_post($post);
1792         }
1794         return $discussion;
1795     }
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;
1806         $this->resetAfterTest();
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);
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();
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);
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);
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);
1845         // Retrieve the first post.
1846         $post = $DB->get_record('forum_posts', array('discussion' => $discussion->id));
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);
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));
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));
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));
1898     }
1900     /**
1901      * Test forum_get_discussions
1902      */
1903     public function test_forum_get_discussions_with_groups() {
1904         global $DB;
1906         $this->resetAfterTest(true);
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();
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);
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);
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));
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);
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);
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);
1946         $record['groupid'] = $group2->id;
1947         $discussiong2u1 = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1949         $record['userid'] = $user2->id;
1950         $record['groupid'] = $group1->id;
1951         $discussiong1u2 = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1953         $record['userid'] = $user3->id;
1954         $record['groupid'] = $group3->id;
1955         $discussiong3u3 = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
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         }
1965         // Get all my discussions.
1966         $discussions = forum_get_discussions($cm, '', true, -1, -1, false, -1, 0, 0);
1967         self::assertCount(3, $discussions);
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         }
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);
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);
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);
1992         self::setUser($user2);
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         }
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         }
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);
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);
2016     }
2018     /**
2019      * Test forum_user_can_post_discussion
2020      */
2021     public function test_forum_user_can_post_discussion() {
2022         global $CFG, $DB;
2024         $this->resetAfterTest(true);
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);
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);
2038         self::setUser($user);
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);
2044         // Create a group.
2045         $group = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
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);
2051         // Add the user to a group.
2052         groups_add_member($group->id, $user->id);
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);
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);
2062         $can = forum_user_can_post_discussion($forum, $group->id, -1, $cm, $context);
2063         $this->assertTrue($can);
2065         // Test all groups.
2066         $can = forum_user_can_post_discussion($forum, -1, -1, $cm, $context);
2067         $this->assertFalse($can);
2069         $this->setAdminUser();
2070         $can = forum_user_can_post_discussion($forum, -1, -1, $cm, $context);
2071         $this->assertTrue($can);
2073         // Change forum type.
2074         $forum->type = 'news';
2075         $DB->update_record('forum', $forum);
2077         // Admin can post news.
2078         $can = forum_user_can_post_discussion($forum, null, -1, $cm, $context);
2079         $this->assertTrue($can);
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);
2086         // Change forum type.
2087         $forum->type = 'eachuser';
2088         $DB->update_record('forum', $forum);
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);
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);
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);
2105         // Last check with no groups, normal forum and course.
2106         $course->groupmode = NOGROUPS;
2107         $course->groupmodeforce = 0;
2108         $DB->update_record('course', $course);
2110         $forum->type = 'general';
2111         $forum->groupmode = NOGROUPS;
2112         $DB->update_record('forum', $forum);
2114         $can = forum_user_can_post_discussion($forum, null, -1, $cm, $context);
2115         $this->assertTrue($can);
2116     }