MDL-55091 phpunit: Add support for findNodes, as they are now deprecated
[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);
05c0d19a 665 $url = new \moodle_url('/mod/forum/subscribers.php', array('id' => $forum->id));
f548bc4d 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);
05c0d19a 762 $url = new \moodle_url('/mod/forum/subscribers.php', array('id' => $forum->id));
f548bc4d 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
880c5073
AG
1043 $this->setExpectedException('coding_exception',
1044 'Context level must be either CONTEXT_SYSTEM, CONTEXT_COURSE or CONTEXT_USER.');
f3d98189 1045 \mod_forum\event\user_report_viewed::create($params);
a52c04d5
DP
1046 }
1047
1048 /**
f3d98189 1049 * Ensure user_report_viewed event validates that the relateduserid is set.
a52c04d5 1050 */
f3d98189 1051 public function test_user_report_viewed_relateduserid_validation() {
a52c04d5
DP
1052
1053 $params = array(
1054 'context' => context_system::instance(),
1055 'other' => array('reportmode' => 'posts'),
1056 );
1057
02a5a4b2 1058 $this->setExpectedException('coding_exception', 'The \'relateduserid\' must be set.');
f3d98189 1059 \mod_forum\event\user_report_viewed::create($params);
a52c04d5
DP
1060 }
1061
1062 /**
f3d98189 1063 * Test the user_report_viewed event.
a52c04d5 1064 */
f3d98189 1065 public function test_user_report_viewed() {
a52c04d5
DP
1066 // Setup test data.
1067 $user = $this->getDataGenerator()->create_user();
1068 $course = $this->getDataGenerator()->create_course();
1069 $context = context_course::instance($course->id);
1070
1071 $params = array(
1072 'context' => $context,
1073 'relateduserid' => $user->id,
1074 'other' => array('reportmode' => 'discussions'),
1075 );
1076
f3d98189 1077 $event = \mod_forum\event\user_report_viewed::create($params);
a52c04d5
DP
1078
1079 // Trigger and capture the event.
1080 $sink = $this->redirectEvents();
1081 $event->trigger();
1082 $events = $sink->get_events();
1083 $this->assertCount(1, $events);
1084 $event = reset($events);
1085
1086 // Checking that the event contains the expected values.
f3d98189 1087 $this->assertInstanceOf('\mod_forum\event\user_report_viewed', $event);
a52c04d5
DP
1088 $this->assertEquals($context, $event->get_context());
1089 $expected = array($course->id, 'forum', 'user report',
1090 "user.php?id={$user->id}&amp;mode=discussions&amp;course={$course->id}", $user->id);
1091 $this->assertEventLegacyLogData($expected, $event);
1092 $this->assertEventContextNotUsed($event);
1093
1094 $this->assertNotEmpty($event->get_name());
1095 }
1096
1097 /**
1098 * Ensure post_created event validates that the postid is set.
1099 */
1100 public function test_post_created_postid_validation() {
1101 $course = $this->getDataGenerator()->create_course();
1102 $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
1103 $user = $this->getDataGenerator()->create_user();
1104
1105 // Add a discussion.
1106 $record = array();
1107 $record['course'] = $course->id;
1108 $record['forum'] = $forum->id;
1109 $record['userid'] = $user->id;
1110 $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1111
1112 $params = array(
1113 'context' => context_module::instance($forum->cmid),
1114 'other' => array('forumid' => $forum->id, 'forumtype' => $forum->type, 'discussionid' => $discussion->id)
1115 );
1116
a52c04d5
DP
1117 \mod_forum\event\post_created::create($params);
1118 }
1119
1120 /**
1121 * Ensure post_created event validates that the discussionid is set.
1122 */
1123 public function test_post_created_discussionid_validation() {
1124 $course = $this->getDataGenerator()->create_course();
1125 $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
1126 $user = $this->getDataGenerator()->create_user();
1127
1128 // Add a discussion.
1129 $record = array();
1130 $record['course'] = $course->id;
1131 $record['forum'] = $forum->id;
1132 $record['userid'] = $user->id;
1133 $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1134
1135 // Add a post.
1136 $record = array();
1137 $record['discussion'] = $discussion->id;
1138 $record['userid'] = $user->id;
1139 $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
1140
1141 $params = array(
1142 'context' => context_module::instance($forum->cmid),
1143 'objectid' => $post->id,
1144 'other' => array('forumid' => $forum->id, 'forumtype' => $forum->type)
1145 );
1146
02a5a4b2 1147 $this->setExpectedException('coding_exception', 'The \'discussionid\' value must be set in other.');
a52c04d5
DP
1148 \mod_forum\event\post_created::create($params);
1149 }
1150
1151 /**
1152 * Ensure post_created event validates that the forumid is set.
1153 */
1154 public function test_post_created_forumid_validation() {
1155 $course = $this->getDataGenerator()->create_course();
1156 $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
1157 $user = $this->getDataGenerator()->create_user();
1158
1159 // Add a discussion.
1160 $record = array();
1161 $record['course'] = $course->id;
1162 $record['forum'] = $forum->id;
1163 $record['userid'] = $user->id;
1164 $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1165
1166 // Add a post.
1167 $record = array();
1168 $record['discussion'] = $discussion->id;
1169 $record['userid'] = $user->id;
1170 $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
1171
1172 $params = array(
1173 'context' => context_module::instance($forum->cmid),
1174 'objectid' => $post->id,
1175 'other' => array('discussionid' => $discussion->id, 'forumtype' => $forum->type)
1176 );
1177
02a5a4b2 1178 $this->setExpectedException('coding_exception', 'The \'forumid\' value must be set in other.');
a52c04d5
DP
1179 \mod_forum\event\post_created::create($params);
1180 }
1181
1182 /**
1183 * Ensure post_created event validates that the forumtype is set.
1184 */
1185 public function test_post_created_forumtype_validation() {
1186 $course = $this->getDataGenerator()->create_course();
1187 $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
1188 $user = $this->getDataGenerator()->create_user();
1189
1190 // Add a discussion.
1191 $record = array();
1192 $record['course'] = $course->id;
1193 $record['forum'] = $forum->id;
1194 $record['userid'] = $user->id;
1195 $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1196
1197 // Add a post.
1198 $record = array();
1199 $record['discussion'] = $discussion->id;
1200 $record['userid'] = $user->id;
1201 $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
1202
1203 $params = array(
1204 'context' => context_module::instance($forum->cmid),
1205 'objectid' => $post->id,
1206 'other' => array('discussionid' => $discussion->id, 'forumid' => $forum->id)
1207 );
1208
02a5a4b2 1209 $this->setExpectedException('coding_exception', 'The \'forumtype\' value must be set in other.');
a52c04d5
DP
1210 \mod_forum\event\post_created::create($params);
1211 }
1212
1213 /**
1214 * Ensure post_created event validates that the contextlevel is correct.
1215 */
1216 public function test_post_created_context_validation() {
1217 $course = $this->getDataGenerator()->create_course();
1218 $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
1219 $user = $this->getDataGenerator()->create_user();
1220
1221 // Add a discussion.
1222 $record = array();
1223 $record['course'] = $course->id;
1224 $record['forum'] = $forum->id;
1225 $record['userid'] = $user->id;
1226 $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1227
1228 // Add a post.
1229 $record = array();
1230 $record['discussion'] = $discussion->id;
1231 $record['userid'] = $user->id;
1232 $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
1233
1234 $params = array(
1235 'context' => context_system::instance(),
1236 'objectid' => $post->id,
1237 'other' => array('discussionid' => $discussion->id, 'forumid' => $forum->id, 'forumtype' => $forum->type)
1238 );
1239
02a5a4b2 1240 $this->setExpectedException('coding_exception', 'Context level must be CONTEXT_MODULE');
a52c04d5
DP
1241 \mod_forum\event\post_created::create($params);
1242 }
1243
1244 /**
1245 * Test the post_created event.
1246 */
1247 public function test_post_created() {
1248 // Setup test data.
1249 $course = $this->getDataGenerator()->create_course();
1250 $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
1251 $user = $this->getDataGenerator()->create_user();
1252
1253 // Add a discussion.
1254 $record = array();
1255 $record['course'] = $course->id;
1256 $record['forum'] = $forum->id;
1257 $record['userid'] = $user->id;
1258 $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1259
1260 // Add a post.
1261 $record = array();
1262 $record['discussion'] = $discussion->id;
1263 $record['userid'] = $user->id;
1264 $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
1265
1266 $context = context_module::instance($forum->cmid);
1267
1268 $params = array(
1269 'context' => $context,
1270 'objectid' => $post->id,
1271 'other' => array('discussionid' => $discussion->id, 'forumid' => $forum->id, 'forumtype' => $forum->type)
1272 );
1273
1274 $event = \mod_forum\event\post_created::create($params);
1275
1276 // Trigger and capturing the event.
1277 $sink = $this->redirectEvents();
1278 $event->trigger();
1279 $events = $sink->get_events();
1280 $this->assertCount(1, $events);
1281 $event = reset($events);
1282
1283 // Checking that the event contains the expected values.
1284 $this->assertInstanceOf('\mod_forum\event\post_created', $event);
1285 $this->assertEquals($context, $event->get_context());
1286 $expected = array($course->id, 'forum', 'add post', "discuss.php?d={$discussion->id}#p{$post->id}",
1287 $forum->id, $forum->cmid);
1288 $this->assertEventLegacyLogData($expected, $event);
f548bc4d
JO
1289 $url = new \moodle_url('/mod/forum/discuss.php', array('d' => $discussion->id));
1290 $url->set_anchor('p'.$event->objectid);
1291 $this->assertEquals($url, $event->get_url());
1292 $this->assertEventContextNotUsed($event);
1293
1294 $this->assertNotEmpty($event->get_name());
1295 }
1296
1297 /**
1298 * Test the post_created event for a single discussion forum.
1299 */
1300 public function test_post_created_single() {
1301 // Setup test data.
1302 $course = $this->getDataGenerator()->create_course();
1303 $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id, 'type' => 'single'));
1304 $user = $this->getDataGenerator()->create_user();
1305
1306 // Add a discussion.
1307 $record = array();
1308 $record['course'] = $course->id;
1309 $record['forum'] = $forum->id;
1310 $record['userid'] = $user->id;
1311 $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1312
1313 // Add a post.
1314 $record = array();
1315 $record['discussion'] = $discussion->id;
1316 $record['userid'] = $user->id;
1317 $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
1318
1319 $context = context_module::instance($forum->cmid);
1320
1321 $params = array(
1322 'context' => $context,
1323 'objectid' => $post->id,
1324 'other' => array('discussionid' => $discussion->id, 'forumid' => $forum->id, 'forumtype' => $forum->type)
1325 );
1326
1327 $event = \mod_forum\event\post_created::create($params);
1328
1329 // Trigger and capturing the event.
1330 $sink = $this->redirectEvents();
1331 $event->trigger();
1332 $events = $sink->get_events();
1333 $this->assertCount(1, $events);
1334 $event = reset($events);
1335
1336 // Checking that the event contains the expected values.
1337 $this->assertInstanceOf('\mod_forum\event\post_created', $event);
1338 $this->assertEquals($context, $event->get_context());
1339 $expected = array($course->id, 'forum', 'add post', "view.php?f={$forum->id}#p{$post->id}",
1340 $forum->id, $forum->cmid);
1341 $this->assertEventLegacyLogData($expected, $event);
1342 $url = new \moodle_url('/mod/forum/view.php', array('f' => $forum->id));
1343 $url->set_anchor('p'.$event->objectid);
1344 $this->assertEquals($url, $event->get_url());
a52c04d5
DP
1345 $this->assertEventContextNotUsed($event);
1346
1347 $this->assertNotEmpty($event->get_name());
1348 }
1349
1350 /**
1351 * Ensure post_deleted event validates that the postid is set.
1352 */
1353 public function test_post_deleted_postid_validation() {
1354 $course = $this->getDataGenerator()->create_course();
1355 $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
1356 $user = $this->getDataGenerator()->create_user();
1357
1358 // Add a discussion.
1359 $record = array();
1360 $record['course'] = $course->id;
1361 $record['forum'] = $forum->id;
1362 $record['userid'] = $user->id;
1363 $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1364
1365 $params = array(
1366 'context' => context_module::instance($forum->cmid),
1367 'other' => array('forumid' => $forum->id, 'forumtype' => $forum->type, 'discussionid' => $discussion->id)
1368 );
1369
a52c04d5
DP
1370 \mod_forum\event\post_deleted::create($params);
1371 }
1372
1373 /**
1374 * Ensure post_deleted event validates that the discussionid is set.
1375 */
1376 public function test_post_deleted_discussionid_validation() {
1377 $course = $this->getDataGenerator()->create_course();
1378 $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
1379 $user = $this->getDataGenerator()->create_user();
1380
1381 // Add a discussion.
1382 $record = array();
1383 $record['course'] = $course->id;
1384 $record['forum'] = $forum->id;
1385 $record['userid'] = $user->id;
1386 $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1387
1388 // Add a post.
1389 $record = array();
1390 $record['discussion'] = $discussion->id;
1391 $record['userid'] = $user->id;
1392 $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
1393
1394 $params = array(
1395 'context' => context_module::instance($forum->cmid),
1396 'objectid' => $post->id,
1397 'other' => array('forumid' => $forum->id, 'forumtype' => $forum->type)
1398 );
1399
02a5a4b2 1400 $this->setExpectedException('coding_exception', 'The \'discussionid\' value must be set in other.');
a52c04d5
DP
1401 \mod_forum\event\post_deleted::create($params);
1402 }
1403
1404 /**
1405 * Ensure post_deleted event validates that the forumid is set.
1406 */
1407 public function test_post_deleted_forumid_validation() {
1408 $course = $this->getDataGenerator()->create_course();
1409 $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
1410 $user = $this->getDataGenerator()->create_user();
1411
1412 // Add a discussion.
1413 $record = array();
1414 $record['course'] = $course->id;
1415 $record['forum'] = $forum->id;
1416 $record['userid'] = $user->id;
1417 $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1418
1419 // Add a post.
1420 $record = array();
1421 $record['discussion'] = $discussion->id;
1422 $record['userid'] = $user->id;
1423 $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
1424
1425 $params = array(
1426 'context' => context_module::instance($forum->cmid),
1427 'objectid' => $post->id,
1428 'other' => array('discussionid' => $discussion->id, 'forumtype' => $forum->type)
1429 );
1430
02a5a4b2 1431 $this->setExpectedException('coding_exception', 'The \'forumid\' value must be set in other.');
a52c04d5
DP
1432 \mod_forum\event\post_deleted::create($params);
1433 }
1434
1435 /**
1436 * Ensure post_deleted event validates that the forumtype is set.
1437 */
1438 public function test_post_deleted_forumtype_validation() {
1439 $course = $this->getDataGenerator()->create_course();
1440 $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
1441 $user = $this->getDataGenerator()->create_user();
1442
1443 // Add a discussion.
1444 $record = array();
1445 $record['course'] = $course->id;
1446 $record['forum'] = $forum->id;
1447 $record['userid'] = $user->id;
1448 $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1449
1450 // Add a post.
1451 $record = array();
1452 $record['discussion'] = $discussion->id;
1453 $record['userid'] = $user->id;
1454 $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
1455
1456 $params = array(
1457 'context' => context_module::instance($forum->cmid),
1458 'objectid' => $post->id,
1459 'other' => array('discussionid' => $discussion->id, 'forumid' => $forum->id)
1460 );
1461
02a5a4b2 1462 $this->setExpectedException('coding_exception', 'The \'forumtype\' value must be set in other.');
a52c04d5
DP
1463 \mod_forum\event\post_deleted::create($params);
1464 }
1465
1466 /**
1467 * Ensure post_deleted event validates that the contextlevel is correct.
1468 */
1469 public function test_post_deleted_context_validation() {
1470 $course = $this->getDataGenerator()->create_course();
1471 $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
1472 $user = $this->getDataGenerator()->create_user();
1473
1474 // Add a discussion.
1475 $record = array();
1476 $record['course'] = $course->id;
1477 $record['forum'] = $forum->id;
1478 $record['userid'] = $user->id;
1479 $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1480
1481 // Add a post.
1482 $record = array();
1483 $record['discussion'] = $discussion->id;
1484 $record['userid'] = $user->id;
1485 $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
1486
1487 $params = array(
1488 'context' => context_system::instance(),
1489 'objectid' => $post->id,
1490 'other' => array('discussionid' => $discussion->id, 'forumid' => $forum->id, 'forumtype' => $forum->type)
1491 );
1492
02a5a4b2 1493 $this->setExpectedException('coding_exception', 'Context level must be CONTEXT_MODULE');
a52c04d5
DP
1494 \mod_forum\event\post_deleted::create($params);
1495 }
1496
1497 /**
1498 * Test post_deleted event.
1499 */
1500 public function test_post_deleted() {
fcb30f64
MN
1501 global $DB;
1502
a52c04d5
DP
1503 // Setup test data.
1504 $course = $this->getDataGenerator()->create_course();
1505 $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
1506 $user = $this->getDataGenerator()->create_user();
fcb30f64 1507 $cm = get_coursemodule_from_instance('forum', $forum->id, $forum->course);
a52c04d5
DP
1508
1509 // Add a discussion.
1510 $record = array();
1511 $record['course'] = $course->id;
1512 $record['forum'] = $forum->id;
1513 $record['userid'] = $user->id;
1514 $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1515
fcb30f64
MN
1516 // When creating a discussion we also create a post, so get the post.
1517 $discussionpost = $DB->get_records('forum_posts');
1518 // Will only be one here.
1519 $discussionpost = reset($discussionpost);
1520
1521 // Add a few posts.
a52c04d5
DP
1522 $record = array();
1523 $record['discussion'] = $discussion->id;
1524 $record['userid'] = $user->id;
e7be37b1
MN
1525 $posts = array();
1526 $posts[$discussionpost->id] = $discussionpost;
1527 for ($i = 0; $i < 3; $i++) {
1528 $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
1529 $posts[$post->id] = $post;
1530 }
a52c04d5 1531
fcb30f64 1532 // Delete the last post and capture the event.
e7be37b1 1533 $lastpost = end($posts);
a52c04d5 1534 $sink = $this->redirectEvents();
e7be37b1 1535 forum_delete_post($lastpost, true, $course, $cm, $forum);
a52c04d5
DP
1536 $events = $sink->get_events();
1537 $this->assertCount(1, $events);
1538 $event = reset($events);
1539
fcb30f64 1540 // Check that the events contain the expected values.
a52c04d5 1541 $this->assertInstanceOf('\mod_forum\event\post_deleted', $event);
fcb30f64 1542 $this->assertEquals(context_module::instance($forum->cmid), $event->get_context());
e7be37b1 1543 $expected = array($course->id, 'forum', 'delete post', "discuss.php?d={$discussion->id}", $lastpost->id, $forum->cmid);
a52c04d5 1544 $this->assertEventLegacyLogData($expected, $event);
f548bc4d
JO
1545 $url = new \moodle_url('/mod/forum/discuss.php', array('d' => $discussion->id));
1546 $this->assertEquals($url, $event->get_url());
1547 $this->assertEventContextNotUsed($event);
f548bc4d 1548 $this->assertNotEmpty($event->get_name());
fcb30f64
MN
1549
1550 // Delete the whole discussion and capture the events.
1551 $sink = $this->redirectEvents();
1552 forum_delete_discussion($discussion, true, $course, $cm, $forum);
1553 $events = $sink->get_events();
1554 // We will have 3 events. One for the discussion (creating a discussion creates a post), and two for the posts.
1555 $this->assertCount(3, $events);
1556
1557 // Loop through the events and check they are valid.
e7be37b1
MN
1558 foreach ($events as $event) {
1559 $post = $posts[$event->objectid];
fcb30f64
MN
1560
1561 // Check that the event contains the expected values.
1562 $this->assertInstanceOf('\mod_forum\event\post_deleted', $event);
1563 $this->assertEquals(context_module::instance($forum->cmid), $event->get_context());
1564 $expected = array($course->id, 'forum', 'delete post', "discuss.php?d={$discussion->id}", $post->id, $forum->cmid);
1565 $this->assertEventLegacyLogData($expected, $event);
1566 $url = new \moodle_url('/mod/forum/discuss.php', array('d' => $discussion->id));
1567 $this->assertEquals($url, $event->get_url());
1568 $this->assertEventContextNotUsed($event);
1569 $this->assertNotEmpty($event->get_name());
1570 }
f548bc4d
JO
1571 }
1572
1573 /**
1574 * Test post_deleted event for a single discussion forum.
1575 */
1576 public function test_post_deleted_single() {
1577 // Setup test data.
1578 $course = $this->getDataGenerator()->create_course();
1579 $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id, 'type' => 'single'));
1580 $user = $this->getDataGenerator()->create_user();
1581
1582 // Add a discussion.
1583 $record = array();
1584 $record['course'] = $course->id;
1585 $record['forum'] = $forum->id;
1586 $record['userid'] = $user->id;
1587 $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1588
1589 // Add a post.
1590 $record = array();
1591 $record['discussion'] = $discussion->id;
1592 $record['userid'] = $user->id;
1593 $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
1594
1595 $context = context_module::instance($forum->cmid);
1596
1597 $params = array(
1598 'context' => $context,
1599 'objectid' => $post->id,
1600 'other' => array('discussionid' => $discussion->id, 'forumid' => $forum->id, 'forumtype' => $forum->type)
1601 );
1602
1603 $event = \mod_forum\event\post_deleted::create($params);
1604
1605 // Trigger and capture the event.
1606 $sink = $this->redirectEvents();
1607 $event->trigger();
1608 $events = $sink->get_events();
1609 $this->assertCount(1, $events);
1610 $event = reset($events);
1611
1612 // Checking that the event contains the expected values.
1613 $this->assertInstanceOf('\mod_forum\event\post_deleted', $event);
1614 $this->assertEquals($context, $event->get_context());
1615 $expected = array($course->id, 'forum', 'delete post', "view.php?f={$forum->id}", $post->id, $forum->cmid);
1616 $this->assertEventLegacyLogData($expected, $event);
1617 $url = new \moodle_url('/mod/forum/view.php', array('f' => $forum->id));
1618 $this->assertEquals($url, $event->get_url());
a52c04d5
DP
1619 $this->assertEventContextNotUsed($event);
1620
1621 $this->assertNotEmpty($event->get_name());
1622 }
1623
a52c04d5
DP
1624 /**
1625 * Ensure post_updated event validates that the discussionid is set.
1626 */
1627 public function test_post_updated_discussionid_validation() {
1628 $course = $this->getDataGenerator()->create_course();
1629 $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
1630 $user = $this->getDataGenerator()->create_user();
1631
1632 // Add a discussion.
1633 $record = array();
1634 $record['course'] = $course->id;
1635 $record['forum'] = $forum->id;
1636 $record['userid'] = $user->id;
1637 $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1638
1639 // Add a post.
1640 $record = array();
1641 $record['discussion'] = $discussion->id;
1642 $record['userid'] = $user->id;
1643 $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
1644
1645 $params = array(
1646 'context' => context_module::instance($forum->cmid),
1647 'objectid' => $post->id,
1648 'other' => array('forumid' => $forum->id, 'forumtype' => $forum->type)
1649 );
1650
02a5a4b2 1651 $this->setExpectedException('coding_exception', 'The \'discussionid\' value must be set in other.');
a52c04d5
DP
1652 \mod_forum\event\post_updated::create($params);
1653 }
1654
1655 /**
1656 * Ensure post_updated event validates that the forumid is set.
1657 */
1658 public function test_post_updated_forumid_validation() {
1659 $course = $this->getDataGenerator()->create_course();
1660 $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
1661 $user = $this->getDataGenerator()->create_user();
1662
1663 // Add a discussion.
1664 $record = array();
1665 $record['course'] = $course->id;
1666 $record['forum'] = $forum->id;
1667 $record['userid'] = $user->id;
1668 $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1669
1670 // Add a post.
1671 $record = array();
1672 $record['discussion'] = $discussion->id;
1673 $record['userid'] = $user->id;
1674 $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
1675
1676 $params = array(
1677 'context' => context_module::instance($forum->cmid),
1678 'objectid' => $post->id,
1679 'other' => array('discussionid' => $discussion->id, 'forumtype' => $forum->type)
1680 );
1681
02a5a4b2 1682 $this->setExpectedException('coding_exception', 'The \'forumid\' value must be set in other.');
a52c04d5
DP
1683 \mod_forum\event\post_updated::create($params);
1684 }
1685
1686 /**
1687 * Ensure post_updated event validates that the forumtype is set.
1688 */
1689 public function test_post_updated_forumtype_validation() {
1690 $course = $this->getDataGenerator()->create_course();
1691 $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
1692 $user = $this->getDataGenerator()->create_user();
1693
1694 // Add a discussion.
1695 $record = array();
1696 $record['course'] = $course->id;
1697 $record['forum'] = $forum->id;
1698 $record['userid'] = $user->id;
1699 $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1700
1701 // Add a post.
1702 $record = array();
1703 $record['discussion'] = $discussion->id;
1704 $record['userid'] = $user->id;
1705 $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
1706
1707 $params = array(
1708 'context' => context_module::instance($forum->cmid),
1709 'objectid' => $post->id,
1710 'other' => array('discussionid' => $discussion->id, 'forumid' => $forum->id)
1711 );
1712
02a5a4b2 1713 $this->setExpectedException('coding_exception', 'The \'forumtype\' value must be set in other.');
a52c04d5
DP
1714 \mod_forum\event\post_updated::create($params);
1715 }
1716
1717 /**
1718 * Ensure post_updated event validates that the contextlevel is correct.
1719 */
1720 public function test_post_updated_context_validation() {
1721 $course = $this->getDataGenerator()->create_course();
1722 $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
1723 $user = $this->getDataGenerator()->create_user();
1724
1725 // Add a discussion.
1726 $record = array();
1727 $record['course'] = $course->id;
1728 $record['forum'] = $forum->id;
1729 $record['userid'] = $user->id;
1730 $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1731
1732 // Add a post.
1733 $record = array();
1734 $record['discussion'] = $discussion->id;
1735 $record['userid'] = $user->id;
1736 $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
1737
1738 $params = array(
1739 'context' => context_system::instance(),
1740 'objectid' => $post->id,
1741 'other' => array('discussionid' => $discussion->id, 'forumid' => $forum->id, 'forumtype' => $forum->type)
1742 );
1743
02a5a4b2 1744 $this->setExpectedException('coding_exception', 'Context level must be CONTEXT_MODULE');
a52c04d5
DP
1745 \mod_forum\event\post_updated::create($params);
1746 }
1747
1748 /**
1749 * Test post_updated event.
1750 */
1751 public function test_post_updated() {
1752 // Setup test data.
1753 $course = $this->getDataGenerator()->create_course();
1754 $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
1755 $user = $this->getDataGenerator()->create_user();
1756
1757 // Add a discussion.
1758 $record = array();
1759 $record['course'] = $course->id;
1760 $record['forum'] = $forum->id;
1761 $record['userid'] = $user->id;
1762 $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1763
1764 // Add a post.
1765 $record = array();
1766 $record['discussion'] = $discussion->id;
1767 $record['userid'] = $user->id;
1768 $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
1769
1770 $context = context_module::instance($forum->cmid);
1771
1772 $params = array(
1773 'context' => $context,
1774 'objectid' => $post->id,
1775 'other' => array('discussionid' => $discussion->id, 'forumid' => $forum->id, 'forumtype' => $forum->type)
1776 );
1777
1778 $event = \mod_forum\event\post_updated::create($params);
1779
1780 // Trigger and capturing the event.
1781 $sink = $this->redirectEvents();
1782 $event->trigger();
1783 $events = $sink->get_events();
1784 $this->assertCount(1, $events);
1785 $event = reset($events);
1786
1787 // Checking that the event contains the expected values.
1788 $this->assertInstanceOf('\mod_forum\event\post_updated', $event);
1789 $this->assertEquals($context, $event->get_context());
1790 $expected = array($course->id, 'forum', 'update post', "discuss.php?d={$discussion->id}#p{$post->id}",
1791 $post->id, $forum->cmid);
1792 $this->assertEventLegacyLogData($expected, $event);
f548bc4d
JO
1793 $url = new \moodle_url('/mod/forum/discuss.php', array('d' => $discussion->id));
1794 $url->set_anchor('p'.$event->objectid);
1795 $this->assertEquals($url, $event->get_url());
1796 $this->assertEventContextNotUsed($event);
1797
1798 $this->assertNotEmpty($event->get_name());
1799 }
1800
1801 /**
1802 * Test post_updated event.
1803 */
1804 public function test_post_updated_single() {
1805 // Setup test data.
1806 $course = $this->getDataGenerator()->create_course();
1807 $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id, 'type' => 'single'));
1808 $user = $this->getDataGenerator()->create_user();
1809
1810 // Add a discussion.
1811 $record = array();
1812 $record['course'] = $course->id;
1813 $record['forum'] = $forum->id;
1814 $record['userid'] = $user->id;
1815 $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1816
1817 // Add a post.
1818 $record = array();
1819 $record['discussion'] = $discussion->id;
1820 $record['userid'] = $user->id;
1821 $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
1822
1823 $context = context_module::instance($forum->cmid);
1824
1825 $params = array(
1826 'context' => $context,
1827 'objectid' => $post->id,
1828 'other' => array('discussionid' => $discussion->id, 'forumid' => $forum->id, 'forumtype' => $forum->type)
1829 );
1830
1831 $event = \mod_forum\event\post_updated::create($params);
1832
1833 // Trigger and capturing the event.
1834 $sink = $this->redirectEvents();
1835 $event->trigger();
1836 $events = $sink->get_events();
1837 $this->assertCount(1, $events);
1838 $event = reset($events);
1839
1840 // Checking that the event contains the expected values.
1841 $this->assertInstanceOf('\mod_forum\event\post_updated', $event);
1842 $this->assertEquals($context, $event->get_context());
1843 $expected = array($course->id, 'forum', 'update post', "view.php?f={$forum->id}#p{$post->id}",
1844 $post->id, $forum->cmid);
1845 $this->assertEventLegacyLogData($expected, $event);
1846 $url = new \moodle_url('/mod/forum/view.php', array('f' => $forum->id));
1847 $url->set_anchor('p'.$post->id);
1848 $this->assertEquals($url, $event->get_url());
a52c04d5
DP
1849 $this->assertEventContextNotUsed($event);
1850
1851 $this->assertNotEmpty($event->get_name());
1852 }
e3bbfb52
AN
1853
1854 /**
1855 * Test discussion_subscription_created event.
1856 */
1857 public function test_discussion_subscription_created() {
1858 global $CFG;
1859 require_once($CFG->dirroot . '/mod/forum/lib.php');
1860
1861 // Setup test data.
1862 $course = $this->getDataGenerator()->create_course();
1863 $user = $this->getDataGenerator()->create_user();
1864 $this->getDataGenerator()->enrol_user($user->id, $course->id);
1865
1866 $options = array('course' => $course->id, 'forcesubscribe' => FORUM_CHOOSESUBSCRIBE);
1867 $forum = $this->getDataGenerator()->create_module('forum', $options);
1868
1869 // Add a discussion.
1870 $record = array();
1871 $record['course'] = $course->id;
1872 $record['forum'] = $forum->id;
1873 $record['userid'] = $user->id;
1874 $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1875
1876 // Add a post.
1877 $record = array();
1878 $record['discussion'] = $discussion->id;
1879 $record['userid'] = $user->id;
1880 $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
1881
1882 // Trigger and capturing the event.
1883 $sink = $this->redirectEvents();
1884
1885 // Trigger the event by subscribing the user to the forum discussion.
1886 \mod_forum\subscriptions::subscribe_user_to_discussion($user->id, $discussion);
1887
1888 $events = $sink->get_events();
1889 $this->assertCount(1, $events);
1890 $event = reset($events);
1891
1892 // Checking that the event contains the expected values.
1893 $this->assertInstanceOf('\mod_forum\event\discussion_subscription_created', $event);
1894
1895
1896 $cm = get_coursemodule_from_instance('forum', $discussion->forum);
1897 $context = \context_module::instance($cm->id);
1898 $this->assertEquals($context, $event->get_context());
1899
1900 $url = new \moodle_url('/mod/forum/subscribe.php', array(
1901 'id' => $forum->id,
1902 'd' => $discussion->id
1903 ));
1904
1905 $this->assertEquals($url, $event->get_url());
1906 $this->assertEventContextNotUsed($event);
1907 $this->assertNotEmpty($event->get_name());
1908 }
1909
1910 /**
1911 * Test validation of discussion_subscription_created event.
1912 */
1913 public function test_discussion_subscription_created_validation() {
1914 global $CFG, $DB;
1915 require_once($CFG->dirroot . '/mod/forum/lib.php');
1916
1917 // Setup test data.
1918 $course = $this->getDataGenerator()->create_course();
1919 $user = $this->getDataGenerator()->create_user();
1920 $this->getDataGenerator()->enrol_user($user->id, $course->id);
1921
1922 $options = array('course' => $course->id, 'forcesubscribe' => FORUM_CHOOSESUBSCRIBE);
1923 $forum = $this->getDataGenerator()->create_module('forum', $options);
1924
1925 // Add a discussion.
1926 $record = array();
1927 $record['course'] = $course->id;
1928 $record['forum'] = $forum->id;
1929 $record['userid'] = $user->id;
1930 $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1931
1932 // Add a post.
1933 $record = array();
1934 $record['discussion'] = $discussion->id;
1935 $record['userid'] = $user->id;
1936 $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
1937
1938 // The user is not subscribed to the forum. Insert a new discussion subscription.
1939 $subscription = new \stdClass();
1940 $subscription->userid = $user->id;
1941 $subscription->forum = $forum->id;
1942 $subscription->discussion = $discussion->id;
eb451c79 1943 $subscription->preference = time();
e3bbfb52
AN
1944
1945 $subscription->id = $DB->insert_record('forum_discussion_subs', $subscription);
1946
1947 $context = context_module::instance($forum->cmid);
1948
1949 $params = array(
1950 'context' => $context,
1951 'objectid' => $subscription->id,
1952 'relateduserid' => $user->id,
1953 'other' => array(
1954 'forumid' => $forum->id,
1955 'discussion' => $discussion->id,
1956 )
1957 );
1958
1959 $event = \mod_forum\event\discussion_subscription_created::create($params);
1960
1961 // Trigger and capturing the event.
1962 $sink = $this->redirectEvents();
1963 $event->trigger();
1964 $events = $sink->get_events();
1965 $this->assertCount(1, $events);
1966 $event = reset($events);
1967 }
1968
1969 /**
1970 * Test contextlevel validation of discussion_subscription_created event.
1971 */
1972 public function test_discussion_subscription_created_validation_contextlevel() {
1973 global $CFG, $DB;
1974 require_once($CFG->dirroot . '/mod/forum/lib.php');
1975
1976 // Setup test data.
1977 $course = $this->getDataGenerator()->create_course();
1978 $user = $this->getDataGenerator()->create_user();
1979 $this->getDataGenerator()->enrol_user($user->id, $course->id);
1980
1981 $options = array('course' => $course->id, 'forcesubscribe' => FORUM_CHOOSESUBSCRIBE);
1982 $forum = $this->getDataGenerator()->create_module('forum', $options);
1983
1984 // Add a discussion.
1985 $record = array();
1986 $record['course'] = $course->id;
1987 $record['forum'] = $forum->id;
1988 $record['userid'] = $user->id;
1989 $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1990
1991 // Add a post.
1992 $record = array();
1993 $record['discussion'] = $discussion->id;
1994 $record['userid'] = $user->id;
1995 $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
1996
1997 // The user is not subscribed to the forum. Insert a new discussion subscription.
1998 $subscription = new \stdClass();
1999 $subscription->userid = $user->id;
2000 $subscription->forum = $forum->id;
2001 $subscription->discussion = $discussion->id;
eb451c79 2002 $subscription->preference = time();
e3bbfb52
AN
2003
2004 $subscription->id = $DB->insert_record('forum_discussion_subs', $subscription);
2005
2006 $context = context_module::instance($forum->cmid);
2007
2008 $params = array(
2009 'context' => \context_course::instance($course->id),
2010 'objectid' => $subscription->id,
2011 'relateduserid' => $user->id,
2012 'other' => array(
2013 'forumid' => $forum->id,
2014 'discussion' => $discussion->id,
2015 )
2016 );
2017
2018 // Without an invalid context.
2019 $this->setExpectedException('coding_exception', 'Context level must be CONTEXT_MODULE.');
2020 \mod_forum\event\discussion_subscription_created::create($params);
2021 }
2022
2023 /**
2024 * Test discussion validation of discussion_subscription_created event.
2025 */
2026 public function test_discussion_subscription_created_validation_discussion() {
2027 global $CFG, $DB;
2028 require_once($CFG->dirroot . '/mod/forum/lib.php');
2029
2030 // Setup test data.
2031 $course = $this->getDataGenerator()->create_course();
2032 $user = $this->getDataGenerator()->create_user();
2033 $this->getDataGenerator()->enrol_user($user->id, $course->id);
2034
2035 $options = array('course' => $course->id, 'forcesubscribe' => FORUM_CHOOSESUBSCRIBE);
2036 $forum = $this->getDataGenerator()->create_module('forum', $options);
2037
2038 // Add a discussion.
2039 $record = array();
2040 $record['course'] = $course->id;
2041 $record['forum'] = $forum->id;
2042 $record['userid'] = $user->id;
2043 $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
2044
2045 // Add a post.
2046 $record = array();
2047 $record['discussion'] = $discussion->id;
2048 $record['userid'] = $user->id;
2049 $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
2050
2051 // The user is not subscribed to the forum. Insert a new discussion subscription.
2052 $subscription = new \stdClass();
2053 $subscription->userid = $user->id;
2054 $subscription->forum = $forum->id;
2055 $subscription->discussion = $discussion->id;
eb451c79 2056 $subscription->preference = time();
e3bbfb52
AN
2057
2058 $subscription->id = $DB->insert_record('forum_discussion_subs', $subscription);
2059
2060 // Without the discussion.
2061 $params = array(
2062 'context' => context_module::instance($forum->cmid),
2063 'objectid' => $subscription->id,
2064 'relateduserid' => $user->id,
2065 'other' => array(
2066 'forumid' => $forum->id,
2067 )
2068 );
2069
2070 $this->setExpectedException('coding_exception', "The 'discussion' value must be set in other.");
2071 \mod_forum\event\discussion_subscription_created::create($params);
2072 }
2073
2074 /**
2075 * Test forumid validation of discussion_subscription_created event.
2076 */
2077 public function test_discussion_subscription_created_validation_forumid() {
2078 global $CFG, $DB;
2079 require_once($CFG->dirroot . '/mod/forum/lib.php');
2080
2081 // Setup test data.
2082 $course = $this->getDataGenerator()->create_course();
2083 $user = $this->getDataGenerator()->create_user();
2084 $this->getDataGenerator()->enrol_user($user->id, $course->id);
2085
2086 $options = array('course' => $course->id, 'forcesubscribe' => FORUM_CHOOSESUBSCRIBE);
2087 $forum = $this->getDataGenerator()->create_module('forum', $options);
2088
2089 // Add a discussion.
2090 $record = array();
2091 $record['course'] = $course->id;
2092 $record['forum'] = $forum->id;
2093 $record['userid'] = $user->id;
2094 $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
2095
2096 // Add a post.
2097 $record = array();
2098 $record['discussion'] = $discussion->id;
2099 $record['userid'] = $user->id;
2100 $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
2101
2102 // The user is not subscribed to the forum. Insert a new discussion subscription.
2103 $subscription = new \stdClass();
2104 $subscription->userid = $user->id;
2105 $subscription->forum = $forum->id;
2106 $subscription->discussion = $discussion->id;
eb451c79 2107 $subscription->preference = time();
e3bbfb52
AN
2108
2109 $subscription->id = $DB->insert_record('forum_discussion_subs', $subscription);
2110
2111 // Without the forumid.
2112 $params = array(
2113 'context' => context_module::instance($forum->cmid),
2114 'objectid' => $subscription->id,
2115 'relateduserid' => $user->id,
2116 'other' => array(
2117 'discussion' => $discussion->id,
2118 )
2119 );
2120
2121 $this->setExpectedException('coding_exception', "The 'forumid' value must be set in other.");
2122 \mod_forum\event\discussion_subscription_created::create($params);
2123 }
2124
2125 /**
2126 * Test relateduserid validation of discussion_subscription_created event.
2127 */
2128 public function test_discussion_subscription_created_validation_relateduserid() {
2129 global $CFG, $DB;
2130 require_once($CFG->dirroot . '/mod/forum/lib.php');
2131
2132 // Setup test data.
2133 $course = $this->getDataGenerator()->create_course();
2134 $user = $this->getDataGenerator()->create_user();
2135 $this->getDataGenerator()->enrol_user($user->id, $course->id);
2136
2137 $options = array('course' => $course->id, 'forcesubscribe' => FORUM_CHOOSESUBSCRIBE);
2138 $forum = $this->getDataGenerator()->create_module('forum', $options);
2139
2140 // Add a discussion.
2141 $record = array();
2142 $record['course'] = $course->id;
2143 $record['forum'] = $forum->id;
2144 $record['userid'] = $user->id;
2145 $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
2146
2147 // Add a post.
2148 $record = array();
2149 $record['discussion'] = $discussion->id;
2150 $record['userid'] = $user->id;
2151 $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
2152
2153 // The user is not subscribed to the forum. Insert a new discussion subscription.
2154 $subscription = new \stdClass();
2155 $subscription->userid = $user->id;
2156 $subscription->forum = $forum->id;
2157 $subscription->discussion = $discussion->id;
eb451c79 2158 $subscription->preference = time();
e3bbfb52
AN
2159
2160 $subscription->id = $DB->insert_record('forum_discussion_subs', $subscription);
2161
2162 $context = context_module::instance($forum->cmid);
2163
2164 // Without the relateduserid.
2165 $params = array(
2166 'context' => context_module::instance($forum->cmid),
2167 'objectid' => $subscription->id,
2168 'other' => array(
2169 'forumid' => $forum->id,
2170 'discussion' => $discussion->id,
2171 )
2172 );
2173
2174 $this->setExpectedException('coding_exception', "The 'relateduserid' must be set.");
2175 \mod_forum\event\discussion_subscription_created::create($params);
2176 }
2177
2178 /**
2179 * Test discussion_subscription_deleted event.
2180 */
2181 public function test_discussion_subscription_deleted() {
2182 global $CFG;
2183 require_once($CFG->dirroot . '/mod/forum/lib.php');
2184
2185 // Setup test data.
2186 $course = $this->getDataGenerator()->create_course();
2187 $user = $this->getDataGenerator()->create_user();
2188 $this->getDataGenerator()->enrol_user($user->id, $course->id);
2189
2190 $options = array('course' => $course->id, 'forcesubscribe' => FORUM_INITIALSUBSCRIBE);
2191 $forum = $this->getDataGenerator()->create_module('forum', $options);
2192
2193 // Add a discussion.
2194 $record = array();
2195 $record['course'] = $course->id;
2196 $record['forum'] = $forum->id;
2197 $record['userid'] = $user->id;
2198 $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
2199
2200 // Add a post.
2201 $record = array();
2202 $record['discussion'] = $discussion->id;
2203 $record['userid'] = $user->id;
2204 $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
2205
2206 // Trigger and capturing the event.
2207 $sink = $this->redirectEvents();
2208
2209 // Trigger the event by unsubscribing the user to the forum discussion.
2210 \mod_forum\subscriptions::unsubscribe_user_from_discussion($user->id, $discussion);
2211
2212 $events = $sink->get_events();
2213 $this->assertCount(1, $events);
2214 $event = reset($events);
2215
2216 // Checking that the event contains the expected values.
2217 $this->assertInstanceOf('\mod_forum\event\discussion_subscription_deleted', $event);
2218
2219
2220 $cm = get_coursemodule_from_instance('forum', $discussion->forum);
2221 $context = \context_module::instance($cm->id);
2222 $this->assertEquals($context, $event->get_context());
2223
2224 $url = new \moodle_url('/mod/forum/subscribe.php', array(
2225 'id' => $forum->id,
2226 'd' => $discussion->id
2227 ));
2228
2229 $this->assertEquals($url, $event->get_url());
2230 $this->assertEventContextNotUsed($event);
2231 $this->assertNotEmpty($event->get_name());
2232 }
2233
2234 /**
2235 * Test validation of discussion_subscription_deleted event.
2236 */
2237 public function test_discussion_subscription_deleted_validation() {
2238 global $CFG, $DB;
2239 require_once($CFG->dirroot . '/mod/forum/lib.php');
2240
2241 // Setup test data.
2242 $course = $this->getDataGenerator()->create_course();
2243 $user = $this->getDataGenerator()->create_user();
2244 $this->getDataGenerator()->enrol_user($user->id, $course->id);
2245
2246 $options = array('course' => $course->id, 'forcesubscribe' => FORUM_INITIALSUBSCRIBE);
2247 $forum = $this->getDataGenerator()->create_module('forum', $options);
2248
2249 // Add a discussion.
2250 $record = array();
2251 $record['course'] = $course->id;
2252 $record['forum'] = $forum->id;
2253 $record['userid'] = $user->id;
2254 $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
2255
2256 // Add a post.
2257 $record = array();
2258 $record['discussion'] = $discussion->id;
2259 $record['userid'] = $user->id;
2260 $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
2261
2262 // The user is not subscribed to the forum. Insert a new discussion subscription.
2263 $subscription = new \stdClass();
2264 $subscription->userid = $user->id;
2265 $subscription->forum = $forum->id;
2266 $subscription->discussion = $discussion->id;
2267 $subscription->preference = \mod_forum\subscriptions::FORUM_DISCUSSION_UNSUBSCRIBED;
2268
2269 $subscription->id = $DB->insert_record('forum_discussion_subs', $subscription);
2270
2271 $context = context_module::instance($forum->cmid);
2272
2273 $params = array(
2274 'context' => $context,
2275 'objectid' => $subscription->id,
2276 'relateduserid' => $user->id,
2277 'other' => array(
2278 'forumid' => $forum->id,
2279 'discussion' => $discussion->id,
2280 )
2281 );
2282
2283 $event = \mod_forum\event\discussion_subscription_deleted::create($params);
2284
2285 // Trigger and capturing the event.
2286 $sink = $this->redirectEvents();
2287 $event->trigger();
2288 $events = $sink->get_events();
2289 $this->assertCount(1, $events);
2290 $event = reset($events);
2291
2292 // Without an invalid context.
2293 $params['context'] = \context_course::instance($course->id);
2294 $this->setExpectedException('coding_exception', 'Context level must be CONTEXT_MODULE.');
2295 \mod_forum\event\discussion_deleted::create($params);
2296
2297 // Without the discussion.
2298 unset($params['discussion']);
2299 $this->setExpectedException('coding_exception', 'The \'discussion\' value must be set in other.');
2300 \mod_forum\event\discussion_deleted::create($params);
2301
2302 // Without the forumid.
2303 unset($params['forumid']);
2304 $this->setExpectedException('coding_exception', 'The \'forumid\' value must be set in other.');
2305 \mod_forum\event\discussion_deleted::create($params);
2306
2307 // Without the relateduserid.
2308 unset($params['relateduserid']);
2309 $this->setExpectedException('coding_exception', 'The \'relateduserid\' value must be set in other.');
2310 \mod_forum\event\discussion_deleted::create($params);
2311 }
2312
2313 /**
2314 * Test contextlevel validation of discussion_subscription_deleted event.
2315 */
2316 public function test_discussion_subscription_deleted_validation_contextlevel() {
2317 global $CFG, $DB;
2318 require_once($CFG->dirroot . '/mod/forum/lib.php');
2319
2320 // Setup test data.
2321 $course = $this->getDataGenerator()->create_course();
2322 $user = $this->getDataGenerator()->create_user();
2323 $this->getDataGenerator()->enrol_user($user->id, $course->id);
2324
2325 $options = array('course' => $course->id, 'forcesubscribe' => FORUM_CHOOSESUBSCRIBE);
2326 $forum = $this->getDataGenerator()->create_module('forum', $options);
2327
2328 // Add a discussion.
2329 $record = array();
2330 $record['course'] = $course->id;
2331 $record['forum'] = $forum->id;
2332 $record['userid'] = $user->id;
2333 $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
2334
2335 // Add a post.
2336 $record = array();
2337 $record['discussion'] = $discussion->id;
2338 $record['userid'] = $user->id;
2339 $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
2340
2341 // The user is not subscribed to the forum. Insert a new discussion subscription.
2342 $subscription = new \stdClass();
2343 $subscription->userid = $user->id;
2344 $subscription->forum = $forum->id;
2345 $subscription->discussion = $discussion->id;
eb451c79 2346 $subscription->preference = time();
e3bbfb52
AN
2347
2348 $subscription->id = $DB->insert_record('forum_discussion_subs', $subscription);
2349
2350 $context = context_module::instance($forum->cmid);
2351
2352 $params = array(
2353 'context' => \context_course::instance($course->id),
2354 'objectid' => $subscription->id,
2355 'relateduserid' => $user->id,
2356 'other' => array(
2357 'forumid' => $forum->id,
2358 'discussion' => $discussion->id,
2359 )
2360 );
2361
2362 // Without an invalid context.
2363 $this->setExpectedException('coding_exception', 'Context level must be CONTEXT_MODULE.');
2364 \mod_forum\event\discussion_subscription_deleted::create($params);
2365 }
2366
2367 /**
2368 * Test discussion validation of discussion_subscription_deleted event.
2369 */
2370 public function test_discussion_subscription_deleted_validation_discussion() {
2371 global $CFG, $DB;
2372 require_once($CFG->dirroot . '/mod/forum/lib.php');
2373
2374 // Setup test data.
2375 $course = $this->getDataGenerator()->create_course();
2376 $user = $this->getDataGenerator()->create_user();
2377 $this->getDataGenerator()->enrol_user($user->id, $course->id);
2378
2379 $options = array('course' => $course->id, 'forcesubscribe' => FORUM_CHOOSESUBSCRIBE);
2380 $forum = $this->getDataGenerator()->create_module('forum', $options);
2381
2382 // Add a discussion.
2383 $record = array();
2384 $record['course'] = $course->id;
2385 $record['forum'] = $forum->id;
2386 $record['userid'] = $user->id;
2387 $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
2388
2389 // Add a post.
2390 $record = array();
2391 $record['discussion'] = $discussion->id;
2392 $record['userid'] = $user->id;
2393 $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
2394
2395 // The user is not subscribed to the forum. Insert a new discussion subscription.
2396 $subscription = new \stdClass();
2397 $subscription->userid = $user->id;
2398 $subscription->forum = $forum->id;
2399 $subscription->discussion = $discussion->id;
eb451c79 2400 $subscription->preference = time();
e3bbfb52
AN
2401
2402 $subscription->id = $DB->insert_record('forum_discussion_subs', $subscription);
2403
2404 // Without the discussion.
2405 $params = array(
2406 'context' => context_module::instance($forum->cmid),
2407 'objectid' => $subscription->id,
2408 'relateduserid' => $user->id,
2409 'other' => array(
2410 'forumid' => $forum->id,
2411 )
2412 );
2413
2414 $this->setExpectedException('coding_exception', "The 'discussion' value must be set in other.");
2415 \mod_forum\event\discussion_subscription_deleted::create($params);
2416 }
2417
2418 /**
2419 * Test forumid validation of discussion_subscription_deleted event.
2420 */
2421 public function test_discussion_subscription_deleted_validation_forumid() {
2422 global $CFG, $DB;
2423 require_once($CFG->dirroot . '/mod/forum/lib.php');
2424
2425 // Setup test data.
2426 $course = $this->getDataGenerator()->create_course();
2427 $user = $this->getDataGenerator()->create_user();
2428 $this->getDataGenerator()->enrol_user($user->id, $course->id);
2429
2430 $options = array('course' => $course->id, 'forcesubscribe' => FORUM_CHOOSESUBSCRIBE);
2431 $forum = $this->getDataGenerator()->create_module('forum', $options);
2432
2433 // Add a discussion.
2434 $record = array();
2435 $record['course'] = $course->id;
2436 $record['forum'] = $forum->id;
2437 $record['userid'] = $user->id;
2438 $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
2439
2440 // Add a post.
2441 $record = array();
2442 $record['discussion'] = $discussion->id;
2443 $record['userid'] = $user->id;
2444 $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
2445
2446 // The user is not subscribed to the forum. Insert a new discussion subscription.
2447 $subscription = new \stdClass();
2448 $subscription->userid = $user->id;
2449 $subscription->forum = $forum->id;
2450 $subscription->discussion = $discussion->id;
eb451c79 2451 $subscription->preference = time();
e3bbfb52
AN
2452
2453 $subscription->id = $DB->insert_record('forum_discussion_subs', $subscription);
2454
2455 // Without the forumid.
2456 $params = array(
2457 'context' => context_module::instance($forum->cmid),
2458 'objectid' => $subscription->id,
2459 'relateduserid' => $user->id,
2460 'other' => array(
2461 'discussion' => $discussion->id,
2462 )
2463 );
2464
2465 $this->setExpectedException('coding_exception', "The 'forumid' value must be set in other.");
2466 \mod_forum\event\discussion_subscription_deleted::create($params);
2467 }
2468
2469 /**
2470 * Test relateduserid validation of discussion_subscription_deleted event.
2471 */
2472 public function test_discussion_subscription_deleted_validation_relateduserid() {
2473 global $CFG, $DB;
2474 require_once($CFG->dirroot . '/mod/forum/lib.php');
2475
2476 // Setup test data.
2477 $course = $this->getDataGenerator()->create_course();
2478 $user = $this->getDataGenerator()->create_user();
2479 $this->getDataGenerator()->enrol_user($user->id, $course->id);
2480
2481 $options = array('course' => $course->id, 'forcesubscribe' => FORUM_CHOOSESUBSCRIBE);
2482 $forum = $this->getDataGenerator()->create_module('forum', $options);
2483
2484 // Add a discussion.
2485 $record = array();
2486 $record['course'] = $course->id;
2487 $record['forum'] = $forum->id;
2488 $record['userid'] = $user->id;
2489 $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
2490
2491 // Add a post.
2492 $record = array();
2493 $record['discussion'] = $discussion->id;
2494 $record['userid'] = $user->id;
2495 $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
2496
2497 // The user is not subscribed to the forum. Insert a new discussion subscription.
2498 $subscription = new \stdClass();
2499 $subscription->userid = $user->id;
2500 $subscription->forum = $forum->id;
2501 $subscription->discussion = $discussion->id;
eb451c79 2502 $subscription->preference = time();
e3bbfb52
AN
2503
2504 $subscription->id = $DB->insert_record('forum_discussion_subs', $subscription);
2505
2506 $context = context_module::instance($forum->cmid);
2507
2508 // Without the relateduserid.
2509 $params = array(
2510 'context' => context_module::instance($forum->cmid),
2511 'objectid' => $subscription->id,
2512 'other' => array(
2513 'forumid' => $forum->id,
2514 'discussion' => $discussion->id,
2515 )
2516 );
2517
2518 $this->setExpectedException('coding_exception', "The 'relateduserid' must be set.");
2519 \mod_forum\event\discussion_subscription_deleted::create($params);
2520 }
2521
2522 /**
2523 * Test that the correct context is used in the events when subscribing
2524 * users.
2525 */
2526 public function test_forum_subscription_page_context_valid() {
2527 global $CFG, $PAGE;
2528 require_once($CFG->dirroot . '/mod/forum/lib.php');
2529
2530 // Setup test data.
2531 $course = $this->getDataGenerator()->create_course();
2532 $user = $this->getDataGenerator()->create_user();
2533 $this->getDataGenerator()->enrol_user($user->id, $course->id);
2534
2535 $options = array('course' => $course->id, 'forcesubscribe' => FORUM_CHOOSESUBSCRIBE);
2536 $forum = $this->getDataGenerator()->create_module('forum', $options);
2537 $quiz = $this->getDataGenerator()->create_module('quiz', $options);
2538
2539 // Add a discussion.
2540 $record = array();
2541 $record['course'] = $course->id;
2542 $record['forum'] = $forum->id;
2543 $record['userid'] = $user->id;
2544 $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
2545
2546 // Add a post.
2547 $record = array();
2548 $record['discussion'] = $discussion->id;
2549 $record['userid'] = $user->id;
2550 $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
2551
2552 // Set up the default page event to use this forum.
2553 $PAGE = new moodle_page();
2554 $cm = get_coursemodule_from_instance('forum', $discussion->forum);
2555 $context = \context_module::instance($cm->id);
2556 $PAGE->set_context($context);
2557 $PAGE->set_cm($cm, $course, $forum);
2558
2559 // Trigger and capturing the event.
2560 $sink = $this->redirectEvents();
2561
2562 // Trigger the event by subscribing the user to the forum.
2563 \mod_forum\subscriptions::subscribe_user($user->id, $forum);
2564
2565 $events = $sink->get_events();
2566 $sink->clear();
2567 $this->assertCount(1, $events);
2568 $event = reset($events);
2569
2570 // Checking that the event contains the expected values.
2571 $this->assertInstanceOf('\mod_forum\event\subscription_created', $event);
2572 $this->assertEquals($context, $event->get_context());
2573
2574 // Trigger the event by unsubscribing the user to the forum.
2575 \mod_forum\subscriptions::unsubscribe_user($user->id, $forum);
2576
2577 $events = $sink->get_events();
2578 $sink->clear();
2579 $this->assertCount(1, $events);
2580 $event = reset($events);
2581
2582 // Checking that the event contains the expected values.
2583 $this->assertInstanceOf('\mod_forum\event\subscription_deleted', $event);
2584 $this->assertEquals($context, $event->get_context());
2585
2586 // Trigger the event by subscribing the user to the discussion.
2587 \mod_forum\subscriptions::subscribe_user_to_discussion($user->id, $discussion);
2588
2589 $events = $sink->get_events();
2590 $sink->clear();
2591 $this->assertCount(1, $events);
2592 $event = reset($events);
2593
2594 // Checking that the event contains the expected values.
2595 $this->assertInstanceOf('\mod_forum\event\discussion_subscription_created', $event);
2596 $this->assertEquals($context, $event->get_context());
2597
2598 // Trigger the event by unsubscribing the user from the discussion.
2599 \mod_forum\subscriptions::unsubscribe_user_from_discussion($user->id, $discussion);
2600
2601 $events = $sink->get_events();
2602 $sink->clear();
2603 $this->assertCount(1, $events);
2604 $event = reset($events);
2605
2606 // Checking that the event contains the expected values.
2607 $this->assertInstanceOf('\mod_forum\event\discussion_subscription_deleted', $event);
2608 $this->assertEquals($context, $event->get_context());
2609
2610 // Now try with the context for a different module (quiz).
2611 $PAGE = new moodle_page();
2612 $cm = get_coursemodule_from_instance('quiz', $quiz->id);
2613 $quizcontext = \context_module::instance($cm->id);
2614 $PAGE->set_context($quizcontext);
2615 $PAGE->set_cm($cm, $course, $quiz);
2616
2617 // Trigger and capturing the event.
2618 $sink = $this->redirectEvents();
2619
2620 // Trigger the event by subscribing the user to the forum.
2621 \mod_forum\subscriptions::subscribe_user($user->id, $forum);
2622
2623 $events = $sink->get_events();
2624 $sink->clear();
2625 $this->assertCount(1, $events);
2626 $event = reset($events);
2627
2628 // Checking that the event contains the expected values.
2629 $this->assertInstanceOf('\mod_forum\event\subscription_created', $event);
2630 $this->assertEquals($context, $event->get_context());
2631
2632 // Trigger the event by unsubscribing the user to the forum.
2633 \mod_forum\subscriptions::unsubscribe_user($user->id, $forum);
2634
2635 $events = $sink->get_events();
2636 $sink->clear();
2637 $this->assertCount(1, $events);
2638 $event = reset($events);
2639
2640 // Checking that the event contains the expected values.
2641 $this->assertInstanceOf('\mod_forum\event\subscription_deleted', $event);
2642 $this->assertEquals($context, $event->get_context());
2643
2644 // Trigger the event by subscribing the user to the discussion.
2645 \mod_forum\subscriptions::subscribe_user_to_discussion($user->id, $discussion);
2646
2647 $events = $sink->get_events();
2648 $sink->clear();
2649 $this->assertCount(1, $events);
2650 $event = reset($events);
2651
2652 // Checking that the event contains the expected values.
2653 $this->assertInstanceOf('\mod_forum\event\discussion_subscription_created', $event);
2654 $this->assertEquals($context, $event->get_context());
2655
2656 // Trigger the event by unsubscribing the user from the discussion.
2657 \mod_forum\subscriptions::unsubscribe_user_from_discussion($user->id, $discussion);
2658
2659 $events = $sink->get_events();
2660 $sink->clear();
2661 $this->assertCount(1, $events);
2662 $event = reset($events);
2663
2664 // Checking that the event contains the expected values.
2665 $this->assertInstanceOf('\mod_forum\event\discussion_subscription_deleted', $event);
2666 $this->assertEquals($context, $event->get_context());
2667
2668 // Now try with the course context - the module context should still be used.
2669 $PAGE = new moodle_page();
2670 $coursecontext = \context_course::instance($course->id);
2671 $PAGE->set_context($coursecontext);
2672
2673 // Trigger the event by subscribing the user to the forum.
2674 \mod_forum\subscriptions::subscribe_user($user->id, $forum);
2675
2676 $events = $sink->get_events();
2677 $sink->clear();
2678 $this->assertCount(1, $events);
2679 $event = reset($events);
2680
2681 // Checking that the event contains the expected values.
2682 $this->assertInstanceOf('\mod_forum\event\subscription_created', $event);
2683 $this->assertEquals($context, $event->get_context());
2684
2685 // Trigger the event by unsubscribing the user to the forum.
2686 \mod_forum\subscriptions::unsubscribe_user($user->id, $forum);
2687
2688 $events = $sink->get_events();
2689 $sink->clear();
2690 $this->assertCount(1, $events);
2691 $event = reset($events);
2692
2693 // Checking that the event contains the expected values.
2694 $this->assertInstanceOf('\mod_forum\event\subscription_deleted', $event);
2695 $this->assertEquals($context, $event->get_context());
2696
2697 // Trigger the event by subscribing the user to the discussion.
2698 \mod_forum\subscriptions::subscribe_user_to_discussion($user->id, $discussion);
2699
2700 $events = $sink->get_events();
2701 $sink->clear();
2702 $this->assertCount(1, $events);
2703 $event = reset($events);
2704
2705 // Checking that the event contains the expected values.
2706 $this->assertInstanceOf('\mod_forum\event\discussion_subscription_created', $event);
2707 $this->assertEquals($context, $event->get_context());
2708
2709 // Trigger the event by unsubscribing the user from the discussion.
2710 \mod_forum\subscriptions::unsubscribe_user_from_discussion($user->id, $discussion);
2711
2712 $events = $sink->get_events();
2713 $sink->clear();
2714 $this->assertCount(1, $events);
2715 $event = reset($events);
2716
2717 // Checking that the event contains the expected values.
2718 $this->assertInstanceOf('\mod_forum\event\discussion_subscription_deleted', $event);
2719 $this->assertEquals($context, $event->get_context());
2720
2721 }
fd635686
DM
2722
2723 /**
2724 * Test mod_forum_observer methods.
2725 */
2726 public function test_observers() {
2727 global $DB, $CFG;
2728
2729 require_once($CFG->dirroot . '/mod/forum/lib.php');
2730
2731 $forumgen = $this->getDataGenerator()->get_plugin_generator('mod_forum');
2732
2733 $course = $this->getDataGenerator()->create_course();
2734 $trackedrecord = array('course' => $course->id, 'type' => 'general', 'forcesubscribe' => FORUM_INITIALSUBSCRIBE);
2735 $untrackedrecord = array('course' => $course->id, 'type' => 'general');
2736 $trackedforum = $this->getDataGenerator()->create_module('forum', $trackedrecord);
2737 $untrackedforum = $this->getDataGenerator()->create_module('forum', $untrackedrecord);
2738
2739 // Used functions don't require these settings; adding
2740 // them just in case there are APIs changes in future.
2741 $user = $this->getDataGenerator()->create_user(array(
2742 'maildigest' => 1,
2743 'trackforums' => 1
2744 ));
2745
2746 $manplugin = enrol_get_plugin('manual');
2747 $manualenrol = $DB->get_record('enrol', array('courseid' => $course->id, 'enrol' => 'manual'));
2748 $student = $DB->get_record('role', array('shortname' => 'student'));
2749
2750 // The role_assign observer does it's job adding the forum_subscriptions record.
2751 $manplugin->enrol_user($manualenrol, $user->id, $student->id);
2752
2753 // They are not required, but in a real environment they are supposed to be required;
2754 // adding them just in case there are APIs changes in future.
2755 set_config('forum_trackingtype', 1);
2756 set_config('forum_trackreadposts', 1);
2757
2758 $record = array();
2759 $record['course'] = $course->id;
2760 $record['forum'] = $trackedforum->id;
2761 $record['userid'] = $user->id;
2762 $discussion = $forumgen->create_discussion($record);
2763
2764 $record = array();
2765 $record['discussion'] = $discussion->id;
2766 $record['userid'] = $user->id;
2767 $post = $forumgen->create_post($record);
2768
2769 forum_tp_add_read_record($user->id, $post->id);
2770 forum_set_user_maildigest($trackedforum, 2, $user);
2771 forum_tp_stop_tracking($untrackedforum->id, $user->id);
2772
2773 $this->assertEquals(1, $DB->count_records('forum_subscriptions'));
2774 $this->assertEquals(1, $DB->count_records('forum_digests'));
2775 $this->assertEquals(1, $DB->count_records('forum_track_prefs'));
2776 $this->assertEquals(1, $DB->count_records('forum_read'));
2777
2778 // The course_module_created observer does it's job adding a subscription.
2779 $forumrecord = array('course' => $course->id, 'type' => 'general', 'forcesubscribe' => FORUM_INITIALSUBSCRIBE);
2780 $extraforum = $this->getDataGenerator()->create_module('forum', $forumrecord);
2781 $this->assertEquals(2, $DB->count_records('forum_subscriptions'));
2782
2783 $manplugin->unenrol_user($manualenrol, $user->id);
2784
2785 $this->assertEquals(0, $DB->count_records('forum_digests'));
2786 $this->assertEquals(0, $DB->count_records('forum_subscriptions'));
2787 $this->assertEquals(0, $DB->count_records('forum_track_prefs'));
2788 $this->assertEquals(0, $DB->count_records('forum_read'));
2789 }
2790
a52c04d5 2791}