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