MDL-40062 mod_forum: add event unit tests
[moodle.git] / mod / forum / tests / events_test.php
CommitLineData
a52c04d5
DP
1<?php
2// This file is part of Moodle - http://moodle.org/
3//
4// Moodle is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// Moodle is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
16
17/**
18 * Tests for forum events.
19 *
20 * @package mod_forum
21 * @category test
22 * @copyright 2014 Dan Poltawski <dan@moodle.com>
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 */
25
26defined('MOODLE_INTERNAL') || die();
27
28/**
29 * Tests for forum events.
30 *
31 * @package mod_forum
32 * @category test
33 * @copyright 2014 Dan Poltawski <dan@moodle.com>
34 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
35 */
36class mod_forum_events_testcase extends advanced_testcase {
37
38 /**
39 * Tests set up.
40 */
41 public function setUp() {
42 $this->resetAfterTest();
43 }
44
45 /**
46 * Ensure course_searched event validates that searchterm is set.
47 */
48 public function test_course_searched_searchterm_validation() {
49 $course = $this->getDataGenerator()->create_course();
50 $coursectx = context_course::instance($course->id);
51 $params = array(
52 'context' => $coursectx,
53 );
54
55 $this->setExpectedException('coding_exception', 'searchterm must be set in $other.');
56 \mod_forum\event\course_searched::create($params);
57 }
58
59 /**
60 * Ensure course_searched event validates that context is the correct level.
61 */
62 public function test_course_searched_context_validation() {
63 $course = $this->getDataGenerator()->create_course();
64 $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
65 $context = context_module::instance($forum->cmid);
66 $params = array(
67 'context' => $context,
68 'other' => array('searchterm' => 'testing'),
69 );
70
71 $this->setExpectedException('coding_exception', 'Context passed must be course.');
72 \mod_forum\event\course_searched::create($params);
73 }
74
75 /**
76 * Test course_searched event.
77 */
78 public function test_course_searched() {
79
80 // Setup test data.
81 $course = $this->getDataGenerator()->create_course();
82 $coursectx = context_course::instance($course->id);
83 $searchterm = 'testing123';
84
85 $params = array(
86 'context' => $coursectx,
87 'other' => array('searchterm' => $searchterm),
88 );
89
90 // Create event.
91 $event = \mod_forum\event\course_searched::create($params);
92
93 // Trigger and capture the event.
94 $sink = $this->redirectEvents();
95 $event->trigger();
96 $events = $sink->get_events();
97 $this->assertCount(1, $events);
98 $event = reset($events);
99
100 // Checking that the event contains the expected values.
101 $this->assertInstanceOf('\mod_forum\event\course_searched', $event);
102 $this->assertEquals($coursectx, $event->get_context());
103 $expected = array($course->id, 'forum', 'search', "search.php?id={$course->id}&amp;search={$searchterm}", $searchterm);
104 $this->assertEventLegacyLogData($expected, $event);
105 $this->assertEventContextNotUsed($event);
106
107 $this->assertNotEmpty($event->get_name());
108 }
109
110 /**
111 * Ensure discussion_created event validates that forumid is set.
112 */
113 public function test_discussion_created_forumid_validation() {
114 $course = $this->getDataGenerator()->create_course();
115 $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
116 $context = context_module::instance($forum->cmid);
117
118 $params = array(
119 'context' => $context,
120 );
121
122 $this->setExpectedException('coding_exception', 'forumid must be set in $other.');
123 \mod_forum\event\discussion_created::create($params);
124 }
125
126 /**
127 * Ensure discussion_created event validates that discussionid is set.
128 */
129 public function test_discussion_created_objectid_validation() {
130 $course = $this->getDataGenerator()->create_course();
131 $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
132 $context = context_module::instance($forum->cmid);
133
134 $params = array(
135 'context' => $context,
136 'other' => array('forumid' => $forum->id)
137 );
138
139 $this->setExpectedException('coding_exception', 'objectid must be set to the discussionid.');
140 \mod_forum\event\discussion_created::create($params);
141 }
142
143 /**
144 * Ensure discussion_created event validates that the context is the correct level.
145 */
146 public function test_discussion_created_context_validation() {
147 $course = $this->getDataGenerator()->create_course();
148 $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
149
150 $params = array(
151 'context' => context_system::instance(),
152 'other' => array('forumid' => $forum->id),
153 );
154
155 $this->setExpectedException('coding_exception', 'Context passed must be module context.');
156 \mod_forum\event\discussion_created::create($params);
157 }
158
159 /**
160 * Test discussion_created event.
161 */
162 public function test_discussion_created() {
163
164 // Setup test data.
165 $course = $this->getDataGenerator()->create_course();
166 $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
167 $user = $this->getDataGenerator()->create_user();
168
169 // Add a discussion.
170 $record = array();
171 $record['course'] = $course->id;
172 $record['forum'] = $forum->id;
173 $record['userid'] = $user->id;
174 $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
175
176 $context = context_module::instance($forum->cmid);
177
178 $params = array(
179 'context' => $context,
180 'objectid' => $discussion->id,
181 'other' => array('forumid' => $forum->id),
182 );
183
184 // Create the event.
185 $event = \mod_forum\event\discussion_created::create($params);
186
187 // Trigger and capturing the event.
188 $sink = $this->redirectEvents();
189 $event->trigger();
190 $events = $sink->get_events();
191 $this->assertCount(1, $events);
192 $event = reset($events);
193
194 // Check that the event contains the expected values.
195 $this->assertInstanceOf('\mod_forum\event\discussion_created', $event);
196 $this->assertEquals($context, $event->get_context());
197 $expected = array($course->id, 'forum', 'add discussion', "discuss.php?d={$discussion->id}", $discussion->id, $forum->cmid);
198 $this->assertEventLegacyLogData($expected, $event);
199 $this->assertEventContextNotUsed($event);
200
201 $this->assertNotEmpty($event->get_name());
202 }
203
204 /**
205 * Ensure discussion_deleted event validates that forumid is set.
206 */
207 public function test_discussion_deleted_forumid_validation() {
208 $course = $this->getDataGenerator()->create_course();
209 $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
210 $context = context_module::instance($forum->cmid);
211
212 $params = array(
213 'context' => $context,
214 );
215
216 $this->setExpectedException('coding_exception', 'forumid must be set in $other.');
217 \mod_forum\event\discussion_deleted::create($params);
218 }
219
220 /**
221 * Ensure discussion_deleted event validates that discussionid is set.
222 */
223 public function test_discussion_deleted_objectid_validation() {
224 $course = $this->getDataGenerator()->create_course();
225 $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
226 $context = context_module::instance($forum->cmid);
227
228 $params = array(
229 'context' => $context,
230 'other' => array('forumid' => $forum->id)
231 );
232
233 $this->setExpectedException('coding_exception', 'objectid must be set to the discussionid.');
234 \mod_forum\event\discussion_deleted::create($params);
235 }
236
237 /**
238 * Ensure discussion_deleted event validates that context is of the correct level.
239 */
240 public function test_discussion_deleted_context_validation() {
241 $course = $this->getDataGenerator()->create_course();
242 $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
243
244 $params = array(
245 'context' => context_system::instance(),
246 'other' => array('forumid' => $forum->id),
247 );
248
249 $this->setExpectedException('coding_exception', 'Context passed must be module context.');
250 \mod_forum\event\discussion_deleted::create($params);
251 }
252
253 /**
254 * Test discussion_deleted event.
255 */
256 public function test_discussion_deleted() {
257
258 // Setup test data.
259 $course = $this->getDataGenerator()->create_course();
260 $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
261 $user = $this->getDataGenerator()->create_user();
262
263 // Add a discussion.
264 $record = array();
265 $record['course'] = $course->id;
266 $record['forum'] = $forum->id;
267 $record['userid'] = $user->id;
268 $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
269
270 $context = context_module::instance($forum->cmid);
271
272 $params = array(
273 'context' => $context,
274 'objectid' => $discussion->id,
275 'other' => array('forumid' => $forum->id),
276 );
277
278 $event = \mod_forum\event\discussion_deleted::create($params);
279
280 // Trigger and capture the event.
281 $sink = $this->redirectEvents();
282 $event->trigger();
283 $events = $sink->get_events();
284 $this->assertCount(1, $events);
285 $event = reset($events);
286
287 // Checking that the event contains the expected values.
288 $this->assertInstanceOf('\mod_forum\event\discussion_deleted', $event);
289 $this->assertEquals($context, $event->get_context());
290 $expected = array($course->id, 'forum', 'delete discussion', "view.php?id={$forum->cmid}", $forum->id, $forum->cmid);
291 $this->assertEventLegacyLogData($expected, $event);
292 $this->assertEventContextNotUsed($event);
293
294 $this->assertNotEmpty($event->get_name());
295 }
296
297 /**
298 * Ensure discussion_moved event validates that fromforumid is set.
299 */
300 public function test_discussion_moved_fromforumid_validation() {
301 $course = $this->getDataGenerator()->create_course();
302 $toforum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
303
304 $context = context_module::instance($toforum->cmid);
305
306 $params = array(
307 'context' => $context,
308 'other' => array('toforumid' => $toforum->id)
309 );
310
311 $this->setExpectedException('coding_exception', 'fromforumid must be set in $other.');
312 \mod_forum\event\discussion_moved::create($params);
313 }
314
315 /**
316 * Ensure discussion_moved event validates that toforumid is set.
317 */
318 public function test_discussion_moved_toforumid_validation() {
319 $course = $this->getDataGenerator()->create_course();
320 $fromforum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
321 $toforum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
322 $context = context_module::instance($toforum->cmid);
323
324 $params = array(
325 'context' => $context,
326 'other' => array('fromforumid' => $fromforum->id)
327 );
328
329 $this->setExpectedException('coding_exception', 'toforumid must be set in $other.');
330 \mod_forum\event\discussion_moved::create($params);
331 }
332
333 /**
334 * Ensure discussion_moved event validates that the discussionid is set.
335 */
336 public function test_discussion_moved_objectid_validation() {
337 $course = $this->getDataGenerator()->create_course();
338 $fromforum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
339 $toforum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
340 $context = context_module::instance($toforum->cmid);
341
342 $params = array(
343 'context' => $context,
344 'other' => array('fromforumid' => $fromforum->id, 'toforumid' => $toforum->id)
345 );
346
347 $this->setExpectedException('coding_exception', 'objectid must be set to the discussionid.');
348 \mod_forum\event\discussion_moved::create($params);
349 }
350
351 /**
352 * Ensure discussion_moved event validates that the context level is correct.
353 */
354 public function test_discussion_moved_context_validation() {
355 $course = $this->getDataGenerator()->create_course();
356 $fromforum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
357 $toforum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
358 $user = $this->getDataGenerator()->create_user();
359
360 // Add a discussion.
361 $record = array();
362 $record['course'] = $course->id;
363 $record['forum'] = $fromforum->id;
364 $record['userid'] = $user->id;
365 $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
366
367 $params = array(
368 'context' => context_system::instance(),
369 'objectid' => $discussion->id,
370 'other' => array('fromforumid' => $fromforum->id, 'toforumid' => $toforum->id)
371 );
372
373 $this->setExpectedException('coding_exception', 'Context passed must be module context.');
374 \mod_forum\event\discussion_moved::create($params);
375 }
376
377 /**
378 * Test discussion_moved event.
379 */
380 public function test_discussion_moved() {
381 // Setup test data.
382 $course = $this->getDataGenerator()->create_course();
383 $fromforum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
384 $toforum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
385 $user = $this->getDataGenerator()->create_user();
386
387 // Add a discussion.
388 $record = array();
389 $record['course'] = $course->id;
390 $record['forum'] = $fromforum->id;
391 $record['userid'] = $user->id;
392 $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
393
394 $context = context_module::instance($toforum->cmid);
395
396 $params = array(
397 'context' => $context,
398 'objectid' => $discussion->id,
399 'other' => array('fromforumid' => $fromforum->id, 'toforumid' => $toforum->id)
400 );
401
402 $event = \mod_forum\event\discussion_moved::create($params);
403
404 // Trigger and capture the event.
405 $sink = $this->redirectEvents();
406 $event->trigger();
407 $events = $sink->get_events();
408 $this->assertCount(1, $events);
409 $event = reset($events);
410
411 // Checking that the event contains the expected values.
412 $this->assertInstanceOf('\mod_forum\event\discussion_moved', $event);
413 $this->assertEquals($context, $event->get_context());
414 $expected = array($course->id, 'forum', 'move discussion', "discuss.php?d={$discussion->id}",
415 $discussion->id, $toforum->cmid);
416 $this->assertEventLegacyLogData($expected, $event);
417 $this->assertEventContextNotUsed($event);
418
419 $this->assertNotEmpty($event->get_name());
420 }
421
422 /**
423 * Ensure discussion_viewed event validates that the discussionid is set
424 */
425 public function test_discussion_viewed_objectid_validation() {
426 $course = $this->getDataGenerator()->create_course();
427 $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
428 $context = context_module::instance($forum->cmid);
429
430 $params = array(
431 'context' => $context,
432 );
433
434 $this->setExpectedException('coding_exception', 'objectid must be set to the discussionid.');
435 \mod_forum\event\discussion_viewed::create($params);
436 }
437
438 /**
439 * Ensure discussion_viewed event validates that the contextlevel is correct.
440 */
441 public function test_discussion_viewed_context_validation() {
442 $course = $this->getDataGenerator()->create_course();
443 $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
444 $user = $this->getDataGenerator()->create_user();
445
446 // Add a discussion.
447 $record = array();
448 $record['course'] = $course->id;
449 $record['forum'] = $forum->id;
450 $record['userid'] = $user->id;
451 $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
452
453 $params = array(
454 'context' => context_system::instance(),
455 'objectid' => $discussion->id,
456 );
457
458 $this->setExpectedException('coding_exception', 'Context passed must be module context.');
459 \mod_forum\event\discussion_viewed::create($params);
460 }
461
462 /**
463 * Test discussion_viewed event.
464 */
465 public function test_discussion_viewed() {
466 // Setup test data.
467 $course = $this->getDataGenerator()->create_course();
468 $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
469 $user = $this->getDataGenerator()->create_user();
470
471 // Add a discussion.
472 $record = array();
473 $record['course'] = $course->id;
474 $record['forum'] = $forum->id;
475 $record['userid'] = $user->id;
476 $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
477
478 $context = context_module::instance($forum->cmid);
479
480 $params = array(
481 'context' => $context,
482 'objectid' => $discussion->id,
483 );
484
485 $event = \mod_forum\event\discussion_viewed::create($params);
486
487 // Trigger and capture the event.
488 $sink = $this->redirectEvents();
489 $event->trigger();
490 $events = $sink->get_events();
491 $this->assertCount(1, $events);
492 $event = reset($events);
493
494 // Checking that the event contains the expected values.
495 $this->assertInstanceOf('\mod_forum\event\discussion_viewed', $event);
496 $this->assertEquals($context, $event->get_context());
497 $expected = array($course->id, 'forum', 'view discussion', "discuss.php?d={$discussion->id}",
498 $discussion->id, $forum->cmid);
499 $this->assertEventLegacyLogData($expected, $event);
500 $this->assertEventContextNotUsed($event);
501
502 $this->assertNotEmpty($event->get_name());
503 }
504
505 /**
506 * Ensure forum_viewed event validates that the forumid is set.
507 */
508 public function test_forum_viewed_objectid_validation() {
509 $course = $this->getDataGenerator()->create_course();
510 $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
511 $context = context_module::instance($forum->cmid);
512
513 $params = array(
514 'context' => $context,
515 );
516
517 $this->setExpectedException('coding_exception', 'objectid must be set to the forumid.');
518 \mod_forum\event\forum_viewed::create($params);
519 }
520
521 /**
522 * Ensure forum_viewed event validates that the contextlevel is correct.
523 */
524 public function test_forum_viewed_context_validation() {
525 $course = $this->getDataGenerator()->create_course();
526 $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
527
528 $params = array(
529 'context' => context_system::instance(),
530 'objectid' => $forum->id,
531 );
532
533 $this->setExpectedException('coding_exception', 'Context passed must be module context.');
534 \mod_forum\event\forum_viewed::create($params);
535 }
536
537 /**
538 * Test the forum_viewed event.
539 */
540 public function test_forum_viewed() {
541 // Setup test data.
542 $course = $this->getDataGenerator()->create_course();
543 $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
544
545 $context = context_module::instance($forum->cmid);
546
547 $params = array(
548 'context' => $context,
549 'objectid' => $forum->id,
550 );
551
552 $event = \mod_forum\event\forum_viewed::create($params);
553
554 // Trigger and capture the event.
555 $sink = $this->redirectEvents();
556 $event->trigger();
557 $events = $sink->get_events();
558 $this->assertCount(1, $events);
559 $event = reset($events);
560
561 // Checking that the event contains the expected values.
562 $this->assertInstanceOf('\mod_forum\event\forum_viewed', $event);
563 $this->assertEquals($context, $event->get_context());
564 $expected = array($course->id, 'forum', 'view forum', "view.php?f={$forum->id}", $forum->id, $forum->cmid);
565 $this->assertEventLegacyLogData($expected, $event);
566 $this->assertEventContextNotUsed($event);
567
568 $this->assertNotEmpty($event->get_name());
569 }
570
571 /**
572 * Ensure subscription_created event validates that the forumid is set.
573 */
574 public function test_subscription_created_forumid_validation() {
575 $user = $this->getDataGenerator()->create_user();
576 $course = $this->getDataGenerator()->create_course();
577 $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
578
579 $params = array(
580 'context' => context_module::instance($forum->cmid),
581 'relateduserid' => $user->id,
582 );
583
584 $this->setExpectedException('coding_exception', 'forumid must be set in other.');
585 \mod_forum\event\subscription_created::create($params);
586 }
587
588 /**
589 * Ensure subscription_created event validates that the relateduserid is set.
590 */
591 public function test_subscription_created_relateduserid_validation() {
592 $course = $this->getDataGenerator()->create_course();
593 $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
594
595 $params = array(
596 'context' => context_module::instance($forum->cmid),
597 'objectid' => $forum->id,
598 );
599
600 $this->setExpectedException('coding_exception', 'relateduserid must be set.');
601 \mod_forum\event\subscription_created::create($params);
602 }
603
604 /**
605 * Ensure subscription_created event validates that the contextlevel is correct.
606 */
607 public function test_subscription_created_contextlevel_validation() {
608 $user = $this->getDataGenerator()->create_user();
609 $course = $this->getDataGenerator()->create_course();
610 $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
611
612 $params = array(
613 'context' => context_system::instance(),
614 'other' => array('forumid' => $forum->id),
615 'relateduserid' => $user->id,
616 );
617
618 $this->setExpectedException('coding_exception', 'Context passed must be module context.');
619 \mod_forum\event\subscription_created::create($params);
620 }
621
622 /**
623 * Test the subscription_created event.
624 */
625 public function test_subscription_created() {
626 // Setup test data.
627 $user = $this->getDataGenerator()->create_user();
628 $course = $this->getDataGenerator()->create_course();
629 $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
630 $context = context_module::instance($forum->cmid);
631
632 $params = array(
633 'context' => $context,
634 'other' => array('forumid' => $forum->id),
635 'relateduserid' => $user->id,
636 );
637
638 $event = \mod_forum\event\subscription_created::create($params);
639
640 // Trigger and capturing the event.
641 $sink = $this->redirectEvents();
642 $event->trigger();
643 $events = $sink->get_events();
644 $this->assertCount(1, $events);
645 $event = reset($events);
646
647 // Checking that the event contains the expected values.
648 $this->assertInstanceOf('\mod_forum\event\subscription_created', $event);
649 $this->assertEquals($context, $event->get_context());
650 $expected = array($course->id, 'forum', 'subscribe', "view.php?f={$forum->id}", $forum->id, $forum->cmid);
651 $this->assertEventLegacyLogData($expected, $event);
652 $this->assertEventContextNotUsed($event);
653
654 $this->assertNotEmpty($event->get_name());
655 }
656
657 /**
658 * Ensure subscription_deleted event validates that the forumid is set.
659 */
660 public function test_subscription_deleted_forumid_validation() {
661 $user = $this->getDataGenerator()->create_user();
662 $course = $this->getDataGenerator()->create_course();
663 $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
664
665 $params = array(
666 'context' => context_module::instance($forum->cmid),
667 'relateduserid' => $user->id,
668 );
669
670 $this->setExpectedException('coding_exception', 'forumid must be set in other.');
671 \mod_forum\event\subscription_deleted::create($params);
672 }
673
674 /**
675 * Ensure subscription_deleted event validates that the relateduserid is set.
676 */
677 public function test_subscription_deleted_relateduserid_validation() {
678 $course = $this->getDataGenerator()->create_course();
679 $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
680
681 $params = array(
682 'context' => context_module::instance($forum->cmid),
683 'objectid' => $forum->id,
684 );
685
686 $this->setExpectedException('coding_exception', 'relateduserid must be set.');
687 \mod_forum\event\subscription_deleted::create($params);
688 }
689
690 /**
691 * Ensure subscription_deleted event validates that the contextlevel is correct.
692 */
693 public function test_subscription_deleted_contextlevel_validation() {
694 $user = $this->getDataGenerator()->create_user();
695 $course = $this->getDataGenerator()->create_course();
696 $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
697
698 $params = array(
699 'context' => context_system::instance(),
700 'other' => array('forumid' => $forum->id),
701 'relateduserid' => $user->id,
702 );
703
704 $this->setExpectedException('coding_exception', 'Context passed must be module context.');
705 \mod_forum\event\subscription_deleted::create($params);
706 }
707
708 /**
709 * Test the subscription_deleted event.
710 */
711 public function test_subscription_deleted() {
712 // Setup test data.
713 $user = $this->getDataGenerator()->create_user();
714 $course = $this->getDataGenerator()->create_course();
715 $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
716 $context = context_module::instance($forum->cmid);
717
718 $params = array(
719 'context' => $context,
720 'other' => array('forumid' => $forum->id),
721 'relateduserid' => $user->id,
722 );
723
724 $event = \mod_forum\event\subscription_deleted::create($params);
725
726 // Trigger and capturing the event.
727 $sink = $this->redirectEvents();
728 $event->trigger();
729 $events = $sink->get_events();
730 $this->assertCount(1, $events);
731 $event = reset($events);
732
733 // Checking that the event contains the expected values.
734 $this->assertInstanceOf('\mod_forum\event\subscription_deleted', $event);
735 $this->assertEquals($context, $event->get_context());
736 $expected = array($course->id, 'forum', 'unsubscribe', "view.php?f={$forum->id}", $forum->id, $forum->cmid);
737 $this->assertEventLegacyLogData($expected, $event);
738 $this->assertEventContextNotUsed($event);
739
740 $this->assertNotEmpty($event->get_name());
741 }
742
743 /**
744 * Ensure readtracking_enabled event validates that the forumid is set.
745 */
746 public function test_readtracking_enabled_forumid_validation() {
747 $user = $this->getDataGenerator()->create_user();
748 $course = $this->getDataGenerator()->create_course();
749 $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
750
751 $params = array(
752 'context' => context_module::instance($forum->cmid),
753 'relateduserid' => $user->id,
754 );
755
756 $this->setExpectedException('coding_exception', 'forumid must be set in other.');
757 \mod_forum\event\readtracking_enabled::create($params);
758 }
759
760 /**
761 * Ensure readtracking_enabled event validates that the relateduserid is set.
762 */
763 public function test_readtracking_enabled_relateduserid_validation() {
764 $course = $this->getDataGenerator()->create_course();
765 $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
766
767 $params = array(
768 'context' => context_module::instance($forum->cmid),
769 'objectid' => $forum->id,
770 );
771
772 $this->setExpectedException('coding_exception', 'relateduserid must be set.');
773 \mod_forum\event\readtracking_enabled::create($params);
774 }
775
776 /**
777 * Ensure readtracking_enabled event validates that the contextlevel is correct.
778 */
779 public function test_readtracking_enabled_contextlevel_validation() {
780 $user = $this->getDataGenerator()->create_user();
781 $course = $this->getDataGenerator()->create_course();
782 $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
783
784 $params = array(
785 'context' => context_system::instance(),
786 'other' => array('forumid' => $forum->id),
787 'relateduserid' => $user->id,
788 );
789
790 $this->setExpectedException('coding_exception', 'Context passed must be module context.');
791 \mod_forum\event\readtracking_enabled::create($params);
792 }
793
794 /**
795 * Test the readtracking_enabled event.
796 */
797 public function test_readtracking_enabled() {
798 // Setup test data.
799 $user = $this->getDataGenerator()->create_user();
800 $course = $this->getDataGenerator()->create_course();
801 $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
802 $context = context_module::instance($forum->cmid);
803
804 $params = array(
805 'context' => $context,
806 'other' => array('forumid' => $forum->id),
807 'relateduserid' => $user->id,
808 );
809
810 $event = \mod_forum\event\readtracking_enabled::create($params);
811
812 // Trigger and capture the event.
813 $sink = $this->redirectEvents();
814 $event->trigger();
815 $events = $sink->get_events();
816 $this->assertCount(1, $events);
817 $event = reset($events);
818
819 // Checking that the event contains the expected values.
820 $this->assertInstanceOf('\mod_forum\event\readtracking_enabled', $event);
821 $this->assertEquals($context, $event->get_context());
822 $expected = array($course->id, 'forum', 'start tracking', "view.php?f={$forum->id}", $forum->id, $forum->cmid);
823 $this->assertEventLegacyLogData($expected, $event);
824 $this->assertEventContextNotUsed($event);
825
826 $this->assertNotEmpty($event->get_name());
827 }
828
829 /**
830 * Ensure readtracking_disabled event validates that the forumid is set.
831 */
832 public function test_readtracking_disabled_forumid_validation() {
833 $user = $this->getDataGenerator()->create_user();
834 $course = $this->getDataGenerator()->create_course();
835 $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
836
837 $params = array(
838 'context' => context_module::instance($forum->cmid),
839 'relateduserid' => $user->id,
840 );
841
842 $this->setExpectedException('coding_exception', 'forumid must be set in other.');
843 \mod_forum\event\readtracking_disabled::create($params);
844 }
845
846 /**
847 * Ensure readtracking_disabled event validates that the relateduserid is set.
848 */
849 public function test_readtracking_disabled_relateduserid_validation() {
850 $course = $this->getDataGenerator()->create_course();
851 $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
852
853 $params = array(
854 'context' => context_module::instance($forum->cmid),
855 'objectid' => $forum->id,
856 );
857
858 $this->setExpectedException('coding_exception', 'relateduserid must be set.');
859 \mod_forum\event\readtracking_disabled::create($params);
860 }
861
862 /**
863 * Ensure readtracking_disabled event validates that the contextlevel is correct
864 */
865 public function test_readtracking_disabled_contextlevel_validation() {
866 $user = $this->getDataGenerator()->create_user();
867 $course = $this->getDataGenerator()->create_course();
868 $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
869
870 $params = array(
871 'context' => context_system::instance(),
872 'other' => array('forumid' => $forum->id),
873 'relateduserid' => $user->id,
874 );
875
876 $this->setExpectedException('coding_exception', 'Context passed must be module context.');
877 \mod_forum\event\readtracking_disabled::create($params);
878 }
879
880 /**
881 * Test the readtracking_disabled event.
882 */
883 public function test_readtracking_disabled() {
884 // Setup test data.
885 $user = $this->getDataGenerator()->create_user();
886 $course = $this->getDataGenerator()->create_course();
887 $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
888 $context = context_module::instance($forum->cmid);
889
890 $params = array(
891 'context' => $context,
892 'other' => array('forumid' => $forum->id),
893 'relateduserid' => $user->id,
894 );
895
896 $event = \mod_forum\event\readtracking_disabled::create($params);
897
898 // Trigger and capture the event.
899 $sink = $this->redirectEvents();
900 $event->trigger();
901 $events = $sink->get_events();
902 $this->assertCount(1, $events);
903 $event = reset($events);
904
905 // Checking that the event contains the expected values.
906 $this->assertInstanceOf('\mod_forum\event\readtracking_disabled', $event);
907 $this->assertEquals($context, $event->get_context());
908 $expected = array($course->id, 'forum', 'stop tracking', "view.php?f={$forum->id}", $forum->id, $forum->cmid);
909 $this->assertEventLegacyLogData($expected, $event);
910 $this->assertEventContextNotUsed($event);
911
912 $this->assertNotEmpty($event->get_name());
913 }
914
915 /**
916 * Ensure subscribers_viewed event validates that the forumid is set.
917 */
918 public function test_subscribers_viewed_forumid_validation() {
919 $user = $this->getDataGenerator()->create_user();
920 $course = $this->getDataGenerator()->create_course();
921 $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
922
923 $params = array(
924 'context' => context_module::instance($forum->cmid),
925 'relateduserid' => $user->id,
926 );
927
928 $this->setExpectedException('coding_exception', 'forumid must be set in other.');
929 \mod_forum\event\subscribers_viewed::create($params);
930 }
931
932 /**
933 * Ensure subscribers_viewed event validates that the contextlevel is correct.
934 */
935 public function test_subscribers_viewed_contextlevel_validation() {
936 $user = $this->getDataGenerator()->create_user();
937 $course = $this->getDataGenerator()->create_course();
938 $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
939
940 $params = array(
941 'context' => context_system::instance(),
942 'other' => array('forumid' => $forum->id),
943 'relateduserid' => $user->id,
944 );
945
946 $this->setExpectedException('coding_exception', 'Context passed must be module context.');
947 \mod_forum\event\subscribers_viewed::create($params);
948 }
949
950 /**
951 * Test the subscribers_viewed event.
952 */
953 public function test_subscribers_viewed() {
954 // Setup test data.
955 $course = $this->getDataGenerator()->create_course();
956 $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
957 $context = context_module::instance($forum->cmid);
958
959 $params = array(
960 'context' => $context,
961 'other' => array('forumid' => $forum->id),
962 );
963
964 $event = \mod_forum\event\subscribers_viewed::create($params);
965
966 // Trigger and capture the event.
967 $sink = $this->redirectEvents();
968 $event->trigger();
969 $events = $sink->get_events();
970 $this->assertCount(1, $events);
971 $event = reset($events);
972
973 // Checking that the event contains the expected values.
974 $this->assertInstanceOf('\mod_forum\event\subscribers_viewed', $event);
975 $this->assertEquals($context, $event->get_context());
976 $expected = array($course->id, 'forum', 'view subscribers', "subscribers.php?id={$forum->id}", $forum->id, $forum->cmid);
977 $this->assertEventLegacyLogData($expected, $event);
978 $this->assertEventContextNotUsed($event);
979
980 $this->assertNotEmpty($event->get_name());
981 }
982
983 /**
984 * Ensure userreport_viewed event validates that the reportmode is set.
985 */
986 public function test_userreport_viewed_reportmode_validation() {
987 $user = $this->getDataGenerator()->create_user();
988 $course = $this->getDataGenerator()->create_course();
989
990 $params = array(
991 'context' => context_course::instance($course->id),
992 'relateduserid' => $user->id,
993 );
994
995 $this->setExpectedException('coding_exception', 'reportmode must be set in other.');
996 \mod_forum\event\userreport_viewed::create($params);
997 }
998
999 /**
1000 * Ensure userreport_viewed event validates that the contextlevel is correct.
1001 */
1002 public function test_userreport_viewed_contextlevel_validation() {
1003 $user = $this->getDataGenerator()->create_user();
1004 $course = $this->getDataGenerator()->create_course();
1005 $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
1006
1007 $params = array(
1008 'context' => context_module::instance($forum->id),
1009 'other' => array('reportmode' => 'posts'),
1010 'relateduserid' => $user->id,
1011 );
1012
1013 $this->setExpectedException('coding_exception', 'Context passed must be system or course.');
1014 \mod_forum\event\userreport_viewed::create($params);
1015 }
1016
1017 /**
1018 * Ensure userreport_viewed event validates that the relateduserid is set.
1019 */
1020 public function test_userreport_viewed_relateduserid_validation() {
1021
1022 $params = array(
1023 'context' => context_system::instance(),
1024 'other' => array('reportmode' => 'posts'),
1025 );
1026
1027 $this->setExpectedException('coding_exception', 'relateduserid must be set.');
1028 \mod_forum\event\userreport_viewed::create($params);
1029 }
1030
1031 /**
1032 * Test the userreport_viewed event.
1033 */
1034 public function test_userreport_viewed() {
1035 // Setup test data.
1036 $user = $this->getDataGenerator()->create_user();
1037 $course = $this->getDataGenerator()->create_course();
1038 $context = context_course::instance($course->id);
1039
1040 $params = array(
1041 'context' => $context,
1042 'relateduserid' => $user->id,
1043 'other' => array('reportmode' => 'discussions'),
1044 );
1045
1046 $event = \mod_forum\event\userreport_viewed::create($params);
1047
1048 // Trigger and capture the event.
1049 $sink = $this->redirectEvents();
1050 $event->trigger();
1051 $events = $sink->get_events();
1052 $this->assertCount(1, $events);
1053 $event = reset($events);
1054
1055 // Checking that the event contains the expected values.
1056 $this->assertInstanceOf('\mod_forum\event\userreport_viewed', $event);
1057 $this->assertEquals($context, $event->get_context());
1058 $expected = array($course->id, 'forum', 'user report',
1059 "user.php?id={$user->id}&amp;mode=discussions&amp;course={$course->id}", $user->id);
1060 $this->assertEventLegacyLogData($expected, $event);
1061 $this->assertEventContextNotUsed($event);
1062
1063 $this->assertNotEmpty($event->get_name());
1064 }
1065
1066 /**
1067 * Ensure post_created event validates that the postid is set.
1068 */
1069 public function test_post_created_postid_validation() {
1070 $course = $this->getDataGenerator()->create_course();
1071 $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
1072 $user = $this->getDataGenerator()->create_user();
1073
1074 // Add a discussion.
1075 $record = array();
1076 $record['course'] = $course->id;
1077 $record['forum'] = $forum->id;
1078 $record['userid'] = $user->id;
1079 $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1080
1081 $params = array(
1082 'context' => context_module::instance($forum->cmid),
1083 'other' => array('forumid' => $forum->id, 'forumtype' => $forum->type, 'discussionid' => $discussion->id)
1084 );
1085
1086 $this->setExpectedException('coding_exception', 'objectid must be set to the postid.');
1087 \mod_forum\event\post_created::create($params);
1088 }
1089
1090 /**
1091 * Ensure post_created event validates that the discussionid is set.
1092 */
1093 public function test_post_created_discussionid_validation() {
1094 $course = $this->getDataGenerator()->create_course();
1095 $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
1096 $user = $this->getDataGenerator()->create_user();
1097
1098 // Add a discussion.
1099 $record = array();
1100 $record['course'] = $course->id;
1101 $record['forum'] = $forum->id;
1102 $record['userid'] = $user->id;
1103 $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1104
1105 // Add a post.
1106 $record = array();
1107 $record['discussion'] = $discussion->id;
1108 $record['userid'] = $user->id;
1109 $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
1110
1111 $params = array(
1112 'context' => context_module::instance($forum->cmid),
1113 'objectid' => $post->id,
1114 'other' => array('forumid' => $forum->id, 'forumtype' => $forum->type)
1115 );
1116
1117 $this->setExpectedException('coding_exception', 'discussionid must be set in other.');
1118 \mod_forum\event\post_created::create($params);
1119 }
1120
1121 /**
1122 * Ensure post_created event validates that the forumid is set.
1123 */
1124 public function test_post_created_forumid_validation() {
1125 $course = $this->getDataGenerator()->create_course();
1126 $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
1127 $user = $this->getDataGenerator()->create_user();
1128
1129 // Add a discussion.
1130 $record = array();
1131 $record['course'] = $course->id;
1132 $record['forum'] = $forum->id;
1133 $record['userid'] = $user->id;
1134 $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1135
1136 // Add a post.
1137 $record = array();
1138 $record['discussion'] = $discussion->id;
1139 $record['userid'] = $user->id;
1140 $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
1141
1142 $params = array(
1143 'context' => context_module::instance($forum->cmid),
1144 'objectid' => $post->id,
1145 'other' => array('discussionid' => $discussion->id, 'forumtype' => $forum->type)
1146 );
1147
1148 $this->setExpectedException('coding_exception', 'forumid must be set in other.');
1149 \mod_forum\event\post_created::create($params);
1150 }
1151
1152 /**
1153 * Ensure post_created event validates that the forumtype is set.
1154 */
1155 public function test_post_created_forumtype_validation() {
1156 $course = $this->getDataGenerator()->create_course();
1157 $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
1158 $user = $this->getDataGenerator()->create_user();
1159
1160 // Add a discussion.
1161 $record = array();
1162 $record['course'] = $course->id;
1163 $record['forum'] = $forum->id;
1164 $record['userid'] = $user->id;
1165 $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1166
1167 // Add a post.
1168 $record = array();
1169 $record['discussion'] = $discussion->id;
1170 $record['userid'] = $user->id;
1171 $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
1172
1173 $params = array(
1174 'context' => context_module::instance($forum->cmid),
1175 'objectid' => $post->id,
1176 'other' => array('discussionid' => $discussion->id, 'forumid' => $forum->id)
1177 );
1178
1179 $this->setExpectedException('coding_exception', 'forumtype must be set in other.');
1180 \mod_forum\event\post_created::create($params);
1181 }
1182
1183 /**
1184 * Ensure post_created event validates that the contextlevel is correct.
1185 */
1186 public function test_post_created_context_validation() {
1187 $course = $this->getDataGenerator()->create_course();
1188 $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
1189 $user = $this->getDataGenerator()->create_user();
1190
1191 // Add a discussion.
1192 $record = array();
1193 $record['course'] = $course->id;
1194 $record['forum'] = $forum->id;
1195 $record['userid'] = $user->id;
1196 $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1197
1198 // Add a post.
1199 $record = array();
1200 $record['discussion'] = $discussion->id;
1201 $record['userid'] = $user->id;
1202 $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
1203
1204 $params = array(
1205 'context' => context_system::instance(),
1206 'objectid' => $post->id,
1207 'other' => array('discussionid' => $discussion->id, 'forumid' => $forum->id, 'forumtype' => $forum->type)
1208 );
1209
1210 $this->setExpectedException('coding_exception', 'Context passed must be module context');
1211 \mod_forum\event\post_created::create($params);
1212 }
1213
1214 /**
1215 * Test the post_created event.
1216 */
1217 public function test_post_created() {
1218 // Setup test data.
1219 $course = $this->getDataGenerator()->create_course();
1220 $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
1221 $user = $this->getDataGenerator()->create_user();
1222
1223 // Add a discussion.
1224 $record = array();
1225 $record['course'] = $course->id;
1226 $record['forum'] = $forum->id;
1227 $record['userid'] = $user->id;
1228 $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1229
1230 // Add a post.
1231 $record = array();
1232 $record['discussion'] = $discussion->id;
1233 $record['userid'] = $user->id;
1234 $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
1235
1236 $context = context_module::instance($forum->cmid);
1237
1238 $params = array(
1239 'context' => $context,
1240 'objectid' => $post->id,
1241 'other' => array('discussionid' => $discussion->id, 'forumid' => $forum->id, 'forumtype' => $forum->type)
1242 );
1243
1244 $event = \mod_forum\event\post_created::create($params);
1245
1246 // Trigger and capturing the event.
1247 $sink = $this->redirectEvents();
1248 $event->trigger();
1249 $events = $sink->get_events();
1250 $this->assertCount(1, $events);
1251 $event = reset($events);
1252
1253 // Checking that the event contains the expected values.
1254 $this->assertInstanceOf('\mod_forum\event\post_created', $event);
1255 $this->assertEquals($context, $event->get_context());
1256 $expected = array($course->id, 'forum', 'add post', "discuss.php?d={$discussion->id}#p{$post->id}",
1257 $forum->id, $forum->cmid);
1258 $this->assertEventLegacyLogData($expected, $event);
1259 $this->assertEventContextNotUsed($event);
1260
1261 $this->assertNotEmpty($event->get_name());
1262 }
1263
1264 /**
1265 * Ensure post_deleted event validates that the postid is set.
1266 */
1267 public function test_post_deleted_postid_validation() {
1268 $course = $this->getDataGenerator()->create_course();
1269 $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
1270 $user = $this->getDataGenerator()->create_user();
1271
1272 // Add a discussion.
1273 $record = array();
1274 $record['course'] = $course->id;
1275 $record['forum'] = $forum->id;
1276 $record['userid'] = $user->id;
1277 $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1278
1279 $params = array(
1280 'context' => context_module::instance($forum->cmid),
1281 'other' => array('forumid' => $forum->id, 'forumtype' => $forum->type, 'discussionid' => $discussion->id)
1282 );
1283
1284 $this->setExpectedException('coding_exception', 'objectid must be set to the postid.');
1285 \mod_forum\event\post_deleted::create($params);
1286 }
1287
1288 /**
1289 * Ensure post_deleted event validates that the discussionid is set.
1290 */
1291 public function test_post_deleted_discussionid_validation() {
1292 $course = $this->getDataGenerator()->create_course();
1293 $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
1294 $user = $this->getDataGenerator()->create_user();
1295
1296 // Add a discussion.
1297 $record = array();
1298 $record['course'] = $course->id;
1299 $record['forum'] = $forum->id;
1300 $record['userid'] = $user->id;
1301 $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1302
1303 // Add a post.
1304 $record = array();
1305 $record['discussion'] = $discussion->id;
1306 $record['userid'] = $user->id;
1307 $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
1308
1309 $params = array(
1310 'context' => context_module::instance($forum->cmid),
1311 'objectid' => $post->id,
1312 'other' => array('forumid' => $forum->id, 'forumtype' => $forum->type)
1313 );
1314
1315 $this->setExpectedException('coding_exception', 'discussionid must be set in other.');
1316 \mod_forum\event\post_deleted::create($params);
1317 }
1318
1319 /**
1320 * Ensure post_deleted event validates that the forumid is set.
1321 */
1322 public function test_post_deleted_forumid_validation() {
1323 $course = $this->getDataGenerator()->create_course();
1324 $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
1325 $user = $this->getDataGenerator()->create_user();
1326
1327 // Add a discussion.
1328 $record = array();
1329 $record['course'] = $course->id;
1330 $record['forum'] = $forum->id;
1331 $record['userid'] = $user->id;
1332 $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1333
1334 // Add a post.
1335 $record = array();
1336 $record['discussion'] = $discussion->id;
1337 $record['userid'] = $user->id;
1338 $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
1339
1340 $params = array(
1341 'context' => context_module::instance($forum->cmid),
1342 'objectid' => $post->id,
1343 'other' => array('discussionid' => $discussion->id, 'forumtype' => $forum->type)
1344 );
1345
1346 $this->setExpectedException('coding_exception', 'forumid must be set in other.');
1347 \mod_forum\event\post_deleted::create($params);
1348 }
1349
1350 /**
1351 * Ensure post_deleted event validates that the forumtype is set.
1352 */
1353 public function test_post_deleted_forumtype_validation() {
1354 $course = $this->getDataGenerator()->create_course();
1355 $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
1356 $user = $this->getDataGenerator()->create_user();
1357
1358 // Add a discussion.
1359 $record = array();
1360 $record['course'] = $course->id;
1361 $record['forum'] = $forum->id;
1362 $record['userid'] = $user->id;
1363 $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1364
1365 // Add a post.
1366 $record = array();
1367 $record['discussion'] = $discussion->id;
1368 $record['userid'] = $user->id;
1369 $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
1370
1371 $params = array(
1372 'context' => context_module::instance($forum->cmid),
1373 'objectid' => $post->id,
1374 'other' => array('discussionid' => $discussion->id, 'forumid' => $forum->id)
1375 );
1376
1377 $this->setExpectedException('coding_exception', 'forumtype must be set in other.');
1378 \mod_forum\event\post_deleted::create($params);
1379 }
1380
1381 /**
1382 * Ensure post_deleted event validates that the contextlevel is correct.
1383 */
1384 public function test_post_deleted_context_validation() {
1385 $course = $this->getDataGenerator()->create_course();
1386 $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
1387 $user = $this->getDataGenerator()->create_user();
1388
1389 // Add a discussion.
1390 $record = array();
1391 $record['course'] = $course->id;
1392 $record['forum'] = $forum->id;
1393 $record['userid'] = $user->id;
1394 $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1395
1396 // Add a post.
1397 $record = array();
1398 $record['discussion'] = $discussion->id;
1399 $record['userid'] = $user->id;
1400 $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
1401
1402 $params = array(
1403 'context' => context_system::instance(),
1404 'objectid' => $post->id,
1405 'other' => array('discussionid' => $discussion->id, 'forumid' => $forum->id, 'forumtype' => $forum->type)
1406 );
1407
1408 $this->setExpectedException('coding_exception', 'Context passed must be module context');
1409 \mod_forum\event\post_deleted::create($params);
1410 }
1411
1412 /**
1413 * Test post_deleted event.
1414 */
1415 public function test_post_deleted() {
1416 // Setup test data.
1417 $course = $this->getDataGenerator()->create_course();
1418 $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
1419 $user = $this->getDataGenerator()->create_user();
1420
1421 // Add a discussion.
1422 $record = array();
1423 $record['course'] = $course->id;
1424 $record['forum'] = $forum->id;
1425 $record['userid'] = $user->id;
1426 $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1427
1428 // Add a post.
1429 $record = array();
1430 $record['discussion'] = $discussion->id;
1431 $record['userid'] = $user->id;
1432 $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
1433
1434 $context = context_module::instance($forum->cmid);
1435
1436 $params = array(
1437 'context' => $context,
1438 'objectid' => $post->id,
1439 'other' => array('discussionid' => $discussion->id, 'forumid' => $forum->id, 'forumtype' => $forum->type)
1440 );
1441
1442 $event = \mod_forum\event\post_deleted::create($params);
1443
1444 // Trigger and capture the event.
1445 $sink = $this->redirectEvents();
1446 $event->trigger();
1447 $events = $sink->get_events();
1448 $this->assertCount(1, $events);
1449 $event = reset($events);
1450
1451 // Checking that the event contains the expected values.
1452 $this->assertInstanceOf('\mod_forum\event\post_deleted', $event);
1453 $this->assertEquals($context, $event->get_context());
1454 $expected = array($course->id, 'forum', 'delete post', "discuss.php?d={$discussion->id}", $post->id, $forum->cmid);
1455 $this->assertEventLegacyLogData($expected, $event);
1456 $this->assertEventContextNotUsed($event);
1457
1458 $this->assertNotEmpty($event->get_name());
1459 }
1460
1461 /**
1462 * Ensure post_updated event validates that the postid is set.
1463 */
1464 public function test_post_updated_postid_validation() {
1465 $course = $this->getDataGenerator()->create_course();
1466 $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
1467 $user = $this->getDataGenerator()->create_user();
1468
1469 // Add a discussion.
1470 $record = array();
1471 $record['course'] = $course->id;
1472 $record['forum'] = $forum->id;
1473 $record['userid'] = $user->id;
1474 $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1475
1476 $params = array(
1477 'context' => context_module::instance($forum->cmid),
1478 'other' => array('forumid' => $forum->id, 'forumtype' => $forum->type, 'discussionid' => $discussion->id)
1479 );
1480
1481 $this->setExpectedException('coding_exception', 'objectid must be set to the postid.');
1482 \mod_forum\event\post_updated::create($params);
1483 }
1484
1485 /**
1486 * Ensure post_updated event validates that the discussionid is set.
1487 */
1488 public function test_post_updated_discussionid_validation() {
1489 $course = $this->getDataGenerator()->create_course();
1490 $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
1491 $user = $this->getDataGenerator()->create_user();
1492
1493 // Add a discussion.
1494 $record = array();
1495 $record['course'] = $course->id;
1496 $record['forum'] = $forum->id;
1497 $record['userid'] = $user->id;
1498 $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1499
1500 // Add a post.
1501 $record = array();
1502 $record['discussion'] = $discussion->id;
1503 $record['userid'] = $user->id;
1504 $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
1505
1506 $params = array(
1507 'context' => context_module::instance($forum->cmid),
1508 'objectid' => $post->id,
1509 'other' => array('forumid' => $forum->id, 'forumtype' => $forum->type)
1510 );
1511
1512 $this->setExpectedException('coding_exception', 'discussionid must be set in other.');
1513 \mod_forum\event\post_updated::create($params);
1514 }
1515
1516 /**
1517 * Ensure post_updated event validates that the forumid is set.
1518 */
1519 public function test_post_updated_forumid_validation() {
1520 $course = $this->getDataGenerator()->create_course();
1521 $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
1522 $user = $this->getDataGenerator()->create_user();
1523
1524 // Add a discussion.
1525 $record = array();
1526 $record['course'] = $course->id;
1527 $record['forum'] = $forum->id;
1528 $record['userid'] = $user->id;
1529 $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1530
1531 // Add a post.
1532 $record = array();
1533 $record['discussion'] = $discussion->id;
1534 $record['userid'] = $user->id;
1535 $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
1536
1537 $params = array(
1538 'context' => context_module::instance($forum->cmid),
1539 'objectid' => $post->id,
1540 'other' => array('discussionid' => $discussion->id, 'forumtype' => $forum->type)
1541 );
1542
1543 $this->setExpectedException('coding_exception', 'forumid must be set in other.');
1544 \mod_forum\event\post_updated::create($params);
1545 }
1546
1547 /**
1548 * Ensure post_updated event validates that the forumtype is set.
1549 */
1550 public function test_post_updated_forumtype_validation() {
1551 $course = $this->getDataGenerator()->create_course();
1552 $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
1553 $user = $this->getDataGenerator()->create_user();
1554
1555 // Add a discussion.
1556 $record = array();
1557 $record['course'] = $course->id;
1558 $record['forum'] = $forum->id;
1559 $record['userid'] = $user->id;
1560 $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1561
1562 // Add a post.
1563 $record = array();
1564 $record['discussion'] = $discussion->id;
1565 $record['userid'] = $user->id;
1566 $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
1567
1568 $params = array(
1569 'context' => context_module::instance($forum->cmid),
1570 'objectid' => $post->id,
1571 'other' => array('discussionid' => $discussion->id, 'forumid' => $forum->id)
1572 );
1573
1574 $this->setExpectedException('coding_exception', 'forumtype must be set in other.');
1575 \mod_forum\event\post_updated::create($params);
1576 }
1577
1578 /**
1579 * Ensure post_updated event validates that the contextlevel is correct.
1580 */
1581 public function test_post_updated_context_validation() {
1582 $course = $this->getDataGenerator()->create_course();
1583 $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
1584 $user = $this->getDataGenerator()->create_user();
1585
1586 // Add a discussion.
1587 $record = array();
1588 $record['course'] = $course->id;
1589 $record['forum'] = $forum->id;
1590 $record['userid'] = $user->id;
1591 $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1592
1593 // Add a post.
1594 $record = array();
1595 $record['discussion'] = $discussion->id;
1596 $record['userid'] = $user->id;
1597 $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
1598
1599 $params = array(
1600 'context' => context_system::instance(),
1601 'objectid' => $post->id,
1602 'other' => array('discussionid' => $discussion->id, 'forumid' => $forum->id, 'forumtype' => $forum->type)
1603 );
1604
1605 $this->setExpectedException('coding_exception', 'Context passed must be module context');
1606 \mod_forum\event\post_updated::create($params);
1607 }
1608
1609 /**
1610 * Test post_updated event.
1611 */
1612 public function test_post_updated() {
1613 // Setup test data.
1614 $course = $this->getDataGenerator()->create_course();
1615 $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
1616 $user = $this->getDataGenerator()->create_user();
1617
1618 // Add a discussion.
1619 $record = array();
1620 $record['course'] = $course->id;
1621 $record['forum'] = $forum->id;
1622 $record['userid'] = $user->id;
1623 $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1624
1625 // Add a post.
1626 $record = array();
1627 $record['discussion'] = $discussion->id;
1628 $record['userid'] = $user->id;
1629 $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
1630
1631 $context = context_module::instance($forum->cmid);
1632
1633 $params = array(
1634 'context' => $context,
1635 'objectid' => $post->id,
1636 'other' => array('discussionid' => $discussion->id, 'forumid' => $forum->id, 'forumtype' => $forum->type)
1637 );
1638
1639 $event = \mod_forum\event\post_updated::create($params);
1640
1641 // Trigger and capturing the event.
1642 $sink = $this->redirectEvents();
1643 $event->trigger();
1644 $events = $sink->get_events();
1645 $this->assertCount(1, $events);
1646 $event = reset($events);
1647
1648 // Checking that the event contains the expected values.
1649 $this->assertInstanceOf('\mod_forum\event\post_updated', $event);
1650 $this->assertEquals($context, $event->get_context());
1651 $expected = array($course->id, 'forum', 'update post', "discuss.php?d={$discussion->id}#p{$post->id}",
1652 $post->id, $forum->cmid);
1653 $this->assertEventLegacyLogData($expected, $event);
1654 $this->assertEventContextNotUsed($event);
1655
1656 $this->assertNotEmpty($event->get_name());
1657 }
1658}