424d5a935a9afc544faace2c052a5f83886107de
[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         $this->resetAfterTest();
43     }
45     /**
46      * Ensure course_searched event validates that searchterm is set.
47      */
48     public function test_course_searched_searchterm_validation() {
49         $course = $this->getDataGenerator()->create_course();
50         $coursectx = context_course::instance($course->id);
51         $params = array(
52             'context' => $coursectx,
53         );
55         $this->setExpectedException('coding_exception', 'The \'searchterm\' value must be set in other.');
56         \mod_forum\event\course_searched::create($params);
57     }
59     /**
60      * Ensure course_searched event validates that context is the correct level.
61      */
62     public function test_course_searched_context_validation() {
63         $course = $this->getDataGenerator()->create_course();
64         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
65         $context = context_module::instance($forum->cmid);
66         $params = array(
67             'context' => $context,
68             'other' => array('searchterm' => 'testing'),
69         );
71         $this->setExpectedException('coding_exception', 'Context level must be CONTEXT_COURSE.');
72         \mod_forum\event\course_searched::create($params);
73     }
75     /**
76      * Test course_searched event.
77      */
78     public function test_course_searched() {
80         // Setup test data.
81         $course = $this->getDataGenerator()->create_course();
82         $coursectx = context_course::instance($course->id);
83         $searchterm = 'testing123';
85         $params = array(
86             'context' => $coursectx,
87             'other' => array('searchterm' => $searchterm),
88         );
90         // Create event.
91         $event = \mod_forum\event\course_searched::create($params);
93         // Trigger and capture the event.
94         $sink = $this->redirectEvents();
95         $event->trigger();
96         $events = $sink->get_events();
97         $this->assertCount(1, $events);
98         $event = reset($events);
100          // Checking that the event contains the expected values.
101         $this->assertInstanceOf('\mod_forum\event\course_searched', $event);
102         $this->assertEquals($coursectx, $event->get_context());
103         $expected = array($course->id, 'forum', 'search', "search.php?id={$course->id}&amp;search={$searchterm}", $searchterm);
104         $this->assertEventLegacyLogData($expected, $event);
105         $this->assertEventContextNotUsed($event);
107         $this->assertNotEmpty($event->get_name());
108     }
110     /**
111      * Ensure discussion_created event validates that forumid is set.
112      */
113     public function test_discussion_created_forumid_validation() {
114         $course = $this->getDataGenerator()->create_course();
115         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
116         $context = context_module::instance($forum->cmid);
118         $params = array(
119             'context' => $context,
120         );
122         $this->setExpectedException('coding_exception', 'The \'forumid\' value must be set in other.');
123         \mod_forum\event\discussion_created::create($params);
124     }
126     /**
127      * Ensure discussion_created event validates that the context is the correct level.
128      */
129     public function test_discussion_created_context_validation() {
130         $course = $this->getDataGenerator()->create_course();
131         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
133         $params = array(
134             'context' => context_system::instance(),
135             'other' => array('forumid' => $forum->id),
136         );
138         $this->setExpectedException('coding_exception', 'Context level must be CONTEXT_MODULE.');
139         \mod_forum\event\discussion_created::create($params);
140     }
142     /**
143      * Test discussion_created event.
144      */
145     public function test_discussion_created() {
147         // Setup test data.
148         $course = $this->getDataGenerator()->create_course();
149         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
150         $user = $this->getDataGenerator()->create_user();
152         // Add a discussion.
153         $record = array();
154         $record['course'] = $course->id;
155         $record['forum'] = $forum->id;
156         $record['userid'] = $user->id;
157         $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
159         $context = context_module::instance($forum->cmid);
161         $params = array(
162             'context' => $context,
163             'objectid' => $discussion->id,
164             'other' => array('forumid' => $forum->id),
165         );
167         // Create the event.
168         $event = \mod_forum\event\discussion_created::create($params);
170         // Trigger and capturing the event.
171         $sink = $this->redirectEvents();
172         $event->trigger();
173         $events = $sink->get_events();
174         $this->assertCount(1, $events);
175         $event = reset($events);
177         // Check that the event contains the expected values.
178         $this->assertInstanceOf('\mod_forum\event\discussion_created', $event);
179         $this->assertEquals($context, $event->get_context());
180         $expected = array($course->id, 'forum', 'add discussion', "discuss.php?d={$discussion->id}", $discussion->id, $forum->cmid);
181         $this->assertEventLegacyLogData($expected, $event);
182         $this->assertEventContextNotUsed($event);
184         $this->assertNotEmpty($event->get_name());
185     }
187     /**
188      * Ensure discussion_updated event validates that forumid is set.
189      */
190     public function test_discussion_updated_forumid_validation() {
191         $course = $this->getDataGenerator()->create_course();
192         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
193         $context = context_module::instance($forum->cmid);
195         $params = array(
196             'context' => $context,
197         );
199         $this->setExpectedException('coding_exception', 'The \'forumid\' value must be set in other.');
200         \mod_forum\event\discussion_updated::create($params);
201     }
203     /**
204      * Ensure discussion_created event validates that the context is the correct level.
205      */
206     public function test_discussion_updated_context_validation() {
207         $course = $this->getDataGenerator()->create_course();
208         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
210         $params = array(
211             'context' => context_system::instance(),
212             'other' => array('forumid' => $forum->id),
213         );
215         $this->setExpectedException('coding_exception', 'Context level must be CONTEXT_MODULE.');
216         \mod_forum\event\discussion_updated::create($params);
217     }
219     /**
220      * Test discussion_created event.
221      */
222     public function test_discussion_updated() {
224         // Setup test data.
225         $course = $this->getDataGenerator()->create_course();
226         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
227         $user = $this->getDataGenerator()->create_user();
229         // Add a discussion.
230         $record = array();
231         $record['course'] = $course->id;
232         $record['forum'] = $forum->id;
233         $record['userid'] = $user->id;
234         $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
236         $context = context_module::instance($forum->cmid);
238         $params = array(
239             'context' => $context,
240             'objectid' => $discussion->id,
241             'other' => array('forumid' => $forum->id),
242         );
244         // Create the event.
245         $event = \mod_forum\event\discussion_updated::create($params);
247         // Trigger and capturing the event.
248         $sink = $this->redirectEvents();
249         $event->trigger();
250         $events = $sink->get_events();
251         $this->assertCount(1, $events);
252         $event = reset($events);
254         // Check that the event contains the expected values.
255         $this->assertInstanceOf('\mod_forum\event\discussion_updated', $event);
256         $this->assertEquals($context, $event->get_context());
257         $this->assertEventContextNotUsed($event);
259         $this->assertNotEmpty($event->get_name());
260     }
262     /**
263      * Ensure discussion_deleted event validates that forumid is set.
264      */
265     public function test_discussion_deleted_forumid_validation() {
266         $course = $this->getDataGenerator()->create_course();
267         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
268         $context = context_module::instance($forum->cmid);
270         $params = array(
271             'context' => $context,
272         );
274         $this->setExpectedException('coding_exception', 'The \'forumid\' value must be set in other.');
275         \mod_forum\event\discussion_deleted::create($params);
276     }
278     /**
279      * Ensure discussion_deleted event validates that context is of the correct level.
280      */
281     public function test_discussion_deleted_context_validation() {
282         $course = $this->getDataGenerator()->create_course();
283         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
285         $params = array(
286             'context' => context_system::instance(),
287             'other' => array('forumid' => $forum->id),
288         );
290         $this->setExpectedException('coding_exception', 'Context level must be CONTEXT_MODULE.');
291         \mod_forum\event\discussion_deleted::create($params);
292     }
294     /**
295      * Test discussion_deleted event.
296      */
297     public function test_discussion_deleted() {
299         // Setup test data.
300         $course = $this->getDataGenerator()->create_course();
301         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
302         $user = $this->getDataGenerator()->create_user();
304         // Add a discussion.
305         $record = array();
306         $record['course'] = $course->id;
307         $record['forum'] = $forum->id;
308         $record['userid'] = $user->id;
309         $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
311         $context = context_module::instance($forum->cmid);
313         $params = array(
314             'context' => $context,
315             'objectid' => $discussion->id,
316             'other' => array('forumid' => $forum->id),
317         );
319         $event = \mod_forum\event\discussion_deleted::create($params);
321         // Trigger and capture the event.
322         $sink = $this->redirectEvents();
323         $event->trigger();
324         $events = $sink->get_events();
325         $this->assertCount(1, $events);
326         $event = reset($events);
328         // Checking that the event contains the expected values.
329         $this->assertInstanceOf('\mod_forum\event\discussion_deleted', $event);
330         $this->assertEquals($context, $event->get_context());
331         $expected = array($course->id, 'forum', 'delete discussion', "view.php?id={$forum->cmid}", $forum->id, $forum->cmid);
332         $this->assertEventLegacyLogData($expected, $event);
333         $this->assertEventContextNotUsed($event);
335         $this->assertNotEmpty($event->get_name());
336     }
338     /**
339      * Ensure discussion_moved event validates that fromforumid is set.
340      */
341     public function test_discussion_moved_fromforumid_validation() {
342         $course = $this->getDataGenerator()->create_course();
343         $toforum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
345         $context = context_module::instance($toforum->cmid);
347         $params = array(
348             'context' => $context,
349             'other' => array('toforumid' => $toforum->id)
350         );
352         $this->setExpectedException('coding_exception', 'The \'fromforumid\' value must be set in other.');
353         \mod_forum\event\discussion_moved::create($params);
354     }
356     /**
357      * Ensure discussion_moved event validates that toforumid is set.
358      */
359     public function test_discussion_moved_toforumid_validation() {
360         $course = $this->getDataGenerator()->create_course();
361         $fromforum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
362         $toforum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
363         $context = context_module::instance($toforum->cmid);
365         $params = array(
366             'context' => $context,
367             'other' => array('fromforumid' => $fromforum->id)
368         );
370         $this->setExpectedException('coding_exception', 'The \'toforumid\' value must be set in other.');
371         \mod_forum\event\discussion_moved::create($params);
372     }
374     /**
375      * Ensure discussion_moved event validates that the context level is correct.
376      */
377     public function test_discussion_moved_context_validation() {
378         $course = $this->getDataGenerator()->create_course();
379         $fromforum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
380         $toforum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
381         $user = $this->getDataGenerator()->create_user();
383         // Add a discussion.
384         $record = array();
385         $record['course'] = $course->id;
386         $record['forum'] = $fromforum->id;
387         $record['userid'] = $user->id;
388         $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
390         $params = array(
391             'context' => context_system::instance(),
392             'objectid' => $discussion->id,
393             'other' => array('fromforumid' => $fromforum->id, 'toforumid' => $toforum->id)
394         );
396         $this->setExpectedException('coding_exception', 'Context level must be CONTEXT_MODULE.');
397         \mod_forum\event\discussion_moved::create($params);
398     }
400     /**
401      * Test discussion_moved event.
402      */
403     public function test_discussion_moved() {
404         // Setup test data.
405         $course = $this->getDataGenerator()->create_course();
406         $fromforum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
407         $toforum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
408         $user = $this->getDataGenerator()->create_user();
410         // Add a discussion.
411         $record = array();
412         $record['course'] = $course->id;
413         $record['forum'] = $fromforum->id;
414         $record['userid'] = $user->id;
415         $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
417         $context = context_module::instance($toforum->cmid);
419         $params = array(
420             'context' => $context,
421             'objectid' => $discussion->id,
422             'other' => array('fromforumid' => $fromforum->id, 'toforumid' => $toforum->id)
423         );
425         $event = \mod_forum\event\discussion_moved::create($params);
427         // Trigger and capture the event.
428         $sink = $this->redirectEvents();
429         $event->trigger();
430         $events = $sink->get_events();
431         $this->assertCount(1, $events);
432         $event = reset($events);
434         // Checking that the event contains the expected values.
435         $this->assertInstanceOf('\mod_forum\event\discussion_moved', $event);
436         $this->assertEquals($context, $event->get_context());
437         $expected = array($course->id, 'forum', 'move discussion', "discuss.php?d={$discussion->id}",
438             $discussion->id, $toforum->cmid);
439         $this->assertEventLegacyLogData($expected, $event);
440         $this->assertEventContextNotUsed($event);
442         $this->assertNotEmpty($event->get_name());
443     }
446     /**
447      * Ensure discussion_viewed event validates that the contextlevel is correct.
448      */
449     public function test_discussion_viewed_context_validation() {
450         $course = $this->getDataGenerator()->create_course();
451         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
452         $user = $this->getDataGenerator()->create_user();
454         // Add a discussion.
455         $record = array();
456         $record['course'] = $course->id;
457         $record['forum'] = $forum->id;
458         $record['userid'] = $user->id;
459         $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
461         $params = array(
462             'context' => context_system::instance(),
463             'objectid' => $discussion->id,
464         );
466         $this->setExpectedException('coding_exception', 'Context level must be CONTEXT_MODULE.');
467         \mod_forum\event\discussion_viewed::create($params);
468     }
470     /**
471      * Test discussion_viewed event.
472      */
473     public function test_discussion_viewed() {
474         // Setup test data.
475         $course = $this->getDataGenerator()->create_course();
476         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
477         $user = $this->getDataGenerator()->create_user();
479         // Add a discussion.
480         $record = array();
481         $record['course'] = $course->id;
482         $record['forum'] = $forum->id;
483         $record['userid'] = $user->id;
484         $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
486         $context = context_module::instance($forum->cmid);
488         $params = array(
489             'context' => $context,
490             'objectid' => $discussion->id,
491         );
493         $event = \mod_forum\event\discussion_viewed::create($params);
495         // Trigger and capture the event.
496         $sink = $this->redirectEvents();
497         $event->trigger();
498         $events = $sink->get_events();
499         $this->assertCount(1, $events);
500         $event = reset($events);
502         // Checking that the event contains the expected values.
503         $this->assertInstanceOf('\mod_forum\event\discussion_viewed', $event);
504         $this->assertEquals($context, $event->get_context());
505         $expected = array($course->id, 'forum', 'view discussion', "discuss.php?d={$discussion->id}",
506             $discussion->id, $forum->cmid);
507         $this->assertEventLegacyLogData($expected, $event);
508         $this->assertEventContextNotUsed($event);
510         $this->assertNotEmpty($event->get_name());
511     }
513     /**
514      * Ensure course_module_viewed event validates that the contextlevel is correct.
515      */
516     public function test_course_module_viewed_context_validation() {
517         $course = $this->getDataGenerator()->create_course();
518         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
520         $params = array(
521             'context' => context_system::instance(),
522             'objectid' => $forum->id,
523         );
525         $this->setExpectedException('coding_exception', 'Context level must be CONTEXT_MODULE.');
526         \mod_forum\event\course_module_viewed::create($params);
527     }
529     /**
530      * Test the course_module_viewed event.
531      */
532     public function test_course_module_viewed() {
533         // Setup test data.
534         $course = $this->getDataGenerator()->create_course();
535         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
537         $context = context_module::instance($forum->cmid);
539         $params = array(
540             'context' => $context,
541             'objectid' => $forum->id,
542         );
544         $event = \mod_forum\event\course_module_viewed::create($params);
546         // Trigger and capture the event.
547         $sink = $this->redirectEvents();
548         $event->trigger();
549         $events = $sink->get_events();
550         $this->assertCount(1, $events);
551         $event = reset($events);
553         // Checking that the event contains the expected values.
554         $this->assertInstanceOf('\mod_forum\event\course_module_viewed', $event);
555         $this->assertEquals($context, $event->get_context());
556         $expected = array($course->id, 'forum', 'view forum', "view.php?f={$forum->id}", $forum->id, $forum->cmid);
557         $this->assertEventLegacyLogData($expected, $event);
558         $url = new \moodle_url('/mod/forum/view.php', array('f' => $forum->id));
559         $this->assertEquals($url, $event->get_url());
560         $this->assertEventContextNotUsed($event);
562         $this->assertNotEmpty($event->get_name());
563     }
565     /**
566      * Ensure subscription_created event validates that the forumid is set.
567      */
568     public function test_subscription_created_forumid_validation() {
569         $user = $this->getDataGenerator()->create_user();
570         $course = $this->getDataGenerator()->create_course();
571         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
573         $params = array(
574             'context' => context_module::instance($forum->cmid),
575             'relateduserid' => $user->id,
576         );
578         $this->setExpectedException('coding_exception', 'The \'forumid\' value must be set in other.');
579         \mod_forum\event\subscription_created::create($params);
580     }
582     /**
583      * Ensure subscription_created event validates that the relateduserid is set.
584      */
585     public function test_subscription_created_relateduserid_validation() {
586         $course = $this->getDataGenerator()->create_course();
587         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
589         $params = array(
590             'context' => context_module::instance($forum->cmid),
591             'objectid' => $forum->id,
592         );
594         $this->setExpectedException('coding_exception', 'The \'relateduserid\' must be set.');
595         \mod_forum\event\subscription_created::create($params);
596     }
598     /**
599      * Ensure subscription_created event validates that the contextlevel is correct.
600      */
601     public function test_subscription_created_contextlevel_validation() {
602         $user = $this->getDataGenerator()->create_user();
603         $course = $this->getDataGenerator()->create_course();
604         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
606         $params = array(
607             'context' => context_system::instance(),
608             'other' => array('forumid' => $forum->id),
609             'relateduserid' => $user->id,
610         );
612         $this->setExpectedException('coding_exception', 'Context level must be CONTEXT_MODULE.');
613         \mod_forum\event\subscription_created::create($params);
614     }
616     /**
617      * Test the subscription_created event.
618      */
619     public function test_subscription_created() {
620         // Setup test data.
621         $user = $this->getDataGenerator()->create_user();
622         $course = $this->getDataGenerator()->create_course();
623         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
624         $context = context_module::instance($forum->cmid);
626         $params = array(
627             'context' => $context,
628             'other' => array('forumid' => $forum->id),
629             'relateduserid' => $user->id,
630         );
632         $event = \mod_forum\event\subscription_created::create($params);
634         // Trigger and capturing the event.
635         $sink = $this->redirectEvents();
636         $event->trigger();
637         $events = $sink->get_events();
638         $this->assertCount(1, $events);
639         $event = reset($events);
641         // Checking that the event contains the expected values.
642         $this->assertInstanceOf('\mod_forum\event\subscription_created', $event);
643         $this->assertEquals($context, $event->get_context());
644         $expected = array($course->id, 'forum', 'subscribe', "view.php?f={$forum->id}", $forum->id, $forum->cmid);
645         $this->assertEventLegacyLogData($expected, $event);
646         $url = new \moodle_url('/mod/forum/view.php', array('f' => $forum->id));
647         $this->assertEquals($url, $event->get_url());
648         $this->assertEventContextNotUsed($event);
650         $this->assertNotEmpty($event->get_name());
651     }
653     /**
654      * Ensure subscription_deleted event validates that the forumid is set.
655      */
656     public function test_subscription_deleted_forumid_validation() {
657         $user = $this->getDataGenerator()->create_user();
658         $course = $this->getDataGenerator()->create_course();
659         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
661         $params = array(
662             'context' => context_module::instance($forum->cmid),
663             'relateduserid' => $user->id,
664         );
666         $this->setExpectedException('coding_exception', 'The \'forumid\' value must be set in other.');
667         \mod_forum\event\subscription_deleted::create($params);
668     }
670     /**
671      * Ensure subscription_deleted event validates that the relateduserid is set.
672      */
673     public function test_subscription_deleted_relateduserid_validation() {
674         $course = $this->getDataGenerator()->create_course();
675         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
677         $params = array(
678             'context' => context_module::instance($forum->cmid),
679             'objectid' => $forum->id,
680         );
682         $this->setExpectedException('coding_exception', 'The \'relateduserid\' must be set.');
683         \mod_forum\event\subscription_deleted::create($params);
684     }
686     /**
687      * Ensure subscription_deleted event validates that the contextlevel is correct.
688      */
689     public function test_subscription_deleted_contextlevel_validation() {
690         $user = $this->getDataGenerator()->create_user();
691         $course = $this->getDataGenerator()->create_course();
692         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
694         $params = array(
695             'context' => context_system::instance(),
696             'other' => array('forumid' => $forum->id),
697             'relateduserid' => $user->id,
698         );
700         $this->setExpectedException('coding_exception', 'Context level must be CONTEXT_MODULE.');
701         \mod_forum\event\subscription_deleted::create($params);
702     }
704     /**
705      * Test the subscription_deleted event.
706      */
707     public function test_subscription_deleted() {
708         // Setup test data.
709         $user = $this->getDataGenerator()->create_user();
710         $course = $this->getDataGenerator()->create_course();
711         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
712         $context = context_module::instance($forum->cmid);
714         $params = array(
715             'context' => $context,
716             'other' => array('forumid' => $forum->id),
717             'relateduserid' => $user->id,
718         );
720         $event = \mod_forum\event\subscription_deleted::create($params);
722         // Trigger and capturing the event.
723         $sink = $this->redirectEvents();
724         $event->trigger();
725         $events = $sink->get_events();
726         $this->assertCount(1, $events);
727         $event = reset($events);
729         // Checking that the event contains the expected values.
730         $this->assertInstanceOf('\mod_forum\event\subscription_deleted', $event);
731         $this->assertEquals($context, $event->get_context());
732         $expected = array($course->id, 'forum', 'unsubscribe', "view.php?f={$forum->id}", $forum->id, $forum->cmid);
733         $this->assertEventLegacyLogData($expected, $event);
734         $url = new \moodle_url('/mod/forum/view.php', array('f' => $forum->id));
735         $this->assertEquals($url, $event->get_url());
736         $this->assertEventContextNotUsed($event);
738         $this->assertNotEmpty($event->get_name());
739     }
741     /**
742      * Ensure readtracking_enabled event validates that the forumid is set.
743      */
744     public function test_readtracking_enabled_forumid_validation() {
745         $user = $this->getDataGenerator()->create_user();
746         $course = $this->getDataGenerator()->create_course();
747         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
749         $params = array(
750             'context' => context_module::instance($forum->cmid),
751             'relateduserid' => $user->id,
752         );
754         $this->setExpectedException('coding_exception', 'The \'forumid\' value must be set in other.');
755         \mod_forum\event\readtracking_enabled::create($params);
756     }
758     /**
759      * Ensure readtracking_enabled event validates that the relateduserid is set.
760      */
761     public function test_readtracking_enabled_relateduserid_validation() {
762         $course = $this->getDataGenerator()->create_course();
763         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
765         $params = array(
766             'context' => context_module::instance($forum->cmid),
767             'objectid' => $forum->id,
768         );
770         $this->setExpectedException('coding_exception', 'The \'relateduserid\' must be set.');
771         \mod_forum\event\readtracking_enabled::create($params);
772     }
774     /**
775      * Ensure readtracking_enabled event validates that the contextlevel is correct.
776      */
777     public function test_readtracking_enabled_contextlevel_validation() {
778         $user = $this->getDataGenerator()->create_user();
779         $course = $this->getDataGenerator()->create_course();
780         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
782         $params = array(
783             'context' => context_system::instance(),
784             'other' => array('forumid' => $forum->id),
785             'relateduserid' => $user->id,
786         );
788         $this->setExpectedException('coding_exception', 'Context level must be CONTEXT_MODULE.');
789         \mod_forum\event\readtracking_enabled::create($params);
790     }
792     /**
793      * Test the readtracking_enabled event.
794      */
795     public function test_readtracking_enabled() {
796         // Setup test data.
797         $user = $this->getDataGenerator()->create_user();
798         $course = $this->getDataGenerator()->create_course();
799         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
800         $context = context_module::instance($forum->cmid);
802         $params = array(
803             'context' => $context,
804             'other' => array('forumid' => $forum->id),
805             'relateduserid' => $user->id,
806         );
808         $event = \mod_forum\event\readtracking_enabled::create($params);
810         // Trigger and capture the event.
811         $sink = $this->redirectEvents();
812         $event->trigger();
813         $events = $sink->get_events();
814         $this->assertCount(1, $events);
815         $event = reset($events);
817         // Checking that the event contains the expected values.
818         $this->assertInstanceOf('\mod_forum\event\readtracking_enabled', $event);
819         $this->assertEquals($context, $event->get_context());
820         $expected = array($course->id, 'forum', 'start tracking', "view.php?f={$forum->id}", $forum->id, $forum->cmid);
821         $this->assertEventLegacyLogData($expected, $event);
822         $url = new \moodle_url('/mod/forum/view.php', array('f' => $forum->id));
823         $this->assertEquals($url, $event->get_url());
824         $this->assertEventContextNotUsed($event);
826         $this->assertNotEmpty($event->get_name());
827     }
829     /**
830      *  Ensure readtracking_disabled event validates that the forumid is set.
831      */
832     public function test_readtracking_disabled_forumid_validation() {
833         $user = $this->getDataGenerator()->create_user();
834         $course = $this->getDataGenerator()->create_course();
835         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
837         $params = array(
838             'context' => context_module::instance($forum->cmid),
839             'relateduserid' => $user->id,
840         );
842         $this->setExpectedException('coding_exception', 'The \'forumid\' value must be set in other.');
843         \mod_forum\event\readtracking_disabled::create($params);
844     }
846     /**
847      *  Ensure readtracking_disabled event validates that the relateduserid is set.
848      */
849     public function test_readtracking_disabled_relateduserid_validation() {
850         $course = $this->getDataGenerator()->create_course();
851         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
853         $params = array(
854             'context' => context_module::instance($forum->cmid),
855             'objectid' => $forum->id,
856         );
858         $this->setExpectedException('coding_exception', 'The \'relateduserid\' must be set.');
859         \mod_forum\event\readtracking_disabled::create($params);
860     }
862     /**
863      *  Ensure readtracking_disabled event validates that the contextlevel is correct
864      */
865     public function test_readtracking_disabled_contextlevel_validation() {
866         $user = $this->getDataGenerator()->create_user();
867         $course = $this->getDataGenerator()->create_course();
868         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
870         $params = array(
871             'context' => context_system::instance(),
872             'other' => array('forumid' => $forum->id),
873             'relateduserid' => $user->id,
874         );
876         $this->setExpectedException('coding_exception', 'Context level must be CONTEXT_MODULE.');
877         \mod_forum\event\readtracking_disabled::create($params);
878     }
880     /**
881      *  Test the readtracking_disabled event.
882      */
883     public function test_readtracking_disabled() {
884         // Setup test data.
885         $user = $this->getDataGenerator()->create_user();
886         $course = $this->getDataGenerator()->create_course();
887         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
888         $context = context_module::instance($forum->cmid);
890         $params = array(
891             'context' => $context,
892             'other' => array('forumid' => $forum->id),
893             'relateduserid' => $user->id,
894         );
896         $event = \mod_forum\event\readtracking_disabled::create($params);
898         // Trigger and capture the event.
899         $sink = $this->redirectEvents();
900         $event->trigger();
901         $events = $sink->get_events();
902         $this->assertCount(1, $events);
903         $event = reset($events);
905         // Checking that the event contains the expected values.
906         $this->assertInstanceOf('\mod_forum\event\readtracking_disabled', $event);
907         $this->assertEquals($context, $event->get_context());
908         $expected = array($course->id, 'forum', 'stop tracking', "view.php?f={$forum->id}", $forum->id, $forum->cmid);
909         $this->assertEventLegacyLogData($expected, $event);
910         $url = new \moodle_url('/mod/forum/view.php', array('f' => $forum->id));
911         $this->assertEquals($url, $event->get_url());
912         $this->assertEventContextNotUsed($event);
914         $this->assertNotEmpty($event->get_name());
915     }
917     /**
918      *  Ensure subscribers_viewed event validates that the forumid is set.
919      */
920     public function test_subscribers_viewed_forumid_validation() {
921         $user = $this->getDataGenerator()->create_user();
922         $course = $this->getDataGenerator()->create_course();
923         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
925         $params = array(
926             'context' => context_module::instance($forum->cmid),
927             'relateduserid' => $user->id,
928         );
930         $this->setExpectedException('coding_exception', 'The \'forumid\' value must be set in other.');
931         \mod_forum\event\subscribers_viewed::create($params);
932     }
934     /**
935      *  Ensure subscribers_viewed event validates that the contextlevel is correct.
936      */
937     public function test_subscribers_viewed_contextlevel_validation() {
938         $user = $this->getDataGenerator()->create_user();
939         $course = $this->getDataGenerator()->create_course();
940         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
942         $params = array(
943             'context' => context_system::instance(),
944             'other' => array('forumid' => $forum->id),
945             'relateduserid' => $user->id,
946         );
948         $this->setExpectedException('coding_exception', 'Context level must be CONTEXT_MODULE.');
949         \mod_forum\event\subscribers_viewed::create($params);
950     }
952     /**
953      *  Test the subscribers_viewed event.
954      */
955     public function test_subscribers_viewed() {
956         // Setup test data.
957         $course = $this->getDataGenerator()->create_course();
958         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
959         $context = context_module::instance($forum->cmid);
961         $params = array(
962             'context' => $context,
963             'other' => array('forumid' => $forum->id),
964         );
966         $event = \mod_forum\event\subscribers_viewed::create($params);
968         // Trigger and capture the event.
969         $sink = $this->redirectEvents();
970         $event->trigger();
971         $events = $sink->get_events();
972         $this->assertCount(1, $events);
973         $event = reset($events);
975         // Checking that the event contains the expected values.
976         $this->assertInstanceOf('\mod_forum\event\subscribers_viewed', $event);
977         $this->assertEquals($context, $event->get_context());
978         $expected = array($course->id, 'forum', 'view subscribers', "subscribers.php?id={$forum->id}", $forum->id, $forum->cmid);
979         $this->assertEventLegacyLogData($expected, $event);
980         $this->assertEventContextNotUsed($event);
982         $this->assertNotEmpty($event->get_name());
983     }
985     /**
986      *  Ensure user_report_viewed event validates that the reportmode is set.
987      */
988     public function test_user_report_viewed_reportmode_validation() {
989         $user = $this->getDataGenerator()->create_user();
990         $course = $this->getDataGenerator()->create_course();
992         $params = array(
993             'context' => context_course::instance($course->id),
994             'relateduserid' => $user->id,
995         );
997         $this->setExpectedException('coding_exception', 'The \'reportmode\' value must be set in other.');
998         \mod_forum\event\user_report_viewed::create($params);
999     }
1001     /**
1002      *  Ensure user_report_viewed event validates that the contextlevel is correct.
1003      */
1004     public function test_user_report_viewed_contextlevel_validation() {
1005         $user = $this->getDataGenerator()->create_user();
1006         $course = $this->getDataGenerator()->create_course();
1007         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
1009         $params = array(
1010             'context' => context_module::instance($forum->id),
1011             'other' => array('reportmode' => 'posts'),
1012             'relateduserid' => $user->id,
1013         );
1015         $this->setExpectedException('coding_exception', 'Context level must be either CONTEXT_SYSTEM or CONTEXT_COURSE.');
1016         \mod_forum\event\user_report_viewed::create($params);
1017     }
1019     /**
1020      *  Ensure user_report_viewed event validates that the relateduserid is set.
1021      */
1022     public function test_user_report_viewed_relateduserid_validation() {
1024         $params = array(
1025             'context' => context_system::instance(),
1026             'other' => array('reportmode' => 'posts'),
1027         );
1029         $this->setExpectedException('coding_exception', 'The \'relateduserid\' must be set.');
1030         \mod_forum\event\user_report_viewed::create($params);
1031     }
1033     /**
1034      * Test the user_report_viewed event.
1035      */
1036     public function test_user_report_viewed() {
1037         // Setup test data.
1038         $user = $this->getDataGenerator()->create_user();
1039         $course = $this->getDataGenerator()->create_course();
1040         $context = context_course::instance($course->id);
1042         $params = array(
1043             'context' => $context,
1044             'relateduserid' => $user->id,
1045             'other' => array('reportmode' => 'discussions'),
1046         );
1048         $event = \mod_forum\event\user_report_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\user_report_viewed', $event);
1059         $this->assertEquals($context, $event->get_context());
1060         $expected = array($course->id, 'forum', 'user report',
1061             "user.php?id={$user->id}&amp;mode=discussions&amp;course={$course->id}", $user->id);
1062         $this->assertEventLegacyLogData($expected, $event);
1063         $this->assertEventContextNotUsed($event);
1065         $this->assertNotEmpty($event->get_name());
1066     }
1068     /**
1069      *  Ensure post_created event validates that the postid is set.
1070      */
1071     public function test_post_created_postid_validation() {
1072         $course = $this->getDataGenerator()->create_course();
1073         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
1074         $user = $this->getDataGenerator()->create_user();
1076         // Add a discussion.
1077         $record = array();
1078         $record['course'] = $course->id;
1079         $record['forum'] = $forum->id;
1080         $record['userid'] = $user->id;
1081         $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1083         $params = array(
1084             'context' => context_module::instance($forum->cmid),
1085             'other' => array('forumid' => $forum->id, 'forumtype' => $forum->type, 'discussionid' => $discussion->id)
1086         );
1088         \mod_forum\event\post_created::create($params);
1089     }
1091     /**
1092      *  Ensure post_created event validates that the discussionid is set.
1093      */
1094     public function test_post_created_discussionid_validation() {
1095         $course = $this->getDataGenerator()->create_course();
1096         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
1097         $user = $this->getDataGenerator()->create_user();
1099         // Add a discussion.
1100         $record = array();
1101         $record['course'] = $course->id;
1102         $record['forum'] = $forum->id;
1103         $record['userid'] = $user->id;
1104         $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1106         // Add a post.
1107         $record = array();
1108         $record['discussion'] = $discussion->id;
1109         $record['userid'] = $user->id;
1110         $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
1112         $params = array(
1113             'context' => context_module::instance($forum->cmid),
1114             'objectid' => $post->id,
1115             'other' => array('forumid' => $forum->id, 'forumtype' => $forum->type)
1116         );
1118         $this->setExpectedException('coding_exception', 'The \'discussionid\' value must be set in other.');
1119         \mod_forum\event\post_created::create($params);
1120     }
1122     /**
1123      *  Ensure post_created event validates that the forumid is set.
1124      */
1125     public function test_post_created_forumid_validation() {
1126         $course = $this->getDataGenerator()->create_course();
1127         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
1128         $user = $this->getDataGenerator()->create_user();
1130         // Add a discussion.
1131         $record = array();
1132         $record['course'] = $course->id;
1133         $record['forum'] = $forum->id;
1134         $record['userid'] = $user->id;
1135         $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1137         // Add a post.
1138         $record = array();
1139         $record['discussion'] = $discussion->id;
1140         $record['userid'] = $user->id;
1141         $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
1143         $params = array(
1144             'context' => context_module::instance($forum->cmid),
1145             'objectid' => $post->id,
1146             'other' => array('discussionid' => $discussion->id, 'forumtype' => $forum->type)
1147         );
1149         $this->setExpectedException('coding_exception', 'The \'forumid\' value must be set in other.');
1150         \mod_forum\event\post_created::create($params);
1151     }
1153     /**
1154      *  Ensure post_created event validates that the forumtype is set.
1155      */
1156     public function test_post_created_forumtype_validation() {
1157         $course = $this->getDataGenerator()->create_course();
1158         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
1159         $user = $this->getDataGenerator()->create_user();
1161         // Add a discussion.
1162         $record = array();
1163         $record['course'] = $course->id;
1164         $record['forum'] = $forum->id;
1165         $record['userid'] = $user->id;
1166         $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1168         // Add a post.
1169         $record = array();
1170         $record['discussion'] = $discussion->id;
1171         $record['userid'] = $user->id;
1172         $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
1174         $params = array(
1175             'context' => context_module::instance($forum->cmid),
1176             'objectid' => $post->id,
1177             'other' => array('discussionid' => $discussion->id, 'forumid' => $forum->id)
1178         );
1180         $this->setExpectedException('coding_exception', 'The \'forumtype\' value must be set in other.');
1181         \mod_forum\event\post_created::create($params);
1182     }
1184     /**
1185      *  Ensure post_created event validates that the contextlevel is correct.
1186      */
1187     public function test_post_created_context_validation() {
1188         $course = $this->getDataGenerator()->create_course();
1189         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
1190         $user = $this->getDataGenerator()->create_user();
1192         // Add a discussion.
1193         $record = array();
1194         $record['course'] = $course->id;
1195         $record['forum'] = $forum->id;
1196         $record['userid'] = $user->id;
1197         $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1199         // Add a post.
1200         $record = array();
1201         $record['discussion'] = $discussion->id;
1202         $record['userid'] = $user->id;
1203         $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
1205         $params = array(
1206             'context' => context_system::instance(),
1207             'objectid' => $post->id,
1208             'other' => array('discussionid' => $discussion->id, 'forumid' => $forum->id, 'forumtype' => $forum->type)
1209         );
1211         $this->setExpectedException('coding_exception', 'Context level must be CONTEXT_MODULE');
1212         \mod_forum\event\post_created::create($params);
1213     }
1215     /**
1216      * Test the post_created event.
1217      */
1218     public function test_post_created() {
1219         // Setup test data.
1220         $course = $this->getDataGenerator()->create_course();
1221         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
1222         $user = $this->getDataGenerator()->create_user();
1224         // Add a discussion.
1225         $record = array();
1226         $record['course'] = $course->id;
1227         $record['forum'] = $forum->id;
1228         $record['userid'] = $user->id;
1229         $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1231         // Add a post.
1232         $record = array();
1233         $record['discussion'] = $discussion->id;
1234         $record['userid'] = $user->id;
1235         $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
1237         $context = context_module::instance($forum->cmid);
1239         $params = array(
1240             'context' => $context,
1241             'objectid' => $post->id,
1242             'other' => array('discussionid' => $discussion->id, 'forumid' => $forum->id, 'forumtype' => $forum->type)
1243         );
1245         $event = \mod_forum\event\post_created::create($params);
1247         // Trigger and capturing the event.
1248         $sink = $this->redirectEvents();
1249         $event->trigger();
1250         $events = $sink->get_events();
1251         $this->assertCount(1, $events);
1252         $event = reset($events);
1254         // Checking that the event contains the expected values.
1255         $this->assertInstanceOf('\mod_forum\event\post_created', $event);
1256         $this->assertEquals($context, $event->get_context());
1257         $expected = array($course->id, 'forum', 'add post', "discuss.php?d={$discussion->id}#p{$post->id}",
1258             $forum->id, $forum->cmid);
1259         $this->assertEventLegacyLogData($expected, $event);
1260         $url = new \moodle_url('/mod/forum/discuss.php', array('d' => $discussion->id));
1261         $url->set_anchor('p'.$event->objectid);
1262         $this->assertEquals($url, $event->get_url());
1263         $this->assertEventContextNotUsed($event);
1265         $this->assertNotEmpty($event->get_name());
1266     }
1268     /**
1269      * Test the post_created event for a single discussion forum.
1270      */
1271     public function test_post_created_single() {
1272         // Setup test data.
1273         $course = $this->getDataGenerator()->create_course();
1274         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id, 'type' => 'single'));
1275         $user = $this->getDataGenerator()->create_user();
1277         // Add a discussion.
1278         $record = array();
1279         $record['course'] = $course->id;
1280         $record['forum'] = $forum->id;
1281         $record['userid'] = $user->id;
1282         $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1284         // Add a post.
1285         $record = array();
1286         $record['discussion'] = $discussion->id;
1287         $record['userid'] = $user->id;
1288         $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
1290         $context = context_module::instance($forum->cmid);
1292         $params = array(
1293             'context' => $context,
1294             'objectid' => $post->id,
1295             'other' => array('discussionid' => $discussion->id, 'forumid' => $forum->id, 'forumtype' => $forum->type)
1296         );
1298         $event = \mod_forum\event\post_created::create($params);
1300         // Trigger and capturing the event.
1301         $sink = $this->redirectEvents();
1302         $event->trigger();
1303         $events = $sink->get_events();
1304         $this->assertCount(1, $events);
1305         $event = reset($events);
1307         // Checking that the event contains the expected values.
1308         $this->assertInstanceOf('\mod_forum\event\post_created', $event);
1309         $this->assertEquals($context, $event->get_context());
1310         $expected = array($course->id, 'forum', 'add post', "view.php?f={$forum->id}#p{$post->id}",
1311             $forum->id, $forum->cmid);
1312         $this->assertEventLegacyLogData($expected, $event);
1313         $url = new \moodle_url('/mod/forum/view.php', array('f' => $forum->id));
1314         $url->set_anchor('p'.$event->objectid);
1315         $this->assertEquals($url, $event->get_url());
1316         $this->assertEventContextNotUsed($event);
1318         $this->assertNotEmpty($event->get_name());
1319     }
1321     /**
1322      *  Ensure post_deleted event validates that the postid is set.
1323      */
1324     public function test_post_deleted_postid_validation() {
1325         $course = $this->getDataGenerator()->create_course();
1326         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
1327         $user = $this->getDataGenerator()->create_user();
1329         // Add a discussion.
1330         $record = array();
1331         $record['course'] = $course->id;
1332         $record['forum'] = $forum->id;
1333         $record['userid'] = $user->id;
1334         $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1336         $params = array(
1337             'context' => context_module::instance($forum->cmid),
1338             'other' => array('forumid' => $forum->id, 'forumtype' => $forum->type, 'discussionid' => $discussion->id)
1339         );
1341         \mod_forum\event\post_deleted::create($params);
1342     }
1344     /**
1345      *  Ensure post_deleted event validates that the discussionid is set.
1346      */
1347     public function test_post_deleted_discussionid_validation() {
1348         $course = $this->getDataGenerator()->create_course();
1349         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
1350         $user = $this->getDataGenerator()->create_user();
1352         // Add a discussion.
1353         $record = array();
1354         $record['course'] = $course->id;
1355         $record['forum'] = $forum->id;
1356         $record['userid'] = $user->id;
1357         $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1359         // Add a post.
1360         $record = array();
1361         $record['discussion'] = $discussion->id;
1362         $record['userid'] = $user->id;
1363         $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
1365         $params = array(
1366             'context' => context_module::instance($forum->cmid),
1367             'objectid' => $post->id,
1368             'other' => array('forumid' => $forum->id, 'forumtype' => $forum->type)
1369         );
1371         $this->setExpectedException('coding_exception', 'The \'discussionid\' value must be set in other.');
1372         \mod_forum\event\post_deleted::create($params);
1373     }
1375     /**
1376      *  Ensure post_deleted event validates that the forumid is set.
1377      */
1378     public function test_post_deleted_forumid_validation() {
1379         $course = $this->getDataGenerator()->create_course();
1380         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
1381         $user = $this->getDataGenerator()->create_user();
1383         // Add a discussion.
1384         $record = array();
1385         $record['course'] = $course->id;
1386         $record['forum'] = $forum->id;
1387         $record['userid'] = $user->id;
1388         $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1390         // Add a post.
1391         $record = array();
1392         $record['discussion'] = $discussion->id;
1393         $record['userid'] = $user->id;
1394         $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
1396         $params = array(
1397             'context' => context_module::instance($forum->cmid),
1398             'objectid' => $post->id,
1399             'other' => array('discussionid' => $discussion->id, 'forumtype' => $forum->type)
1400         );
1402         $this->setExpectedException('coding_exception', 'The \'forumid\' value must be set in other.');
1403         \mod_forum\event\post_deleted::create($params);
1404     }
1406     /**
1407      *  Ensure post_deleted event validates that the forumtype is set.
1408      */
1409     public function test_post_deleted_forumtype_validation() {
1410         $course = $this->getDataGenerator()->create_course();
1411         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
1412         $user = $this->getDataGenerator()->create_user();
1414         // Add a discussion.
1415         $record = array();
1416         $record['course'] = $course->id;
1417         $record['forum'] = $forum->id;
1418         $record['userid'] = $user->id;
1419         $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1421         // Add a post.
1422         $record = array();
1423         $record['discussion'] = $discussion->id;
1424         $record['userid'] = $user->id;
1425         $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
1427         $params = array(
1428             'context' => context_module::instance($forum->cmid),
1429             'objectid' => $post->id,
1430             'other' => array('discussionid' => $discussion->id, 'forumid' => $forum->id)
1431         );
1433         $this->setExpectedException('coding_exception', 'The \'forumtype\' value must be set in other.');
1434         \mod_forum\event\post_deleted::create($params);
1435     }
1437     /**
1438      *  Ensure post_deleted event validates that the contextlevel is correct.
1439      */
1440     public function test_post_deleted_context_validation() {
1441         $course = $this->getDataGenerator()->create_course();
1442         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
1443         $user = $this->getDataGenerator()->create_user();
1445         // Add a discussion.
1446         $record = array();
1447         $record['course'] = $course->id;
1448         $record['forum'] = $forum->id;
1449         $record['userid'] = $user->id;
1450         $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1452         // Add a post.
1453         $record = array();
1454         $record['discussion'] = $discussion->id;
1455         $record['userid'] = $user->id;
1456         $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
1458         $params = array(
1459             'context' => context_system::instance(),
1460             'objectid' => $post->id,
1461             'other' => array('discussionid' => $discussion->id, 'forumid' => $forum->id, 'forumtype' => $forum->type)
1462         );
1464         $this->setExpectedException('coding_exception', 'Context level must be CONTEXT_MODULE');
1465         \mod_forum\event\post_deleted::create($params);
1466     }
1468     /**
1469      * Test post_deleted event.
1470      */
1471     public function test_post_deleted() {
1472         // Setup test data.
1473         $course = $this->getDataGenerator()->create_course();
1474         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
1475         $user = $this->getDataGenerator()->create_user();
1477         // Add a discussion.
1478         $record = array();
1479         $record['course'] = $course->id;
1480         $record['forum'] = $forum->id;
1481         $record['userid'] = $user->id;
1482         $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1484         // Add a post.
1485         $record = array();
1486         $record['discussion'] = $discussion->id;
1487         $record['userid'] = $user->id;
1488         $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
1490         $context = context_module::instance($forum->cmid);
1492         $params = array(
1493             'context' => $context,
1494             'objectid' => $post->id,
1495             'other' => array('discussionid' => $discussion->id, 'forumid' => $forum->id, 'forumtype' => $forum->type)
1496         );
1498         $event = \mod_forum\event\post_deleted::create($params);
1500         // Trigger and capture the event.
1501         $sink = $this->redirectEvents();
1502         $event->trigger();
1503         $events = $sink->get_events();
1504         $this->assertCount(1, $events);
1505         $event = reset($events);
1507         // Checking that the event contains the expected values.
1508         $this->assertInstanceOf('\mod_forum\event\post_deleted', $event);
1509         $this->assertEquals($context, $event->get_context());
1510         $expected = array($course->id, 'forum', 'delete post', "discuss.php?d={$discussion->id}", $post->id, $forum->cmid);
1511         $this->assertEventLegacyLogData($expected, $event);
1512         $url = new \moodle_url('/mod/forum/discuss.php', array('d' => $discussion->id));
1513         $this->assertEquals($url, $event->get_url());
1514         $this->assertEventContextNotUsed($event);
1516         $this->assertNotEmpty($event->get_name());
1517     }
1519     /**
1520      * Test post_deleted event for a single discussion forum.
1521      */
1522     public function test_post_deleted_single() {
1523         // Setup test data.
1524         $course = $this->getDataGenerator()->create_course();
1525         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id, 'type' => 'single'));
1526         $user = $this->getDataGenerator()->create_user();
1528         // Add a discussion.
1529         $record = array();
1530         $record['course'] = $course->id;
1531         $record['forum'] = $forum->id;
1532         $record['userid'] = $user->id;
1533         $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1535         // Add a post.
1536         $record = array();
1537         $record['discussion'] = $discussion->id;
1538         $record['userid'] = $user->id;
1539         $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
1541         $context = context_module::instance($forum->cmid);
1543         $params = array(
1544             'context' => $context,
1545             'objectid' => $post->id,
1546             'other' => array('discussionid' => $discussion->id, 'forumid' => $forum->id, 'forumtype' => $forum->type)
1547         );
1549         $event = \mod_forum\event\post_deleted::create($params);
1551         // Trigger and capture the event.
1552         $sink = $this->redirectEvents();
1553         $event->trigger();
1554         $events = $sink->get_events();
1555         $this->assertCount(1, $events);
1556         $event = reset($events);
1558         // Checking that the event contains the expected values.
1559         $this->assertInstanceOf('\mod_forum\event\post_deleted', $event);
1560         $this->assertEquals($context, $event->get_context());
1561         $expected = array($course->id, 'forum', 'delete post', "view.php?f={$forum->id}", $post->id, $forum->cmid);
1562         $this->assertEventLegacyLogData($expected, $event);
1563         $url = new \moodle_url('/mod/forum/view.php', array('f' => $forum->id));
1564         $this->assertEquals($url, $event->get_url());
1565         $this->assertEventContextNotUsed($event);
1567         $this->assertNotEmpty($event->get_name());
1568     }
1570     /**
1571      *  Ensure post_updated event validates that the discussionid is set.
1572      */
1573     public function test_post_updated_discussionid_validation() {
1574         $course = $this->getDataGenerator()->create_course();
1575         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
1576         $user = $this->getDataGenerator()->create_user();
1578         // Add a discussion.
1579         $record = array();
1580         $record['course'] = $course->id;
1581         $record['forum'] = $forum->id;
1582         $record['userid'] = $user->id;
1583         $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1585         // Add a post.
1586         $record = array();
1587         $record['discussion'] = $discussion->id;
1588         $record['userid'] = $user->id;
1589         $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
1591         $params = array(
1592             'context' => context_module::instance($forum->cmid),
1593             'objectid' => $post->id,
1594             'other' => array('forumid' => $forum->id, 'forumtype' => $forum->type)
1595         );
1597         $this->setExpectedException('coding_exception', 'The \'discussionid\' value must be set in other.');
1598         \mod_forum\event\post_updated::create($params);
1599     }
1601     /**
1602      *  Ensure post_updated event validates that the forumid is set.
1603      */
1604     public function test_post_updated_forumid_validation() {
1605         $course = $this->getDataGenerator()->create_course();
1606         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
1607         $user = $this->getDataGenerator()->create_user();
1609         // Add a discussion.
1610         $record = array();
1611         $record['course'] = $course->id;
1612         $record['forum'] = $forum->id;
1613         $record['userid'] = $user->id;
1614         $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1616         // Add a post.
1617         $record = array();
1618         $record['discussion'] = $discussion->id;
1619         $record['userid'] = $user->id;
1620         $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
1622         $params = array(
1623             'context' => context_module::instance($forum->cmid),
1624             'objectid' => $post->id,
1625             'other' => array('discussionid' => $discussion->id, 'forumtype' => $forum->type)
1626         );
1628         $this->setExpectedException('coding_exception', 'The \'forumid\' value must be set in other.');
1629         \mod_forum\event\post_updated::create($params);
1630     }
1632     /**
1633      *  Ensure post_updated event validates that the forumtype is set.
1634      */
1635     public function test_post_updated_forumtype_validation() {
1636         $course = $this->getDataGenerator()->create_course();
1637         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
1638         $user = $this->getDataGenerator()->create_user();
1640         // Add a discussion.
1641         $record = array();
1642         $record['course'] = $course->id;
1643         $record['forum'] = $forum->id;
1644         $record['userid'] = $user->id;
1645         $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1647         // Add a post.
1648         $record = array();
1649         $record['discussion'] = $discussion->id;
1650         $record['userid'] = $user->id;
1651         $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
1653         $params = array(
1654             'context' => context_module::instance($forum->cmid),
1655             'objectid' => $post->id,
1656             'other' => array('discussionid' => $discussion->id, 'forumid' => $forum->id)
1657         );
1659         $this->setExpectedException('coding_exception', 'The \'forumtype\' value must be set in other.');
1660         \mod_forum\event\post_updated::create($params);
1661     }
1663     /**
1664      *  Ensure post_updated event validates that the contextlevel is correct.
1665      */
1666     public function test_post_updated_context_validation() {
1667         $course = $this->getDataGenerator()->create_course();
1668         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
1669         $user = $this->getDataGenerator()->create_user();
1671         // Add a discussion.
1672         $record = array();
1673         $record['course'] = $course->id;
1674         $record['forum'] = $forum->id;
1675         $record['userid'] = $user->id;
1676         $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1678         // Add a post.
1679         $record = array();
1680         $record['discussion'] = $discussion->id;
1681         $record['userid'] = $user->id;
1682         $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
1684         $params = array(
1685             'context' => context_system::instance(),
1686             'objectid' => $post->id,
1687             'other' => array('discussionid' => $discussion->id, 'forumid' => $forum->id, 'forumtype' => $forum->type)
1688         );
1690         $this->setExpectedException('coding_exception', 'Context level must be CONTEXT_MODULE');
1691         \mod_forum\event\post_updated::create($params);
1692     }
1694     /**
1695      * Test post_updated event.
1696      */
1697     public function test_post_updated() {
1698         // Setup test data.
1699         $course = $this->getDataGenerator()->create_course();
1700         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
1701         $user = $this->getDataGenerator()->create_user();
1703         // Add a discussion.
1704         $record = array();
1705         $record['course'] = $course->id;
1706         $record['forum'] = $forum->id;
1707         $record['userid'] = $user->id;
1708         $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1710         // Add a post.
1711         $record = array();
1712         $record['discussion'] = $discussion->id;
1713         $record['userid'] = $user->id;
1714         $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
1716         $context = context_module::instance($forum->cmid);
1718         $params = array(
1719             'context' => $context,
1720             'objectid' => $post->id,
1721             'other' => array('discussionid' => $discussion->id, 'forumid' => $forum->id, 'forumtype' => $forum->type)
1722         );
1724         $event = \mod_forum\event\post_updated::create($params);
1726         // Trigger and capturing the event.
1727         $sink = $this->redirectEvents();
1728         $event->trigger();
1729         $events = $sink->get_events();
1730         $this->assertCount(1, $events);
1731         $event = reset($events);
1733         // Checking that the event contains the expected values.
1734         $this->assertInstanceOf('\mod_forum\event\post_updated', $event);
1735         $this->assertEquals($context, $event->get_context());
1736         $expected = array($course->id, 'forum', 'update post', "discuss.php?d={$discussion->id}#p{$post->id}",
1737             $post->id, $forum->cmid);
1738         $this->assertEventLegacyLogData($expected, $event);
1739         $url = new \moodle_url('/mod/forum/discuss.php', array('d' => $discussion->id));
1740         $url->set_anchor('p'.$event->objectid);
1741         $this->assertEquals($url, $event->get_url());
1742         $this->assertEventContextNotUsed($event);
1744         $this->assertNotEmpty($event->get_name());
1745     }
1747     /**
1748      * Test post_updated event.
1749      */
1750     public function test_post_updated_single() {
1751         // Setup test data.
1752         $course = $this->getDataGenerator()->create_course();
1753         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id, 'type' => 'single'));
1754         $user = $this->getDataGenerator()->create_user();
1756         // Add a discussion.
1757         $record = array();
1758         $record['course'] = $course->id;
1759         $record['forum'] = $forum->id;
1760         $record['userid'] = $user->id;
1761         $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1763         // Add a post.
1764         $record = array();
1765         $record['discussion'] = $discussion->id;
1766         $record['userid'] = $user->id;
1767         $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
1769         $context = context_module::instance($forum->cmid);
1771         $params = array(
1772             'context' => $context,
1773             'objectid' => $post->id,
1774             'other' => array('discussionid' => $discussion->id, 'forumid' => $forum->id, 'forumtype' => $forum->type)
1775         );
1777         $event = \mod_forum\event\post_updated::create($params);
1779         // Trigger and capturing the event.
1780         $sink = $this->redirectEvents();
1781         $event->trigger();
1782         $events = $sink->get_events();
1783         $this->assertCount(1, $events);
1784         $event = reset($events);
1786         // Checking that the event contains the expected values.
1787         $this->assertInstanceOf('\mod_forum\event\post_updated', $event);
1788         $this->assertEquals($context, $event->get_context());
1789         $expected = array($course->id, 'forum', 'update post', "view.php?f={$forum->id}#p{$post->id}",
1790             $post->id, $forum->cmid);
1791         $this->assertEventLegacyLogData($expected, $event);
1792         $url = new \moodle_url('/mod/forum/view.php', array('f' => $forum->id));
1793         $url->set_anchor('p'.$post->id);
1794         $this->assertEquals($url, $event->get_url());
1795         $this->assertEventContextNotUsed($event);
1797         $this->assertNotEmpty($event->get_name());
1798     }