MDL-45381 mod_forum: changed event URL for subscription created/deleted events
[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/view.php', array('f' => $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/view.php', array('f' => $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', 'Context level must be either CONTEXT_SYSTEM or CONTEXT_COURSE.');
1044         \mod_forum\event\user_report_viewed::create($params);
1045     }
1047     /**
1048      *  Ensure user_report_viewed event validates that the relateduserid is set.
1049      */
1050     public function test_user_report_viewed_relateduserid_validation() {
1052         $params = array(
1053             'context' => context_system::instance(),
1054             'other' => array('reportmode' => 'posts'),
1055         );
1057         $this->setExpectedException('coding_exception', 'The \'relateduserid\' must be set.');
1058         \mod_forum\event\user_report_viewed::create($params);
1059     }
1061     /**
1062      * Test the user_report_viewed event.
1063      */
1064     public function test_user_report_viewed() {
1065         // Setup test data.
1066         $user = $this->getDataGenerator()->create_user();
1067         $course = $this->getDataGenerator()->create_course();
1068         $context = context_course::instance($course->id);
1070         $params = array(
1071             'context' => $context,
1072             'relateduserid' => $user->id,
1073             'other' => array('reportmode' => 'discussions'),
1074         );
1076         $event = \mod_forum\event\user_report_viewed::create($params);
1078         // Trigger and capture the event.
1079         $sink = $this->redirectEvents();
1080         $event->trigger();
1081         $events = $sink->get_events();
1082         $this->assertCount(1, $events);
1083         $event = reset($events);
1085         // Checking that the event contains the expected values.
1086         $this->assertInstanceOf('\mod_forum\event\user_report_viewed', $event);
1087         $this->assertEquals($context, $event->get_context());
1088         $expected = array($course->id, 'forum', 'user report',
1089             "user.php?id={$user->id}&amp;mode=discussions&amp;course={$course->id}", $user->id);
1090         $this->assertEventLegacyLogData($expected, $event);
1091         $this->assertEventContextNotUsed($event);
1093         $this->assertNotEmpty($event->get_name());
1094     }
1096     /**
1097      *  Ensure post_created event validates that the postid is set.
1098      */
1099     public function test_post_created_postid_validation() {
1100         $course = $this->getDataGenerator()->create_course();
1101         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
1102         $user = $this->getDataGenerator()->create_user();
1104         // Add a discussion.
1105         $record = array();
1106         $record['course'] = $course->id;
1107         $record['forum'] = $forum->id;
1108         $record['userid'] = $user->id;
1109         $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1111         $params = array(
1112             'context' => context_module::instance($forum->cmid),
1113             'other' => array('forumid' => $forum->id, 'forumtype' => $forum->type, 'discussionid' => $discussion->id)
1114         );
1116         \mod_forum\event\post_created::create($params);
1117     }
1119     /**
1120      *  Ensure post_created event validates that the discussionid is set.
1121      */
1122     public function test_post_created_discussionid_validation() {
1123         $course = $this->getDataGenerator()->create_course();
1124         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
1125         $user = $this->getDataGenerator()->create_user();
1127         // Add a discussion.
1128         $record = array();
1129         $record['course'] = $course->id;
1130         $record['forum'] = $forum->id;
1131         $record['userid'] = $user->id;
1132         $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1134         // Add a post.
1135         $record = array();
1136         $record['discussion'] = $discussion->id;
1137         $record['userid'] = $user->id;
1138         $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
1140         $params = array(
1141             'context' => context_module::instance($forum->cmid),
1142             'objectid' => $post->id,
1143             'other' => array('forumid' => $forum->id, 'forumtype' => $forum->type)
1144         );
1146         $this->setExpectedException('coding_exception', 'The \'discussionid\' value must be set in other.');
1147         \mod_forum\event\post_created::create($params);
1148     }
1150     /**
1151      *  Ensure post_created event validates that the forumid is set.
1152      */
1153     public function test_post_created_forumid_validation() {
1154         $course = $this->getDataGenerator()->create_course();
1155         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
1156         $user = $this->getDataGenerator()->create_user();
1158         // Add a discussion.
1159         $record = array();
1160         $record['course'] = $course->id;
1161         $record['forum'] = $forum->id;
1162         $record['userid'] = $user->id;
1163         $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1165         // Add a post.
1166         $record = array();
1167         $record['discussion'] = $discussion->id;
1168         $record['userid'] = $user->id;
1169         $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
1171         $params = array(
1172             'context' => context_module::instance($forum->cmid),
1173             'objectid' => $post->id,
1174             'other' => array('discussionid' => $discussion->id, 'forumtype' => $forum->type)
1175         );
1177         $this->setExpectedException('coding_exception', 'The \'forumid\' value must be set in other.');
1178         \mod_forum\event\post_created::create($params);
1179     }
1181     /**
1182      *  Ensure post_created event validates that the forumtype is set.
1183      */
1184     public function test_post_created_forumtype_validation() {
1185         $course = $this->getDataGenerator()->create_course();
1186         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
1187         $user = $this->getDataGenerator()->create_user();
1189         // Add a discussion.
1190         $record = array();
1191         $record['course'] = $course->id;
1192         $record['forum'] = $forum->id;
1193         $record['userid'] = $user->id;
1194         $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1196         // Add a post.
1197         $record = array();
1198         $record['discussion'] = $discussion->id;
1199         $record['userid'] = $user->id;
1200         $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
1202         $params = array(
1203             'context' => context_module::instance($forum->cmid),
1204             'objectid' => $post->id,
1205             'other' => array('discussionid' => $discussion->id, 'forumid' => $forum->id)
1206         );
1208         $this->setExpectedException('coding_exception', 'The \'forumtype\' value must be set in other.');
1209         \mod_forum\event\post_created::create($params);
1210     }
1212     /**
1213      *  Ensure post_created event validates that the contextlevel is correct.
1214      */
1215     public function test_post_created_context_validation() {
1216         $course = $this->getDataGenerator()->create_course();
1217         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
1218         $user = $this->getDataGenerator()->create_user();
1220         // Add a discussion.
1221         $record = array();
1222         $record['course'] = $course->id;
1223         $record['forum'] = $forum->id;
1224         $record['userid'] = $user->id;
1225         $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1227         // Add a post.
1228         $record = array();
1229         $record['discussion'] = $discussion->id;
1230         $record['userid'] = $user->id;
1231         $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
1233         $params = array(
1234             'context' => context_system::instance(),
1235             'objectid' => $post->id,
1236             'other' => array('discussionid' => $discussion->id, 'forumid' => $forum->id, 'forumtype' => $forum->type)
1237         );
1239         $this->setExpectedException('coding_exception', 'Context level must be CONTEXT_MODULE');
1240         \mod_forum\event\post_created::create($params);
1241     }
1243     /**
1244      * Test the post_created event.
1245      */
1246     public function test_post_created() {
1247         // Setup test data.
1248         $course = $this->getDataGenerator()->create_course();
1249         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
1250         $user = $this->getDataGenerator()->create_user();
1252         // Add a discussion.
1253         $record = array();
1254         $record['course'] = $course->id;
1255         $record['forum'] = $forum->id;
1256         $record['userid'] = $user->id;
1257         $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1259         // Add a post.
1260         $record = array();
1261         $record['discussion'] = $discussion->id;
1262         $record['userid'] = $user->id;
1263         $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
1265         $context = context_module::instance($forum->cmid);
1267         $params = array(
1268             'context' => $context,
1269             'objectid' => $post->id,
1270             'other' => array('discussionid' => $discussion->id, 'forumid' => $forum->id, 'forumtype' => $forum->type)
1271         );
1273         $event = \mod_forum\event\post_created::create($params);
1275         // Trigger and capturing the event.
1276         $sink = $this->redirectEvents();
1277         $event->trigger();
1278         $events = $sink->get_events();
1279         $this->assertCount(1, $events);
1280         $event = reset($events);
1282         // Checking that the event contains the expected values.
1283         $this->assertInstanceOf('\mod_forum\event\post_created', $event);
1284         $this->assertEquals($context, $event->get_context());
1285         $expected = array($course->id, 'forum', 'add post', "discuss.php?d={$discussion->id}#p{$post->id}",
1286             $forum->id, $forum->cmid);
1287         $this->assertEventLegacyLogData($expected, $event);
1288         $url = new \moodle_url('/mod/forum/discuss.php', array('d' => $discussion->id));
1289         $url->set_anchor('p'.$event->objectid);
1290         $this->assertEquals($url, $event->get_url());
1291         $this->assertEventContextNotUsed($event);
1293         $this->assertNotEmpty($event->get_name());
1294     }
1296     /**
1297      * Test the post_created event for a single discussion forum.
1298      */
1299     public function test_post_created_single() {
1300         // Setup test data.
1301         $course = $this->getDataGenerator()->create_course();
1302         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id, 'type' => 'single'));
1303         $user = $this->getDataGenerator()->create_user();
1305         // Add a discussion.
1306         $record = array();
1307         $record['course'] = $course->id;
1308         $record['forum'] = $forum->id;
1309         $record['userid'] = $user->id;
1310         $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1312         // Add a post.
1313         $record = array();
1314         $record['discussion'] = $discussion->id;
1315         $record['userid'] = $user->id;
1316         $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
1318         $context = context_module::instance($forum->cmid);
1320         $params = array(
1321             'context' => $context,
1322             'objectid' => $post->id,
1323             'other' => array('discussionid' => $discussion->id, 'forumid' => $forum->id, 'forumtype' => $forum->type)
1324         );
1326         $event = \mod_forum\event\post_created::create($params);
1328         // Trigger and capturing the event.
1329         $sink = $this->redirectEvents();
1330         $event->trigger();
1331         $events = $sink->get_events();
1332         $this->assertCount(1, $events);
1333         $event = reset($events);
1335         // Checking that the event contains the expected values.
1336         $this->assertInstanceOf('\mod_forum\event\post_created', $event);
1337         $this->assertEquals($context, $event->get_context());
1338         $expected = array($course->id, 'forum', 'add post', "view.php?f={$forum->id}#p{$post->id}",
1339             $forum->id, $forum->cmid);
1340         $this->assertEventLegacyLogData($expected, $event);
1341         $url = new \moodle_url('/mod/forum/view.php', array('f' => $forum->id));
1342         $url->set_anchor('p'.$event->objectid);
1343         $this->assertEquals($url, $event->get_url());
1344         $this->assertEventContextNotUsed($event);
1346         $this->assertNotEmpty($event->get_name());
1347     }
1349     /**
1350      *  Ensure post_deleted event validates that the postid is set.
1351      */
1352     public function test_post_deleted_postid_validation() {
1353         $course = $this->getDataGenerator()->create_course();
1354         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
1355         $user = $this->getDataGenerator()->create_user();
1357         // Add a discussion.
1358         $record = array();
1359         $record['course'] = $course->id;
1360         $record['forum'] = $forum->id;
1361         $record['userid'] = $user->id;
1362         $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1364         $params = array(
1365             'context' => context_module::instance($forum->cmid),
1366             'other' => array('forumid' => $forum->id, 'forumtype' => $forum->type, 'discussionid' => $discussion->id)
1367         );
1369         \mod_forum\event\post_deleted::create($params);
1370     }
1372     /**
1373      *  Ensure post_deleted event validates that the discussionid is set.
1374      */
1375     public function test_post_deleted_discussionid_validation() {
1376         $course = $this->getDataGenerator()->create_course();
1377         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
1378         $user = $this->getDataGenerator()->create_user();
1380         // Add a discussion.
1381         $record = array();
1382         $record['course'] = $course->id;
1383         $record['forum'] = $forum->id;
1384         $record['userid'] = $user->id;
1385         $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1387         // Add a post.
1388         $record = array();
1389         $record['discussion'] = $discussion->id;
1390         $record['userid'] = $user->id;
1391         $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
1393         $params = array(
1394             'context' => context_module::instance($forum->cmid),
1395             'objectid' => $post->id,
1396             'other' => array('forumid' => $forum->id, 'forumtype' => $forum->type)
1397         );
1399         $this->setExpectedException('coding_exception', 'The \'discussionid\' value must be set in other.');
1400         \mod_forum\event\post_deleted::create($params);
1401     }
1403     /**
1404      *  Ensure post_deleted event validates that the forumid is set.
1405      */
1406     public function test_post_deleted_forumid_validation() {
1407         $course = $this->getDataGenerator()->create_course();
1408         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
1409         $user = $this->getDataGenerator()->create_user();
1411         // Add a discussion.
1412         $record = array();
1413         $record['course'] = $course->id;
1414         $record['forum'] = $forum->id;
1415         $record['userid'] = $user->id;
1416         $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1418         // Add a post.
1419         $record = array();
1420         $record['discussion'] = $discussion->id;
1421         $record['userid'] = $user->id;
1422         $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
1424         $params = array(
1425             'context' => context_module::instance($forum->cmid),
1426             'objectid' => $post->id,
1427             'other' => array('discussionid' => $discussion->id, 'forumtype' => $forum->type)
1428         );
1430         $this->setExpectedException('coding_exception', 'The \'forumid\' value must be set in other.');
1431         \mod_forum\event\post_deleted::create($params);
1432     }
1434     /**
1435      *  Ensure post_deleted event validates that the forumtype is set.
1436      */
1437     public function test_post_deleted_forumtype_validation() {
1438         $course = $this->getDataGenerator()->create_course();
1439         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
1440         $user = $this->getDataGenerator()->create_user();
1442         // Add a discussion.
1443         $record = array();
1444         $record['course'] = $course->id;
1445         $record['forum'] = $forum->id;
1446         $record['userid'] = $user->id;
1447         $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1449         // Add a post.
1450         $record = array();
1451         $record['discussion'] = $discussion->id;
1452         $record['userid'] = $user->id;
1453         $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
1455         $params = array(
1456             'context' => context_module::instance($forum->cmid),
1457             'objectid' => $post->id,
1458             'other' => array('discussionid' => $discussion->id, 'forumid' => $forum->id)
1459         );
1461         $this->setExpectedException('coding_exception', 'The \'forumtype\' value must be set in other.');
1462         \mod_forum\event\post_deleted::create($params);
1463     }
1465     /**
1466      *  Ensure post_deleted event validates that the contextlevel is correct.
1467      */
1468     public function test_post_deleted_context_validation() {
1469         $course = $this->getDataGenerator()->create_course();
1470         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
1471         $user = $this->getDataGenerator()->create_user();
1473         // Add a discussion.
1474         $record = array();
1475         $record['course'] = $course->id;
1476         $record['forum'] = $forum->id;
1477         $record['userid'] = $user->id;
1478         $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1480         // Add a post.
1481         $record = array();
1482         $record['discussion'] = $discussion->id;
1483         $record['userid'] = $user->id;
1484         $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
1486         $params = array(
1487             'context' => context_system::instance(),
1488             'objectid' => $post->id,
1489             'other' => array('discussionid' => $discussion->id, 'forumid' => $forum->id, 'forumtype' => $forum->type)
1490         );
1492         $this->setExpectedException('coding_exception', 'Context level must be CONTEXT_MODULE');
1493         \mod_forum\event\post_deleted::create($params);
1494     }
1496     /**
1497      * Test post_deleted event.
1498      */
1499     public function test_post_deleted() {
1500         // Setup test data.
1501         $course = $this->getDataGenerator()->create_course();
1502         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
1503         $user = $this->getDataGenerator()->create_user();
1505         // Add a discussion.
1506         $record = array();
1507         $record['course'] = $course->id;
1508         $record['forum'] = $forum->id;
1509         $record['userid'] = $user->id;
1510         $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1512         // Add a post.
1513         $record = array();
1514         $record['discussion'] = $discussion->id;
1515         $record['userid'] = $user->id;
1516         $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
1518         $context = context_module::instance($forum->cmid);
1520         $params = array(
1521             'context' => $context,
1522             'objectid' => $post->id,
1523             'other' => array('discussionid' => $discussion->id, 'forumid' => $forum->id, 'forumtype' => $forum->type)
1524         );
1526         $event = \mod_forum\event\post_deleted::create($params);
1528         // Trigger and capture the event.
1529         $sink = $this->redirectEvents();
1530         $event->trigger();
1531         $events = $sink->get_events();
1532         $this->assertCount(1, $events);
1533         $event = reset($events);
1535         // Checking that the event contains the expected values.
1536         $this->assertInstanceOf('\mod_forum\event\post_deleted', $event);
1537         $this->assertEquals($context, $event->get_context());
1538         $expected = array($course->id, 'forum', 'delete post', "discuss.php?d={$discussion->id}", $post->id, $forum->cmid);
1539         $this->assertEventLegacyLogData($expected, $event);
1540         $url = new \moodle_url('/mod/forum/discuss.php', array('d' => $discussion->id));
1541         $this->assertEquals($url, $event->get_url());
1542         $this->assertEventContextNotUsed($event);
1544         $this->assertNotEmpty($event->get_name());
1545     }
1547     /**
1548      * Test post_deleted event for a single discussion forum.
1549      */
1550     public function test_post_deleted_single() {
1551         // Setup test data.
1552         $course = $this->getDataGenerator()->create_course();
1553         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id, 'type' => 'single'));
1554         $user = $this->getDataGenerator()->create_user();
1556         // Add a discussion.
1557         $record = array();
1558         $record['course'] = $course->id;
1559         $record['forum'] = $forum->id;
1560         $record['userid'] = $user->id;
1561         $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1563         // Add a post.
1564         $record = array();
1565         $record['discussion'] = $discussion->id;
1566         $record['userid'] = $user->id;
1567         $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
1569         $context = context_module::instance($forum->cmid);
1571         $params = array(
1572             'context' => $context,
1573             'objectid' => $post->id,
1574             'other' => array('discussionid' => $discussion->id, 'forumid' => $forum->id, 'forumtype' => $forum->type)
1575         );
1577         $event = \mod_forum\event\post_deleted::create($params);
1579         // Trigger and capture the event.
1580         $sink = $this->redirectEvents();
1581         $event->trigger();
1582         $events = $sink->get_events();
1583         $this->assertCount(1, $events);
1584         $event = reset($events);
1586         // Checking that the event contains the expected values.
1587         $this->assertInstanceOf('\mod_forum\event\post_deleted', $event);
1588         $this->assertEquals($context, $event->get_context());
1589         $expected = array($course->id, 'forum', 'delete post', "view.php?f={$forum->id}", $post->id, $forum->cmid);
1590         $this->assertEventLegacyLogData($expected, $event);
1591         $url = new \moodle_url('/mod/forum/view.php', array('f' => $forum->id));
1592         $this->assertEquals($url, $event->get_url());
1593         $this->assertEventContextNotUsed($event);
1595         $this->assertNotEmpty($event->get_name());
1596     }
1598     /**
1599      *  Ensure post_updated event validates that the discussionid is set.
1600      */
1601     public function test_post_updated_discussionid_validation() {
1602         $course = $this->getDataGenerator()->create_course();
1603         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
1604         $user = $this->getDataGenerator()->create_user();
1606         // Add a discussion.
1607         $record = array();
1608         $record['course'] = $course->id;
1609         $record['forum'] = $forum->id;
1610         $record['userid'] = $user->id;
1611         $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1613         // Add a post.
1614         $record = array();
1615         $record['discussion'] = $discussion->id;
1616         $record['userid'] = $user->id;
1617         $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
1619         $params = array(
1620             'context' => context_module::instance($forum->cmid),
1621             'objectid' => $post->id,
1622             'other' => array('forumid' => $forum->id, 'forumtype' => $forum->type)
1623         );
1625         $this->setExpectedException('coding_exception', 'The \'discussionid\' value must be set in other.');
1626         \mod_forum\event\post_updated::create($params);
1627     }
1629     /**
1630      *  Ensure post_updated event validates that the forumid is set.
1631      */
1632     public function test_post_updated_forumid_validation() {
1633         $course = $this->getDataGenerator()->create_course();
1634         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
1635         $user = $this->getDataGenerator()->create_user();
1637         // Add a discussion.
1638         $record = array();
1639         $record['course'] = $course->id;
1640         $record['forum'] = $forum->id;
1641         $record['userid'] = $user->id;
1642         $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1644         // Add a post.
1645         $record = array();
1646         $record['discussion'] = $discussion->id;
1647         $record['userid'] = $user->id;
1648         $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
1650         $params = array(
1651             'context' => context_module::instance($forum->cmid),
1652             'objectid' => $post->id,
1653             'other' => array('discussionid' => $discussion->id, 'forumtype' => $forum->type)
1654         );
1656         $this->setExpectedException('coding_exception', 'The \'forumid\' value must be set in other.');
1657         \mod_forum\event\post_updated::create($params);
1658     }
1660     /**
1661      *  Ensure post_updated event validates that the forumtype is set.
1662      */
1663     public function test_post_updated_forumtype_validation() {
1664         $course = $this->getDataGenerator()->create_course();
1665         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
1666         $user = $this->getDataGenerator()->create_user();
1668         // Add a discussion.
1669         $record = array();
1670         $record['course'] = $course->id;
1671         $record['forum'] = $forum->id;
1672         $record['userid'] = $user->id;
1673         $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1675         // Add a post.
1676         $record = array();
1677         $record['discussion'] = $discussion->id;
1678         $record['userid'] = $user->id;
1679         $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
1681         $params = array(
1682             'context' => context_module::instance($forum->cmid),
1683             'objectid' => $post->id,
1684             'other' => array('discussionid' => $discussion->id, 'forumid' => $forum->id)
1685         );
1687         $this->setExpectedException('coding_exception', 'The \'forumtype\' value must be set in other.');
1688         \mod_forum\event\post_updated::create($params);
1689     }
1691     /**
1692      *  Ensure post_updated event validates that the contextlevel is correct.
1693      */
1694     public function test_post_updated_context_validation() {
1695         $course = $this->getDataGenerator()->create_course();
1696         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
1697         $user = $this->getDataGenerator()->create_user();
1699         // Add a discussion.
1700         $record = array();
1701         $record['course'] = $course->id;
1702         $record['forum'] = $forum->id;
1703         $record['userid'] = $user->id;
1704         $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1706         // Add a post.
1707         $record = array();
1708         $record['discussion'] = $discussion->id;
1709         $record['userid'] = $user->id;
1710         $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
1712         $params = array(
1713             'context' => context_system::instance(),
1714             'objectid' => $post->id,
1715             'other' => array('discussionid' => $discussion->id, 'forumid' => $forum->id, 'forumtype' => $forum->type)
1716         );
1718         $this->setExpectedException('coding_exception', 'Context level must be CONTEXT_MODULE');
1719         \mod_forum\event\post_updated::create($params);
1720     }
1722     /**
1723      * Test post_updated event.
1724      */
1725     public function test_post_updated() {
1726         // Setup test data.
1727         $course = $this->getDataGenerator()->create_course();
1728         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
1729         $user = $this->getDataGenerator()->create_user();
1731         // Add a discussion.
1732         $record = array();
1733         $record['course'] = $course->id;
1734         $record['forum'] = $forum->id;
1735         $record['userid'] = $user->id;
1736         $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1738         // Add a post.
1739         $record = array();
1740         $record['discussion'] = $discussion->id;
1741         $record['userid'] = $user->id;
1742         $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
1744         $context = context_module::instance($forum->cmid);
1746         $params = array(
1747             'context' => $context,
1748             'objectid' => $post->id,
1749             'other' => array('discussionid' => $discussion->id, 'forumid' => $forum->id, 'forumtype' => $forum->type)
1750         );
1752         $event = \mod_forum\event\post_updated::create($params);
1754         // Trigger and capturing the event.
1755         $sink = $this->redirectEvents();
1756         $event->trigger();
1757         $events = $sink->get_events();
1758         $this->assertCount(1, $events);
1759         $event = reset($events);
1761         // Checking that the event contains the expected values.
1762         $this->assertInstanceOf('\mod_forum\event\post_updated', $event);
1763         $this->assertEquals($context, $event->get_context());
1764         $expected = array($course->id, 'forum', 'update post', "discuss.php?d={$discussion->id}#p{$post->id}",
1765             $post->id, $forum->cmid);
1766         $this->assertEventLegacyLogData($expected, $event);
1767         $url = new \moodle_url('/mod/forum/discuss.php', array('d' => $discussion->id));
1768         $url->set_anchor('p'.$event->objectid);
1769         $this->assertEquals($url, $event->get_url());
1770         $this->assertEventContextNotUsed($event);
1772         $this->assertNotEmpty($event->get_name());
1773     }
1775     /**
1776      * Test post_updated event.
1777      */
1778     public function test_post_updated_single() {
1779         // Setup test data.
1780         $course = $this->getDataGenerator()->create_course();
1781         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id, 'type' => 'single'));
1782         $user = $this->getDataGenerator()->create_user();
1784         // Add a discussion.
1785         $record = array();
1786         $record['course'] = $course->id;
1787         $record['forum'] = $forum->id;
1788         $record['userid'] = $user->id;
1789         $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1791         // Add a post.
1792         $record = array();
1793         $record['discussion'] = $discussion->id;
1794         $record['userid'] = $user->id;
1795         $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
1797         $context = context_module::instance($forum->cmid);
1799         $params = array(
1800             'context' => $context,
1801             'objectid' => $post->id,
1802             'other' => array('discussionid' => $discussion->id, 'forumid' => $forum->id, 'forumtype' => $forum->type)
1803         );
1805         $event = \mod_forum\event\post_updated::create($params);
1807         // Trigger and capturing the event.
1808         $sink = $this->redirectEvents();
1809         $event->trigger();
1810         $events = $sink->get_events();
1811         $this->assertCount(1, $events);
1812         $event = reset($events);
1814         // Checking that the event contains the expected values.
1815         $this->assertInstanceOf('\mod_forum\event\post_updated', $event);
1816         $this->assertEquals($context, $event->get_context());
1817         $expected = array($course->id, 'forum', 'update post', "view.php?f={$forum->id}#p{$post->id}",
1818             $post->id, $forum->cmid);
1819         $this->assertEventLegacyLogData($expected, $event);
1820         $url = new \moodle_url('/mod/forum/view.php', array('f' => $forum->id));
1821         $url->set_anchor('p'.$post->id);
1822         $this->assertEquals($url, $event->get_url());
1823         $this->assertEventContextNotUsed($event);
1825         $this->assertNotEmpty($event->get_name());
1826     }
1828     /**
1829      * Test discussion_subscription_created event.
1830      */
1831     public function test_discussion_subscription_created() {
1832         global $CFG;
1833         require_once($CFG->dirroot . '/mod/forum/lib.php');
1835         // Setup test data.
1836         $course = $this->getDataGenerator()->create_course();
1837         $user = $this->getDataGenerator()->create_user();
1838         $this->getDataGenerator()->enrol_user($user->id, $course->id);
1840         $options = array('course' => $course->id, 'forcesubscribe' => FORUM_CHOOSESUBSCRIBE);
1841         $forum = $this->getDataGenerator()->create_module('forum', $options);
1843         // Add a discussion.
1844         $record = array();
1845         $record['course'] = $course->id;
1846         $record['forum'] = $forum->id;
1847         $record['userid'] = $user->id;
1848         $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1850         // Add a post.
1851         $record = array();
1852         $record['discussion'] = $discussion->id;
1853         $record['userid'] = $user->id;
1854         $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
1856         // Trigger and capturing the event.
1857         $sink = $this->redirectEvents();
1859         // Trigger the event by subscribing the user to the forum discussion.
1860         \mod_forum\subscriptions::subscribe_user_to_discussion($user->id, $discussion);
1862         $events = $sink->get_events();
1863         $this->assertCount(1, $events);
1864         $event = reset($events);
1866         // Checking that the event contains the expected values.
1867         $this->assertInstanceOf('\mod_forum\event\discussion_subscription_created', $event);
1870         $cm = get_coursemodule_from_instance('forum', $discussion->forum);
1871         $context = \context_module::instance($cm->id);
1872         $this->assertEquals($context, $event->get_context());
1874         $url = new \moodle_url('/mod/forum/subscribe.php', array(
1875             'id' => $forum->id,
1876             'd' => $discussion->id
1877         ));
1879         $this->assertEquals($url, $event->get_url());
1880         $this->assertEventContextNotUsed($event);
1881         $this->assertNotEmpty($event->get_name());
1882     }
1884     /**
1885      * Test validation of discussion_subscription_created event.
1886      */
1887     public function test_discussion_subscription_created_validation() {
1888         global $CFG, $DB;
1889         require_once($CFG->dirroot . '/mod/forum/lib.php');
1891         // Setup test data.
1892         $course = $this->getDataGenerator()->create_course();
1893         $user = $this->getDataGenerator()->create_user();
1894         $this->getDataGenerator()->enrol_user($user->id, $course->id);
1896         $options = array('course' => $course->id, 'forcesubscribe' => FORUM_CHOOSESUBSCRIBE);
1897         $forum = $this->getDataGenerator()->create_module('forum', $options);
1899         // Add a discussion.
1900         $record = array();
1901         $record['course'] = $course->id;
1902         $record['forum'] = $forum->id;
1903         $record['userid'] = $user->id;
1904         $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1906         // Add a post.
1907         $record = array();
1908         $record['discussion'] = $discussion->id;
1909         $record['userid'] = $user->id;
1910         $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
1912         // The user is not subscribed to the forum. Insert a new discussion subscription.
1913         $subscription = new \stdClass();
1914         $subscription->userid  = $user->id;
1915         $subscription->forum = $forum->id;
1916         $subscription->discussion = $discussion->id;
1917         $subscription->preference = \mod_forum\subscriptions::FORUM_DISCUSSION_SUBSCRIBED;
1919         $subscription->id = $DB->insert_record('forum_discussion_subs', $subscription);
1921         $context = context_module::instance($forum->cmid);
1923         $params = array(
1924             'context' => $context,
1925             'objectid' => $subscription->id,
1926             'relateduserid' => $user->id,
1927             'other' => array(
1928                 'forumid' => $forum->id,
1929                 'discussion' => $discussion->id,
1930             )
1931         );
1933         $event = \mod_forum\event\discussion_subscription_created::create($params);
1935         // Trigger and capturing the event.
1936         $sink = $this->redirectEvents();
1937         $event->trigger();
1938         $events = $sink->get_events();
1939         $this->assertCount(1, $events);
1940         $event = reset($events);
1941     }
1943     /**
1944      * Test contextlevel validation of discussion_subscription_created event.
1945      */
1946     public function test_discussion_subscription_created_validation_contextlevel() {
1947         global $CFG, $DB;
1948         require_once($CFG->dirroot . '/mod/forum/lib.php');
1950         // Setup test data.
1951         $course = $this->getDataGenerator()->create_course();
1952         $user = $this->getDataGenerator()->create_user();
1953         $this->getDataGenerator()->enrol_user($user->id, $course->id);
1955         $options = array('course' => $course->id, 'forcesubscribe' => FORUM_CHOOSESUBSCRIBE);
1956         $forum = $this->getDataGenerator()->create_module('forum', $options);
1958         // Add a discussion.
1959         $record = array();
1960         $record['course'] = $course->id;
1961         $record['forum'] = $forum->id;
1962         $record['userid'] = $user->id;
1963         $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1965         // Add a post.
1966         $record = array();
1967         $record['discussion'] = $discussion->id;
1968         $record['userid'] = $user->id;
1969         $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
1971         // The user is not subscribed to the forum. Insert a new discussion subscription.
1972         $subscription = new \stdClass();
1973         $subscription->userid  = $user->id;
1974         $subscription->forum = $forum->id;
1975         $subscription->discussion = $discussion->id;
1976         $subscription->preference = \mod_forum\subscriptions::FORUM_DISCUSSION_SUBSCRIBED;
1978         $subscription->id = $DB->insert_record('forum_discussion_subs', $subscription);
1980         $context = context_module::instance($forum->cmid);
1982         $params = array(
1983             'context' => \context_course::instance($course->id),
1984             'objectid' => $subscription->id,
1985             'relateduserid' => $user->id,
1986             'other' => array(
1987                 'forumid' => $forum->id,
1988                 'discussion' => $discussion->id,
1989             )
1990         );
1992         // Without an invalid context.
1993         $this->setExpectedException('coding_exception', 'Context level must be CONTEXT_MODULE.');
1994         \mod_forum\event\discussion_subscription_created::create($params);
1995     }
1997     /**
1998      * Test discussion validation of discussion_subscription_created event.
1999      */
2000     public function test_discussion_subscription_created_validation_discussion() {
2001         global $CFG, $DB;
2002         require_once($CFG->dirroot . '/mod/forum/lib.php');
2004         // Setup test data.
2005         $course = $this->getDataGenerator()->create_course();
2006         $user = $this->getDataGenerator()->create_user();
2007         $this->getDataGenerator()->enrol_user($user->id, $course->id);
2009         $options = array('course' => $course->id, 'forcesubscribe' => FORUM_CHOOSESUBSCRIBE);
2010         $forum = $this->getDataGenerator()->create_module('forum', $options);
2012         // Add a discussion.
2013         $record = array();
2014         $record['course'] = $course->id;
2015         $record['forum'] = $forum->id;
2016         $record['userid'] = $user->id;
2017         $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
2019         // Add a post.
2020         $record = array();
2021         $record['discussion'] = $discussion->id;
2022         $record['userid'] = $user->id;
2023         $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
2025         // The user is not subscribed to the forum. Insert a new discussion subscription.
2026         $subscription = new \stdClass();
2027         $subscription->userid  = $user->id;
2028         $subscription->forum = $forum->id;
2029         $subscription->discussion = $discussion->id;
2030         $subscription->preference = \mod_forum\subscriptions::FORUM_DISCUSSION_SUBSCRIBED;
2032         $subscription->id = $DB->insert_record('forum_discussion_subs', $subscription);
2034         // Without the discussion.
2035         $params = array(
2036             'context' => context_module::instance($forum->cmid),
2037             'objectid' => $subscription->id,
2038             'relateduserid' => $user->id,
2039             'other' => array(
2040                 'forumid' => $forum->id,
2041             )
2042         );
2044         $this->setExpectedException('coding_exception', "The 'discussion' value must be set in other.");
2045         \mod_forum\event\discussion_subscription_created::create($params);
2046     }
2048     /**
2049      * Test forumid validation of discussion_subscription_created event.
2050      */
2051     public function test_discussion_subscription_created_validation_forumid() {
2052         global $CFG, $DB;
2053         require_once($CFG->dirroot . '/mod/forum/lib.php');
2055         // Setup test data.
2056         $course = $this->getDataGenerator()->create_course();
2057         $user = $this->getDataGenerator()->create_user();
2058         $this->getDataGenerator()->enrol_user($user->id, $course->id);
2060         $options = array('course' => $course->id, 'forcesubscribe' => FORUM_CHOOSESUBSCRIBE);
2061         $forum = $this->getDataGenerator()->create_module('forum', $options);
2063         // Add a discussion.
2064         $record = array();
2065         $record['course'] = $course->id;
2066         $record['forum'] = $forum->id;
2067         $record['userid'] = $user->id;
2068         $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
2070         // Add a post.
2071         $record = array();
2072         $record['discussion'] = $discussion->id;
2073         $record['userid'] = $user->id;
2074         $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
2076         // The user is not subscribed to the forum. Insert a new discussion subscription.
2077         $subscription = new \stdClass();
2078         $subscription->userid  = $user->id;
2079         $subscription->forum = $forum->id;
2080         $subscription->discussion = $discussion->id;
2081         $subscription->preference = \mod_forum\subscriptions::FORUM_DISCUSSION_SUBSCRIBED;
2083         $subscription->id = $DB->insert_record('forum_discussion_subs', $subscription);
2085         // Without the forumid.
2086         $params = array(
2087             'context' => context_module::instance($forum->cmid),
2088             'objectid' => $subscription->id,
2089             'relateduserid' => $user->id,
2090             'other' => array(
2091                 'discussion' => $discussion->id,
2092             )
2093         );
2095         $this->setExpectedException('coding_exception', "The 'forumid' value must be set in other.");
2096         \mod_forum\event\discussion_subscription_created::create($params);
2097     }
2099     /**
2100      * Test relateduserid validation of discussion_subscription_created event.
2101      */
2102     public function test_discussion_subscription_created_validation_relateduserid() {
2103         global $CFG, $DB;
2104         require_once($CFG->dirroot . '/mod/forum/lib.php');
2106         // Setup test data.
2107         $course = $this->getDataGenerator()->create_course();
2108         $user = $this->getDataGenerator()->create_user();
2109         $this->getDataGenerator()->enrol_user($user->id, $course->id);
2111         $options = array('course' => $course->id, 'forcesubscribe' => FORUM_CHOOSESUBSCRIBE);
2112         $forum = $this->getDataGenerator()->create_module('forum', $options);
2114         // Add a discussion.
2115         $record = array();
2116         $record['course'] = $course->id;
2117         $record['forum'] = $forum->id;
2118         $record['userid'] = $user->id;
2119         $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
2121         // Add a post.
2122         $record = array();
2123         $record['discussion'] = $discussion->id;
2124         $record['userid'] = $user->id;
2125         $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
2127         // The user is not subscribed to the forum. Insert a new discussion subscription.
2128         $subscription = new \stdClass();
2129         $subscription->userid  = $user->id;
2130         $subscription->forum = $forum->id;
2131         $subscription->discussion = $discussion->id;
2132         $subscription->preference = \mod_forum\subscriptions::FORUM_DISCUSSION_SUBSCRIBED;
2134         $subscription->id = $DB->insert_record('forum_discussion_subs', $subscription);
2136         $context = context_module::instance($forum->cmid);
2138         // Without the relateduserid.
2139         $params = array(
2140             'context' => context_module::instance($forum->cmid),
2141             'objectid' => $subscription->id,
2142             'other' => array(
2143                 'forumid' => $forum->id,
2144                 'discussion' => $discussion->id,
2145             )
2146         );
2148         $this->setExpectedException('coding_exception', "The 'relateduserid' must be set.");
2149         \mod_forum\event\discussion_subscription_created::create($params);
2150     }
2152     /**
2153      * Test discussion_subscription_deleted event.
2154      */
2155     public function test_discussion_subscription_deleted() {
2156         global $CFG;
2157         require_once($CFG->dirroot . '/mod/forum/lib.php');
2159         // Setup test data.
2160         $course = $this->getDataGenerator()->create_course();
2161         $user = $this->getDataGenerator()->create_user();
2162         $this->getDataGenerator()->enrol_user($user->id, $course->id);
2164         $options = array('course' => $course->id, 'forcesubscribe' => FORUM_INITIALSUBSCRIBE);
2165         $forum = $this->getDataGenerator()->create_module('forum', $options);
2167         // Add a discussion.
2168         $record = array();
2169         $record['course'] = $course->id;
2170         $record['forum'] = $forum->id;
2171         $record['userid'] = $user->id;
2172         $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
2174         // Add a post.
2175         $record = array();
2176         $record['discussion'] = $discussion->id;
2177         $record['userid'] = $user->id;
2178         $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
2180         // Trigger and capturing the event.
2181         $sink = $this->redirectEvents();
2183         // Trigger the event by unsubscribing the user to the forum discussion.
2184         \mod_forum\subscriptions::unsubscribe_user_from_discussion($user->id, $discussion);
2186         $events = $sink->get_events();
2187         $this->assertCount(1, $events);
2188         $event = reset($events);
2190         // Checking that the event contains the expected values.
2191         $this->assertInstanceOf('\mod_forum\event\discussion_subscription_deleted', $event);
2194         $cm = get_coursemodule_from_instance('forum', $discussion->forum);
2195         $context = \context_module::instance($cm->id);
2196         $this->assertEquals($context, $event->get_context());
2198         $url = new \moodle_url('/mod/forum/subscribe.php', array(
2199             'id' => $forum->id,
2200             'd' => $discussion->id
2201         ));
2203         $this->assertEquals($url, $event->get_url());
2204         $this->assertEventContextNotUsed($event);
2205         $this->assertNotEmpty($event->get_name());
2206     }
2208     /**
2209      * Test validation of discussion_subscription_deleted event.
2210      */
2211     public function test_discussion_subscription_deleted_validation() {
2212         global $CFG, $DB;
2213         require_once($CFG->dirroot . '/mod/forum/lib.php');
2215         // Setup test data.
2216         $course = $this->getDataGenerator()->create_course();
2217         $user = $this->getDataGenerator()->create_user();
2218         $this->getDataGenerator()->enrol_user($user->id, $course->id);
2220         $options = array('course' => $course->id, 'forcesubscribe' => FORUM_INITIALSUBSCRIBE);
2221         $forum = $this->getDataGenerator()->create_module('forum', $options);
2223         // Add a discussion.
2224         $record = array();
2225         $record['course'] = $course->id;
2226         $record['forum'] = $forum->id;
2227         $record['userid'] = $user->id;
2228         $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
2230         // Add a post.
2231         $record = array();
2232         $record['discussion'] = $discussion->id;
2233         $record['userid'] = $user->id;
2234         $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
2236         // The user is not subscribed to the forum. Insert a new discussion subscription.
2237         $subscription = new \stdClass();
2238         $subscription->userid  = $user->id;
2239         $subscription->forum = $forum->id;
2240         $subscription->discussion = $discussion->id;
2241         $subscription->preference = \mod_forum\subscriptions::FORUM_DISCUSSION_UNSUBSCRIBED;
2243         $subscription->id = $DB->insert_record('forum_discussion_subs', $subscription);
2245         $context = context_module::instance($forum->cmid);
2247         $params = array(
2248             'context' => $context,
2249             'objectid' => $subscription->id,
2250             'relateduserid' => $user->id,
2251             'other' => array(
2252                 'forumid' => $forum->id,
2253                 'discussion' => $discussion->id,
2254             )
2255         );
2257         $event = \mod_forum\event\discussion_subscription_deleted::create($params);
2259         // Trigger and capturing the event.
2260         $sink = $this->redirectEvents();
2261         $event->trigger();
2262         $events = $sink->get_events();
2263         $this->assertCount(1, $events);
2264         $event = reset($events);
2266         // Without an invalid context.
2267         $params['context'] = \context_course::instance($course->id);
2268         $this->setExpectedException('coding_exception', 'Context level must be CONTEXT_MODULE.');
2269         \mod_forum\event\discussion_deleted::create($params);
2271         // Without the discussion.
2272         unset($params['discussion']);
2273         $this->setExpectedException('coding_exception', 'The \'discussion\' value must be set in other.');
2274         \mod_forum\event\discussion_deleted::create($params);
2276         // Without the forumid.
2277         unset($params['forumid']);
2278         $this->setExpectedException('coding_exception', 'The \'forumid\' value must be set in other.');
2279         \mod_forum\event\discussion_deleted::create($params);
2281         // Without the relateduserid.
2282         unset($params['relateduserid']);
2283         $this->setExpectedException('coding_exception', 'The \'relateduserid\' value must be set in other.');
2284         \mod_forum\event\discussion_deleted::create($params);
2285     }
2287     /**
2288      * Test contextlevel validation of discussion_subscription_deleted event.
2289      */
2290     public function test_discussion_subscription_deleted_validation_contextlevel() {
2291         global $CFG, $DB;
2292         require_once($CFG->dirroot . '/mod/forum/lib.php');
2294         // Setup test data.
2295         $course = $this->getDataGenerator()->create_course();
2296         $user = $this->getDataGenerator()->create_user();
2297         $this->getDataGenerator()->enrol_user($user->id, $course->id);
2299         $options = array('course' => $course->id, 'forcesubscribe' => FORUM_CHOOSESUBSCRIBE);
2300         $forum = $this->getDataGenerator()->create_module('forum', $options);
2302         // Add a discussion.
2303         $record = array();
2304         $record['course'] = $course->id;
2305         $record['forum'] = $forum->id;
2306         $record['userid'] = $user->id;
2307         $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
2309         // Add a post.
2310         $record = array();
2311         $record['discussion'] = $discussion->id;
2312         $record['userid'] = $user->id;
2313         $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
2315         // The user is not subscribed to the forum. Insert a new discussion subscription.
2316         $subscription = new \stdClass();
2317         $subscription->userid  = $user->id;
2318         $subscription->forum = $forum->id;
2319         $subscription->discussion = $discussion->id;
2320         $subscription->preference = \mod_forum\subscriptions::FORUM_DISCUSSION_SUBSCRIBED;
2322         $subscription->id = $DB->insert_record('forum_discussion_subs', $subscription);
2324         $context = context_module::instance($forum->cmid);
2326         $params = array(
2327             'context' => \context_course::instance($course->id),
2328             'objectid' => $subscription->id,
2329             'relateduserid' => $user->id,
2330             'other' => array(
2331                 'forumid' => $forum->id,
2332                 'discussion' => $discussion->id,
2333             )
2334         );
2336         // Without an invalid context.
2337         $this->setExpectedException('coding_exception', 'Context level must be CONTEXT_MODULE.');
2338         \mod_forum\event\discussion_subscription_deleted::create($params);
2339     }
2341     /**
2342      * Test discussion validation of discussion_subscription_deleted event.
2343      */
2344     public function test_discussion_subscription_deleted_validation_discussion() {
2345         global $CFG, $DB;
2346         require_once($CFG->dirroot . '/mod/forum/lib.php');
2348         // Setup test data.
2349         $course = $this->getDataGenerator()->create_course();
2350         $user = $this->getDataGenerator()->create_user();
2351         $this->getDataGenerator()->enrol_user($user->id, $course->id);
2353         $options = array('course' => $course->id, 'forcesubscribe' => FORUM_CHOOSESUBSCRIBE);
2354         $forum = $this->getDataGenerator()->create_module('forum', $options);
2356         // Add a discussion.
2357         $record = array();
2358         $record['course'] = $course->id;
2359         $record['forum'] = $forum->id;
2360         $record['userid'] = $user->id;
2361         $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
2363         // Add a post.
2364         $record = array();
2365         $record['discussion'] = $discussion->id;
2366         $record['userid'] = $user->id;
2367         $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
2369         // The user is not subscribed to the forum. Insert a new discussion subscription.
2370         $subscription = new \stdClass();
2371         $subscription->userid  = $user->id;
2372         $subscription->forum = $forum->id;
2373         $subscription->discussion = $discussion->id;
2374         $subscription->preference = \mod_forum\subscriptions::FORUM_DISCUSSION_SUBSCRIBED;
2376         $subscription->id = $DB->insert_record('forum_discussion_subs', $subscription);
2378         // Without the discussion.
2379         $params = array(
2380             'context' => context_module::instance($forum->cmid),
2381             'objectid' => $subscription->id,
2382             'relateduserid' => $user->id,
2383             'other' => array(
2384                 'forumid' => $forum->id,
2385             )
2386         );
2388         $this->setExpectedException('coding_exception', "The 'discussion' value must be set in other.");
2389         \mod_forum\event\discussion_subscription_deleted::create($params);
2390     }
2392     /**
2393      * Test forumid validation of discussion_subscription_deleted event.
2394      */
2395     public function test_discussion_subscription_deleted_validation_forumid() {
2396         global $CFG, $DB;
2397         require_once($CFG->dirroot . '/mod/forum/lib.php');
2399         // Setup test data.
2400         $course = $this->getDataGenerator()->create_course();
2401         $user = $this->getDataGenerator()->create_user();
2402         $this->getDataGenerator()->enrol_user($user->id, $course->id);
2404         $options = array('course' => $course->id, 'forcesubscribe' => FORUM_CHOOSESUBSCRIBE);
2405         $forum = $this->getDataGenerator()->create_module('forum', $options);
2407         // Add a discussion.
2408         $record = array();
2409         $record['course'] = $course->id;
2410         $record['forum'] = $forum->id;
2411         $record['userid'] = $user->id;
2412         $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
2414         // Add a post.
2415         $record = array();
2416         $record['discussion'] = $discussion->id;
2417         $record['userid'] = $user->id;
2418         $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
2420         // The user is not subscribed to the forum. Insert a new discussion subscription.
2421         $subscription = new \stdClass();
2422         $subscription->userid  = $user->id;
2423         $subscription->forum = $forum->id;
2424         $subscription->discussion = $discussion->id;
2425         $subscription->preference = \mod_forum\subscriptions::FORUM_DISCUSSION_SUBSCRIBED;
2427         $subscription->id = $DB->insert_record('forum_discussion_subs', $subscription);
2429         // Without the forumid.
2430         $params = array(
2431             'context' => context_module::instance($forum->cmid),
2432             'objectid' => $subscription->id,
2433             'relateduserid' => $user->id,
2434             'other' => array(
2435                 'discussion' => $discussion->id,
2436             )
2437         );
2439         $this->setExpectedException('coding_exception', "The 'forumid' value must be set in other.");
2440         \mod_forum\event\discussion_subscription_deleted::create($params);
2441     }
2443     /**
2444      * Test relateduserid validation of discussion_subscription_deleted event.
2445      */
2446     public function test_discussion_subscription_deleted_validation_relateduserid() {
2447         global $CFG, $DB;
2448         require_once($CFG->dirroot . '/mod/forum/lib.php');
2450         // Setup test data.
2451         $course = $this->getDataGenerator()->create_course();
2452         $user = $this->getDataGenerator()->create_user();
2453         $this->getDataGenerator()->enrol_user($user->id, $course->id);
2455         $options = array('course' => $course->id, 'forcesubscribe' => FORUM_CHOOSESUBSCRIBE);
2456         $forum = $this->getDataGenerator()->create_module('forum', $options);
2458         // Add a discussion.
2459         $record = array();
2460         $record['course'] = $course->id;
2461         $record['forum'] = $forum->id;
2462         $record['userid'] = $user->id;
2463         $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
2465         // Add a post.
2466         $record = array();
2467         $record['discussion'] = $discussion->id;
2468         $record['userid'] = $user->id;
2469         $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
2471         // The user is not subscribed to the forum. Insert a new discussion subscription.
2472         $subscription = new \stdClass();
2473         $subscription->userid  = $user->id;
2474         $subscription->forum = $forum->id;
2475         $subscription->discussion = $discussion->id;
2476         $subscription->preference = \mod_forum\subscriptions::FORUM_DISCUSSION_SUBSCRIBED;
2478         $subscription->id = $DB->insert_record('forum_discussion_subs', $subscription);
2480         $context = context_module::instance($forum->cmid);
2482         // Without the relateduserid.
2483         $params = array(
2484             'context' => context_module::instance($forum->cmid),
2485             'objectid' => $subscription->id,
2486             'other' => array(
2487                 'forumid' => $forum->id,
2488                 'discussion' => $discussion->id,
2489             )
2490         );
2492         $this->setExpectedException('coding_exception', "The 'relateduserid' must be set.");
2493         \mod_forum\event\discussion_subscription_deleted::create($params);
2494     }
2496     /**
2497      * Test that the correct context is used in the events when subscribing
2498      * users.
2499      */
2500     public function test_forum_subscription_page_context_valid() {
2501         global $CFG, $PAGE;
2502         require_once($CFG->dirroot . '/mod/forum/lib.php');
2504         // Setup test data.
2505         $course = $this->getDataGenerator()->create_course();
2506         $user = $this->getDataGenerator()->create_user();
2507         $this->getDataGenerator()->enrol_user($user->id, $course->id);
2509         $options = array('course' => $course->id, 'forcesubscribe' => FORUM_CHOOSESUBSCRIBE);
2510         $forum = $this->getDataGenerator()->create_module('forum', $options);
2511         $quiz = $this->getDataGenerator()->create_module('quiz', $options);
2513         // Add a discussion.
2514         $record = array();
2515         $record['course'] = $course->id;
2516         $record['forum'] = $forum->id;
2517         $record['userid'] = $user->id;
2518         $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
2520         // Add a post.
2521         $record = array();
2522         $record['discussion'] = $discussion->id;
2523         $record['userid'] = $user->id;
2524         $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
2526         // Set up the default page event to use this forum.
2527         $PAGE = new moodle_page();
2528         $cm = get_coursemodule_from_instance('forum', $discussion->forum);
2529         $context = \context_module::instance($cm->id);
2530         $PAGE->set_context($context);
2531         $PAGE->set_cm($cm, $course, $forum);
2533         // Trigger and capturing the event.
2534         $sink = $this->redirectEvents();
2536         // Trigger the event by subscribing the user to the forum.
2537         \mod_forum\subscriptions::subscribe_user($user->id, $forum);
2539         $events = $sink->get_events();
2540         $sink->clear();
2541         $this->assertCount(1, $events);
2542         $event = reset($events);
2544         // Checking that the event contains the expected values.
2545         $this->assertInstanceOf('\mod_forum\event\subscription_created', $event);
2546         $this->assertEquals($context, $event->get_context());
2548         // Trigger the event by unsubscribing the user to the forum.
2549         \mod_forum\subscriptions::unsubscribe_user($user->id, $forum);
2551         $events = $sink->get_events();
2552         $sink->clear();
2553         $this->assertCount(1, $events);
2554         $event = reset($events);
2556         // Checking that the event contains the expected values.
2557         $this->assertInstanceOf('\mod_forum\event\subscription_deleted', $event);
2558         $this->assertEquals($context, $event->get_context());
2560         // Trigger the event by subscribing the user to the discussion.
2561         \mod_forum\subscriptions::subscribe_user_to_discussion($user->id, $discussion);
2563         $events = $sink->get_events();
2564         $sink->clear();
2565         $this->assertCount(1, $events);
2566         $event = reset($events);
2568         // Checking that the event contains the expected values.
2569         $this->assertInstanceOf('\mod_forum\event\discussion_subscription_created', $event);
2570         $this->assertEquals($context, $event->get_context());
2572         // Trigger the event by unsubscribing the user from the discussion.
2573         \mod_forum\subscriptions::unsubscribe_user_from_discussion($user->id, $discussion);
2575         $events = $sink->get_events();
2576         $sink->clear();
2577         $this->assertCount(1, $events);
2578         $event = reset($events);
2580         // Checking that the event contains the expected values.
2581         $this->assertInstanceOf('\mod_forum\event\discussion_subscription_deleted', $event);
2582         $this->assertEquals($context, $event->get_context());
2584         // Now try with the context for a different module (quiz).
2585         $PAGE = new moodle_page();
2586         $cm = get_coursemodule_from_instance('quiz', $quiz->id);
2587         $quizcontext = \context_module::instance($cm->id);
2588         $PAGE->set_context($quizcontext);
2589         $PAGE->set_cm($cm, $course, $quiz);
2591         // Trigger and capturing the event.
2592         $sink = $this->redirectEvents();
2594         // Trigger the event by subscribing the user to the forum.
2595         \mod_forum\subscriptions::subscribe_user($user->id, $forum);
2597         $events = $sink->get_events();
2598         $sink->clear();
2599         $this->assertCount(1, $events);
2600         $event = reset($events);
2602         // Checking that the event contains the expected values.
2603         $this->assertInstanceOf('\mod_forum\event\subscription_created', $event);
2604         $this->assertEquals($context, $event->get_context());
2606         // Trigger the event by unsubscribing the user to the forum.
2607         \mod_forum\subscriptions::unsubscribe_user($user->id, $forum);
2609         $events = $sink->get_events();
2610         $sink->clear();
2611         $this->assertCount(1, $events);
2612         $event = reset($events);
2614         // Checking that the event contains the expected values.
2615         $this->assertInstanceOf('\mod_forum\event\subscription_deleted', $event);
2616         $this->assertEquals($context, $event->get_context());
2618         // Trigger the event by subscribing the user to the discussion.
2619         \mod_forum\subscriptions::subscribe_user_to_discussion($user->id, $discussion);
2621         $events = $sink->get_events();
2622         $sink->clear();
2623         $this->assertCount(1, $events);
2624         $event = reset($events);
2626         // Checking that the event contains the expected values.
2627         $this->assertInstanceOf('\mod_forum\event\discussion_subscription_created', $event);
2628         $this->assertEquals($context, $event->get_context());
2630         // Trigger the event by unsubscribing the user from the discussion.
2631         \mod_forum\subscriptions::unsubscribe_user_from_discussion($user->id, $discussion);
2633         $events = $sink->get_events();
2634         $sink->clear();
2635         $this->assertCount(1, $events);
2636         $event = reset($events);
2638         // Checking that the event contains the expected values.
2639         $this->assertInstanceOf('\mod_forum\event\discussion_subscription_deleted', $event);
2640         $this->assertEquals($context, $event->get_context());
2642         // Now try with the course context - the module context should still be used.
2643         $PAGE = new moodle_page();
2644         $coursecontext = \context_course::instance($course->id);
2645         $PAGE->set_context($coursecontext);
2647         // Trigger the event by subscribing the user to the forum.
2648         \mod_forum\subscriptions::subscribe_user($user->id, $forum);
2650         $events = $sink->get_events();
2651         $sink->clear();
2652         $this->assertCount(1, $events);
2653         $event = reset($events);
2655         // Checking that the event contains the expected values.
2656         $this->assertInstanceOf('\mod_forum\event\subscription_created', $event);
2657         $this->assertEquals($context, $event->get_context());
2659         // Trigger the event by unsubscribing the user to the forum.
2660         \mod_forum\subscriptions::unsubscribe_user($user->id, $forum);
2662         $events = $sink->get_events();
2663         $sink->clear();
2664         $this->assertCount(1, $events);
2665         $event = reset($events);
2667         // Checking that the event contains the expected values.
2668         $this->assertInstanceOf('\mod_forum\event\subscription_deleted', $event);
2669         $this->assertEquals($context, $event->get_context());
2671         // Trigger the event by subscribing the user to the discussion.
2672         \mod_forum\subscriptions::subscribe_user_to_discussion($user->id, $discussion);
2674         $events = $sink->get_events();
2675         $sink->clear();
2676         $this->assertCount(1, $events);
2677         $event = reset($events);
2679         // Checking that the event contains the expected values.
2680         $this->assertInstanceOf('\mod_forum\event\discussion_subscription_created', $event);
2681         $this->assertEquals($context, $event->get_context());
2683         // Trigger the event by unsubscribing the user from the discussion.
2684         \mod_forum\subscriptions::unsubscribe_user_from_discussion($user->id, $discussion);
2686         $events = $sink->get_events();
2687         $sink->clear();
2688         $this->assertCount(1, $events);
2689         $event = reset($events);
2691         // Checking that the event contains the expected values.
2692         $this->assertInstanceOf('\mod_forum\event\discussion_subscription_deleted', $event);
2693         $this->assertEquals($context, $event->get_context());
2695     }