Merge branch 'MDL-66927-run-new-adhoc' of https://github.com/brendanheywood/moodle
[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      * @expectedException        coding_exception
59      * @expectedExceptionMessage The 'searchterm' value must be set in other.
60      */
61     public function test_course_searched_searchterm_validation() {
62         $course = $this->getDataGenerator()->create_course();
63         $coursectx = context_course::instance($course->id);
64         $params = array(
65             'context' => $coursectx,
66         );
68         \mod_forum\event\course_searched::create($params);
69     }
71     /**
72      * Ensure course_searched event validates that context is the correct level.
73      *
74      * @expectedException        coding_exception
75      * @expectedExceptionMessage Context level must be CONTEXT_COURSE.
76      */
77     public function test_course_searched_context_validation() {
78         $course = $this->getDataGenerator()->create_course();
79         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
80         $context = context_module::instance($forum->cmid);
81         $params = array(
82             'context' => $context,
83             'other' => array('searchterm' => 'testing'),
84         );
86         \mod_forum\event\course_searched::create($params);
87     }
89     /**
90      * Test course_searched event.
91      */
92     public function test_course_searched() {
94         // Setup test data.
95         $course = $this->getDataGenerator()->create_course();
96         $coursectx = context_course::instance($course->id);
97         $searchterm = 'testing123';
99         $params = array(
100             'context' => $coursectx,
101             'other' => array('searchterm' => $searchterm),
102         );
104         // Create event.
105         $event = \mod_forum\event\course_searched::create($params);
107         // Trigger and capture the event.
108         $sink = $this->redirectEvents();
109         $event->trigger();
110         $events = $sink->get_events();
111         $this->assertCount(1, $events);
112         $event = reset($events);
114          // Checking that the event contains the expected values.
115         $this->assertInstanceOf('\mod_forum\event\course_searched', $event);
116         $this->assertEquals($coursectx, $event->get_context());
117         $expected = array($course->id, 'forum', 'search', "search.php?id={$course->id}&amp;search={$searchterm}", $searchterm);
118         $this->assertEventLegacyLogData($expected, $event);
119         $this->assertEventContextNotUsed($event);
121         $this->assertNotEmpty($event->get_name());
122     }
124     /**
125      * Ensure discussion_created event validates that forumid is set.
126      *
127      * @expectedException        coding_exception
128      * @expectedExceptionMessage The 'forumid' value must be set in other.
129      */
130     public function test_discussion_created_forumid_validation() {
131         $course = $this->getDataGenerator()->create_course();
132         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
133         $context = context_module::instance($forum->cmid);
135         $params = array(
136             'context' => $context,
137         );
139         \mod_forum\event\discussion_created::create($params);
140     }
142     /**
143      * Ensure discussion_created event validates that the context is the correct level.
144      *
145      * @expectedException        coding_exception
146      * @expectedExceptionMessage Context level must be CONTEXT_MODULE.
147      */
148     public function test_discussion_created_context_validation() {
149         $course = $this->getDataGenerator()->create_course();
150         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
152         $params = array(
153             'context' => context_system::instance(),
154             'other' => array('forumid' => $forum->id),
155         );
157         \mod_forum\event\discussion_created::create($params);
158     }
160     /**
161      * Test discussion_created event.
162      */
163     public function test_discussion_created() {
165         // Setup test data.
166         $course = $this->getDataGenerator()->create_course();
167         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
168         $user = $this->getDataGenerator()->create_user();
170         // Add a discussion.
171         $record = array();
172         $record['course'] = $course->id;
173         $record['forum'] = $forum->id;
174         $record['userid'] = $user->id;
175         $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
177         $context = context_module::instance($forum->cmid);
179         $params = array(
180             'context' => $context,
181             'objectid' => $discussion->id,
182             'other' => array('forumid' => $forum->id),
183         );
185         // Create the event.
186         $event = \mod_forum\event\discussion_created::create($params);
188         // Trigger and capturing the event.
189         $sink = $this->redirectEvents();
190         $event->trigger();
191         $events = $sink->get_events();
192         $this->assertCount(1, $events);
193         $event = reset($events);
195         // Check that the event contains the expected values.
196         $this->assertInstanceOf('\mod_forum\event\discussion_created', $event);
197         $this->assertEquals($context, $event->get_context());
198         $expected = array($course->id, 'forum', 'add discussion', "discuss.php?d={$discussion->id}", $discussion->id, $forum->cmid);
199         $this->assertEventLegacyLogData($expected, $event);
200         $this->assertEventContextNotUsed($event);
202         $this->assertNotEmpty($event->get_name());
203     }
205     /**
206      * Ensure discussion_updated event validates that forumid is set.
207      *
208      * @expectedException        coding_exception
209      * @expectedExceptionMessage The 'forumid' value must be set in other.
210      */
211     public function test_discussion_updated_forumid_validation() {
212         $course = $this->getDataGenerator()->create_course();
213         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
214         $context = context_module::instance($forum->cmid);
216         $params = array(
217             'context' => $context,
218         );
220         \mod_forum\event\discussion_updated::create($params);
221     }
223     /**
224      * Ensure discussion_created event validates that the context is the correct level.
225      *
226      * @expectedException        coding_exception
227      * @expectedExceptionMessage Context level must be CONTEXT_MODULE.
228      */
229     public function test_discussion_updated_context_validation() {
230         $course = $this->getDataGenerator()->create_course();
231         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
233         $params = array(
234             'context' => context_system::instance(),
235             'other' => array('forumid' => $forum->id),
236         );
238         \mod_forum\event\discussion_updated::create($params);
239     }
241     /**
242      * Test discussion_created event.
243      */
244     public function test_discussion_updated() {
246         // Setup test data.
247         $course = $this->getDataGenerator()->create_course();
248         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
249         $user = $this->getDataGenerator()->create_user();
251         // Add a discussion.
252         $record = array();
253         $record['course'] = $course->id;
254         $record['forum'] = $forum->id;
255         $record['userid'] = $user->id;
256         $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
258         $context = context_module::instance($forum->cmid);
260         $params = array(
261             'context' => $context,
262             'objectid' => $discussion->id,
263             'other' => array('forumid' => $forum->id),
264         );
266         // Create the event.
267         $event = \mod_forum\event\discussion_updated::create($params);
269         // Trigger and capturing the event.
270         $sink = $this->redirectEvents();
271         $event->trigger();
272         $events = $sink->get_events();
273         $this->assertCount(1, $events);
274         $event = reset($events);
276         // Check that the event contains the expected values.
277         $this->assertInstanceOf('\mod_forum\event\discussion_updated', $event);
278         $this->assertEquals($context, $event->get_context());
279         $this->assertEventContextNotUsed($event);
281         $this->assertNotEmpty($event->get_name());
282     }
284     /**
285      * Ensure discussion_deleted event validates that forumid is set.
286      *
287      * @expectedException        coding_exception
288      * @expectedExceptionMessage The 'forumid' value must be set in other.
289      */
290     public function test_discussion_deleted_forumid_validation() {
291         $course = $this->getDataGenerator()->create_course();
292         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
293         $context = context_module::instance($forum->cmid);
295         $params = array(
296             'context' => $context,
297         );
299         \mod_forum\event\discussion_deleted::create($params);
300     }
302     /**
303      * Ensure discussion_deleted event validates that context is of the correct level.
304      *
305      * @expectedException        coding_exception
306      * @expectedExceptionMessage Context level must be CONTEXT_MODULE.
307      */
308     public function test_discussion_deleted_context_validation() {
309         $course = $this->getDataGenerator()->create_course();
310         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
312         $params = array(
313             'context' => context_system::instance(),
314             'other' => array('forumid' => $forum->id),
315         );
317         \mod_forum\event\discussion_deleted::create($params);
318     }
320     /**
321      * Test discussion_deleted event.
322      */
323     public function test_discussion_deleted() {
325         // Setup test data.
326         $course = $this->getDataGenerator()->create_course();
327         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
328         $user = $this->getDataGenerator()->create_user();
330         // Add a discussion.
331         $record = array();
332         $record['course'] = $course->id;
333         $record['forum'] = $forum->id;
334         $record['userid'] = $user->id;
335         $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
337         $context = context_module::instance($forum->cmid);
339         $params = array(
340             'context' => $context,
341             'objectid' => $discussion->id,
342             'other' => array('forumid' => $forum->id),
343         );
345         $event = \mod_forum\event\discussion_deleted::create($params);
347         // Trigger and capture the event.
348         $sink = $this->redirectEvents();
349         $event->trigger();
350         $events = $sink->get_events();
351         $this->assertCount(1, $events);
352         $event = reset($events);
354         // Checking that the event contains the expected values.
355         $this->assertInstanceOf('\mod_forum\event\discussion_deleted', $event);
356         $this->assertEquals($context, $event->get_context());
357         $expected = array($course->id, 'forum', 'delete discussion', "view.php?id={$forum->cmid}", $forum->id, $forum->cmid);
358         $this->assertEventLegacyLogData($expected, $event);
359         $this->assertEventContextNotUsed($event);
361         $this->assertNotEmpty($event->get_name());
362     }
364     /**
365      * Ensure discussion_moved event validates that fromforumid is set.
366      *
367      * @expectedException        coding_exception
368      * @expectedExceptionMessage The 'fromforumid' value must be set in other.
369      */
370     public function test_discussion_moved_fromforumid_validation() {
371         $course = $this->getDataGenerator()->create_course();
372         $toforum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
374         $context = context_module::instance($toforum->cmid);
376         $params = array(
377             'context' => $context,
378             'other' => array('toforumid' => $toforum->id)
379         );
381         \mod_forum\event\discussion_moved::create($params);
382     }
384     /**
385      * Ensure discussion_moved event validates that toforumid is set.
386      *
387      * @expectedException        coding_exception
388      * @expectedExceptionMessage The 'toforumid' value must be set in other.
389      */
390     public function test_discussion_moved_toforumid_validation() {
391         $course = $this->getDataGenerator()->create_course();
392         $fromforum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
393         $toforum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
394         $context = context_module::instance($toforum->cmid);
396         $params = array(
397             'context' => $context,
398             'other' => array('fromforumid' => $fromforum->id)
399         );
401         \mod_forum\event\discussion_moved::create($params);
402     }
404     /**
405      * Ensure discussion_moved event validates that the context level is correct.
406      *
407      * @expectedException        coding_exception
408      * @expectedExceptionMessage Context level must be CONTEXT_MODULE.
409      */
410     public function test_discussion_moved_context_validation() {
411         $course = $this->getDataGenerator()->create_course();
412         $fromforum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
413         $toforum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
414         $user = $this->getDataGenerator()->create_user();
416         // Add a discussion.
417         $record = array();
418         $record['course'] = $course->id;
419         $record['forum'] = $fromforum->id;
420         $record['userid'] = $user->id;
421         $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
423         $params = array(
424             'context' => context_system::instance(),
425             'objectid' => $discussion->id,
426             'other' => array('fromforumid' => $fromforum->id, 'toforumid' => $toforum->id)
427         );
429         \mod_forum\event\discussion_moved::create($params);
430     }
432     /**
433      * Test discussion_moved event.
434      */
435     public function test_discussion_moved() {
436         // Setup test data.
437         $course = $this->getDataGenerator()->create_course();
438         $fromforum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
439         $toforum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
440         $user = $this->getDataGenerator()->create_user();
442         // Add a discussion.
443         $record = array();
444         $record['course'] = $course->id;
445         $record['forum'] = $fromforum->id;
446         $record['userid'] = $user->id;
447         $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
449         $context = context_module::instance($toforum->cmid);
451         $params = array(
452             'context' => $context,
453             'objectid' => $discussion->id,
454             'other' => array('fromforumid' => $fromforum->id, 'toforumid' => $toforum->id)
455         );
457         $event = \mod_forum\event\discussion_moved::create($params);
459         // Trigger and capture the event.
460         $sink = $this->redirectEvents();
461         $event->trigger();
462         $events = $sink->get_events();
463         $this->assertCount(1, $events);
464         $event = reset($events);
466         // Checking that the event contains the expected values.
467         $this->assertInstanceOf('\mod_forum\event\discussion_moved', $event);
468         $this->assertEquals($context, $event->get_context());
469         $expected = array($course->id, 'forum', 'move discussion', "discuss.php?d={$discussion->id}",
470             $discussion->id, $toforum->cmid);
471         $this->assertEventLegacyLogData($expected, $event);
472         $this->assertEventContextNotUsed($event);
474         $this->assertNotEmpty($event->get_name());
475     }
478     /**
479      * Ensure discussion_viewed event validates that the contextlevel is correct.
480      *
481      * @expectedException        coding_exception
482      * @expectedExceptionMessage Context level must be CONTEXT_MODULE.
483      */
484     public function test_discussion_viewed_context_validation() {
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         $params = array(
497             'context' => context_system::instance(),
498             'objectid' => $discussion->id,
499         );
501         \mod_forum\event\discussion_viewed::create($params);
502     }
504     /**
505      * Test discussion_viewed event.
506      */
507     public function test_discussion_viewed() {
508         // Setup test data.
509         $course = $this->getDataGenerator()->create_course();
510         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
511         $user = $this->getDataGenerator()->create_user();
513         // Add a discussion.
514         $record = array();
515         $record['course'] = $course->id;
516         $record['forum'] = $forum->id;
517         $record['userid'] = $user->id;
518         $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
520         $context = context_module::instance($forum->cmid);
522         $params = array(
523             'context' => $context,
524             'objectid' => $discussion->id,
525         );
527         $event = \mod_forum\event\discussion_viewed::create($params);
529         // Trigger and capture the event.
530         $sink = $this->redirectEvents();
531         $event->trigger();
532         $events = $sink->get_events();
533         $this->assertCount(1, $events);
534         $event = reset($events);
536         // Checking that the event contains the expected values.
537         $this->assertInstanceOf('\mod_forum\event\discussion_viewed', $event);
538         $this->assertEquals($context, $event->get_context());
539         $expected = array($course->id, 'forum', 'view discussion', "discuss.php?d={$discussion->id}",
540             $discussion->id, $forum->cmid);
541         $this->assertEventLegacyLogData($expected, $event);
542         $this->assertEventContextNotUsed($event);
544         $this->assertNotEmpty($event->get_name());
545     }
547     /**
548      * Ensure course_module_viewed event validates that the contextlevel is correct.
549      *
550      * @expectedException        coding_exception
551      * @expectedExceptionMessage Context level must be CONTEXT_MODULE.
552      */
553     public function test_course_module_viewed_context_validation() {
554         $course = $this->getDataGenerator()->create_course();
555         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
557         $params = array(
558             'context' => context_system::instance(),
559             'objectid' => $forum->id,
560         );
562         \mod_forum\event\course_module_viewed::create($params);
563     }
565     /**
566      * Test the course_module_viewed event.
567      */
568     public function test_course_module_viewed() {
569         // Setup test data.
570         $course = $this->getDataGenerator()->create_course();
571         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
573         $context = context_module::instance($forum->cmid);
575         $params = array(
576             'context' => $context,
577             'objectid' => $forum->id,
578         );
580         $event = \mod_forum\event\course_module_viewed::create($params);
582         // Trigger and capture the event.
583         $sink = $this->redirectEvents();
584         $event->trigger();
585         $events = $sink->get_events();
586         $this->assertCount(1, $events);
587         $event = reset($events);
589         // Checking that the event contains the expected values.
590         $this->assertInstanceOf('\mod_forum\event\course_module_viewed', $event);
591         $this->assertEquals($context, $event->get_context());
592         $expected = array($course->id, 'forum', 'view forum', "view.php?f={$forum->id}", $forum->id, $forum->cmid);
593         $this->assertEventLegacyLogData($expected, $event);
594         $url = new \moodle_url('/mod/forum/view.php', array('f' => $forum->id));
595         $this->assertEquals($url, $event->get_url());
596         $this->assertEventContextNotUsed($event);
598         $this->assertNotEmpty($event->get_name());
599     }
601     /**
602      * Ensure subscription_created event validates that the forumid is set.
603      *
604      * @expectedException        coding_exception
605      * @expectedExceptionMessage The 'forumid' value must be set in other.
606      */
607     public function test_subscription_created_forumid_validation() {
608         $user = $this->getDataGenerator()->create_user();
609         $course = $this->getDataGenerator()->create_course();
610         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
612         $params = array(
613             'context' => context_module::instance($forum->cmid),
614             'relateduserid' => $user->id,
615         );
617         \mod_forum\event\subscription_created::create($params);
618     }
620     /**
621      * Ensure subscription_created event validates that the relateduserid is set.
622      *
623      * @expectedException        coding_exception
624      * @expectedExceptionMessage The 'relateduserid' must be set.
625      */
626     public function test_subscription_created_relateduserid_validation() {
627         $course = $this->getDataGenerator()->create_course();
628         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
630         $params = array(
631             'context' => context_module::instance($forum->cmid),
632             'objectid' => $forum->id,
633         );
635         \mod_forum\event\subscription_created::create($params);
636     }
638     /**
639      * Ensure subscription_created event validates that the contextlevel is correct.
640      *
641      * @expectedException        coding_exception
642      * @expectedExceptionMessage Context level must be CONTEXT_MODULE.
643      */
644     public function test_subscription_created_contextlevel_validation() {
645         $user = $this->getDataGenerator()->create_user();
646         $course = $this->getDataGenerator()->create_course();
647         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
649         $params = array(
650             'context' => context_system::instance(),
651             'other' => array('forumid' => $forum->id),
652             'relateduserid' => $user->id,
653         );
655         \mod_forum\event\subscription_created::create($params);
656     }
658     /**
659      * Test the subscription_created event.
660      */
661     public function test_subscription_created() {
662         // Setup test data.
663         $user = $this->getDataGenerator()->create_user();
664         $course = $this->getDataGenerator()->create_course();
665         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
666         $user = $this->getDataGenerator()->create_user();
667         $context = context_module::instance($forum->cmid);
669         // Add a subscription.
670         $record = array();
671         $record['course'] = $course->id;
672         $record['forum'] = $forum->id;
673         $record['userid'] = $user->id;
674         $subscription = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_subscription($record);
676         $params = array(
677             'context' => $context,
678             'objectid' => $subscription->id,
679             'other' => array('forumid' => $forum->id),
680             'relateduserid' => $user->id,
681         );
683         $event = \mod_forum\event\subscription_created::create($params);
685         // Trigger and capturing the event.
686         $sink = $this->redirectEvents();
687         $event->trigger();
688         $events = $sink->get_events();
689         $this->assertCount(1, $events);
690         $event = reset($events);
692         // Checking that the event contains the expected values.
693         $this->assertInstanceOf('\mod_forum\event\subscription_created', $event);
694         $this->assertEquals($context, $event->get_context());
695         $expected = array($course->id, 'forum', 'subscribe', "view.php?f={$forum->id}", $forum->id, $forum->cmid);
696         $this->assertEventLegacyLogData($expected, $event);
697         $url = new \moodle_url('/mod/forum/subscribers.php', array('id' => $forum->id));
698         $this->assertEquals($url, $event->get_url());
699         $this->assertEventContextNotUsed($event);
701         $this->assertNotEmpty($event->get_name());
702     }
704     /**
705      * Ensure subscription_deleted event validates that the forumid is set.
706      *
707      * @expectedException        coding_exception
708      * @expectedExceptionMessage The 'forumid' value must be set in other.
709      */
710     public function test_subscription_deleted_forumid_validation() {
711         $user = $this->getDataGenerator()->create_user();
712         $course = $this->getDataGenerator()->create_course();
713         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
715         $params = array(
716             'context' => context_module::instance($forum->cmid),
717             'relateduserid' => $user->id,
718         );
720         \mod_forum\event\subscription_deleted::create($params);
721     }
723     /**
724      * Ensure subscription_deleted event validates that the relateduserid is set.
725      *
726      * @expectedException        coding_exception
727      * @expectedExceptionMessage The 'relateduserid' must be set.
728      */
729     public function test_subscription_deleted_relateduserid_validation() {
730         $course = $this->getDataGenerator()->create_course();
731         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
733         $params = array(
734             'context' => context_module::instance($forum->cmid),
735             'objectid' => $forum->id,
736         );
738         \mod_forum\event\subscription_deleted::create($params);
739     }
741     /**
742      * Ensure subscription_deleted event validates that the contextlevel is correct.
743      *
744      * @expectedException        coding_exception
745      * @expectedExceptionMessage Context level must be CONTEXT_MODULE.
746      */
747     public function test_subscription_deleted_contextlevel_validation() {
748         $user = $this->getDataGenerator()->create_user();
749         $course = $this->getDataGenerator()->create_course();
750         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
752         $params = array(
753             'context' => context_system::instance(),
754             'other' => array('forumid' => $forum->id),
755             'relateduserid' => $user->id,
756         );
758         \mod_forum\event\subscription_deleted::create($params);
759     }
761     /**
762      * Test the subscription_deleted event.
763      */
764     public function test_subscription_deleted() {
765         // Setup test data.
766         $user = $this->getDataGenerator()->create_user();
767         $course = $this->getDataGenerator()->create_course();
768         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
769         $user = $this->getDataGenerator()->create_user();
770         $context = context_module::instance($forum->cmid);
772         // Add a subscription.
773         $record = array();
774         $record['course'] = $course->id;
775         $record['forum'] = $forum->id;
776         $record['userid'] = $user->id;
777         $subscription = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_subscription($record);
779         $params = array(
780             'context' => $context,
781             'objectid' => $subscription->id,
782             'other' => array('forumid' => $forum->id),
783             'relateduserid' => $user->id,
784         );
786         $event = \mod_forum\event\subscription_deleted::create($params);
788         // Trigger and capturing the event.
789         $sink = $this->redirectEvents();
790         $event->trigger();
791         $events = $sink->get_events();
792         $this->assertCount(1, $events);
793         $event = reset($events);
795         // Checking that the event contains the expected values.
796         $this->assertInstanceOf('\mod_forum\event\subscription_deleted', $event);
797         $this->assertEquals($context, $event->get_context());
798         $expected = array($course->id, 'forum', 'unsubscribe', "view.php?f={$forum->id}", $forum->id, $forum->cmid);
799         $this->assertEventLegacyLogData($expected, $event);
800         $url = new \moodle_url('/mod/forum/subscribers.php', array('id' => $forum->id));
801         $this->assertEquals($url, $event->get_url());
802         $this->assertEventContextNotUsed($event);
804         $this->assertNotEmpty($event->get_name());
805     }
807     /**
808      * Ensure readtracking_enabled event validates that the forumid is set.
809      *
810      * @expectedException        coding_exception
811      * @expectedExceptionMessage The 'forumid' value must be set in other.
812      */
813     public function test_readtracking_enabled_forumid_validation() {
814         $user = $this->getDataGenerator()->create_user();
815         $course = $this->getDataGenerator()->create_course();
816         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
818         $params = array(
819             'context' => context_module::instance($forum->cmid),
820             'relateduserid' => $user->id,
821         );
823         \mod_forum\event\readtracking_enabled::create($params);
824     }
826     /**
827      * Ensure readtracking_enabled event validates that the relateduserid is set.
828      *
829      * @expectedException        coding_exception
830      * @expectedExceptionMessage The 'relateduserid' must be set.
831      */
832     public function test_readtracking_enabled_relateduserid_validation() {
833         $course = $this->getDataGenerator()->create_course();
834         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
836         $params = array(
837             'context' => context_module::instance($forum->cmid),
838             'objectid' => $forum->id,
839         );
841         \mod_forum\event\readtracking_enabled::create($params);
842     }
844     /**
845      * Ensure readtracking_enabled event validates that the contextlevel is correct.
846      *
847      * @expectedException        coding_exception
848      * @expectedExceptionMessage Context level must be CONTEXT_MODULE.
849      */
850     public function test_readtracking_enabled_contextlevel_validation() {
851         $user = $this->getDataGenerator()->create_user();
852         $course = $this->getDataGenerator()->create_course();
853         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
855         $params = array(
856             'context' => context_system::instance(),
857             'other' => array('forumid' => $forum->id),
858             'relateduserid' => $user->id,
859         );
861         \mod_forum\event\readtracking_enabled::create($params);
862     }
864     /**
865      * Test the readtracking_enabled event.
866      */
867     public function test_readtracking_enabled() {
868         // Setup test data.
869         $user = $this->getDataGenerator()->create_user();
870         $course = $this->getDataGenerator()->create_course();
871         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
872         $context = context_module::instance($forum->cmid);
874         $params = array(
875             'context' => $context,
876             'other' => array('forumid' => $forum->id),
877             'relateduserid' => $user->id,
878         );
880         $event = \mod_forum\event\readtracking_enabled::create($params);
882         // Trigger and capture the event.
883         $sink = $this->redirectEvents();
884         $event->trigger();
885         $events = $sink->get_events();
886         $this->assertCount(1, $events);
887         $event = reset($events);
889         // Checking that the event contains the expected values.
890         $this->assertInstanceOf('\mod_forum\event\readtracking_enabled', $event);
891         $this->assertEquals($context, $event->get_context());
892         $expected = array($course->id, 'forum', 'start tracking', "view.php?f={$forum->id}", $forum->id, $forum->cmid);
893         $this->assertEventLegacyLogData($expected, $event);
894         $url = new \moodle_url('/mod/forum/view.php', array('f' => $forum->id));
895         $this->assertEquals($url, $event->get_url());
896         $this->assertEventContextNotUsed($event);
898         $this->assertNotEmpty($event->get_name());
899     }
901     /**
902      *  Ensure readtracking_disabled event validates that the forumid is set.
903      *
904      * @expectedException        coding_exception
905      * @expectedExceptionMessage The 'forumid' value must be set in other.
906      */
907     public function test_readtracking_disabled_forumid_validation() {
908         $user = $this->getDataGenerator()->create_user();
909         $course = $this->getDataGenerator()->create_course();
910         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
912         $params = array(
913             'context' => context_module::instance($forum->cmid),
914             'relateduserid' => $user->id,
915         );
917         \mod_forum\event\readtracking_disabled::create($params);
918     }
920     /**
921      *  Ensure readtracking_disabled event validates that the relateduserid is set.
922      *
923      * @expectedException        coding_exception
924      * @expectedExceptionMessage The 'relateduserid' must be set.
925      */
926     public function test_readtracking_disabled_relateduserid_validation() {
927         $course = $this->getDataGenerator()->create_course();
928         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
930         $params = array(
931             'context' => context_module::instance($forum->cmid),
932             'objectid' => $forum->id,
933         );
935         \mod_forum\event\readtracking_disabled::create($params);
936     }
938     /**
939      *  Ensure readtracking_disabled event validates that the contextlevel is correct
940      *
941      * @expectedException        coding_exception
942      * @expectedExceptionMessage Context level must be CONTEXT_MODULE.
943      */
944     public function test_readtracking_disabled_contextlevel_validation() {
945         $user = $this->getDataGenerator()->create_user();
946         $course = $this->getDataGenerator()->create_course();
947         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
949         $params = array(
950             'context' => context_system::instance(),
951             'other' => array('forumid' => $forum->id),
952             'relateduserid' => $user->id,
953         );
955         \mod_forum\event\readtracking_disabled::create($params);
956     }
958     /**
959      *  Test the readtracking_disabled event.
960      */
961     public function test_readtracking_disabled() {
962         // Setup test data.
963         $user = $this->getDataGenerator()->create_user();
964         $course = $this->getDataGenerator()->create_course();
965         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
966         $context = context_module::instance($forum->cmid);
968         $params = array(
969             'context' => $context,
970             'other' => array('forumid' => $forum->id),
971             'relateduserid' => $user->id,
972         );
974         $event = \mod_forum\event\readtracking_disabled::create($params);
976         // Trigger and capture the event.
977         $sink = $this->redirectEvents();
978         $event->trigger();
979         $events = $sink->get_events();
980         $this->assertCount(1, $events);
981         $event = reset($events);
983         // Checking that the event contains the expected values.
984         $this->assertInstanceOf('\mod_forum\event\readtracking_disabled', $event);
985         $this->assertEquals($context, $event->get_context());
986         $expected = array($course->id, 'forum', 'stop tracking', "view.php?f={$forum->id}", $forum->id, $forum->cmid);
987         $this->assertEventLegacyLogData($expected, $event);
988         $url = new \moodle_url('/mod/forum/view.php', array('f' => $forum->id));
989         $this->assertEquals($url, $event->get_url());
990         $this->assertEventContextNotUsed($event);
992         $this->assertNotEmpty($event->get_name());
993     }
995     /**
996      *  Ensure subscribers_viewed event validates that the forumid is set.
997      *
998      * @expectedException        coding_exception
999      * @expectedExceptionMessage The 'forumid' value must be set in other.
1000      */
1001     public function test_subscribers_viewed_forumid_validation() {
1002         $user = $this->getDataGenerator()->create_user();
1003         $course = $this->getDataGenerator()->create_course();
1004         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
1006         $params = array(
1007             'context' => context_module::instance($forum->cmid),
1008             'relateduserid' => $user->id,
1009         );
1011         \mod_forum\event\subscribers_viewed::create($params);
1012     }
1014     /**
1015      *  Ensure subscribers_viewed event validates that the contextlevel is correct.
1016      *
1017      * @expectedException        coding_exception
1018      * @expectedExceptionMessage Context level must be CONTEXT_MODULE.
1019      */
1020     public function test_subscribers_viewed_contextlevel_validation() {
1021         $user = $this->getDataGenerator()->create_user();
1022         $course = $this->getDataGenerator()->create_course();
1023         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
1025         $params = array(
1026             'context' => context_system::instance(),
1027             'other' => array('forumid' => $forum->id),
1028             'relateduserid' => $user->id,
1029         );
1031         \mod_forum\event\subscribers_viewed::create($params);
1032     }
1034     /**
1035      *  Test the subscribers_viewed event.
1036      */
1037     public function test_subscribers_viewed() {
1038         // Setup test data.
1039         $course = $this->getDataGenerator()->create_course();
1040         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
1041         $context = context_module::instance($forum->cmid);
1043         $params = array(
1044             'context' => $context,
1045             'other' => array('forumid' => $forum->id),
1046         );
1048         $event = \mod_forum\event\subscribers_viewed::create($params);
1050         // Trigger and capture the event.
1051         $sink = $this->redirectEvents();
1052         $event->trigger();
1053         $events = $sink->get_events();
1054         $this->assertCount(1, $events);
1055         $event = reset($events);
1057         // Checking that the event contains the expected values.
1058         $this->assertInstanceOf('\mod_forum\event\subscribers_viewed', $event);
1059         $this->assertEquals($context, $event->get_context());
1060         $expected = array($course->id, 'forum', 'view subscribers', "subscribers.php?id={$forum->id}", $forum->id, $forum->cmid);
1061         $this->assertEventLegacyLogData($expected, $event);
1062         $this->assertEventContextNotUsed($event);
1064         $this->assertNotEmpty($event->get_name());
1065     }
1067     /**
1068      * Ensure user_report_viewed event validates that the reportmode is set.
1069      *
1070      * @expectedException        coding_exception
1071      * @expectedExceptionMessage The 'reportmode' value must be set in other.
1072      */
1073     public function test_user_report_viewed_reportmode_validation() {
1074         $user = $this->getDataGenerator()->create_user();
1075         $course = $this->getDataGenerator()->create_course();
1077         $params = array(
1078             'context' => context_course::instance($course->id),
1079             'relateduserid' => $user->id,
1080         );
1082         \mod_forum\event\user_report_viewed::create($params);
1083     }
1085     /**
1086      * Ensure user_report_viewed event validates that the contextlevel is correct.
1087      *
1088      * @expectedException        coding_exception
1089      * @expectedExceptionMessage Context level must be either CONTEXT_SYSTEM, CONTEXT_COURSE or CONTEXT_USER.
1090      */
1091     public function test_user_report_viewed_contextlevel_validation() {
1092         $user = $this->getDataGenerator()->create_user();
1093         $course = $this->getDataGenerator()->create_course();
1094         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
1096         $params = array(
1097             'context' => context_module::instance($forum->cmid),
1098             'other' => array('reportmode' => 'posts'),
1099             'relateduserid' => $user->id,
1100         );
1102         \mod_forum\event\user_report_viewed::create($params);
1103     }
1105     /**
1106      *  Ensure user_report_viewed event validates that the relateduserid is set.
1107      *
1108      * @expectedException        coding_exception
1109      * @expectedExceptionMessage The 'relateduserid' must be set.
1110      */
1111     public function test_user_report_viewed_relateduserid_validation() {
1113         $params = array(
1114             'context' => context_system::instance(),
1115             'other' => array('reportmode' => 'posts'),
1116         );
1118         \mod_forum\event\user_report_viewed::create($params);
1119     }
1121     /**
1122      * Test the user_report_viewed event.
1123      */
1124     public function test_user_report_viewed() {
1125         // Setup test data.
1126         $user = $this->getDataGenerator()->create_user();
1127         $course = $this->getDataGenerator()->create_course();
1128         $context = context_course::instance($course->id);
1130         $params = array(
1131             'context' => $context,
1132             'relateduserid' => $user->id,
1133             'other' => array('reportmode' => 'discussions'),
1134         );
1136         $event = \mod_forum\event\user_report_viewed::create($params);
1138         // Trigger and capture the event.
1139         $sink = $this->redirectEvents();
1140         $event->trigger();
1141         $events = $sink->get_events();
1142         $this->assertCount(1, $events);
1143         $event = reset($events);
1145         // Checking that the event contains the expected values.
1146         $this->assertInstanceOf('\mod_forum\event\user_report_viewed', $event);
1147         $this->assertEquals($context, $event->get_context());
1148         $expected = array($course->id, 'forum', 'user report',
1149             "user.php?id={$user->id}&amp;mode=discussions&amp;course={$course->id}", $user->id);
1150         $this->assertEventLegacyLogData($expected, $event);
1151         $this->assertEventContextNotUsed($event);
1153         $this->assertNotEmpty($event->get_name());
1154     }
1156     /**
1157      *  Ensure post_created event validates that the postid is set.
1158      */
1159     public function test_post_created_postid_validation() {
1160         $course = $this->getDataGenerator()->create_course();
1161         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
1162         $user = $this->getDataGenerator()->create_user();
1164         // Add a discussion.
1165         $record = array();
1166         $record['course'] = $course->id;
1167         $record['forum'] = $forum->id;
1168         $record['userid'] = $user->id;
1169         $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1171         $params = array(
1172             'context' => context_module::instance($forum->cmid),
1173             'other' => array('forumid' => $forum->id, 'forumtype' => $forum->type, 'discussionid' => $discussion->id)
1174         );
1176         \mod_forum\event\post_created::create($params);
1177     }
1179     /**
1180      * Ensure post_created event validates that the discussionid is set.
1181      *
1182      * @expectedException        coding_exception
1183      * @expectedExceptionMessage The 'discussionid' value must be set in other.
1184      */
1185     public function test_post_created_discussionid_validation() {
1186         $course = $this->getDataGenerator()->create_course();
1187         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
1188         $user = $this->getDataGenerator()->create_user();
1190         // Add a discussion.
1191         $record = array();
1192         $record['course'] = $course->id;
1193         $record['forum'] = $forum->id;
1194         $record['userid'] = $user->id;
1195         $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1197         // Add a post.
1198         $record = array();
1199         $record['discussion'] = $discussion->id;
1200         $record['userid'] = $user->id;
1201         $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
1203         $params = array(
1204             'context' => context_module::instance($forum->cmid),
1205             'objectid' => $post->id,
1206             'other' => array('forumid' => $forum->id, 'forumtype' => $forum->type)
1207         );
1209         \mod_forum\event\post_created::create($params);
1210     }
1212     /**
1213      *  Ensure post_created event validates that the forumid is set.
1214      *
1215      * @expectedException        coding_exception
1216      * @expectedExceptionMessage The 'forumid' value must be set in other.
1217      */
1218     public function test_post_created_forumid_validation() {
1219         $course = $this->getDataGenerator()->create_course();
1220         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
1221         $user = $this->getDataGenerator()->create_user();
1223         // Add a discussion.
1224         $record = array();
1225         $record['course'] = $course->id;
1226         $record['forum'] = $forum->id;
1227         $record['userid'] = $user->id;
1228         $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1230         // Add a post.
1231         $record = array();
1232         $record['discussion'] = $discussion->id;
1233         $record['userid'] = $user->id;
1234         $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
1236         $params = array(
1237             'context' => context_module::instance($forum->cmid),
1238             'objectid' => $post->id,
1239             'other' => array('discussionid' => $discussion->id, 'forumtype' => $forum->type)
1240         );
1242         \mod_forum\event\post_created::create($params);
1243     }
1245     /**
1246      * Ensure post_created event validates that the forumtype is set.
1247      *
1248      * @expectedException        coding_exception
1249      * @expectedExceptionMessage The 'forumtype' value must be set in other.
1250      */
1251     public function test_post_created_forumtype_validation() {
1252         $course = $this->getDataGenerator()->create_course();
1253         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
1254         $user = $this->getDataGenerator()->create_user();
1256         // Add a discussion.
1257         $record = array();
1258         $record['course'] = $course->id;
1259         $record['forum'] = $forum->id;
1260         $record['userid'] = $user->id;
1261         $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1263         // Add a post.
1264         $record = array();
1265         $record['discussion'] = $discussion->id;
1266         $record['userid'] = $user->id;
1267         $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
1269         $params = array(
1270             'context' => context_module::instance($forum->cmid),
1271             'objectid' => $post->id,
1272             'other' => array('discussionid' => $discussion->id, 'forumid' => $forum->id)
1273         );
1275         \mod_forum\event\post_created::create($params);
1276     }
1278     /**
1279      *  Ensure post_created event validates that the contextlevel is correct.
1280      *
1281      * @expectedException        coding_exception
1282      * @expectedExceptionMessage Context level must be CONTEXT_MODULE.
1283      */
1284     public function test_post_created_context_validation() {
1285         $course = $this->getDataGenerator()->create_course();
1286         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
1287         $user = $this->getDataGenerator()->create_user();
1289         // Add a discussion.
1290         $record = array();
1291         $record['course'] = $course->id;
1292         $record['forum'] = $forum->id;
1293         $record['userid'] = $user->id;
1294         $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1296         // Add a post.
1297         $record = array();
1298         $record['discussion'] = $discussion->id;
1299         $record['userid'] = $user->id;
1300         $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
1302         $params = array(
1303             'context' => context_system::instance(),
1304             'objectid' => $post->id,
1305             'other' => array('discussionid' => $discussion->id, 'forumid' => $forum->id, 'forumtype' => $forum->type)
1306         );
1308         \mod_forum\event\post_created::create($params);
1309     }
1311     /**
1312      * Test the post_created event.
1313      */
1314     public function test_post_created() {
1315         // Setup test data.
1316         $course = $this->getDataGenerator()->create_course();
1317         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
1318         $user = $this->getDataGenerator()->create_user();
1320         // Add a discussion.
1321         $record = array();
1322         $record['course'] = $course->id;
1323         $record['forum'] = $forum->id;
1324         $record['userid'] = $user->id;
1325         $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1327         // Add a post.
1328         $record = array();
1329         $record['discussion'] = $discussion->id;
1330         $record['userid'] = $user->id;
1331         $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
1333         $context = context_module::instance($forum->cmid);
1335         $params = array(
1336             'context' => $context,
1337             'objectid' => $post->id,
1338             'other' => array('discussionid' => $discussion->id, 'forumid' => $forum->id, 'forumtype' => $forum->type)
1339         );
1341         $event = \mod_forum\event\post_created::create($params);
1343         // Trigger and capturing the event.
1344         $sink = $this->redirectEvents();
1345         $event->trigger();
1346         $events = $sink->get_events();
1347         $this->assertCount(1, $events);
1348         $event = reset($events);
1350         // Checking that the event contains the expected values.
1351         $this->assertInstanceOf('\mod_forum\event\post_created', $event);
1352         $this->assertEquals($context, $event->get_context());
1353         $expected = array($course->id, 'forum', 'add post', "discuss.php?d={$discussion->id}#p{$post->id}",
1354             $forum->id, $forum->cmid);
1355         $this->assertEventLegacyLogData($expected, $event);
1356         $url = new \moodle_url('/mod/forum/discuss.php', array('d' => $discussion->id));
1357         $url->set_anchor('p'.$event->objectid);
1358         $this->assertEquals($url, $event->get_url());
1359         $this->assertEventContextNotUsed($event);
1361         $this->assertNotEmpty($event->get_name());
1362     }
1364     /**
1365      * Test the post_created event for a single discussion forum.
1366      */
1367     public function test_post_created_single() {
1368         // Setup test data.
1369         $course = $this->getDataGenerator()->create_course();
1370         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id, 'type' => 'single'));
1371         $user = $this->getDataGenerator()->create_user();
1373         // Add a discussion.
1374         $record = array();
1375         $record['course'] = $course->id;
1376         $record['forum'] = $forum->id;
1377         $record['userid'] = $user->id;
1378         $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1380         // Add a post.
1381         $record = array();
1382         $record['discussion'] = $discussion->id;
1383         $record['userid'] = $user->id;
1384         $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
1386         $context = context_module::instance($forum->cmid);
1388         $params = array(
1389             'context' => $context,
1390             'objectid' => $post->id,
1391             'other' => array('discussionid' => $discussion->id, 'forumid' => $forum->id, 'forumtype' => $forum->type)
1392         );
1394         $event = \mod_forum\event\post_created::create($params);
1396         // Trigger and capturing the event.
1397         $sink = $this->redirectEvents();
1398         $event->trigger();
1399         $events = $sink->get_events();
1400         $this->assertCount(1, $events);
1401         $event = reset($events);
1403         // Checking that the event contains the expected values.
1404         $this->assertInstanceOf('\mod_forum\event\post_created', $event);
1405         $this->assertEquals($context, $event->get_context());
1406         $expected = array($course->id, 'forum', 'add post', "view.php?f={$forum->id}#p{$post->id}",
1407             $forum->id, $forum->cmid);
1408         $this->assertEventLegacyLogData($expected, $event);
1409         $url = new \moodle_url('/mod/forum/view.php', array('f' => $forum->id));
1410         $url->set_anchor('p'.$event->objectid);
1411         $this->assertEquals($url, $event->get_url());
1412         $this->assertEventContextNotUsed($event);
1414         $this->assertNotEmpty($event->get_name());
1415     }
1417     /**
1418      *  Ensure post_deleted event validates that the postid is set.
1419      */
1420     public function test_post_deleted_postid_validation() {
1421         $course = $this->getDataGenerator()->create_course();
1422         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
1423         $user = $this->getDataGenerator()->create_user();
1425         // Add a discussion.
1426         $record = array();
1427         $record['course'] = $course->id;
1428         $record['forum'] = $forum->id;
1429         $record['userid'] = $user->id;
1430         $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1432         $params = array(
1433             'context' => context_module::instance($forum->cmid),
1434             'other' => array('forumid' => $forum->id, 'forumtype' => $forum->type, 'discussionid' => $discussion->id)
1435         );
1437         \mod_forum\event\post_deleted::create($params);
1438     }
1440     /**
1441      * Ensure post_deleted event validates that the discussionid is set.
1442      *
1443      * @expectedException        coding_exception
1444      * @expectedExceptionMessage The 'discussionid' value must be set in other.
1445      */
1446     public function test_post_deleted_discussionid_validation() {
1447         $course = $this->getDataGenerator()->create_course();
1448         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
1449         $user = $this->getDataGenerator()->create_user();
1451         // Add a discussion.
1452         $record = array();
1453         $record['course'] = $course->id;
1454         $record['forum'] = $forum->id;
1455         $record['userid'] = $user->id;
1456         $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1458         // Add a post.
1459         $record = array();
1460         $record['discussion'] = $discussion->id;
1461         $record['userid'] = $user->id;
1462         $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
1464         $params = array(
1465             'context' => context_module::instance($forum->cmid),
1466             'objectid' => $post->id,
1467             'other' => array('forumid' => $forum->id, 'forumtype' => $forum->type)
1468         );
1470         \mod_forum\event\post_deleted::create($params);
1471     }
1473     /**
1474      *  Ensure post_deleted event validates that the forumid is set.
1475      *
1476      * @expectedException        coding_exception
1477      * @expectedExceptionMessage The 'forumid' value must be set in other.
1478      */
1479     public function test_post_deleted_forumid_validation() {
1480         $course = $this->getDataGenerator()->create_course();
1481         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
1482         $user = $this->getDataGenerator()->create_user();
1484         // Add a discussion.
1485         $record = array();
1486         $record['course'] = $course->id;
1487         $record['forum'] = $forum->id;
1488         $record['userid'] = $user->id;
1489         $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1491         // Add a post.
1492         $record = array();
1493         $record['discussion'] = $discussion->id;
1494         $record['userid'] = $user->id;
1495         $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
1497         $params = array(
1498             'context' => context_module::instance($forum->cmid),
1499             'objectid' => $post->id,
1500             'other' => array('discussionid' => $discussion->id, 'forumtype' => $forum->type)
1501         );
1503         \mod_forum\event\post_deleted::create($params);
1504     }
1506     /**
1507      * Ensure post_deleted event validates that the forumtype is set.
1508      *
1509      * @expectedException        coding_exception
1510      * @expectedExceptionMessage The 'forumtype' value must be set in other.
1511      */
1512     public function test_post_deleted_forumtype_validation() {
1513         $course = $this->getDataGenerator()->create_course();
1514         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
1515         $user = $this->getDataGenerator()->create_user();
1517         // Add a discussion.
1518         $record = array();
1519         $record['course'] = $course->id;
1520         $record['forum'] = $forum->id;
1521         $record['userid'] = $user->id;
1522         $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1524         // Add a post.
1525         $record = array();
1526         $record['discussion'] = $discussion->id;
1527         $record['userid'] = $user->id;
1528         $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
1530         $params = array(
1531             'context' => context_module::instance($forum->cmid),
1532             'objectid' => $post->id,
1533             'other' => array('discussionid' => $discussion->id, 'forumid' => $forum->id)
1534         );
1536         \mod_forum\event\post_deleted::create($params);
1537     }
1539     /**
1540      *  Ensure post_deleted event validates that the contextlevel is correct.
1541      *
1542      * @expectedException        coding_exception
1543      * @expectedExceptionMessage Context level must be CONTEXT_MODULE.
1544      */
1545     public function test_post_deleted_context_validation() {
1546         $course = $this->getDataGenerator()->create_course();
1547         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
1548         $user = $this->getDataGenerator()->create_user();
1550         // Add a discussion.
1551         $record = array();
1552         $record['course'] = $course->id;
1553         $record['forum'] = $forum->id;
1554         $record['userid'] = $user->id;
1555         $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1557         // Add a post.
1558         $record = array();
1559         $record['discussion'] = $discussion->id;
1560         $record['userid'] = $user->id;
1561         $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
1563         $params = array(
1564             'context' => context_system::instance(),
1565             'objectid' => $post->id,
1566             'other' => array('discussionid' => $discussion->id, 'forumid' => $forum->id, 'forumtype' => $forum->type)
1567         );
1569         \mod_forum\event\post_deleted::create($params);
1570     }
1572     /**
1573      * Test post_deleted event.
1574      */
1575     public function test_post_deleted() {
1576         global $DB;
1578         // Setup test data.
1579         $course = $this->getDataGenerator()->create_course();
1580         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
1581         $user = $this->getDataGenerator()->create_user();
1582         $cm = get_coursemodule_from_instance('forum', $forum->id, $forum->course);
1584         // Add a discussion.
1585         $record = array();
1586         $record['course'] = $course->id;
1587         $record['forum'] = $forum->id;
1588         $record['userid'] = $user->id;
1589         $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1591         // When creating a discussion we also create a post, so get the post.
1592         $discussionpost = $DB->get_records('forum_posts');
1593         // Will only be one here.
1594         $discussionpost = reset($discussionpost);
1596         // Add a few posts.
1597         $record = array();
1598         $record['discussion'] = $discussion->id;
1599         $record['userid'] = $user->id;
1600         $posts = array();
1601         $posts[$discussionpost->id] = $discussionpost;
1602         for ($i = 0; $i < 3; $i++) {
1603             $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
1604             $posts[$post->id] = $post;
1605         }
1607         // Delete the last post and capture the event.
1608         $lastpost = end($posts);
1609         $sink = $this->redirectEvents();
1610         forum_delete_post($lastpost, true, $course, $cm, $forum);
1611         $events = $sink->get_events();
1612         $this->assertCount(1, $events);
1613         $event = reset($events);
1615         // Check that the events contain the expected values.
1616         $this->assertInstanceOf('\mod_forum\event\post_deleted', $event);
1617         $this->assertEquals(context_module::instance($forum->cmid), $event->get_context());
1618         $expected = array($course->id, 'forum', 'delete post', "discuss.php?d={$discussion->id}", $lastpost->id, $forum->cmid);
1619         $this->assertEventLegacyLogData($expected, $event);
1620         $url = new \moodle_url('/mod/forum/discuss.php', array('d' => $discussion->id));
1621         $this->assertEquals($url, $event->get_url());
1622         $this->assertEventContextNotUsed($event);
1623         $this->assertNotEmpty($event->get_name());
1625         // Delete the whole discussion and capture the events.
1626         $sink = $this->redirectEvents();
1627         forum_delete_discussion($discussion, true, $course, $cm, $forum);
1628         $events = $sink->get_events();
1629         // We will have 3 events. One for the discussion (creating a discussion creates a post), and two for the posts.
1630         $this->assertCount(3, $events);
1632         // Loop through the events and check they are valid.
1633         foreach ($events as $event) {
1634             $post = $posts[$event->objectid];
1636             // Check that the event contains the expected values.
1637             $this->assertInstanceOf('\mod_forum\event\post_deleted', $event);
1638             $this->assertEquals(context_module::instance($forum->cmid), $event->get_context());
1639             $expected = array($course->id, 'forum', 'delete post', "discuss.php?d={$discussion->id}", $post->id, $forum->cmid);
1640             $this->assertEventLegacyLogData($expected, $event);
1641             $url = new \moodle_url('/mod/forum/discuss.php', array('d' => $discussion->id));
1642             $this->assertEquals($url, $event->get_url());
1643             $this->assertEventContextNotUsed($event);
1644             $this->assertNotEmpty($event->get_name());
1645         }
1646     }
1648     /**
1649      * Test post_deleted event for a single discussion forum.
1650      */
1651     public function test_post_deleted_single() {
1652         // Setup test data.
1653         $course = $this->getDataGenerator()->create_course();
1654         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id, 'type' => 'single'));
1655         $user = $this->getDataGenerator()->create_user();
1657         // Add a discussion.
1658         $record = array();
1659         $record['course'] = $course->id;
1660         $record['forum'] = $forum->id;
1661         $record['userid'] = $user->id;
1662         $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1664         // Add a post.
1665         $record = array();
1666         $record['discussion'] = $discussion->id;
1667         $record['userid'] = $user->id;
1668         $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
1670         $context = context_module::instance($forum->cmid);
1672         $params = array(
1673             'context' => $context,
1674             'objectid' => $post->id,
1675             'other' => array('discussionid' => $discussion->id, 'forumid' => $forum->id, 'forumtype' => $forum->type)
1676         );
1678         $event = \mod_forum\event\post_deleted::create($params);
1680         // Trigger and capture the event.
1681         $sink = $this->redirectEvents();
1682         $event->trigger();
1683         $events = $sink->get_events();
1684         $this->assertCount(1, $events);
1685         $event = reset($events);
1687         // Checking that the event contains the expected values.
1688         $this->assertInstanceOf('\mod_forum\event\post_deleted', $event);
1689         $this->assertEquals($context, $event->get_context());
1690         $expected = array($course->id, 'forum', 'delete post', "view.php?f={$forum->id}", $post->id, $forum->cmid);
1691         $this->assertEventLegacyLogData($expected, $event);
1692         $url = new \moodle_url('/mod/forum/view.php', array('f' => $forum->id));
1693         $this->assertEquals($url, $event->get_url());
1694         $this->assertEventContextNotUsed($event);
1696         $this->assertNotEmpty($event->get_name());
1697     }
1699     /**
1700      * Ensure post_updated event validates that the discussionid is set.
1701      *
1702      * @expectedException        coding_exception
1703      * @expectedExceptionMessage The 'discussionid' value must be set in other.
1704      */
1705     public function test_post_updated_discussionid_validation() {
1706         $course = $this->getDataGenerator()->create_course();
1707         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
1708         $user = $this->getDataGenerator()->create_user();
1710         // Add a discussion.
1711         $record = array();
1712         $record['course'] = $course->id;
1713         $record['forum'] = $forum->id;
1714         $record['userid'] = $user->id;
1715         $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1717         // Add a post.
1718         $record = array();
1719         $record['discussion'] = $discussion->id;
1720         $record['userid'] = $user->id;
1721         $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
1723         $params = array(
1724             'context' => context_module::instance($forum->cmid),
1725             'objectid' => $post->id,
1726             'other' => array('forumid' => $forum->id, 'forumtype' => $forum->type)
1727         );
1729         \mod_forum\event\post_updated::create($params);
1730     }
1732     /**
1733      * Ensure post_updated event validates that the forumid is set.
1734      *
1735      * @expectedException        coding_exception
1736      * @expectedExceptionMessage The 'forumid' value must be set in other.
1737      */
1738     public function test_post_updated_forumid_validation() {
1739         $course = $this->getDataGenerator()->create_course();
1740         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
1741         $user = $this->getDataGenerator()->create_user();
1743         // Add a discussion.
1744         $record = array();
1745         $record['course'] = $course->id;
1746         $record['forum'] = $forum->id;
1747         $record['userid'] = $user->id;
1748         $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1750         // Add a post.
1751         $record = array();
1752         $record['discussion'] = $discussion->id;
1753         $record['userid'] = $user->id;
1754         $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
1756         $params = array(
1757             'context' => context_module::instance($forum->cmid),
1758             'objectid' => $post->id,
1759             'other' => array('discussionid' => $discussion->id, 'forumtype' => $forum->type)
1760         );
1762         \mod_forum\event\post_updated::create($params);
1763     }
1765     /**
1766      * Ensure post_updated event validates that the forumtype is set.
1767      *
1768      * @expectedException        coding_exception
1769      * @expectedExceptionMessage The 'forumtype' value must be set in other.
1770      */
1771     public function test_post_updated_forumtype_validation() {
1772         $course = $this->getDataGenerator()->create_course();
1773         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
1774         $user = $this->getDataGenerator()->create_user();
1776         // Add a discussion.
1777         $record = array();
1778         $record['course'] = $course->id;
1779         $record['forum'] = $forum->id;
1780         $record['userid'] = $user->id;
1781         $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1783         // Add a post.
1784         $record = array();
1785         $record['discussion'] = $discussion->id;
1786         $record['userid'] = $user->id;
1787         $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
1789         $params = array(
1790             'context' => context_module::instance($forum->cmid),
1791             'objectid' => $post->id,
1792             'other' => array('discussionid' => $discussion->id, 'forumid' => $forum->id)
1793         );
1795         \mod_forum\event\post_updated::create($params);
1796     }
1798     /**
1799      *  Ensure post_updated event validates that the contextlevel is correct.
1800      *
1801      * @expectedException        coding_exception
1802      * @expectedExceptionMessage Context level must be CONTEXT_MODULE.
1803      */
1804     public function test_post_updated_context_validation() {
1805         $course = $this->getDataGenerator()->create_course();
1806         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
1807         $user = $this->getDataGenerator()->create_user();
1809         // Add a discussion.
1810         $record = array();
1811         $record['course'] = $course->id;
1812         $record['forum'] = $forum->id;
1813         $record['userid'] = $user->id;
1814         $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1816         // Add a post.
1817         $record = array();
1818         $record['discussion'] = $discussion->id;
1819         $record['userid'] = $user->id;
1820         $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
1822         $params = array(
1823             'context' => context_system::instance(),
1824             'objectid' => $post->id,
1825             'other' => array('discussionid' => $discussion->id, 'forumid' => $forum->id, 'forumtype' => $forum->type)
1826         );
1828         \mod_forum\event\post_updated::create($params);
1829     }
1831     /**
1832      * Test post_updated event.
1833      */
1834     public function test_post_updated() {
1835         // Setup test data.
1836         $course = $this->getDataGenerator()->create_course();
1837         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
1838         $user = $this->getDataGenerator()->create_user();
1840         // Add a discussion.
1841         $record = array();
1842         $record['course'] = $course->id;
1843         $record['forum'] = $forum->id;
1844         $record['userid'] = $user->id;
1845         $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1847         // Add a post.
1848         $record = array();
1849         $record['discussion'] = $discussion->id;
1850         $record['userid'] = $user->id;
1851         $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
1853         $context = context_module::instance($forum->cmid);
1855         $params = array(
1856             'context' => $context,
1857             'objectid' => $post->id,
1858             'other' => array('discussionid' => $discussion->id, 'forumid' => $forum->id, 'forumtype' => $forum->type)
1859         );
1861         $event = \mod_forum\event\post_updated::create($params);
1863         // Trigger and capturing the event.
1864         $sink = $this->redirectEvents();
1865         $event->trigger();
1866         $events = $sink->get_events();
1867         $this->assertCount(1, $events);
1868         $event = reset($events);
1870         // Checking that the event contains the expected values.
1871         $this->assertInstanceOf('\mod_forum\event\post_updated', $event);
1872         $this->assertEquals($context, $event->get_context());
1873         $expected = array($course->id, 'forum', 'update post', "discuss.php?d={$discussion->id}#p{$post->id}",
1874             $post->id, $forum->cmid);
1875         $this->assertEventLegacyLogData($expected, $event);
1876         $url = new \moodle_url('/mod/forum/discuss.php', array('d' => $discussion->id));
1877         $url->set_anchor('p'.$event->objectid);
1878         $this->assertEquals($url, $event->get_url());
1879         $this->assertEventContextNotUsed($event);
1881         $this->assertNotEmpty($event->get_name());
1882     }
1884     /**
1885      * Test post_updated event.
1886      */
1887     public function test_post_updated_single() {
1888         // Setup test data.
1889         $course = $this->getDataGenerator()->create_course();
1890         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id, 'type' => 'single'));
1891         $user = $this->getDataGenerator()->create_user();
1893         // Add a discussion.
1894         $record = array();
1895         $record['course'] = $course->id;
1896         $record['forum'] = $forum->id;
1897         $record['userid'] = $user->id;
1898         $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1900         // Add a post.
1901         $record = array();
1902         $record['discussion'] = $discussion->id;
1903         $record['userid'] = $user->id;
1904         $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
1906         $context = context_module::instance($forum->cmid);
1908         $params = array(
1909             'context' => $context,
1910             'objectid' => $post->id,
1911             'other' => array('discussionid' => $discussion->id, 'forumid' => $forum->id, 'forumtype' => $forum->type)
1912         );
1914         $event = \mod_forum\event\post_updated::create($params);
1916         // Trigger and capturing the event.
1917         $sink = $this->redirectEvents();
1918         $event->trigger();
1919         $events = $sink->get_events();
1920         $this->assertCount(1, $events);
1921         $event = reset($events);
1923         // Checking that the event contains the expected values.
1924         $this->assertInstanceOf('\mod_forum\event\post_updated', $event);
1925         $this->assertEquals($context, $event->get_context());
1926         $expected = array($course->id, 'forum', 'update post', "view.php?f={$forum->id}#p{$post->id}",
1927             $post->id, $forum->cmid);
1928         $this->assertEventLegacyLogData($expected, $event);
1929         $url = new \moodle_url('/mod/forum/view.php', array('f' => $forum->id));
1930         $url->set_anchor('p'.$post->id);
1931         $this->assertEquals($url, $event->get_url());
1932         $this->assertEventContextNotUsed($event);
1934         $this->assertNotEmpty($event->get_name());
1935     }
1937     /**
1938      * Test discussion_subscription_created event.
1939      */
1940     public function test_discussion_subscription_created() {
1941         global $CFG;
1942         require_once($CFG->dirroot . '/mod/forum/lib.php');
1944         // Setup test data.
1945         $course = $this->getDataGenerator()->create_course();
1946         $user = $this->getDataGenerator()->create_user();
1947         $this->getDataGenerator()->enrol_user($user->id, $course->id);
1949         $options = array('course' => $course->id, 'forcesubscribe' => FORUM_CHOOSESUBSCRIBE);
1950         $forum = $this->getDataGenerator()->create_module('forum', $options);
1952         // Add a discussion.
1953         $record = array();
1954         $record['course'] = $course->id;
1955         $record['forum'] = $forum->id;
1956         $record['userid'] = $user->id;
1957         $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1959         // Add a post.
1960         $record = array();
1961         $record['discussion'] = $discussion->id;
1962         $record['userid'] = $user->id;
1963         $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
1965         // Trigger and capturing the event.
1966         $sink = $this->redirectEvents();
1968         // Trigger the event by subscribing the user to the forum discussion.
1969         \mod_forum\subscriptions::subscribe_user_to_discussion($user->id, $discussion);
1971         $events = $sink->get_events();
1972         $this->assertCount(1, $events);
1973         $event = reset($events);
1975         // Checking that the event contains the expected values.
1976         $this->assertInstanceOf('\mod_forum\event\discussion_subscription_created', $event);
1979         $cm = get_coursemodule_from_instance('forum', $discussion->forum);
1980         $context = \context_module::instance($cm->id);
1981         $this->assertEquals($context, $event->get_context());
1983         $url = new \moodle_url('/mod/forum/subscribe.php', array(
1984             'id' => $forum->id,
1985             'd' => $discussion->id
1986         ));
1988         $this->assertEquals($url, $event->get_url());
1989         $this->assertEventContextNotUsed($event);
1990         $this->assertNotEmpty($event->get_name());
1991     }
1993     /**
1994      * Test validation of discussion_subscription_created event.
1995      */
1996     public function test_discussion_subscription_created_validation() {
1997         global $CFG, $DB;
1998         require_once($CFG->dirroot . '/mod/forum/lib.php');
2000         // Setup test data.
2001         $course = $this->getDataGenerator()->create_course();
2002         $user = $this->getDataGenerator()->create_user();
2003         $this->getDataGenerator()->enrol_user($user->id, $course->id);
2005         $options = array('course' => $course->id, 'forcesubscribe' => FORUM_CHOOSESUBSCRIBE);
2006         $forum = $this->getDataGenerator()->create_module('forum', $options);
2008         // Add a discussion.
2009         $record = array();
2010         $record['course'] = $course->id;
2011         $record['forum'] = $forum->id;
2012         $record['userid'] = $user->id;
2013         $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
2015         // Add a post.
2016         $record = array();
2017         $record['discussion'] = $discussion->id;
2018         $record['userid'] = $user->id;
2019         $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
2021         // The user is not subscribed to the forum. Insert a new discussion subscription.
2022         $subscription = new \stdClass();
2023         $subscription->userid  = $user->id;
2024         $subscription->forum = $forum->id;
2025         $subscription->discussion = $discussion->id;
2026         $subscription->preference = time();
2028         $subscription->id = $DB->insert_record('forum_discussion_subs', $subscription);
2030         $context = context_module::instance($forum->cmid);
2032         $params = array(
2033             'context' => $context,
2034             'objectid' => $subscription->id,
2035             'relateduserid' => $user->id,
2036             'other' => array(
2037                 'forumid' => $forum->id,
2038                 'discussion' => $discussion->id,
2039             )
2040         );
2042         $event = \mod_forum\event\discussion_subscription_created::create($params);
2044         // Trigger and capturing the event.
2045         $sink = $this->redirectEvents();
2046         $event->trigger();
2047         $events = $sink->get_events();
2048         $this->assertCount(1, $events);
2049         $event = reset($events);
2050     }
2052     /**
2053      * Test contextlevel validation of discussion_subscription_created event.
2054      *
2055      * @expectedException        coding_exception
2056      * @expectedExceptionMessage Context level must be CONTEXT_MODULE.
2057      */
2058     public function test_discussion_subscription_created_validation_contextlevel() {
2059         global $CFG, $DB;
2060         require_once($CFG->dirroot . '/mod/forum/lib.php');
2062         // Setup test data.
2063         $course = $this->getDataGenerator()->create_course();
2064         $user = $this->getDataGenerator()->create_user();
2065         $this->getDataGenerator()->enrol_user($user->id, $course->id);
2067         $options = array('course' => $course->id, 'forcesubscribe' => FORUM_CHOOSESUBSCRIBE);
2068         $forum = $this->getDataGenerator()->create_module('forum', $options);
2070         // Add a discussion.
2071         $record = array();
2072         $record['course'] = $course->id;
2073         $record['forum'] = $forum->id;
2074         $record['userid'] = $user->id;
2075         $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
2077         // Add a post.
2078         $record = array();
2079         $record['discussion'] = $discussion->id;
2080         $record['userid'] = $user->id;
2081         $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
2083         // The user is not subscribed to the forum. Insert a new discussion subscription.
2084         $subscription = new \stdClass();
2085         $subscription->userid  = $user->id;
2086         $subscription->forum = $forum->id;
2087         $subscription->discussion = $discussion->id;
2088         $subscription->preference = time();
2090         $subscription->id = $DB->insert_record('forum_discussion_subs', $subscription);
2092         $context = context_module::instance($forum->cmid);
2094         $params = array(
2095             'context' => \context_course::instance($course->id),
2096             'objectid' => $subscription->id,
2097             'relateduserid' => $user->id,
2098             'other' => array(
2099                 'forumid' => $forum->id,
2100                 'discussion' => $discussion->id,
2101             )
2102         );
2104         // Without an invalid context.
2105         \mod_forum\event\discussion_subscription_created::create($params);
2106     }
2108     /**
2109      * Test discussion validation of discussion_subscription_created event.
2110      *
2111      * @expectedException        coding_exception
2112      * @expectedExceptionMessage The 'discussion' value must be set in other.
2113      */
2114     public function test_discussion_subscription_created_validation_discussion() {
2115         global $CFG, $DB;
2116         require_once($CFG->dirroot . '/mod/forum/lib.php');
2118         // Setup test data.
2119         $course = $this->getDataGenerator()->create_course();
2120         $user = $this->getDataGenerator()->create_user();
2121         $this->getDataGenerator()->enrol_user($user->id, $course->id);
2123         $options = array('course' => $course->id, 'forcesubscribe' => FORUM_CHOOSESUBSCRIBE);
2124         $forum = $this->getDataGenerator()->create_module('forum', $options);
2126         // Add a discussion.
2127         $record = array();
2128         $record['course'] = $course->id;
2129         $record['forum'] = $forum->id;
2130         $record['userid'] = $user->id;
2131         $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
2133         // Add a post.
2134         $record = array();
2135         $record['discussion'] = $discussion->id;
2136         $record['userid'] = $user->id;
2137         $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
2139         // The user is not subscribed to the forum. Insert a new discussion subscription.
2140         $subscription = new \stdClass();
2141         $subscription->userid  = $user->id;
2142         $subscription->forum = $forum->id;
2143         $subscription->discussion = $discussion->id;
2144         $subscription->preference = time();
2146         $subscription->id = $DB->insert_record('forum_discussion_subs', $subscription);
2148         // Without the discussion.
2149         $params = array(
2150             'context' => context_module::instance($forum->cmid),
2151             'objectid' => $subscription->id,
2152             'relateduserid' => $user->id,
2153             'other' => array(
2154                 'forumid' => $forum->id,
2155             )
2156         );
2158         \mod_forum\event\discussion_subscription_created::create($params);
2159     }
2161     /**
2162      * Test forumid validation of discussion_subscription_created event.
2163      *
2164      * @expectedException        coding_exception
2165      * @expectedExceptionMessage The 'forumid' value must be set in other.
2166      */
2167     public function test_discussion_subscription_created_validation_forumid() {
2168         global $CFG, $DB;
2169         require_once($CFG->dirroot . '/mod/forum/lib.php');
2171         // Setup test data.
2172         $course = $this->getDataGenerator()->create_course();
2173         $user = $this->getDataGenerator()->create_user();
2174         $this->getDataGenerator()->enrol_user($user->id, $course->id);
2176         $options = array('course' => $course->id, 'forcesubscribe' => FORUM_CHOOSESUBSCRIBE);
2177         $forum = $this->getDataGenerator()->create_module('forum', $options);
2179         // Add a discussion.
2180         $record = array();
2181         $record['course'] = $course->id;
2182         $record['forum'] = $forum->id;
2183         $record['userid'] = $user->id;
2184         $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
2186         // Add a post.
2187         $record = array();
2188         $record['discussion'] = $discussion->id;
2189         $record['userid'] = $user->id;
2190         $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
2192         // The user is not subscribed to the forum. Insert a new discussion subscription.
2193         $subscription = new \stdClass();
2194         $subscription->userid  = $user->id;
2195         $subscription->forum = $forum->id;
2196         $subscription->discussion = $discussion->id;
2197         $subscription->preference = time();
2199         $subscription->id = $DB->insert_record('forum_discussion_subs', $subscription);
2201         // Without the forumid.
2202         $params = array(
2203             'context' => context_module::instance($forum->cmid),
2204             'objectid' => $subscription->id,
2205             'relateduserid' => $user->id,
2206             'other' => array(
2207                 'discussion' => $discussion->id,
2208             )
2209         );
2211         \mod_forum\event\discussion_subscription_created::create($params);
2212     }
2214     /**
2215      * Test relateduserid validation of discussion_subscription_created event.
2216      *
2217      * @expectedException        coding_exception
2218      * @expectedExceptionMessage The 'relateduserid' must be set.
2219      */
2220     public function test_discussion_subscription_created_validation_relateduserid() {
2221         global $CFG, $DB;
2222         require_once($CFG->dirroot . '/mod/forum/lib.php');
2224         // Setup test data.
2225         $course = $this->getDataGenerator()->create_course();
2226         $user = $this->getDataGenerator()->create_user();
2227         $this->getDataGenerator()->enrol_user($user->id, $course->id);
2229         $options = array('course' => $course->id, 'forcesubscribe' => FORUM_CHOOSESUBSCRIBE);
2230         $forum = $this->getDataGenerator()->create_module('forum', $options);
2232         // Add a discussion.
2233         $record = array();
2234         $record['course'] = $course->id;
2235         $record['forum'] = $forum->id;
2236         $record['userid'] = $user->id;
2237         $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
2239         // Add a post.
2240         $record = array();
2241         $record['discussion'] = $discussion->id;
2242         $record['userid'] = $user->id;
2243         $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
2245         // The user is not subscribed to the forum. Insert a new discussion subscription.
2246         $subscription = new \stdClass();
2247         $subscription->userid  = $user->id;
2248         $subscription->forum = $forum->id;
2249         $subscription->discussion = $discussion->id;
2250         $subscription->preference = time();
2252         $subscription->id = $DB->insert_record('forum_discussion_subs', $subscription);
2254         $context = context_module::instance($forum->cmid);
2256         // Without the relateduserid.
2257         $params = array(
2258             'context' => context_module::instance($forum->cmid),
2259             'objectid' => $subscription->id,
2260             'other' => array(
2261                 'forumid' => $forum->id,
2262                 'discussion' => $discussion->id,
2263             )
2264         );
2266         \mod_forum\event\discussion_subscription_created::create($params);
2267     }
2269     /**
2270      * Test discussion_subscription_deleted event.
2271      */
2272     public function test_discussion_subscription_deleted() {
2273         global $CFG;
2274         require_once($CFG->dirroot . '/mod/forum/lib.php');
2276         // Setup test data.
2277         $course = $this->getDataGenerator()->create_course();
2278         $user = $this->getDataGenerator()->create_user();
2279         $this->getDataGenerator()->enrol_user($user->id, $course->id);
2281         $options = array('course' => $course->id, 'forcesubscribe' => FORUM_INITIALSUBSCRIBE);
2282         $forum = $this->getDataGenerator()->create_module('forum', $options);
2284         // Add a discussion.
2285         $record = array();
2286         $record['course'] = $course->id;
2287         $record['forum'] = $forum->id;
2288         $record['userid'] = $user->id;
2289         $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
2291         // Add a post.
2292         $record = array();
2293         $record['discussion'] = $discussion->id;
2294         $record['userid'] = $user->id;
2295         $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
2297         // Trigger and capturing the event.
2298         $sink = $this->redirectEvents();
2300         // Trigger the event by unsubscribing the user to the forum discussion.
2301         \mod_forum\subscriptions::unsubscribe_user_from_discussion($user->id, $discussion);
2303         $events = $sink->get_events();
2304         $this->assertCount(1, $events);
2305         $event = reset($events);
2307         // Checking that the event contains the expected values.
2308         $this->assertInstanceOf('\mod_forum\event\discussion_subscription_deleted', $event);
2311         $cm = get_coursemodule_from_instance('forum', $discussion->forum);
2312         $context = \context_module::instance($cm->id);
2313         $this->assertEquals($context, $event->get_context());
2315         $url = new \moodle_url('/mod/forum/subscribe.php', array(
2316             'id' => $forum->id,
2317             'd' => $discussion->id
2318         ));
2320         $this->assertEquals($url, $event->get_url());
2321         $this->assertEventContextNotUsed($event);
2322         $this->assertNotEmpty($event->get_name());
2323     }
2325     /**
2326      * Test validation of discussion_subscription_deleted event.
2327      */
2328     public function test_discussion_subscription_deleted_validation() {
2329         global $CFG, $DB;
2330         require_once($CFG->dirroot . '/mod/forum/lib.php');
2332         // Setup test data.
2333         $course = $this->getDataGenerator()->create_course();
2334         $user = $this->getDataGenerator()->create_user();
2335         $this->getDataGenerator()->enrol_user($user->id, $course->id);
2337         $options = array('course' => $course->id, 'forcesubscribe' => FORUM_INITIALSUBSCRIBE);
2338         $forum = $this->getDataGenerator()->create_module('forum', $options);
2340         // Add a discussion.
2341         $record = array();
2342         $record['course'] = $course->id;
2343         $record['forum'] = $forum->id;
2344         $record['userid'] = $user->id;
2345         $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
2347         // Add a post.
2348         $record = array();
2349         $record['discussion'] = $discussion->id;
2350         $record['userid'] = $user->id;
2351         $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
2353         // The user is not subscribed to the forum. Insert a new discussion subscription.
2354         $subscription = new \stdClass();
2355         $subscription->userid  = $user->id;
2356         $subscription->forum = $forum->id;
2357         $subscription->discussion = $discussion->id;
2358         $subscription->preference = \mod_forum\subscriptions::FORUM_DISCUSSION_UNSUBSCRIBED;
2360         $subscription->id = $DB->insert_record('forum_discussion_subs', $subscription);
2362         $context = context_module::instance($forum->cmid);
2364         $params = array(
2365             'context' => $context,
2366             'objectid' => $subscription->id,
2367             'relateduserid' => $user->id,
2368             'other' => array(
2369                 'forumid' => $forum->id,
2370                 'discussion' => $discussion->id,
2371             )
2372         );
2374         $event = \mod_forum\event\discussion_subscription_deleted::create($params);
2376         // Trigger and capturing the event.
2377         $sink = $this->redirectEvents();
2378         $event->trigger();
2379         $events = $sink->get_events();
2380         $this->assertCount(1, $events);
2381         $event = reset($events);
2383         // Without an invalid context.
2384         $params['context'] = \context_course::instance($course->id);
2385         $this->expectException('coding_exception');
2386         $this->expectExceptionMessage('Context level must be CONTEXT_MODULE.');
2387         \mod_forum\event\discussion_deleted::create($params);
2389         // Without the discussion.
2390         unset($params['discussion']);
2391         $this->expectException('coding_exception');
2392         $this->expectExceptionMessage('The \'discussion\' value must be set in other.');
2393         \mod_forum\event\discussion_deleted::create($params);
2395         // Without the forumid.
2396         unset($params['forumid']);
2397         $this->expectException('coding_exception');
2398         $this->expectExceptionMessage('The \'forumid\' value must be set in other.');
2399         \mod_forum\event\discussion_deleted::create($params);
2401         // Without the relateduserid.
2402         unset($params['relateduserid']);
2403         $this->expectException('coding_exception');
2404         $this->expectExceptionMessage('The \'relateduserid\' value must be set in other.');
2405         \mod_forum\event\discussion_deleted::create($params);
2406     }
2408     /**
2409      * Test contextlevel validation of discussion_subscription_deleted event.
2410      *
2411      * @expectedException        coding_exception
2412      * @expectedExceptionMessage Context level must be CONTEXT_MODULE.
2413      */
2414     public function test_discussion_subscription_deleted_validation_contextlevel() {
2415         global $CFG, $DB;
2416         require_once($CFG->dirroot . '/mod/forum/lib.php');
2418         // Setup test data.
2419         $course = $this->getDataGenerator()->create_course();
2420         $user = $this->getDataGenerator()->create_user();
2421         $this->getDataGenerator()->enrol_user($user->id, $course->id);
2423         $options = array('course' => $course->id, 'forcesubscribe' => FORUM_CHOOSESUBSCRIBE);
2424         $forum = $this->getDataGenerator()->create_module('forum', $options);
2426         // Add a discussion.
2427         $record = array();
2428         $record['course'] = $course->id;
2429         $record['forum'] = $forum->id;
2430         $record['userid'] = $user->id;
2431         $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
2433         // Add a post.
2434         $record = array();
2435         $record['discussion'] = $discussion->id;
2436         $record['userid'] = $user->id;
2437         $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
2439         // The user is not subscribed to the forum. Insert a new discussion subscription.
2440         $subscription = new \stdClass();
2441         $subscription->userid  = $user->id;
2442         $subscription->forum = $forum->id;
2443         $subscription->discussion = $discussion->id;
2444         $subscription->preference = time();
2446         $subscription->id = $DB->insert_record('forum_discussion_subs', $subscription);
2448         $context = context_module::instance($forum->cmid);
2450         $params = array(
2451             'context' => \context_course::instance($course->id),
2452             'objectid' => $subscription->id,
2453             'relateduserid' => $user->id,
2454             'other' => array(
2455                 'forumid' => $forum->id,
2456                 'discussion' => $discussion->id,
2457             )
2458         );
2460         // Without an invalid context.
2461         \mod_forum\event\discussion_subscription_deleted::create($params);
2462     }
2464     /**
2465      * Test discussion validation of discussion_subscription_deleted event.
2466      *
2467      * @expectedException        coding_exception
2468      * @expectedExceptionMessage The 'discussion' value must be set in other.
2469      */
2470     public function test_discussion_subscription_deleted_validation_discussion() {
2471         global $CFG, $DB;
2472         require_once($CFG->dirroot . '/mod/forum/lib.php');
2474         // Setup test data.
2475         $course = $this->getDataGenerator()->create_course();
2476         $user = $this->getDataGenerator()->create_user();
2477         $this->getDataGenerator()->enrol_user($user->id, $course->id);
2479         $options = array('course' => $course->id, 'forcesubscribe' => FORUM_CHOOSESUBSCRIBE);
2480         $forum = $this->getDataGenerator()->create_module('forum', $options);
2482         // Add a discussion.
2483         $record = array();
2484         $record['course'] = $course->id;
2485         $record['forum'] = $forum->id;
2486         $record['userid'] = $user->id;
2487         $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
2489         // Add a post.
2490         $record = array();
2491         $record['discussion'] = $discussion->id;
2492         $record['userid'] = $user->id;
2493         $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
2495         // The user is not subscribed to the forum. Insert a new discussion subscription.
2496         $subscription = new \stdClass();
2497         $subscription->userid  = $user->id;
2498         $subscription->forum = $forum->id;
2499         $subscription->discussion = $discussion->id;
2500         $subscription->preference = time();
2502         $subscription->id = $DB->insert_record('forum_discussion_subs', $subscription);
2504         // Without the discussion.
2505         $params = array(
2506             'context' => context_module::instance($forum->cmid),
2507             'objectid' => $subscription->id,
2508             'relateduserid' => $user->id,
2509             'other' => array(
2510                 'forumid' => $forum->id,
2511             )
2512         );
2514         \mod_forum\event\discussion_subscription_deleted::create($params);
2515     }
2517     /**
2518      * Test forumid validation of discussion_subscription_deleted event.
2519      *
2520      * @expectedException        coding_exception
2521      * @expectedExceptionMessage The 'forumid' value must be set in other.
2522      */
2523     public function test_discussion_subscription_deleted_validation_forumid() {
2524         global $CFG, $DB;
2525         require_once($CFG->dirroot . '/mod/forum/lib.php');
2527         // Setup test data.
2528         $course = $this->getDataGenerator()->create_course();
2529         $user = $this->getDataGenerator()->create_user();
2530         $this->getDataGenerator()->enrol_user($user->id, $course->id);
2532         $options = array('course' => $course->id, 'forcesubscribe' => FORUM_CHOOSESUBSCRIBE);
2533         $forum = $this->getDataGenerator()->create_module('forum', $options);
2535         // Add a discussion.
2536         $record = array();
2537         $record['course'] = $course->id;
2538         $record['forum'] = $forum->id;
2539         $record['userid'] = $user->id;
2540         $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
2542         // Add a post.
2543         $record = array();
2544         $record['discussion'] = $discussion->id;
2545         $record['userid'] = $user->id;
2546         $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
2548         // The user is not subscribed to the forum. Insert a new discussion subscription.
2549         $subscription = new \stdClass();
2550         $subscription->userid  = $user->id;
2551         $subscription->forum = $forum->id;
2552         $subscription->discussion = $discussion->id;
2553         $subscription->preference = time();
2555         $subscription->id = $DB->insert_record('forum_discussion_subs', $subscription);
2557         // Without the forumid.
2558         $params = array(
2559             'context' => context_module::instance($forum->cmid),
2560             'objectid' => $subscription->id,
2561             'relateduserid' => $user->id,
2562             'other' => array(
2563                 'discussion' => $discussion->id,
2564             )
2565         );
2567         \mod_forum\event\discussion_subscription_deleted::create($params);
2568     }
2570     /**
2571      * Test relateduserid validation of discussion_subscription_deleted event.
2572      *
2573      * @expectedException        coding_exception
2574      * @expectedExceptionMessage The 'relateduserid' must be set.
2575      */
2576     public function test_discussion_subscription_deleted_validation_relateduserid() {
2577         global $CFG, $DB;
2578         require_once($CFG->dirroot . '/mod/forum/lib.php');
2580         // Setup test data.
2581         $course = $this->getDataGenerator()->create_course();
2582         $user = $this->getDataGenerator()->create_user();
2583         $this->getDataGenerator()->enrol_user($user->id, $course->id);
2585         $options = array('course' => $course->id, 'forcesubscribe' => FORUM_CHOOSESUBSCRIBE);
2586         $forum = $this->getDataGenerator()->create_module('forum', $options);
2588         // Add a discussion.
2589         $record = array();
2590         $record['course'] = $course->id;
2591         $record['forum'] = $forum->id;
2592         $record['userid'] = $user->id;
2593         $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
2595         // Add a post.
2596         $record = array();
2597         $record['discussion'] = $discussion->id;
2598         $record['userid'] = $user->id;
2599         $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
2601         // The user is not subscribed to the forum. Insert a new discussion subscription.
2602         $subscription = new \stdClass();
2603         $subscription->userid  = $user->id;
2604         $subscription->forum = $forum->id;
2605         $subscription->discussion = $discussion->id;
2606         $subscription->preference = time();
2608         $subscription->id = $DB->insert_record('forum_discussion_subs', $subscription);
2610         $context = context_module::instance($forum->cmid);
2612         // Without the relateduserid.
2613         $params = array(
2614             'context' => context_module::instance($forum->cmid),
2615             'objectid' => $subscription->id,
2616             'other' => array(
2617                 'forumid' => $forum->id,
2618                 'discussion' => $discussion->id,
2619             )
2620         );
2622         \mod_forum\event\discussion_subscription_deleted::create($params);
2623     }
2625     /**
2626      * Test that the correct context is used in the events when subscribing
2627      * users.
2628      */
2629     public function test_forum_subscription_page_context_valid() {
2630         global $CFG, $PAGE;
2631         require_once($CFG->dirroot . '/mod/forum/lib.php');
2633         // Setup test data.
2634         $course = $this->getDataGenerator()->create_course();
2635         $user = $this->getDataGenerator()->create_user();
2636         $this->getDataGenerator()->enrol_user($user->id, $course->id);
2638         $options = array('course' => $course->id, 'forcesubscribe' => FORUM_CHOOSESUBSCRIBE);
2639         $forum = $this->getDataGenerator()->create_module('forum', $options);
2640         $quiz = $this->getDataGenerator()->create_module('quiz', $options);
2642         // Add a discussion.
2643         $record = array();
2644         $record['course'] = $course->id;
2645         $record['forum'] = $forum->id;
2646         $record['userid'] = $user->id;
2647         $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
2649         // Add a post.
2650         $record = array();
2651         $record['discussion'] = $discussion->id;
2652         $record['userid'] = $user->id;
2653         $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
2655         // Set up the default page event to use this forum.
2656         $PAGE = new moodle_page();
2657         $cm = get_coursemodule_from_instance('forum', $discussion->forum);
2658         $context = \context_module::instance($cm->id);
2659         $PAGE->set_context($context);
2660         $PAGE->set_cm($cm, $course, $forum);
2662         // Trigger and capturing the event.
2663         $sink = $this->redirectEvents();
2665         // Trigger the event by subscribing the user to the forum.
2666         \mod_forum\subscriptions::subscribe_user($user->id, $forum);
2668         $events = $sink->get_events();
2669         $sink->clear();
2670         $this->assertCount(1, $events);
2671         $event = reset($events);
2673         // Checking that the event contains the expected values.
2674         $this->assertInstanceOf('\mod_forum\event\subscription_created', $event);
2675         $this->assertEquals($context, $event->get_context());
2677         // Trigger the event by unsubscribing the user to the forum.
2678         \mod_forum\subscriptions::unsubscribe_user($user->id, $forum);
2680         $events = $sink->get_events();
2681         $sink->clear();
2682         $this->assertCount(1, $events);
2683         $event = reset($events);
2685         // Checking that the event contains the expected values.
2686         $this->assertInstanceOf('\mod_forum\event\subscription_deleted', $event);
2687         $this->assertEquals($context, $event->get_context());
2689         // Trigger the event by subscribing the user to the discussion.
2690         \mod_forum\subscriptions::subscribe_user_to_discussion($user->id, $discussion);
2692         $events = $sink->get_events();
2693         $sink->clear();
2694         $this->assertCount(1, $events);
2695         $event = reset($events);
2697         // Checking that the event contains the expected values.
2698         $this->assertInstanceOf('\mod_forum\event\discussion_subscription_created', $event);
2699         $this->assertEquals($context, $event->get_context());
2701         // Trigger the event by unsubscribing the user from the discussion.
2702         \mod_forum\subscriptions::unsubscribe_user_from_discussion($user->id, $discussion);
2704         $events = $sink->get_events();
2705         $sink->clear();
2706         $this->assertCount(1, $events);
2707         $event = reset($events);
2709         // Checking that the event contains the expected values.
2710         $this->assertInstanceOf('\mod_forum\event\discussion_subscription_deleted', $event);
2711         $this->assertEquals($context, $event->get_context());
2713         // Now try with the context for a different module (quiz).
2714         $PAGE = new moodle_page();
2715         $cm = get_coursemodule_from_instance('quiz', $quiz->id);
2716         $quizcontext = \context_module::instance($cm->id);
2717         $PAGE->set_context($quizcontext);
2718         $PAGE->set_cm($cm, $course, $quiz);
2720         // Trigger and capturing the event.
2721         $sink = $this->redirectEvents();
2723         // Trigger the event by subscribing the user to the forum.
2724         \mod_forum\subscriptions::subscribe_user($user->id, $forum);
2726         $events = $sink->get_events();
2727         $sink->clear();
2728         $this->assertCount(1, $events);
2729         $event = reset($events);
2731         // Checking that the event contains the expected values.
2732         $this->assertInstanceOf('\mod_forum\event\subscription_created', $event);
2733         $this->assertEquals($context, $event->get_context());
2735         // Trigger the event by unsubscribing the user to the forum.
2736         \mod_forum\subscriptions::unsubscribe_user($user->id, $forum);
2738         $events = $sink->get_events();
2739         $sink->clear();
2740         $this->assertCount(1, $events);
2741         $event = reset($events);
2743         // Checking that the event contains the expected values.
2744         $this->assertInstanceOf('\mod_forum\event\subscription_deleted', $event);
2745         $this->assertEquals($context, $event->get_context());
2747         // Trigger the event by subscribing the user to the discussion.
2748         \mod_forum\subscriptions::subscribe_user_to_discussion($user->id, $discussion);
2750         $events = $sink->get_events();
2751         $sink->clear();
2752         $this->assertCount(1, $events);
2753         $event = reset($events);
2755         // Checking that the event contains the expected values.
2756         $this->assertInstanceOf('\mod_forum\event\discussion_subscription_created', $event);
2757         $this->assertEquals($context, $event->get_context());
2759         // Trigger the event by unsubscribing the user from the discussion.
2760         \mod_forum\subscriptions::unsubscribe_user_from_discussion($user->id, $discussion);
2762         $events = $sink->get_events();
2763         $sink->clear();
2764         $this->assertCount(1, $events);
2765         $event = reset($events);
2767         // Checking that the event contains the expected values.
2768         $this->assertInstanceOf('\mod_forum\event\discussion_subscription_deleted', $event);
2769         $this->assertEquals($context, $event->get_context());
2771         // Now try with the course context - the module context should still be used.
2772         $PAGE = new moodle_page();
2773         $coursecontext = \context_course::instance($course->id);
2774         $PAGE->set_context($coursecontext);
2776         // Trigger the event by subscribing the user to the forum.
2777         \mod_forum\subscriptions::subscribe_user($user->id, $forum);
2779         $events = $sink->get_events();
2780         $sink->clear();
2781         $this->assertCount(1, $events);
2782         $event = reset($events);
2784         // Checking that the event contains the expected values.
2785         $this->assertInstanceOf('\mod_forum\event\subscription_created', $event);
2786         $this->assertEquals($context, $event->get_context());
2788         // Trigger the event by unsubscribing the user to the forum.
2789         \mod_forum\subscriptions::unsubscribe_user($user->id, $forum);
2791         $events = $sink->get_events();
2792         $sink->clear();
2793         $this->assertCount(1, $events);
2794         $event = reset($events);
2796         // Checking that the event contains the expected values.
2797         $this->assertInstanceOf('\mod_forum\event\subscription_deleted', $event);
2798         $this->assertEquals($context, $event->get_context());
2800         // Trigger the event by subscribing the user to the discussion.
2801         \mod_forum\subscriptions::subscribe_user_to_discussion($user->id, $discussion);
2803         $events = $sink->get_events();
2804         $sink->clear();
2805         $this->assertCount(1, $events);
2806         $event = reset($events);
2808         // Checking that the event contains the expected values.
2809         $this->assertInstanceOf('\mod_forum\event\discussion_subscription_created', $event);
2810         $this->assertEquals($context, $event->get_context());
2812         // Trigger the event by unsubscribing the user from the discussion.
2813         \mod_forum\subscriptions::unsubscribe_user_from_discussion($user->id, $discussion);
2815         $events = $sink->get_events();
2816         $sink->clear();
2817         $this->assertCount(1, $events);
2818         $event = reset($events);
2820         // Checking that the event contains the expected values.
2821         $this->assertInstanceOf('\mod_forum\event\discussion_subscription_deleted', $event);
2822         $this->assertEquals($context, $event->get_context());
2824     }
2826     /**
2827      * Test mod_forum_observer methods.
2828      */
2829     public function test_observers() {
2830         global $DB, $CFG;
2832         require_once($CFG->dirroot . '/mod/forum/lib.php');
2834         $forumgen = $this->getDataGenerator()->get_plugin_generator('mod_forum');
2836         $course = $this->getDataGenerator()->create_course();
2837         $trackedrecord = array('course' => $course->id, 'type' => 'general', 'forcesubscribe' => FORUM_INITIALSUBSCRIBE);
2838         $untrackedrecord = array('course' => $course->id, 'type' => 'general');
2839         $trackedforum = $this->getDataGenerator()->create_module('forum', $trackedrecord);
2840         $untrackedforum = $this->getDataGenerator()->create_module('forum', $untrackedrecord);
2842         // Used functions don't require these settings; adding
2843         // them just in case there are APIs changes in future.
2844         $user = $this->getDataGenerator()->create_user(array(
2845             'maildigest' => 1,
2846             'trackforums' => 1
2847         ));
2849         $manplugin = enrol_get_plugin('manual');
2850         $manualenrol = $DB->get_record('enrol', array('courseid' => $course->id, 'enrol' => 'manual'));
2851         $student = $DB->get_record('role', array('shortname' => 'student'));
2853         // The role_assign observer does it's job adding the forum_subscriptions record.
2854         $manplugin->enrol_user($manualenrol, $user->id, $student->id);
2856         // They are not required, but in a real environment they are supposed to be required;
2857         // adding them just in case there are APIs changes in future.
2858         set_config('forum_trackingtype', 1);
2859         set_config('forum_trackreadposts', 1);
2861         $record = array();
2862         $record['course'] = $course->id;
2863         $record['forum'] = $trackedforum->id;
2864         $record['userid'] = $user->id;
2865         $discussion = $forumgen->create_discussion($record);
2867         $record = array();
2868         $record['discussion'] = $discussion->id;
2869         $record['userid'] = $user->id;
2870         $post = $forumgen->create_post($record);
2872         forum_tp_add_read_record($user->id, $post->id);
2873         forum_set_user_maildigest($trackedforum, 2, $user);
2874         forum_tp_stop_tracking($untrackedforum->id, $user->id);
2876         $this->assertEquals(1, $DB->count_records('forum_subscriptions'));
2877         $this->assertEquals(1, $DB->count_records('forum_digests'));
2878         $this->assertEquals(1, $DB->count_records('forum_track_prefs'));
2879         $this->assertEquals(1, $DB->count_records('forum_read'));
2881         // The course_module_created observer does it's job adding a subscription.
2882         $forumrecord = array('course' => $course->id, 'type' => 'general', 'forcesubscribe' => FORUM_INITIALSUBSCRIBE);
2883         $extraforum = $this->getDataGenerator()->create_module('forum', $forumrecord);
2884         $this->assertEquals(2, $DB->count_records('forum_subscriptions'));
2886         $manplugin->unenrol_user($manualenrol, $user->id);
2888         $this->assertEquals(0, $DB->count_records('forum_digests'));
2889         $this->assertEquals(0, $DB->count_records('forum_subscriptions'));
2890         $this->assertEquals(0, $DB->count_records('forum_track_prefs'));
2891         $this->assertEquals(0, $DB->count_records('forum_read'));
2892     }