MDL-45296 tests: Fix unit tests to have objectid
[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         $user = $this->getDataGenerator()->create_user();
625         $context = context_module::instance($forum->cmid);
627         // Add a subscription.
628         $record = array();
629         $record['course'] = $course->id;
630         $record['forum'] = $forum->id;
631         $record['userid'] = $user->id;
632         $subscription = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_subscription($record);
634         $params = array(
635             'context' => $context,
636             'objectid' => $subscription->id,
637             'other' => array('forumid' => $forum->id),
638             'relateduserid' => $user->id,
639         );
641         $event = \mod_forum\event\subscription_created::create($params);
643         // Trigger and capturing the event.
644         $sink = $this->redirectEvents();
645         $event->trigger();
646         $events = $sink->get_events();
647         $this->assertCount(1, $events);
648         $event = reset($events);
650         // Checking that the event contains the expected values.
651         $this->assertInstanceOf('\mod_forum\event\subscription_created', $event);
652         $this->assertEquals($context, $event->get_context());
653         $expected = array($course->id, 'forum', 'subscribe', "view.php?f={$forum->id}", $forum->id, $forum->cmid);
654         $this->assertEventLegacyLogData($expected, $event);
655         $url = new \moodle_url('/mod/forum/view.php', array('f' => $forum->id));
656         $this->assertEquals($url, $event->get_url());
657         $this->assertEventContextNotUsed($event);
659         $this->assertNotEmpty($event->get_name());
660     }
662     /**
663      * Ensure subscription_deleted event validates that the forumid is set.
664      */
665     public function test_subscription_deleted_forumid_validation() {
666         $user = $this->getDataGenerator()->create_user();
667         $course = $this->getDataGenerator()->create_course();
668         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
670         $params = array(
671             'context' => context_module::instance($forum->cmid),
672             'relateduserid' => $user->id,
673         );
675         $this->setExpectedException('coding_exception', 'The \'forumid\' value must be set in other.');
676         \mod_forum\event\subscription_deleted::create($params);
677     }
679     /**
680      * Ensure subscription_deleted event validates that the relateduserid is set.
681      */
682     public function test_subscription_deleted_relateduserid_validation() {
683         $course = $this->getDataGenerator()->create_course();
684         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
686         $params = array(
687             'context' => context_module::instance($forum->cmid),
688             'objectid' => $forum->id,
689         );
691         $this->setExpectedException('coding_exception', 'The \'relateduserid\' must be set.');
692         \mod_forum\event\subscription_deleted::create($params);
693     }
695     /**
696      * Ensure subscription_deleted event validates that the contextlevel is correct.
697      */
698     public function test_subscription_deleted_contextlevel_validation() {
699         $user = $this->getDataGenerator()->create_user();
700         $course = $this->getDataGenerator()->create_course();
701         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
703         $params = array(
704             'context' => context_system::instance(),
705             'other' => array('forumid' => $forum->id),
706             'relateduserid' => $user->id,
707         );
709         $this->setExpectedException('coding_exception', 'Context level must be CONTEXT_MODULE.');
710         \mod_forum\event\subscription_deleted::create($params);
711     }
713     /**
714      * Test the subscription_deleted event.
715      */
716     public function test_subscription_deleted() {
717         // Setup test data.
718         $user = $this->getDataGenerator()->create_user();
719         $course = $this->getDataGenerator()->create_course();
720         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
721         $user = $this->getDataGenerator()->create_user();
722         $context = context_module::instance($forum->cmid);
724         // Add a subscription.
725         $record = array();
726         $record['course'] = $course->id;
727         $record['forum'] = $forum->id;
728         $record['userid'] = $user->id;
729         $subscription = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_subscription($record);
731         $params = array(
732             'context' => $context,
733             'objectid' => $subscription->id,
734             'other' => array('forumid' => $forum->id),
735             'relateduserid' => $user->id,
736         );
738         $event = \mod_forum\event\subscription_deleted::create($params);
740         // Trigger and capturing the event.
741         $sink = $this->redirectEvents();
742         $event->trigger();
743         $events = $sink->get_events();
744         $this->assertCount(1, $events);
745         $event = reset($events);
747         // Checking that the event contains the expected values.
748         $this->assertInstanceOf('\mod_forum\event\subscription_deleted', $event);
749         $this->assertEquals($context, $event->get_context());
750         $expected = array($course->id, 'forum', 'unsubscribe', "view.php?f={$forum->id}", $forum->id, $forum->cmid);
751         $this->assertEventLegacyLogData($expected, $event);
752         $url = new \moodle_url('/mod/forum/view.php', array('f' => $forum->id));
753         $this->assertEquals($url, $event->get_url());
754         $this->assertEventContextNotUsed($event);
756         $this->assertNotEmpty($event->get_name());
757     }
759     /**
760      * Ensure readtracking_enabled event validates that the forumid is set.
761      */
762     public function test_readtracking_enabled_forumid_validation() {
763         $user = $this->getDataGenerator()->create_user();
764         $course = $this->getDataGenerator()->create_course();
765         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
767         $params = array(
768             'context' => context_module::instance($forum->cmid),
769             'relateduserid' => $user->id,
770         );
772         $this->setExpectedException('coding_exception', 'The \'forumid\' value must be set in other.');
773         \mod_forum\event\readtracking_enabled::create($params);
774     }
776     /**
777      * Ensure readtracking_enabled event validates that the relateduserid is set.
778      */
779     public function test_readtracking_enabled_relateduserid_validation() {
780         $course = $this->getDataGenerator()->create_course();
781         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
783         $params = array(
784             'context' => context_module::instance($forum->cmid),
785             'objectid' => $forum->id,
786         );
788         $this->setExpectedException('coding_exception', 'The \'relateduserid\' must be set.');
789         \mod_forum\event\readtracking_enabled::create($params);
790     }
792     /**
793      * Ensure readtracking_enabled event validates that the contextlevel is correct.
794      */
795     public function test_readtracking_enabled_contextlevel_validation() {
796         $user = $this->getDataGenerator()->create_user();
797         $course = $this->getDataGenerator()->create_course();
798         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
800         $params = array(
801             'context' => context_system::instance(),
802             'other' => array('forumid' => $forum->id),
803             'relateduserid' => $user->id,
804         );
806         $this->setExpectedException('coding_exception', 'Context level must be CONTEXT_MODULE.');
807         \mod_forum\event\readtracking_enabled::create($params);
808     }
810     /**
811      * Test the readtracking_enabled event.
812      */
813     public function test_readtracking_enabled() {
814         // Setup test data.
815         $user = $this->getDataGenerator()->create_user();
816         $course = $this->getDataGenerator()->create_course();
817         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
818         $context = context_module::instance($forum->cmid);
820         $params = array(
821             'context' => $context,
822             'other' => array('forumid' => $forum->id),
823             'relateduserid' => $user->id,
824         );
826         $event = \mod_forum\event\readtracking_enabled::create($params);
828         // Trigger and capture the event.
829         $sink = $this->redirectEvents();
830         $event->trigger();
831         $events = $sink->get_events();
832         $this->assertCount(1, $events);
833         $event = reset($events);
835         // Checking that the event contains the expected values.
836         $this->assertInstanceOf('\mod_forum\event\readtracking_enabled', $event);
837         $this->assertEquals($context, $event->get_context());
838         $expected = array($course->id, 'forum', 'start tracking', "view.php?f={$forum->id}", $forum->id, $forum->cmid);
839         $this->assertEventLegacyLogData($expected, $event);
840         $url = new \moodle_url('/mod/forum/view.php', array('f' => $forum->id));
841         $this->assertEquals($url, $event->get_url());
842         $this->assertEventContextNotUsed($event);
844         $this->assertNotEmpty($event->get_name());
845     }
847     /**
848      *  Ensure readtracking_disabled event validates that the forumid is set.
849      */
850     public function test_readtracking_disabled_forumid_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_module::instance($forum->cmid),
857             'relateduserid' => $user->id,
858         );
860         $this->setExpectedException('coding_exception', 'The \'forumid\' value must be set in other.');
861         \mod_forum\event\readtracking_disabled::create($params);
862     }
864     /**
865      *  Ensure readtracking_disabled event validates that the relateduserid is set.
866      */
867     public function test_readtracking_disabled_relateduserid_validation() {
868         $course = $this->getDataGenerator()->create_course();
869         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
871         $params = array(
872             'context' => context_module::instance($forum->cmid),
873             'objectid' => $forum->id,
874         );
876         $this->setExpectedException('coding_exception', 'The \'relateduserid\' must be set.');
877         \mod_forum\event\readtracking_disabled::create($params);
878     }
880     /**
881      *  Ensure readtracking_disabled event validates that the contextlevel is correct
882      */
883     public function test_readtracking_disabled_contextlevel_validation() {
884         $user = $this->getDataGenerator()->create_user();
885         $course = $this->getDataGenerator()->create_course();
886         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
888         $params = array(
889             'context' => context_system::instance(),
890             'other' => array('forumid' => $forum->id),
891             'relateduserid' => $user->id,
892         );
894         $this->setExpectedException('coding_exception', 'Context level must be CONTEXT_MODULE.');
895         \mod_forum\event\readtracking_disabled::create($params);
896     }
898     /**
899      *  Test the readtracking_disabled event.
900      */
901     public function test_readtracking_disabled() {
902         // Setup test data.
903         $user = $this->getDataGenerator()->create_user();
904         $course = $this->getDataGenerator()->create_course();
905         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
906         $context = context_module::instance($forum->cmid);
908         $params = array(
909             'context' => $context,
910             'other' => array('forumid' => $forum->id),
911             'relateduserid' => $user->id,
912         );
914         $event = \mod_forum\event\readtracking_disabled::create($params);
916         // Trigger and capture the event.
917         $sink = $this->redirectEvents();
918         $event->trigger();
919         $events = $sink->get_events();
920         $this->assertCount(1, $events);
921         $event = reset($events);
923         // Checking that the event contains the expected values.
924         $this->assertInstanceOf('\mod_forum\event\readtracking_disabled', $event);
925         $this->assertEquals($context, $event->get_context());
926         $expected = array($course->id, 'forum', 'stop tracking', "view.php?f={$forum->id}", $forum->id, $forum->cmid);
927         $this->assertEventLegacyLogData($expected, $event);
928         $url = new \moodle_url('/mod/forum/view.php', array('f' => $forum->id));
929         $this->assertEquals($url, $event->get_url());
930         $this->assertEventContextNotUsed($event);
932         $this->assertNotEmpty($event->get_name());
933     }
935     /**
936      *  Ensure subscribers_viewed event validates that the forumid is set.
937      */
938     public function test_subscribers_viewed_forumid_validation() {
939         $user = $this->getDataGenerator()->create_user();
940         $course = $this->getDataGenerator()->create_course();
941         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
943         $params = array(
944             'context' => context_module::instance($forum->cmid),
945             'relateduserid' => $user->id,
946         );
948         $this->setExpectedException('coding_exception', 'The \'forumid\' value must be set in other.');
949         \mod_forum\event\subscribers_viewed::create($params);
950     }
952     /**
953      *  Ensure subscribers_viewed event validates that the contextlevel is correct.
954      */
955     public function test_subscribers_viewed_contextlevel_validation() {
956         $user = $this->getDataGenerator()->create_user();
957         $course = $this->getDataGenerator()->create_course();
958         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
960         $params = array(
961             'context' => context_system::instance(),
962             'other' => array('forumid' => $forum->id),
963             'relateduserid' => $user->id,
964         );
966         $this->setExpectedException('coding_exception', 'Context level must be CONTEXT_MODULE.');
967         \mod_forum\event\subscribers_viewed::create($params);
968     }
970     /**
971      *  Test the subscribers_viewed event.
972      */
973     public function test_subscribers_viewed() {
974         // Setup test data.
975         $course = $this->getDataGenerator()->create_course();
976         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
977         $context = context_module::instance($forum->cmid);
979         $params = array(
980             'context' => $context,
981             'other' => array('forumid' => $forum->id),
982         );
984         $event = \mod_forum\event\subscribers_viewed::create($params);
986         // Trigger and capture the event.
987         $sink = $this->redirectEvents();
988         $event->trigger();
989         $events = $sink->get_events();
990         $this->assertCount(1, $events);
991         $event = reset($events);
993         // Checking that the event contains the expected values.
994         $this->assertInstanceOf('\mod_forum\event\subscribers_viewed', $event);
995         $this->assertEquals($context, $event->get_context());
996         $expected = array($course->id, 'forum', 'view subscribers', "subscribers.php?id={$forum->id}", $forum->id, $forum->cmid);
997         $this->assertEventLegacyLogData($expected, $event);
998         $this->assertEventContextNotUsed($event);
1000         $this->assertNotEmpty($event->get_name());
1001     }
1003     /**
1004      *  Ensure user_report_viewed event validates that the reportmode is set.
1005      */
1006     public function test_user_report_viewed_reportmode_validation() {
1007         $user = $this->getDataGenerator()->create_user();
1008         $course = $this->getDataGenerator()->create_course();
1010         $params = array(
1011             'context' => context_course::instance($course->id),
1012             'relateduserid' => $user->id,
1013         );
1015         $this->setExpectedException('coding_exception', 'The \'reportmode\' value must be set in other.');
1016         \mod_forum\event\user_report_viewed::create($params);
1017     }
1019     /**
1020      *  Ensure user_report_viewed event validates that the contextlevel is correct.
1021      */
1022     public function test_user_report_viewed_contextlevel_validation() {
1023         $user = $this->getDataGenerator()->create_user();
1024         $course = $this->getDataGenerator()->create_course();
1025         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
1027         $params = array(
1028             'context' => context_module::instance($forum->id),
1029             'other' => array('reportmode' => 'posts'),
1030             'relateduserid' => $user->id,
1031         );
1033         $this->setExpectedException('coding_exception', 'Context level must be either CONTEXT_SYSTEM or CONTEXT_COURSE.');
1034         \mod_forum\event\user_report_viewed::create($params);
1035     }
1037     /**
1038      *  Ensure user_report_viewed event validates that the relateduserid is set.
1039      */
1040     public function test_user_report_viewed_relateduserid_validation() {
1042         $params = array(
1043             'context' => context_system::instance(),
1044             'other' => array('reportmode' => 'posts'),
1045         );
1047         $this->setExpectedException('coding_exception', 'The \'relateduserid\' must be set.');
1048         \mod_forum\event\user_report_viewed::create($params);
1049     }
1051     /**
1052      * Test the user_report_viewed event.
1053      */
1054     public function test_user_report_viewed() {
1055         // Setup test data.
1056         $user = $this->getDataGenerator()->create_user();
1057         $course = $this->getDataGenerator()->create_course();
1058         $context = context_course::instance($course->id);
1060         $params = array(
1061             'context' => $context,
1062             'relateduserid' => $user->id,
1063             'other' => array('reportmode' => 'discussions'),
1064         );
1066         $event = \mod_forum\event\user_report_viewed::create($params);
1068         // Trigger and capture the event.
1069         $sink = $this->redirectEvents();
1070         $event->trigger();
1071         $events = $sink->get_events();
1072         $this->assertCount(1, $events);
1073         $event = reset($events);
1075         // Checking that the event contains the expected values.
1076         $this->assertInstanceOf('\mod_forum\event\user_report_viewed', $event);
1077         $this->assertEquals($context, $event->get_context());
1078         $expected = array($course->id, 'forum', 'user report',
1079             "user.php?id={$user->id}&amp;mode=discussions&amp;course={$course->id}", $user->id);
1080         $this->assertEventLegacyLogData($expected, $event);
1081         $this->assertEventContextNotUsed($event);
1083         $this->assertNotEmpty($event->get_name());
1084     }
1086     /**
1087      *  Ensure post_created event validates that the postid is set.
1088      */
1089     public function test_post_created_postid_validation() {
1090         $course = $this->getDataGenerator()->create_course();
1091         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
1092         $user = $this->getDataGenerator()->create_user();
1094         // Add a discussion.
1095         $record = array();
1096         $record['course'] = $course->id;
1097         $record['forum'] = $forum->id;
1098         $record['userid'] = $user->id;
1099         $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1101         $params = array(
1102             'context' => context_module::instance($forum->cmid),
1103             'other' => array('forumid' => $forum->id, 'forumtype' => $forum->type, 'discussionid' => $discussion->id)
1104         );
1106         \mod_forum\event\post_created::create($params);
1107     }
1109     /**
1110      *  Ensure post_created event validates that the discussionid is set.
1111      */
1112     public function test_post_created_discussionid_validation() {
1113         $course = $this->getDataGenerator()->create_course();
1114         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
1115         $user = $this->getDataGenerator()->create_user();
1117         // Add a discussion.
1118         $record = array();
1119         $record['course'] = $course->id;
1120         $record['forum'] = $forum->id;
1121         $record['userid'] = $user->id;
1122         $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1124         // Add a post.
1125         $record = array();
1126         $record['discussion'] = $discussion->id;
1127         $record['userid'] = $user->id;
1128         $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
1130         $params = array(
1131             'context' => context_module::instance($forum->cmid),
1132             'objectid' => $post->id,
1133             'other' => array('forumid' => $forum->id, 'forumtype' => $forum->type)
1134         );
1136         $this->setExpectedException('coding_exception', 'The \'discussionid\' value must be set in other.');
1137         \mod_forum\event\post_created::create($params);
1138     }
1140     /**
1141      *  Ensure post_created event validates that the forumid is set.
1142      */
1143     public function test_post_created_forumid_validation() {
1144         $course = $this->getDataGenerator()->create_course();
1145         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
1146         $user = $this->getDataGenerator()->create_user();
1148         // Add a discussion.
1149         $record = array();
1150         $record['course'] = $course->id;
1151         $record['forum'] = $forum->id;
1152         $record['userid'] = $user->id;
1153         $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1155         // Add a post.
1156         $record = array();
1157         $record['discussion'] = $discussion->id;
1158         $record['userid'] = $user->id;
1159         $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
1161         $params = array(
1162             'context' => context_module::instance($forum->cmid),
1163             'objectid' => $post->id,
1164             'other' => array('discussionid' => $discussion->id, 'forumtype' => $forum->type)
1165         );
1167         $this->setExpectedException('coding_exception', 'The \'forumid\' value must be set in other.');
1168         \mod_forum\event\post_created::create($params);
1169     }
1171     /**
1172      *  Ensure post_created event validates that the forumtype is set.
1173      */
1174     public function test_post_created_forumtype_validation() {
1175         $course = $this->getDataGenerator()->create_course();
1176         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
1177         $user = $this->getDataGenerator()->create_user();
1179         // Add a discussion.
1180         $record = array();
1181         $record['course'] = $course->id;
1182         $record['forum'] = $forum->id;
1183         $record['userid'] = $user->id;
1184         $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1186         // Add a post.
1187         $record = array();
1188         $record['discussion'] = $discussion->id;
1189         $record['userid'] = $user->id;
1190         $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
1192         $params = array(
1193             'context' => context_module::instance($forum->cmid),
1194             'objectid' => $post->id,
1195             'other' => array('discussionid' => $discussion->id, 'forumid' => $forum->id)
1196         );
1198         $this->setExpectedException('coding_exception', 'The \'forumtype\' value must be set in other.');
1199         \mod_forum\event\post_created::create($params);
1200     }
1202     /**
1203      *  Ensure post_created event validates that the contextlevel is correct.
1204      */
1205     public function test_post_created_context_validation() {
1206         $course = $this->getDataGenerator()->create_course();
1207         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
1208         $user = $this->getDataGenerator()->create_user();
1210         // Add a discussion.
1211         $record = array();
1212         $record['course'] = $course->id;
1213         $record['forum'] = $forum->id;
1214         $record['userid'] = $user->id;
1215         $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1217         // Add a post.
1218         $record = array();
1219         $record['discussion'] = $discussion->id;
1220         $record['userid'] = $user->id;
1221         $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
1223         $params = array(
1224             'context' => context_system::instance(),
1225             'objectid' => $post->id,
1226             'other' => array('discussionid' => $discussion->id, 'forumid' => $forum->id, 'forumtype' => $forum->type)
1227         );
1229         $this->setExpectedException('coding_exception', 'Context level must be CONTEXT_MODULE');
1230         \mod_forum\event\post_created::create($params);
1231     }
1233     /**
1234      * Test the post_created event.
1235      */
1236     public function test_post_created() {
1237         // Setup test data.
1238         $course = $this->getDataGenerator()->create_course();
1239         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
1240         $user = $this->getDataGenerator()->create_user();
1242         // Add a discussion.
1243         $record = array();
1244         $record['course'] = $course->id;
1245         $record['forum'] = $forum->id;
1246         $record['userid'] = $user->id;
1247         $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1249         // Add a post.
1250         $record = array();
1251         $record['discussion'] = $discussion->id;
1252         $record['userid'] = $user->id;
1253         $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
1255         $context = context_module::instance($forum->cmid);
1257         $params = array(
1258             'context' => $context,
1259             'objectid' => $post->id,
1260             'other' => array('discussionid' => $discussion->id, 'forumid' => $forum->id, 'forumtype' => $forum->type)
1261         );
1263         $event = \mod_forum\event\post_created::create($params);
1265         // Trigger and capturing the event.
1266         $sink = $this->redirectEvents();
1267         $event->trigger();
1268         $events = $sink->get_events();
1269         $this->assertCount(1, $events);
1270         $event = reset($events);
1272         // Checking that the event contains the expected values.
1273         $this->assertInstanceOf('\mod_forum\event\post_created', $event);
1274         $this->assertEquals($context, $event->get_context());
1275         $expected = array($course->id, 'forum', 'add post', "discuss.php?d={$discussion->id}#p{$post->id}",
1276             $forum->id, $forum->cmid);
1277         $this->assertEventLegacyLogData($expected, $event);
1278         $url = new \moodle_url('/mod/forum/discuss.php', array('d' => $discussion->id));
1279         $url->set_anchor('p'.$event->objectid);
1280         $this->assertEquals($url, $event->get_url());
1281         $this->assertEventContextNotUsed($event);
1283         $this->assertNotEmpty($event->get_name());
1284     }
1286     /**
1287      * Test the post_created event for a single discussion forum.
1288      */
1289     public function test_post_created_single() {
1290         // Setup test data.
1291         $course = $this->getDataGenerator()->create_course();
1292         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id, 'type' => 'single'));
1293         $user = $this->getDataGenerator()->create_user();
1295         // Add a discussion.
1296         $record = array();
1297         $record['course'] = $course->id;
1298         $record['forum'] = $forum->id;
1299         $record['userid'] = $user->id;
1300         $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1302         // Add a post.
1303         $record = array();
1304         $record['discussion'] = $discussion->id;
1305         $record['userid'] = $user->id;
1306         $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
1308         $context = context_module::instance($forum->cmid);
1310         $params = array(
1311             'context' => $context,
1312             'objectid' => $post->id,
1313             'other' => array('discussionid' => $discussion->id, 'forumid' => $forum->id, 'forumtype' => $forum->type)
1314         );
1316         $event = \mod_forum\event\post_created::create($params);
1318         // Trigger and capturing the event.
1319         $sink = $this->redirectEvents();
1320         $event->trigger();
1321         $events = $sink->get_events();
1322         $this->assertCount(1, $events);
1323         $event = reset($events);
1325         // Checking that the event contains the expected values.
1326         $this->assertInstanceOf('\mod_forum\event\post_created', $event);
1327         $this->assertEquals($context, $event->get_context());
1328         $expected = array($course->id, 'forum', 'add post', "view.php?f={$forum->id}#p{$post->id}",
1329             $forum->id, $forum->cmid);
1330         $this->assertEventLegacyLogData($expected, $event);
1331         $url = new \moodle_url('/mod/forum/view.php', array('f' => $forum->id));
1332         $url->set_anchor('p'.$event->objectid);
1333         $this->assertEquals($url, $event->get_url());
1334         $this->assertEventContextNotUsed($event);
1336         $this->assertNotEmpty($event->get_name());
1337     }
1339     /**
1340      *  Ensure post_deleted event validates that the postid is set.
1341      */
1342     public function test_post_deleted_postid_validation() {
1343         $course = $this->getDataGenerator()->create_course();
1344         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
1345         $user = $this->getDataGenerator()->create_user();
1347         // Add a discussion.
1348         $record = array();
1349         $record['course'] = $course->id;
1350         $record['forum'] = $forum->id;
1351         $record['userid'] = $user->id;
1352         $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1354         $params = array(
1355             'context' => context_module::instance($forum->cmid),
1356             'other' => array('forumid' => $forum->id, 'forumtype' => $forum->type, 'discussionid' => $discussion->id)
1357         );
1359         \mod_forum\event\post_deleted::create($params);
1360     }
1362     /**
1363      *  Ensure post_deleted event validates that the discussionid is set.
1364      */
1365     public function test_post_deleted_discussionid_validation() {
1366         $course = $this->getDataGenerator()->create_course();
1367         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
1368         $user = $this->getDataGenerator()->create_user();
1370         // Add a discussion.
1371         $record = array();
1372         $record['course'] = $course->id;
1373         $record['forum'] = $forum->id;
1374         $record['userid'] = $user->id;
1375         $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1377         // Add a post.
1378         $record = array();
1379         $record['discussion'] = $discussion->id;
1380         $record['userid'] = $user->id;
1381         $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
1383         $params = array(
1384             'context' => context_module::instance($forum->cmid),
1385             'objectid' => $post->id,
1386             'other' => array('forumid' => $forum->id, 'forumtype' => $forum->type)
1387         );
1389         $this->setExpectedException('coding_exception', 'The \'discussionid\' value must be set in other.');
1390         \mod_forum\event\post_deleted::create($params);
1391     }
1393     /**
1394      *  Ensure post_deleted event validates that the forumid is set.
1395      */
1396     public function test_post_deleted_forumid_validation() {
1397         $course = $this->getDataGenerator()->create_course();
1398         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
1399         $user = $this->getDataGenerator()->create_user();
1401         // Add a discussion.
1402         $record = array();
1403         $record['course'] = $course->id;
1404         $record['forum'] = $forum->id;
1405         $record['userid'] = $user->id;
1406         $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1408         // Add a post.
1409         $record = array();
1410         $record['discussion'] = $discussion->id;
1411         $record['userid'] = $user->id;
1412         $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
1414         $params = array(
1415             'context' => context_module::instance($forum->cmid),
1416             'objectid' => $post->id,
1417             'other' => array('discussionid' => $discussion->id, 'forumtype' => $forum->type)
1418         );
1420         $this->setExpectedException('coding_exception', 'The \'forumid\' value must be set in other.');
1421         \mod_forum\event\post_deleted::create($params);
1422     }
1424     /**
1425      *  Ensure post_deleted event validates that the forumtype is set.
1426      */
1427     public function test_post_deleted_forumtype_validation() {
1428         $course = $this->getDataGenerator()->create_course();
1429         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
1430         $user = $this->getDataGenerator()->create_user();
1432         // Add a discussion.
1433         $record = array();
1434         $record['course'] = $course->id;
1435         $record['forum'] = $forum->id;
1436         $record['userid'] = $user->id;
1437         $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1439         // Add a post.
1440         $record = array();
1441         $record['discussion'] = $discussion->id;
1442         $record['userid'] = $user->id;
1443         $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
1445         $params = array(
1446             'context' => context_module::instance($forum->cmid),
1447             'objectid' => $post->id,
1448             'other' => array('discussionid' => $discussion->id, 'forumid' => $forum->id)
1449         );
1451         $this->setExpectedException('coding_exception', 'The \'forumtype\' value must be set in other.');
1452         \mod_forum\event\post_deleted::create($params);
1453     }
1455     /**
1456      *  Ensure post_deleted event validates that the contextlevel is correct.
1457      */
1458     public function test_post_deleted_context_validation() {
1459         $course = $this->getDataGenerator()->create_course();
1460         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
1461         $user = $this->getDataGenerator()->create_user();
1463         // Add a discussion.
1464         $record = array();
1465         $record['course'] = $course->id;
1466         $record['forum'] = $forum->id;
1467         $record['userid'] = $user->id;
1468         $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1470         // Add a post.
1471         $record = array();
1472         $record['discussion'] = $discussion->id;
1473         $record['userid'] = $user->id;
1474         $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
1476         $params = array(
1477             'context' => context_system::instance(),
1478             'objectid' => $post->id,
1479             'other' => array('discussionid' => $discussion->id, 'forumid' => $forum->id, 'forumtype' => $forum->type)
1480         );
1482         $this->setExpectedException('coding_exception', 'Context level must be CONTEXT_MODULE');
1483         \mod_forum\event\post_deleted::create($params);
1484     }
1486     /**
1487      * Test post_deleted event.
1488      */
1489     public function test_post_deleted() {
1490         // Setup test data.
1491         $course = $this->getDataGenerator()->create_course();
1492         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
1493         $user = $this->getDataGenerator()->create_user();
1495         // Add a discussion.
1496         $record = array();
1497         $record['course'] = $course->id;
1498         $record['forum'] = $forum->id;
1499         $record['userid'] = $user->id;
1500         $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1502         // Add a post.
1503         $record = array();
1504         $record['discussion'] = $discussion->id;
1505         $record['userid'] = $user->id;
1506         $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
1508         $context = context_module::instance($forum->cmid);
1510         $params = array(
1511             'context' => $context,
1512             'objectid' => $post->id,
1513             'other' => array('discussionid' => $discussion->id, 'forumid' => $forum->id, 'forumtype' => $forum->type)
1514         );
1516         $event = \mod_forum\event\post_deleted::create($params);
1518         // Trigger and capture the event.
1519         $sink = $this->redirectEvents();
1520         $event->trigger();
1521         $events = $sink->get_events();
1522         $this->assertCount(1, $events);
1523         $event = reset($events);
1525         // Checking that the event contains the expected values.
1526         $this->assertInstanceOf('\mod_forum\event\post_deleted', $event);
1527         $this->assertEquals($context, $event->get_context());
1528         $expected = array($course->id, 'forum', 'delete post', "discuss.php?d={$discussion->id}", $post->id, $forum->cmid);
1529         $this->assertEventLegacyLogData($expected, $event);
1530         $url = new \moodle_url('/mod/forum/discuss.php', array('d' => $discussion->id));
1531         $this->assertEquals($url, $event->get_url());
1532         $this->assertEventContextNotUsed($event);
1534         $this->assertNotEmpty($event->get_name());
1535     }
1537     /**
1538      * Test post_deleted event for a single discussion forum.
1539      */
1540     public function test_post_deleted_single() {
1541         // Setup test data.
1542         $course = $this->getDataGenerator()->create_course();
1543         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id, 'type' => 'single'));
1544         $user = $this->getDataGenerator()->create_user();
1546         // Add a discussion.
1547         $record = array();
1548         $record['course'] = $course->id;
1549         $record['forum'] = $forum->id;
1550         $record['userid'] = $user->id;
1551         $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1553         // Add a post.
1554         $record = array();
1555         $record['discussion'] = $discussion->id;
1556         $record['userid'] = $user->id;
1557         $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
1559         $context = context_module::instance($forum->cmid);
1561         $params = array(
1562             'context' => $context,
1563             'objectid' => $post->id,
1564             'other' => array('discussionid' => $discussion->id, 'forumid' => $forum->id, 'forumtype' => $forum->type)
1565         );
1567         $event = \mod_forum\event\post_deleted::create($params);
1569         // Trigger and capture the event.
1570         $sink = $this->redirectEvents();
1571         $event->trigger();
1572         $events = $sink->get_events();
1573         $this->assertCount(1, $events);
1574         $event = reset($events);
1576         // Checking that the event contains the expected values.
1577         $this->assertInstanceOf('\mod_forum\event\post_deleted', $event);
1578         $this->assertEquals($context, $event->get_context());
1579         $expected = array($course->id, 'forum', 'delete post', "view.php?f={$forum->id}", $post->id, $forum->cmid);
1580         $this->assertEventLegacyLogData($expected, $event);
1581         $url = new \moodle_url('/mod/forum/view.php', array('f' => $forum->id));
1582         $this->assertEquals($url, $event->get_url());
1583         $this->assertEventContextNotUsed($event);
1585         $this->assertNotEmpty($event->get_name());
1586     }
1588     /**
1589      *  Ensure post_updated event validates that the discussionid is set.
1590      */
1591     public function test_post_updated_discussionid_validation() {
1592         $course = $this->getDataGenerator()->create_course();
1593         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
1594         $user = $this->getDataGenerator()->create_user();
1596         // Add a discussion.
1597         $record = array();
1598         $record['course'] = $course->id;
1599         $record['forum'] = $forum->id;
1600         $record['userid'] = $user->id;
1601         $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1603         // Add a post.
1604         $record = array();
1605         $record['discussion'] = $discussion->id;
1606         $record['userid'] = $user->id;
1607         $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
1609         $params = array(
1610             'context' => context_module::instance($forum->cmid),
1611             'objectid' => $post->id,
1612             'other' => array('forumid' => $forum->id, 'forumtype' => $forum->type)
1613         );
1615         $this->setExpectedException('coding_exception', 'The \'discussionid\' value must be set in other.');
1616         \mod_forum\event\post_updated::create($params);
1617     }
1619     /**
1620      *  Ensure post_updated event validates that the forumid is set.
1621      */
1622     public function test_post_updated_forumid_validation() {
1623         $course = $this->getDataGenerator()->create_course();
1624         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
1625         $user = $this->getDataGenerator()->create_user();
1627         // Add a discussion.
1628         $record = array();
1629         $record['course'] = $course->id;
1630         $record['forum'] = $forum->id;
1631         $record['userid'] = $user->id;
1632         $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1634         // Add a post.
1635         $record = array();
1636         $record['discussion'] = $discussion->id;
1637         $record['userid'] = $user->id;
1638         $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
1640         $params = array(
1641             'context' => context_module::instance($forum->cmid),
1642             'objectid' => $post->id,
1643             'other' => array('discussionid' => $discussion->id, 'forumtype' => $forum->type)
1644         );
1646         $this->setExpectedException('coding_exception', 'The \'forumid\' value must be set in other.');
1647         \mod_forum\event\post_updated::create($params);
1648     }
1650     /**
1651      *  Ensure post_updated event validates that the forumtype is set.
1652      */
1653     public function test_post_updated_forumtype_validation() {
1654         $course = $this->getDataGenerator()->create_course();
1655         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
1656         $user = $this->getDataGenerator()->create_user();
1658         // Add a discussion.
1659         $record = array();
1660         $record['course'] = $course->id;
1661         $record['forum'] = $forum->id;
1662         $record['userid'] = $user->id;
1663         $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1665         // Add a post.
1666         $record = array();
1667         $record['discussion'] = $discussion->id;
1668         $record['userid'] = $user->id;
1669         $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
1671         $params = array(
1672             'context' => context_module::instance($forum->cmid),
1673             'objectid' => $post->id,
1674             'other' => array('discussionid' => $discussion->id, 'forumid' => $forum->id)
1675         );
1677         $this->setExpectedException('coding_exception', 'The \'forumtype\' value must be set in other.');
1678         \mod_forum\event\post_updated::create($params);
1679     }
1681     /**
1682      *  Ensure post_updated event validates that the contextlevel is correct.
1683      */
1684     public function test_post_updated_context_validation() {
1685         $course = $this->getDataGenerator()->create_course();
1686         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
1687         $user = $this->getDataGenerator()->create_user();
1689         // Add a discussion.
1690         $record = array();
1691         $record['course'] = $course->id;
1692         $record['forum'] = $forum->id;
1693         $record['userid'] = $user->id;
1694         $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1696         // Add a post.
1697         $record = array();
1698         $record['discussion'] = $discussion->id;
1699         $record['userid'] = $user->id;
1700         $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
1702         $params = array(
1703             'context' => context_system::instance(),
1704             'objectid' => $post->id,
1705             'other' => array('discussionid' => $discussion->id, 'forumid' => $forum->id, 'forumtype' => $forum->type)
1706         );
1708         $this->setExpectedException('coding_exception', 'Context level must be CONTEXT_MODULE');
1709         \mod_forum\event\post_updated::create($params);
1710     }
1712     /**
1713      * Test post_updated event.
1714      */
1715     public function test_post_updated() {
1716         // Setup test data.
1717         $course = $this->getDataGenerator()->create_course();
1718         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
1719         $user = $this->getDataGenerator()->create_user();
1721         // Add a discussion.
1722         $record = array();
1723         $record['course'] = $course->id;
1724         $record['forum'] = $forum->id;
1725         $record['userid'] = $user->id;
1726         $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1728         // Add a post.
1729         $record = array();
1730         $record['discussion'] = $discussion->id;
1731         $record['userid'] = $user->id;
1732         $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
1734         $context = context_module::instance($forum->cmid);
1736         $params = array(
1737             'context' => $context,
1738             'objectid' => $post->id,
1739             'other' => array('discussionid' => $discussion->id, 'forumid' => $forum->id, 'forumtype' => $forum->type)
1740         );
1742         $event = \mod_forum\event\post_updated::create($params);
1744         // Trigger and capturing the event.
1745         $sink = $this->redirectEvents();
1746         $event->trigger();
1747         $events = $sink->get_events();
1748         $this->assertCount(1, $events);
1749         $event = reset($events);
1751         // Checking that the event contains the expected values.
1752         $this->assertInstanceOf('\mod_forum\event\post_updated', $event);
1753         $this->assertEquals($context, $event->get_context());
1754         $expected = array($course->id, 'forum', 'update post', "discuss.php?d={$discussion->id}#p{$post->id}",
1755             $post->id, $forum->cmid);
1756         $this->assertEventLegacyLogData($expected, $event);
1757         $url = new \moodle_url('/mod/forum/discuss.php', array('d' => $discussion->id));
1758         $url->set_anchor('p'.$event->objectid);
1759         $this->assertEquals($url, $event->get_url());
1760         $this->assertEventContextNotUsed($event);
1762         $this->assertNotEmpty($event->get_name());
1763     }
1765     /**
1766      * Test post_updated event.
1767      */
1768     public function test_post_updated_single() {
1769         // Setup test data.
1770         $course = $this->getDataGenerator()->create_course();
1771         $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id, 'type' => 'single'));
1772         $user = $this->getDataGenerator()->create_user();
1774         // Add a discussion.
1775         $record = array();
1776         $record['course'] = $course->id;
1777         $record['forum'] = $forum->id;
1778         $record['userid'] = $user->id;
1779         $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1781         // Add a post.
1782         $record = array();
1783         $record['discussion'] = $discussion->id;
1784         $record['userid'] = $user->id;
1785         $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
1787         $context = context_module::instance($forum->cmid);
1789         $params = array(
1790             'context' => $context,
1791             'objectid' => $post->id,
1792             'other' => array('discussionid' => $discussion->id, 'forumid' => $forum->id, 'forumtype' => $forum->type)
1793         );
1795         $event = \mod_forum\event\post_updated::create($params);
1797         // Trigger and capturing the event.
1798         $sink = $this->redirectEvents();
1799         $event->trigger();
1800         $events = $sink->get_events();
1801         $this->assertCount(1, $events);
1802         $event = reset($events);
1804         // Checking that the event contains the expected values.
1805         $this->assertInstanceOf('\mod_forum\event\post_updated', $event);
1806         $this->assertEquals($context, $event->get_context());
1807         $expected = array($course->id, 'forum', 'update post', "view.php?f={$forum->id}#p{$post->id}",
1808             $post->id, $forum->cmid);
1809         $this->assertEventLegacyLogData($expected, $event);
1810         $url = new \moodle_url('/mod/forum/view.php', array('f' => $forum->id));
1811         $url->set_anchor('p'.$post->id);
1812         $this->assertEquals($url, $event->get_url());
1813         $this->assertEventContextNotUsed($event);
1815         $this->assertNotEmpty($event->get_name());
1816     }