MDL-44928 mod_forum: Fix urls in log entries for forum events
[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
97802bea
DP
204 /**
205 * Ensure discussion_updated event validates that forumid is set.
206 */
207 public function test_discussion_updated_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_updated::create($params);
218 }
219
220 /**
221 * Ensure discussion_updated event validates that discussionid is set.
222 */
223 public function test_discussion_updated_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_updated::create($params);
235 }
236
237 /**
238 * Ensure discussion_created event validates that the context is the correct level.
239 */
240 public function test_discussion_updated_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_updated::create($params);
251 }
252
253 /**
254 * Test discussion_created event.
255 */
256 public function test_discussion_updated() {
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 // Create the event.
279 $event = \mod_forum\event\discussion_updated::create($params);
280
281 // Trigger and capturing the event.
282 $sink = $this->redirectEvents();
283 $event->trigger();
284 $events = $sink->get_events();
285 $this->assertCount(1, $events);
286 $event = reset($events);
287
288 // Check that the event contains the expected values.
289 $this->assertInstanceOf('\mod_forum\event\discussion_updated', $event);
290 $this->assertEquals($context, $event->get_context());
291 $this->assertEventContextNotUsed($event);
292
293 $this->assertNotEmpty($event->get_name());
294 }
295
a52c04d5
DP
296 /**
297 * Ensure discussion_deleted event validates that forumid is set.
298 */
299 public function test_discussion_deleted_forumid_validation() {
300 $course = $this->getDataGenerator()->create_course();
301 $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
302 $context = context_module::instance($forum->cmid);
303
304 $params = array(
305 'context' => $context,
306 );
307
308 $this->setExpectedException('coding_exception', 'forumid must be set in $other.');
309 \mod_forum\event\discussion_deleted::create($params);
310 }
311
312 /**
313 * Ensure discussion_deleted event validates that discussionid is set.
314 */
315 public function test_discussion_deleted_objectid_validation() {
316 $course = $this->getDataGenerator()->create_course();
317 $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
318 $context = context_module::instance($forum->cmid);
319
320 $params = array(
321 'context' => $context,
322 'other' => array('forumid' => $forum->id)
323 );
324
325 $this->setExpectedException('coding_exception', 'objectid must be set to the discussionid.');
326 \mod_forum\event\discussion_deleted::create($params);
327 }
328
329 /**
330 * Ensure discussion_deleted event validates that context is of the correct level.
331 */
332 public function test_discussion_deleted_context_validation() {
333 $course = $this->getDataGenerator()->create_course();
334 $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
335
336 $params = array(
337 'context' => context_system::instance(),
338 'other' => array('forumid' => $forum->id),
339 );
340
341 $this->setExpectedException('coding_exception', 'Context passed must be module context.');
342 \mod_forum\event\discussion_deleted::create($params);
343 }
344
345 /**
346 * Test discussion_deleted event.
347 */
348 public function test_discussion_deleted() {
349
350 // Setup test data.
351 $course = $this->getDataGenerator()->create_course();
352 $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
353 $user = $this->getDataGenerator()->create_user();
354
355 // Add a discussion.
356 $record = array();
357 $record['course'] = $course->id;
358 $record['forum'] = $forum->id;
359 $record['userid'] = $user->id;
360 $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
361
362 $context = context_module::instance($forum->cmid);
363
364 $params = array(
365 'context' => $context,
366 'objectid' => $discussion->id,
367 'other' => array('forumid' => $forum->id),
368 );
369
370 $event = \mod_forum\event\discussion_deleted::create($params);
371
372 // Trigger and capture the event.
373 $sink = $this->redirectEvents();
374 $event->trigger();
375 $events = $sink->get_events();
376 $this->assertCount(1, $events);
377 $event = reset($events);
378
379 // Checking that the event contains the expected values.
380 $this->assertInstanceOf('\mod_forum\event\discussion_deleted', $event);
381 $this->assertEquals($context, $event->get_context());
382 $expected = array($course->id, 'forum', 'delete discussion', "view.php?id={$forum->cmid}", $forum->id, $forum->cmid);
383 $this->assertEventLegacyLogData($expected, $event);
384 $this->assertEventContextNotUsed($event);
385
386 $this->assertNotEmpty($event->get_name());
387 }
388
389 /**
390 * Ensure discussion_moved event validates that fromforumid is set.
391 */
392 public function test_discussion_moved_fromforumid_validation() {
393 $course = $this->getDataGenerator()->create_course();
394 $toforum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
395
396 $context = context_module::instance($toforum->cmid);
397
398 $params = array(
399 'context' => $context,
400 'other' => array('toforumid' => $toforum->id)
401 );
402
403 $this->setExpectedException('coding_exception', 'fromforumid must be set in $other.');
404 \mod_forum\event\discussion_moved::create($params);
405 }
406
407 /**
408 * Ensure discussion_moved event validates that toforumid is set.
409 */
410 public function test_discussion_moved_toforumid_validation() {
411 $course = $this->getDataGenerator()->create_course();
412 $fromforum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
413 $toforum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
414 $context = context_module::instance($toforum->cmid);
415
416 $params = array(
417 'context' => $context,
418 'other' => array('fromforumid' => $fromforum->id)
419 );
420
421 $this->setExpectedException('coding_exception', 'toforumid must be set in $other.');
422 \mod_forum\event\discussion_moved::create($params);
423 }
424
425 /**
426 * Ensure discussion_moved event validates that the discussionid is set.
427 */
428 public function test_discussion_moved_objectid_validation() {
429 $course = $this->getDataGenerator()->create_course();
430 $fromforum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
431 $toforum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
432 $context = context_module::instance($toforum->cmid);
433
434 $params = array(
435 'context' => $context,
436 'other' => array('fromforumid' => $fromforum->id, 'toforumid' => $toforum->id)
437 );
438
439 $this->setExpectedException('coding_exception', 'objectid must be set to the discussionid.');
440 \mod_forum\event\discussion_moved::create($params);
441 }
442
443 /**
444 * Ensure discussion_moved event validates that the context level is correct.
445 */
446 public function test_discussion_moved_context_validation() {
447 $course = $this->getDataGenerator()->create_course();
448 $fromforum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
449 $toforum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
450 $user = $this->getDataGenerator()->create_user();
451
452 // Add a discussion.
453 $record = array();
454 $record['course'] = $course->id;
455 $record['forum'] = $fromforum->id;
456 $record['userid'] = $user->id;
457 $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
458
459 $params = array(
460 'context' => context_system::instance(),
461 'objectid' => $discussion->id,
462 'other' => array('fromforumid' => $fromforum->id, 'toforumid' => $toforum->id)
463 );
464
465 $this->setExpectedException('coding_exception', 'Context passed must be module context.');
466 \mod_forum\event\discussion_moved::create($params);
467 }
468
469 /**
470 * Test discussion_moved event.
471 */
472 public function test_discussion_moved() {
473 // Setup test data.
474 $course = $this->getDataGenerator()->create_course();
475 $fromforum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
476 $toforum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
477 $user = $this->getDataGenerator()->create_user();
478
479 // Add a discussion.
480 $record = array();
481 $record['course'] = $course->id;
482 $record['forum'] = $fromforum->id;
483 $record['userid'] = $user->id;
484 $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
485
486 $context = context_module::instance($toforum->cmid);
487
488 $params = array(
489 'context' => $context,
490 'objectid' => $discussion->id,
491 'other' => array('fromforumid' => $fromforum->id, 'toforumid' => $toforum->id)
492 );
493
494 $event = \mod_forum\event\discussion_moved::create($params);
495
496 // Trigger and capture the event.
497 $sink = $this->redirectEvents();
498 $event->trigger();
499 $events = $sink->get_events();
500 $this->assertCount(1, $events);
501 $event = reset($events);
502
503 // Checking that the event contains the expected values.
504 $this->assertInstanceOf('\mod_forum\event\discussion_moved', $event);
505 $this->assertEquals($context, $event->get_context());
506 $expected = array($course->id, 'forum', 'move discussion', "discuss.php?d={$discussion->id}",
507 $discussion->id, $toforum->cmid);
508 $this->assertEventLegacyLogData($expected, $event);
509 $this->assertEventContextNotUsed($event);
510
511 $this->assertNotEmpty($event->get_name());
512 }
513
514 /**
515 * Ensure discussion_viewed event validates that the discussionid is set
516 */
517 public function test_discussion_viewed_objectid_validation() {
518 $course = $this->getDataGenerator()->create_course();
519 $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
520 $context = context_module::instance($forum->cmid);
521
522 $params = array(
523 'context' => $context,
524 );
525
526 $this->setExpectedException('coding_exception', 'objectid must be set to the discussionid.');
527 \mod_forum\event\discussion_viewed::create($params);
528 }
529
530 /**
531 * Ensure discussion_viewed event validates that the contextlevel is correct.
532 */
533 public function test_discussion_viewed_context_validation() {
534 $course = $this->getDataGenerator()->create_course();
535 $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
536 $user = $this->getDataGenerator()->create_user();
537
538 // Add a discussion.
539 $record = array();
540 $record['course'] = $course->id;
541 $record['forum'] = $forum->id;
542 $record['userid'] = $user->id;
543 $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
544
545 $params = array(
546 'context' => context_system::instance(),
547 'objectid' => $discussion->id,
548 );
549
550 $this->setExpectedException('coding_exception', 'Context passed must be module context.');
551 \mod_forum\event\discussion_viewed::create($params);
552 }
553
554 /**
555 * Test discussion_viewed event.
556 */
557 public function test_discussion_viewed() {
558 // Setup test data.
559 $course = $this->getDataGenerator()->create_course();
560 $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
561 $user = $this->getDataGenerator()->create_user();
562
563 // Add a discussion.
564 $record = array();
565 $record['course'] = $course->id;
566 $record['forum'] = $forum->id;
567 $record['userid'] = $user->id;
568 $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
569
570 $context = context_module::instance($forum->cmid);
571
572 $params = array(
573 'context' => $context,
574 'objectid' => $discussion->id,
575 );
576
577 $event = \mod_forum\event\discussion_viewed::create($params);
578
579 // Trigger and capture the event.
580 $sink = $this->redirectEvents();
581 $event->trigger();
582 $events = $sink->get_events();
583 $this->assertCount(1, $events);
584 $event = reset($events);
585
586 // Checking that the event contains the expected values.
587 $this->assertInstanceOf('\mod_forum\event\discussion_viewed', $event);
588 $this->assertEquals($context, $event->get_context());
589 $expected = array($course->id, 'forum', 'view discussion', "discuss.php?d={$discussion->id}",
590 $discussion->id, $forum->cmid);
591 $this->assertEventLegacyLogData($expected, $event);
592 $this->assertEventContextNotUsed($event);
593
594 $this->assertNotEmpty($event->get_name());
595 }
596
597 /**
fbc4b778 598 * Ensure course_module_viewed event validates that the forumid is set.
a52c04d5 599 */
fbc4b778 600 public function test_course_module_viewed_objectid_validation() {
a52c04d5
DP
601 $course = $this->getDataGenerator()->create_course();
602 $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
603 $context = context_module::instance($forum->cmid);
604
605 $params = array(
606 'context' => $context,
607 );
608
fbc4b778
MG
609 $this->setExpectedException('coding_exception', 'must define objectid');
610 \mod_forum\event\course_module_viewed::create($params);
a52c04d5
DP
611 }
612
613 /**
fbc4b778 614 * Ensure course_module_viewed event validates that the contextlevel is correct.
a52c04d5 615 */
fbc4b778 616 public function test_course_module_viewed_context_validation() {
a52c04d5
DP
617 $course = $this->getDataGenerator()->create_course();
618 $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
619
620 $params = array(
621 'context' => context_system::instance(),
622 'objectid' => $forum->id,
623 );
624
625 $this->setExpectedException('coding_exception', 'Context passed must be module context.');
fbc4b778 626 \mod_forum\event\course_module_viewed::create($params);
a52c04d5
DP
627 }
628
629 /**
fbc4b778 630 * Test the course_module_viewed event.
a52c04d5 631 */
fbc4b778 632 public function test_course_module_viewed() {
a52c04d5
DP
633 // Setup test data.
634 $course = $this->getDataGenerator()->create_course();
635 $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
636
637 $context = context_module::instance($forum->cmid);
638
639 $params = array(
640 'context' => $context,
641 'objectid' => $forum->id,
642 );
643
fbc4b778 644 $event = \mod_forum\event\course_module_viewed::create($params);
a52c04d5
DP
645
646 // Trigger and capture the event.
647 $sink = $this->redirectEvents();
648 $event->trigger();
649 $events = $sink->get_events();
650 $this->assertCount(1, $events);
651 $event = reset($events);
652
653 // Checking that the event contains the expected values.
fbc4b778 654 $this->assertInstanceOf('\mod_forum\event\course_module_viewed', $event);
a52c04d5
DP
655 $this->assertEquals($context, $event->get_context());
656 $expected = array($course->id, 'forum', 'view forum', "view.php?f={$forum->id}", $forum->id, $forum->cmid);
657 $this->assertEventLegacyLogData($expected, $event);
658 $this->assertEventContextNotUsed($event);
659
660 $this->assertNotEmpty($event->get_name());
661 }
662
663 /**
664 * Ensure subscription_created event validates that the forumid is set.
665 */
666 public function test_subscription_created_forumid_validation() {
667 $user = $this->getDataGenerator()->create_user();
668 $course = $this->getDataGenerator()->create_course();
669 $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
670
671 $params = array(
672 'context' => context_module::instance($forum->cmid),
673 'relateduserid' => $user->id,
674 );
675
676 $this->setExpectedException('coding_exception', 'forumid must be set in other.');
677 \mod_forum\event\subscription_created::create($params);
678 }
679
680 /**
681 * Ensure subscription_created event validates that the relateduserid is set.
682 */
683 public function test_subscription_created_relateduserid_validation() {
684 $course = $this->getDataGenerator()->create_course();
685 $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
686
687 $params = array(
688 'context' => context_module::instance($forum->cmid),
689 'objectid' => $forum->id,
690 );
691
692 $this->setExpectedException('coding_exception', 'relateduserid must be set.');
693 \mod_forum\event\subscription_created::create($params);
694 }
695
696 /**
697 * Ensure subscription_created event validates that the contextlevel is correct.
698 */
699 public function test_subscription_created_contextlevel_validation() {
700 $user = $this->getDataGenerator()->create_user();
701 $course = $this->getDataGenerator()->create_course();
702 $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
703
704 $params = array(
705 'context' => context_system::instance(),
706 'other' => array('forumid' => $forum->id),
707 'relateduserid' => $user->id,
708 );
709
710 $this->setExpectedException('coding_exception', 'Context passed must be module context.');
711 \mod_forum\event\subscription_created::create($params);
712 }
713
714 /**
715 * Test the subscription_created event.
716 */
717 public function test_subscription_created() {
718 // Setup test data.
719 $user = $this->getDataGenerator()->create_user();
720 $course = $this->getDataGenerator()->create_course();
721 $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
722 $context = context_module::instance($forum->cmid);
723
724 $params = array(
725 'context' => $context,
726 'other' => array('forumid' => $forum->id),
727 'relateduserid' => $user->id,
728 );
729
730 $event = \mod_forum\event\subscription_created::create($params);
731
732 // Trigger and capturing the event.
733 $sink = $this->redirectEvents();
734 $event->trigger();
735 $events = $sink->get_events();
736 $this->assertCount(1, $events);
737 $event = reset($events);
738
739 // Checking that the event contains the expected values.
740 $this->assertInstanceOf('\mod_forum\event\subscription_created', $event);
741 $this->assertEquals($context, $event->get_context());
742 $expected = array($course->id, 'forum', 'subscribe', "view.php?f={$forum->id}", $forum->id, $forum->cmid);
743 $this->assertEventLegacyLogData($expected, $event);
744 $this->assertEventContextNotUsed($event);
745
746 $this->assertNotEmpty($event->get_name());
747 }
748
749 /**
750 * Ensure subscription_deleted event validates that the forumid is set.
751 */
752 public function test_subscription_deleted_forumid_validation() {
753 $user = $this->getDataGenerator()->create_user();
754 $course = $this->getDataGenerator()->create_course();
755 $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
756
757 $params = array(
758 'context' => context_module::instance($forum->cmid),
759 'relateduserid' => $user->id,
760 );
761
762 $this->setExpectedException('coding_exception', 'forumid must be set in other.');
763 \mod_forum\event\subscription_deleted::create($params);
764 }
765
766 /**
767 * Ensure subscription_deleted event validates that the relateduserid is set.
768 */
769 public function test_subscription_deleted_relateduserid_validation() {
770 $course = $this->getDataGenerator()->create_course();
771 $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
772
773 $params = array(
774 'context' => context_module::instance($forum->cmid),
775 'objectid' => $forum->id,
776 );
777
778 $this->setExpectedException('coding_exception', 'relateduserid must be set.');
779 \mod_forum\event\subscription_deleted::create($params);
780 }
781
782 /**
783 * Ensure subscription_deleted event validates that the contextlevel is correct.
784 */
785 public function test_subscription_deleted_contextlevel_validation() {
786 $user = $this->getDataGenerator()->create_user();
787 $course = $this->getDataGenerator()->create_course();
788 $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
789
790 $params = array(
791 'context' => context_system::instance(),
792 'other' => array('forumid' => $forum->id),
793 'relateduserid' => $user->id,
794 );
795
796 $this->setExpectedException('coding_exception', 'Context passed must be module context.');
797 \mod_forum\event\subscription_deleted::create($params);
798 }
799
800 /**
801 * Test the subscription_deleted event.
802 */
803 public function test_subscription_deleted() {
804 // Setup test data.
805 $user = $this->getDataGenerator()->create_user();
806 $course = $this->getDataGenerator()->create_course();
807 $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
808 $context = context_module::instance($forum->cmid);
809
810 $params = array(
811 'context' => $context,
812 'other' => array('forumid' => $forum->id),
813 'relateduserid' => $user->id,
814 );
815
816 $event = \mod_forum\event\subscription_deleted::create($params);
817
818 // Trigger and capturing the event.
819 $sink = $this->redirectEvents();
820 $event->trigger();
821 $events = $sink->get_events();
822 $this->assertCount(1, $events);
823 $event = reset($events);
824
825 // Checking that the event contains the expected values.
826 $this->assertInstanceOf('\mod_forum\event\subscription_deleted', $event);
827 $this->assertEquals($context, $event->get_context());
828 $expected = array($course->id, 'forum', 'unsubscribe', "view.php?f={$forum->id}", $forum->id, $forum->cmid);
829 $this->assertEventLegacyLogData($expected, $event);
830 $this->assertEventContextNotUsed($event);
831
832 $this->assertNotEmpty($event->get_name());
833 }
834
835 /**
836 * Ensure readtracking_enabled event validates that the forumid is set.
837 */
838 public function test_readtracking_enabled_forumid_validation() {
839 $user = $this->getDataGenerator()->create_user();
840 $course = $this->getDataGenerator()->create_course();
841 $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
842
843 $params = array(
844 'context' => context_module::instance($forum->cmid),
845 'relateduserid' => $user->id,
846 );
847
848 $this->setExpectedException('coding_exception', 'forumid must be set in other.');
849 \mod_forum\event\readtracking_enabled::create($params);
850 }
851
852 /**
853 * Ensure readtracking_enabled event validates that the relateduserid is set.
854 */
855 public function test_readtracking_enabled_relateduserid_validation() {
856 $course = $this->getDataGenerator()->create_course();
857 $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
858
859 $params = array(
860 'context' => context_module::instance($forum->cmid),
861 'objectid' => $forum->id,
862 );
863
864 $this->setExpectedException('coding_exception', 'relateduserid must be set.');
865 \mod_forum\event\readtracking_enabled::create($params);
866 }
867
868 /**
869 * Ensure readtracking_enabled event validates that the contextlevel is correct.
870 */
871 public function test_readtracking_enabled_contextlevel_validation() {
872 $user = $this->getDataGenerator()->create_user();
873 $course = $this->getDataGenerator()->create_course();
874 $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
875
876 $params = array(
877 'context' => context_system::instance(),
878 'other' => array('forumid' => $forum->id),
879 'relateduserid' => $user->id,
880 );
881
882 $this->setExpectedException('coding_exception', 'Context passed must be module context.');
883 \mod_forum\event\readtracking_enabled::create($params);
884 }
885
886 /**
887 * Test the readtracking_enabled event.
888 */
889 public function test_readtracking_enabled() {
890 // Setup test data.
891 $user = $this->getDataGenerator()->create_user();
892 $course = $this->getDataGenerator()->create_course();
893 $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
894 $context = context_module::instance($forum->cmid);
895
896 $params = array(
897 'context' => $context,
898 'other' => array('forumid' => $forum->id),
899 'relateduserid' => $user->id,
900 );
901
902 $event = \mod_forum\event\readtracking_enabled::create($params);
903
904 // Trigger and capture the event.
905 $sink = $this->redirectEvents();
906 $event->trigger();
907 $events = $sink->get_events();
908 $this->assertCount(1, $events);
909 $event = reset($events);
910
911 // Checking that the event contains the expected values.
912 $this->assertInstanceOf('\mod_forum\event\readtracking_enabled', $event);
913 $this->assertEquals($context, $event->get_context());
914 $expected = array($course->id, 'forum', 'start tracking', "view.php?f={$forum->id}", $forum->id, $forum->cmid);
915 $this->assertEventLegacyLogData($expected, $event);
916 $this->assertEventContextNotUsed($event);
917
918 $this->assertNotEmpty($event->get_name());
919 }
920
921 /**
922 * Ensure readtracking_disabled event validates that the forumid is set.
923 */
924 public function test_readtracking_disabled_forumid_validation() {
925 $user = $this->getDataGenerator()->create_user();
926 $course = $this->getDataGenerator()->create_course();
927 $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
928
929 $params = array(
930 'context' => context_module::instance($forum->cmid),
931 'relateduserid' => $user->id,
932 );
933
934 $this->setExpectedException('coding_exception', 'forumid must be set in other.');
935 \mod_forum\event\readtracking_disabled::create($params);
936 }
937
938 /**
939 * Ensure readtracking_disabled event validates that the relateduserid is set.
940 */
941 public function test_readtracking_disabled_relateduserid_validation() {
942 $course = $this->getDataGenerator()->create_course();
943 $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
944
945 $params = array(
946 'context' => context_module::instance($forum->cmid),
947 'objectid' => $forum->id,
948 );
949
950 $this->setExpectedException('coding_exception', 'relateduserid must be set.');
951 \mod_forum\event\readtracking_disabled::create($params);
952 }
953
954 /**
955 * Ensure readtracking_disabled event validates that the contextlevel is correct
956 */
957 public function test_readtracking_disabled_contextlevel_validation() {
958 $user = $this->getDataGenerator()->create_user();
959 $course = $this->getDataGenerator()->create_course();
960 $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
961
962 $params = array(
963 'context' => context_system::instance(),
964 'other' => array('forumid' => $forum->id),
965 'relateduserid' => $user->id,
966 );
967
968 $this->setExpectedException('coding_exception', 'Context passed must be module context.');
969 \mod_forum\event\readtracking_disabled::create($params);
970 }
971
972 /**
973 * Test the readtracking_disabled event.
974 */
975 public function test_readtracking_disabled() {
976 // Setup test data.
977 $user = $this->getDataGenerator()->create_user();
978 $course = $this->getDataGenerator()->create_course();
979 $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
980 $context = context_module::instance($forum->cmid);
981
982 $params = array(
983 'context' => $context,
984 'other' => array('forumid' => $forum->id),
985 'relateduserid' => $user->id,
986 );
987
988 $event = \mod_forum\event\readtracking_disabled::create($params);
989
990 // Trigger and capture the event.
991 $sink = $this->redirectEvents();
992 $event->trigger();
993 $events = $sink->get_events();
994 $this->assertCount(1, $events);
995 $event = reset($events);
996
997 // Checking that the event contains the expected values.
998 $this->assertInstanceOf('\mod_forum\event\readtracking_disabled', $event);
999 $this->assertEquals($context, $event->get_context());
1000 $expected = array($course->id, 'forum', 'stop tracking', "view.php?f={$forum->id}", $forum->id, $forum->cmid);
1001 $this->assertEventLegacyLogData($expected, $event);
1002 $this->assertEventContextNotUsed($event);
1003
1004 $this->assertNotEmpty($event->get_name());
1005 }
1006
1007 /**
1008 * Ensure subscribers_viewed event validates that the forumid is set.
1009 */
1010 public function test_subscribers_viewed_forumid_validation() {
1011 $user = $this->getDataGenerator()->create_user();
1012 $course = $this->getDataGenerator()->create_course();
1013 $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
1014
1015 $params = array(
1016 'context' => context_module::instance($forum->cmid),
1017 'relateduserid' => $user->id,
1018 );
1019
1020 $this->setExpectedException('coding_exception', 'forumid must be set in other.');
1021 \mod_forum\event\subscribers_viewed::create($params);
1022 }
1023
1024 /**
1025 * Ensure subscribers_viewed event validates that the contextlevel is correct.
1026 */
1027 public function test_subscribers_viewed_contextlevel_validation() {
1028 $user = $this->getDataGenerator()->create_user();
1029 $course = $this->getDataGenerator()->create_course();
1030 $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
1031
1032 $params = array(
1033 'context' => context_system::instance(),
1034 'other' => array('forumid' => $forum->id),
1035 'relateduserid' => $user->id,
1036 );
1037
1038 $this->setExpectedException('coding_exception', 'Context passed must be module context.');
1039 \mod_forum\event\subscribers_viewed::create($params);
1040 }
1041
1042 /**
1043 * Test the subscribers_viewed event.
1044 */
1045 public function test_subscribers_viewed() {
1046 // Setup test data.
1047 $course = $this->getDataGenerator()->create_course();
1048 $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
1049 $context = context_module::instance($forum->cmid);
1050
1051 $params = array(
1052 'context' => $context,
1053 'other' => array('forumid' => $forum->id),
1054 );
1055
1056 $event = \mod_forum\event\subscribers_viewed::create($params);
1057
1058 // Trigger and capture the event.
1059 $sink = $this->redirectEvents();
1060 $event->trigger();
1061 $events = $sink->get_events();
1062 $this->assertCount(1, $events);
1063 $event = reset($events);
1064
1065 // Checking that the event contains the expected values.
1066 $this->assertInstanceOf('\mod_forum\event\subscribers_viewed', $event);
1067 $this->assertEquals($context, $event->get_context());
1068 $expected = array($course->id, 'forum', 'view subscribers', "subscribers.php?id={$forum->id}", $forum->id, $forum->cmid);
1069 $this->assertEventLegacyLogData($expected, $event);
1070 $this->assertEventContextNotUsed($event);
1071
1072 $this->assertNotEmpty($event->get_name());
1073 }
1074
1075 /**
1076 * Ensure userreport_viewed event validates that the reportmode is set.
1077 */
1078 public function test_userreport_viewed_reportmode_validation() {
1079 $user = $this->getDataGenerator()->create_user();
1080 $course = $this->getDataGenerator()->create_course();
1081
1082 $params = array(
1083 'context' => context_course::instance($course->id),
1084 'relateduserid' => $user->id,
1085 );
1086
1087 $this->setExpectedException('coding_exception', 'reportmode must be set in other.');
1088 \mod_forum\event\userreport_viewed::create($params);
1089 }
1090
1091 /**
1092 * Ensure userreport_viewed event validates that the contextlevel is correct.
1093 */
1094 public function test_userreport_viewed_contextlevel_validation() {
1095 $user = $this->getDataGenerator()->create_user();
1096 $course = $this->getDataGenerator()->create_course();
1097 $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
1098
1099 $params = array(
1100 'context' => context_module::instance($forum->id),
1101 'other' => array('reportmode' => 'posts'),
1102 'relateduserid' => $user->id,
1103 );
1104
1105 $this->setExpectedException('coding_exception', 'Context passed must be system or course.');
1106 \mod_forum\event\userreport_viewed::create($params);
1107 }
1108
1109 /**
1110 * Ensure userreport_viewed event validates that the relateduserid is set.
1111 */
1112 public function test_userreport_viewed_relateduserid_validation() {
1113
1114 $params = array(
1115 'context' => context_system::instance(),
1116 'other' => array('reportmode' => 'posts'),
1117 );
1118
1119 $this->setExpectedException('coding_exception', 'relateduserid must be set.');
1120 \mod_forum\event\userreport_viewed::create($params);
1121 }
1122
1123 /**
1124 * Test the userreport_viewed event.
1125 */
1126 public function test_userreport_viewed() {
1127 // Setup test data.
1128 $user = $this->getDataGenerator()->create_user();
1129 $course = $this->getDataGenerator()->create_course();
1130 $context = context_course::instance($course->id);
1131
1132 $params = array(
1133 'context' => $context,
1134 'relateduserid' => $user->id,
1135 'other' => array('reportmode' => 'discussions'),
1136 );
1137
1138 $event = \mod_forum\event\userreport_viewed::create($params);
1139
1140 // Trigger and capture the event.
1141 $sink = $this->redirectEvents();
1142 $event->trigger();
1143 $events = $sink->get_events();
1144 $this->assertCount(1, $events);
1145 $event = reset($events);
1146
1147 // Checking that the event contains the expected values.
1148 $this->assertInstanceOf('\mod_forum\event\userreport_viewed', $event);
1149 $this->assertEquals($context, $event->get_context());
1150 $expected = array($course->id, 'forum', 'user report',
1151 "user.php?id={$user->id}&amp;mode=discussions&amp;course={$course->id}", $user->id);
1152 $this->assertEventLegacyLogData($expected, $event);
1153 $this->assertEventContextNotUsed($event);
1154
1155 $this->assertNotEmpty($event->get_name());
1156 }
1157
1158 /**
1159 * Ensure post_created event validates that the postid is set.
1160 */
1161 public function test_post_created_postid_validation() {
1162 $course = $this->getDataGenerator()->create_course();
1163 $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
1164 $user = $this->getDataGenerator()->create_user();
1165
1166 // Add a discussion.
1167 $record = array();
1168 $record['course'] = $course->id;
1169 $record['forum'] = $forum->id;
1170 $record['userid'] = $user->id;
1171 $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1172
1173 $params = array(
1174 'context' => context_module::instance($forum->cmid),
1175 'other' => array('forumid' => $forum->id, 'forumtype' => $forum->type, 'discussionid' => $discussion->id)
1176 );
1177
1178 $this->setExpectedException('coding_exception', 'objectid must be set to the postid.');
1179 \mod_forum\event\post_created::create($params);
1180 }
1181
1182 /**
1183 * Ensure post_created event validates that the discussionid is set.
1184 */
1185 public function test_post_created_discussionid_validation() {
1186 $course = $this->getDataGenerator()->create_course();
1187 $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
1188 $user = $this->getDataGenerator()->create_user();
1189
1190 // Add a discussion.
1191 $record = array();
1192 $record['course'] = $course->id;
1193 $record['forum'] = $forum->id;
1194 $record['userid'] = $user->id;
1195 $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1196
1197 // Add a post.
1198 $record = array();
1199 $record['discussion'] = $discussion->id;
1200 $record['userid'] = $user->id;
1201 $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
1202
1203 $params = array(
1204 'context' => context_module::instance($forum->cmid),
1205 'objectid' => $post->id,
1206 'other' => array('forumid' => $forum->id, 'forumtype' => $forum->type)
1207 );
1208
1209 $this->setExpectedException('coding_exception', 'discussionid must be set in other.');
1210 \mod_forum\event\post_created::create($params);
1211 }
1212
1213 /**
1214 * Ensure post_created event validates that the forumid is set.
1215 */
1216 public function test_post_created_forumid_validation() {
1217 $course = $this->getDataGenerator()->create_course();
1218 $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
1219 $user = $this->getDataGenerator()->create_user();
1220
1221 // Add a discussion.
1222 $record = array();
1223 $record['course'] = $course->id;
1224 $record['forum'] = $forum->id;
1225 $record['userid'] = $user->id;
1226 $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1227
1228 // Add a post.
1229 $record = array();
1230 $record['discussion'] = $discussion->id;
1231 $record['userid'] = $user->id;
1232 $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
1233
1234 $params = array(
1235 'context' => context_module::instance($forum->cmid),
1236 'objectid' => $post->id,
1237 'other' => array('discussionid' => $discussion->id, 'forumtype' => $forum->type)
1238 );
1239
1240 $this->setExpectedException('coding_exception', 'forumid must be set in other.');
1241 \mod_forum\event\post_created::create($params);
1242 }
1243
1244 /**
1245 * Ensure post_created event validates that the forumtype is set.
1246 */
1247 public function test_post_created_forumtype_validation() {
1248 $course = $this->getDataGenerator()->create_course();
1249 $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
1250 $user = $this->getDataGenerator()->create_user();
1251
1252 // Add a discussion.
1253 $record = array();
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);
1258
1259 // Add a post.
1260 $record = array();
1261 $record['discussion'] = $discussion->id;
1262 $record['userid'] = $user->id;
1263 $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
1264
1265 $params = array(
1266 'context' => context_module::instance($forum->cmid),
1267 'objectid' => $post->id,
1268 'other' => array('discussionid' => $discussion->id, 'forumid' => $forum->id)
1269 );
1270
1271 $this->setExpectedException('coding_exception', 'forumtype must be set in other.');
1272 \mod_forum\event\post_created::create($params);
1273 }
1274
1275 /**
1276 * Ensure post_created event validates that the contextlevel is correct.
1277 */
1278 public function test_post_created_context_validation() {
1279 $course = $this->getDataGenerator()->create_course();
1280 $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
1281 $user = $this->getDataGenerator()->create_user();
1282
1283 // Add a discussion.
1284 $record = array();
1285 $record['course'] = $course->id;
1286 $record['forum'] = $forum->id;
1287 $record['userid'] = $user->id;
1288 $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1289
1290 // Add a post.
1291 $record = array();
1292 $record['discussion'] = $discussion->id;
1293 $record['userid'] = $user->id;
1294 $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
1295
1296 $params = array(
1297 'context' => context_system::instance(),
1298 'objectid' => $post->id,
1299 'other' => array('discussionid' => $discussion->id, 'forumid' => $forum->id, 'forumtype' => $forum->type)
1300 );
1301
1302 $this->setExpectedException('coding_exception', 'Context passed must be module context');
1303 \mod_forum\event\post_created::create($params);
1304 }
1305
1306 /**
1307 * Test the post_created event.
1308 */
1309 public function test_post_created() {
1310 // Setup test data.
1311 $course = $this->getDataGenerator()->create_course();
1312 $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
1313 $user = $this->getDataGenerator()->create_user();
1314
1315 // Add a discussion.
1316 $record = array();
1317 $record['course'] = $course->id;
1318 $record['forum'] = $forum->id;
1319 $record['userid'] = $user->id;
1320 $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1321
1322 // Add a post.
1323 $record = array();
1324 $record['discussion'] = $discussion->id;
1325 $record['userid'] = $user->id;
1326 $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
1327
1328 $context = context_module::instance($forum->cmid);
1329
1330 $params = array(
1331 'context' => $context,
1332 'objectid' => $post->id,
1333 'other' => array('discussionid' => $discussion->id, 'forumid' => $forum->id, 'forumtype' => $forum->type)
1334 );
1335
1336 $event = \mod_forum\event\post_created::create($params);
1337
1338 // Trigger and capturing the event.
1339 $sink = $this->redirectEvents();
1340 $event->trigger();
1341 $events = $sink->get_events();
1342 $this->assertCount(1, $events);
1343 $event = reset($events);
1344
1345 // Checking that the event contains the expected values.
1346 $this->assertInstanceOf('\mod_forum\event\post_created', $event);
1347 $this->assertEquals($context, $event->get_context());
1348 $expected = array($course->id, 'forum', 'add post', "discuss.php?d={$discussion->id}#p{$post->id}",
1349 $forum->id, $forum->cmid);
1350 $this->assertEventLegacyLogData($expected, $event);
1351 $this->assertEventContextNotUsed($event);
1352
1353 $this->assertNotEmpty($event->get_name());
1354 }
1355
1356 /**
1357 * Ensure post_deleted event validates that the postid is set.
1358 */
1359 public function test_post_deleted_postid_validation() {
1360 $course = $this->getDataGenerator()->create_course();
1361 $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
1362 $user = $this->getDataGenerator()->create_user();
1363
1364 // Add a discussion.
1365 $record = array();
1366 $record['course'] = $course->id;
1367 $record['forum'] = $forum->id;
1368 $record['userid'] = $user->id;
1369 $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1370
1371 $params = array(
1372 'context' => context_module::instance($forum->cmid),
1373 'other' => array('forumid' => $forum->id, 'forumtype' => $forum->type, 'discussionid' => $discussion->id)
1374 );
1375
1376 $this->setExpectedException('coding_exception', 'objectid must be set to the postid.');
1377 \mod_forum\event\post_deleted::create($params);
1378 }
1379
1380 /**
1381 * Ensure post_deleted event validates that the discussionid is set.
1382 */
1383 public function test_post_deleted_discussionid_validation() {
1384 $course = $this->getDataGenerator()->create_course();
1385 $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
1386 $user = $this->getDataGenerator()->create_user();
1387
1388 // Add a discussion.
1389 $record = array();
1390 $record['course'] = $course->id;
1391 $record['forum'] = $forum->id;
1392 $record['userid'] = $user->id;
1393 $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1394
1395 // Add a post.
1396 $record = array();
1397 $record['discussion'] = $discussion->id;
1398 $record['userid'] = $user->id;
1399 $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
1400
1401 $params = array(
1402 'context' => context_module::instance($forum->cmid),
1403 'objectid' => $post->id,
1404 'other' => array('forumid' => $forum->id, 'forumtype' => $forum->type)
1405 );
1406
1407 $this->setExpectedException('coding_exception', 'discussionid must be set in other.');
1408 \mod_forum\event\post_deleted::create($params);
1409 }
1410
1411 /**
1412 * Ensure post_deleted event validates that the forumid is set.
1413 */
1414 public function test_post_deleted_forumid_validation() {
1415 $course = $this->getDataGenerator()->create_course();
1416 $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
1417 $user = $this->getDataGenerator()->create_user();
1418
1419 // Add a discussion.
1420 $record = array();
1421 $record['course'] = $course->id;
1422 $record['forum'] = $forum->id;
1423 $record['userid'] = $user->id;
1424 $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1425
1426 // Add a post.
1427 $record = array();
1428 $record['discussion'] = $discussion->id;
1429 $record['userid'] = $user->id;
1430 $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
1431
1432 $params = array(
1433 'context' => context_module::instance($forum->cmid),
1434 'objectid' => $post->id,
1435 'other' => array('discussionid' => $discussion->id, 'forumtype' => $forum->type)
1436 );
1437
1438 $this->setExpectedException('coding_exception', 'forumid must be set in other.');
1439 \mod_forum\event\post_deleted::create($params);
1440 }
1441
1442 /**
1443 * Ensure post_deleted event validates that the forumtype is set.
1444 */
1445 public function test_post_deleted_forumtype_validation() {
1446 $course = $this->getDataGenerator()->create_course();
1447 $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
1448 $user = $this->getDataGenerator()->create_user();
1449
1450 // Add a discussion.
1451 $record = array();
1452 $record['course'] = $course->id;
1453 $record['forum'] = $forum->id;
1454 $record['userid'] = $user->id;
1455 $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1456
1457 // Add a post.
1458 $record = array();
1459 $record['discussion'] = $discussion->id;
1460 $record['userid'] = $user->id;
1461 $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
1462
1463 $params = array(
1464 'context' => context_module::instance($forum->cmid),
1465 'objectid' => $post->id,
1466 'other' => array('discussionid' => $discussion->id, 'forumid' => $forum->id)
1467 );
1468
1469 $this->setExpectedException('coding_exception', 'forumtype must be set in other.');
1470 \mod_forum\event\post_deleted::create($params);
1471 }
1472
1473 /**
1474 * Ensure post_deleted event validates that the contextlevel is correct.
1475 */
1476 public function test_post_deleted_context_validation() {
1477 $course = $this->getDataGenerator()->create_course();
1478 $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
1479 $user = $this->getDataGenerator()->create_user();
1480
1481 // Add a discussion.
1482 $record = array();
1483 $record['course'] = $course->id;
1484 $record['forum'] = $forum->id;
1485 $record['userid'] = $user->id;
1486 $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1487
1488 // Add a post.
1489 $record = array();
1490 $record['discussion'] = $discussion->id;
1491 $record['userid'] = $user->id;
1492 $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
1493
1494 $params = array(
1495 'context' => context_system::instance(),
1496 'objectid' => $post->id,
1497 'other' => array('discussionid' => $discussion->id, 'forumid' => $forum->id, 'forumtype' => $forum->type)
1498 );
1499
1500 $this->setExpectedException('coding_exception', 'Context passed must be module context');
1501 \mod_forum\event\post_deleted::create($params);
1502 }
1503
1504 /**
1505 * Test post_deleted event.
1506 */
1507 public function test_post_deleted() {
1508 // Setup test data.
1509 $course = $this->getDataGenerator()->create_course();
1510 $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
1511 $user = $this->getDataGenerator()->create_user();
1512
1513 // Add a discussion.
1514 $record = array();
1515 $record['course'] = $course->id;
1516 $record['forum'] = $forum->id;
1517 $record['userid'] = $user->id;
1518 $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1519
1520 // Add a post.
1521 $record = array();
1522 $record['discussion'] = $discussion->id;
1523 $record['userid'] = $user->id;
1524 $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
1525
1526 $context = context_module::instance($forum->cmid);
1527
1528 $params = array(
1529 'context' => $context,
1530 'objectid' => $post->id,
1531 'other' => array('discussionid' => $discussion->id, 'forumid' => $forum->id, 'forumtype' => $forum->type)
1532 );
1533
1534 $event = \mod_forum\event\post_deleted::create($params);
1535
1536 // Trigger and capture the event.
1537 $sink = $this->redirectEvents();
1538 $event->trigger();
1539 $events = $sink->get_events();
1540 $this->assertCount(1, $events);
1541 $event = reset($events);
1542
1543 // Checking that the event contains the expected values.
1544 $this->assertInstanceOf('\mod_forum\event\post_deleted', $event);
1545 $this->assertEquals($context, $event->get_context());
1546 $expected = array($course->id, 'forum', 'delete post', "discuss.php?d={$discussion->id}", $post->id, $forum->cmid);
1547 $this->assertEventLegacyLogData($expected, $event);
1548 $this->assertEventContextNotUsed($event);
1549
1550 $this->assertNotEmpty($event->get_name());
1551 }
1552
1553 /**
1554 * Ensure post_updated event validates that the postid is set.
1555 */
1556 public function test_post_updated_postid_validation() {
1557 $course = $this->getDataGenerator()->create_course();
1558 $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
1559 $user = $this->getDataGenerator()->create_user();
1560
1561 // Add a discussion.
1562 $record = array();
1563 $record['course'] = $course->id;
1564 $record['forum'] = $forum->id;
1565 $record['userid'] = $user->id;
1566 $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1567
1568 $params = array(
1569 'context' => context_module::instance($forum->cmid),
1570 'other' => array('forumid' => $forum->id, 'forumtype' => $forum->type, 'discussionid' => $discussion->id)
1571 );
1572
1573 $this->setExpectedException('coding_exception', 'objectid must be set to the postid.');
1574 \mod_forum\event\post_updated::create($params);
1575 }
1576
1577 /**
1578 * Ensure post_updated event validates that the discussionid is set.
1579 */
1580 public function test_post_updated_discussionid_validation() {
1581 $course = $this->getDataGenerator()->create_course();
1582 $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
1583 $user = $this->getDataGenerator()->create_user();
1584
1585 // Add a discussion.
1586 $record = array();
1587 $record['course'] = $course->id;
1588 $record['forum'] = $forum->id;
1589 $record['userid'] = $user->id;
1590 $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1591
1592 // Add a post.
1593 $record = array();
1594 $record['discussion'] = $discussion->id;
1595 $record['userid'] = $user->id;
1596 $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
1597
1598 $params = array(
1599 'context' => context_module::instance($forum->cmid),
1600 'objectid' => $post->id,
1601 'other' => array('forumid' => $forum->id, 'forumtype' => $forum->type)
1602 );
1603
1604 $this->setExpectedException('coding_exception', 'discussionid must be set in other.');
1605 \mod_forum\event\post_updated::create($params);
1606 }
1607
1608 /**
1609 * Ensure post_updated event validates that the forumid is set.
1610 */
1611 public function test_post_updated_forumid_validation() {
1612 $course = $this->getDataGenerator()->create_course();
1613 $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
1614 $user = $this->getDataGenerator()->create_user();
1615
1616 // Add a discussion.
1617 $record = array();
1618 $record['course'] = $course->id;
1619 $record['forum'] = $forum->id;
1620 $record['userid'] = $user->id;
1621 $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1622
1623 // Add a post.
1624 $record = array();
1625 $record['discussion'] = $discussion->id;
1626 $record['userid'] = $user->id;
1627 $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
1628
1629 $params = array(
1630 'context' => context_module::instance($forum->cmid),
1631 'objectid' => $post->id,
1632 'other' => array('discussionid' => $discussion->id, 'forumtype' => $forum->type)
1633 );
1634
1635 $this->setExpectedException('coding_exception', 'forumid must be set in other.');
1636 \mod_forum\event\post_updated::create($params);
1637 }
1638
1639 /**
1640 * Ensure post_updated event validates that the forumtype is set.
1641 */
1642 public function test_post_updated_forumtype_validation() {
1643 $course = $this->getDataGenerator()->create_course();
1644 $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
1645 $user = $this->getDataGenerator()->create_user();
1646
1647 // Add a discussion.
1648 $record = array();
1649 $record['course'] = $course->id;
1650 $record['forum'] = $forum->id;
1651 $record['userid'] = $user->id;
1652 $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1653
1654 // Add a post.
1655 $record = array();
1656 $record['discussion'] = $discussion->id;
1657 $record['userid'] = $user->id;
1658 $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
1659
1660 $params = array(
1661 'context' => context_module::instance($forum->cmid),
1662 'objectid' => $post->id,
1663 'other' => array('discussionid' => $discussion->id, 'forumid' => $forum->id)
1664 );
1665
1666 $this->setExpectedException('coding_exception', 'forumtype must be set in other.');
1667 \mod_forum\event\post_updated::create($params);
1668 }
1669
1670 /**
1671 * Ensure post_updated event validates that the contextlevel is correct.
1672 */
1673 public function test_post_updated_context_validation() {
1674 $course = $this->getDataGenerator()->create_course();
1675 $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
1676 $user = $this->getDataGenerator()->create_user();
1677
1678 // Add a discussion.
1679 $record = array();
1680 $record['course'] = $course->id;
1681 $record['forum'] = $forum->id;
1682 $record['userid'] = $user->id;
1683 $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1684
1685 // Add a post.
1686 $record = array();
1687 $record['discussion'] = $discussion->id;
1688 $record['userid'] = $user->id;
1689 $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
1690
1691 $params = array(
1692 'context' => context_system::instance(),
1693 'objectid' => $post->id,
1694 'other' => array('discussionid' => $discussion->id, 'forumid' => $forum->id, 'forumtype' => $forum->type)
1695 );
1696
1697 $this->setExpectedException('coding_exception', 'Context passed must be module context');
1698 \mod_forum\event\post_updated::create($params);
1699 }
1700
1701 /**
1702 * Test post_updated event.
1703 */
1704 public function test_post_updated() {
1705 // Setup test data.
1706 $course = $this->getDataGenerator()->create_course();
1707 $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
1708 $user = $this->getDataGenerator()->create_user();
1709
1710 // Add a discussion.
1711 $record = array();
1712 $record['course'] = $course->id;
1713 $record['forum'] = $forum->id;
1714 $record['userid'] = $user->id;
1715 $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1716
1717 // Add a post.
1718 $record = array();
1719 $record['discussion'] = $discussion->id;
1720 $record['userid'] = $user->id;
1721 $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
1722
1723 $context = context_module::instance($forum->cmid);
1724
1725 $params = array(
1726 'context' => $context,
1727 'objectid' => $post->id,
1728 'other' => array('discussionid' => $discussion->id, 'forumid' => $forum->id, 'forumtype' => $forum->type)
1729 );
1730
1731 $event = \mod_forum\event\post_updated::create($params);
1732
1733 // Trigger and capturing the event.
1734 $sink = $this->redirectEvents();
1735 $event->trigger();
1736 $events = $sink->get_events();
1737 $this->assertCount(1, $events);
1738 $event = reset($events);
1739
1740 // Checking that the event contains the expected values.
1741 $this->assertInstanceOf('\mod_forum\event\post_updated', $event);
1742 $this->assertEquals($context, $event->get_context());
1743 $expected = array($course->id, 'forum', 'update post', "discuss.php?d={$discussion->id}#p{$post->id}",
1744 $post->id, $forum->cmid);
1745 $this->assertEventLegacyLogData($expected, $event);
1746 $this->assertEventContextNotUsed($event);
1747
1748 $this->assertNotEmpty($event->get_name());
1749 }
1750}