MDL-43557 mod_lesson: removed incorrect usage of snapshots
[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);
f548bc4d
JO
658 $url = new \moodle_url('/mod/forum/view.php', array('f' => $forum->id));
659 $this->assertEquals($url, $event->get_url());
a52c04d5
DP
660 $this->assertEventContextNotUsed($event);
661
662 $this->assertNotEmpty($event->get_name());
663 }
664
665 /**
666 * Ensure subscription_created event validates that the forumid is set.
667 */
668 public function test_subscription_created_forumid_validation() {
669 $user = $this->getDataGenerator()->create_user();
670 $course = $this->getDataGenerator()->create_course();
671 $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
672
673 $params = array(
674 'context' => context_module::instance($forum->cmid),
675 'relateduserid' => $user->id,
676 );
677
678 $this->setExpectedException('coding_exception', 'forumid must be set in other.');
679 \mod_forum\event\subscription_created::create($params);
680 }
681
682 /**
683 * Ensure subscription_created event validates that the relateduserid is set.
684 */
685 public function test_subscription_created_relateduserid_validation() {
686 $course = $this->getDataGenerator()->create_course();
687 $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
688
689 $params = array(
690 'context' => context_module::instance($forum->cmid),
691 'objectid' => $forum->id,
692 );
693
694 $this->setExpectedException('coding_exception', 'relateduserid must be set.');
695 \mod_forum\event\subscription_created::create($params);
696 }
697
698 /**
699 * Ensure subscription_created event validates that the contextlevel is correct.
700 */
701 public function test_subscription_created_contextlevel_validation() {
702 $user = $this->getDataGenerator()->create_user();
703 $course = $this->getDataGenerator()->create_course();
704 $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
705
706 $params = array(
707 'context' => context_system::instance(),
708 'other' => array('forumid' => $forum->id),
709 'relateduserid' => $user->id,
710 );
711
712 $this->setExpectedException('coding_exception', 'Context passed must be module context.');
713 \mod_forum\event\subscription_created::create($params);
714 }
715
716 /**
717 * Test the subscription_created event.
718 */
719 public function test_subscription_created() {
720 // Setup test data.
721 $user = $this->getDataGenerator()->create_user();
722 $course = $this->getDataGenerator()->create_course();
723 $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
724 $context = context_module::instance($forum->cmid);
725
726 $params = array(
727 'context' => $context,
728 'other' => array('forumid' => $forum->id),
729 'relateduserid' => $user->id,
730 );
731
732 $event = \mod_forum\event\subscription_created::create($params);
733
734 // Trigger and capturing the event.
735 $sink = $this->redirectEvents();
736 $event->trigger();
737 $events = $sink->get_events();
738 $this->assertCount(1, $events);
739 $event = reset($events);
740
741 // Checking that the event contains the expected values.
742 $this->assertInstanceOf('\mod_forum\event\subscription_created', $event);
743 $this->assertEquals($context, $event->get_context());
744 $expected = array($course->id, 'forum', 'subscribe', "view.php?f={$forum->id}", $forum->id, $forum->cmid);
745 $this->assertEventLegacyLogData($expected, $event);
f548bc4d
JO
746 $url = new \moodle_url('/mod/forum/view.php', array('f' => $forum->id));
747 $this->assertEquals($url, $event->get_url());
a52c04d5
DP
748 $this->assertEventContextNotUsed($event);
749
750 $this->assertNotEmpty($event->get_name());
751 }
752
753 /**
754 * Ensure subscription_deleted event validates that the forumid is set.
755 */
756 public function test_subscription_deleted_forumid_validation() {
757 $user = $this->getDataGenerator()->create_user();
758 $course = $this->getDataGenerator()->create_course();
759 $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
760
761 $params = array(
762 'context' => context_module::instance($forum->cmid),
763 'relateduserid' => $user->id,
764 );
765
766 $this->setExpectedException('coding_exception', 'forumid must be set in other.');
767 \mod_forum\event\subscription_deleted::create($params);
768 }
769
770 /**
771 * Ensure subscription_deleted event validates that the relateduserid is set.
772 */
773 public function test_subscription_deleted_relateduserid_validation() {
774 $course = $this->getDataGenerator()->create_course();
775 $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
776
777 $params = array(
778 'context' => context_module::instance($forum->cmid),
779 'objectid' => $forum->id,
780 );
781
782 $this->setExpectedException('coding_exception', 'relateduserid must be set.');
783 \mod_forum\event\subscription_deleted::create($params);
784 }
785
786 /**
787 * Ensure subscription_deleted event validates that the contextlevel is correct.
788 */
789 public function test_subscription_deleted_contextlevel_validation() {
790 $user = $this->getDataGenerator()->create_user();
791 $course = $this->getDataGenerator()->create_course();
792 $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
793
794 $params = array(
795 'context' => context_system::instance(),
796 'other' => array('forumid' => $forum->id),
797 'relateduserid' => $user->id,
798 );
799
800 $this->setExpectedException('coding_exception', 'Context passed must be module context.');
801 \mod_forum\event\subscription_deleted::create($params);
802 }
803
804 /**
805 * Test the subscription_deleted event.
806 */
807 public function test_subscription_deleted() {
808 // Setup test data.
809 $user = $this->getDataGenerator()->create_user();
810 $course = $this->getDataGenerator()->create_course();
811 $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
812 $context = context_module::instance($forum->cmid);
813
814 $params = array(
815 'context' => $context,
816 'other' => array('forumid' => $forum->id),
817 'relateduserid' => $user->id,
818 );
819
820 $event = \mod_forum\event\subscription_deleted::create($params);
821
822 // Trigger and capturing the event.
823 $sink = $this->redirectEvents();
824 $event->trigger();
825 $events = $sink->get_events();
826 $this->assertCount(1, $events);
827 $event = reset($events);
828
829 // Checking that the event contains the expected values.
830 $this->assertInstanceOf('\mod_forum\event\subscription_deleted', $event);
831 $this->assertEquals($context, $event->get_context());
832 $expected = array($course->id, 'forum', 'unsubscribe', "view.php?f={$forum->id}", $forum->id, $forum->cmid);
833 $this->assertEventLegacyLogData($expected, $event);
f548bc4d
JO
834 $url = new \moodle_url('/mod/forum/view.php', array('f' => $forum->id));
835 $this->assertEquals($url, $event->get_url());
a52c04d5
DP
836 $this->assertEventContextNotUsed($event);
837
838 $this->assertNotEmpty($event->get_name());
839 }
840
841 /**
842 * Ensure readtracking_enabled event validates that the forumid is set.
843 */
844 public function test_readtracking_enabled_forumid_validation() {
845 $user = $this->getDataGenerator()->create_user();
846 $course = $this->getDataGenerator()->create_course();
847 $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
848
849 $params = array(
850 'context' => context_module::instance($forum->cmid),
851 'relateduserid' => $user->id,
852 );
853
854 $this->setExpectedException('coding_exception', 'forumid must be set in other.');
855 \mod_forum\event\readtracking_enabled::create($params);
856 }
857
858 /**
859 * Ensure readtracking_enabled event validates that the relateduserid is set.
860 */
861 public function test_readtracking_enabled_relateduserid_validation() {
862 $course = $this->getDataGenerator()->create_course();
863 $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
864
865 $params = array(
866 'context' => context_module::instance($forum->cmid),
867 'objectid' => $forum->id,
868 );
869
870 $this->setExpectedException('coding_exception', 'relateduserid must be set.');
871 \mod_forum\event\readtracking_enabled::create($params);
872 }
873
874 /**
875 * Ensure readtracking_enabled event validates that the contextlevel is correct.
876 */
877 public function test_readtracking_enabled_contextlevel_validation() {
878 $user = $this->getDataGenerator()->create_user();
879 $course = $this->getDataGenerator()->create_course();
880 $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
881
882 $params = array(
883 'context' => context_system::instance(),
884 'other' => array('forumid' => $forum->id),
885 'relateduserid' => $user->id,
886 );
887
888 $this->setExpectedException('coding_exception', 'Context passed must be module context.');
889 \mod_forum\event\readtracking_enabled::create($params);
890 }
891
892 /**
893 * Test the readtracking_enabled event.
894 */
895 public function test_readtracking_enabled() {
896 // Setup test data.
897 $user = $this->getDataGenerator()->create_user();
898 $course = $this->getDataGenerator()->create_course();
899 $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
900 $context = context_module::instance($forum->cmid);
901
902 $params = array(
903 'context' => $context,
904 'other' => array('forumid' => $forum->id),
905 'relateduserid' => $user->id,
906 );
907
908 $event = \mod_forum\event\readtracking_enabled::create($params);
909
910 // Trigger and capture the event.
911 $sink = $this->redirectEvents();
912 $event->trigger();
913 $events = $sink->get_events();
914 $this->assertCount(1, $events);
915 $event = reset($events);
916
917 // Checking that the event contains the expected values.
918 $this->assertInstanceOf('\mod_forum\event\readtracking_enabled', $event);
919 $this->assertEquals($context, $event->get_context());
920 $expected = array($course->id, 'forum', 'start tracking', "view.php?f={$forum->id}", $forum->id, $forum->cmid);
921 $this->assertEventLegacyLogData($expected, $event);
f548bc4d
JO
922 $url = new \moodle_url('/mod/forum/view.php', array('f' => $forum->id));
923 $this->assertEquals($url, $event->get_url());
a52c04d5
DP
924 $this->assertEventContextNotUsed($event);
925
926 $this->assertNotEmpty($event->get_name());
927 }
928
929 /**
930 * Ensure readtracking_disabled event validates that the forumid is set.
931 */
932 public function test_readtracking_disabled_forumid_validation() {
933 $user = $this->getDataGenerator()->create_user();
934 $course = $this->getDataGenerator()->create_course();
935 $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
936
937 $params = array(
938 'context' => context_module::instance($forum->cmid),
939 'relateduserid' => $user->id,
940 );
941
942 $this->setExpectedException('coding_exception', 'forumid must be set in other.');
943 \mod_forum\event\readtracking_disabled::create($params);
944 }
945
946 /**
947 * Ensure readtracking_disabled event validates that the relateduserid is set.
948 */
949 public function test_readtracking_disabled_relateduserid_validation() {
950 $course = $this->getDataGenerator()->create_course();
951 $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
952
953 $params = array(
954 'context' => context_module::instance($forum->cmid),
955 'objectid' => $forum->id,
956 );
957
958 $this->setExpectedException('coding_exception', 'relateduserid must be set.');
959 \mod_forum\event\readtracking_disabled::create($params);
960 }
961
962 /**
963 * Ensure readtracking_disabled event validates that the contextlevel is correct
964 */
965 public function test_readtracking_disabled_contextlevel_validation() {
966 $user = $this->getDataGenerator()->create_user();
967 $course = $this->getDataGenerator()->create_course();
968 $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
969
970 $params = array(
971 'context' => context_system::instance(),
972 'other' => array('forumid' => $forum->id),
973 'relateduserid' => $user->id,
974 );
975
976 $this->setExpectedException('coding_exception', 'Context passed must be module context.');
977 \mod_forum\event\readtracking_disabled::create($params);
978 }
979
980 /**
981 * Test the readtracking_disabled event.
982 */
983 public function test_readtracking_disabled() {
984 // Setup test data.
985 $user = $this->getDataGenerator()->create_user();
986 $course = $this->getDataGenerator()->create_course();
987 $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
988 $context = context_module::instance($forum->cmid);
989
990 $params = array(
991 'context' => $context,
992 'other' => array('forumid' => $forum->id),
993 'relateduserid' => $user->id,
994 );
995
996 $event = \mod_forum\event\readtracking_disabled::create($params);
997
998 // Trigger and capture the event.
999 $sink = $this->redirectEvents();
1000 $event->trigger();
1001 $events = $sink->get_events();
1002 $this->assertCount(1, $events);
1003 $event = reset($events);
1004
1005 // Checking that the event contains the expected values.
1006 $this->assertInstanceOf('\mod_forum\event\readtracking_disabled', $event);
1007 $this->assertEquals($context, $event->get_context());
1008 $expected = array($course->id, 'forum', 'stop tracking', "view.php?f={$forum->id}", $forum->id, $forum->cmid);
1009 $this->assertEventLegacyLogData($expected, $event);
f548bc4d
JO
1010 $url = new \moodle_url('/mod/forum/view.php', array('f' => $forum->id));
1011 $this->assertEquals($url, $event->get_url());
a52c04d5
DP
1012 $this->assertEventContextNotUsed($event);
1013
1014 $this->assertNotEmpty($event->get_name());
1015 }
1016
1017 /**
1018 * Ensure subscribers_viewed event validates that the forumid is set.
1019 */
1020 public function test_subscribers_viewed_forumid_validation() {
1021 $user = $this->getDataGenerator()->create_user();
1022 $course = $this->getDataGenerator()->create_course();
1023 $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
1024
1025 $params = array(
1026 'context' => context_module::instance($forum->cmid),
1027 'relateduserid' => $user->id,
1028 );
1029
1030 $this->setExpectedException('coding_exception', 'forumid must be set in other.');
1031 \mod_forum\event\subscribers_viewed::create($params);
1032 }
1033
1034 /**
1035 * Ensure subscribers_viewed event validates that the contextlevel is correct.
1036 */
1037 public function test_subscribers_viewed_contextlevel_validation() {
1038 $user = $this->getDataGenerator()->create_user();
1039 $course = $this->getDataGenerator()->create_course();
1040 $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
1041
1042 $params = array(
1043 'context' => context_system::instance(),
1044 'other' => array('forumid' => $forum->id),
1045 'relateduserid' => $user->id,
1046 );
1047
1048 $this->setExpectedException('coding_exception', 'Context passed must be module context.');
1049 \mod_forum\event\subscribers_viewed::create($params);
1050 }
1051
1052 /**
1053 * Test the subscribers_viewed event.
1054 */
1055 public function test_subscribers_viewed() {
1056 // Setup test data.
1057 $course = $this->getDataGenerator()->create_course();
1058 $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
1059 $context = context_module::instance($forum->cmid);
1060
1061 $params = array(
1062 'context' => $context,
1063 'other' => array('forumid' => $forum->id),
1064 );
1065
1066 $event = \mod_forum\event\subscribers_viewed::create($params);
1067
1068 // Trigger and capture the event.
1069 $sink = $this->redirectEvents();
1070 $event->trigger();
1071 $events = $sink->get_events();
1072 $this->assertCount(1, $events);
1073 $event = reset($events);
1074
1075 // Checking that the event contains the expected values.
1076 $this->assertInstanceOf('\mod_forum\event\subscribers_viewed', $event);
1077 $this->assertEquals($context, $event->get_context());
1078 $expected = array($course->id, 'forum', 'view subscribers', "subscribers.php?id={$forum->id}", $forum->id, $forum->cmid);
1079 $this->assertEventLegacyLogData($expected, $event);
1080 $this->assertEventContextNotUsed($event);
1081
1082 $this->assertNotEmpty($event->get_name());
1083 }
1084
1085 /**
f3d98189 1086 * Ensure user_report_viewed event validates that the reportmode is set.
a52c04d5 1087 */
f3d98189 1088 public function test_user_report_viewed_reportmode_validation() {
a52c04d5
DP
1089 $user = $this->getDataGenerator()->create_user();
1090 $course = $this->getDataGenerator()->create_course();
1091
1092 $params = array(
1093 'context' => context_course::instance($course->id),
1094 'relateduserid' => $user->id,
1095 );
1096
1097 $this->setExpectedException('coding_exception', 'reportmode must be set in other.');
f3d98189 1098 \mod_forum\event\user_report_viewed::create($params);
a52c04d5
DP
1099 }
1100
1101 /**
f3d98189 1102 * Ensure user_report_viewed event validates that the contextlevel is correct.
a52c04d5 1103 */
f3d98189 1104 public function test_user_report_viewed_contextlevel_validation() {
a52c04d5
DP
1105 $user = $this->getDataGenerator()->create_user();
1106 $course = $this->getDataGenerator()->create_course();
1107 $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
1108
1109 $params = array(
1110 'context' => context_module::instance($forum->id),
1111 'other' => array('reportmode' => 'posts'),
1112 'relateduserid' => $user->id,
1113 );
1114
1115 $this->setExpectedException('coding_exception', 'Context passed must be system or course.');
f3d98189 1116 \mod_forum\event\user_report_viewed::create($params);
a52c04d5
DP
1117 }
1118
1119 /**
f3d98189 1120 * Ensure user_report_viewed event validates that the relateduserid is set.
a52c04d5 1121 */
f3d98189 1122 public function test_user_report_viewed_relateduserid_validation() {
a52c04d5
DP
1123
1124 $params = array(
1125 'context' => context_system::instance(),
1126 'other' => array('reportmode' => 'posts'),
1127 );
1128
1129 $this->setExpectedException('coding_exception', 'relateduserid must be set.');
f3d98189 1130 \mod_forum\event\user_report_viewed::create($params);
a52c04d5
DP
1131 }
1132
1133 /**
f3d98189 1134 * Test the user_report_viewed event.
a52c04d5 1135 */
f3d98189 1136 public function test_user_report_viewed() {
a52c04d5
DP
1137 // Setup test data.
1138 $user = $this->getDataGenerator()->create_user();
1139 $course = $this->getDataGenerator()->create_course();
1140 $context = context_course::instance($course->id);
1141
1142 $params = array(
1143 'context' => $context,
1144 'relateduserid' => $user->id,
1145 'other' => array('reportmode' => 'discussions'),
1146 );
1147
f3d98189 1148 $event = \mod_forum\event\user_report_viewed::create($params);
a52c04d5
DP
1149
1150 // Trigger and capture the event.
1151 $sink = $this->redirectEvents();
1152 $event->trigger();
1153 $events = $sink->get_events();
1154 $this->assertCount(1, $events);
1155 $event = reset($events);
1156
1157 // Checking that the event contains the expected values.
f3d98189 1158 $this->assertInstanceOf('\mod_forum\event\user_report_viewed', $event);
a52c04d5
DP
1159 $this->assertEquals($context, $event->get_context());
1160 $expected = array($course->id, 'forum', 'user report',
1161 "user.php?id={$user->id}&amp;mode=discussions&amp;course={$course->id}", $user->id);
1162 $this->assertEventLegacyLogData($expected, $event);
1163 $this->assertEventContextNotUsed($event);
1164
1165 $this->assertNotEmpty($event->get_name());
1166 }
1167
1168 /**
1169 * Ensure post_created event validates that the postid is set.
1170 */
1171 public function test_post_created_postid_validation() {
1172 $course = $this->getDataGenerator()->create_course();
1173 $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
1174 $user = $this->getDataGenerator()->create_user();
1175
1176 // Add a discussion.
1177 $record = array();
1178 $record['course'] = $course->id;
1179 $record['forum'] = $forum->id;
1180 $record['userid'] = $user->id;
1181 $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1182
1183 $params = array(
1184 'context' => context_module::instance($forum->cmid),
1185 'other' => array('forumid' => $forum->id, 'forumtype' => $forum->type, 'discussionid' => $discussion->id)
1186 );
1187
1188 $this->setExpectedException('coding_exception', 'objectid must be set to the postid.');
1189 \mod_forum\event\post_created::create($params);
1190 }
1191
1192 /**
1193 * Ensure post_created event validates that the discussionid is set.
1194 */
1195 public function test_post_created_discussionid_validation() {
1196 $course = $this->getDataGenerator()->create_course();
1197 $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
1198 $user = $this->getDataGenerator()->create_user();
1199
1200 // Add a discussion.
1201 $record = array();
1202 $record['course'] = $course->id;
1203 $record['forum'] = $forum->id;
1204 $record['userid'] = $user->id;
1205 $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1206
1207 // Add a post.
1208 $record = array();
1209 $record['discussion'] = $discussion->id;
1210 $record['userid'] = $user->id;
1211 $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
1212
1213 $params = array(
1214 'context' => context_module::instance($forum->cmid),
1215 'objectid' => $post->id,
1216 'other' => array('forumid' => $forum->id, 'forumtype' => $forum->type)
1217 );
1218
1219 $this->setExpectedException('coding_exception', 'discussionid must be set in other.');
1220 \mod_forum\event\post_created::create($params);
1221 }
1222
1223 /**
1224 * Ensure post_created event validates that the forumid is set.
1225 */
1226 public function test_post_created_forumid_validation() {
1227 $course = $this->getDataGenerator()->create_course();
1228 $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
1229 $user = $this->getDataGenerator()->create_user();
1230
1231 // Add a discussion.
1232 $record = array();
1233 $record['course'] = $course->id;
1234 $record['forum'] = $forum->id;
1235 $record['userid'] = $user->id;
1236 $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1237
1238 // Add a post.
1239 $record = array();
1240 $record['discussion'] = $discussion->id;
1241 $record['userid'] = $user->id;
1242 $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
1243
1244 $params = array(
1245 'context' => context_module::instance($forum->cmid),
1246 'objectid' => $post->id,
1247 'other' => array('discussionid' => $discussion->id, 'forumtype' => $forum->type)
1248 );
1249
1250 $this->setExpectedException('coding_exception', 'forumid must be set in other.');
1251 \mod_forum\event\post_created::create($params);
1252 }
1253
1254 /**
1255 * Ensure post_created event validates that the forumtype is set.
1256 */
1257 public function test_post_created_forumtype_validation() {
1258 $course = $this->getDataGenerator()->create_course();
1259 $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
1260 $user = $this->getDataGenerator()->create_user();
1261
1262 // Add a discussion.
1263 $record = array();
1264 $record['course'] = $course->id;
1265 $record['forum'] = $forum->id;
1266 $record['userid'] = $user->id;
1267 $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1268
1269 // Add a post.
1270 $record = array();
1271 $record['discussion'] = $discussion->id;
1272 $record['userid'] = $user->id;
1273 $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
1274
1275 $params = array(
1276 'context' => context_module::instance($forum->cmid),
1277 'objectid' => $post->id,
1278 'other' => array('discussionid' => $discussion->id, 'forumid' => $forum->id)
1279 );
1280
1281 $this->setExpectedException('coding_exception', 'forumtype must be set in other.');
1282 \mod_forum\event\post_created::create($params);
1283 }
1284
1285 /**
1286 * Ensure post_created event validates that the contextlevel is correct.
1287 */
1288 public function test_post_created_context_validation() {
1289 $course = $this->getDataGenerator()->create_course();
1290 $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
1291 $user = $this->getDataGenerator()->create_user();
1292
1293 // Add a discussion.
1294 $record = array();
1295 $record['course'] = $course->id;
1296 $record['forum'] = $forum->id;
1297 $record['userid'] = $user->id;
1298 $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1299
1300 // Add a post.
1301 $record = array();
1302 $record['discussion'] = $discussion->id;
1303 $record['userid'] = $user->id;
1304 $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
1305
1306 $params = array(
1307 'context' => context_system::instance(),
1308 'objectid' => $post->id,
1309 'other' => array('discussionid' => $discussion->id, 'forumid' => $forum->id, 'forumtype' => $forum->type)
1310 );
1311
1312 $this->setExpectedException('coding_exception', 'Context passed must be module context');
1313 \mod_forum\event\post_created::create($params);
1314 }
1315
1316 /**
1317 * Test the post_created event.
1318 */
1319 public function test_post_created() {
1320 // Setup test data.
1321 $course = $this->getDataGenerator()->create_course();
1322 $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
1323 $user = $this->getDataGenerator()->create_user();
1324
1325 // Add a discussion.
1326 $record = array();
1327 $record['course'] = $course->id;
1328 $record['forum'] = $forum->id;
1329 $record['userid'] = $user->id;
1330 $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1331
1332 // Add a post.
1333 $record = array();
1334 $record['discussion'] = $discussion->id;
1335 $record['userid'] = $user->id;
1336 $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
1337
1338 $context = context_module::instance($forum->cmid);
1339
1340 $params = array(
1341 'context' => $context,
1342 'objectid' => $post->id,
1343 'other' => array('discussionid' => $discussion->id, 'forumid' => $forum->id, 'forumtype' => $forum->type)
1344 );
1345
1346 $event = \mod_forum\event\post_created::create($params);
1347
1348 // Trigger and capturing the event.
1349 $sink = $this->redirectEvents();
1350 $event->trigger();
1351 $events = $sink->get_events();
1352 $this->assertCount(1, $events);
1353 $event = reset($events);
1354
1355 // Checking that the event contains the expected values.
1356 $this->assertInstanceOf('\mod_forum\event\post_created', $event);
1357 $this->assertEquals($context, $event->get_context());
1358 $expected = array($course->id, 'forum', 'add post', "discuss.php?d={$discussion->id}#p{$post->id}",
1359 $forum->id, $forum->cmid);
1360 $this->assertEventLegacyLogData($expected, $event);
f548bc4d
JO
1361 $url = new \moodle_url('/mod/forum/discuss.php', array('d' => $discussion->id));
1362 $url->set_anchor('p'.$event->objectid);
1363 $this->assertEquals($url, $event->get_url());
1364 $this->assertEventContextNotUsed($event);
1365
1366 $this->assertNotEmpty($event->get_name());
1367 }
1368
1369 /**
1370 * Test the post_created event for a single discussion forum.
1371 */
1372 public function test_post_created_single() {
1373 // Setup test data.
1374 $course = $this->getDataGenerator()->create_course();
1375 $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id, 'type' => 'single'));
1376 $user = $this->getDataGenerator()->create_user();
1377
1378 // Add a discussion.
1379 $record = array();
1380 $record['course'] = $course->id;
1381 $record['forum'] = $forum->id;
1382 $record['userid'] = $user->id;
1383 $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1384
1385 // Add a post.
1386 $record = array();
1387 $record['discussion'] = $discussion->id;
1388 $record['userid'] = $user->id;
1389 $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
1390
1391 $context = context_module::instance($forum->cmid);
1392
1393 $params = array(
1394 'context' => $context,
1395 'objectid' => $post->id,
1396 'other' => array('discussionid' => $discussion->id, 'forumid' => $forum->id, 'forumtype' => $forum->type)
1397 );
1398
1399 $event = \mod_forum\event\post_created::create($params);
1400
1401 // Trigger and capturing the event.
1402 $sink = $this->redirectEvents();
1403 $event->trigger();
1404 $events = $sink->get_events();
1405 $this->assertCount(1, $events);
1406 $event = reset($events);
1407
1408 // Checking that the event contains the expected values.
1409 $this->assertInstanceOf('\mod_forum\event\post_created', $event);
1410 $this->assertEquals($context, $event->get_context());
1411 $expected = array($course->id, 'forum', 'add post', "view.php?f={$forum->id}#p{$post->id}",
1412 $forum->id, $forum->cmid);
1413 $this->assertEventLegacyLogData($expected, $event);
1414 $url = new \moodle_url('/mod/forum/view.php', array('f' => $forum->id));
1415 $url->set_anchor('p'.$event->objectid);
1416 $this->assertEquals($url, $event->get_url());
a52c04d5
DP
1417 $this->assertEventContextNotUsed($event);
1418
1419 $this->assertNotEmpty($event->get_name());
1420 }
1421
1422 /**
1423 * Ensure post_deleted event validates that the postid is set.
1424 */
1425 public function test_post_deleted_postid_validation() {
1426 $course = $this->getDataGenerator()->create_course();
1427 $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
1428 $user = $this->getDataGenerator()->create_user();
1429
1430 // Add a discussion.
1431 $record = array();
1432 $record['course'] = $course->id;
1433 $record['forum'] = $forum->id;
1434 $record['userid'] = $user->id;
1435 $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1436
1437 $params = array(
1438 'context' => context_module::instance($forum->cmid),
1439 'other' => array('forumid' => $forum->id, 'forumtype' => $forum->type, 'discussionid' => $discussion->id)
1440 );
1441
1442 $this->setExpectedException('coding_exception', 'objectid must be set to the postid.');
1443 \mod_forum\event\post_deleted::create($params);
1444 }
1445
1446 /**
1447 * Ensure post_deleted event validates that the discussionid is set.
1448 */
1449 public function test_post_deleted_discussionid_validation() {
1450 $course = $this->getDataGenerator()->create_course();
1451 $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
1452 $user = $this->getDataGenerator()->create_user();
1453
1454 // Add a discussion.
1455 $record = array();
1456 $record['course'] = $course->id;
1457 $record['forum'] = $forum->id;
1458 $record['userid'] = $user->id;
1459 $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1460
1461 // Add a post.
1462 $record = array();
1463 $record['discussion'] = $discussion->id;
1464 $record['userid'] = $user->id;
1465 $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
1466
1467 $params = array(
1468 'context' => context_module::instance($forum->cmid),
1469 'objectid' => $post->id,
1470 'other' => array('forumid' => $forum->id, 'forumtype' => $forum->type)
1471 );
1472
1473 $this->setExpectedException('coding_exception', 'discussionid must be set in other.');
1474 \mod_forum\event\post_deleted::create($params);
1475 }
1476
1477 /**
1478 * Ensure post_deleted event validates that the forumid is set.
1479 */
1480 public function test_post_deleted_forumid_validation() {
1481 $course = $this->getDataGenerator()->create_course();
1482 $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
1483 $user = $this->getDataGenerator()->create_user();
1484
1485 // Add a discussion.
1486 $record = array();
1487 $record['course'] = $course->id;
1488 $record['forum'] = $forum->id;
1489 $record['userid'] = $user->id;
1490 $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1491
1492 // Add a post.
1493 $record = array();
1494 $record['discussion'] = $discussion->id;
1495 $record['userid'] = $user->id;
1496 $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
1497
1498 $params = array(
1499 'context' => context_module::instance($forum->cmid),
1500 'objectid' => $post->id,
1501 'other' => array('discussionid' => $discussion->id, 'forumtype' => $forum->type)
1502 );
1503
1504 $this->setExpectedException('coding_exception', 'forumid must be set in other.');
1505 \mod_forum\event\post_deleted::create($params);
1506 }
1507
1508 /**
1509 * Ensure post_deleted event validates that the forumtype is set.
1510 */
1511 public function test_post_deleted_forumtype_validation() {
1512 $course = $this->getDataGenerator()->create_course();
1513 $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
1514 $user = $this->getDataGenerator()->create_user();
1515
1516 // Add a discussion.
1517 $record = array();
1518 $record['course'] = $course->id;
1519 $record['forum'] = $forum->id;
1520 $record['userid'] = $user->id;
1521 $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1522
1523 // Add a post.
1524 $record = array();
1525 $record['discussion'] = $discussion->id;
1526 $record['userid'] = $user->id;
1527 $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
1528
1529 $params = array(
1530 'context' => context_module::instance($forum->cmid),
1531 'objectid' => $post->id,
1532 'other' => array('discussionid' => $discussion->id, 'forumid' => $forum->id)
1533 );
1534
1535 $this->setExpectedException('coding_exception', 'forumtype must be set in other.');
1536 \mod_forum\event\post_deleted::create($params);
1537 }
1538
1539 /**
1540 * Ensure post_deleted event validates that the contextlevel is correct.
1541 */
1542 public function test_post_deleted_context_validation() {
1543 $course = $this->getDataGenerator()->create_course();
1544 $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
1545 $user = $this->getDataGenerator()->create_user();
1546
1547 // Add a discussion.
1548 $record = array();
1549 $record['course'] = $course->id;
1550 $record['forum'] = $forum->id;
1551 $record['userid'] = $user->id;
1552 $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1553
1554 // Add a post.
1555 $record = array();
1556 $record['discussion'] = $discussion->id;
1557 $record['userid'] = $user->id;
1558 $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
1559
1560 $params = array(
1561 'context' => context_system::instance(),
1562 'objectid' => $post->id,
1563 'other' => array('discussionid' => $discussion->id, 'forumid' => $forum->id, 'forumtype' => $forum->type)
1564 );
1565
1566 $this->setExpectedException('coding_exception', 'Context passed must be module context');
1567 \mod_forum\event\post_deleted::create($params);
1568 }
1569
1570 /**
1571 * Test post_deleted event.
1572 */
1573 public function test_post_deleted() {
1574 // Setup test data.
1575 $course = $this->getDataGenerator()->create_course();
1576 $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
1577 $user = $this->getDataGenerator()->create_user();
1578
1579 // Add a discussion.
1580 $record = array();
1581 $record['course'] = $course->id;
1582 $record['forum'] = $forum->id;
1583 $record['userid'] = $user->id;
1584 $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1585
1586 // Add a post.
1587 $record = array();
1588 $record['discussion'] = $discussion->id;
1589 $record['userid'] = $user->id;
1590 $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
1591
1592 $context = context_module::instance($forum->cmid);
1593
1594 $params = array(
1595 'context' => $context,
1596 'objectid' => $post->id,
1597 'other' => array('discussionid' => $discussion->id, 'forumid' => $forum->id, 'forumtype' => $forum->type)
1598 );
1599
1600 $event = \mod_forum\event\post_deleted::create($params);
1601
1602 // Trigger and capture the event.
1603 $sink = $this->redirectEvents();
1604 $event->trigger();
1605 $events = $sink->get_events();
1606 $this->assertCount(1, $events);
1607 $event = reset($events);
1608
1609 // Checking that the event contains the expected values.
1610 $this->assertInstanceOf('\mod_forum\event\post_deleted', $event);
1611 $this->assertEquals($context, $event->get_context());
1612 $expected = array($course->id, 'forum', 'delete post', "discuss.php?d={$discussion->id}", $post->id, $forum->cmid);
1613 $this->assertEventLegacyLogData($expected, $event);
f548bc4d
JO
1614 $url = new \moodle_url('/mod/forum/discuss.php', array('d' => $discussion->id));
1615 $this->assertEquals($url, $event->get_url());
1616 $this->assertEventContextNotUsed($event);
1617
1618 $this->assertNotEmpty($event->get_name());
1619 }
1620
1621 /**
1622 * Test post_deleted event for a single discussion forum.
1623 */
1624 public function test_post_deleted_single() {
1625 // Setup test data.
1626 $course = $this->getDataGenerator()->create_course();
1627 $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id, 'type' => 'single'));
1628 $user = $this->getDataGenerator()->create_user();
1629
1630 // Add a discussion.
1631 $record = array();
1632 $record['course'] = $course->id;
1633 $record['forum'] = $forum->id;
1634 $record['userid'] = $user->id;
1635 $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1636
1637 // Add a post.
1638 $record = array();
1639 $record['discussion'] = $discussion->id;
1640 $record['userid'] = $user->id;
1641 $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
1642
1643 $context = context_module::instance($forum->cmid);
1644
1645 $params = array(
1646 'context' => $context,
1647 'objectid' => $post->id,
1648 'other' => array('discussionid' => $discussion->id, 'forumid' => $forum->id, 'forumtype' => $forum->type)
1649 );
1650
1651 $event = \mod_forum\event\post_deleted::create($params);
1652
1653 // Trigger and capture the event.
1654 $sink = $this->redirectEvents();
1655 $event->trigger();
1656 $events = $sink->get_events();
1657 $this->assertCount(1, $events);
1658 $event = reset($events);
1659
1660 // Checking that the event contains the expected values.
1661 $this->assertInstanceOf('\mod_forum\event\post_deleted', $event);
1662 $this->assertEquals($context, $event->get_context());
1663 $expected = array($course->id, 'forum', 'delete post', "view.php?f={$forum->id}", $post->id, $forum->cmid);
1664 $this->assertEventLegacyLogData($expected, $event);
1665 $url = new \moodle_url('/mod/forum/view.php', array('f' => $forum->id));
1666 $this->assertEquals($url, $event->get_url());
a52c04d5
DP
1667 $this->assertEventContextNotUsed($event);
1668
1669 $this->assertNotEmpty($event->get_name());
1670 }
1671
1672 /**
1673 * Ensure post_updated event validates that the postid is set.
1674 */
1675 public function test_post_updated_postid_validation() {
1676 $course = $this->getDataGenerator()->create_course();
1677 $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
1678 $user = $this->getDataGenerator()->create_user();
1679
1680 // Add a discussion.
1681 $record = array();
1682 $record['course'] = $course->id;
1683 $record['forum'] = $forum->id;
1684 $record['userid'] = $user->id;
1685 $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1686
1687 $params = array(
1688 'context' => context_module::instance($forum->cmid),
1689 'other' => array('forumid' => $forum->id, 'forumtype' => $forum->type, 'discussionid' => $discussion->id)
1690 );
1691
1692 $this->setExpectedException('coding_exception', 'objectid must be set to the postid.');
1693 \mod_forum\event\post_updated::create($params);
1694 }
1695
1696 /**
1697 * Ensure post_updated event validates that the discussionid is set.
1698 */
1699 public function test_post_updated_discussionid_validation() {
1700 $course = $this->getDataGenerator()->create_course();
1701 $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
1702 $user = $this->getDataGenerator()->create_user();
1703
1704 // Add a discussion.
1705 $record = array();
1706 $record['course'] = $course->id;
1707 $record['forum'] = $forum->id;
1708 $record['userid'] = $user->id;
1709 $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1710
1711 // Add a post.
1712 $record = array();
1713 $record['discussion'] = $discussion->id;
1714 $record['userid'] = $user->id;
1715 $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
1716
1717 $params = array(
1718 'context' => context_module::instance($forum->cmid),
1719 'objectid' => $post->id,
1720 'other' => array('forumid' => $forum->id, 'forumtype' => $forum->type)
1721 );
1722
1723 $this->setExpectedException('coding_exception', 'discussionid must be set in other.');
1724 \mod_forum\event\post_updated::create($params);
1725 }
1726
1727 /**
1728 * Ensure post_updated event validates that the forumid is set.
1729 */
1730 public function test_post_updated_forumid_validation() {
1731 $course = $this->getDataGenerator()->create_course();
1732 $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
1733 $user = $this->getDataGenerator()->create_user();
1734
1735 // Add a discussion.
1736 $record = array();
1737 $record['course'] = $course->id;
1738 $record['forum'] = $forum->id;
1739 $record['userid'] = $user->id;
1740 $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1741
1742 // Add a post.
1743 $record = array();
1744 $record['discussion'] = $discussion->id;
1745 $record['userid'] = $user->id;
1746 $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
1747
1748 $params = array(
1749 'context' => context_module::instance($forum->cmid),
1750 'objectid' => $post->id,
1751 'other' => array('discussionid' => $discussion->id, 'forumtype' => $forum->type)
1752 );
1753
1754 $this->setExpectedException('coding_exception', 'forumid must be set in other.');
1755 \mod_forum\event\post_updated::create($params);
1756 }
1757
1758 /**
1759 * Ensure post_updated event validates that the forumtype is set.
1760 */
1761 public function test_post_updated_forumtype_validation() {
1762 $course = $this->getDataGenerator()->create_course();
1763 $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
1764 $user = $this->getDataGenerator()->create_user();
1765
1766 // Add a discussion.
1767 $record = array();
1768 $record['course'] = $course->id;
1769 $record['forum'] = $forum->id;
1770 $record['userid'] = $user->id;
1771 $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1772
1773 // Add a post.
1774 $record = array();
1775 $record['discussion'] = $discussion->id;
1776 $record['userid'] = $user->id;
1777 $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
1778
1779 $params = array(
1780 'context' => context_module::instance($forum->cmid),
1781 'objectid' => $post->id,
1782 'other' => array('discussionid' => $discussion->id, 'forumid' => $forum->id)
1783 );
1784
1785 $this->setExpectedException('coding_exception', 'forumtype must be set in other.');
1786 \mod_forum\event\post_updated::create($params);
1787 }
1788
1789 /**
1790 * Ensure post_updated event validates that the contextlevel is correct.
1791 */
1792 public function test_post_updated_context_validation() {
1793 $course = $this->getDataGenerator()->create_course();
1794 $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
1795 $user = $this->getDataGenerator()->create_user();
1796
1797 // Add a discussion.
1798 $record = array();
1799 $record['course'] = $course->id;
1800 $record['forum'] = $forum->id;
1801 $record['userid'] = $user->id;
1802 $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1803
1804 // Add a post.
1805 $record = array();
1806 $record['discussion'] = $discussion->id;
1807 $record['userid'] = $user->id;
1808 $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
1809
1810 $params = array(
1811 'context' => context_system::instance(),
1812 'objectid' => $post->id,
1813 'other' => array('discussionid' => $discussion->id, 'forumid' => $forum->id, 'forumtype' => $forum->type)
1814 );
1815
1816 $this->setExpectedException('coding_exception', 'Context passed must be module context');
1817 \mod_forum\event\post_updated::create($params);
1818 }
1819
1820 /**
1821 * Test post_updated event.
1822 */
1823 public function test_post_updated() {
1824 // Setup test data.
1825 $course = $this->getDataGenerator()->create_course();
1826 $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
1827 $user = $this->getDataGenerator()->create_user();
1828
1829 // Add a discussion.
1830 $record = array();
1831 $record['course'] = $course->id;
1832 $record['forum'] = $forum->id;
1833 $record['userid'] = $user->id;
1834 $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1835
1836 // Add a post.
1837 $record = array();
1838 $record['discussion'] = $discussion->id;
1839 $record['userid'] = $user->id;
1840 $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
1841
1842 $context = context_module::instance($forum->cmid);
1843
1844 $params = array(
1845 'context' => $context,
1846 'objectid' => $post->id,
1847 'other' => array('discussionid' => $discussion->id, 'forumid' => $forum->id, 'forumtype' => $forum->type)
1848 );
1849
1850 $event = \mod_forum\event\post_updated::create($params);
1851
1852 // Trigger and capturing the event.
1853 $sink = $this->redirectEvents();
1854 $event->trigger();
1855 $events = $sink->get_events();
1856 $this->assertCount(1, $events);
1857 $event = reset($events);
1858
1859 // Checking that the event contains the expected values.
1860 $this->assertInstanceOf('\mod_forum\event\post_updated', $event);
1861 $this->assertEquals($context, $event->get_context());
1862 $expected = array($course->id, 'forum', 'update post', "discuss.php?d={$discussion->id}#p{$post->id}",
1863 $post->id, $forum->cmid);
1864 $this->assertEventLegacyLogData($expected, $event);
f548bc4d
JO
1865 $url = new \moodle_url('/mod/forum/discuss.php', array('d' => $discussion->id));
1866 $url->set_anchor('p'.$event->objectid);
1867 $this->assertEquals($url, $event->get_url());
1868 $this->assertEventContextNotUsed($event);
1869
1870 $this->assertNotEmpty($event->get_name());
1871 }
1872
1873 /**
1874 * Test post_updated event.
1875 */
1876 public function test_post_updated_single() {
1877 // Setup test data.
1878 $course = $this->getDataGenerator()->create_course();
1879 $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id, 'type' => 'single'));
1880 $user = $this->getDataGenerator()->create_user();
1881
1882 // Add a discussion.
1883 $record = array();
1884 $record['course'] = $course->id;
1885 $record['forum'] = $forum->id;
1886 $record['userid'] = $user->id;
1887 $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1888
1889 // Add a post.
1890 $record = array();
1891 $record['discussion'] = $discussion->id;
1892 $record['userid'] = $user->id;
1893 $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
1894
1895 $context = context_module::instance($forum->cmid);
1896
1897 $params = array(
1898 'context' => $context,
1899 'objectid' => $post->id,
1900 'other' => array('discussionid' => $discussion->id, 'forumid' => $forum->id, 'forumtype' => $forum->type)
1901 );
1902
1903 $event = \mod_forum\event\post_updated::create($params);
1904
1905 // Trigger and capturing the event.
1906 $sink = $this->redirectEvents();
1907 $event->trigger();
1908 $events = $sink->get_events();
1909 $this->assertCount(1, $events);
1910 $event = reset($events);
1911
1912 // Checking that the event contains the expected values.
1913 $this->assertInstanceOf('\mod_forum\event\post_updated', $event);
1914 $this->assertEquals($context, $event->get_context());
1915 $expected = array($course->id, 'forum', 'update post', "view.php?f={$forum->id}#p{$post->id}",
1916 $post->id, $forum->cmid);
1917 $this->assertEventLegacyLogData($expected, $event);
1918 $url = new \moodle_url('/mod/forum/view.php', array('f' => $forum->id));
1919 $url->set_anchor('p'.$post->id);
1920 $this->assertEquals($url, $event->get_url());
a52c04d5
DP
1921 $this->assertEventContextNotUsed($event);
1922
1923 $this->assertNotEmpty($event->get_name());
1924 }
1925}