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