MDL-50173 ratings: Use proper checks to ensure ratings are viewable.
[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);
826         $this->assertEmpty($neighbours['prev']);
827         $this->assertEquals($disc2->id, $neighbours['next']->id);
829         $neighbours = forum_get_discussion_neighbours($cm, $disc2);
830         $this->assertEquals($disc1->id, $neighbours['prev']->id);
831         $this->assertEquals($disc3->id, $neighbours['next']->id);
833         $neighbours = forum_get_discussion_neighbours($cm, $disc3);
834         $this->assertEquals($disc2->id, $neighbours['prev']->id);
835         $this->assertEquals($disc4->id, $neighbours['next']->id);
837         $neighbours = forum_get_discussion_neighbours($cm, $disc4);
838         $this->assertEquals($disc3->id, $neighbours['prev']->id);
839         $this->assertEquals($disc5->id, $neighbours['next']->id);
841         $neighbours = forum_get_discussion_neighbours($cm, $disc5);
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);
852         $this->assertEquals($disc4->id, $neighbours['prev']->id);
853         $this->assertEquals($disc1->id, $neighbours['next']->id);
855         $neighbours = forum_get_discussion_neighbours($cm, $disc2);
856         $this->assertEmpty($neighbours['prev']);
857         $this->assertEquals($disc3->id, $neighbours['next']->id);
859         $neighbours = forum_get_discussion_neighbours($cm, $disc1);
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);
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);
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);
907         // Admin user ignores the timed settings of discussions.
908         $this->setAdminUser();
909         $neighbours = forum_get_discussion_neighbours($cm, $disc8);
910         $this->assertEquals($disc7->id, $neighbours['prev']->id);
911         $this->assertEquals($disc9->id, $neighbours['next']->id);
913         $neighbours = forum_get_discussion_neighbours($cm, $disc9);
914         $this->assertEquals($disc8->id, $neighbours['prev']->id);
915         $this->assertEquals($disc10->id, $neighbours['next']->id);
917         $neighbours = forum_get_discussion_neighbours($cm, $disc10);
918         $this->assertEquals($disc9->id, $neighbours['prev']->id);
919         $this->assertEquals($disc11->id, $neighbours['next']->id);
921         $neighbours = forum_get_discussion_neighbours($cm, $disc11);
922         $this->assertEquals($disc10->id, $neighbours['prev']->id);
923         $this->assertEquals($disc12->id, $neighbours['next']->id);
925         $neighbours = forum_get_discussion_neighbours($cm, $disc12);
926         $this->assertEquals($disc11->id, $neighbours['prev']->id);
927         $this->assertEmpty($neighbours['next']);
929         // Normal user can see their own timed discussions.
930         $this->setUser($user);
931         $neighbours = forum_get_discussion_neighbours($cm, $disc8);
932         $this->assertEquals($disc7->id, $neighbours['prev']->id);
933         $this->assertEquals($disc9->id, $neighbours['next']->id);
935         $neighbours = forum_get_discussion_neighbours($cm, $disc9);
936         $this->assertEquals($disc8->id, $neighbours['prev']->id);
937         $this->assertEquals($disc10->id, $neighbours['next']->id);
939         $neighbours = forum_get_discussion_neighbours($cm, $disc10);
940         $this->assertEquals($disc9->id, $neighbours['prev']->id);
941         $this->assertEquals($disc11->id, $neighbours['next']->id);
943         $neighbours = forum_get_discussion_neighbours($cm, $disc11);
944         $this->assertEquals($disc10->id, $neighbours['prev']->id);
945         $this->assertEquals($disc12->id, $neighbours['next']->id);
947         $neighbours = forum_get_discussion_neighbours($cm, $disc12);
948         $this->assertEquals($disc11->id, $neighbours['prev']->id);
949         $this->assertEmpty($neighbours['next']);
951         // Normal user does not ignore timed settings.
952         $this->setUser($user2);
953         $neighbours = forum_get_discussion_neighbours($cm, $disc8);
954         $this->assertEquals($disc7->id, $neighbours['prev']->id);
955         $this->assertEquals($disc10->id, $neighbours['next']->id);
957         $neighbours = forum_get_discussion_neighbours($cm, $disc10);
958         $this->assertEquals($disc8->id, $neighbours['prev']->id);
959         $this->assertEquals($disc12->id, $neighbours['next']->id);
961         $neighbours = forum_get_discussion_neighbours($cm, $disc12);
962         $this->assertEquals($disc10->id, $neighbours['prev']->id);
963         $this->assertEmpty($neighbours['next']);
965         // Reset to normal mode.
966         $CFG->forum_enabletimedposts = false;
967         $this->setAdminUser();
969         // Two discussions with identical timemodified ignore each other.
970         sleep(1);
971         $now = time();
972         $DB->update_record('forum_discussions', (object) array('id' => $disc3->id, 'timemodified' => $now));
973         $DB->update_record('forum_discussions', (object) array('id' => $disc2->id, 'timemodified' => $now));
974         $disc2 = $DB->get_record('forum_discussions', array('id' => $disc2->id));
975         $disc3 = $DB->get_record('forum_discussions', array('id' => $disc3->id));
977         $neighbours = forum_get_discussion_neighbours($cm, $disc2);
978         $this->assertEquals($disc12->id, $neighbours['prev']->id);
979         $this->assertEmpty($neighbours['next']);
981         $neighbours = forum_get_discussion_neighbours($cm, $disc3);
982         $this->assertEquals($disc12->id, $neighbours['prev']->id);
983         $this->assertEmpty($neighbours['next']);
984     }
986     /**
987      * Test getting the neighbour threads of a discussion.
988      */
989     public function test_forum_get_neighbours_with_groups() {
990         $this->resetAfterTest();
992         // Setup test data.
993         $forumgen = $this->getDataGenerator()->get_plugin_generator('mod_forum');
994         $course = $this->getDataGenerator()->create_course();
995         $group1 = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
996         $group2 = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
997         $user1 = $this->getDataGenerator()->create_user();
998         $user2 = $this->getDataGenerator()->create_user();
999         $this->getDataGenerator()->enrol_user($user1->id, $course->id);
1000         $this->getDataGenerator()->enrol_user($user2->id, $course->id);
1001         $this->getDataGenerator()->create_group_member(array('userid' => $user1->id, 'groupid' => $group1->id));
1003         $forum1 = $this->getDataGenerator()->create_module('forum', array('course' => $course->id, 'groupmode' => VISIBLEGROUPS));
1004         $forum2 = $this->getDataGenerator()->create_module('forum', array('course' => $course->id, 'groupmode' => SEPARATEGROUPS));
1005         $cm1 = get_coursemodule_from_instance('forum', $forum1->id);
1006         $cm2 = get_coursemodule_from_instance('forum', $forum2->id);
1007         $context1 = context_module::instance($cm1->id);
1008         $context2 = context_module::instance($cm2->id);
1010         // Creating discussions in both forums.
1011         $record = new stdClass();
1012         $record->course = $course->id;
1013         $record->userid = $user1->id;
1014         $record->forum = $forum1->id;
1015         $record->groupid = $group1->id;
1016         $disc11 = $forumgen->create_discussion($record);
1017         $record->forum = $forum2->id;
1018         $disc21 = $forumgen->create_discussion($record);
1020         sleep(1);
1021         $record->userid = $user2->id;
1022         $record->forum = $forum1->id;
1023         $record->groupid = $group2->id;
1024         $disc12 = $forumgen->create_discussion($record);
1025         $record->forum = $forum2->id;
1026         $disc22 = $forumgen->create_discussion($record);
1028         sleep(1);
1029         $record->userid = $user1->id;
1030         $record->forum = $forum1->id;
1031         $record->groupid = null;
1032         $disc13 = $forumgen->create_discussion($record);
1033         $record->forum = $forum2->id;
1034         $disc23 = $forumgen->create_discussion($record);
1036         sleep(1);
1037         $record->userid = $user2->id;
1038         $record->forum = $forum1->id;
1039         $record->groupid = $group2->id;
1040         $disc14 = $forumgen->create_discussion($record);
1041         $record->forum = $forum2->id;
1042         $disc24 = $forumgen->create_discussion($record);
1044         sleep(1);
1045         $record->userid = $user1->id;
1046         $record->forum = $forum1->id;
1047         $record->groupid = $group1->id;
1048         $disc15 = $forumgen->create_discussion($record);
1049         $record->forum = $forum2->id;
1050         $disc25 = $forumgen->create_discussion($record);
1052         // Admin user can see all groups.
1053         $this->setAdminUser();
1054         $neighbours = forum_get_discussion_neighbours($cm1, $disc11);
1055         $this->assertEmpty($neighbours['prev']);
1056         $this->assertEquals($disc12->id, $neighbours['next']->id);
1057         $neighbours = forum_get_discussion_neighbours($cm2, $disc21);
1058         $this->assertEmpty($neighbours['prev']);
1059         $this->assertEquals($disc22->id, $neighbours['next']->id);
1061         $neighbours = forum_get_discussion_neighbours($cm1, $disc12);
1062         $this->assertEquals($disc11->id, $neighbours['prev']->id);
1063         $this->assertEquals($disc13->id, $neighbours['next']->id);
1064         $neighbours = forum_get_discussion_neighbours($cm2, $disc22);
1065         $this->assertEquals($disc21->id, $neighbours['prev']->id);
1066         $this->assertEquals($disc23->id, $neighbours['next']->id);
1068         $neighbours = forum_get_discussion_neighbours($cm1, $disc13);
1069         $this->assertEquals($disc12->id, $neighbours['prev']->id);
1070         $this->assertEquals($disc14->id, $neighbours['next']->id);
1071         $neighbours = forum_get_discussion_neighbours($cm2, $disc23);
1072         $this->assertEquals($disc22->id, $neighbours['prev']->id);
1073         $this->assertEquals($disc24->id, $neighbours['next']->id);
1075         $neighbours = forum_get_discussion_neighbours($cm1, $disc14);
1076         $this->assertEquals($disc13->id, $neighbours['prev']->id);
1077         $this->assertEquals($disc15->id, $neighbours['next']->id);
1078         $neighbours = forum_get_discussion_neighbours($cm2, $disc24);
1079         $this->assertEquals($disc23->id, $neighbours['prev']->id);
1080         $this->assertEquals($disc25->id, $neighbours['next']->id);
1082         $neighbours = forum_get_discussion_neighbours($cm1, $disc15);
1083         $this->assertEquals($disc14->id, $neighbours['prev']->id);
1084         $this->assertEmpty($neighbours['next']);
1085         $neighbours = forum_get_discussion_neighbours($cm2, $disc25);
1086         $this->assertEquals($disc24->id, $neighbours['prev']->id);
1087         $this->assertEmpty($neighbours['next']);
1089         // Admin user is only viewing group 1.
1090         $_POST['group'] = $group1->id;
1091         $this->assertEquals($group1->id, groups_get_activity_group($cm1, true));
1092         $this->assertEquals($group1->id, groups_get_activity_group($cm2, true));
1094         $neighbours = forum_get_discussion_neighbours($cm1, $disc11);
1095         $this->assertEmpty($neighbours['prev']);
1096         $this->assertEquals($disc13->id, $neighbours['next']->id);
1097         $neighbours = forum_get_discussion_neighbours($cm2, $disc21);
1098         $this->assertEmpty($neighbours['prev']);
1099         $this->assertEquals($disc23->id, $neighbours['next']->id);
1101         $neighbours = forum_get_discussion_neighbours($cm1, $disc13);
1102         $this->assertEquals($disc11->id, $neighbours['prev']->id);
1103         $this->assertEquals($disc15->id, $neighbours['next']->id);
1104         $neighbours = forum_get_discussion_neighbours($cm2, $disc23);
1105         $this->assertEquals($disc21->id, $neighbours['prev']->id);
1106         $this->assertEquals($disc25->id, $neighbours['next']->id);
1108         $neighbours = forum_get_discussion_neighbours($cm1, $disc15);
1109         $this->assertEquals($disc13->id, $neighbours['prev']->id);
1110         $this->assertEmpty($neighbours['next']);
1111         $neighbours = forum_get_discussion_neighbours($cm2, $disc25);
1112         $this->assertEquals($disc23->id, $neighbours['prev']->id);
1113         $this->assertEmpty($neighbours['next']);
1115         // Normal user viewing non-grouped posts (this is only possible in visible groups).
1116         $this->setUser($user1);
1117         $_POST['group'] = 0;
1118         $this->assertEquals(0, groups_get_activity_group($cm1, true));
1120         // They can see anything in visible groups.
1121         $neighbours = forum_get_discussion_neighbours($cm1, $disc12);
1122         $this->assertEquals($disc11->id, $neighbours['prev']->id);
1123         $this->assertEquals($disc13->id, $neighbours['next']->id);
1124         $neighbours = forum_get_discussion_neighbours($cm1, $disc13);
1125         $this->assertEquals($disc12->id, $neighbours['prev']->id);
1126         $this->assertEquals($disc14->id, $neighbours['next']->id);
1128         // Normal user, orphan of groups, can only see non-grouped posts in separate groups.
1129         $this->setUser($user2);
1130         $_POST['group'] = 0;
1131         $this->assertEquals(0, groups_get_activity_group($cm2, true));
1133         $neighbours = forum_get_discussion_neighbours($cm2, $disc23);
1134         $this->assertEmpty($neighbours['prev']);
1135         $this->assertEmpty($neighbours['next']);
1137         $neighbours = forum_get_discussion_neighbours($cm2, $disc22);
1138         $this->assertEmpty($neighbours['prev']);
1139         $this->assertEquals($disc23->id, $neighbours['next']->id);
1141         $neighbours = forum_get_discussion_neighbours($cm2, $disc24);
1142         $this->assertEquals($disc23->id, $neighbours['prev']->id);
1143         $this->assertEmpty($neighbours['next']);
1145         // Switching to viewing group 1.
1146         $this->setUser($user1);
1147         $_POST['group'] = $group1->id;
1148         $this->assertEquals($group1->id, groups_get_activity_group($cm1, true));
1149         $this->assertEquals($group1->id, groups_get_activity_group($cm2, true));
1151         // They can see non-grouped or same group.
1152         $neighbours = forum_get_discussion_neighbours($cm1, $disc11);
1153         $this->assertEmpty($neighbours['prev']);
1154         $this->assertEquals($disc13->id, $neighbours['next']->id);
1155         $neighbours = forum_get_discussion_neighbours($cm2, $disc21);
1156         $this->assertEmpty($neighbours['prev']);
1157         $this->assertEquals($disc23->id, $neighbours['next']->id);
1159         $neighbours = forum_get_discussion_neighbours($cm1, $disc13);
1160         $this->assertEquals($disc11->id, $neighbours['prev']->id);
1161         $this->assertEquals($disc15->id, $neighbours['next']->id);
1162         $neighbours = forum_get_discussion_neighbours($cm2, $disc23);
1163         $this->assertEquals($disc21->id, $neighbours['prev']->id);
1164         $this->assertEquals($disc25->id, $neighbours['next']->id);
1166         $neighbours = forum_get_discussion_neighbours($cm1, $disc15);
1167         $this->assertEquals($disc13->id, $neighbours['prev']->id);
1168         $this->assertEmpty($neighbours['next']);
1169         $neighbours = forum_get_discussion_neighbours($cm2, $disc25);
1170         $this->assertEquals($disc23->id, $neighbours['prev']->id);
1171         $this->assertEmpty($neighbours['next']);
1173         // Querying the neighbours of a discussion passing the wrong CM.
1174         $this->setExpectedException('coding_exception');
1175         forum_get_discussion_neighbours($cm2, $disc11);
1176     }
1178     public function test_count_discussion_replies_basic() {
1179         list($forum, $discussionids) = $this->create_multiple_discussions_with_replies(10, 5);
1181         // Count the discussion replies in the forum.
1182         $result = forum_count_discussion_replies($forum->id);
1183         $this->assertCount(10, $result);
1184     }
1186     public function test_count_discussion_replies_limited() {
1187         list($forum, $discussionids) = $this->create_multiple_discussions_with_replies(10, 5);
1188         // Adding limits shouldn't make a difference.
1189         $result = forum_count_discussion_replies($forum->id, "", 20);
1190         $this->assertCount(10, $result);
1191     }
1193     public function test_count_discussion_replies_paginated() {
1194         list($forum, $discussionids) = $this->create_multiple_discussions_with_replies(10, 5);
1195         // Adding paging shouldn't make any difference.
1196         $result = forum_count_discussion_replies($forum->id, "", -1, 0, 100);
1197         $this->assertCount(10, $result);
1198     }
1200     public function test_count_discussion_replies_paginated_sorted() {
1201         list($forum, $discussionids) = $this->create_multiple_discussions_with_replies(10, 5);
1202         // Specifying the forumsort should also give a good result. This follows a different path.
1203         $result = forum_count_discussion_replies($forum->id, "d.id asc", -1, 0, 100);
1204         $this->assertCount(10, $result);
1205         foreach ($result as $row) {
1206             // Grab the first discussionid.
1207             $discussionid = array_shift($discussionids);
1208             $this->assertEquals($discussionid, $row->discussion);
1209         }
1210     }
1212     public function test_count_discussion_replies_limited_sorted() {
1213         list($forum, $discussionids) = $this->create_multiple_discussions_with_replies(10, 5);
1214         // Adding limits, and a forumsort shouldn't make a difference.
1215         $result = forum_count_discussion_replies($forum->id, "d.id asc", 20);
1216         $this->assertCount(10, $result);
1217         foreach ($result as $row) {
1218             // Grab the first discussionid.
1219             $discussionid = array_shift($discussionids);
1220             $this->assertEquals($discussionid, $row->discussion);
1221         }
1222     }
1224     public function test_count_discussion_replies_paginated_sorted_small() {
1225         list($forum, $discussionids) = $this->create_multiple_discussions_with_replies(10, 5);
1226         // Grabbing a smaller subset and they should be ordered as expected.
1227         $result = forum_count_discussion_replies($forum->id, "d.id asc", -1, 0, 5);
1228         $this->assertCount(5, $result);
1229         foreach ($result as $row) {
1230             // Grab the first discussionid.
1231             $discussionid = array_shift($discussionids);
1232             $this->assertEquals($discussionid, $row->discussion);
1233         }
1234     }
1236     public function test_count_discussion_replies_paginated_sorted_small_reverse() {
1237         list($forum, $discussionids) = $this->create_multiple_discussions_with_replies(10, 5);
1238         // Grabbing a smaller subset and they should be ordered as expected.
1239         $result = forum_count_discussion_replies($forum->id, "d.id desc", -1, 0, 5);
1240         $this->assertCount(5, $result);
1241         foreach ($result as $row) {
1242             // Grab the last discussionid.
1243             $discussionid = array_pop($discussionids);
1244             $this->assertEquals($discussionid, $row->discussion);
1245         }
1246     }
1248     public function test_count_discussion_replies_limited_sorted_small_reverse() {
1249         list($forum, $discussionids) = $this->create_multiple_discussions_with_replies(10, 5);
1250         // Adding limits, and a forumsort shouldn't make a difference.
1251         $result = forum_count_discussion_replies($forum->id, "d.id desc", 5);
1252         $this->assertCount(5, $result);
1253         foreach ($result as $row) {
1254             // Grab the last discussionid.
1255             $discussionid = array_pop($discussionids);
1256             $this->assertEquals($discussionid, $row->discussion);
1257         }
1258     }
1260     public function test_forum_view() {
1261         global $CFG;
1263         $CFG->enablecompletion = 1;
1264         $this->resetAfterTest();
1266         // Setup test data.
1267         $course = $this->getDataGenerator()->create_course(array('enablecompletion' => 1));
1268         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id),
1269                                                             array('completion' => 2, 'completionview' => 1));
1270         $context = context_module::instance($forum->cmid);
1271         $cm = get_coursemodule_from_instance('forum', $forum->id);
1273         // Trigger and capture the event.
1274         $sink = $this->redirectEvents();
1276         $this->setAdminUser();
1277         forum_view($forum, $course, $cm, $context);
1279         $events = $sink->get_events();
1280         // 2 additional events thanks to completion.
1281         $this->assertCount(3, $events);
1282         $event = array_pop($events);
1284         // Checking that the event contains the expected values.
1285         $this->assertInstanceOf('\mod_forum\event\course_module_viewed', $event);
1286         $this->assertEquals($context, $event->get_context());
1287         $url = new \moodle_url('/mod/forum/view.php', array('f' => $forum->id));
1288         $this->assertEquals($url, $event->get_url());
1289         $this->assertEventContextNotUsed($event);
1290         $this->assertNotEmpty($event->get_name());
1292         // Check completion status.
1293         $completion = new completion_info($course);
1294         $completiondata = $completion->get_data($cm);
1295         $this->assertEquals(1, $completiondata->completionstate);
1297     }
1299     /**
1300      * Test forum_discussion_view.
1301      */
1302     public function test_forum_discussion_view() {
1303         global $CFG, $USER;
1305         $this->resetAfterTest();
1307         // Setup test data.
1308         $course = $this->getDataGenerator()->create_course();
1309         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
1310         $discussion = $this->create_single_discussion_with_replies($forum, $USER, 2);
1312         $context = context_module::instance($forum->cmid);
1313         $cm = get_coursemodule_from_instance('forum', $forum->id);
1315         // Trigger and capture the event.
1316         $sink = $this->redirectEvents();
1318         $this->setAdminUser();
1319         forum_discussion_view($context, $forum, $discussion);
1321         $events = $sink->get_events();
1322         $this->assertCount(1, $events);
1323         $event = array_pop($events);
1325         // Checking that the event contains the expected values.
1326         $this->assertInstanceOf('\mod_forum\event\discussion_viewed', $event);
1327         $this->assertEquals($context, $event->get_context());
1328         $expected = array($course->id, 'forum', 'view discussion', "discuss.php?d={$discussion->id}",
1329             $discussion->id, $forum->cmid);
1330         $this->assertEventLegacyLogData($expected, $event);
1331         $this->assertEventContextNotUsed($event);
1333         $this->assertNotEmpty($event->get_name());
1335     }
1337     /**
1338      * Create a new course, forum, and user with a number of discussions and replies.
1339      *
1340      * @param int $discussioncount The number of discussions to create
1341      * @param int $replycount The number of replies to create in each discussion
1342      * @return array Containing the created forum object, and the ids of the created discussions.
1343      */
1344     protected function create_multiple_discussions_with_replies($discussioncount, $replycount) {
1345         $this->resetAfterTest();
1347         // Setup the content.
1348         $user = $this->getDataGenerator()->create_user();
1349         $course = $this->getDataGenerator()->create_course();
1350         $record = new stdClass();
1351         $record->course = $course->id;
1352         $forum = $this->getDataGenerator()->create_module('forum', $record);
1354         // Create 10 discussions with replies.
1355         $discussionids = array();
1356         for ($i = 0; $i < $discussioncount; $i++) {
1357             $discussion = $this->create_single_discussion_with_replies($forum, $user, $replycount);
1358             $discussionids[] = $discussion->id;
1359         }
1360         return array($forum, $discussionids);
1361     }
1363     /**
1364      * Create a discussion with a number of replies.
1365      *
1366      * @param object $forum The forum which has been created
1367      * @param object $user The user making the discussion and replies
1368      * @param int $replycount The number of replies
1369      * @return object $discussion
1370      */
1371     protected function create_single_discussion_with_replies($forum, $user, $replycount) {
1372         global $DB;
1374         $generator = self::getDataGenerator()->get_plugin_generator('mod_forum');
1376         $record = new stdClass();
1377         $record->course = $forum->course;
1378         $record->forum = $forum->id;
1379         $record->userid = $user->id;
1380         $discussion = $generator->create_discussion($record);
1382         // Retrieve the first post.
1383         $replyto = $DB->get_record('forum_posts', array('discussion' => $discussion->id));
1385         // Create the replies.
1386         $post = new stdClass();
1387         $post->userid = $user->id;
1388         $post->discussion = $discussion->id;
1389         $post->parent = $replyto->id;
1391         for ($i = 0; $i < $replycount; $i++) {
1392             $generator->create_post($post);
1393         }
1395         return $discussion;
1396     }
1398     /**
1399      * Tests for mod_forum_rating_can_see_item_ratings().
1400      *
1401      * @throws coding_exception
1402      * @throws rating_exception
1403      */
1404     public function test_mod_forum_rating_can_see_item_ratings() {
1405         global $DB;
1407         $this->resetAfterTest();
1409         // Setup test data.
1410         $course = new stdClass();
1411         $course->groupmode = SEPARATEGROUPS;
1412         $course->groupmodeforce = true;
1413         $course = $this->getDataGenerator()->create_course($course);
1414         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
1415         $generator = self::getDataGenerator()->get_plugin_generator('mod_forum');
1416         $cm = get_coursemodule_from_instance('forum', $forum->id);
1417         $context = context_module::instance($cm->id);
1419         // Create users.
1420         $user1 = $this->getDataGenerator()->create_user();
1421         $user2 = $this->getDataGenerator()->create_user();
1422         $user3 = $this->getDataGenerator()->create_user();
1423         $user4 = $this->getDataGenerator()->create_user();
1425         // Groups and stuff.
1426         $role = $DB->get_record('role', array('shortname' => 'teacher'), '*', MUST_EXIST);
1427         $this->getDataGenerator()->enrol_user($user1->id, $course->id, $role->id);
1428         $this->getDataGenerator()->enrol_user($user2->id, $course->id, $role->id);
1429         $this->getDataGenerator()->enrol_user($user3->id, $course->id, $role->id);
1430         $this->getDataGenerator()->enrol_user($user4->id, $course->id, $role->id);
1432         $group1 = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
1433         $group2 = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
1434         groups_add_member($group1, $user1);
1435         groups_add_member($group1, $user2);
1436         groups_add_member($group2, $user3);
1437         groups_add_member($group2, $user4);
1439         $record = new stdClass();
1440         $record->course = $forum->course;
1441         $record->forum = $forum->id;
1442         $record->userid = $user1->id;
1443         $record->groupid = $group1->id;
1444         $discussion = $generator->create_discussion($record);
1446         // Retrieve the first post.
1447         $post = $DB->get_record('forum_posts', array('discussion' => $discussion->id));
1449         $ratingoptions = new stdClass;
1450         $ratingoptions->context = $context;
1451         $ratingoptions->ratingarea = 'post';
1452         $ratingoptions->component = 'mod_forum';
1453         $ratingoptions->itemid  = $post->id;
1454         $ratingoptions->scaleid = 2;
1455         $ratingoptions->userid  = $user2->id;
1456         $rating = new rating($ratingoptions);
1457         $rating->update_rating(2);
1459         // Now try to access it as various users.
1460         unassign_capability('moodle/site:accessallgroups', $role->id);
1461         $params = array('contextid' => 2,
1462                         'component' => 'mod_forum',
1463                         'ratingarea' => 'post',
1464                         'itemid' => $post->id,
1465                         'scaleid' => 2);
1466         $this->setUser($user1);
1467         $this->assertTrue(mod_forum_rating_can_see_item_ratings($params));
1468         $this->setUser($user2);
1469         $this->assertTrue(mod_forum_rating_can_see_item_ratings($params));
1470         $this->setUser($user3);
1471         $this->assertFalse(mod_forum_rating_can_see_item_ratings($params));
1472         $this->setUser($user4);
1473         $this->assertFalse(mod_forum_rating_can_see_item_ratings($params));
1475         // Now try with accessallgroups cap and make sure everything is visible.
1476         assign_capability('moodle/site:accessallgroups', CAP_ALLOW, $role->id, $context->id);
1477         $this->setUser($user1);
1478         $this->assertTrue(mod_forum_rating_can_see_item_ratings($params));
1479         $this->setUser($user2);
1480         $this->assertTrue(mod_forum_rating_can_see_item_ratings($params));
1481         $this->setUser($user3);
1482         $this->assertTrue(mod_forum_rating_can_see_item_ratings($params));
1483         $this->setUser($user4);
1484         $this->assertTrue(mod_forum_rating_can_see_item_ratings($params));
1486         // Change group mode and verify visibility.
1487         $course->groupmode = VISIBLEGROUPS;
1488         $DB->update_record('course', $course);
1489         unassign_capability('moodle/site:accessallgroups', $role->id);
1490         $this->setUser($user1);
1491         $this->assertTrue(mod_forum_rating_can_see_item_ratings($params));
1492         $this->setUser($user2);
1493         $this->assertTrue(mod_forum_rating_can_see_item_ratings($params));
1494         $this->setUser($user3);
1495         $this->assertTrue(mod_forum_rating_can_see_item_ratings($params));
1496         $this->setUser($user4);
1497         $this->assertTrue(mod_forum_rating_can_see_item_ratings($params));
1499     }