2 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
18 * Tests for forum events.
22 * @copyright 2014 Dan Poltawski <dan@moodle.com>
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 defined('MOODLE_INTERNAL') || die();
29 * Tests for forum events.
33 * @copyright 2014 Dan Poltawski <dan@moodle.com>
34 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
36 class mod_forum_events_testcase extends advanced_testcase {
41 public function setUp() {
42 // We must clear the subscription caches. This has to be done both before each test, and after in case of other
43 // tests using these functions.
44 \mod_forum\subscriptions::reset_forum_cache();
46 $this->resetAfterTest();
49 public function tearDown() {
50 // We must clear the subscription caches. This has to be done both before each test, and after in case of other
51 // tests using these functions.
52 \mod_forum\subscriptions::reset_forum_cache();
56 * Ensure course_searched event validates that searchterm is set.
58 public function test_course_searched_searchterm_validation() {
59 $course = $this->getDataGenerator()->create_course();
60 $coursectx = context_course::instance($course->id);
62 'context' => $coursectx,
65 $this->setExpectedException('coding_exception', 'The \'searchterm\' value must be set in other.');
66 \mod_forum\event\course_searched::create($params);
70 * Ensure course_searched event validates that context is the correct level.
72 public function test_course_searched_context_validation() {
73 $course = $this->getDataGenerator()->create_course();
74 $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
75 $context = context_module::instance($forum->cmid);
77 'context' => $context,
78 'other' => array('searchterm' => 'testing'),
81 $this->setExpectedException('coding_exception', 'Context level must be CONTEXT_COURSE.');
82 \mod_forum\event\course_searched::create($params);
86 * Test course_searched event.
88 public function test_course_searched() {
91 $course = $this->getDataGenerator()->create_course();
92 $coursectx = context_course::instance($course->id);
93 $searchterm = 'testing123';
96 'context' => $coursectx,
97 'other' => array('searchterm' => $searchterm),
101 $event = \mod_forum\event\course_searched::create($params);
103 // Trigger and capture the event.
104 $sink = $this->redirectEvents();
106 $events = $sink->get_events();
107 $this->assertCount(1, $events);
108 $event = reset($events);
110 // Checking that the event contains the expected values.
111 $this->assertInstanceOf('\mod_forum\event\course_searched', $event);
112 $this->assertEquals($coursectx, $event->get_context());
113 $expected = array($course->id, 'forum', 'search', "search.php?id={$course->id}&search={$searchterm}", $searchterm);
114 $this->assertEventLegacyLogData($expected, $event);
115 $this->assertEventContextNotUsed($event);
117 $this->assertNotEmpty($event->get_name());
121 * Ensure discussion_created event validates that forumid is set.
123 public function test_discussion_created_forumid_validation() {
124 $course = $this->getDataGenerator()->create_course();
125 $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
126 $context = context_module::instance($forum->cmid);
129 'context' => $context,
132 $this->setExpectedException('coding_exception', 'The \'forumid\' value must be set in other.');
133 \mod_forum\event\discussion_created::create($params);
137 * Ensure discussion_created event validates that the context is the correct level.
139 public function test_discussion_created_context_validation() {
140 $course = $this->getDataGenerator()->create_course();
141 $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
144 'context' => context_system::instance(),
145 'other' => array('forumid' => $forum->id),
148 $this->setExpectedException('coding_exception', 'Context level must be CONTEXT_MODULE.');
149 \mod_forum\event\discussion_created::create($params);
153 * Test discussion_created event.
155 public function test_discussion_created() {
158 $course = $this->getDataGenerator()->create_course();
159 $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
160 $user = $this->getDataGenerator()->create_user();
164 $record['course'] = $course->id;
165 $record['forum'] = $forum->id;
166 $record['userid'] = $user->id;
167 $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
169 $context = context_module::instance($forum->cmid);
172 'context' => $context,
173 'objectid' => $discussion->id,
174 'other' => array('forumid' => $forum->id),
178 $event = \mod_forum\event\discussion_created::create($params);
180 // Trigger and capturing the event.
181 $sink = $this->redirectEvents();
183 $events = $sink->get_events();
184 $this->assertCount(1, $events);
185 $event = reset($events);
187 // Check that the event contains the expected values.
188 $this->assertInstanceOf('\mod_forum\event\discussion_created', $event);
189 $this->assertEquals($context, $event->get_context());
190 $expected = array($course->id, 'forum', 'add discussion', "discuss.php?d={$discussion->id}", $discussion->id, $forum->cmid);
191 $this->assertEventLegacyLogData($expected, $event);
192 $this->assertEventContextNotUsed($event);
194 $this->assertNotEmpty($event->get_name());
198 * Ensure discussion_updated event validates that forumid is set.
200 public function test_discussion_updated_forumid_validation() {
201 $course = $this->getDataGenerator()->create_course();
202 $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
203 $context = context_module::instance($forum->cmid);
206 'context' => $context,
209 $this->setExpectedException('coding_exception', 'The \'forumid\' value must be set in other.');
210 \mod_forum\event\discussion_updated::create($params);
214 * Ensure discussion_created event validates that the context is the correct level.
216 public function test_discussion_updated_context_validation() {
217 $course = $this->getDataGenerator()->create_course();
218 $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
221 'context' => context_system::instance(),
222 'other' => array('forumid' => $forum->id),
225 $this->setExpectedException('coding_exception', 'Context level must be CONTEXT_MODULE.');
226 \mod_forum\event\discussion_updated::create($params);
230 * Test discussion_created event.
232 public function test_discussion_updated() {
235 $course = $this->getDataGenerator()->create_course();
236 $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
237 $user = $this->getDataGenerator()->create_user();
241 $record['course'] = $course->id;
242 $record['forum'] = $forum->id;
243 $record['userid'] = $user->id;
244 $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
246 $context = context_module::instance($forum->cmid);
249 'context' => $context,
250 'objectid' => $discussion->id,
251 'other' => array('forumid' => $forum->id),
255 $event = \mod_forum\event\discussion_updated::create($params);
257 // Trigger and capturing the event.
258 $sink = $this->redirectEvents();
260 $events = $sink->get_events();
261 $this->assertCount(1, $events);
262 $event = reset($events);
264 // Check that the event contains the expected values.
265 $this->assertInstanceOf('\mod_forum\event\discussion_updated', $event);
266 $this->assertEquals($context, $event->get_context());
267 $this->assertEventContextNotUsed($event);
269 $this->assertNotEmpty($event->get_name());
273 * Ensure discussion_deleted event validates that forumid is set.
275 public function test_discussion_deleted_forumid_validation() {
276 $course = $this->getDataGenerator()->create_course();
277 $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
278 $context = context_module::instance($forum->cmid);
281 'context' => $context,
284 $this->setExpectedException('coding_exception', 'The \'forumid\' value must be set in other.');
285 \mod_forum\event\discussion_deleted::create($params);
289 * Ensure discussion_deleted event validates that context is of the correct level.
291 public function test_discussion_deleted_context_validation() {
292 $course = $this->getDataGenerator()->create_course();
293 $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
296 'context' => context_system::instance(),
297 'other' => array('forumid' => $forum->id),
300 $this->setExpectedException('coding_exception', 'Context level must be CONTEXT_MODULE.');
301 \mod_forum\event\discussion_deleted::create($params);
305 * Test discussion_deleted event.
307 public function test_discussion_deleted() {
310 $course = $this->getDataGenerator()->create_course();
311 $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
312 $user = $this->getDataGenerator()->create_user();
316 $record['course'] = $course->id;
317 $record['forum'] = $forum->id;
318 $record['userid'] = $user->id;
319 $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
321 $context = context_module::instance($forum->cmid);
324 'context' => $context,
325 'objectid' => $discussion->id,
326 'other' => array('forumid' => $forum->id),
329 $event = \mod_forum\event\discussion_deleted::create($params);
331 // Trigger and capture the event.
332 $sink = $this->redirectEvents();
334 $events = $sink->get_events();
335 $this->assertCount(1, $events);
336 $event = reset($events);
338 // Checking that the event contains the expected values.
339 $this->assertInstanceOf('\mod_forum\event\discussion_deleted', $event);
340 $this->assertEquals($context, $event->get_context());
341 $expected = array($course->id, 'forum', 'delete discussion', "view.php?id={$forum->cmid}", $forum->id, $forum->cmid);
342 $this->assertEventLegacyLogData($expected, $event);
343 $this->assertEventContextNotUsed($event);
345 $this->assertNotEmpty($event->get_name());
349 * Ensure discussion_moved event validates that fromforumid is set.
351 public function test_discussion_moved_fromforumid_validation() {
352 $course = $this->getDataGenerator()->create_course();
353 $toforum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
355 $context = context_module::instance($toforum->cmid);
358 'context' => $context,
359 'other' => array('toforumid' => $toforum->id)
362 $this->setExpectedException('coding_exception', 'The \'fromforumid\' value must be set in other.');
363 \mod_forum\event\discussion_moved::create($params);
367 * Ensure discussion_moved event validates that toforumid is set.
369 public function test_discussion_moved_toforumid_validation() {
370 $course = $this->getDataGenerator()->create_course();
371 $fromforum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
372 $toforum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
373 $context = context_module::instance($toforum->cmid);
376 'context' => $context,
377 'other' => array('fromforumid' => $fromforum->id)
380 $this->setExpectedException('coding_exception', 'The \'toforumid\' value must be set in other.');
381 \mod_forum\event\discussion_moved::create($params);
385 * Ensure discussion_moved event validates that the context level is correct.
387 public function test_discussion_moved_context_validation() {
388 $course = $this->getDataGenerator()->create_course();
389 $fromforum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
390 $toforum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
391 $user = $this->getDataGenerator()->create_user();
395 $record['course'] = $course->id;
396 $record['forum'] = $fromforum->id;
397 $record['userid'] = $user->id;
398 $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
401 'context' => context_system::instance(),
402 'objectid' => $discussion->id,
403 'other' => array('fromforumid' => $fromforum->id, 'toforumid' => $toforum->id)
406 $this->setExpectedException('coding_exception', 'Context level must be CONTEXT_MODULE.');
407 \mod_forum\event\discussion_moved::create($params);
411 * Test discussion_moved event.
413 public function test_discussion_moved() {
415 $course = $this->getDataGenerator()->create_course();
416 $fromforum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
417 $toforum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
418 $user = $this->getDataGenerator()->create_user();
422 $record['course'] = $course->id;
423 $record['forum'] = $fromforum->id;
424 $record['userid'] = $user->id;
425 $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
427 $context = context_module::instance($toforum->cmid);
430 'context' => $context,
431 'objectid' => $discussion->id,
432 'other' => array('fromforumid' => $fromforum->id, 'toforumid' => $toforum->id)
435 $event = \mod_forum\event\discussion_moved::create($params);
437 // Trigger and capture the event.
438 $sink = $this->redirectEvents();
440 $events = $sink->get_events();
441 $this->assertCount(1, $events);
442 $event = reset($events);
444 // Checking that the event contains the expected values.
445 $this->assertInstanceOf('\mod_forum\event\discussion_moved', $event);
446 $this->assertEquals($context, $event->get_context());
447 $expected = array($course->id, 'forum', 'move discussion', "discuss.php?d={$discussion->id}",
448 $discussion->id, $toforum->cmid);
449 $this->assertEventLegacyLogData($expected, $event);
450 $this->assertEventContextNotUsed($event);
452 $this->assertNotEmpty($event->get_name());
457 * Ensure discussion_viewed event validates that the contextlevel is correct.
459 public function test_discussion_viewed_context_validation() {
460 $course = $this->getDataGenerator()->create_course();
461 $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
462 $user = $this->getDataGenerator()->create_user();
466 $record['course'] = $course->id;
467 $record['forum'] = $forum->id;
468 $record['userid'] = $user->id;
469 $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
472 'context' => context_system::instance(),
473 'objectid' => $discussion->id,
476 $this->setExpectedException('coding_exception', 'Context level must be CONTEXT_MODULE.');
477 \mod_forum\event\discussion_viewed::create($params);
481 * Test discussion_viewed event.
483 public function test_discussion_viewed() {
485 $course = $this->getDataGenerator()->create_course();
486 $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
487 $user = $this->getDataGenerator()->create_user();
491 $record['course'] = $course->id;
492 $record['forum'] = $forum->id;
493 $record['userid'] = $user->id;
494 $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
496 $context = context_module::instance($forum->cmid);
499 'context' => $context,
500 'objectid' => $discussion->id,
503 $event = \mod_forum\event\discussion_viewed::create($params);
505 // Trigger and capture the event.
506 $sink = $this->redirectEvents();
508 $events = $sink->get_events();
509 $this->assertCount(1, $events);
510 $event = reset($events);
512 // Checking that the event contains the expected values.
513 $this->assertInstanceOf('\mod_forum\event\discussion_viewed', $event);
514 $this->assertEquals($context, $event->get_context());
515 $expected = array($course->id, 'forum', 'view discussion', "discuss.php?d={$discussion->id}",
516 $discussion->id, $forum->cmid);
517 $this->assertEventLegacyLogData($expected, $event);
518 $this->assertEventContextNotUsed($event);
520 $this->assertNotEmpty($event->get_name());
524 * Ensure course_module_viewed event validates that the contextlevel is correct.
526 public function test_course_module_viewed_context_validation() {
527 $course = $this->getDataGenerator()->create_course();
528 $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
531 'context' => context_system::instance(),
532 'objectid' => $forum->id,
535 $this->setExpectedException('coding_exception', 'Context level must be CONTEXT_MODULE.');
536 \mod_forum\event\course_module_viewed::create($params);
540 * Test the course_module_viewed event.
542 public function test_course_module_viewed() {
544 $course = $this->getDataGenerator()->create_course();
545 $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
547 $context = context_module::instance($forum->cmid);
550 'context' => $context,
551 'objectid' => $forum->id,
554 $event = \mod_forum\event\course_module_viewed::create($params);
556 // Trigger and capture the event.
557 $sink = $this->redirectEvents();
559 $events = $sink->get_events();
560 $this->assertCount(1, $events);
561 $event = reset($events);
563 // Checking that the event contains the expected values.
564 $this->assertInstanceOf('\mod_forum\event\course_module_viewed', $event);
565 $this->assertEquals($context, $event->get_context());
566 $expected = array($course->id, 'forum', 'view forum', "view.php?f={$forum->id}", $forum->id, $forum->cmid);
567 $this->assertEventLegacyLogData($expected, $event);
568 $url = new \moodle_url('/mod/forum/view.php', array('f' => $forum->id));
569 $this->assertEquals($url, $event->get_url());
570 $this->assertEventContextNotUsed($event);
572 $this->assertNotEmpty($event->get_name());
576 * Ensure subscription_created event validates that the forumid is set.
578 public function test_subscription_created_forumid_validation() {
579 $user = $this->getDataGenerator()->create_user();
580 $course = $this->getDataGenerator()->create_course();
581 $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
584 'context' => context_module::instance($forum->cmid),
585 'relateduserid' => $user->id,
588 $this->setExpectedException('coding_exception', 'The \'forumid\' value must be set in other.');
589 \mod_forum\event\subscription_created::create($params);
593 * Ensure subscription_created event validates that the relateduserid is set.
595 public function test_subscription_created_relateduserid_validation() {
596 $course = $this->getDataGenerator()->create_course();
597 $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
600 'context' => context_module::instance($forum->cmid),
601 'objectid' => $forum->id,
604 $this->setExpectedException('coding_exception', 'The \'relateduserid\' must be set.');
605 \mod_forum\event\subscription_created::create($params);
609 * Ensure subscription_created event validates that the contextlevel is correct.
611 public function test_subscription_created_contextlevel_validation() {
612 $user = $this->getDataGenerator()->create_user();
613 $course = $this->getDataGenerator()->create_course();
614 $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
617 'context' => context_system::instance(),
618 'other' => array('forumid' => $forum->id),
619 'relateduserid' => $user->id,
622 $this->setExpectedException('coding_exception', 'Context level must be CONTEXT_MODULE.');
623 \mod_forum\event\subscription_created::create($params);
627 * Test the subscription_created event.
629 public function test_subscription_created() {
631 $user = $this->getDataGenerator()->create_user();
632 $course = $this->getDataGenerator()->create_course();
633 $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
634 $user = $this->getDataGenerator()->create_user();
635 $context = context_module::instance($forum->cmid);
637 // Add a subscription.
639 $record['course'] = $course->id;
640 $record['forum'] = $forum->id;
641 $record['userid'] = $user->id;
642 $subscription = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_subscription($record);
645 'context' => $context,
646 'objectid' => $subscription->id,
647 'other' => array('forumid' => $forum->id),
648 'relateduserid' => $user->id,
651 $event = \mod_forum\event\subscription_created::create($params);
653 // Trigger and capturing the event.
654 $sink = $this->redirectEvents();
656 $events = $sink->get_events();
657 $this->assertCount(1, $events);
658 $event = reset($events);
660 // Checking that the event contains the expected values.
661 $this->assertInstanceOf('\mod_forum\event\subscription_created', $event);
662 $this->assertEquals($context, $event->get_context());
663 $expected = array($course->id, 'forum', 'subscribe', "view.php?f={$forum->id}", $forum->id, $forum->cmid);
664 $this->assertEventLegacyLogData($expected, $event);
665 $url = new \moodle_url('/mod/forum/subscribers.php', array('id' => $forum->id));
666 $this->assertEquals($url, $event->get_url());
667 $this->assertEventContextNotUsed($event);
669 $this->assertNotEmpty($event->get_name());
673 * Ensure subscription_deleted event validates that the forumid is set.
675 public function test_subscription_deleted_forumid_validation() {
676 $user = $this->getDataGenerator()->create_user();
677 $course = $this->getDataGenerator()->create_course();
678 $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
681 'context' => context_module::instance($forum->cmid),
682 'relateduserid' => $user->id,
685 $this->setExpectedException('coding_exception', 'The \'forumid\' value must be set in other.');
686 \mod_forum\event\subscription_deleted::create($params);
690 * Ensure subscription_deleted event validates that the relateduserid is set.
692 public function test_subscription_deleted_relateduserid_validation() {
693 $course = $this->getDataGenerator()->create_course();
694 $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
697 'context' => context_module::instance($forum->cmid),
698 'objectid' => $forum->id,
701 $this->setExpectedException('coding_exception', 'The \'relateduserid\' must be set.');
702 \mod_forum\event\subscription_deleted::create($params);
706 * Ensure subscription_deleted event validates that the contextlevel is correct.
708 public function test_subscription_deleted_contextlevel_validation() {
709 $user = $this->getDataGenerator()->create_user();
710 $course = $this->getDataGenerator()->create_course();
711 $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
714 'context' => context_system::instance(),
715 'other' => array('forumid' => $forum->id),
716 'relateduserid' => $user->id,
719 $this->setExpectedException('coding_exception', 'Context level must be CONTEXT_MODULE.');
720 \mod_forum\event\subscription_deleted::create($params);
724 * Test the subscription_deleted event.
726 public function test_subscription_deleted() {
728 $user = $this->getDataGenerator()->create_user();
729 $course = $this->getDataGenerator()->create_course();
730 $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
731 $user = $this->getDataGenerator()->create_user();
732 $context = context_module::instance($forum->cmid);
734 // Add a subscription.
736 $record['course'] = $course->id;
737 $record['forum'] = $forum->id;
738 $record['userid'] = $user->id;
739 $subscription = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_subscription($record);
742 'context' => $context,
743 'objectid' => $subscription->id,
744 'other' => array('forumid' => $forum->id),
745 'relateduserid' => $user->id,
748 $event = \mod_forum\event\subscription_deleted::create($params);
750 // Trigger and capturing the event.
751 $sink = $this->redirectEvents();
753 $events = $sink->get_events();
754 $this->assertCount(1, $events);
755 $event = reset($events);
757 // Checking that the event contains the expected values.
758 $this->assertInstanceOf('\mod_forum\event\subscription_deleted', $event);
759 $this->assertEquals($context, $event->get_context());
760 $expected = array($course->id, 'forum', 'unsubscribe', "view.php?f={$forum->id}", $forum->id, $forum->cmid);
761 $this->assertEventLegacyLogData($expected, $event);
762 $url = new \moodle_url('/mod/forum/subscribers.php', array('id' => $forum->id));
763 $this->assertEquals($url, $event->get_url());
764 $this->assertEventContextNotUsed($event);
766 $this->assertNotEmpty($event->get_name());
770 * Ensure readtracking_enabled event validates that the forumid is set.
772 public function test_readtracking_enabled_forumid_validation() {
773 $user = $this->getDataGenerator()->create_user();
774 $course = $this->getDataGenerator()->create_course();
775 $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
778 'context' => context_module::instance($forum->cmid),
779 'relateduserid' => $user->id,
782 $this->setExpectedException('coding_exception', 'The \'forumid\' value must be set in other.');
783 \mod_forum\event\readtracking_enabled::create($params);
787 * Ensure readtracking_enabled event validates that the relateduserid is set.
789 public function test_readtracking_enabled_relateduserid_validation() {
790 $course = $this->getDataGenerator()->create_course();
791 $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
794 'context' => context_module::instance($forum->cmid),
795 'objectid' => $forum->id,
798 $this->setExpectedException('coding_exception', 'The \'relateduserid\' must be set.');
799 \mod_forum\event\readtracking_enabled::create($params);
803 * Ensure readtracking_enabled event validates that the contextlevel is correct.
805 public function test_readtracking_enabled_contextlevel_validation() {
806 $user = $this->getDataGenerator()->create_user();
807 $course = $this->getDataGenerator()->create_course();
808 $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
811 'context' => context_system::instance(),
812 'other' => array('forumid' => $forum->id),
813 'relateduserid' => $user->id,
816 $this->setExpectedException('coding_exception', 'Context level must be CONTEXT_MODULE.');
817 \mod_forum\event\readtracking_enabled::create($params);
821 * Test the readtracking_enabled event.
823 public function test_readtracking_enabled() {
825 $user = $this->getDataGenerator()->create_user();
826 $course = $this->getDataGenerator()->create_course();
827 $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
828 $context = context_module::instance($forum->cmid);
831 'context' => $context,
832 'other' => array('forumid' => $forum->id),
833 'relateduserid' => $user->id,
836 $event = \mod_forum\event\readtracking_enabled::create($params);
838 // Trigger and capture the event.
839 $sink = $this->redirectEvents();
841 $events = $sink->get_events();
842 $this->assertCount(1, $events);
843 $event = reset($events);
845 // Checking that the event contains the expected values.
846 $this->assertInstanceOf('\mod_forum\event\readtracking_enabled', $event);
847 $this->assertEquals($context, $event->get_context());
848 $expected = array($course->id, 'forum', 'start tracking', "view.php?f={$forum->id}", $forum->id, $forum->cmid);
849 $this->assertEventLegacyLogData($expected, $event);
850 $url = new \moodle_url('/mod/forum/view.php', array('f' => $forum->id));
851 $this->assertEquals($url, $event->get_url());
852 $this->assertEventContextNotUsed($event);
854 $this->assertNotEmpty($event->get_name());
858 * Ensure readtracking_disabled event validates that the forumid is set.
860 public function test_readtracking_disabled_forumid_validation() {
861 $user = $this->getDataGenerator()->create_user();
862 $course = $this->getDataGenerator()->create_course();
863 $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
866 'context' => context_module::instance($forum->cmid),
867 'relateduserid' => $user->id,
870 $this->setExpectedException('coding_exception', 'The \'forumid\' value must be set in other.');
871 \mod_forum\event\readtracking_disabled::create($params);
875 * Ensure readtracking_disabled event validates that the relateduserid is set.
877 public function test_readtracking_disabled_relateduserid_validation() {
878 $course = $this->getDataGenerator()->create_course();
879 $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
882 'context' => context_module::instance($forum->cmid),
883 'objectid' => $forum->id,
886 $this->setExpectedException('coding_exception', 'The \'relateduserid\' must be set.');
887 \mod_forum\event\readtracking_disabled::create($params);
891 * Ensure readtracking_disabled event validates that the contextlevel is correct
893 public function test_readtracking_disabled_contextlevel_validation() {
894 $user = $this->getDataGenerator()->create_user();
895 $course = $this->getDataGenerator()->create_course();
896 $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
899 'context' => context_system::instance(),
900 'other' => array('forumid' => $forum->id),
901 'relateduserid' => $user->id,
904 $this->setExpectedException('coding_exception', 'Context level must be CONTEXT_MODULE.');
905 \mod_forum\event\readtracking_disabled::create($params);
909 * Test the readtracking_disabled event.
911 public function test_readtracking_disabled() {
913 $user = $this->getDataGenerator()->create_user();
914 $course = $this->getDataGenerator()->create_course();
915 $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
916 $context = context_module::instance($forum->cmid);
919 'context' => $context,
920 'other' => array('forumid' => $forum->id),
921 'relateduserid' => $user->id,
924 $event = \mod_forum\event\readtracking_disabled::create($params);
926 // Trigger and capture the event.
927 $sink = $this->redirectEvents();
929 $events = $sink->get_events();
930 $this->assertCount(1, $events);
931 $event = reset($events);
933 // Checking that the event contains the expected values.
934 $this->assertInstanceOf('\mod_forum\event\readtracking_disabled', $event);
935 $this->assertEquals($context, $event->get_context());
936 $expected = array($course->id, 'forum', 'stop tracking', "view.php?f={$forum->id}", $forum->id, $forum->cmid);
937 $this->assertEventLegacyLogData($expected, $event);
938 $url = new \moodle_url('/mod/forum/view.php', array('f' => $forum->id));
939 $this->assertEquals($url, $event->get_url());
940 $this->assertEventContextNotUsed($event);
942 $this->assertNotEmpty($event->get_name());
946 * Ensure subscribers_viewed event validates that the forumid is set.
948 public function test_subscribers_viewed_forumid_validation() {
949 $user = $this->getDataGenerator()->create_user();
950 $course = $this->getDataGenerator()->create_course();
951 $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
954 'context' => context_module::instance($forum->cmid),
955 'relateduserid' => $user->id,
958 $this->setExpectedException('coding_exception', 'The \'forumid\' value must be set in other.');
959 \mod_forum\event\subscribers_viewed::create($params);
963 * Ensure subscribers_viewed event validates that the contextlevel is correct.
965 public function test_subscribers_viewed_contextlevel_validation() {
966 $user = $this->getDataGenerator()->create_user();
967 $course = $this->getDataGenerator()->create_course();
968 $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
971 'context' => context_system::instance(),
972 'other' => array('forumid' => $forum->id),
973 'relateduserid' => $user->id,
976 $this->setExpectedException('coding_exception', 'Context level must be CONTEXT_MODULE.');
977 \mod_forum\event\subscribers_viewed::create($params);
981 * Test the subscribers_viewed event.
983 public function test_subscribers_viewed() {
985 $course = $this->getDataGenerator()->create_course();
986 $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
987 $context = context_module::instance($forum->cmid);
990 'context' => $context,
991 'other' => array('forumid' => $forum->id),
994 $event = \mod_forum\event\subscribers_viewed::create($params);
996 // Trigger and capture the event.
997 $sink = $this->redirectEvents();
999 $events = $sink->get_events();
1000 $this->assertCount(1, $events);
1001 $event = reset($events);
1003 // Checking that the event contains the expected values.
1004 $this->assertInstanceOf('\mod_forum\event\subscribers_viewed', $event);
1005 $this->assertEquals($context, $event->get_context());
1006 $expected = array($course->id, 'forum', 'view subscribers', "subscribers.php?id={$forum->id}", $forum->id, $forum->cmid);
1007 $this->assertEventLegacyLogData($expected, $event);
1008 $this->assertEventContextNotUsed($event);
1010 $this->assertNotEmpty($event->get_name());
1014 * Ensure user_report_viewed event validates that the reportmode is set.
1016 public function test_user_report_viewed_reportmode_validation() {
1017 $user = $this->getDataGenerator()->create_user();
1018 $course = $this->getDataGenerator()->create_course();
1021 'context' => context_course::instance($course->id),
1022 'relateduserid' => $user->id,
1025 $this->setExpectedException('coding_exception', 'The \'reportmode\' value must be set in other.');
1026 \mod_forum\event\user_report_viewed::create($params);
1030 * Ensure user_report_viewed event validates that the contextlevel is correct.
1032 public function test_user_report_viewed_contextlevel_validation() {
1033 $user = $this->getDataGenerator()->create_user();
1034 $course = $this->getDataGenerator()->create_course();
1035 $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
1038 'context' => context_module::instance($forum->cmid),
1039 'other' => array('reportmode' => 'posts'),
1040 'relateduserid' => $user->id,
1043 $this->setExpectedException('coding_exception', 'Context level must be either CONTEXT_SYSTEM or CONTEXT_COURSE.');
1044 \mod_forum\event\user_report_viewed::create($params);
1048 * Ensure user_report_viewed event validates that the relateduserid is set.
1050 public function test_user_report_viewed_relateduserid_validation() {
1053 'context' => context_system::instance(),
1054 'other' => array('reportmode' => 'posts'),
1057 $this->setExpectedException('coding_exception', 'The \'relateduserid\' must be set.');
1058 \mod_forum\event\user_report_viewed::create($params);
1062 * Test the user_report_viewed event.
1064 public function test_user_report_viewed() {
1066 $user = $this->getDataGenerator()->create_user();
1067 $course = $this->getDataGenerator()->create_course();
1068 $context = context_course::instance($course->id);
1071 'context' => $context,
1072 'relateduserid' => $user->id,
1073 'other' => array('reportmode' => 'discussions'),
1076 $event = \mod_forum\event\user_report_viewed::create($params);
1078 // Trigger and capture the event.
1079 $sink = $this->redirectEvents();
1081 $events = $sink->get_events();
1082 $this->assertCount(1, $events);
1083 $event = reset($events);
1085 // Checking that the event contains the expected values.
1086 $this->assertInstanceOf('\mod_forum\event\user_report_viewed', $event);
1087 $this->assertEquals($context, $event->get_context());
1088 $expected = array($course->id, 'forum', 'user report',
1089 "user.php?id={$user->id}&mode=discussions&course={$course->id}", $user->id);
1090 $this->assertEventLegacyLogData($expected, $event);
1091 $this->assertEventContextNotUsed($event);
1093 $this->assertNotEmpty($event->get_name());
1097 * Ensure post_created event validates that the postid is set.
1099 public function test_post_created_postid_validation() {
1100 $course = $this->getDataGenerator()->create_course();
1101 $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
1102 $user = $this->getDataGenerator()->create_user();
1104 // Add a discussion.
1106 $record['course'] = $course->id;
1107 $record['forum'] = $forum->id;
1108 $record['userid'] = $user->id;
1109 $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1112 'context' => context_module::instance($forum->cmid),
1113 'other' => array('forumid' => $forum->id, 'forumtype' => $forum->type, 'discussionid' => $discussion->id)
1116 \mod_forum\event\post_created::create($params);
1120 * Ensure post_created event validates that the discussionid is set.
1122 public function test_post_created_discussionid_validation() {
1123 $course = $this->getDataGenerator()->create_course();
1124 $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
1125 $user = $this->getDataGenerator()->create_user();
1127 // Add a discussion.
1129 $record['course'] = $course->id;
1130 $record['forum'] = $forum->id;
1131 $record['userid'] = $user->id;
1132 $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1136 $record['discussion'] = $discussion->id;
1137 $record['userid'] = $user->id;
1138 $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
1141 'context' => context_module::instance($forum->cmid),
1142 'objectid' => $post->id,
1143 'other' => array('forumid' => $forum->id, 'forumtype' => $forum->type)
1146 $this->setExpectedException('coding_exception', 'The \'discussionid\' value must be set in other.');
1147 \mod_forum\event\post_created::create($params);
1151 * Ensure post_created event validates that the forumid is set.
1153 public function test_post_created_forumid_validation() {
1154 $course = $this->getDataGenerator()->create_course();
1155 $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
1156 $user = $this->getDataGenerator()->create_user();
1158 // Add a discussion.
1160 $record['course'] = $course->id;
1161 $record['forum'] = $forum->id;
1162 $record['userid'] = $user->id;
1163 $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1167 $record['discussion'] = $discussion->id;
1168 $record['userid'] = $user->id;
1169 $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
1172 'context' => context_module::instance($forum->cmid),
1173 'objectid' => $post->id,
1174 'other' => array('discussionid' => $discussion->id, 'forumtype' => $forum->type)
1177 $this->setExpectedException('coding_exception', 'The \'forumid\' value must be set in other.');
1178 \mod_forum\event\post_created::create($params);
1182 * Ensure post_created event validates that the forumtype is set.
1184 public function test_post_created_forumtype_validation() {
1185 $course = $this->getDataGenerator()->create_course();
1186 $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
1187 $user = $this->getDataGenerator()->create_user();
1189 // Add a discussion.
1191 $record['course'] = $course->id;
1192 $record['forum'] = $forum->id;
1193 $record['userid'] = $user->id;
1194 $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1198 $record['discussion'] = $discussion->id;
1199 $record['userid'] = $user->id;
1200 $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
1203 'context' => context_module::instance($forum->cmid),
1204 'objectid' => $post->id,
1205 'other' => array('discussionid' => $discussion->id, 'forumid' => $forum->id)
1208 $this->setExpectedException('coding_exception', 'The \'forumtype\' value must be set in other.');
1209 \mod_forum\event\post_created::create($params);
1213 * Ensure post_created event validates that the contextlevel is correct.
1215 public function test_post_created_context_validation() {
1216 $course = $this->getDataGenerator()->create_course();
1217 $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
1218 $user = $this->getDataGenerator()->create_user();
1220 // Add a discussion.
1222 $record['course'] = $course->id;
1223 $record['forum'] = $forum->id;
1224 $record['userid'] = $user->id;
1225 $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1229 $record['discussion'] = $discussion->id;
1230 $record['userid'] = $user->id;
1231 $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
1234 'context' => context_system::instance(),
1235 'objectid' => $post->id,
1236 'other' => array('discussionid' => $discussion->id, 'forumid' => $forum->id, 'forumtype' => $forum->type)
1239 $this->setExpectedException('coding_exception', 'Context level must be CONTEXT_MODULE');
1240 \mod_forum\event\post_created::create($params);
1244 * Test the post_created event.
1246 public function test_post_created() {
1248 $course = $this->getDataGenerator()->create_course();
1249 $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
1250 $user = $this->getDataGenerator()->create_user();
1252 // Add a discussion.
1254 $record['course'] = $course->id;
1255 $record['forum'] = $forum->id;
1256 $record['userid'] = $user->id;
1257 $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1261 $record['discussion'] = $discussion->id;
1262 $record['userid'] = $user->id;
1263 $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
1265 $context = context_module::instance($forum->cmid);
1268 'context' => $context,
1269 'objectid' => $post->id,
1270 'other' => array('discussionid' => $discussion->id, 'forumid' => $forum->id, 'forumtype' => $forum->type)
1273 $event = \mod_forum\event\post_created::create($params);
1275 // Trigger and capturing the event.
1276 $sink = $this->redirectEvents();
1278 $events = $sink->get_events();
1279 $this->assertCount(1, $events);
1280 $event = reset($events);
1282 // Checking that the event contains the expected values.
1283 $this->assertInstanceOf('\mod_forum\event\post_created', $event);
1284 $this->assertEquals($context, $event->get_context());
1285 $expected = array($course->id, 'forum', 'add post', "discuss.php?d={$discussion->id}#p{$post->id}",
1286 $forum->id, $forum->cmid);
1287 $this->assertEventLegacyLogData($expected, $event);
1288 $url = new \moodle_url('/mod/forum/discuss.php', array('d' => $discussion->id));
1289 $url->set_anchor('p'.$event->objectid);
1290 $this->assertEquals($url, $event->get_url());
1291 $this->assertEventContextNotUsed($event);
1293 $this->assertNotEmpty($event->get_name());
1297 * Test the post_created event for a single discussion forum.
1299 public function test_post_created_single() {
1301 $course = $this->getDataGenerator()->create_course();
1302 $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id, 'type' => 'single'));
1303 $user = $this->getDataGenerator()->create_user();
1305 // Add a discussion.
1307 $record['course'] = $course->id;
1308 $record['forum'] = $forum->id;
1309 $record['userid'] = $user->id;
1310 $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1314 $record['discussion'] = $discussion->id;
1315 $record['userid'] = $user->id;
1316 $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
1318 $context = context_module::instance($forum->cmid);
1321 'context' => $context,
1322 'objectid' => $post->id,
1323 'other' => array('discussionid' => $discussion->id, 'forumid' => $forum->id, 'forumtype' => $forum->type)
1326 $event = \mod_forum\event\post_created::create($params);
1328 // Trigger and capturing the event.
1329 $sink = $this->redirectEvents();
1331 $events = $sink->get_events();
1332 $this->assertCount(1, $events);
1333 $event = reset($events);
1335 // Checking that the event contains the expected values.
1336 $this->assertInstanceOf('\mod_forum\event\post_created', $event);
1337 $this->assertEquals($context, $event->get_context());
1338 $expected = array($course->id, 'forum', 'add post', "view.php?f={$forum->id}#p{$post->id}",
1339 $forum->id, $forum->cmid);
1340 $this->assertEventLegacyLogData($expected, $event);
1341 $url = new \moodle_url('/mod/forum/view.php', array('f' => $forum->id));
1342 $url->set_anchor('p'.$event->objectid);
1343 $this->assertEquals($url, $event->get_url());
1344 $this->assertEventContextNotUsed($event);
1346 $this->assertNotEmpty($event->get_name());
1350 * Ensure post_deleted event validates that the postid is set.
1352 public function test_post_deleted_postid_validation() {
1353 $course = $this->getDataGenerator()->create_course();
1354 $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
1355 $user = $this->getDataGenerator()->create_user();
1357 // Add a discussion.
1359 $record['course'] = $course->id;
1360 $record['forum'] = $forum->id;
1361 $record['userid'] = $user->id;
1362 $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1365 'context' => context_module::instance($forum->cmid),
1366 'other' => array('forumid' => $forum->id, 'forumtype' => $forum->type, 'discussionid' => $discussion->id)
1369 \mod_forum\event\post_deleted::create($params);
1373 * Ensure post_deleted event validates that the discussionid is set.
1375 public function test_post_deleted_discussionid_validation() {
1376 $course = $this->getDataGenerator()->create_course();
1377 $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
1378 $user = $this->getDataGenerator()->create_user();
1380 // Add a discussion.
1382 $record['course'] = $course->id;
1383 $record['forum'] = $forum->id;
1384 $record['userid'] = $user->id;
1385 $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1389 $record['discussion'] = $discussion->id;
1390 $record['userid'] = $user->id;
1391 $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
1394 'context' => context_module::instance($forum->cmid),
1395 'objectid' => $post->id,
1396 'other' => array('forumid' => $forum->id, 'forumtype' => $forum->type)
1399 $this->setExpectedException('coding_exception', 'The \'discussionid\' value must be set in other.');
1400 \mod_forum\event\post_deleted::create($params);
1404 * Ensure post_deleted event validates that the forumid is set.
1406 public function test_post_deleted_forumid_validation() {
1407 $course = $this->getDataGenerator()->create_course();
1408 $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
1409 $user = $this->getDataGenerator()->create_user();
1411 // Add a discussion.
1413 $record['course'] = $course->id;
1414 $record['forum'] = $forum->id;
1415 $record['userid'] = $user->id;
1416 $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1420 $record['discussion'] = $discussion->id;
1421 $record['userid'] = $user->id;
1422 $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
1425 'context' => context_module::instance($forum->cmid),
1426 'objectid' => $post->id,
1427 'other' => array('discussionid' => $discussion->id, 'forumtype' => $forum->type)
1430 $this->setExpectedException('coding_exception', 'The \'forumid\' value must be set in other.');
1431 \mod_forum\event\post_deleted::create($params);
1435 * Ensure post_deleted event validates that the forumtype is set.
1437 public function test_post_deleted_forumtype_validation() {
1438 $course = $this->getDataGenerator()->create_course();
1439 $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
1440 $user = $this->getDataGenerator()->create_user();
1442 // Add a discussion.
1444 $record['course'] = $course->id;
1445 $record['forum'] = $forum->id;
1446 $record['userid'] = $user->id;
1447 $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1451 $record['discussion'] = $discussion->id;
1452 $record['userid'] = $user->id;
1453 $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
1456 'context' => context_module::instance($forum->cmid),
1457 'objectid' => $post->id,
1458 'other' => array('discussionid' => $discussion->id, 'forumid' => $forum->id)
1461 $this->setExpectedException('coding_exception', 'The \'forumtype\' value must be set in other.');
1462 \mod_forum\event\post_deleted::create($params);
1466 * Ensure post_deleted event validates that the contextlevel is correct.
1468 public function test_post_deleted_context_validation() {
1469 $course = $this->getDataGenerator()->create_course();
1470 $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
1471 $user = $this->getDataGenerator()->create_user();
1473 // Add a discussion.
1475 $record['course'] = $course->id;
1476 $record['forum'] = $forum->id;
1477 $record['userid'] = $user->id;
1478 $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1482 $record['discussion'] = $discussion->id;
1483 $record['userid'] = $user->id;
1484 $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
1487 'context' => context_system::instance(),
1488 'objectid' => $post->id,
1489 'other' => array('discussionid' => $discussion->id, 'forumid' => $forum->id, 'forumtype' => $forum->type)
1492 $this->setExpectedException('coding_exception', 'Context level must be CONTEXT_MODULE');
1493 \mod_forum\event\post_deleted::create($params);
1497 * Test post_deleted event.
1499 public function test_post_deleted() {
1501 $course = $this->getDataGenerator()->create_course();
1502 $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
1503 $user = $this->getDataGenerator()->create_user();
1505 // Add a discussion.
1507 $record['course'] = $course->id;
1508 $record['forum'] = $forum->id;
1509 $record['userid'] = $user->id;
1510 $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1514 $record['discussion'] = $discussion->id;
1515 $record['userid'] = $user->id;
1516 $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
1518 $context = context_module::instance($forum->cmid);
1521 'context' => $context,
1522 'objectid' => $post->id,
1523 'other' => array('discussionid' => $discussion->id, 'forumid' => $forum->id, 'forumtype' => $forum->type)
1526 $event = \mod_forum\event\post_deleted::create($params);
1528 // Trigger and capture the event.
1529 $sink = $this->redirectEvents();
1531 $events = $sink->get_events();
1532 $this->assertCount(1, $events);
1533 $event = reset($events);
1535 // Checking that the event contains the expected values.
1536 $this->assertInstanceOf('\mod_forum\event\post_deleted', $event);
1537 $this->assertEquals($context, $event->get_context());
1538 $expected = array($course->id, 'forum', 'delete post', "discuss.php?d={$discussion->id}", $post->id, $forum->cmid);
1539 $this->assertEventLegacyLogData($expected, $event);
1540 $url = new \moodle_url('/mod/forum/discuss.php', array('d' => $discussion->id));
1541 $this->assertEquals($url, $event->get_url());
1542 $this->assertEventContextNotUsed($event);
1544 $this->assertNotEmpty($event->get_name());
1548 * Test post_deleted event for a single discussion forum.
1550 public function test_post_deleted_single() {
1552 $course = $this->getDataGenerator()->create_course();
1553 $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id, 'type' => 'single'));
1554 $user = $this->getDataGenerator()->create_user();
1556 // Add a discussion.
1558 $record['course'] = $course->id;
1559 $record['forum'] = $forum->id;
1560 $record['userid'] = $user->id;
1561 $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1565 $record['discussion'] = $discussion->id;
1566 $record['userid'] = $user->id;
1567 $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
1569 $context = context_module::instance($forum->cmid);
1572 'context' => $context,
1573 'objectid' => $post->id,
1574 'other' => array('discussionid' => $discussion->id, 'forumid' => $forum->id, 'forumtype' => $forum->type)
1577 $event = \mod_forum\event\post_deleted::create($params);
1579 // Trigger and capture the event.
1580 $sink = $this->redirectEvents();
1582 $events = $sink->get_events();
1583 $this->assertCount(1, $events);
1584 $event = reset($events);
1586 // Checking that the event contains the expected values.
1587 $this->assertInstanceOf('\mod_forum\event\post_deleted', $event);
1588 $this->assertEquals($context, $event->get_context());
1589 $expected = array($course->id, 'forum', 'delete post', "view.php?f={$forum->id}", $post->id, $forum->cmid);
1590 $this->assertEventLegacyLogData($expected, $event);
1591 $url = new \moodle_url('/mod/forum/view.php', array('f' => $forum->id));
1592 $this->assertEquals($url, $event->get_url());
1593 $this->assertEventContextNotUsed($event);
1595 $this->assertNotEmpty($event->get_name());
1599 * Ensure post_updated event validates that the discussionid is set.
1601 public function test_post_updated_discussionid_validation() {
1602 $course = $this->getDataGenerator()->create_course();
1603 $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
1604 $user = $this->getDataGenerator()->create_user();
1606 // Add a discussion.
1608 $record['course'] = $course->id;
1609 $record['forum'] = $forum->id;
1610 $record['userid'] = $user->id;
1611 $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1615 $record['discussion'] = $discussion->id;
1616 $record['userid'] = $user->id;
1617 $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
1620 'context' => context_module::instance($forum->cmid),
1621 'objectid' => $post->id,
1622 'other' => array('forumid' => $forum->id, 'forumtype' => $forum->type)
1625 $this->setExpectedException('coding_exception', 'The \'discussionid\' value must be set in other.');
1626 \mod_forum\event\post_updated::create($params);
1630 * Ensure post_updated event validates that the forumid is set.
1632 public function test_post_updated_forumid_validation() {
1633 $course = $this->getDataGenerator()->create_course();
1634 $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
1635 $user = $this->getDataGenerator()->create_user();
1637 // Add a discussion.
1639 $record['course'] = $course->id;
1640 $record['forum'] = $forum->id;
1641 $record['userid'] = $user->id;
1642 $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1646 $record['discussion'] = $discussion->id;
1647 $record['userid'] = $user->id;
1648 $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
1651 'context' => context_module::instance($forum->cmid),
1652 'objectid' => $post->id,
1653 'other' => array('discussionid' => $discussion->id, 'forumtype' => $forum->type)
1656 $this->setExpectedException('coding_exception', 'The \'forumid\' value must be set in other.');
1657 \mod_forum\event\post_updated::create($params);
1661 * Ensure post_updated event validates that the forumtype is set.
1663 public function test_post_updated_forumtype_validation() {
1664 $course = $this->getDataGenerator()->create_course();
1665 $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
1666 $user = $this->getDataGenerator()->create_user();
1668 // Add a discussion.
1670 $record['course'] = $course->id;
1671 $record['forum'] = $forum->id;
1672 $record['userid'] = $user->id;
1673 $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1677 $record['discussion'] = $discussion->id;
1678 $record['userid'] = $user->id;
1679 $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
1682 'context' => context_module::instance($forum->cmid),
1683 'objectid' => $post->id,
1684 'other' => array('discussionid' => $discussion->id, 'forumid' => $forum->id)
1687 $this->setExpectedException('coding_exception', 'The \'forumtype\' value must be set in other.');
1688 \mod_forum\event\post_updated::create($params);
1692 * Ensure post_updated event validates that the contextlevel is correct.
1694 public function test_post_updated_context_validation() {
1695 $course = $this->getDataGenerator()->create_course();
1696 $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
1697 $user = $this->getDataGenerator()->create_user();
1699 // Add a discussion.
1701 $record['course'] = $course->id;
1702 $record['forum'] = $forum->id;
1703 $record['userid'] = $user->id;
1704 $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1708 $record['discussion'] = $discussion->id;
1709 $record['userid'] = $user->id;
1710 $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
1713 'context' => context_system::instance(),
1714 'objectid' => $post->id,
1715 'other' => array('discussionid' => $discussion->id, 'forumid' => $forum->id, 'forumtype' => $forum->type)
1718 $this->setExpectedException('coding_exception', 'Context level must be CONTEXT_MODULE');
1719 \mod_forum\event\post_updated::create($params);
1723 * Test post_updated event.
1725 public function test_post_updated() {
1727 $course = $this->getDataGenerator()->create_course();
1728 $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
1729 $user = $this->getDataGenerator()->create_user();
1731 // Add a discussion.
1733 $record['course'] = $course->id;
1734 $record['forum'] = $forum->id;
1735 $record['userid'] = $user->id;
1736 $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1740 $record['discussion'] = $discussion->id;
1741 $record['userid'] = $user->id;
1742 $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
1744 $context = context_module::instance($forum->cmid);
1747 'context' => $context,
1748 'objectid' => $post->id,
1749 'other' => array('discussionid' => $discussion->id, 'forumid' => $forum->id, 'forumtype' => $forum->type)
1752 $event = \mod_forum\event\post_updated::create($params);
1754 // Trigger and capturing the event.
1755 $sink = $this->redirectEvents();
1757 $events = $sink->get_events();
1758 $this->assertCount(1, $events);
1759 $event = reset($events);
1761 // Checking that the event contains the expected values.
1762 $this->assertInstanceOf('\mod_forum\event\post_updated', $event);
1763 $this->assertEquals($context, $event->get_context());
1764 $expected = array($course->id, 'forum', 'update post', "discuss.php?d={$discussion->id}#p{$post->id}",
1765 $post->id, $forum->cmid);
1766 $this->assertEventLegacyLogData($expected, $event);
1767 $url = new \moodle_url('/mod/forum/discuss.php', array('d' => $discussion->id));
1768 $url->set_anchor('p'.$event->objectid);
1769 $this->assertEquals($url, $event->get_url());
1770 $this->assertEventContextNotUsed($event);
1772 $this->assertNotEmpty($event->get_name());
1776 * Test post_updated event.
1778 public function test_post_updated_single() {
1780 $course = $this->getDataGenerator()->create_course();
1781 $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id, 'type' => 'single'));
1782 $user = $this->getDataGenerator()->create_user();
1784 // Add a discussion.
1786 $record['course'] = $course->id;
1787 $record['forum'] = $forum->id;
1788 $record['userid'] = $user->id;
1789 $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1793 $record['discussion'] = $discussion->id;
1794 $record['userid'] = $user->id;
1795 $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
1797 $context = context_module::instance($forum->cmid);
1800 'context' => $context,
1801 'objectid' => $post->id,
1802 'other' => array('discussionid' => $discussion->id, 'forumid' => $forum->id, 'forumtype' => $forum->type)
1805 $event = \mod_forum\event\post_updated::create($params);
1807 // Trigger and capturing the event.
1808 $sink = $this->redirectEvents();
1810 $events = $sink->get_events();
1811 $this->assertCount(1, $events);
1812 $event = reset($events);
1814 // Checking that the event contains the expected values.
1815 $this->assertInstanceOf('\mod_forum\event\post_updated', $event);
1816 $this->assertEquals($context, $event->get_context());
1817 $expected = array($course->id, 'forum', 'update post', "view.php?f={$forum->id}#p{$post->id}",
1818 $post->id, $forum->cmid);
1819 $this->assertEventLegacyLogData($expected, $event);
1820 $url = new \moodle_url('/mod/forum/view.php', array('f' => $forum->id));
1821 $url->set_anchor('p'.$post->id);
1822 $this->assertEquals($url, $event->get_url());
1823 $this->assertEventContextNotUsed($event);
1825 $this->assertNotEmpty($event->get_name());
1829 * Test discussion_subscription_created event.
1831 public function test_discussion_subscription_created() {
1833 require_once($CFG->dirroot . '/mod/forum/lib.php');
1836 $course = $this->getDataGenerator()->create_course();
1837 $user = $this->getDataGenerator()->create_user();
1838 $this->getDataGenerator()->enrol_user($user->id, $course->id);
1840 $options = array('course' => $course->id, 'forcesubscribe' => FORUM_CHOOSESUBSCRIBE);
1841 $forum = $this->getDataGenerator()->create_module('forum', $options);
1843 // Add a discussion.
1845 $record['course'] = $course->id;
1846 $record['forum'] = $forum->id;
1847 $record['userid'] = $user->id;
1848 $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1852 $record['discussion'] = $discussion->id;
1853 $record['userid'] = $user->id;
1854 $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
1856 // Trigger and capturing the event.
1857 $sink = $this->redirectEvents();
1859 // Trigger the event by subscribing the user to the forum discussion.
1860 \mod_forum\subscriptions::subscribe_user_to_discussion($user->id, $discussion);
1862 $events = $sink->get_events();
1863 $this->assertCount(1, $events);
1864 $event = reset($events);
1866 // Checking that the event contains the expected values.
1867 $this->assertInstanceOf('\mod_forum\event\discussion_subscription_created', $event);
1870 $cm = get_coursemodule_from_instance('forum', $discussion->forum);
1871 $context = \context_module::instance($cm->id);
1872 $this->assertEquals($context, $event->get_context());
1874 $url = new \moodle_url('/mod/forum/subscribe.php', array(
1876 'd' => $discussion->id
1879 $this->assertEquals($url, $event->get_url());
1880 $this->assertEventContextNotUsed($event);
1881 $this->assertNotEmpty($event->get_name());
1885 * Test validation of discussion_subscription_created event.
1887 public function test_discussion_subscription_created_validation() {
1889 require_once($CFG->dirroot . '/mod/forum/lib.php');
1892 $course = $this->getDataGenerator()->create_course();
1893 $user = $this->getDataGenerator()->create_user();
1894 $this->getDataGenerator()->enrol_user($user->id, $course->id);
1896 $options = array('course' => $course->id, 'forcesubscribe' => FORUM_CHOOSESUBSCRIBE);
1897 $forum = $this->getDataGenerator()->create_module('forum', $options);
1899 // Add a discussion.
1901 $record['course'] = $course->id;
1902 $record['forum'] = $forum->id;
1903 $record['userid'] = $user->id;
1904 $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1908 $record['discussion'] = $discussion->id;
1909 $record['userid'] = $user->id;
1910 $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
1912 // The user is not subscribed to the forum. Insert a new discussion subscription.
1913 $subscription = new \stdClass();
1914 $subscription->userid = $user->id;
1915 $subscription->forum = $forum->id;
1916 $subscription->discussion = $discussion->id;
1917 $subscription->preference = \mod_forum\subscriptions::FORUM_DISCUSSION_SUBSCRIBED;
1919 $subscription->id = $DB->insert_record('forum_discussion_subs', $subscription);
1921 $context = context_module::instance($forum->cmid);
1924 'context' => $context,
1925 'objectid' => $subscription->id,
1926 'relateduserid' => $user->id,
1928 'forumid' => $forum->id,
1929 'discussion' => $discussion->id,
1933 $event = \mod_forum\event\discussion_subscription_created::create($params);
1935 // Trigger and capturing the event.
1936 $sink = $this->redirectEvents();
1938 $events = $sink->get_events();
1939 $this->assertCount(1, $events);
1940 $event = reset($events);
1944 * Test contextlevel validation of discussion_subscription_created event.
1946 public function test_discussion_subscription_created_validation_contextlevel() {
1948 require_once($CFG->dirroot . '/mod/forum/lib.php');
1951 $course = $this->getDataGenerator()->create_course();
1952 $user = $this->getDataGenerator()->create_user();
1953 $this->getDataGenerator()->enrol_user($user->id, $course->id);
1955 $options = array('course' => $course->id, 'forcesubscribe' => FORUM_CHOOSESUBSCRIBE);
1956 $forum = $this->getDataGenerator()->create_module('forum', $options);
1958 // Add a discussion.
1960 $record['course'] = $course->id;
1961 $record['forum'] = $forum->id;
1962 $record['userid'] = $user->id;
1963 $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1967 $record['discussion'] = $discussion->id;
1968 $record['userid'] = $user->id;
1969 $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
1971 // The user is not subscribed to the forum. Insert a new discussion subscription.
1972 $subscription = new \stdClass();
1973 $subscription->userid = $user->id;
1974 $subscription->forum = $forum->id;
1975 $subscription->discussion = $discussion->id;
1976 $subscription->preference = \mod_forum\subscriptions::FORUM_DISCUSSION_SUBSCRIBED;
1978 $subscription->id = $DB->insert_record('forum_discussion_subs', $subscription);
1980 $context = context_module::instance($forum->cmid);
1983 'context' => \context_course::instance($course->id),
1984 'objectid' => $subscription->id,
1985 'relateduserid' => $user->id,
1987 'forumid' => $forum->id,
1988 'discussion' => $discussion->id,
1992 // Without an invalid context.
1993 $this->setExpectedException('coding_exception', 'Context level must be CONTEXT_MODULE.');
1994 \mod_forum\event\discussion_subscription_created::create($params);
1998 * Test discussion validation of discussion_subscription_created event.
2000 public function test_discussion_subscription_created_validation_discussion() {
2002 require_once($CFG->dirroot . '/mod/forum/lib.php');
2005 $course = $this->getDataGenerator()->create_course();
2006 $user = $this->getDataGenerator()->create_user();
2007 $this->getDataGenerator()->enrol_user($user->id, $course->id);
2009 $options = array('course' => $course->id, 'forcesubscribe' => FORUM_CHOOSESUBSCRIBE);
2010 $forum = $this->getDataGenerator()->create_module('forum', $options);
2012 // Add a discussion.
2014 $record['course'] = $course->id;
2015 $record['forum'] = $forum->id;
2016 $record['userid'] = $user->id;
2017 $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
2021 $record['discussion'] = $discussion->id;
2022 $record['userid'] = $user->id;
2023 $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
2025 // The user is not subscribed to the forum. Insert a new discussion subscription.
2026 $subscription = new \stdClass();
2027 $subscription->userid = $user->id;
2028 $subscription->forum = $forum->id;
2029 $subscription->discussion = $discussion->id;
2030 $subscription->preference = \mod_forum\subscriptions::FORUM_DISCUSSION_SUBSCRIBED;
2032 $subscription->id = $DB->insert_record('forum_discussion_subs', $subscription);
2034 // Without the discussion.
2036 'context' => context_module::instance($forum->cmid),
2037 'objectid' => $subscription->id,
2038 'relateduserid' => $user->id,
2040 'forumid' => $forum->id,
2044 $this->setExpectedException('coding_exception', "The 'discussion' value must be set in other.");
2045 \mod_forum\event\discussion_subscription_created::create($params);
2049 * Test forumid validation of discussion_subscription_created event.
2051 public function test_discussion_subscription_created_validation_forumid() {
2053 require_once($CFG->dirroot . '/mod/forum/lib.php');
2056 $course = $this->getDataGenerator()->create_course();
2057 $user = $this->getDataGenerator()->create_user();
2058 $this->getDataGenerator()->enrol_user($user->id, $course->id);
2060 $options = array('course' => $course->id, 'forcesubscribe' => FORUM_CHOOSESUBSCRIBE);
2061 $forum = $this->getDataGenerator()->create_module('forum', $options);
2063 // Add a discussion.
2065 $record['course'] = $course->id;
2066 $record['forum'] = $forum->id;
2067 $record['userid'] = $user->id;
2068 $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
2072 $record['discussion'] = $discussion->id;
2073 $record['userid'] = $user->id;
2074 $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
2076 // The user is not subscribed to the forum. Insert a new discussion subscription.
2077 $subscription = new \stdClass();
2078 $subscription->userid = $user->id;
2079 $subscription->forum = $forum->id;
2080 $subscription->discussion = $discussion->id;
2081 $subscription->preference = \mod_forum\subscriptions::FORUM_DISCUSSION_SUBSCRIBED;
2083 $subscription->id = $DB->insert_record('forum_discussion_subs', $subscription);
2085 // Without the forumid.
2087 'context' => context_module::instance($forum->cmid),
2088 'objectid' => $subscription->id,
2089 'relateduserid' => $user->id,
2091 'discussion' => $discussion->id,
2095 $this->setExpectedException('coding_exception', "The 'forumid' value must be set in other.");
2096 \mod_forum\event\discussion_subscription_created::create($params);
2100 * Test relateduserid validation of discussion_subscription_created event.
2102 public function test_discussion_subscription_created_validation_relateduserid() {
2104 require_once($CFG->dirroot . '/mod/forum/lib.php');
2107 $course = $this->getDataGenerator()->create_course();
2108 $user = $this->getDataGenerator()->create_user();
2109 $this->getDataGenerator()->enrol_user($user->id, $course->id);
2111 $options = array('course' => $course->id, 'forcesubscribe' => FORUM_CHOOSESUBSCRIBE);
2112 $forum = $this->getDataGenerator()->create_module('forum', $options);
2114 // Add a discussion.
2116 $record['course'] = $course->id;
2117 $record['forum'] = $forum->id;
2118 $record['userid'] = $user->id;
2119 $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
2123 $record['discussion'] = $discussion->id;
2124 $record['userid'] = $user->id;
2125 $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
2127 // The user is not subscribed to the forum. Insert a new discussion subscription.
2128 $subscription = new \stdClass();
2129 $subscription->userid = $user->id;
2130 $subscription->forum = $forum->id;
2131 $subscription->discussion = $discussion->id;
2132 $subscription->preference = \mod_forum\subscriptions::FORUM_DISCUSSION_SUBSCRIBED;
2134 $subscription->id = $DB->insert_record('forum_discussion_subs', $subscription);
2136 $context = context_module::instance($forum->cmid);
2138 // Without the relateduserid.
2140 'context' => context_module::instance($forum->cmid),
2141 'objectid' => $subscription->id,
2143 'forumid' => $forum->id,
2144 'discussion' => $discussion->id,
2148 $this->setExpectedException('coding_exception', "The 'relateduserid' must be set.");
2149 \mod_forum\event\discussion_subscription_created::create($params);
2153 * Test discussion_subscription_deleted event.
2155 public function test_discussion_subscription_deleted() {
2157 require_once($CFG->dirroot . '/mod/forum/lib.php');
2160 $course = $this->getDataGenerator()->create_course();
2161 $user = $this->getDataGenerator()->create_user();
2162 $this->getDataGenerator()->enrol_user($user->id, $course->id);
2164 $options = array('course' => $course->id, 'forcesubscribe' => FORUM_INITIALSUBSCRIBE);
2165 $forum = $this->getDataGenerator()->create_module('forum', $options);
2167 // Add a discussion.
2169 $record['course'] = $course->id;
2170 $record['forum'] = $forum->id;
2171 $record['userid'] = $user->id;
2172 $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
2176 $record['discussion'] = $discussion->id;
2177 $record['userid'] = $user->id;
2178 $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
2180 // Trigger and capturing the event.
2181 $sink = $this->redirectEvents();
2183 // Trigger the event by unsubscribing the user to the forum discussion.
2184 \mod_forum\subscriptions::unsubscribe_user_from_discussion($user->id, $discussion);
2186 $events = $sink->get_events();
2187 $this->assertCount(1, $events);
2188 $event = reset($events);
2190 // Checking that the event contains the expected values.
2191 $this->assertInstanceOf('\mod_forum\event\discussion_subscription_deleted', $event);
2194 $cm = get_coursemodule_from_instance('forum', $discussion->forum);
2195 $context = \context_module::instance($cm->id);
2196 $this->assertEquals($context, $event->get_context());
2198 $url = new \moodle_url('/mod/forum/subscribe.php', array(
2200 'd' => $discussion->id
2203 $this->assertEquals($url, $event->get_url());
2204 $this->assertEventContextNotUsed($event);
2205 $this->assertNotEmpty($event->get_name());
2209 * Test validation of discussion_subscription_deleted event.
2211 public function test_discussion_subscription_deleted_validation() {
2213 require_once($CFG->dirroot . '/mod/forum/lib.php');
2216 $course = $this->getDataGenerator()->create_course();
2217 $user = $this->getDataGenerator()->create_user();
2218 $this->getDataGenerator()->enrol_user($user->id, $course->id);
2220 $options = array('course' => $course->id, 'forcesubscribe' => FORUM_INITIALSUBSCRIBE);
2221 $forum = $this->getDataGenerator()->create_module('forum', $options);
2223 // Add a discussion.
2225 $record['course'] = $course->id;
2226 $record['forum'] = $forum->id;
2227 $record['userid'] = $user->id;
2228 $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
2232 $record['discussion'] = $discussion->id;
2233 $record['userid'] = $user->id;
2234 $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
2236 // The user is not subscribed to the forum. Insert a new discussion subscription.
2237 $subscription = new \stdClass();
2238 $subscription->userid = $user->id;
2239 $subscription->forum = $forum->id;
2240 $subscription->discussion = $discussion->id;
2241 $subscription->preference = \mod_forum\subscriptions::FORUM_DISCUSSION_UNSUBSCRIBED;
2243 $subscription->id = $DB->insert_record('forum_discussion_subs', $subscription);
2245 $context = context_module::instance($forum->cmid);
2248 'context' => $context,
2249 'objectid' => $subscription->id,
2250 'relateduserid' => $user->id,
2252 'forumid' => $forum->id,
2253 'discussion' => $discussion->id,
2257 $event = \mod_forum\event\discussion_subscription_deleted::create($params);
2259 // Trigger and capturing the event.
2260 $sink = $this->redirectEvents();
2262 $events = $sink->get_events();
2263 $this->assertCount(1, $events);
2264 $event = reset($events);
2266 // Without an invalid context.
2267 $params['context'] = \context_course::instance($course->id);
2268 $this->setExpectedException('coding_exception', 'Context level must be CONTEXT_MODULE.');
2269 \mod_forum\event\discussion_deleted::create($params);
2271 // Without the discussion.
2272 unset($params['discussion']);
2273 $this->setExpectedException('coding_exception', 'The \'discussion\' value must be set in other.');
2274 \mod_forum\event\discussion_deleted::create($params);
2276 // Without the forumid.
2277 unset($params['forumid']);
2278 $this->setExpectedException('coding_exception', 'The \'forumid\' value must be set in other.');
2279 \mod_forum\event\discussion_deleted::create($params);
2281 // Without the relateduserid.
2282 unset($params['relateduserid']);
2283 $this->setExpectedException('coding_exception', 'The \'relateduserid\' value must be set in other.');
2284 \mod_forum\event\discussion_deleted::create($params);
2288 * Test contextlevel validation of discussion_subscription_deleted event.
2290 public function test_discussion_subscription_deleted_validation_contextlevel() {
2292 require_once($CFG->dirroot . '/mod/forum/lib.php');
2295 $course = $this->getDataGenerator()->create_course();
2296 $user = $this->getDataGenerator()->create_user();
2297 $this->getDataGenerator()->enrol_user($user->id, $course->id);
2299 $options = array('course' => $course->id, 'forcesubscribe' => FORUM_CHOOSESUBSCRIBE);
2300 $forum = $this->getDataGenerator()->create_module('forum', $options);
2302 // Add a discussion.
2304 $record['course'] = $course->id;
2305 $record['forum'] = $forum->id;
2306 $record['userid'] = $user->id;
2307 $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
2311 $record['discussion'] = $discussion->id;
2312 $record['userid'] = $user->id;
2313 $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
2315 // The user is not subscribed to the forum. Insert a new discussion subscription.
2316 $subscription = new \stdClass();
2317 $subscription->userid = $user->id;
2318 $subscription->forum = $forum->id;
2319 $subscription->discussion = $discussion->id;
2320 $subscription->preference = \mod_forum\subscriptions::FORUM_DISCUSSION_SUBSCRIBED;
2322 $subscription->id = $DB->insert_record('forum_discussion_subs', $subscription);
2324 $context = context_module::instance($forum->cmid);
2327 'context' => \context_course::instance($course->id),
2328 'objectid' => $subscription->id,
2329 'relateduserid' => $user->id,
2331 'forumid' => $forum->id,
2332 'discussion' => $discussion->id,
2336 // Without an invalid context.
2337 $this->setExpectedException('coding_exception', 'Context level must be CONTEXT_MODULE.');
2338 \mod_forum\event\discussion_subscription_deleted::create($params);
2342 * Test discussion validation of discussion_subscription_deleted event.
2344 public function test_discussion_subscription_deleted_validation_discussion() {
2346 require_once($CFG->dirroot . '/mod/forum/lib.php');
2349 $course = $this->getDataGenerator()->create_course();
2350 $user = $this->getDataGenerator()->create_user();
2351 $this->getDataGenerator()->enrol_user($user->id, $course->id);
2353 $options = array('course' => $course->id, 'forcesubscribe' => FORUM_CHOOSESUBSCRIBE);
2354 $forum = $this->getDataGenerator()->create_module('forum', $options);
2356 // Add a discussion.
2358 $record['course'] = $course->id;
2359 $record['forum'] = $forum->id;
2360 $record['userid'] = $user->id;
2361 $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
2365 $record['discussion'] = $discussion->id;
2366 $record['userid'] = $user->id;
2367 $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
2369 // The user is not subscribed to the forum. Insert a new discussion subscription.
2370 $subscription = new \stdClass();
2371 $subscription->userid = $user->id;
2372 $subscription->forum = $forum->id;
2373 $subscription->discussion = $discussion->id;
2374 $subscription->preference = \mod_forum\subscriptions::FORUM_DISCUSSION_SUBSCRIBED;
2376 $subscription->id = $DB->insert_record('forum_discussion_subs', $subscription);
2378 // Without the discussion.
2380 'context' => context_module::instance($forum->cmid),
2381 'objectid' => $subscription->id,
2382 'relateduserid' => $user->id,
2384 'forumid' => $forum->id,
2388 $this->setExpectedException('coding_exception', "The 'discussion' value must be set in other.");
2389 \mod_forum\event\discussion_subscription_deleted::create($params);
2393 * Test forumid validation of discussion_subscription_deleted event.
2395 public function test_discussion_subscription_deleted_validation_forumid() {
2397 require_once($CFG->dirroot . '/mod/forum/lib.php');
2400 $course = $this->getDataGenerator()->create_course();
2401 $user = $this->getDataGenerator()->create_user();
2402 $this->getDataGenerator()->enrol_user($user->id, $course->id);
2404 $options = array('course' => $course->id, 'forcesubscribe' => FORUM_CHOOSESUBSCRIBE);
2405 $forum = $this->getDataGenerator()->create_module('forum', $options);
2407 // Add a discussion.
2409 $record['course'] = $course->id;
2410 $record['forum'] = $forum->id;
2411 $record['userid'] = $user->id;
2412 $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
2416 $record['discussion'] = $discussion->id;
2417 $record['userid'] = $user->id;
2418 $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
2420 // The user is not subscribed to the forum. Insert a new discussion subscription.
2421 $subscription = new \stdClass();
2422 $subscription->userid = $user->id;
2423 $subscription->forum = $forum->id;
2424 $subscription->discussion = $discussion->id;
2425 $subscription->preference = \mod_forum\subscriptions::FORUM_DISCUSSION_SUBSCRIBED;
2427 $subscription->id = $DB->insert_record('forum_discussion_subs', $subscription);
2429 // Without the forumid.
2431 'context' => context_module::instance($forum->cmid),
2432 'objectid' => $subscription->id,
2433 'relateduserid' => $user->id,
2435 'discussion' => $discussion->id,
2439 $this->setExpectedException('coding_exception', "The 'forumid' value must be set in other.");
2440 \mod_forum\event\discussion_subscription_deleted::create($params);
2444 * Test relateduserid validation of discussion_subscription_deleted event.
2446 public function test_discussion_subscription_deleted_validation_relateduserid() {
2448 require_once($CFG->dirroot . '/mod/forum/lib.php');
2451 $course = $this->getDataGenerator()->create_course();
2452 $user = $this->getDataGenerator()->create_user();
2453 $this->getDataGenerator()->enrol_user($user->id, $course->id);
2455 $options = array('course' => $course->id, 'forcesubscribe' => FORUM_CHOOSESUBSCRIBE);
2456 $forum = $this->getDataGenerator()->create_module('forum', $options);
2458 // Add a discussion.
2460 $record['course'] = $course->id;
2461 $record['forum'] = $forum->id;
2462 $record['userid'] = $user->id;
2463 $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
2467 $record['discussion'] = $discussion->id;
2468 $record['userid'] = $user->id;
2469 $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
2471 // The user is not subscribed to the forum. Insert a new discussion subscription.
2472 $subscription = new \stdClass();
2473 $subscription->userid = $user->id;
2474 $subscription->forum = $forum->id;
2475 $subscription->discussion = $discussion->id;
2476 $subscription->preference = \mod_forum\subscriptions::FORUM_DISCUSSION_SUBSCRIBED;
2478 $subscription->id = $DB->insert_record('forum_discussion_subs', $subscription);
2480 $context = context_module::instance($forum->cmid);
2482 // Without the relateduserid.
2484 'context' => context_module::instance($forum->cmid),
2485 'objectid' => $subscription->id,
2487 'forumid' => $forum->id,
2488 'discussion' => $discussion->id,
2492 $this->setExpectedException('coding_exception', "The 'relateduserid' must be set.");
2493 \mod_forum\event\discussion_subscription_deleted::create($params);
2497 * Test that the correct context is used in the events when subscribing
2500 public function test_forum_subscription_page_context_valid() {
2502 require_once($CFG->dirroot . '/mod/forum/lib.php');
2505 $course = $this->getDataGenerator()->create_course();
2506 $user = $this->getDataGenerator()->create_user();
2507 $this->getDataGenerator()->enrol_user($user->id, $course->id);
2509 $options = array('course' => $course->id, 'forcesubscribe' => FORUM_CHOOSESUBSCRIBE);
2510 $forum = $this->getDataGenerator()->create_module('forum', $options);
2511 $quiz = $this->getDataGenerator()->create_module('quiz', $options);
2513 // Add a discussion.
2515 $record['course'] = $course->id;
2516 $record['forum'] = $forum->id;
2517 $record['userid'] = $user->id;
2518 $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
2522 $record['discussion'] = $discussion->id;
2523 $record['userid'] = $user->id;
2524 $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
2526 // Set up the default page event to use this forum.
2527 $PAGE = new moodle_page();
2528 $cm = get_coursemodule_from_instance('forum', $discussion->forum);
2529 $context = \context_module::instance($cm->id);
2530 $PAGE->set_context($context);
2531 $PAGE->set_cm($cm, $course, $forum);
2533 // Trigger and capturing the event.
2534 $sink = $this->redirectEvents();
2536 // Trigger the event by subscribing the user to the forum.
2537 \mod_forum\subscriptions::subscribe_user($user->id, $forum);
2539 $events = $sink->get_events();
2541 $this->assertCount(1, $events);
2542 $event = reset($events);
2544 // Checking that the event contains the expected values.
2545 $this->assertInstanceOf('\mod_forum\event\subscription_created', $event);
2546 $this->assertEquals($context, $event->get_context());
2548 // Trigger the event by unsubscribing the user to the forum.
2549 \mod_forum\subscriptions::unsubscribe_user($user->id, $forum);
2551 $events = $sink->get_events();
2553 $this->assertCount(1, $events);
2554 $event = reset($events);
2556 // Checking that the event contains the expected values.
2557 $this->assertInstanceOf('\mod_forum\event\subscription_deleted', $event);
2558 $this->assertEquals($context, $event->get_context());
2560 // Trigger the event by subscribing the user to the discussion.
2561 \mod_forum\subscriptions::subscribe_user_to_discussion($user->id, $discussion);
2563 $events = $sink->get_events();
2565 $this->assertCount(1, $events);
2566 $event = reset($events);
2568 // Checking that the event contains the expected values.
2569 $this->assertInstanceOf('\mod_forum\event\discussion_subscription_created', $event);
2570 $this->assertEquals($context, $event->get_context());
2572 // Trigger the event by unsubscribing the user from the discussion.
2573 \mod_forum\subscriptions::unsubscribe_user_from_discussion($user->id, $discussion);
2575 $events = $sink->get_events();
2577 $this->assertCount(1, $events);
2578 $event = reset($events);
2580 // Checking that the event contains the expected values.
2581 $this->assertInstanceOf('\mod_forum\event\discussion_subscription_deleted', $event);
2582 $this->assertEquals($context, $event->get_context());
2584 // Now try with the context for a different module (quiz).
2585 $PAGE = new moodle_page();
2586 $cm = get_coursemodule_from_instance('quiz', $quiz->id);
2587 $quizcontext = \context_module::instance($cm->id);
2588 $PAGE->set_context($quizcontext);
2589 $PAGE->set_cm($cm, $course, $quiz);
2591 // Trigger and capturing the event.
2592 $sink = $this->redirectEvents();
2594 // Trigger the event by subscribing the user to the forum.
2595 \mod_forum\subscriptions::subscribe_user($user->id, $forum);
2597 $events = $sink->get_events();
2599 $this->assertCount(1, $events);
2600 $event = reset($events);
2602 // Checking that the event contains the expected values.
2603 $this->assertInstanceOf('\mod_forum\event\subscription_created', $event);
2604 $this->assertEquals($context, $event->get_context());
2606 // Trigger the event by unsubscribing the user to the forum.
2607 \mod_forum\subscriptions::unsubscribe_user($user->id, $forum);
2609 $events = $sink->get_events();
2611 $this->assertCount(1, $events);
2612 $event = reset($events);
2614 // Checking that the event contains the expected values.
2615 $this->assertInstanceOf('\mod_forum\event\subscription_deleted', $event);
2616 $this->assertEquals($context, $event->get_context());
2618 // Trigger the event by subscribing the user to the discussion.
2619 \mod_forum\subscriptions::subscribe_user_to_discussion($user->id, $discussion);
2621 $events = $sink->get_events();
2623 $this->assertCount(1, $events);
2624 $event = reset($events);
2626 // Checking that the event contains the expected values.
2627 $this->assertInstanceOf('\mod_forum\event\discussion_subscription_created', $event);
2628 $this->assertEquals($context, $event->get_context());
2630 // Trigger the event by unsubscribing the user from the discussion.
2631 \mod_forum\subscriptions::unsubscribe_user_from_discussion($user->id, $discussion);
2633 $events = $sink->get_events();
2635 $this->assertCount(1, $events);
2636 $event = reset($events);
2638 // Checking that the event contains the expected values.
2639 $this->assertInstanceOf('\mod_forum\event\discussion_subscription_deleted', $event);
2640 $this->assertEquals($context, $event->get_context());
2642 // Now try with the course context - the module context should still be used.
2643 $PAGE = new moodle_page();
2644 $coursecontext = \context_course::instance($course->id);
2645 $PAGE->set_context($coursecontext);
2647 // Trigger the event by subscribing the user to the forum.
2648 \mod_forum\subscriptions::subscribe_user($user->id, $forum);
2650 $events = $sink->get_events();
2652 $this->assertCount(1, $events);
2653 $event = reset($events);
2655 // Checking that the event contains the expected values.
2656 $this->assertInstanceOf('\mod_forum\event\subscription_created', $event);
2657 $this->assertEquals($context, $event->get_context());
2659 // Trigger the event by unsubscribing the user to the forum.
2660 \mod_forum\subscriptions::unsubscribe_user($user->id, $forum);
2662 $events = $sink->get_events();
2664 $this->assertCount(1, $events);
2665 $event = reset($events);
2667 // Checking that the event contains the expected values.
2668 $this->assertInstanceOf('\mod_forum\event\subscription_deleted', $event);
2669 $this->assertEquals($context, $event->get_context());
2671 // Trigger the event by subscribing the user to the discussion.
2672 \mod_forum\subscriptions::subscribe_user_to_discussion($user->id, $discussion);
2674 $events = $sink->get_events();
2676 $this->assertCount(1, $events);
2677 $event = reset($events);
2679 // Checking that the event contains the expected values.
2680 $this->assertInstanceOf('\mod_forum\event\discussion_subscription_created', $event);
2681 $this->assertEquals($context, $event->get_context());
2683 // Trigger the event by unsubscribing the user from the discussion.
2684 \mod_forum\subscriptions::unsubscribe_user_from_discussion($user->id, $discussion);
2686 $events = $sink->get_events();
2688 $this->assertCount(1, $events);
2689 $event = reset($events);
2691 // Checking that the event contains the expected values.
2692 $this->assertInstanceOf('\mod_forum\event\discussion_subscription_deleted', $event);
2693 $this->assertEquals($context, $event->get_context());