Merge branch 'MDL-53166' of https://github.com/eugeneventer/moodle-fixes
[moodle.git] / mod / forum / tests / events_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  * Tests for forum events.
19  *
20  * @package    mod_forum
21  * @category   test
22  * @copyright  2014 Dan Poltawski <dan@moodle.com>
23  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24  */
26 defined('MOODLE_INTERNAL') || die();
28 /**
29  * Tests for forum events.
30  *
31  * @package    mod_forum
32  * @category   test
33  * @copyright  2014 Dan Poltawski <dan@moodle.com>
34  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
35  */
36 class mod_forum_events_testcase extends advanced_testcase {
38     /**
39      * Tests set up.
40      */
41     public function setUp() {
42         // We must clear the subscription caches. This has to be done both before each test, and after in case of other
43         // tests using these functions.
44         \mod_forum\subscriptions::reset_forum_cache();
46         $this->resetAfterTest();
47     }
49     public function tearDown() {
50         // We must clear the subscription caches. This has to be done both before each test, and after in case of other
51         // tests using these functions.
52         \mod_forum\subscriptions::reset_forum_cache();
53     }
55     /**
56      * Ensure course_searched event validates that searchterm is set.
57      */
58     public function test_course_searched_searchterm_validation() {
59         $course = $this->getDataGenerator()->create_course();
60         $coursectx = context_course::instance($course->id);
61         $params = array(
62             'context' => $coursectx,
63         );
65         $this->setExpectedException('coding_exception', 'The \'searchterm\' value must be set in other.');
66         \mod_forum\event\course_searched::create($params);
67     }
69     /**
70      * Ensure course_searched event validates that context is the correct level.
71      */
72     public function test_course_searched_context_validation() {
73         $course = $this->getDataGenerator()->create_course();
74         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
75         $context = context_module::instance($forum->cmid);
76         $params = array(
77             'context' => $context,
78             'other' => array('searchterm' => 'testing'),
79         );
81         $this->setExpectedException('coding_exception', 'Context level must be CONTEXT_COURSE.');
82         \mod_forum\event\course_searched::create($params);
83     }
85     /**
86      * Test course_searched event.
87      */
88     public function test_course_searched() {
90         // Setup test data.
91         $course = $this->getDataGenerator()->create_course();
92         $coursectx = context_course::instance($course->id);
93         $searchterm = 'testing123';
95         $params = array(
96             'context' => $coursectx,
97             'other' => array('searchterm' => $searchterm),
98         );
100         // Create event.
101         $event = \mod_forum\event\course_searched::create($params);
103         // Trigger and capture the event.
104         $sink = $this->redirectEvents();
105         $event->trigger();
106         $events = $sink->get_events();
107         $this->assertCount(1, $events);
108         $event = reset($events);
110          // Checking that the event contains the expected values.
111         $this->assertInstanceOf('\mod_forum\event\course_searched', $event);
112         $this->assertEquals($coursectx, $event->get_context());
113         $expected = array($course->id, 'forum', 'search', "search.php?id={$course->id}&amp;search={$searchterm}", $searchterm);
114         $this->assertEventLegacyLogData($expected, $event);
115         $this->assertEventContextNotUsed($event);
117         $this->assertNotEmpty($event->get_name());
118     }
120     /**
121      * Ensure discussion_created event validates that forumid is set.
122      */
123     public function test_discussion_created_forumid_validation() {
124         $course = $this->getDataGenerator()->create_course();
125         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
126         $context = context_module::instance($forum->cmid);
128         $params = array(
129             'context' => $context,
130         );
132         $this->setExpectedException('coding_exception', 'The \'forumid\' value must be set in other.');
133         \mod_forum\event\discussion_created::create($params);
134     }
136     /**
137      * Ensure discussion_created event validates that the context is the correct level.
138      */
139     public function test_discussion_created_context_validation() {
140         $course = $this->getDataGenerator()->create_course();
141         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
143         $params = array(
144             'context' => context_system::instance(),
145             'other' => array('forumid' => $forum->id),
146         );
148         $this->setExpectedException('coding_exception', 'Context level must be CONTEXT_MODULE.');
149         \mod_forum\event\discussion_created::create($params);
150     }
152     /**
153      * Test discussion_created event.
154      */
155     public function test_discussion_created() {
157         // Setup test data.
158         $course = $this->getDataGenerator()->create_course();
159         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
160         $user = $this->getDataGenerator()->create_user();
162         // Add a discussion.
163         $record = array();
164         $record['course'] = $course->id;
165         $record['forum'] = $forum->id;
166         $record['userid'] = $user->id;
167         $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
169         $context = context_module::instance($forum->cmid);
171         $params = array(
172             'context' => $context,
173             'objectid' => $discussion->id,
174             'other' => array('forumid' => $forum->id),
175         );
177         // Create the event.
178         $event = \mod_forum\event\discussion_created::create($params);
180         // Trigger and capturing the event.
181         $sink = $this->redirectEvents();
182         $event->trigger();
183         $events = $sink->get_events();
184         $this->assertCount(1, $events);
185         $event = reset($events);
187         // Check that the event contains the expected values.
188         $this->assertInstanceOf('\mod_forum\event\discussion_created', $event);
189         $this->assertEquals($context, $event->get_context());
190         $expected = array($course->id, 'forum', 'add discussion', "discuss.php?d={$discussion->id}", $discussion->id, $forum->cmid);
191         $this->assertEventLegacyLogData($expected, $event);
192         $this->assertEventContextNotUsed($event);
194         $this->assertNotEmpty($event->get_name());
195     }
197     /**
198      * Ensure discussion_updated event validates that forumid is set.
199      */
200     public function test_discussion_updated_forumid_validation() {
201         $course = $this->getDataGenerator()->create_course();
202         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
203         $context = context_module::instance($forum->cmid);
205         $params = array(
206             'context' => $context,
207         );
209         $this->setExpectedException('coding_exception', 'The \'forumid\' value must be set in other.');
210         \mod_forum\event\discussion_updated::create($params);
211     }
213     /**
214      * Ensure discussion_created event validates that the context is the correct level.
215      */
216     public function test_discussion_updated_context_validation() {
217         $course = $this->getDataGenerator()->create_course();
218         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
220         $params = array(
221             'context' => context_system::instance(),
222             'other' => array('forumid' => $forum->id),
223         );
225         $this->setExpectedException('coding_exception', 'Context level must be CONTEXT_MODULE.');
226         \mod_forum\event\discussion_updated::create($params);
227     }
229     /**
230      * Test discussion_created event.
231      */
232     public function test_discussion_updated() {
234         // Setup test data.
235         $course = $this->getDataGenerator()->create_course();
236         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
237         $user = $this->getDataGenerator()->create_user();
239         // Add a discussion.
240         $record = array();
241         $record['course'] = $course->id;
242         $record['forum'] = $forum->id;
243         $record['userid'] = $user->id;
244         $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
246         $context = context_module::instance($forum->cmid);
248         $params = array(
249             'context' => $context,
250             'objectid' => $discussion->id,
251             'other' => array('forumid' => $forum->id),
252         );
254         // Create the event.
255         $event = \mod_forum\event\discussion_updated::create($params);
257         // Trigger and capturing the event.
258         $sink = $this->redirectEvents();
259         $event->trigger();
260         $events = $sink->get_events();
261         $this->assertCount(1, $events);
262         $event = reset($events);
264         // Check that the event contains the expected values.
265         $this->assertInstanceOf('\mod_forum\event\discussion_updated', $event);
266         $this->assertEquals($context, $event->get_context());
267         $this->assertEventContextNotUsed($event);
269         $this->assertNotEmpty($event->get_name());
270     }
272     /**
273      * Ensure discussion_deleted event validates that forumid is set.
274      */
275     public function test_discussion_deleted_forumid_validation() {
276         $course = $this->getDataGenerator()->create_course();
277         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
278         $context = context_module::instance($forum->cmid);
280         $params = array(
281             'context' => $context,
282         );
284         $this->setExpectedException('coding_exception', 'The \'forumid\' value must be set in other.');
285         \mod_forum\event\discussion_deleted::create($params);
286     }
288     /**
289      * Ensure discussion_deleted event validates that context is of the correct level.
290      */
291     public function test_discussion_deleted_context_validation() {
292         $course = $this->getDataGenerator()->create_course();
293         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
295         $params = array(
296             'context' => context_system::instance(),
297             'other' => array('forumid' => $forum->id),
298         );
300         $this->setExpectedException('coding_exception', 'Context level must be CONTEXT_MODULE.');
301         \mod_forum\event\discussion_deleted::create($params);
302     }
304     /**
305      * Test discussion_deleted event.
306      */
307     public function test_discussion_deleted() {
309         // Setup test data.
310         $course = $this->getDataGenerator()->create_course();
311         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
312         $user = $this->getDataGenerator()->create_user();
314         // Add a discussion.
315         $record = array();
316         $record['course'] = $course->id;
317         $record['forum'] = $forum->id;
318         $record['userid'] = $user->id;
319         $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
321         $context = context_module::instance($forum->cmid);
323         $params = array(
324             'context' => $context,
325             'objectid' => $discussion->id,
326             'other' => array('forumid' => $forum->id),
327         );
329         $event = \mod_forum\event\discussion_deleted::create($params);
331         // Trigger and capture the event.
332         $sink = $this->redirectEvents();
333         $event->trigger();
334         $events = $sink->get_events();
335         $this->assertCount(1, $events);
336         $event = reset($events);
338         // Checking that the event contains the expected values.
339         $this->assertInstanceOf('\mod_forum\event\discussion_deleted', $event);
340         $this->assertEquals($context, $event->get_context());
341         $expected = array($course->id, 'forum', 'delete discussion', "view.php?id={$forum->cmid}", $forum->id, $forum->cmid);
342         $this->assertEventLegacyLogData($expected, $event);
343         $this->assertEventContextNotUsed($event);
345         $this->assertNotEmpty($event->get_name());
346     }
348     /**
349      * Ensure discussion_moved event validates that fromforumid is set.
350      */
351     public function test_discussion_moved_fromforumid_validation() {
352         $course = $this->getDataGenerator()->create_course();
353         $toforum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
355         $context = context_module::instance($toforum->cmid);
357         $params = array(
358             'context' => $context,
359             'other' => array('toforumid' => $toforum->id)
360         );
362         $this->setExpectedException('coding_exception', 'The \'fromforumid\' value must be set in other.');
363         \mod_forum\event\discussion_moved::create($params);
364     }
366     /**
367      * Ensure discussion_moved event validates that toforumid is set.
368      */
369     public function test_discussion_moved_toforumid_validation() {
370         $course = $this->getDataGenerator()->create_course();
371         $fromforum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
372         $toforum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
373         $context = context_module::instance($toforum->cmid);
375         $params = array(
376             'context' => $context,
377             'other' => array('fromforumid' => $fromforum->id)
378         );
380         $this->setExpectedException('coding_exception', 'The \'toforumid\' value must be set in other.');
381         \mod_forum\event\discussion_moved::create($params);
382     }
384     /**
385      * Ensure discussion_moved event validates that the context level is correct.
386      */
387     public function test_discussion_moved_context_validation() {
388         $course = $this->getDataGenerator()->create_course();
389         $fromforum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
390         $toforum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
391         $user = $this->getDataGenerator()->create_user();
393         // Add a discussion.
394         $record = array();
395         $record['course'] = $course->id;
396         $record['forum'] = $fromforum->id;
397         $record['userid'] = $user->id;
398         $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
400         $params = array(
401             'context' => context_system::instance(),
402             'objectid' => $discussion->id,
403             'other' => array('fromforumid' => $fromforum->id, 'toforumid' => $toforum->id)
404         );
406         $this->setExpectedException('coding_exception', 'Context level must be CONTEXT_MODULE.');
407         \mod_forum\event\discussion_moved::create($params);
408     }
410     /**
411      * Test discussion_moved event.
412      */
413     public function test_discussion_moved() {
414         // Setup test data.
415         $course = $this->getDataGenerator()->create_course();
416         $fromforum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
417         $toforum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
418         $user = $this->getDataGenerator()->create_user();
420         // Add a discussion.
421         $record = array();
422         $record['course'] = $course->id;
423         $record['forum'] = $fromforum->id;
424         $record['userid'] = $user->id;
425         $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
427         $context = context_module::instance($toforum->cmid);
429         $params = array(
430             'context' => $context,
431             'objectid' => $discussion->id,
432             'other' => array('fromforumid' => $fromforum->id, 'toforumid' => $toforum->id)
433         );
435         $event = \mod_forum\event\discussion_moved::create($params);
437         // Trigger and capture the event.
438         $sink = $this->redirectEvents();
439         $event->trigger();
440         $events = $sink->get_events();
441         $this->assertCount(1, $events);
442         $event = reset($events);
444         // Checking that the event contains the expected values.
445         $this->assertInstanceOf('\mod_forum\event\discussion_moved', $event);
446         $this->assertEquals($context, $event->get_context());
447         $expected = array($course->id, 'forum', 'move discussion', "discuss.php?d={$discussion->id}",
448             $discussion->id, $toforum->cmid);
449         $this->assertEventLegacyLogData($expected, $event);
450         $this->assertEventContextNotUsed($event);
452         $this->assertNotEmpty($event->get_name());
453     }
456     /**
457      * Ensure discussion_viewed event validates that the contextlevel is correct.
458      */
459     public function test_discussion_viewed_context_validation() {
460         $course = $this->getDataGenerator()->create_course();
461         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
462         $user = $this->getDataGenerator()->create_user();
464         // Add a discussion.
465         $record = array();
466         $record['course'] = $course->id;
467         $record['forum'] = $forum->id;
468         $record['userid'] = $user->id;
469         $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
471         $params = array(
472             'context' => context_system::instance(),
473             'objectid' => $discussion->id,
474         );
476         $this->setExpectedException('coding_exception', 'Context level must be CONTEXT_MODULE.');
477         \mod_forum\event\discussion_viewed::create($params);
478     }
480     /**
481      * Test discussion_viewed event.
482      */
483     public function test_discussion_viewed() {
484         // Setup test data.
485         $course = $this->getDataGenerator()->create_course();
486         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
487         $user = $this->getDataGenerator()->create_user();
489         // Add a discussion.
490         $record = array();
491         $record['course'] = $course->id;
492         $record['forum'] = $forum->id;
493         $record['userid'] = $user->id;
494         $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
496         $context = context_module::instance($forum->cmid);
498         $params = array(
499             'context' => $context,
500             'objectid' => $discussion->id,
501         );
503         $event = \mod_forum\event\discussion_viewed::create($params);
505         // Trigger and capture the event.
506         $sink = $this->redirectEvents();
507         $event->trigger();
508         $events = $sink->get_events();
509         $this->assertCount(1, $events);
510         $event = reset($events);
512         // Checking that the event contains the expected values.
513         $this->assertInstanceOf('\mod_forum\event\discussion_viewed', $event);
514         $this->assertEquals($context, $event->get_context());
515         $expected = array($course->id, 'forum', 'view discussion', "discuss.php?d={$discussion->id}",
516             $discussion->id, $forum->cmid);
517         $this->assertEventLegacyLogData($expected, $event);
518         $this->assertEventContextNotUsed($event);
520         $this->assertNotEmpty($event->get_name());
521     }
523     /**
524      * Ensure course_module_viewed event validates that the contextlevel is correct.
525      */
526     public function test_course_module_viewed_context_validation() {
527         $course = $this->getDataGenerator()->create_course();
528         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
530         $params = array(
531             'context' => context_system::instance(),
532             'objectid' => $forum->id,
533         );
535         $this->setExpectedException('coding_exception', 'Context level must be CONTEXT_MODULE.');
536         \mod_forum\event\course_module_viewed::create($params);
537     }
539     /**
540      * Test the course_module_viewed event.
541      */
542     public function test_course_module_viewed() {
543         // Setup test data.
544         $course = $this->getDataGenerator()->create_course();
545         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
547         $context = context_module::instance($forum->cmid);
549         $params = array(
550             'context' => $context,
551             'objectid' => $forum->id,
552         );
554         $event = \mod_forum\event\course_module_viewed::create($params);
556         // Trigger and capture the event.
557         $sink = $this->redirectEvents();
558         $event->trigger();
559         $events = $sink->get_events();
560         $this->assertCount(1, $events);
561         $event = reset($events);
563         // Checking that the event contains the expected values.
564         $this->assertInstanceOf('\mod_forum\event\course_module_viewed', $event);
565         $this->assertEquals($context, $event->get_context());
566         $expected = array($course->id, 'forum', 'view forum', "view.php?f={$forum->id}", $forum->id, $forum->cmid);
567         $this->assertEventLegacyLogData($expected, $event);
568         $url = new \moodle_url('/mod/forum/view.php', array('f' => $forum->id));
569         $this->assertEquals($url, $event->get_url());
570         $this->assertEventContextNotUsed($event);
572         $this->assertNotEmpty($event->get_name());
573     }
575     /**
576      * Ensure subscription_created event validates that the forumid is set.
577      */
578     public function test_subscription_created_forumid_validation() {
579         $user = $this->getDataGenerator()->create_user();
580         $course = $this->getDataGenerator()->create_course();
581         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
583         $params = array(
584             'context' => context_module::instance($forum->cmid),
585             'relateduserid' => $user->id,
586         );
588         $this->setExpectedException('coding_exception', 'The \'forumid\' value must be set in other.');
589         \mod_forum\event\subscription_created::create($params);
590     }
592     /**
593      * Ensure subscription_created event validates that the relateduserid is set.
594      */
595     public function test_subscription_created_relateduserid_validation() {
596         $course = $this->getDataGenerator()->create_course();
597         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
599         $params = array(
600             'context' => context_module::instance($forum->cmid),
601             'objectid' => $forum->id,
602         );
604         $this->setExpectedException('coding_exception', 'The \'relateduserid\' must be set.');
605         \mod_forum\event\subscription_created::create($params);
606     }
608     /**
609      * Ensure subscription_created event validates that the contextlevel is correct.
610      */
611     public function test_subscription_created_contextlevel_validation() {
612         $user = $this->getDataGenerator()->create_user();
613         $course = $this->getDataGenerator()->create_course();
614         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
616         $params = array(
617             'context' => context_system::instance(),
618             'other' => array('forumid' => $forum->id),
619             'relateduserid' => $user->id,
620         );
622         $this->setExpectedException('coding_exception', 'Context level must be CONTEXT_MODULE.');
623         \mod_forum\event\subscription_created::create($params);
624     }
626     /**
627      * Test the subscription_created event.
628      */
629     public function test_subscription_created() {
630         // Setup test data.
631         $user = $this->getDataGenerator()->create_user();
632         $course = $this->getDataGenerator()->create_course();
633         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
634         $user = $this->getDataGenerator()->create_user();
635         $context = context_module::instance($forum->cmid);
637         // Add a subscription.
638         $record = array();
639         $record['course'] = $course->id;
640         $record['forum'] = $forum->id;
641         $record['userid'] = $user->id;
642         $subscription = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_subscription($record);
644         $params = array(
645             'context' => $context,
646             'objectid' => $subscription->id,
647             'other' => array('forumid' => $forum->id),
648             'relateduserid' => $user->id,
649         );
651         $event = \mod_forum\event\subscription_created::create($params);
653         // Trigger and capturing the event.
654         $sink = $this->redirectEvents();
655         $event->trigger();
656         $events = $sink->get_events();
657         $this->assertCount(1, $events);
658         $event = reset($events);
660         // Checking that the event contains the expected values.
661         $this->assertInstanceOf('\mod_forum\event\subscription_created', $event);
662         $this->assertEquals($context, $event->get_context());
663         $expected = array($course->id, 'forum', 'subscribe', "view.php?f={$forum->id}", $forum->id, $forum->cmid);
664         $this->assertEventLegacyLogData($expected, $event);
665         $url = new \moodle_url('/mod/forum/subscribers.php', array('id' => $forum->id));
666         $this->assertEquals($url, $event->get_url());
667         $this->assertEventContextNotUsed($event);
669         $this->assertNotEmpty($event->get_name());
670     }
672     /**
673      * Ensure subscription_deleted event validates that the forumid is set.
674      */
675     public function test_subscription_deleted_forumid_validation() {
676         $user = $this->getDataGenerator()->create_user();
677         $course = $this->getDataGenerator()->create_course();
678         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
680         $params = array(
681             'context' => context_module::instance($forum->cmid),
682             'relateduserid' => $user->id,
683         );
685         $this->setExpectedException('coding_exception', 'The \'forumid\' value must be set in other.');
686         \mod_forum\event\subscription_deleted::create($params);
687     }
689     /**
690      * Ensure subscription_deleted event validates that the relateduserid is set.
691      */
692     public function test_subscription_deleted_relateduserid_validation() {
693         $course = $this->getDataGenerator()->create_course();
694         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
696         $params = array(
697             'context' => context_module::instance($forum->cmid),
698             'objectid' => $forum->id,
699         );
701         $this->setExpectedException('coding_exception', 'The \'relateduserid\' must be set.');
702         \mod_forum\event\subscription_deleted::create($params);
703     }
705     /**
706      * Ensure subscription_deleted event validates that the contextlevel is correct.
707      */
708     public function test_subscription_deleted_contextlevel_validation() {
709         $user = $this->getDataGenerator()->create_user();
710         $course = $this->getDataGenerator()->create_course();
711         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
713         $params = array(
714             'context' => context_system::instance(),
715             'other' => array('forumid' => $forum->id),
716             'relateduserid' => $user->id,
717         );
719         $this->setExpectedException('coding_exception', 'Context level must be CONTEXT_MODULE.');
720         \mod_forum\event\subscription_deleted::create($params);
721     }
723     /**
724      * Test the subscription_deleted event.
725      */
726     public function test_subscription_deleted() {
727         // Setup test data.
728         $user = $this->getDataGenerator()->create_user();
729         $course = $this->getDataGenerator()->create_course();
730         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
731         $user = $this->getDataGenerator()->create_user();
732         $context = context_module::instance($forum->cmid);
734         // Add a subscription.
735         $record = array();
736         $record['course'] = $course->id;
737         $record['forum'] = $forum->id;
738         $record['userid'] = $user->id;
739         $subscription = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_subscription($record);
741         $params = array(
742             'context' => $context,
743             'objectid' => $subscription->id,
744             'other' => array('forumid' => $forum->id),
745             'relateduserid' => $user->id,
746         );
748         $event = \mod_forum\event\subscription_deleted::create($params);
750         // Trigger and capturing the event.
751         $sink = $this->redirectEvents();
752         $event->trigger();
753         $events = $sink->get_events();
754         $this->assertCount(1, $events);
755         $event = reset($events);
757         // Checking that the event contains the expected values.
758         $this->assertInstanceOf('\mod_forum\event\subscription_deleted', $event);
759         $this->assertEquals($context, $event->get_context());
760         $expected = array($course->id, 'forum', 'unsubscribe', "view.php?f={$forum->id}", $forum->id, $forum->cmid);
761         $this->assertEventLegacyLogData($expected, $event);
762         $url = new \moodle_url('/mod/forum/subscribers.php', array('id' => $forum->id));
763         $this->assertEquals($url, $event->get_url());
764         $this->assertEventContextNotUsed($event);
766         $this->assertNotEmpty($event->get_name());
767     }
769     /**
770      * Ensure readtracking_enabled event validates that the forumid is set.
771      */
772     public function test_readtracking_enabled_forumid_validation() {
773         $user = $this->getDataGenerator()->create_user();
774         $course = $this->getDataGenerator()->create_course();
775         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
777         $params = array(
778             'context' => context_module::instance($forum->cmid),
779             'relateduserid' => $user->id,
780         );
782         $this->setExpectedException('coding_exception', 'The \'forumid\' value must be set in other.');
783         \mod_forum\event\readtracking_enabled::create($params);
784     }
786     /**
787      * Ensure readtracking_enabled event validates that the relateduserid is set.
788      */
789     public function test_readtracking_enabled_relateduserid_validation() {
790         $course = $this->getDataGenerator()->create_course();
791         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
793         $params = array(
794             'context' => context_module::instance($forum->cmid),
795             'objectid' => $forum->id,
796         );
798         $this->setExpectedException('coding_exception', 'The \'relateduserid\' must be set.');
799         \mod_forum\event\readtracking_enabled::create($params);
800     }
802     /**
803      * Ensure readtracking_enabled event validates that the contextlevel is correct.
804      */
805     public function test_readtracking_enabled_contextlevel_validation() {
806         $user = $this->getDataGenerator()->create_user();
807         $course = $this->getDataGenerator()->create_course();
808         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
810         $params = array(
811             'context' => context_system::instance(),
812             'other' => array('forumid' => $forum->id),
813             'relateduserid' => $user->id,
814         );
816         $this->setExpectedException('coding_exception', 'Context level must be CONTEXT_MODULE.');
817         \mod_forum\event\readtracking_enabled::create($params);
818     }
820     /**
821      * Test the readtracking_enabled event.
822      */
823     public function test_readtracking_enabled() {
824         // Setup test data.
825         $user = $this->getDataGenerator()->create_user();
826         $course = $this->getDataGenerator()->create_course();
827         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
828         $context = context_module::instance($forum->cmid);
830         $params = array(
831             'context' => $context,
832             'other' => array('forumid' => $forum->id),
833             'relateduserid' => $user->id,
834         );
836         $event = \mod_forum\event\readtracking_enabled::create($params);
838         // Trigger and capture the event.
839         $sink = $this->redirectEvents();
840         $event->trigger();
841         $events = $sink->get_events();
842         $this->assertCount(1, $events);
843         $event = reset($events);
845         // Checking that the event contains the expected values.
846         $this->assertInstanceOf('\mod_forum\event\readtracking_enabled', $event);
847         $this->assertEquals($context, $event->get_context());
848         $expected = array($course->id, 'forum', 'start tracking', "view.php?f={$forum->id}", $forum->id, $forum->cmid);
849         $this->assertEventLegacyLogData($expected, $event);
850         $url = new \moodle_url('/mod/forum/view.php', array('f' => $forum->id));
851         $this->assertEquals($url, $event->get_url());
852         $this->assertEventContextNotUsed($event);
854         $this->assertNotEmpty($event->get_name());
855     }
857     /**
858      *  Ensure readtracking_disabled event validates that the forumid is set.
859      */
860     public function test_readtracking_disabled_forumid_validation() {
861         $user = $this->getDataGenerator()->create_user();
862         $course = $this->getDataGenerator()->create_course();
863         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
865         $params = array(
866             'context' => context_module::instance($forum->cmid),
867             'relateduserid' => $user->id,
868         );
870         $this->setExpectedException('coding_exception', 'The \'forumid\' value must be set in other.');
871         \mod_forum\event\readtracking_disabled::create($params);
872     }
874     /**
875      *  Ensure readtracking_disabled event validates that the relateduserid is set.
876      */
877     public function test_readtracking_disabled_relateduserid_validation() {
878         $course = $this->getDataGenerator()->create_course();
879         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
881         $params = array(
882             'context' => context_module::instance($forum->cmid),
883             'objectid' => $forum->id,
884         );
886         $this->setExpectedException('coding_exception', 'The \'relateduserid\' must be set.');
887         \mod_forum\event\readtracking_disabled::create($params);
888     }
890     /**
891      *  Ensure readtracking_disabled event validates that the contextlevel is correct
892      */
893     public function test_readtracking_disabled_contextlevel_validation() {
894         $user = $this->getDataGenerator()->create_user();
895         $course = $this->getDataGenerator()->create_course();
896         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
898         $params = array(
899             'context' => context_system::instance(),
900             'other' => array('forumid' => $forum->id),
901             'relateduserid' => $user->id,
902         );
904         $this->setExpectedException('coding_exception', 'Context level must be CONTEXT_MODULE.');
905         \mod_forum\event\readtracking_disabled::create($params);
906     }
908     /**
909      *  Test the readtracking_disabled event.
910      */
911     public function test_readtracking_disabled() {
912         // Setup test data.
913         $user = $this->getDataGenerator()->create_user();
914         $course = $this->getDataGenerator()->create_course();
915         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
916         $context = context_module::instance($forum->cmid);
918         $params = array(
919             'context' => $context,
920             'other' => array('forumid' => $forum->id),
921             'relateduserid' => $user->id,
922         );
924         $event = \mod_forum\event\readtracking_disabled::create($params);
926         // Trigger and capture the event.
927         $sink = $this->redirectEvents();
928         $event->trigger();
929         $events = $sink->get_events();
930         $this->assertCount(1, $events);
931         $event = reset($events);
933         // Checking that the event contains the expected values.
934         $this->assertInstanceOf('\mod_forum\event\readtracking_disabled', $event);
935         $this->assertEquals($context, $event->get_context());
936         $expected = array($course->id, 'forum', 'stop tracking', "view.php?f={$forum->id}", $forum->id, $forum->cmid);
937         $this->assertEventLegacyLogData($expected, $event);
938         $url = new \moodle_url('/mod/forum/view.php', array('f' => $forum->id));
939         $this->assertEquals($url, $event->get_url());
940         $this->assertEventContextNotUsed($event);
942         $this->assertNotEmpty($event->get_name());
943     }
945     /**
946      *  Ensure subscribers_viewed event validates that the forumid is set.
947      */
948     public function test_subscribers_viewed_forumid_validation() {
949         $user = $this->getDataGenerator()->create_user();
950         $course = $this->getDataGenerator()->create_course();
951         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
953         $params = array(
954             'context' => context_module::instance($forum->cmid),
955             'relateduserid' => $user->id,
956         );
958         $this->setExpectedException('coding_exception', 'The \'forumid\' value must be set in other.');
959         \mod_forum\event\subscribers_viewed::create($params);
960     }
962     /**
963      *  Ensure subscribers_viewed event validates that the contextlevel is correct.
964      */
965     public function test_subscribers_viewed_contextlevel_validation() {
966         $user = $this->getDataGenerator()->create_user();
967         $course = $this->getDataGenerator()->create_course();
968         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
970         $params = array(
971             'context' => context_system::instance(),
972             'other' => array('forumid' => $forum->id),
973             'relateduserid' => $user->id,
974         );
976         $this->setExpectedException('coding_exception', 'Context level must be CONTEXT_MODULE.');
977         \mod_forum\event\subscribers_viewed::create($params);
978     }
980     /**
981      *  Test the subscribers_viewed event.
982      */
983     public function test_subscribers_viewed() {
984         // Setup test data.
985         $course = $this->getDataGenerator()->create_course();
986         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
987         $context = context_module::instance($forum->cmid);
989         $params = array(
990             'context' => $context,
991             'other' => array('forumid' => $forum->id),
992         );
994         $event = \mod_forum\event\subscribers_viewed::create($params);
996         // Trigger and capture the event.
997         $sink = $this->redirectEvents();
998         $event->trigger();
999         $events = $sink->get_events();
1000         $this->assertCount(1, $events);
1001         $event = reset($events);
1003         // Checking that the event contains the expected values.
1004         $this->assertInstanceOf('\mod_forum\event\subscribers_viewed', $event);
1005         $this->assertEquals($context, $event->get_context());
1006         $expected = array($course->id, 'forum', 'view subscribers', "subscribers.php?id={$forum->id}", $forum->id, $forum->cmid);
1007         $this->assertEventLegacyLogData($expected, $event);
1008         $this->assertEventContextNotUsed($event);
1010         $this->assertNotEmpty($event->get_name());
1011     }
1013     /**
1014      *  Ensure user_report_viewed event validates that the reportmode is set.
1015      */
1016     public function test_user_report_viewed_reportmode_validation() {
1017         $user = $this->getDataGenerator()->create_user();
1018         $course = $this->getDataGenerator()->create_course();
1020         $params = array(
1021             'context' => context_course::instance($course->id),
1022             'relateduserid' => $user->id,
1023         );
1025         $this->setExpectedException('coding_exception', 'The \'reportmode\' value must be set in other.');
1026         \mod_forum\event\user_report_viewed::create($params);
1027     }
1029     /**
1030      *  Ensure user_report_viewed event validates that the contextlevel is correct.
1031      */
1032     public function test_user_report_viewed_contextlevel_validation() {
1033         $user = $this->getDataGenerator()->create_user();
1034         $course = $this->getDataGenerator()->create_course();
1035         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
1037         $params = array(
1038             'context' => context_module::instance($forum->cmid),
1039             'other' => array('reportmode' => 'posts'),
1040             'relateduserid' => $user->id,
1041         );
1043         $this->setExpectedException('coding_exception',
1044                 'Context level must be either CONTEXT_SYSTEM, CONTEXT_COURSE or CONTEXT_USER.');
1045         \mod_forum\event\user_report_viewed::create($params);
1046     }
1048     /**
1049      *  Ensure user_report_viewed event validates that the relateduserid is set.
1050      */
1051     public function test_user_report_viewed_relateduserid_validation() {
1053         $params = array(
1054             'context' => context_system::instance(),
1055             'other' => array('reportmode' => 'posts'),
1056         );
1058         $this->setExpectedException('coding_exception', 'The \'relateduserid\' must be set.');
1059         \mod_forum\event\user_report_viewed::create($params);
1060     }
1062     /**
1063      * Test the user_report_viewed event.
1064      */
1065     public function test_user_report_viewed() {
1066         // Setup test data.
1067         $user = $this->getDataGenerator()->create_user();
1068         $course = $this->getDataGenerator()->create_course();
1069         $context = context_course::instance($course->id);
1071         $params = array(
1072             'context' => $context,
1073             'relateduserid' => $user->id,
1074             'other' => array('reportmode' => 'discussions'),
1075         );
1077         $event = \mod_forum\event\user_report_viewed::create($params);
1079         // Trigger and capture the event.
1080         $sink = $this->redirectEvents();
1081         $event->trigger();
1082         $events = $sink->get_events();
1083         $this->assertCount(1, $events);
1084         $event = reset($events);
1086         // Checking that the event contains the expected values.
1087         $this->assertInstanceOf('\mod_forum\event\user_report_viewed', $event);
1088         $this->assertEquals($context, $event->get_context());
1089         $expected = array($course->id, 'forum', 'user report',
1090             "user.php?id={$user->id}&amp;mode=discussions&amp;course={$course->id}", $user->id);
1091         $this->assertEventLegacyLogData($expected, $event);
1092         $this->assertEventContextNotUsed($event);
1094         $this->assertNotEmpty($event->get_name());
1095     }
1097     /**
1098      *  Ensure post_created event validates that the postid is set.
1099      */
1100     public function test_post_created_postid_validation() {
1101         $course = $this->getDataGenerator()->create_course();
1102         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
1103         $user = $this->getDataGenerator()->create_user();
1105         // Add a discussion.
1106         $record = array();
1107         $record['course'] = $course->id;
1108         $record['forum'] = $forum->id;
1109         $record['userid'] = $user->id;
1110         $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1112         $params = array(
1113             'context' => context_module::instance($forum->cmid),
1114             'other' => array('forumid' => $forum->id, 'forumtype' => $forum->type, 'discussionid' => $discussion->id)
1115         );
1117         \mod_forum\event\post_created::create($params);
1118     }
1120     /**
1121      *  Ensure post_created event validates that the discussionid is set.
1122      */
1123     public function test_post_created_discussionid_validation() {
1124         $course = $this->getDataGenerator()->create_course();
1125         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
1126         $user = $this->getDataGenerator()->create_user();
1128         // Add a discussion.
1129         $record = array();
1130         $record['course'] = $course->id;
1131         $record['forum'] = $forum->id;
1132         $record['userid'] = $user->id;
1133         $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1135         // Add a post.
1136         $record = array();
1137         $record['discussion'] = $discussion->id;
1138         $record['userid'] = $user->id;
1139         $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
1141         $params = array(
1142             'context' => context_module::instance($forum->cmid),
1143             'objectid' => $post->id,
1144             'other' => array('forumid' => $forum->id, 'forumtype' => $forum->type)
1145         );
1147         $this->setExpectedException('coding_exception', 'The \'discussionid\' value must be set in other.');
1148         \mod_forum\event\post_created::create($params);
1149     }
1151     /**
1152      *  Ensure post_created event validates that the forumid is set.
1153      */
1154     public function test_post_created_forumid_validation() {
1155         $course = $this->getDataGenerator()->create_course();
1156         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
1157         $user = $this->getDataGenerator()->create_user();
1159         // Add a discussion.
1160         $record = array();
1161         $record['course'] = $course->id;
1162         $record['forum'] = $forum->id;
1163         $record['userid'] = $user->id;
1164         $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1166         // Add a post.
1167         $record = array();
1168         $record['discussion'] = $discussion->id;
1169         $record['userid'] = $user->id;
1170         $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
1172         $params = array(
1173             'context' => context_module::instance($forum->cmid),
1174             'objectid' => $post->id,
1175             'other' => array('discussionid' => $discussion->id, 'forumtype' => $forum->type)
1176         );
1178         $this->setExpectedException('coding_exception', 'The \'forumid\' value must be set in other.');
1179         \mod_forum\event\post_created::create($params);
1180     }
1182     /**
1183      *  Ensure post_created event validates that the forumtype is set.
1184      */
1185     public function test_post_created_forumtype_validation() {
1186         $course = $this->getDataGenerator()->create_course();
1187         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
1188         $user = $this->getDataGenerator()->create_user();
1190         // Add a discussion.
1191         $record = array();
1192         $record['course'] = $course->id;
1193         $record['forum'] = $forum->id;
1194         $record['userid'] = $user->id;
1195         $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1197         // Add a post.
1198         $record = array();
1199         $record['discussion'] = $discussion->id;
1200         $record['userid'] = $user->id;
1201         $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
1203         $params = array(
1204             'context' => context_module::instance($forum->cmid),
1205             'objectid' => $post->id,
1206             'other' => array('discussionid' => $discussion->id, 'forumid' => $forum->id)
1207         );
1209         $this->setExpectedException('coding_exception', 'The \'forumtype\' value must be set in other.');
1210         \mod_forum\event\post_created::create($params);
1211     }
1213     /**
1214      *  Ensure post_created event validates that the contextlevel is correct.
1215      */
1216     public function test_post_created_context_validation() {
1217         $course = $this->getDataGenerator()->create_course();
1218         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
1219         $user = $this->getDataGenerator()->create_user();
1221         // Add a discussion.
1222         $record = array();
1223         $record['course'] = $course->id;
1224         $record['forum'] = $forum->id;
1225         $record['userid'] = $user->id;
1226         $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1228         // Add a post.
1229         $record = array();
1230         $record['discussion'] = $discussion->id;
1231         $record['userid'] = $user->id;
1232         $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
1234         $params = array(
1235             'context' => context_system::instance(),
1236             'objectid' => $post->id,
1237             'other' => array('discussionid' => $discussion->id, 'forumid' => $forum->id, 'forumtype' => $forum->type)
1238         );
1240         $this->setExpectedException('coding_exception', 'Context level must be CONTEXT_MODULE');
1241         \mod_forum\event\post_created::create($params);
1242     }
1244     /**
1245      * Test the post_created event.
1246      */
1247     public function test_post_created() {
1248         // Setup test data.
1249         $course = $this->getDataGenerator()->create_course();
1250         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
1251         $user = $this->getDataGenerator()->create_user();
1253         // Add a discussion.
1254         $record = array();
1255         $record['course'] = $course->id;
1256         $record['forum'] = $forum->id;
1257         $record['userid'] = $user->id;
1258         $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1260         // Add a post.
1261         $record = array();
1262         $record['discussion'] = $discussion->id;
1263         $record['userid'] = $user->id;
1264         $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
1266         $context = context_module::instance($forum->cmid);
1268         $params = array(
1269             'context' => $context,
1270             'objectid' => $post->id,
1271             'other' => array('discussionid' => $discussion->id, 'forumid' => $forum->id, 'forumtype' => $forum->type)
1272         );
1274         $event = \mod_forum\event\post_created::create($params);
1276         // Trigger and capturing the event.
1277         $sink = $this->redirectEvents();
1278         $event->trigger();
1279         $events = $sink->get_events();
1280         $this->assertCount(1, $events);
1281         $event = reset($events);
1283         // Checking that the event contains the expected values.
1284         $this->assertInstanceOf('\mod_forum\event\post_created', $event);
1285         $this->assertEquals($context, $event->get_context());
1286         $expected = array($course->id, 'forum', 'add post', "discuss.php?d={$discussion->id}#p{$post->id}",
1287             $forum->id, $forum->cmid);
1288         $this->assertEventLegacyLogData($expected, $event);
1289         $url = new \moodle_url('/mod/forum/discuss.php', array('d' => $discussion->id));
1290         $url->set_anchor('p'.$event->objectid);
1291         $this->assertEquals($url, $event->get_url());
1292         $this->assertEventContextNotUsed($event);
1294         $this->assertNotEmpty($event->get_name());
1295     }
1297     /**
1298      * Test the post_created event for a single discussion forum.
1299      */
1300     public function test_post_created_single() {
1301         // Setup test data.
1302         $course = $this->getDataGenerator()->create_course();
1303         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id, 'type' => 'single'));
1304         $user = $this->getDataGenerator()->create_user();
1306         // Add a discussion.
1307         $record = array();
1308         $record['course'] = $course->id;
1309         $record['forum'] = $forum->id;
1310         $record['userid'] = $user->id;
1311         $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1313         // Add a post.
1314         $record = array();
1315         $record['discussion'] = $discussion->id;
1316         $record['userid'] = $user->id;
1317         $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
1319         $context = context_module::instance($forum->cmid);
1321         $params = array(
1322             'context' => $context,
1323             'objectid' => $post->id,
1324             'other' => array('discussionid' => $discussion->id, 'forumid' => $forum->id, 'forumtype' => $forum->type)
1325         );
1327         $event = \mod_forum\event\post_created::create($params);
1329         // Trigger and capturing the event.
1330         $sink = $this->redirectEvents();
1331         $event->trigger();
1332         $events = $sink->get_events();
1333         $this->assertCount(1, $events);
1334         $event = reset($events);
1336         // Checking that the event contains the expected values.
1337         $this->assertInstanceOf('\mod_forum\event\post_created', $event);
1338         $this->assertEquals($context, $event->get_context());
1339         $expected = array($course->id, 'forum', 'add post', "view.php?f={$forum->id}#p{$post->id}",
1340             $forum->id, $forum->cmid);
1341         $this->assertEventLegacyLogData($expected, $event);
1342         $url = new \moodle_url('/mod/forum/view.php', array('f' => $forum->id));
1343         $url->set_anchor('p'.$event->objectid);
1344         $this->assertEquals($url, $event->get_url());
1345         $this->assertEventContextNotUsed($event);
1347         $this->assertNotEmpty($event->get_name());
1348     }
1350     /**
1351      *  Ensure post_deleted event validates that the postid is set.
1352      */
1353     public function test_post_deleted_postid_validation() {
1354         $course = $this->getDataGenerator()->create_course();
1355         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
1356         $user = $this->getDataGenerator()->create_user();
1358         // Add a discussion.
1359         $record = array();
1360         $record['course'] = $course->id;
1361         $record['forum'] = $forum->id;
1362         $record['userid'] = $user->id;
1363         $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1365         $params = array(
1366             'context' => context_module::instance($forum->cmid),
1367             'other' => array('forumid' => $forum->id, 'forumtype' => $forum->type, 'discussionid' => $discussion->id)
1368         );
1370         \mod_forum\event\post_deleted::create($params);
1371     }
1373     /**
1374      *  Ensure post_deleted event validates that the discussionid is set.
1375      */
1376     public function test_post_deleted_discussionid_validation() {
1377         $course = $this->getDataGenerator()->create_course();
1378         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
1379         $user = $this->getDataGenerator()->create_user();
1381         // Add a discussion.
1382         $record = array();
1383         $record['course'] = $course->id;
1384         $record['forum'] = $forum->id;
1385         $record['userid'] = $user->id;
1386         $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1388         // Add a post.
1389         $record = array();
1390         $record['discussion'] = $discussion->id;
1391         $record['userid'] = $user->id;
1392         $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
1394         $params = array(
1395             'context' => context_module::instance($forum->cmid),
1396             'objectid' => $post->id,
1397             'other' => array('forumid' => $forum->id, 'forumtype' => $forum->type)
1398         );
1400         $this->setExpectedException('coding_exception', 'The \'discussionid\' value must be set in other.');
1401         \mod_forum\event\post_deleted::create($params);
1402     }
1404     /**
1405      *  Ensure post_deleted event validates that the forumid is set.
1406      */
1407     public function test_post_deleted_forumid_validation() {
1408         $course = $this->getDataGenerator()->create_course();
1409         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
1410         $user = $this->getDataGenerator()->create_user();
1412         // Add a discussion.
1413         $record = array();
1414         $record['course'] = $course->id;
1415         $record['forum'] = $forum->id;
1416         $record['userid'] = $user->id;
1417         $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1419         // Add a post.
1420         $record = array();
1421         $record['discussion'] = $discussion->id;
1422         $record['userid'] = $user->id;
1423         $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
1425         $params = array(
1426             'context' => context_module::instance($forum->cmid),
1427             'objectid' => $post->id,
1428             'other' => array('discussionid' => $discussion->id, 'forumtype' => $forum->type)
1429         );
1431         $this->setExpectedException('coding_exception', 'The \'forumid\' value must be set in other.');
1432         \mod_forum\event\post_deleted::create($params);
1433     }
1435     /**
1436      *  Ensure post_deleted event validates that the forumtype is set.
1437      */
1438     public function test_post_deleted_forumtype_validation() {
1439         $course = $this->getDataGenerator()->create_course();
1440         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
1441         $user = $this->getDataGenerator()->create_user();
1443         // Add a discussion.
1444         $record = array();
1445         $record['course'] = $course->id;
1446         $record['forum'] = $forum->id;
1447         $record['userid'] = $user->id;
1448         $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1450         // Add a post.
1451         $record = array();
1452         $record['discussion'] = $discussion->id;
1453         $record['userid'] = $user->id;
1454         $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
1456         $params = array(
1457             'context' => context_module::instance($forum->cmid),
1458             'objectid' => $post->id,
1459             'other' => array('discussionid' => $discussion->id, 'forumid' => $forum->id)
1460         );
1462         $this->setExpectedException('coding_exception', 'The \'forumtype\' value must be set in other.');
1463         \mod_forum\event\post_deleted::create($params);
1464     }
1466     /**
1467      *  Ensure post_deleted event validates that the contextlevel is correct.
1468      */
1469     public function test_post_deleted_context_validation() {
1470         $course = $this->getDataGenerator()->create_course();
1471         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
1472         $user = $this->getDataGenerator()->create_user();
1474         // Add a discussion.
1475         $record = array();
1476         $record['course'] = $course->id;
1477         $record['forum'] = $forum->id;
1478         $record['userid'] = $user->id;
1479         $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1481         // Add a post.
1482         $record = array();
1483         $record['discussion'] = $discussion->id;
1484         $record['userid'] = $user->id;
1485         $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
1487         $params = array(
1488             'context' => context_system::instance(),
1489             'objectid' => $post->id,
1490             'other' => array('discussionid' => $discussion->id, 'forumid' => $forum->id, 'forumtype' => $forum->type)
1491         );
1493         $this->setExpectedException('coding_exception', 'Context level must be CONTEXT_MODULE');
1494         \mod_forum\event\post_deleted::create($params);
1495     }
1497     /**
1498      * Test post_deleted event.
1499      */
1500     public function test_post_deleted() {
1501         global $DB;
1503         // Setup test data.
1504         $course = $this->getDataGenerator()->create_course();
1505         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
1506         $user = $this->getDataGenerator()->create_user();
1507         $cm = get_coursemodule_from_instance('forum', $forum->id, $forum->course);
1509         // Add a discussion.
1510         $record = array();
1511         $record['course'] = $course->id;
1512         $record['forum'] = $forum->id;
1513         $record['userid'] = $user->id;
1514         $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1516         // When creating a discussion we also create a post, so get the post.
1517         $discussionpost = $DB->get_records('forum_posts');
1518         // Will only be one here.
1519         $discussionpost = reset($discussionpost);
1521         // Add a few posts.
1522         $record = array();
1523         $record['discussion'] = $discussion->id;
1524         $record['userid'] = $user->id;
1525         $posts = array();
1526         $posts[$discussionpost->id] = $discussionpost;
1527         for ($i = 0; $i < 3; $i++) {
1528             $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
1529             $posts[$post->id] = $post;
1530         }
1532         // Delete the last post and capture the event.
1533         $lastpost = end($posts);
1534         $sink = $this->redirectEvents();
1535         forum_delete_post($lastpost, true, $course, $cm, $forum);
1536         $events = $sink->get_events();
1537         $this->assertCount(1, $events);
1538         $event = reset($events);
1540         // Check that the events contain the expected values.
1541         $this->assertInstanceOf('\mod_forum\event\post_deleted', $event);
1542         $this->assertEquals(context_module::instance($forum->cmid), $event->get_context());
1543         $expected = array($course->id, 'forum', 'delete post', "discuss.php?d={$discussion->id}", $lastpost->id, $forum->cmid);
1544         $this->assertEventLegacyLogData($expected, $event);
1545         $url = new \moodle_url('/mod/forum/discuss.php', array('d' => $discussion->id));
1546         $this->assertEquals($url, $event->get_url());
1547         $this->assertEventContextNotUsed($event);
1548         $this->assertNotEmpty($event->get_name());
1550         // Delete the whole discussion and capture the events.
1551         $sink = $this->redirectEvents();
1552         forum_delete_discussion($discussion, true, $course, $cm, $forum);
1553         $events = $sink->get_events();
1554         // We will have 3 events. One for the discussion (creating a discussion creates a post), and two for the posts.
1555         $this->assertCount(3, $events);
1557         // Loop through the events and check they are valid.
1558         foreach ($events as $event) {
1559             $post = $posts[$event->objectid];
1561             // Check that the event contains the expected values.
1562             $this->assertInstanceOf('\mod_forum\event\post_deleted', $event);
1563             $this->assertEquals(context_module::instance($forum->cmid), $event->get_context());
1564             $expected = array($course->id, 'forum', 'delete post', "discuss.php?d={$discussion->id}", $post->id, $forum->cmid);
1565             $this->assertEventLegacyLogData($expected, $event);
1566             $url = new \moodle_url('/mod/forum/discuss.php', array('d' => $discussion->id));
1567             $this->assertEquals($url, $event->get_url());
1568             $this->assertEventContextNotUsed($event);
1569             $this->assertNotEmpty($event->get_name());
1570         }
1571     }
1573     /**
1574      * Test post_deleted event for a single discussion forum.
1575      */
1576     public function test_post_deleted_single() {
1577         // Setup test data.
1578         $course = $this->getDataGenerator()->create_course();
1579         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id, 'type' => 'single'));
1580         $user = $this->getDataGenerator()->create_user();
1582         // Add a discussion.
1583         $record = array();
1584         $record['course'] = $course->id;
1585         $record['forum'] = $forum->id;
1586         $record['userid'] = $user->id;
1587         $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1589         // Add a post.
1590         $record = array();
1591         $record['discussion'] = $discussion->id;
1592         $record['userid'] = $user->id;
1593         $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
1595         $context = context_module::instance($forum->cmid);
1597         $params = array(
1598             'context' => $context,
1599             'objectid' => $post->id,
1600             'other' => array('discussionid' => $discussion->id, 'forumid' => $forum->id, 'forumtype' => $forum->type)
1601         );
1603         $event = \mod_forum\event\post_deleted::create($params);
1605         // Trigger and capture the event.
1606         $sink = $this->redirectEvents();
1607         $event->trigger();
1608         $events = $sink->get_events();
1609         $this->assertCount(1, $events);
1610         $event = reset($events);
1612         // Checking that the event contains the expected values.
1613         $this->assertInstanceOf('\mod_forum\event\post_deleted', $event);
1614         $this->assertEquals($context, $event->get_context());
1615         $expected = array($course->id, 'forum', 'delete post', "view.php?f={$forum->id}", $post->id, $forum->cmid);
1616         $this->assertEventLegacyLogData($expected, $event);
1617         $url = new \moodle_url('/mod/forum/view.php', array('f' => $forum->id));
1618         $this->assertEquals($url, $event->get_url());
1619         $this->assertEventContextNotUsed($event);
1621         $this->assertNotEmpty($event->get_name());
1622     }
1624     /**
1625      *  Ensure post_updated event validates that the discussionid is set.
1626      */
1627     public function test_post_updated_discussionid_validation() {
1628         $course = $this->getDataGenerator()->create_course();
1629         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
1630         $user = $this->getDataGenerator()->create_user();
1632         // Add a discussion.
1633         $record = array();
1634         $record['course'] = $course->id;
1635         $record['forum'] = $forum->id;
1636         $record['userid'] = $user->id;
1637         $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1639         // Add a post.
1640         $record = array();
1641         $record['discussion'] = $discussion->id;
1642         $record['userid'] = $user->id;
1643         $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
1645         $params = array(
1646             'context' => context_module::instance($forum->cmid),
1647             'objectid' => $post->id,
1648             'other' => array('forumid' => $forum->id, 'forumtype' => $forum->type)
1649         );
1651         $this->setExpectedException('coding_exception', 'The \'discussionid\' value must be set in other.');
1652         \mod_forum\event\post_updated::create($params);
1653     }
1655     /**
1656      *  Ensure post_updated event validates that the forumid is set.
1657      */
1658     public function test_post_updated_forumid_validation() {
1659         $course = $this->getDataGenerator()->create_course();
1660         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
1661         $user = $this->getDataGenerator()->create_user();
1663         // Add a discussion.
1664         $record = array();
1665         $record['course'] = $course->id;
1666         $record['forum'] = $forum->id;
1667         $record['userid'] = $user->id;
1668         $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1670         // Add a post.
1671         $record = array();
1672         $record['discussion'] = $discussion->id;
1673         $record['userid'] = $user->id;
1674         $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
1676         $params = array(
1677             'context' => context_module::instance($forum->cmid),
1678             'objectid' => $post->id,
1679             'other' => array('discussionid' => $discussion->id, 'forumtype' => $forum->type)
1680         );
1682         $this->setExpectedException('coding_exception', 'The \'forumid\' value must be set in other.');
1683         \mod_forum\event\post_updated::create($params);
1684     }
1686     /**
1687      *  Ensure post_updated event validates that the forumtype is set.
1688      */
1689     public function test_post_updated_forumtype_validation() {
1690         $course = $this->getDataGenerator()->create_course();
1691         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
1692         $user = $this->getDataGenerator()->create_user();
1694         // Add a discussion.
1695         $record = array();
1696         $record['course'] = $course->id;
1697         $record['forum'] = $forum->id;
1698         $record['userid'] = $user->id;
1699         $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1701         // Add a post.
1702         $record = array();
1703         $record['discussion'] = $discussion->id;
1704         $record['userid'] = $user->id;
1705         $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
1707         $params = array(
1708             'context' => context_module::instance($forum->cmid),
1709             'objectid' => $post->id,
1710             'other' => array('discussionid' => $discussion->id, 'forumid' => $forum->id)
1711         );
1713         $this->setExpectedException('coding_exception', 'The \'forumtype\' value must be set in other.');
1714         \mod_forum\event\post_updated::create($params);
1715     }
1717     /**
1718      *  Ensure post_updated event validates that the contextlevel is correct.
1719      */
1720     public function test_post_updated_context_validation() {
1721         $course = $this->getDataGenerator()->create_course();
1722         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
1723         $user = $this->getDataGenerator()->create_user();
1725         // Add a discussion.
1726         $record = array();
1727         $record['course'] = $course->id;
1728         $record['forum'] = $forum->id;
1729         $record['userid'] = $user->id;
1730         $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1732         // Add a post.
1733         $record = array();
1734         $record['discussion'] = $discussion->id;
1735         $record['userid'] = $user->id;
1736         $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
1738         $params = array(
1739             'context' => context_system::instance(),
1740             'objectid' => $post->id,
1741             'other' => array('discussionid' => $discussion->id, 'forumid' => $forum->id, 'forumtype' => $forum->type)
1742         );
1744         $this->setExpectedException('coding_exception', 'Context level must be CONTEXT_MODULE');
1745         \mod_forum\event\post_updated::create($params);
1746     }
1748     /**
1749      * Test post_updated event.
1750      */
1751     public function test_post_updated() {
1752         // Setup test data.
1753         $course = $this->getDataGenerator()->create_course();
1754         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
1755         $user = $this->getDataGenerator()->create_user();
1757         // Add a discussion.
1758         $record = array();
1759         $record['course'] = $course->id;
1760         $record['forum'] = $forum->id;
1761         $record['userid'] = $user->id;
1762         $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1764         // Add a post.
1765         $record = array();
1766         $record['discussion'] = $discussion->id;
1767         $record['userid'] = $user->id;
1768         $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
1770         $context = context_module::instance($forum->cmid);
1772         $params = array(
1773             'context' => $context,
1774             'objectid' => $post->id,
1775             'other' => array('discussionid' => $discussion->id, 'forumid' => $forum->id, 'forumtype' => $forum->type)
1776         );
1778         $event = \mod_forum\event\post_updated::create($params);
1780         // Trigger and capturing the event.
1781         $sink = $this->redirectEvents();
1782         $event->trigger();
1783         $events = $sink->get_events();
1784         $this->assertCount(1, $events);
1785         $event = reset($events);
1787         // Checking that the event contains the expected values.
1788         $this->assertInstanceOf('\mod_forum\event\post_updated', $event);
1789         $this->assertEquals($context, $event->get_context());
1790         $expected = array($course->id, 'forum', 'update post', "discuss.php?d={$discussion->id}#p{$post->id}",
1791             $post->id, $forum->cmid);
1792         $this->assertEventLegacyLogData($expected, $event);
1793         $url = new \moodle_url('/mod/forum/discuss.php', array('d' => $discussion->id));
1794         $url->set_anchor('p'.$event->objectid);
1795         $this->assertEquals($url, $event->get_url());
1796         $this->assertEventContextNotUsed($event);
1798         $this->assertNotEmpty($event->get_name());
1799     }
1801     /**
1802      * Test post_updated event.
1803      */
1804     public function test_post_updated_single() {
1805         // Setup test data.
1806         $course = $this->getDataGenerator()->create_course();
1807         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id, 'type' => 'single'));
1808         $user = $this->getDataGenerator()->create_user();
1810         // Add a discussion.
1811         $record = array();
1812         $record['course'] = $course->id;
1813         $record['forum'] = $forum->id;
1814         $record['userid'] = $user->id;
1815         $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1817         // Add a post.
1818         $record = array();
1819         $record['discussion'] = $discussion->id;
1820         $record['userid'] = $user->id;
1821         $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
1823         $context = context_module::instance($forum->cmid);
1825         $params = array(
1826             'context' => $context,
1827             'objectid' => $post->id,
1828             'other' => array('discussionid' => $discussion->id, 'forumid' => $forum->id, 'forumtype' => $forum->type)
1829         );
1831         $event = \mod_forum\event\post_updated::create($params);
1833         // Trigger and capturing the event.
1834         $sink = $this->redirectEvents();
1835         $event->trigger();
1836         $events = $sink->get_events();
1837         $this->assertCount(1, $events);
1838         $event = reset($events);
1840         // Checking that the event contains the expected values.
1841         $this->assertInstanceOf('\mod_forum\event\post_updated', $event);
1842         $this->assertEquals($context, $event->get_context());
1843         $expected = array($course->id, 'forum', 'update post', "view.php?f={$forum->id}#p{$post->id}",
1844             $post->id, $forum->cmid);
1845         $this->assertEventLegacyLogData($expected, $event);
1846         $url = new \moodle_url('/mod/forum/view.php', array('f' => $forum->id));
1847         $url->set_anchor('p'.$post->id);
1848         $this->assertEquals($url, $event->get_url());
1849         $this->assertEventContextNotUsed($event);
1851         $this->assertNotEmpty($event->get_name());
1852     }
1854     /**
1855      * Test discussion_subscription_created event.
1856      */
1857     public function test_discussion_subscription_created() {
1858         global $CFG;
1859         require_once($CFG->dirroot . '/mod/forum/lib.php');
1861         // Setup test data.
1862         $course = $this->getDataGenerator()->create_course();
1863         $user = $this->getDataGenerator()->create_user();
1864         $this->getDataGenerator()->enrol_user($user->id, $course->id);
1866         $options = array('course' => $course->id, 'forcesubscribe' => FORUM_CHOOSESUBSCRIBE);
1867         $forum = $this->getDataGenerator()->create_module('forum', $options);
1869         // Add a discussion.
1870         $record = array();
1871         $record['course'] = $course->id;
1872         $record['forum'] = $forum->id;
1873         $record['userid'] = $user->id;
1874         $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1876         // Add a post.
1877         $record = array();
1878         $record['discussion'] = $discussion->id;
1879         $record['userid'] = $user->id;
1880         $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
1882         // Trigger and capturing the event.
1883         $sink = $this->redirectEvents();
1885         // Trigger the event by subscribing the user to the forum discussion.
1886         \mod_forum\subscriptions::subscribe_user_to_discussion($user->id, $discussion);
1888         $events = $sink->get_events();
1889         $this->assertCount(1, $events);
1890         $event = reset($events);
1892         // Checking that the event contains the expected values.
1893         $this->assertInstanceOf('\mod_forum\event\discussion_subscription_created', $event);
1896         $cm = get_coursemodule_from_instance('forum', $discussion->forum);
1897         $context = \context_module::instance($cm->id);
1898         $this->assertEquals($context, $event->get_context());
1900         $url = new \moodle_url('/mod/forum/subscribe.php', array(
1901             'id' => $forum->id,
1902             'd' => $discussion->id
1903         ));
1905         $this->assertEquals($url, $event->get_url());
1906         $this->assertEventContextNotUsed($event);
1907         $this->assertNotEmpty($event->get_name());
1908     }
1910     /**
1911      * Test validation of discussion_subscription_created event.
1912      */
1913     public function test_discussion_subscription_created_validation() {
1914         global $CFG, $DB;
1915         require_once($CFG->dirroot . '/mod/forum/lib.php');
1917         // Setup test data.
1918         $course = $this->getDataGenerator()->create_course();
1919         $user = $this->getDataGenerator()->create_user();
1920         $this->getDataGenerator()->enrol_user($user->id, $course->id);
1922         $options = array('course' => $course->id, 'forcesubscribe' => FORUM_CHOOSESUBSCRIBE);
1923         $forum = $this->getDataGenerator()->create_module('forum', $options);
1925         // Add a discussion.
1926         $record = array();
1927         $record['course'] = $course->id;
1928         $record['forum'] = $forum->id;
1929         $record['userid'] = $user->id;
1930         $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1932         // Add a post.
1933         $record = array();
1934         $record['discussion'] = $discussion->id;
1935         $record['userid'] = $user->id;
1936         $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
1938         // The user is not subscribed to the forum. Insert a new discussion subscription.
1939         $subscription = new \stdClass();
1940         $subscription->userid  = $user->id;
1941         $subscription->forum = $forum->id;
1942         $subscription->discussion = $discussion->id;
1943         $subscription->preference = time();
1945         $subscription->id = $DB->insert_record('forum_discussion_subs', $subscription);
1947         $context = context_module::instance($forum->cmid);
1949         $params = array(
1950             'context' => $context,
1951             'objectid' => $subscription->id,
1952             'relateduserid' => $user->id,
1953             'other' => array(
1954                 'forumid' => $forum->id,
1955                 'discussion' => $discussion->id,
1956             )
1957         );
1959         $event = \mod_forum\event\discussion_subscription_created::create($params);
1961         // Trigger and capturing the event.
1962         $sink = $this->redirectEvents();
1963         $event->trigger();
1964         $events = $sink->get_events();
1965         $this->assertCount(1, $events);
1966         $event = reset($events);
1967     }
1969     /**
1970      * Test contextlevel validation of discussion_subscription_created event.
1971      */
1972     public function test_discussion_subscription_created_validation_contextlevel() {
1973         global $CFG, $DB;
1974         require_once($CFG->dirroot . '/mod/forum/lib.php');
1976         // Setup test data.
1977         $course = $this->getDataGenerator()->create_course();
1978         $user = $this->getDataGenerator()->create_user();
1979         $this->getDataGenerator()->enrol_user($user->id, $course->id);
1981         $options = array('course' => $course->id, 'forcesubscribe' => FORUM_CHOOSESUBSCRIBE);
1982         $forum = $this->getDataGenerator()->create_module('forum', $options);
1984         // Add a discussion.
1985         $record = array();
1986         $record['course'] = $course->id;
1987         $record['forum'] = $forum->id;
1988         $record['userid'] = $user->id;
1989         $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1991         // Add a post.
1992         $record = array();
1993         $record['discussion'] = $discussion->id;
1994         $record['userid'] = $user->id;
1995         $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
1997         // The user is not subscribed to the forum. Insert a new discussion subscription.
1998         $subscription = new \stdClass();
1999         $subscription->userid  = $user->id;
2000         $subscription->forum = $forum->id;
2001         $subscription->discussion = $discussion->id;
2002         $subscription->preference = time();
2004         $subscription->id = $DB->insert_record('forum_discussion_subs', $subscription);
2006         $context = context_module::instance($forum->cmid);
2008         $params = array(
2009             'context' => \context_course::instance($course->id),
2010             'objectid' => $subscription->id,
2011             'relateduserid' => $user->id,
2012             'other' => array(
2013                 'forumid' => $forum->id,
2014                 'discussion' => $discussion->id,
2015             )
2016         );
2018         // Without an invalid context.
2019         $this->setExpectedException('coding_exception', 'Context level must be CONTEXT_MODULE.');
2020         \mod_forum\event\discussion_subscription_created::create($params);
2021     }
2023     /**
2024      * Test discussion validation of discussion_subscription_created event.
2025      */
2026     public function test_discussion_subscription_created_validation_discussion() {
2027         global $CFG, $DB;
2028         require_once($CFG->dirroot . '/mod/forum/lib.php');
2030         // Setup test data.
2031         $course = $this->getDataGenerator()->create_course();
2032         $user = $this->getDataGenerator()->create_user();
2033         $this->getDataGenerator()->enrol_user($user->id, $course->id);
2035         $options = array('course' => $course->id, 'forcesubscribe' => FORUM_CHOOSESUBSCRIBE);
2036         $forum = $this->getDataGenerator()->create_module('forum', $options);
2038         // Add a discussion.
2039         $record = array();
2040         $record['course'] = $course->id;
2041         $record['forum'] = $forum->id;
2042         $record['userid'] = $user->id;
2043         $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
2045         // Add a post.
2046         $record = array();
2047         $record['discussion'] = $discussion->id;
2048         $record['userid'] = $user->id;
2049         $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
2051         // The user is not subscribed to the forum. Insert a new discussion subscription.
2052         $subscription = new \stdClass();
2053         $subscription->userid  = $user->id;
2054         $subscription->forum = $forum->id;
2055         $subscription->discussion = $discussion->id;
2056         $subscription->preference = time();
2058         $subscription->id = $DB->insert_record('forum_discussion_subs', $subscription);
2060         // Without the discussion.
2061         $params = array(
2062             'context' => context_module::instance($forum->cmid),
2063             'objectid' => $subscription->id,
2064             'relateduserid' => $user->id,
2065             'other' => array(
2066                 'forumid' => $forum->id,
2067             )
2068         );
2070         $this->setExpectedException('coding_exception', "The 'discussion' value must be set in other.");
2071         \mod_forum\event\discussion_subscription_created::create($params);
2072     }
2074     /**
2075      * Test forumid validation of discussion_subscription_created event.
2076      */
2077     public function test_discussion_subscription_created_validation_forumid() {
2078         global $CFG, $DB;
2079         require_once($CFG->dirroot . '/mod/forum/lib.php');
2081         // Setup test data.
2082         $course = $this->getDataGenerator()->create_course();
2083         $user = $this->getDataGenerator()->create_user();
2084         $this->getDataGenerator()->enrol_user($user->id, $course->id);
2086         $options = array('course' => $course->id, 'forcesubscribe' => FORUM_CHOOSESUBSCRIBE);
2087         $forum = $this->getDataGenerator()->create_module('forum', $options);
2089         // Add a discussion.
2090         $record = array();
2091         $record['course'] = $course->id;
2092         $record['forum'] = $forum->id;
2093         $record['userid'] = $user->id;
2094         $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
2096         // Add a post.
2097         $record = array();
2098         $record['discussion'] = $discussion->id;
2099         $record['userid'] = $user->id;
2100         $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
2102         // The user is not subscribed to the forum. Insert a new discussion subscription.
2103         $subscription = new \stdClass();
2104         $subscription->userid  = $user->id;
2105         $subscription->forum = $forum->id;
2106         $subscription->discussion = $discussion->id;
2107         $subscription->preference = time();
2109         $subscription->id = $DB->insert_record('forum_discussion_subs', $subscription);
2111         // Without the forumid.
2112         $params = array(
2113             'context' => context_module::instance($forum->cmid),
2114             'objectid' => $subscription->id,
2115             'relateduserid' => $user->id,
2116             'other' => array(
2117                 'discussion' => $discussion->id,
2118             )
2119         );
2121         $this->setExpectedException('coding_exception', "The 'forumid' value must be set in other.");
2122         \mod_forum\event\discussion_subscription_created::create($params);
2123     }
2125     /**
2126      * Test relateduserid validation of discussion_subscription_created event.
2127      */
2128     public function test_discussion_subscription_created_validation_relateduserid() {
2129         global $CFG, $DB;
2130         require_once($CFG->dirroot . '/mod/forum/lib.php');
2132         // Setup test data.
2133         $course = $this->getDataGenerator()->create_course();
2134         $user = $this->getDataGenerator()->create_user();
2135         $this->getDataGenerator()->enrol_user($user->id, $course->id);
2137         $options = array('course' => $course->id, 'forcesubscribe' => FORUM_CHOOSESUBSCRIBE);
2138         $forum = $this->getDataGenerator()->create_module('forum', $options);
2140         // Add a discussion.
2141         $record = array();
2142         $record['course'] = $course->id;
2143         $record['forum'] = $forum->id;
2144         $record['userid'] = $user->id;
2145         $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
2147         // Add a post.
2148         $record = array();
2149         $record['discussion'] = $discussion->id;
2150         $record['userid'] = $user->id;
2151         $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
2153         // The user is not subscribed to the forum. Insert a new discussion subscription.
2154         $subscription = new \stdClass();
2155         $subscription->userid  = $user->id;
2156         $subscription->forum = $forum->id;
2157         $subscription->discussion = $discussion->id;
2158         $subscription->preference = time();
2160         $subscription->id = $DB->insert_record('forum_discussion_subs', $subscription);
2162         $context = context_module::instance($forum->cmid);
2164         // Without the relateduserid.
2165         $params = array(
2166             'context' => context_module::instance($forum->cmid),
2167             'objectid' => $subscription->id,
2168             'other' => array(
2169                 'forumid' => $forum->id,
2170                 'discussion' => $discussion->id,
2171             )
2172         );
2174         $this->setExpectedException('coding_exception', "The 'relateduserid' must be set.");
2175         \mod_forum\event\discussion_subscription_created::create($params);
2176     }
2178     /**
2179      * Test discussion_subscription_deleted event.
2180      */
2181     public function test_discussion_subscription_deleted() {
2182         global $CFG;
2183         require_once($CFG->dirroot . '/mod/forum/lib.php');
2185         // Setup test data.
2186         $course = $this->getDataGenerator()->create_course();
2187         $user = $this->getDataGenerator()->create_user();
2188         $this->getDataGenerator()->enrol_user($user->id, $course->id);
2190         $options = array('course' => $course->id, 'forcesubscribe' => FORUM_INITIALSUBSCRIBE);
2191         $forum = $this->getDataGenerator()->create_module('forum', $options);
2193         // Add a discussion.
2194         $record = array();
2195         $record['course'] = $course->id;
2196         $record['forum'] = $forum->id;
2197         $record['userid'] = $user->id;
2198         $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
2200         // Add a post.
2201         $record = array();
2202         $record['discussion'] = $discussion->id;
2203         $record['userid'] = $user->id;
2204         $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
2206         // Trigger and capturing the event.
2207         $sink = $this->redirectEvents();
2209         // Trigger the event by unsubscribing the user to the forum discussion.
2210         \mod_forum\subscriptions::unsubscribe_user_from_discussion($user->id, $discussion);
2212         $events = $sink->get_events();
2213         $this->assertCount(1, $events);
2214         $event = reset($events);
2216         // Checking that the event contains the expected values.
2217         $this->assertInstanceOf('\mod_forum\event\discussion_subscription_deleted', $event);
2220         $cm = get_coursemodule_from_instance('forum', $discussion->forum);
2221         $context = \context_module::instance($cm->id);
2222         $this->assertEquals($context, $event->get_context());
2224         $url = new \moodle_url('/mod/forum/subscribe.php', array(
2225             'id' => $forum->id,
2226             'd' => $discussion->id
2227         ));
2229         $this->assertEquals($url, $event->get_url());
2230         $this->assertEventContextNotUsed($event);
2231         $this->assertNotEmpty($event->get_name());
2232     }
2234     /**
2235      * Test validation of discussion_subscription_deleted event.
2236      */
2237     public function test_discussion_subscription_deleted_validation() {
2238         global $CFG, $DB;
2239         require_once($CFG->dirroot . '/mod/forum/lib.php');
2241         // Setup test data.
2242         $course = $this->getDataGenerator()->create_course();
2243         $user = $this->getDataGenerator()->create_user();
2244         $this->getDataGenerator()->enrol_user($user->id, $course->id);
2246         $options = array('course' => $course->id, 'forcesubscribe' => FORUM_INITIALSUBSCRIBE);
2247         $forum = $this->getDataGenerator()->create_module('forum', $options);
2249         // Add a discussion.
2250         $record = array();
2251         $record['course'] = $course->id;
2252         $record['forum'] = $forum->id;
2253         $record['userid'] = $user->id;
2254         $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
2256         // Add a post.
2257         $record = array();
2258         $record['discussion'] = $discussion->id;
2259         $record['userid'] = $user->id;
2260         $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
2262         // The user is not subscribed to the forum. Insert a new discussion subscription.
2263         $subscription = new \stdClass();
2264         $subscription->userid  = $user->id;
2265         $subscription->forum = $forum->id;
2266         $subscription->discussion = $discussion->id;
2267         $subscription->preference = \mod_forum\subscriptions::FORUM_DISCUSSION_UNSUBSCRIBED;
2269         $subscription->id = $DB->insert_record('forum_discussion_subs', $subscription);
2271         $context = context_module::instance($forum->cmid);
2273         $params = array(
2274             'context' => $context,
2275             'objectid' => $subscription->id,
2276             'relateduserid' => $user->id,
2277             'other' => array(
2278                 'forumid' => $forum->id,
2279                 'discussion' => $discussion->id,
2280             )
2281         );
2283         $event = \mod_forum\event\discussion_subscription_deleted::create($params);
2285         // Trigger and capturing the event.
2286         $sink = $this->redirectEvents();
2287         $event->trigger();
2288         $events = $sink->get_events();
2289         $this->assertCount(1, $events);
2290         $event = reset($events);
2292         // Without an invalid context.
2293         $params['context'] = \context_course::instance($course->id);
2294         $this->setExpectedException('coding_exception', 'Context level must be CONTEXT_MODULE.');
2295         \mod_forum\event\discussion_deleted::create($params);
2297         // Without the discussion.
2298         unset($params['discussion']);
2299         $this->setExpectedException('coding_exception', 'The \'discussion\' value must be set in other.');
2300         \mod_forum\event\discussion_deleted::create($params);
2302         // Without the forumid.
2303         unset($params['forumid']);
2304         $this->setExpectedException('coding_exception', 'The \'forumid\' value must be set in other.');
2305         \mod_forum\event\discussion_deleted::create($params);
2307         // Without the relateduserid.
2308         unset($params['relateduserid']);
2309         $this->setExpectedException('coding_exception', 'The \'relateduserid\' value must be set in other.');
2310         \mod_forum\event\discussion_deleted::create($params);
2311     }
2313     /**
2314      * Test contextlevel validation of discussion_subscription_deleted event.
2315      */
2316     public function test_discussion_subscription_deleted_validation_contextlevel() {
2317         global $CFG, $DB;
2318         require_once($CFG->dirroot . '/mod/forum/lib.php');
2320         // Setup test data.
2321         $course = $this->getDataGenerator()->create_course();
2322         $user = $this->getDataGenerator()->create_user();
2323         $this->getDataGenerator()->enrol_user($user->id, $course->id);
2325         $options = array('course' => $course->id, 'forcesubscribe' => FORUM_CHOOSESUBSCRIBE);
2326         $forum = $this->getDataGenerator()->create_module('forum', $options);
2328         // Add a discussion.
2329         $record = array();
2330         $record['course'] = $course->id;
2331         $record['forum'] = $forum->id;
2332         $record['userid'] = $user->id;
2333         $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
2335         // Add a post.
2336         $record = array();
2337         $record['discussion'] = $discussion->id;
2338         $record['userid'] = $user->id;
2339         $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
2341         // The user is not subscribed to the forum. Insert a new discussion subscription.
2342         $subscription = new \stdClass();
2343         $subscription->userid  = $user->id;
2344         $subscription->forum = $forum->id;
2345         $subscription->discussion = $discussion->id;
2346         $subscription->preference = time();
2348         $subscription->id = $DB->insert_record('forum_discussion_subs', $subscription);
2350         $context = context_module::instance($forum->cmid);
2352         $params = array(
2353             'context' => \context_course::instance($course->id),
2354             'objectid' => $subscription->id,
2355             'relateduserid' => $user->id,
2356             'other' => array(
2357                 'forumid' => $forum->id,
2358                 'discussion' => $discussion->id,
2359             )
2360         );
2362         // Without an invalid context.
2363         $this->setExpectedException('coding_exception', 'Context level must be CONTEXT_MODULE.');
2364         \mod_forum\event\discussion_subscription_deleted::create($params);
2365     }
2367     /**
2368      * Test discussion validation of discussion_subscription_deleted event.
2369      */
2370     public function test_discussion_subscription_deleted_validation_discussion() {
2371         global $CFG, $DB;
2372         require_once($CFG->dirroot . '/mod/forum/lib.php');
2374         // Setup test data.
2375         $course = $this->getDataGenerator()->create_course();
2376         $user = $this->getDataGenerator()->create_user();
2377         $this->getDataGenerator()->enrol_user($user->id, $course->id);
2379         $options = array('course' => $course->id, 'forcesubscribe' => FORUM_CHOOSESUBSCRIBE);
2380         $forum = $this->getDataGenerator()->create_module('forum', $options);
2382         // Add a discussion.
2383         $record = array();
2384         $record['course'] = $course->id;
2385         $record['forum'] = $forum->id;
2386         $record['userid'] = $user->id;
2387         $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
2389         // Add a post.
2390         $record = array();
2391         $record['discussion'] = $discussion->id;
2392         $record['userid'] = $user->id;
2393         $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
2395         // The user is not subscribed to the forum. Insert a new discussion subscription.
2396         $subscription = new \stdClass();
2397         $subscription->userid  = $user->id;
2398         $subscription->forum = $forum->id;
2399         $subscription->discussion = $discussion->id;
2400         $subscription->preference = time();
2402         $subscription->id = $DB->insert_record('forum_discussion_subs', $subscription);
2404         // Without the discussion.
2405         $params = array(
2406             'context' => context_module::instance($forum->cmid),
2407             'objectid' => $subscription->id,
2408             'relateduserid' => $user->id,
2409             'other' => array(
2410                 'forumid' => $forum->id,
2411             )
2412         );
2414         $this->setExpectedException('coding_exception', "The 'discussion' value must be set in other.");
2415         \mod_forum\event\discussion_subscription_deleted::create($params);
2416     }
2418     /**
2419      * Test forumid validation of discussion_subscription_deleted event.
2420      */
2421     public function test_discussion_subscription_deleted_validation_forumid() {
2422         global $CFG, $DB;
2423         require_once($CFG->dirroot . '/mod/forum/lib.php');
2425         // Setup test data.
2426         $course = $this->getDataGenerator()->create_course();
2427         $user = $this->getDataGenerator()->create_user();
2428         $this->getDataGenerator()->enrol_user($user->id, $course->id);
2430         $options = array('course' => $course->id, 'forcesubscribe' => FORUM_CHOOSESUBSCRIBE);
2431         $forum = $this->getDataGenerator()->create_module('forum', $options);
2433         // Add a discussion.
2434         $record = array();
2435         $record['course'] = $course->id;
2436         $record['forum'] = $forum->id;
2437         $record['userid'] = $user->id;
2438         $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
2440         // Add a post.
2441         $record = array();
2442         $record['discussion'] = $discussion->id;
2443         $record['userid'] = $user->id;
2444         $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
2446         // The user is not subscribed to the forum. Insert a new discussion subscription.
2447         $subscription = new \stdClass();
2448         $subscription->userid  = $user->id;
2449         $subscription->forum = $forum->id;
2450         $subscription->discussion = $discussion->id;
2451         $subscription->preference = time();
2453         $subscription->id = $DB->insert_record('forum_discussion_subs', $subscription);
2455         // Without the forumid.
2456         $params = array(
2457             'context' => context_module::instance($forum->cmid),
2458             'objectid' => $subscription->id,
2459             'relateduserid' => $user->id,
2460             'other' => array(
2461                 'discussion' => $discussion->id,
2462             )
2463         );
2465         $this->setExpectedException('coding_exception', "The 'forumid' value must be set in other.");
2466         \mod_forum\event\discussion_subscription_deleted::create($params);
2467     }
2469     /**
2470      * Test relateduserid validation of discussion_subscription_deleted event.
2471      */
2472     public function test_discussion_subscription_deleted_validation_relateduserid() {
2473         global $CFG, $DB;
2474         require_once($CFG->dirroot . '/mod/forum/lib.php');
2476         // Setup test data.
2477         $course = $this->getDataGenerator()->create_course();
2478         $user = $this->getDataGenerator()->create_user();
2479         $this->getDataGenerator()->enrol_user($user->id, $course->id);
2481         $options = array('course' => $course->id, 'forcesubscribe' => FORUM_CHOOSESUBSCRIBE);
2482         $forum = $this->getDataGenerator()->create_module('forum', $options);
2484         // Add a discussion.
2485         $record = array();
2486         $record['course'] = $course->id;
2487         $record['forum'] = $forum->id;
2488         $record['userid'] = $user->id;
2489         $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
2491         // Add a post.
2492         $record = array();
2493         $record['discussion'] = $discussion->id;
2494         $record['userid'] = $user->id;
2495         $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
2497         // The user is not subscribed to the forum. Insert a new discussion subscription.
2498         $subscription = new \stdClass();
2499         $subscription->userid  = $user->id;
2500         $subscription->forum = $forum->id;
2501         $subscription->discussion = $discussion->id;
2502         $subscription->preference = time();
2504         $subscription->id = $DB->insert_record('forum_discussion_subs', $subscription);
2506         $context = context_module::instance($forum->cmid);
2508         // Without the relateduserid.
2509         $params = array(
2510             'context' => context_module::instance($forum->cmid),
2511             'objectid' => $subscription->id,
2512             'other' => array(
2513                 'forumid' => $forum->id,
2514                 'discussion' => $discussion->id,
2515             )
2516         );
2518         $this->setExpectedException('coding_exception', "The 'relateduserid' must be set.");
2519         \mod_forum\event\discussion_subscription_deleted::create($params);
2520     }
2522     /**
2523      * Test that the correct context is used in the events when subscribing
2524      * users.
2525      */
2526     public function test_forum_subscription_page_context_valid() {
2527         global $CFG, $PAGE;
2528         require_once($CFG->dirroot . '/mod/forum/lib.php');
2530         // Setup test data.
2531         $course = $this->getDataGenerator()->create_course();
2532         $user = $this->getDataGenerator()->create_user();
2533         $this->getDataGenerator()->enrol_user($user->id, $course->id);
2535         $options = array('course' => $course->id, 'forcesubscribe' => FORUM_CHOOSESUBSCRIBE);
2536         $forum = $this->getDataGenerator()->create_module('forum', $options);
2537         $quiz = $this->getDataGenerator()->create_module('quiz', $options);
2539         // Add a discussion.
2540         $record = array();
2541         $record['course'] = $course->id;
2542         $record['forum'] = $forum->id;
2543         $record['userid'] = $user->id;
2544         $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
2546         // Add a post.
2547         $record = array();
2548         $record['discussion'] = $discussion->id;
2549         $record['userid'] = $user->id;
2550         $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
2552         // Set up the default page event to use this forum.
2553         $PAGE = new moodle_page();
2554         $cm = get_coursemodule_from_instance('forum', $discussion->forum);
2555         $context = \context_module::instance($cm->id);
2556         $PAGE->set_context($context);
2557         $PAGE->set_cm($cm, $course, $forum);
2559         // Trigger and capturing the event.
2560         $sink = $this->redirectEvents();
2562         // Trigger the event by subscribing the user to the forum.
2563         \mod_forum\subscriptions::subscribe_user($user->id, $forum);
2565         $events = $sink->get_events();
2566         $sink->clear();
2567         $this->assertCount(1, $events);
2568         $event = reset($events);
2570         // Checking that the event contains the expected values.
2571         $this->assertInstanceOf('\mod_forum\event\subscription_created', $event);
2572         $this->assertEquals($context, $event->get_context());
2574         // Trigger the event by unsubscribing the user to the forum.
2575         \mod_forum\subscriptions::unsubscribe_user($user->id, $forum);
2577         $events = $sink->get_events();
2578         $sink->clear();
2579         $this->assertCount(1, $events);
2580         $event = reset($events);
2582         // Checking that the event contains the expected values.
2583         $this->assertInstanceOf('\mod_forum\event\subscription_deleted', $event);
2584         $this->assertEquals($context, $event->get_context());
2586         // Trigger the event by subscribing the user to the discussion.
2587         \mod_forum\subscriptions::subscribe_user_to_discussion($user->id, $discussion);
2589         $events = $sink->get_events();
2590         $sink->clear();
2591         $this->assertCount(1, $events);
2592         $event = reset($events);
2594         // Checking that the event contains the expected values.
2595         $this->assertInstanceOf('\mod_forum\event\discussion_subscription_created', $event);
2596         $this->assertEquals($context, $event->get_context());
2598         // Trigger the event by unsubscribing the user from the discussion.
2599         \mod_forum\subscriptions::unsubscribe_user_from_discussion($user->id, $discussion);
2601         $events = $sink->get_events();
2602         $sink->clear();
2603         $this->assertCount(1, $events);
2604         $event = reset($events);
2606         // Checking that the event contains the expected values.
2607         $this->assertInstanceOf('\mod_forum\event\discussion_subscription_deleted', $event);
2608         $this->assertEquals($context, $event->get_context());
2610         // Now try with the context for a different module (quiz).
2611         $PAGE = new moodle_page();
2612         $cm = get_coursemodule_from_instance('quiz', $quiz->id);
2613         $quizcontext = \context_module::instance($cm->id);
2614         $PAGE->set_context($quizcontext);
2615         $PAGE->set_cm($cm, $course, $quiz);
2617         // Trigger and capturing the event.
2618         $sink = $this->redirectEvents();
2620         // Trigger the event by subscribing the user to the forum.
2621         \mod_forum\subscriptions::subscribe_user($user->id, $forum);
2623         $events = $sink->get_events();
2624         $sink->clear();
2625         $this->assertCount(1, $events);
2626         $event = reset($events);
2628         // Checking that the event contains the expected values.
2629         $this->assertInstanceOf('\mod_forum\event\subscription_created', $event);
2630         $this->assertEquals($context, $event->get_context());
2632         // Trigger the event by unsubscribing the user to the forum.
2633         \mod_forum\subscriptions::unsubscribe_user($user->id, $forum);
2635         $events = $sink->get_events();
2636         $sink->clear();
2637         $this->assertCount(1, $events);
2638         $event = reset($events);
2640         // Checking that the event contains the expected values.
2641         $this->assertInstanceOf('\mod_forum\event\subscription_deleted', $event);
2642         $this->assertEquals($context, $event->get_context());
2644         // Trigger the event by subscribing the user to the discussion.
2645         \mod_forum\subscriptions::subscribe_user_to_discussion($user->id, $discussion);
2647         $events = $sink->get_events();
2648         $sink->clear();
2649         $this->assertCount(1, $events);
2650         $event = reset($events);
2652         // Checking that the event contains the expected values.
2653         $this->assertInstanceOf('\mod_forum\event\discussion_subscription_created', $event);
2654         $this->assertEquals($context, $event->get_context());
2656         // Trigger the event by unsubscribing the user from the discussion.
2657         \mod_forum\subscriptions::unsubscribe_user_from_discussion($user->id, $discussion);
2659         $events = $sink->get_events();
2660         $sink->clear();
2661         $this->assertCount(1, $events);
2662         $event = reset($events);
2664         // Checking that the event contains the expected values.
2665         $this->assertInstanceOf('\mod_forum\event\discussion_subscription_deleted', $event);
2666         $this->assertEquals($context, $event->get_context());
2668         // Now try with the course context - the module context should still be used.
2669         $PAGE = new moodle_page();
2670         $coursecontext = \context_course::instance($course->id);
2671         $PAGE->set_context($coursecontext);
2673         // Trigger the event by subscribing the user to the forum.
2674         \mod_forum\subscriptions::subscribe_user($user->id, $forum);
2676         $events = $sink->get_events();
2677         $sink->clear();
2678         $this->assertCount(1, $events);
2679         $event = reset($events);
2681         // Checking that the event contains the expected values.
2682         $this->assertInstanceOf('\mod_forum\event\subscription_created', $event);
2683         $this->assertEquals($context, $event->get_context());
2685         // Trigger the event by unsubscribing the user to the forum.
2686         \mod_forum\subscriptions::unsubscribe_user($user->id, $forum);
2688         $events = $sink->get_events();
2689         $sink->clear();
2690         $this->assertCount(1, $events);
2691         $event = reset($events);
2693         // Checking that the event contains the expected values.
2694         $this->assertInstanceOf('\mod_forum\event\subscription_deleted', $event);
2695         $this->assertEquals($context, $event->get_context());
2697         // Trigger the event by subscribing the user to the discussion.
2698         \mod_forum\subscriptions::subscribe_user_to_discussion($user->id, $discussion);
2700         $events = $sink->get_events();
2701         $sink->clear();
2702         $this->assertCount(1, $events);
2703         $event = reset($events);
2705         // Checking that the event contains the expected values.
2706         $this->assertInstanceOf('\mod_forum\event\discussion_subscription_created', $event);
2707         $this->assertEquals($context, $event->get_context());
2709         // Trigger the event by unsubscribing the user from the discussion.
2710         \mod_forum\subscriptions::unsubscribe_user_from_discussion($user->id, $discussion);
2712         $events = $sink->get_events();
2713         $sink->clear();
2714         $this->assertCount(1, $events);
2715         $event = reset($events);
2717         // Checking that the event contains the expected values.
2718         $this->assertInstanceOf('\mod_forum\event\discussion_subscription_deleted', $event);
2719         $this->assertEquals($context, $event->get_context());
2721     }
2723     /**
2724      * Test mod_forum_observer methods.
2725      */
2726     public function test_observers() {
2727         global $DB, $CFG;
2729         require_once($CFG->dirroot . '/mod/forum/lib.php');
2731         $forumgen = $this->getDataGenerator()->get_plugin_generator('mod_forum');
2733         $course = $this->getDataGenerator()->create_course();
2734         $trackedrecord = array('course' => $course->id, 'type' => 'general', 'forcesubscribe' => FORUM_INITIALSUBSCRIBE);
2735         $untrackedrecord = array('course' => $course->id, 'type' => 'general');
2736         $trackedforum = $this->getDataGenerator()->create_module('forum', $trackedrecord);
2737         $untrackedforum = $this->getDataGenerator()->create_module('forum', $untrackedrecord);
2739         // Used functions don't require these settings; adding
2740         // them just in case there are APIs changes in future.
2741         $user = $this->getDataGenerator()->create_user(array(
2742             'maildigest' => 1,
2743             'trackforums' => 1
2744         ));
2746         $manplugin = enrol_get_plugin('manual');
2747         $manualenrol = $DB->get_record('enrol', array('courseid' => $course->id, 'enrol' => 'manual'));
2748         $student = $DB->get_record('role', array('shortname' => 'student'));
2750         // The role_assign observer does it's job adding the forum_subscriptions record.
2751         $manplugin->enrol_user($manualenrol, $user->id, $student->id);
2753         // They are not required, but in a real environment they are supposed to be required;
2754         // adding them just in case there are APIs changes in future.
2755         set_config('forum_trackingtype', 1);
2756         set_config('forum_trackreadposts', 1);
2758         $record = array();
2759         $record['course'] = $course->id;
2760         $record['forum'] = $trackedforum->id;
2761         $record['userid'] = $user->id;
2762         $discussion = $forumgen->create_discussion($record);
2764         $record = array();
2765         $record['discussion'] = $discussion->id;
2766         $record['userid'] = $user->id;
2767         $post = $forumgen->create_post($record);
2769         forum_tp_add_read_record($user->id, $post->id);
2770         forum_set_user_maildigest($trackedforum, 2, $user);
2771         forum_tp_stop_tracking($untrackedforum->id, $user->id);
2773         $this->assertEquals(1, $DB->count_records('forum_subscriptions'));
2774         $this->assertEquals(1, $DB->count_records('forum_digests'));
2775         $this->assertEquals(1, $DB->count_records('forum_track_prefs'));
2776         $this->assertEquals(1, $DB->count_records('forum_read'));
2778         // The course_module_created observer does it's job adding a subscription.
2779         $forumrecord = array('course' => $course->id, 'type' => 'general', 'forcesubscribe' => FORUM_INITIALSUBSCRIBE);
2780         $extraforum = $this->getDataGenerator()->create_module('forum', $forumrecord);
2781         $this->assertEquals(2, $DB->count_records('forum_subscriptions'));
2783         $manplugin->unenrol_user($manualenrol, $user->id);
2785         $this->assertEquals(0, $DB->count_records('forum_digests'));
2786         $this->assertEquals(0, $DB->count_records('forum_subscriptions'));
2787         $this->assertEquals(0, $DB->count_records('forum_track_prefs'));
2788         $this->assertEquals(0, $DB->count_records('forum_read'));
2789     }