Proyectos de Subversion Moodle

Rev

Rev 1 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 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
 
26
namespace mod_forum\event;
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
 */
36
class events_test extends \advanced_testcase {
37
 
38
    /**
39
     * Tests set up.
40
     */
41
    public function setUp(): void {
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
 
46
        $this->resetAfterTest();
47
    }
48
 
49
    public function tearDown(): void {
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
 
55
    /**
56
     * Ensure course_searched event validates that searchterm is set.
57
     */
11 efrain 58
    public function test_course_searched_searchterm_validation(): void {
1 efrain 59
        $course = $this->getDataGenerator()->create_course();
60
        $coursectx = \context_course::instance($course->id);
61
        $params = array(
62
            'context' => $coursectx,
63
        );
64
 
65
        $this->expectException(\coding_exception::class);
66
        $this->expectExceptionMessage("The 'searchterm' value must be set in other.");
67
        \mod_forum\event\course_searched::create($params);
68
    }
69
 
70
    /**
71
     * Ensure course_searched event validates that context is the correct level.
72
     */
11 efrain 73
    public function test_course_searched_context_validation(): void {
1 efrain 74
        $course = $this->getDataGenerator()->create_course();
75
        $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
76
        $context = \context_module::instance($forum->cmid);
77
        $params = array(
78
            'context' => $context,
79
            'other' => array('searchterm' => 'testing'),
80
        );
81
 
82
        $this->expectException(\coding_exception::class);
83
        $this->expectExceptionMessage('Context level must be CONTEXT_COURSE.');
84
        \mod_forum\event\course_searched::create($params);
85
    }
86
 
87
    /**
88
     * Test course_searched event.
89
     */
11 efrain 90
    public function test_course_searched(): void {
1 efrain 91
 
92
        // Setup test data.
93
        $course = $this->getDataGenerator()->create_course();
94
        $coursectx = \context_course::instance($course->id);
95
        $searchterm = 'testing123';
96
 
97
        $params = array(
98
            'context' => $coursectx,
99
            'other' => array('searchterm' => $searchterm),
100
        );
101
 
102
        // Create event.
103
        $event = \mod_forum\event\course_searched::create($params);
104
 
105
        // Trigger and capture the event.
106
        $sink = $this->redirectEvents();
107
        $event->trigger();
108
        $events = $sink->get_events();
109
        $this->assertCount(1, $events);
110
        $event = reset($events);
111
 
112
         // Checking that the event contains the expected values.
113
        $this->assertInstanceOf('\mod_forum\event\course_searched', $event);
114
        $this->assertEquals($coursectx, $event->get_context());
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
     */
11 efrain 123
    public function test_discussion_created_forumid_validation(): void {
1 efrain 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
 
132
        $this->expectException(\coding_exception::class);
133
        $this->expectExceptionMessage("The 'forumid' value must be set in other.");
134
        \mod_forum\event\discussion_created::create($params);
135
    }
136
 
137
    /**
138
     * Ensure discussion_created event validates that the context is the correct level.
139
     */
11 efrain 140
    public function test_discussion_created_context_validation(): void {
1 efrain 141
        $course = $this->getDataGenerator()->create_course();
142
        $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
143
 
144
        $params = array(
145
            'context' => \context_system::instance(),
146
            'other' => array('forumid' => $forum->id),
147
        );
148
 
149
        $this->expectException(\coding_exception::class);
150
        $this->expectExceptionMessage('Context level must be CONTEXT_MODULE.');
151
        \mod_forum\event\discussion_created::create($params);
152
    }
153
 
154
    /**
155
     * Test discussion_created event.
156
     */
11 efrain 157
    public function test_discussion_created(): void {
1 efrain 158
 
159
        // Setup test data.
160
        $course = $this->getDataGenerator()->create_course();
161
        $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
162
        $user = $this->getDataGenerator()->create_user();
163
 
164
        // Add a discussion.
165
        $record = array();
166
        $record['course'] = $course->id;
167
        $record['forum'] = $forum->id;
168
        $record['userid'] = $user->id;
169
        $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
170
 
171
        $context = \context_module::instance($forum->cmid);
172
 
173
        $params = array(
174
            'context' => $context,
175
            'objectid' => $discussion->id,
176
            'other' => array('forumid' => $forum->id),
177
        );
178
 
179
        // Create the event.
180
        $event = \mod_forum\event\discussion_created::create($params);
181
 
182
        // Trigger and capturing the event.
183
        $sink = $this->redirectEvents();
184
        $event->trigger();
185
        $events = $sink->get_events();
186
        $this->assertCount(1, $events);
187
        $event = reset($events);
188
 
189
        // Check that the event contains the expected values.
190
        $this->assertInstanceOf('\mod_forum\event\discussion_created', $event);
191
        $this->assertEquals($context, $event->get_context());
192
        $this->assertEventContextNotUsed($event);
193
 
194
        $this->assertNotEmpty($event->get_name());
195
    }
196
 
197
    /**
198
     * Ensure discussion_updated event validates that forumid is set.
199
     */
11 efrain 200
    public function test_discussion_updated_forumid_validation(): void {
1 efrain 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
 
209
        $this->expectException(\coding_exception::class);
210
        $this->expectExceptionMessage("The 'forumid' value must be set in other.");
211
        \mod_forum\event\discussion_updated::create($params);
212
    }
213
 
214
    /**
215
     * Ensure discussion_created event validates that the context is the correct level.
216
     */
11 efrain 217
    public function test_discussion_updated_context_validation(): void {
1 efrain 218
        $course = $this->getDataGenerator()->create_course();
219
        $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
220
 
221
        $params = array(
222
            'context' => \context_system::instance(),
223
            'other' => array('forumid' => $forum->id),
224
        );
225
 
226
        $this->expectException(\coding_exception::class);
227
        $this->expectExceptionMessage('Context level must be CONTEXT_MODULE.');
228
        \mod_forum\event\discussion_updated::create($params);
229
    }
230
 
231
    /**
232
     * Test discussion_created event.
233
     */
11 efrain 234
    public function test_discussion_updated(): void {
1 efrain 235
 
236
        // Setup test data.
237
        $course = $this->getDataGenerator()->create_course();
238
        $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
239
        $user = $this->getDataGenerator()->create_user();
240
 
241
        // Add a discussion.
242
        $record = array();
243
        $record['course'] = $course->id;
244
        $record['forum'] = $forum->id;
245
        $record['userid'] = $user->id;
246
        $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
247
 
248
        $context = \context_module::instance($forum->cmid);
249
 
250
        $params = array(
251
            'context' => $context,
252
            'objectid' => $discussion->id,
253
            'other' => array('forumid' => $forum->id),
254
        );
255
 
256
        // Create the event.
257
        $event = \mod_forum\event\discussion_updated::create($params);
258
 
259
        // Trigger and capturing the event.
260
        $sink = $this->redirectEvents();
261
        $event->trigger();
262
        $events = $sink->get_events();
263
        $this->assertCount(1, $events);
264
        $event = reset($events);
265
 
266
        // Check that the event contains the expected values.
267
        $this->assertInstanceOf('\mod_forum\event\discussion_updated', $event);
268
        $this->assertEquals($context, $event->get_context());
269
        $this->assertEventContextNotUsed($event);
270
 
271
        $this->assertNotEmpty($event->get_name());
272
    }
273
 
274
    /**
275
     * Ensure discussion_deleted event validates that forumid is set.
276
     */
11 efrain 277
    public function test_discussion_deleted_forumid_validation(): void {
1 efrain 278
        $course = $this->getDataGenerator()->create_course();
279
        $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
280
        $context = \context_module::instance($forum->cmid);
281
 
282
        $params = array(
283
            'context' => $context,
284
        );
285
 
286
        $this->expectException(\coding_exception::class);
287
        $this->expectExceptionMessage("The 'forumid' value must be set in other.");
288
        \mod_forum\event\discussion_deleted::create($params);
289
    }
290
 
291
    /**
292
     * Ensure discussion_deleted event validates that context is of the correct level.
293
     */
11 efrain 294
    public function test_discussion_deleted_context_validation(): void {
1 efrain 295
        $course = $this->getDataGenerator()->create_course();
296
        $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
297
 
298
        $params = array(
299
            'context' => \context_system::instance(),
300
            'other' => array('forumid' => $forum->id),
301
        );
302
 
303
        $this->expectException(\coding_exception::class);
304
        $this->expectExceptionMessage('Context level must be CONTEXT_MODULE.');
305
        \mod_forum\event\discussion_deleted::create($params);
306
    }
307
 
308
    /**
309
     * Test discussion_deleted event.
310
     */
11 efrain 311
    public function test_discussion_deleted(): void {
1 efrain 312
 
313
        // Setup test data.
314
        $course = $this->getDataGenerator()->create_course();
315
        $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
316
        $user = $this->getDataGenerator()->create_user();
317
 
318
        // Add a discussion.
319
        $record = array();
320
        $record['course'] = $course->id;
321
        $record['forum'] = $forum->id;
322
        $record['userid'] = $user->id;
323
        $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
324
 
325
        $context = \context_module::instance($forum->cmid);
326
 
327
        $params = array(
328
            'context' => $context,
329
            'objectid' => $discussion->id,
330
            'other' => array('forumid' => $forum->id),
331
        );
332
 
333
        $event = \mod_forum\event\discussion_deleted::create($params);
334
 
335
        // Trigger and capture the event.
336
        $sink = $this->redirectEvents();
337
        $event->trigger();
338
        $events = $sink->get_events();
339
        $this->assertCount(1, $events);
340
        $event = reset($events);
341
 
342
        // Checking that the event contains the expected values.
343
        $this->assertInstanceOf('\mod_forum\event\discussion_deleted', $event);
344
        $this->assertEquals($context, $event->get_context());
345
        $this->assertEventContextNotUsed($event);
346
 
347
        $this->assertNotEmpty($event->get_name());
348
    }
349
 
350
    /**
351
     * Ensure discussion_moved event validates that fromforumid is set.
352
     */
11 efrain 353
    public function test_discussion_moved_fromforumid_validation(): void {
1 efrain 354
        $course = $this->getDataGenerator()->create_course();
355
        $toforum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
356
 
357
        $context = \context_module::instance($toforum->cmid);
358
 
359
        $params = array(
360
            'context' => $context,
361
            'other' => array('toforumid' => $toforum->id)
362
        );
363
 
364
        $this->expectException(\coding_exception::class);
365
        $this->expectExceptionMessage("The 'fromforumid' value must be set in other.");
366
        \mod_forum\event\discussion_moved::create($params);
367
    }
368
 
369
    /**
370
     * Ensure discussion_moved event validates that toforumid is set.
371
     */
11 efrain 372
    public function test_discussion_moved_toforumid_validation(): void {
1 efrain 373
        $course = $this->getDataGenerator()->create_course();
374
        $fromforum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
375
        $toforum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
376
        $context = \context_module::instance($toforum->cmid);
377
 
378
        $params = array(
379
            'context' => $context,
380
            'other' => array('fromforumid' => $fromforum->id)
381
        );
382
 
383
        $this->expectException(\coding_exception::class);
384
        $this->expectExceptionMessage("The 'toforumid' value must be set in other.");
385
        \mod_forum\event\discussion_moved::create($params);
386
    }
387
 
388
    /**
389
     * Ensure discussion_moved event validates that the context level is correct.
390
     */
11 efrain 391
    public function test_discussion_moved_context_validation(): void {
1 efrain 392
        $course = $this->getDataGenerator()->create_course();
393
        $fromforum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
394
        $toforum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
395
        $user = $this->getDataGenerator()->create_user();
396
 
397
        // Add a discussion.
398
        $record = array();
399
        $record['course'] = $course->id;
400
        $record['forum'] = $fromforum->id;
401
        $record['userid'] = $user->id;
402
        $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
403
 
404
        $params = array(
405
            'context' => \context_system::instance(),
406
            'objectid' => $discussion->id,
407
            'other' => array('fromforumid' => $fromforum->id, 'toforumid' => $toforum->id)
408
        );
409
 
410
        $this->expectException(\coding_exception::class);
411
        $this->expectExceptionMessage('Context level must be CONTEXT_MODULE.');
412
        \mod_forum\event\discussion_moved::create($params);
413
    }
414
 
415
    /**
416
     * Test discussion_moved event.
417
     */
11 efrain 418
    public function test_discussion_moved(): void {
1 efrain 419
        // Setup test data.
420
        $course = $this->getDataGenerator()->create_course();
421
        $fromforum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
422
        $toforum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
423
        $user = $this->getDataGenerator()->create_user();
424
 
425
        // Add a discussion.
426
        $record = array();
427
        $record['course'] = $course->id;
428
        $record['forum'] = $fromforum->id;
429
        $record['userid'] = $user->id;
430
        $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
431
 
432
        $context = \context_module::instance($toforum->cmid);
433
 
434
        $params = array(
435
            'context' => $context,
436
            'objectid' => $discussion->id,
437
            'other' => array('fromforumid' => $fromforum->id, 'toforumid' => $toforum->id)
438
        );
439
 
440
        $event = \mod_forum\event\discussion_moved::create($params);
441
 
442
        // Trigger and capture the event.
443
        $sink = $this->redirectEvents();
444
        $event->trigger();
445
        $events = $sink->get_events();
446
        $this->assertCount(1, $events);
447
        $event = reset($events);
448
 
449
        // Checking that the event contains the expected values.
450
        $this->assertInstanceOf('\mod_forum\event\discussion_moved', $event);
451
        $this->assertEquals($context, $event->get_context());
452
        $this->assertEventContextNotUsed($event);
453
 
454
        $this->assertNotEmpty($event->get_name());
455
    }
456
 
457
 
458
    /**
459
     * Ensure discussion_viewed event validates that the contextlevel is correct.
460
     */
11 efrain 461
    public function test_discussion_viewed_context_validation(): void {
1 efrain 462
        $course = $this->getDataGenerator()->create_course();
463
        $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
464
        $user = $this->getDataGenerator()->create_user();
465
 
466
        // Add a discussion.
467
        $record = array();
468
        $record['course'] = $course->id;
469
        $record['forum'] = $forum->id;
470
        $record['userid'] = $user->id;
471
        $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
472
 
473
        $params = array(
474
            'context' => \context_system::instance(),
475
            'objectid' => $discussion->id,
476
        );
477
 
478
        $this->expectException(\coding_exception::class);
479
        $this->expectExceptionMessage('Context level must be CONTEXT_MODULE.');
480
        \mod_forum\event\discussion_viewed::create($params);
481
    }
482
 
483
    /**
484
     * Test discussion_viewed event.
485
     */
11 efrain 486
    public function test_discussion_viewed(): void {
1 efrain 487
        // Setup test data.
488
        $course = $this->getDataGenerator()->create_course();
489
        $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
490
        $user = $this->getDataGenerator()->create_user();
491
 
492
        // Add a discussion.
493
        $record = array();
494
        $record['course'] = $course->id;
495
        $record['forum'] = $forum->id;
496
        $record['userid'] = $user->id;
497
        $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
498
 
499
        $context = \context_module::instance($forum->cmid);
500
 
501
        $params = array(
502
            'context' => $context,
503
            'objectid' => $discussion->id,
504
        );
505
 
506
        $event = \mod_forum\event\discussion_viewed::create($params);
507
 
508
        // Trigger and capture the event.
509
        $sink = $this->redirectEvents();
510
        $event->trigger();
511
        $events = $sink->get_events();
512
        $this->assertCount(1, $events);
513
        $event = reset($events);
514
 
515
        // Checking that the event contains the expected values.
516
        $this->assertInstanceOf('\mod_forum\event\discussion_viewed', $event);
517
        $this->assertEquals($context, $event->get_context());
518
        $this->assertEventContextNotUsed($event);
519
 
520
        $this->assertNotEmpty($event->get_name());
521
    }
522
 
523
    /**
524
     * Ensure course_module_viewed event validates that the contextlevel is correct.
525
     */
11 efrain 526
    public function test_course_module_viewed_context_validation(): void {
1 efrain 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
 
535
        $this->expectException(\coding_exception::class);
536
        $this->expectExceptionMessage('Context level must be CONTEXT_MODULE.');
537
        \mod_forum\event\course_module_viewed::create($params);
538
    }
539
 
540
    /**
541
     * Test the course_module_viewed event.
542
     */
11 efrain 543
    public function test_course_module_viewed(): void {
1 efrain 544
        // Setup test data.
545
        $course = $this->getDataGenerator()->create_course();
546
        $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
547
 
548
        $context = \context_module::instance($forum->cmid);
549
 
550
        $params = array(
551
            'context' => $context,
552
            'objectid' => $forum->id,
553
        );
554
 
555
        $event = \mod_forum\event\course_module_viewed::create($params);
556
 
557
        // Trigger and capture the event.
558
        $sink = $this->redirectEvents();
559
        $event->trigger();
560
        $events = $sink->get_events();
561
        $this->assertCount(1, $events);
562
        $event = reset($events);
563
 
564
        // Checking that the event contains the expected values.
565
        $this->assertInstanceOf('\mod_forum\event\course_module_viewed', $event);
566
        $this->assertEquals($context, $event->get_context());
567
        $url = new \moodle_url('/mod/forum/view.php', array('f' => $forum->id));
568
        $this->assertEquals($url, $event->get_url());
569
        $this->assertEventContextNotUsed($event);
570
 
571
        $this->assertNotEmpty($event->get_name());
572
    }
573
 
574
    /**
575
     * Ensure subscription_created event validates that the forumid is set.
576
     */
11 efrain 577
    public function test_subscription_created_forumid_validation(): void {
1 efrain 578
        $user = $this->getDataGenerator()->create_user();
579
        $course = $this->getDataGenerator()->create_course();
580
        $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
581
 
582
        $params = array(
583
            'context' => \context_module::instance($forum->cmid),
584
            'relateduserid' => $user->id,
585
        );
586
 
587
        $this->expectException(\coding_exception::class);
588
        $this->expectExceptionMessage("The 'forumid' value must be set in other.");
589
        \mod_forum\event\subscription_created::create($params);
590
    }
591
 
592
    /**
593
     * Ensure subscription_created event validates that the relateduserid is set.
594
     */
11 efrain 595
    public function test_subscription_created_relateduserid_validation(): void {
1 efrain 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
 
604
        $this->expectException(\coding_exception::class);
605
        $this->expectExceptionMessage("The 'relateduserid' must be set.");
606
        \mod_forum\event\subscription_created::create($params);
607
    }
608
 
609
    /**
610
     * Ensure subscription_created event validates that the contextlevel is correct.
611
     */
11 efrain 612
    public function test_subscription_created_contextlevel_validation(): void {
1 efrain 613
        $user = $this->getDataGenerator()->create_user();
614
        $course = $this->getDataGenerator()->create_course();
615
        $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
616
 
617
        $params = array(
618
            'context' => \context_system::instance(),
619
            'other' => array('forumid' => $forum->id),
620
            'relateduserid' => $user->id,
621
        );
622
 
623
        $this->expectException(\coding_exception::class);
624
        $this->expectExceptionMessage('Context level must be CONTEXT_MODULE.');
625
        \mod_forum\event\subscription_created::create($params);
626
    }
627
 
628
    /**
629
     * Test the subscription_created event.
630
     */
11 efrain 631
    public function test_subscription_created(): void {
1 efrain 632
        // Setup test data.
633
        $user = $this->getDataGenerator()->create_user();
634
        $course = $this->getDataGenerator()->create_course();
635
        $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
636
        $user = $this->getDataGenerator()->create_user();
637
        $context = \context_module::instance($forum->cmid);
638
 
639
        // Add a subscription.
640
        $record = array();
641
        $record['course'] = $course->id;
642
        $record['forum'] = $forum->id;
643
        $record['userid'] = $user->id;
644
        $subscription = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_subscription($record);
645
 
646
        $params = array(
647
            'context' => $context,
648
            'objectid' => $subscription->id,
649
            'other' => array('forumid' => $forum->id),
650
            'relateduserid' => $user->id,
651
        );
652
 
653
        $event = \mod_forum\event\subscription_created::create($params);
654
 
655
        // Trigger and capturing the event.
656
        $sink = $this->redirectEvents();
657
        $event->trigger();
658
        $events = $sink->get_events();
659
        $this->assertCount(1, $events);
660
        $event = reset($events);
661
 
662
        // Checking that the event contains the expected values.
663
        $this->assertInstanceOf('\mod_forum\event\subscription_created', $event);
664
        $this->assertEquals($context, $event->get_context());
665
        $url = new \moodle_url('/mod/forum/subscribers.php', array('id' => $forum->id));
666
        $this->assertEquals($url, $event->get_url());
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
     */
11 efrain 675
    public function test_subscription_deleted_forumid_validation(): void {
1 efrain 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
 
685
        $this->expectException(\coding_exception::class);
686
        $this->expectExceptionMessage("The 'forumid' value must be set in other.");
687
        \mod_forum\event\subscription_deleted::create($params);
688
    }
689
 
690
    /**
691
     * Ensure subscription_deleted event validates that the relateduserid is set.
692
     */
11 efrain 693
    public function test_subscription_deleted_relateduserid_validation(): void {
1 efrain 694
        $course = $this->getDataGenerator()->create_course();
695
        $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
696
 
697
        $params = array(
698
            'context' => \context_module::instance($forum->cmid),
699
            'objectid' => $forum->id,
700
        );
701
 
702
        $this->expectException(\coding_exception::class);
703
        $this->expectExceptionMessage("The 'relateduserid' must be set.");
704
        \mod_forum\event\subscription_deleted::create($params);
705
    }
706
 
707
    /**
708
     * Ensure subscription_deleted event validates that the contextlevel is correct.
709
     */
11 efrain 710
    public function test_subscription_deleted_contextlevel_validation(): void {
1 efrain 711
        $user = $this->getDataGenerator()->create_user();
712
        $course = $this->getDataGenerator()->create_course();
713
        $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
714
 
715
        $params = array(
716
            'context' => \context_system::instance(),
717
            'other' => array('forumid' => $forum->id),
718
            'relateduserid' => $user->id,
719
        );
720
 
721
        $this->expectException(\coding_exception::class);
722
        $this->expectExceptionMessage('Context level must be CONTEXT_MODULE.');
723
        \mod_forum\event\subscription_deleted::create($params);
724
    }
725
 
726
    /**
727
     * Test the subscription_deleted event.
728
     */
11 efrain 729
    public function test_subscription_deleted(): void {
1 efrain 730
        // Setup test data.
731
        $user = $this->getDataGenerator()->create_user();
732
        $course = $this->getDataGenerator()->create_course();
733
        $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
734
        $user = $this->getDataGenerator()->create_user();
735
        $context = \context_module::instance($forum->cmid);
736
 
737
        // Add a subscription.
738
        $record = array();
739
        $record['course'] = $course->id;
740
        $record['forum'] = $forum->id;
741
        $record['userid'] = $user->id;
742
        $subscription = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_subscription($record);
743
 
744
        $params = array(
745
            'context' => $context,
746
            'objectid' => $subscription->id,
747
            'other' => array('forumid' => $forum->id),
748
            'relateduserid' => $user->id,
749
        );
750
 
751
        $event = \mod_forum\event\subscription_deleted::create($params);
752
 
753
        // Trigger and capturing the event.
754
        $sink = $this->redirectEvents();
755
        $event->trigger();
756
        $events = $sink->get_events();
757
        $this->assertCount(1, $events);
758
        $event = reset($events);
759
 
760
        // Checking that the event contains the expected values.
761
        $this->assertInstanceOf('\mod_forum\event\subscription_deleted', $event);
762
        $this->assertEquals($context, $event->get_context());
763
        $url = new \moodle_url('/mod/forum/subscribers.php', array('id' => $forum->id));
764
        $this->assertEquals($url, $event->get_url());
765
        $this->assertEventContextNotUsed($event);
766
 
767
        $this->assertNotEmpty($event->get_name());
768
    }
769
 
770
    /**
771
     * Ensure readtracking_enabled event validates that the forumid is set.
772
     */
11 efrain 773
    public function test_readtracking_enabled_forumid_validation(): void {
1 efrain 774
        $user = $this->getDataGenerator()->create_user();
775
        $course = $this->getDataGenerator()->create_course();
776
        $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
777
 
778
        $params = array(
779
            'context' => \context_module::instance($forum->cmid),
780
            'relateduserid' => $user->id,
781
        );
782
 
783
        $this->expectException(\coding_exception::class);
784
        $this->expectExceptionMessage("The 'forumid' value must be set in other.");
785
        \mod_forum\event\readtracking_enabled::create($params);
786
    }
787
 
788
    /**
789
     * Ensure readtracking_enabled event validates that the relateduserid is set.
790
     */
11 efrain 791
    public function test_readtracking_enabled_relateduserid_validation(): void {
1 efrain 792
        $course = $this->getDataGenerator()->create_course();
793
        $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
794
 
795
        $params = array(
796
            'context' => \context_module::instance($forum->cmid),
797
            'objectid' => $forum->id,
798
        );
799
 
800
        $this->expectException(\coding_exception::class);
801
        $this->expectExceptionMessage("The 'relateduserid' must be set.");
802
        \mod_forum\event\readtracking_enabled::create($params);
803
    }
804
 
805
    /**
806
     * Ensure readtracking_enabled event validates that the contextlevel is correct.
807
     */
11 efrain 808
    public function test_readtracking_enabled_contextlevel_validation(): void {
1 efrain 809
        $user = $this->getDataGenerator()->create_user();
810
        $course = $this->getDataGenerator()->create_course();
811
        $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
812
 
813
        $params = array(
814
            'context' => \context_system::instance(),
815
            'other' => array('forumid' => $forum->id),
816
            'relateduserid' => $user->id,
817
        );
818
 
819
        $this->expectException(\coding_exception::class);
820
        $this->expectExceptionMessage('Context level must be CONTEXT_MODULE.');
821
        \mod_forum\event\readtracking_enabled::create($params);
822
    }
823
 
824
    /**
825
     * Test the readtracking_enabled event.
826
     */
11 efrain 827
    public function test_readtracking_enabled(): void {
1 efrain 828
        // Setup test data.
829
        $user = $this->getDataGenerator()->create_user();
830
        $course = $this->getDataGenerator()->create_course();
831
        $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
832
        $context = \context_module::instance($forum->cmid);
833
 
834
        $params = array(
835
            'context' => $context,
836
            'other' => array('forumid' => $forum->id),
837
            'relateduserid' => $user->id,
838
        );
839
 
840
        $event = \mod_forum\event\readtracking_enabled::create($params);
841
 
842
        // Trigger and capture the event.
843
        $sink = $this->redirectEvents();
844
        $event->trigger();
845
        $events = $sink->get_events();
846
        $this->assertCount(1, $events);
847
        $event = reset($events);
848
 
849
        // Checking that the event contains the expected values.
850
        $this->assertInstanceOf('\mod_forum\event\readtracking_enabled', $event);
851
        $this->assertEquals($context, $event->get_context());
852
        $url = new \moodle_url('/mod/forum/view.php', array('f' => $forum->id));
853
        $this->assertEquals($url, $event->get_url());
854
        $this->assertEventContextNotUsed($event);
855
 
856
        $this->assertNotEmpty($event->get_name());
857
    }
858
 
859
    /**
860
     *  Ensure readtracking_disabled event validates that the forumid is set.
861
     */
11 efrain 862
    public function test_readtracking_disabled_forumid_validation(): void {
1 efrain 863
        $user = $this->getDataGenerator()->create_user();
864
        $course = $this->getDataGenerator()->create_course();
865
        $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
866
 
867
        $params = array(
868
            'context' => \context_module::instance($forum->cmid),
869
            'relateduserid' => $user->id,
870
        );
871
 
872
        $this->expectException(\coding_exception::class);
873
        $this->expectExceptionMessage("The 'forumid' value must be set in other.");
874
        \mod_forum\event\readtracking_disabled::create($params);
875
    }
876
 
877
    /**
878
     *  Ensure readtracking_disabled event validates that the relateduserid is set.
879
     */
11 efrain 880
    public function test_readtracking_disabled_relateduserid_validation(): void {
1 efrain 881
        $course = $this->getDataGenerator()->create_course();
882
        $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
883
 
884
        $params = array(
885
            'context' => \context_module::instance($forum->cmid),
886
            'objectid' => $forum->id,
887
        );
888
 
889
        $this->expectException(\coding_exception::class);
890
        $this->expectExceptionMessage("The 'relateduserid' must be set.");
891
        \mod_forum\event\readtracking_disabled::create($params);
892
    }
893
 
894
    /**
895
     *  Ensure readtracking_disabled event validates that the contextlevel is correct
896
     */
11 efrain 897
    public function test_readtracking_disabled_contextlevel_validation(): void {
1 efrain 898
        $user = $this->getDataGenerator()->create_user();
899
        $course = $this->getDataGenerator()->create_course();
900
        $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
901
 
902
        $params = array(
903
            'context' => \context_system::instance(),
904
            'other' => array('forumid' => $forum->id),
905
            'relateduserid' => $user->id,
906
        );
907
 
908
        $this->expectException(\coding_exception::class);
909
        $this->expectExceptionMessage('Context level must be CONTEXT_MODULE.');
910
        \mod_forum\event\readtracking_disabled::create($params);
911
    }
912
 
913
    /**
914
     *  Test the readtracking_disabled event.
915
     */
11 efrain 916
    public function test_readtracking_disabled(): void {
1 efrain 917
        // Setup test data.
918
        $user = $this->getDataGenerator()->create_user();
919
        $course = $this->getDataGenerator()->create_course();
920
        $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
921
        $context = \context_module::instance($forum->cmid);
922
 
923
        $params = array(
924
            'context' => $context,
925
            'other' => array('forumid' => $forum->id),
926
            'relateduserid' => $user->id,
927
        );
928
 
929
        $event = \mod_forum\event\readtracking_disabled::create($params);
930
 
931
        // Trigger and capture the event.
932
        $sink = $this->redirectEvents();
933
        $event->trigger();
934
        $events = $sink->get_events();
935
        $this->assertCount(1, $events);
936
        $event = reset($events);
937
 
938
        // Checking that the event contains the expected values.
939
        $this->assertInstanceOf('\mod_forum\event\readtracking_disabled', $event);
940
        $this->assertEquals($context, $event->get_context());
941
        $url = new \moodle_url('/mod/forum/view.php', array('f' => $forum->id));
942
        $this->assertEquals($url, $event->get_url());
943
        $this->assertEventContextNotUsed($event);
944
 
945
        $this->assertNotEmpty($event->get_name());
946
    }
947
 
948
    /**
949
     *  Ensure subscribers_viewed event validates that the forumid is set.
950
     */
11 efrain 951
    public function test_subscribers_viewed_forumid_validation(): void {
1 efrain 952
        $user = $this->getDataGenerator()->create_user();
953
        $course = $this->getDataGenerator()->create_course();
954
        $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
955
 
956
        $params = array(
957
            'context' => \context_module::instance($forum->cmid),
958
            'relateduserid' => $user->id,
959
        );
960
 
961
        $this->expectException(\coding_exception::class);
962
        $this->expectExceptionMessage("The 'forumid' value must be set in other.");
963
        \mod_forum\event\subscribers_viewed::create($params);
964
    }
965
 
966
    /**
967
     *  Ensure subscribers_viewed event validates that the contextlevel is correct.
968
     */
11 efrain 969
    public function test_subscribers_viewed_contextlevel_validation(): void {
1 efrain 970
        $user = $this->getDataGenerator()->create_user();
971
        $course = $this->getDataGenerator()->create_course();
972
        $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
973
 
974
        $params = array(
975
            'context' => \context_system::instance(),
976
            'other' => array('forumid' => $forum->id),
977
            'relateduserid' => $user->id,
978
        );
979
 
980
        $this->expectException(\coding_exception::class);
981
        $this->expectExceptionMessage('Context level must be CONTEXT_MODULE.');
982
        \mod_forum\event\subscribers_viewed::create($params);
983
    }
984
 
985
    /**
986
     *  Test the subscribers_viewed event.
987
     */
11 efrain 988
    public function test_subscribers_viewed(): void {
1 efrain 989
        // Setup test data.
990
        $course = $this->getDataGenerator()->create_course();
991
        $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
992
        $context = \context_module::instance($forum->cmid);
993
 
994
        $params = array(
995
            'context' => $context,
996
            'other' => array('forumid' => $forum->id),
997
        );
998
 
999
        $event = \mod_forum\event\subscribers_viewed::create($params);
1000
 
1001
        // Trigger and capture the event.
1002
        $sink = $this->redirectEvents();
1003
        $event->trigger();
1004
        $events = $sink->get_events();
1005
        $this->assertCount(1, $events);
1006
        $event = reset($events);
1007
 
1008
        // Checking that the event contains the expected values.
1009
        $this->assertInstanceOf('\mod_forum\event\subscribers_viewed', $event);
1010
        $this->assertEquals($context, $event->get_context());
1011
        $this->assertEventContextNotUsed($event);
1012
 
1013
        $this->assertNotEmpty($event->get_name());
1014
    }
1015
 
1016
    /**
1017
     * Ensure user_report_viewed event validates that the reportmode is set.
1018
     */
11 efrain 1019
    public function test_user_report_viewed_reportmode_validation(): void {
1 efrain 1020
        $user = $this->getDataGenerator()->create_user();
1021
        $course = $this->getDataGenerator()->create_course();
1022
 
1023
        $params = array(
1024
            'context' => \context_course::instance($course->id),
1025
            'relateduserid' => $user->id,
1026
        );
1027
 
1028
        $this->expectException(\coding_exception::class);
1029
        $this->expectExceptionMessage("The 'reportmode' value must be set in other.");
1030
        \mod_forum\event\user_report_viewed::create($params);
1031
    }
1032
 
1033
    /**
1034
     * Ensure user_report_viewed event validates that the contextlevel is correct.
1035
     */
11 efrain 1036
    public function test_user_report_viewed_contextlevel_validation(): void {
1 efrain 1037
        $user = $this->getDataGenerator()->create_user();
1038
        $course = $this->getDataGenerator()->create_course();
1039
        $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
1040
 
1041
        $params = array(
1042
            'context' => \context_module::instance($forum->cmid),
1043
            'other' => array('reportmode' => 'posts'),
1044
            'relateduserid' => $user->id,
1045
        );
1046
 
1047
        $this->expectException(\coding_exception::class);
1048
        $this->expectExceptionMessage('Context level must be either CONTEXT_SYSTEM, CONTEXT_COURSE or CONTEXT_USER.');
1049
        \mod_forum\event\user_report_viewed::create($params);
1050
    }
1051
 
1052
    /**
1053
     *  Ensure user_report_viewed event validates that the relateduserid is set.
1054
     */
11 efrain 1055
    public function test_user_report_viewed_relateduserid_validation(): void {
1 efrain 1056
 
1057
        $params = array(
1058
            'context' => \context_system::instance(),
1059
            'other' => array('reportmode' => 'posts'),
1060
        );
1061
 
1062
        $this->expectException(\coding_exception::class);
1063
        $this->expectExceptionMessage("The 'relateduserid' must be set.");
1064
        \mod_forum\event\user_report_viewed::create($params);
1065
    }
1066
 
1067
    /**
1068
     * Test the user_report_viewed event.
1069
     */
11 efrain 1070
    public function test_user_report_viewed(): void {
1 efrain 1071
        // Setup test data.
1072
        $user = $this->getDataGenerator()->create_user();
1073
        $course = $this->getDataGenerator()->create_course();
1074
        $context = \context_course::instance($course->id);
1075
 
1076
        $params = array(
1077
            'context' => $context,
1078
            'relateduserid' => $user->id,
1079
            'other' => array('reportmode' => 'discussions'),
1080
        );
1081
 
1082
        $event = \mod_forum\event\user_report_viewed::create($params);
1083
 
1084
        // Trigger and capture the event.
1085
        $sink = $this->redirectEvents();
1086
        $event->trigger();
1087
        $events = $sink->get_events();
1088
        $this->assertCount(1, $events);
1089
        $event = reset($events);
1090
 
1091
        // Checking that the event contains the expected values.
1092
        $this->assertInstanceOf('\mod_forum\event\user_report_viewed', $event);
1093
        $this->assertEquals($context, $event->get_context());
1094
        $this->assertEventContextNotUsed($event);
1095
 
1096
        $this->assertNotEmpty($event->get_name());
1097
    }
1098
 
1099
    /**
1100
     *  Ensure post_created event validates that the postid is set.
1101
     */
11 efrain 1102
    public function test_post_created_postid_validation(): void {
1 efrain 1103
        $course = $this->getDataGenerator()->create_course();
1104
        $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
1105
        $user = $this->getDataGenerator()->create_user();
1106
 
1107
        // Add a discussion.
1108
        $record = array();
1109
        $record['course'] = $course->id;
1110
        $record['forum'] = $forum->id;
1111
        $record['userid'] = $user->id;
1112
        $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1113
 
1114
        $params = array(
1115
            'context' => \context_module::instance($forum->cmid),
1116
            'other' => array('forumid' => $forum->id, 'forumtype' => $forum->type, 'discussionid' => $discussion->id)
1117
        );
1118
 
1119
        \mod_forum\event\post_created::create($params);
1120
    }
1121
 
1122
    /**
1123
     * Ensure post_created event validates that the discussionid is set.
1124
     */
11 efrain 1125
    public function test_post_created_discussionid_validation(): void {
1 efrain 1126
        $course = $this->getDataGenerator()->create_course();
1127
        $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
1128
        $user = $this->getDataGenerator()->create_user();
1129
 
1130
        // Add a discussion.
1131
        $record = array();
1132
        $record['course'] = $course->id;
1133
        $record['forum'] = $forum->id;
1134
        $record['userid'] = $user->id;
1135
        $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1136
 
1137
        // Add a post.
1138
        $record = array();
1139
        $record['discussion'] = $discussion->id;
1140
        $record['userid'] = $user->id;
1141
        $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
1142
 
1143
        $params = array(
1144
            'context' => \context_module::instance($forum->cmid),
1145
            'objectid' => $post->id,
1146
            'other' => array('forumid' => $forum->id, 'forumtype' => $forum->type)
1147
        );
1148
 
1149
        $this->expectException(\coding_exception::class);
1150
        $this->expectExceptionMessage("The 'discussionid' value must be set in other.");
1151
        \mod_forum\event\post_created::create($params);
1152
    }
1153
 
1154
    /**
1155
     *  Ensure post_created event validates that the forumid is set.
1156
     */
11 efrain 1157
    public function test_post_created_forumid_validation(): void {
1 efrain 1158
        $course = $this->getDataGenerator()->create_course();
1159
        $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
1160
        $user = $this->getDataGenerator()->create_user();
1161
 
1162
        // Add a discussion.
1163
        $record = array();
1164
        $record['course'] = $course->id;
1165
        $record['forum'] = $forum->id;
1166
        $record['userid'] = $user->id;
1167
        $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1168
 
1169
        // Add a post.
1170
        $record = array();
1171
        $record['discussion'] = $discussion->id;
1172
        $record['userid'] = $user->id;
1173
        $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
1174
 
1175
        $params = array(
1176
            'context' => \context_module::instance($forum->cmid),
1177
            'objectid' => $post->id,
1178
            'other' => array('discussionid' => $discussion->id, 'forumtype' => $forum->type)
1179
        );
1180
 
1181
        $this->expectException(\coding_exception::class);
1182
        $this->expectExceptionMessage("The 'forumid' value must be set in other.");
1183
        \mod_forum\event\post_created::create($params);
1184
    }
1185
 
1186
    /**
1187
     * Ensure post_created event validates that the forumtype is set.
1188
     */
11 efrain 1189
    public function test_post_created_forumtype_validation(): void {
1 efrain 1190
        $course = $this->getDataGenerator()->create_course();
1191
        $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
1192
        $user = $this->getDataGenerator()->create_user();
1193
 
1194
        // Add a discussion.
1195
        $record = array();
1196
        $record['course'] = $course->id;
1197
        $record['forum'] = $forum->id;
1198
        $record['userid'] = $user->id;
1199
        $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1200
 
1201
        // Add a post.
1202
        $record = array();
1203
        $record['discussion'] = $discussion->id;
1204
        $record['userid'] = $user->id;
1205
        $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
1206
 
1207
        $params = array(
1208
            'context' => \context_module::instance($forum->cmid),
1209
            'objectid' => $post->id,
1210
            'other' => array('discussionid' => $discussion->id, 'forumid' => $forum->id)
1211
        );
1212
 
1213
        $this->expectException(\coding_exception::class);
1214
        $this->expectExceptionMessage("The 'forumtype' value must be set in other.");
1215
        \mod_forum\event\post_created::create($params);
1216
    }
1217
 
1218
    /**
1219
     *  Ensure post_created event validates that the contextlevel is correct.
1220
     */
11 efrain 1221
    public function test_post_created_context_validation(): void {
1 efrain 1222
        $course = $this->getDataGenerator()->create_course();
1223
        $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
1224
        $user = $this->getDataGenerator()->create_user();
1225
 
1226
        // Add a discussion.
1227
        $record = array();
1228
        $record['course'] = $course->id;
1229
        $record['forum'] = $forum->id;
1230
        $record['userid'] = $user->id;
1231
        $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1232
 
1233
        // Add a post.
1234
        $record = array();
1235
        $record['discussion'] = $discussion->id;
1236
        $record['userid'] = $user->id;
1237
        $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
1238
 
1239
        $params = array(
1240
            'context' => \context_system::instance(),
1241
            'objectid' => $post->id,
1242
            'other' => array('discussionid' => $discussion->id, 'forumid' => $forum->id, 'forumtype' => $forum->type)
1243
        );
1244
 
1245
        $this->expectException(\coding_exception::class);
1246
        $this->expectExceptionMessage('Context level must be CONTEXT_MODULE.');
1247
        \mod_forum\event\post_created::create($params);
1248
    }
1249
 
1250
    /**
1251
     * Test the post_created event.
1252
     */
11 efrain 1253
    public function test_post_created(): void {
1 efrain 1254
        // Setup test data.
1255
        $course = $this->getDataGenerator()->create_course();
1256
        $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
1257
        $user = $this->getDataGenerator()->create_user();
1258
 
1259
        // Add a discussion.
1260
        $record = array();
1261
        $record['course'] = $course->id;
1262
        $record['forum'] = $forum->id;
1263
        $record['userid'] = $user->id;
1264
        $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1265
 
1266
        // Add a post.
1267
        $record = array();
1268
        $record['discussion'] = $discussion->id;
1269
        $record['userid'] = $user->id;
1270
        $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
1271
 
1272
        $context = \context_module::instance($forum->cmid);
1273
 
1274
        $params = array(
1275
            'context' => $context,
1276
            'objectid' => $post->id,
1277
            'other' => array('discussionid' => $discussion->id, 'forumid' => $forum->id, 'forumtype' => $forum->type)
1278
        );
1279
 
1280
        $event = \mod_forum\event\post_created::create($params);
1281
 
1282
        // Trigger and capturing the event.
1283
        $sink = $this->redirectEvents();
1284
        $event->trigger();
1285
        $events = $sink->get_events();
1286
        $this->assertCount(1, $events);
1287
        $event = reset($events);
1288
 
1289
        // Checking that the event contains the expected values.
1290
        $this->assertInstanceOf('\mod_forum\event\post_created', $event);
1291
        $this->assertEquals($context, $event->get_context());
1292
        $url = new \moodle_url('/mod/forum/discuss.php', array('d' => $discussion->id));
1293
        $url->set_anchor('p'.$event->objectid);
1294
        $this->assertEquals($url, $event->get_url());
1295
        $this->assertEventContextNotUsed($event);
1296
 
1297
        $this->assertNotEmpty($event->get_name());
1298
    }
1299
 
1300
    /**
1301
     * Test the post_created event for a single discussion forum.
1302
     */
11 efrain 1303
    public function test_post_created_single(): void {
1 efrain 1304
        // Setup test data.
1305
        $course = $this->getDataGenerator()->create_course();
1306
        $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id, 'type' => 'single'));
1307
        $user = $this->getDataGenerator()->create_user();
1308
 
1309
        // Add a discussion.
1310
        $record = array();
1311
        $record['course'] = $course->id;
1312
        $record['forum'] = $forum->id;
1313
        $record['userid'] = $user->id;
1314
        $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1315
 
1316
        // Add a post.
1317
        $record = array();
1318
        $record['discussion'] = $discussion->id;
1319
        $record['userid'] = $user->id;
1320
        $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
1321
 
1322
        $context = \context_module::instance($forum->cmid);
1323
 
1324
        $params = array(
1325
            'context' => $context,
1326
            'objectid' => $post->id,
1327
            'other' => array('discussionid' => $discussion->id, 'forumid' => $forum->id, 'forumtype' => $forum->type)
1328
        );
1329
 
1330
        $event = \mod_forum\event\post_created::create($params);
1331
 
1332
        // Trigger and capturing the event.
1333
        $sink = $this->redirectEvents();
1334
        $event->trigger();
1335
        $events = $sink->get_events();
1336
        $this->assertCount(1, $events);
1337
        $event = reset($events);
1338
 
1339
        // Checking that the event contains the expected values.
1340
        $this->assertInstanceOf('\mod_forum\event\post_created', $event);
1341
        $this->assertEquals($context, $event->get_context());
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());
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
     */
11 efrain 1353
    public function test_post_deleted_postid_validation(): void {
1 efrain 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
 
1370
        \mod_forum\event\post_deleted::create($params);
1371
    }
1372
 
1373
    /**
1374
     * Ensure post_deleted event validates that the discussionid is set.
1375
     */
11 efrain 1376
    public function test_post_deleted_discussionid_validation(): void {
1 efrain 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
 
1400
        $this->expectException(\coding_exception::class);
1401
        $this->expectExceptionMessage("The 'discussionid' value must be set in other.");
1402
        \mod_forum\event\post_deleted::create($params);
1403
    }
1404
 
1405
    /**
1406
     *  Ensure post_deleted event validates that the forumid is set.
1407
     */
11 efrain 1408
    public function test_post_deleted_forumid_validation(): void {
1 efrain 1409
        $course = $this->getDataGenerator()->create_course();
1410
        $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
1411
        $user = $this->getDataGenerator()->create_user();
1412
 
1413
        // Add a discussion.
1414
        $record = array();
1415
        $record['course'] = $course->id;
1416
        $record['forum'] = $forum->id;
1417
        $record['userid'] = $user->id;
1418
        $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1419
 
1420
        // Add a post.
1421
        $record = array();
1422
        $record['discussion'] = $discussion->id;
1423
        $record['userid'] = $user->id;
1424
        $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
1425
 
1426
        $params = array(
1427
            'context' => \context_module::instance($forum->cmid),
1428
            'objectid' => $post->id,
1429
            'other' => array('discussionid' => $discussion->id, 'forumtype' => $forum->type)
1430
        );
1431
 
1432
        $this->expectException(\coding_exception::class);
1433
        $this->expectExceptionMessage("The 'forumid' value must be set in other.");
1434
        \mod_forum\event\post_deleted::create($params);
1435
    }
1436
 
1437
    /**
1438
     * Ensure post_deleted event validates that the forumtype is set.
1439
     */
11 efrain 1440
    public function test_post_deleted_forumtype_validation(): void {
1 efrain 1441
        $course = $this->getDataGenerator()->create_course();
1442
        $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
1443
        $user = $this->getDataGenerator()->create_user();
1444
 
1445
        // Add a discussion.
1446
        $record = array();
1447
        $record['course'] = $course->id;
1448
        $record['forum'] = $forum->id;
1449
        $record['userid'] = $user->id;
1450
        $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1451
 
1452
        // Add a post.
1453
        $record = array();
1454
        $record['discussion'] = $discussion->id;
1455
        $record['userid'] = $user->id;
1456
        $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
1457
 
1458
        $params = array(
1459
            'context' => \context_module::instance($forum->cmid),
1460
            'objectid' => $post->id,
1461
            'other' => array('discussionid' => $discussion->id, 'forumid' => $forum->id)
1462
        );
1463
 
1464
        $this->expectException(\coding_exception::class);
1465
        $this->expectExceptionMessage("The 'forumtype' value must be set in other.");
1466
        \mod_forum\event\post_deleted::create($params);
1467
    }
1468
 
1469
    /**
1470
     *  Ensure post_deleted event validates that the contextlevel is correct.
1471
     */
11 efrain 1472
    public function test_post_deleted_context_validation(): void {
1 efrain 1473
        $course = $this->getDataGenerator()->create_course();
1474
        $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
1475
        $user = $this->getDataGenerator()->create_user();
1476
 
1477
        // Add a discussion.
1478
        $record = array();
1479
        $record['course'] = $course->id;
1480
        $record['forum'] = $forum->id;
1481
        $record['userid'] = $user->id;
1482
        $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1483
 
1484
        // Add a post.
1485
        $record = array();
1486
        $record['discussion'] = $discussion->id;
1487
        $record['userid'] = $user->id;
1488
        $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
1489
 
1490
        $params = array(
1491
            'context' => \context_system::instance(),
1492
            'objectid' => $post->id,
1493
            'other' => array('discussionid' => $discussion->id, 'forumid' => $forum->id, 'forumtype' => $forum->type)
1494
        );
1495
 
1496
        $this->expectException(\coding_exception::class);
1497
        $this->expectExceptionMessage('Context level must be CONTEXT_MODULE.');
1498
        \mod_forum\event\post_deleted::create($params);
1499
    }
1500
 
1501
    /**
1502
     * Test post_deleted event.
1503
     */
11 efrain 1504
    public function test_post_deleted(): void {
1 efrain 1505
        global $DB;
1506
 
1507
        // Setup test data.
1508
        $course = $this->getDataGenerator()->create_course();
1509
        $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
1510
        $user = $this->getDataGenerator()->create_user();
1511
        $cm = get_coursemodule_from_instance('forum', $forum->id, $forum->course);
1512
 
1513
        // Add a discussion.
1514
        $record = array();
1515
        $record['course'] = $course->id;
1516
        $record['forum'] = $forum->id;
1517
        $record['userid'] = $user->id;
1518
        $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1519
 
1520
        // When creating a discussion we also create a post, so get the post.
1521
        $discussionpost = $DB->get_records('forum_posts');
1522
        // Will only be one here.
1523
        $discussionpost = reset($discussionpost);
1524
 
1525
        // Add a few posts.
1526
        $record = array();
1527
        $record['discussion'] = $discussion->id;
1528
        $record['userid'] = $user->id;
1529
        $posts = array();
1530
        $posts[$discussionpost->id] = $discussionpost;
1531
        for ($i = 0; $i < 3; $i++) {
1532
            $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
1533
            $posts[$post->id] = $post;
1534
        }
1535
 
1536
        // Delete the last post and capture the event.
1537
        $lastpost = end($posts);
1538
        $sink = $this->redirectEvents();
1539
        forum_delete_post($lastpost, true, $course, $cm, $forum);
1540
        $events = $sink->get_events();
1541
        $this->assertCount(1, $events);
1542
        $event = reset($events);
1543
 
1544
        // Check that the events contain the expected values.
1545
        $this->assertInstanceOf('\mod_forum\event\post_deleted', $event);
1546
        $this->assertEquals(\context_module::instance($forum->cmid), $event->get_context());
1547
        $url = new \moodle_url('/mod/forum/discuss.php', array('d' => $discussion->id));
1548
        $this->assertEquals($url, $event->get_url());
1549
        $this->assertEventContextNotUsed($event);
1550
        $this->assertNotEmpty($event->get_name());
1551
 
1552
        // Delete the whole discussion and capture the events.
1553
        $sink = $this->redirectEvents();
1554
        forum_delete_discussion($discussion, true, $course, $cm, $forum);
1555
        $events = $sink->get_events();
1556
        // We will have 4 events. One for the discussion, another one for the discussion topic post, and two for the posts.
1557
        $this->assertCount(4, $events);
1558
 
1559
        // Loop through the events and check they are valid.
1560
        foreach ($events as $event) {
1561
            if ($event instanceof \mod_forum\event\discussion_deleted) {
1562
                // Check that the event contains the expected values.
1563
                $this->assertEquals($event->objectid, $discussion->id);
1564
                $this->assertEquals(\context_module::instance($forum->cmid), $event->get_context());
1565
                $url = new \moodle_url('/mod/forum/view.php', array('id' => $forum->cmid));
1566
                $this->assertEquals($url, $event->get_url());
1567
                $this->assertEventContextNotUsed($event);
1568
                $this->assertNotEmpty($event->get_name());
1569
            } else {
1570
                $post = $posts[$event->objectid];
1571
                // Check that the event contains the expected values.
1572
                $this->assertInstanceOf('\mod_forum\event\post_deleted', $event);
1573
                $this->assertEquals($event->objectid, $post->id);
1574
                $this->assertEquals(\context_module::instance($forum->cmid), $event->get_context());
1575
                $url = new \moodle_url('/mod/forum/discuss.php', array('d' => $discussion->id));
1576
                $this->assertEquals($url, $event->get_url());
1577
                $this->assertEventContextNotUsed($event);
1578
                $this->assertNotEmpty($event->get_name());
1579
            }
1580
        }
1581
    }
1582
 
1583
    /**
1584
     * Test post_deleted event for a single discussion forum.
1585
     */
11 efrain 1586
    public function test_post_deleted_single(): void {
1 efrain 1587
        // Setup test data.
1588
        $course = $this->getDataGenerator()->create_course();
1589
        $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id, 'type' => 'single'));
1590
        $user = $this->getDataGenerator()->create_user();
1591
 
1592
        // Add a discussion.
1593
        $record = array();
1594
        $record['course'] = $course->id;
1595
        $record['forum'] = $forum->id;
1596
        $record['userid'] = $user->id;
1597
        $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1598
 
1599
        // Add a post.
1600
        $record = array();
1601
        $record['discussion'] = $discussion->id;
1602
        $record['userid'] = $user->id;
1603
        $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
1604
 
1605
        $context = \context_module::instance($forum->cmid);
1606
 
1607
        $params = array(
1608
            'context' => $context,
1609
            'objectid' => $post->id,
1610
            'other' => array('discussionid' => $discussion->id, 'forumid' => $forum->id, 'forumtype' => $forum->type)
1611
        );
1612
 
1613
        $event = \mod_forum\event\post_deleted::create($params);
1614
 
1615
        // Trigger and capture the event.
1616
        $sink = $this->redirectEvents();
1617
        $event->trigger();
1618
        $events = $sink->get_events();
1619
        $this->assertCount(1, $events);
1620
        $event = reset($events);
1621
 
1622
        // Checking that the event contains the expected values.
1623
        $this->assertInstanceOf('\mod_forum\event\post_deleted', $event);
1624
        $this->assertEquals($context, $event->get_context());
1625
        $url = new \moodle_url('/mod/forum/view.php', array('f' => $forum->id));
1626
        $this->assertEquals($url, $event->get_url());
1627
        $this->assertEventContextNotUsed($event);
1628
 
1629
        $this->assertNotEmpty($event->get_name());
1630
    }
1631
 
1632
    /**
1633
     * Ensure post_updated event validates that the discussionid is set.
1634
     */
11 efrain 1635
    public function test_post_updated_discussionid_validation(): void {
1 efrain 1636
        $course = $this->getDataGenerator()->create_course();
1637
        $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
1638
        $user = $this->getDataGenerator()->create_user();
1639
 
1640
        // Add a discussion.
1641
        $record = array();
1642
        $record['course'] = $course->id;
1643
        $record['forum'] = $forum->id;
1644
        $record['userid'] = $user->id;
1645
        $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1646
 
1647
        // Add a post.
1648
        $record = array();
1649
        $record['discussion'] = $discussion->id;
1650
        $record['userid'] = $user->id;
1651
        $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
1652
 
1653
        $params = array(
1654
            'context' => \context_module::instance($forum->cmid),
1655
            'objectid' => $post->id,
1656
            'other' => array('forumid' => $forum->id, 'forumtype' => $forum->type)
1657
        );
1658
 
1659
        $this->expectException(\coding_exception::class);
1660
        $this->expectExceptionMessage("The 'discussionid' value must be set in other.");
1661
        \mod_forum\event\post_updated::create($params);
1662
    }
1663
 
1664
    /**
1665
     * Ensure post_updated event validates that the forumid is set.
1666
     */
11 efrain 1667
    public function test_post_updated_forumid_validation(): void {
1 efrain 1668
        $course = $this->getDataGenerator()->create_course();
1669
        $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
1670
        $user = $this->getDataGenerator()->create_user();
1671
 
1672
        // Add a discussion.
1673
        $record = array();
1674
        $record['course'] = $course->id;
1675
        $record['forum'] = $forum->id;
1676
        $record['userid'] = $user->id;
1677
        $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1678
 
1679
        // Add a post.
1680
        $record = array();
1681
        $record['discussion'] = $discussion->id;
1682
        $record['userid'] = $user->id;
1683
        $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
1684
 
1685
        $params = array(
1686
            'context' => \context_module::instance($forum->cmid),
1687
            'objectid' => $post->id,
1688
            'other' => array('discussionid' => $discussion->id, 'forumtype' => $forum->type)
1689
        );
1690
 
1691
        $this->expectException(\coding_exception::class);
1692
        $this->expectExceptionMessage("The 'forumid' value must be set in other.");
1693
        \mod_forum\event\post_updated::create($params);
1694
    }
1695
 
1696
    /**
1697
     * Ensure post_updated event validates that the forumtype is set.
1698
     */
11 efrain 1699
    public function test_post_updated_forumtype_validation(): void {
1 efrain 1700
        $course = $this->getDataGenerator()->create_course();
1701
        $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
1702
        $user = $this->getDataGenerator()->create_user();
1703
 
1704
        // Add a discussion.
1705
        $record = array();
1706
        $record['course'] = $course->id;
1707
        $record['forum'] = $forum->id;
1708
        $record['userid'] = $user->id;
1709
        $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1710
 
1711
        // Add a post.
1712
        $record = array();
1713
        $record['discussion'] = $discussion->id;
1714
        $record['userid'] = $user->id;
1715
        $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
1716
 
1717
        $params = array(
1718
            'context' => \context_module::instance($forum->cmid),
1719
            'objectid' => $post->id,
1720
            'other' => array('discussionid' => $discussion->id, 'forumid' => $forum->id)
1721
        );
1722
 
1723
        $this->expectException(\coding_exception::class);
1724
        $this->expectExceptionMessage("The 'forumtype' value must be set in other.");
1725
        \mod_forum\event\post_updated::create($params);
1726
    }
1727
 
1728
    /**
1729
     *  Ensure post_updated event validates that the contextlevel is correct.
1730
     */
11 efrain 1731
    public function test_post_updated_context_validation(): void {
1 efrain 1732
        $course = $this->getDataGenerator()->create_course();
1733
        $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
1734
        $user = $this->getDataGenerator()->create_user();
1735
 
1736
        // Add a discussion.
1737
        $record = array();
1738
        $record['course'] = $course->id;
1739
        $record['forum'] = $forum->id;
1740
        $record['userid'] = $user->id;
1741
        $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1742
 
1743
        // Add a post.
1744
        $record = array();
1745
        $record['discussion'] = $discussion->id;
1746
        $record['userid'] = $user->id;
1747
        $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
1748
 
1749
        $params = array(
1750
            'context' => \context_system::instance(),
1751
            'objectid' => $post->id,
1752
            'other' => array('discussionid' => $discussion->id, 'forumid' => $forum->id, 'forumtype' => $forum->type)
1753
        );
1754
 
1755
        $this->expectException(\coding_exception::class);
1756
        $this->expectExceptionMessage('Context level must be CONTEXT_MODULE.');
1757
        \mod_forum\event\post_updated::create($params);
1758
    }
1759
 
1760
    /**
1761
     * Test post_updated event.
1762
     */
11 efrain 1763
    public function test_post_updated(): void {
1 efrain 1764
        // Setup test data.
1765
        $course = $this->getDataGenerator()->create_course();
1766
        $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
1767
        $user = $this->getDataGenerator()->create_user();
1768
 
1769
        // Add a discussion.
1770
        $record = array();
1771
        $record['course'] = $course->id;
1772
        $record['forum'] = $forum->id;
1773
        $record['userid'] = $user->id;
1774
        $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1775
 
1776
        // Add a post.
1777
        $record = array();
1778
        $record['discussion'] = $discussion->id;
1779
        $record['userid'] = $user->id;
1780
        $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
1781
 
1782
        $context = \context_module::instance($forum->cmid);
1783
 
1784
        $params = array(
1785
            'context' => $context,
1786
            'objectid' => $post->id,
1787
            'other' => array('discussionid' => $discussion->id, 'forumid' => $forum->id, 'forumtype' => $forum->type)
1788
        );
1789
 
1790
        $event = \mod_forum\event\post_updated::create($params);
1791
 
1792
        // Trigger and capturing the event.
1793
        $sink = $this->redirectEvents();
1794
        $event->trigger();
1795
        $events = $sink->get_events();
1796
        $this->assertCount(1, $events);
1797
        $event = reset($events);
1798
 
1799
        // Checking that the event contains the expected values.
1800
        $this->assertInstanceOf('\mod_forum\event\post_updated', $event);
1801
        $this->assertEquals($context, $event->get_context());
1802
        $url = new \moodle_url('/mod/forum/discuss.php', array('d' => $discussion->id));
1803
        $url->set_anchor('p'.$event->objectid);
1804
        $this->assertEquals($url, $event->get_url());
1805
        $this->assertEventContextNotUsed($event);
1806
 
1807
        $this->assertNotEmpty($event->get_name());
1808
    }
1809
 
1810
    /**
1811
     * Test post_updated event.
1812
     */
11 efrain 1813
    public function test_post_updated_single(): void {
1 efrain 1814
        // Setup test data.
1815
        $course = $this->getDataGenerator()->create_course();
1816
        $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id, 'type' => 'single'));
1817
        $user = $this->getDataGenerator()->create_user();
1818
 
1819
        // Add a discussion.
1820
        $record = array();
1821
        $record['course'] = $course->id;
1822
        $record['forum'] = $forum->id;
1823
        $record['userid'] = $user->id;
1824
        $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1825
 
1826
        // Add a post.
1827
        $record = array();
1828
        $record['discussion'] = $discussion->id;
1829
        $record['userid'] = $user->id;
1830
        $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
1831
 
1832
        $context = \context_module::instance($forum->cmid);
1833
 
1834
        $params = array(
1835
            'context' => $context,
1836
            'objectid' => $post->id,
1837
            'other' => array('discussionid' => $discussion->id, 'forumid' => $forum->id, 'forumtype' => $forum->type)
1838
        );
1839
 
1840
        $event = \mod_forum\event\post_updated::create($params);
1841
 
1842
        // Trigger and capturing the event.
1843
        $sink = $this->redirectEvents();
1844
        $event->trigger();
1845
        $events = $sink->get_events();
1846
        $this->assertCount(1, $events);
1847
        $event = reset($events);
1848
 
1849
        // Checking that the event contains the expected values.
1850
        $this->assertInstanceOf('\mod_forum\event\post_updated', $event);
1851
        $this->assertEquals($context, $event->get_context());
1852
        $url = new \moodle_url('/mod/forum/view.php', array('f' => $forum->id));
1853
        $url->set_anchor('p'.$post->id);
1854
        $this->assertEquals($url, $event->get_url());
1855
        $this->assertEventContextNotUsed($event);
1856
 
1857
        $this->assertNotEmpty($event->get_name());
1858
    }
1859
 
1860
    /**
1861
     * Test discussion_subscription_created event.
1862
     */
11 efrain 1863
    public function test_discussion_subscription_created(): void {
1 efrain 1864
        global $CFG;
1865
        require_once($CFG->dirroot . '/mod/forum/lib.php');
1866
 
1867
        // Setup test data.
1868
        $course = $this->getDataGenerator()->create_course();
1869
        $user = $this->getDataGenerator()->create_user();
1870
        $this->getDataGenerator()->enrol_user($user->id, $course->id);
1871
 
1872
        $options = array('course' => $course->id, 'forcesubscribe' => FORUM_CHOOSESUBSCRIBE);
1873
        $forum = $this->getDataGenerator()->create_module('forum', $options);
1874
 
1875
        // Add a discussion.
1876
        $record = array();
1877
        $record['course'] = $course->id;
1878
        $record['forum'] = $forum->id;
1879
        $record['userid'] = $user->id;
1880
        $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1881
 
1882
        // Add a post.
1883
        $record = array();
1884
        $record['discussion'] = $discussion->id;
1885
        $record['userid'] = $user->id;
1886
        $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
1887
 
1888
        // Trigger and capturing the event.
1889
        $sink = $this->redirectEvents();
1890
 
1891
        // Trigger the event by subscribing the user to the forum discussion.
1892
        \mod_forum\subscriptions::subscribe_user_to_discussion($user->id, $discussion);
1893
 
1894
        $events = $sink->get_events();
1895
        $this->assertCount(1, $events);
1896
        $event = reset($events);
1897
 
1898
        // Checking that the event contains the expected values.
1899
        $this->assertInstanceOf('\mod_forum\event\discussion_subscription_created', $event);
1900
 
1901
        $cm = get_coursemodule_from_instance('forum', $discussion->forum);
1902
        $context = \context_module::instance($cm->id);
1903
        $this->assertEquals($context, $event->get_context());
1904
 
1905
        $url = new \moodle_url('/mod/forum/subscribe.php', array(
1906
            'id' => $forum->id,
1907
            'd' => $discussion->id
1908
        ));
1909
 
1910
        $this->assertEquals($url, $event->get_url());
1911
        $this->assertEventContextNotUsed($event);
1912
        $this->assertNotEmpty($event->get_name());
1913
    }
1914
 
1915
    /**
1916
     * Test validation of discussion_subscription_created event.
1917
     */
11 efrain 1918
    public function test_discussion_subscription_created_validation(): void {
1 efrain 1919
        global $CFG, $DB;
1920
        require_once($CFG->dirroot . '/mod/forum/lib.php');
1921
 
1922
        // Setup test data.
1923
        $course = $this->getDataGenerator()->create_course();
1924
        $user = $this->getDataGenerator()->create_user();
1925
        $this->getDataGenerator()->enrol_user($user->id, $course->id);
1926
 
1927
        $options = array('course' => $course->id, 'forcesubscribe' => FORUM_CHOOSESUBSCRIBE);
1928
        $forum = $this->getDataGenerator()->create_module('forum', $options);
1929
 
1930
        // Add a discussion.
1931
        $record = array();
1932
        $record['course'] = $course->id;
1933
        $record['forum'] = $forum->id;
1934
        $record['userid'] = $user->id;
1935
        $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1936
 
1937
        // Add a post.
1938
        $record = array();
1939
        $record['discussion'] = $discussion->id;
1940
        $record['userid'] = $user->id;
1941
        $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
1942
 
1943
        // The user is not subscribed to the forum. Insert a new discussion subscription.
1944
        $subscription = new \stdClass();
1945
        $subscription->userid  = $user->id;
1946
        $subscription->forum = $forum->id;
1947
        $subscription->discussion = $discussion->id;
1948
        $subscription->preference = time();
1949
 
1950
        $subscription->id = $DB->insert_record('forum_discussion_subs', $subscription);
1951
 
1952
        $context = \context_module::instance($forum->cmid);
1953
 
1954
        $params = array(
1955
            'context' => $context,
1956
            'objectid' => $subscription->id,
1957
            'relateduserid' => $user->id,
1958
            'other' => array(
1959
                'forumid' => $forum->id,
1960
                'discussion' => $discussion->id,
1961
            )
1962
        );
1963
 
1964
        $event = \mod_forum\event\discussion_subscription_created::create($params);
1965
 
1966
        // Trigger and capturing the event.
1967
        $sink = $this->redirectEvents();
1968
        $event->trigger();
1969
        $events = $sink->get_events();
1970
        $this->assertCount(1, $events);
1971
        $event = reset($events);
1972
    }
1973
 
1974
    /**
1975
     * Test contextlevel validation of discussion_subscription_created event.
1976
     */
11 efrain 1977
    public function test_discussion_subscription_created_validation_contextlevel(): void {
1 efrain 1978
        global $CFG, $DB;
1979
        require_once($CFG->dirroot . '/mod/forum/lib.php');
1980
 
1981
        // Setup test data.
1982
        $course = $this->getDataGenerator()->create_course();
1983
        $user = $this->getDataGenerator()->create_user();
1984
        $this->getDataGenerator()->enrol_user($user->id, $course->id);
1985
 
1986
        $options = array('course' => $course->id, 'forcesubscribe' => FORUM_CHOOSESUBSCRIBE);
1987
        $forum = $this->getDataGenerator()->create_module('forum', $options);
1988
 
1989
        // Add a discussion.
1990
        $record = array();
1991
        $record['course'] = $course->id;
1992
        $record['forum'] = $forum->id;
1993
        $record['userid'] = $user->id;
1994
        $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1995
 
1996
        // Add a post.
1997
        $record = array();
1998
        $record['discussion'] = $discussion->id;
1999
        $record['userid'] = $user->id;
2000
        $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
2001
 
2002
        // The user is not subscribed to the forum. Insert a new discussion subscription.
2003
        $subscription = new \stdClass();
2004
        $subscription->userid  = $user->id;
2005
        $subscription->forum = $forum->id;
2006
        $subscription->discussion = $discussion->id;
2007
        $subscription->preference = time();
2008
 
2009
        $subscription->id = $DB->insert_record('forum_discussion_subs', $subscription);
2010
 
2011
        $context = \context_module::instance($forum->cmid);
2012
 
2013
        $params = array(
2014
            'context' => \context_course::instance($course->id),
2015
            'objectid' => $subscription->id,
2016
            'relateduserid' => $user->id,
2017
            'other' => array(
2018
                'forumid' => $forum->id,
2019
                'discussion' => $discussion->id,
2020
            )
2021
        );
2022
 
2023
        // Without an invalid context.
2024
        $this->expectException(\coding_exception::class);
2025
        $this->expectExceptionMessage('Context level must be CONTEXT_MODULE.');
2026
        \mod_forum\event\discussion_subscription_created::create($params);
2027
    }
2028
 
2029
    /**
2030
     * Test discussion validation of discussion_subscription_created event.
2031
     */
11 efrain 2032
    public function test_discussion_subscription_created_validation_discussion(): void {
1 efrain 2033
        global $CFG, $DB;
2034
        require_once($CFG->dirroot . '/mod/forum/lib.php');
2035
 
2036
        // Setup test data.
2037
        $course = $this->getDataGenerator()->create_course();
2038
        $user = $this->getDataGenerator()->create_user();
2039
        $this->getDataGenerator()->enrol_user($user->id, $course->id);
2040
 
2041
        $options = array('course' => $course->id, 'forcesubscribe' => FORUM_CHOOSESUBSCRIBE);
2042
        $forum = $this->getDataGenerator()->create_module('forum', $options);
2043
 
2044
        // Add a discussion.
2045
        $record = array();
2046
        $record['course'] = $course->id;
2047
        $record['forum'] = $forum->id;
2048
        $record['userid'] = $user->id;
2049
        $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
2050
 
2051
        // Add a post.
2052
        $record = array();
2053
        $record['discussion'] = $discussion->id;
2054
        $record['userid'] = $user->id;
2055
        $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
2056
 
2057
        // The user is not subscribed to the forum. Insert a new discussion subscription.
2058
        $subscription = new \stdClass();
2059
        $subscription->userid  = $user->id;
2060
        $subscription->forum = $forum->id;
2061
        $subscription->discussion = $discussion->id;
2062
        $subscription->preference = time();
2063
 
2064
        $subscription->id = $DB->insert_record('forum_discussion_subs', $subscription);
2065
 
2066
        // Without the discussion.
2067
        $params = array(
2068
            'context' => \context_module::instance($forum->cmid),
2069
            'objectid' => $subscription->id,
2070
            'relateduserid' => $user->id,
2071
            'other' => array(
2072
                'forumid' => $forum->id,
2073
            )
2074
        );
2075
 
2076
        $this->expectException(\coding_exception::class);
2077
        $this->expectExceptionMessage("The 'discussion' value must be set in other.");
2078
        \mod_forum\event\discussion_subscription_created::create($params);
2079
    }
2080
 
2081
    /**
2082
     * Test forumid validation of discussion_subscription_created event.
2083
     */
11 efrain 2084
    public function test_discussion_subscription_created_validation_forumid(): void {
1 efrain 2085
        global $CFG, $DB;
2086
        require_once($CFG->dirroot . '/mod/forum/lib.php');
2087
 
2088
        // Setup test data.
2089
        $course = $this->getDataGenerator()->create_course();
2090
        $user = $this->getDataGenerator()->create_user();
2091
        $this->getDataGenerator()->enrol_user($user->id, $course->id);
2092
 
2093
        $options = array('course' => $course->id, 'forcesubscribe' => FORUM_CHOOSESUBSCRIBE);
2094
        $forum = $this->getDataGenerator()->create_module('forum', $options);
2095
 
2096
        // Add a discussion.
2097
        $record = array();
2098
        $record['course'] = $course->id;
2099
        $record['forum'] = $forum->id;
2100
        $record['userid'] = $user->id;
2101
        $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
2102
 
2103
        // Add a post.
2104
        $record = array();
2105
        $record['discussion'] = $discussion->id;
2106
        $record['userid'] = $user->id;
2107
        $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
2108
 
2109
        // The user is not subscribed to the forum. Insert a new discussion subscription.
2110
        $subscription = new \stdClass();
2111
        $subscription->userid  = $user->id;
2112
        $subscription->forum = $forum->id;
2113
        $subscription->discussion = $discussion->id;
2114
        $subscription->preference = time();
2115
 
2116
        $subscription->id = $DB->insert_record('forum_discussion_subs', $subscription);
2117
 
2118
        // Without the forumid.
2119
        $params = array(
2120
            'context' => \context_module::instance($forum->cmid),
2121
            'objectid' => $subscription->id,
2122
            'relateduserid' => $user->id,
2123
            'other' => array(
2124
                'discussion' => $discussion->id,
2125
            )
2126
        );
2127
 
2128
        $this->expectException(\coding_exception::class);
2129
        $this->expectExceptionMessage("The 'forumid' value must be set in other.");
2130
        \mod_forum\event\discussion_subscription_created::create($params);
2131
    }
2132
 
2133
    /**
2134
     * Test relateduserid validation of discussion_subscription_created event.
2135
     */
11 efrain 2136
    public function test_discussion_subscription_created_validation_relateduserid(): void {
1 efrain 2137
        global $CFG, $DB;
2138
        require_once($CFG->dirroot . '/mod/forum/lib.php');
2139
 
2140
        // Setup test data.
2141
        $course = $this->getDataGenerator()->create_course();
2142
        $user = $this->getDataGenerator()->create_user();
2143
        $this->getDataGenerator()->enrol_user($user->id, $course->id);
2144
 
2145
        $options = array('course' => $course->id, 'forcesubscribe' => FORUM_CHOOSESUBSCRIBE);
2146
        $forum = $this->getDataGenerator()->create_module('forum', $options);
2147
 
2148
        // Add a discussion.
2149
        $record = array();
2150
        $record['course'] = $course->id;
2151
        $record['forum'] = $forum->id;
2152
        $record['userid'] = $user->id;
2153
        $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
2154
 
2155
        // Add a post.
2156
        $record = array();
2157
        $record['discussion'] = $discussion->id;
2158
        $record['userid'] = $user->id;
2159
        $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
2160
 
2161
        // The user is not subscribed to the forum. Insert a new discussion subscription.
2162
        $subscription = new \stdClass();
2163
        $subscription->userid  = $user->id;
2164
        $subscription->forum = $forum->id;
2165
        $subscription->discussion = $discussion->id;
2166
        $subscription->preference = time();
2167
 
2168
        $subscription->id = $DB->insert_record('forum_discussion_subs', $subscription);
2169
 
2170
        $context = \context_module::instance($forum->cmid);
2171
 
2172
        // Without the relateduserid.
2173
        $params = array(
2174
            'context' => \context_module::instance($forum->cmid),
2175
            'objectid' => $subscription->id,
2176
            'other' => array(
2177
                'forumid' => $forum->id,
2178
                'discussion' => $discussion->id,
2179
            )
2180
        );
2181
 
2182
        $this->expectException(\coding_exception::class);
2183
        $this->expectExceptionMessage("The 'relateduserid' must be set.");
2184
        \mod_forum\event\discussion_subscription_created::create($params);
2185
    }
2186
 
2187
    /**
2188
     * Test discussion_subscription_deleted event.
2189
     */
11 efrain 2190
    public function test_discussion_subscription_deleted(): void {
1 efrain 2191
        global $CFG;
2192
        require_once($CFG->dirroot . '/mod/forum/lib.php');
2193
 
2194
        // Setup test data.
2195
        $course = $this->getDataGenerator()->create_course();
2196
        $user = $this->getDataGenerator()->create_user();
2197
        $this->getDataGenerator()->enrol_user($user->id, $course->id);
2198
 
2199
        $options = array('course' => $course->id, 'forcesubscribe' => FORUM_INITIALSUBSCRIBE);
2200
        $forum = $this->getDataGenerator()->create_module('forum', $options);
2201
 
2202
        // Add a discussion.
2203
        $record = array();
2204
        $record['course'] = $course->id;
2205
        $record['forum'] = $forum->id;
2206
        $record['userid'] = $user->id;
2207
        $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
2208
 
2209
        // Add a post.
2210
        $record = array();
2211
        $record['discussion'] = $discussion->id;
2212
        $record['userid'] = $user->id;
2213
        $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
2214
 
2215
        // Trigger and capturing the event.
2216
        $sink = $this->redirectEvents();
2217
 
2218
        // Trigger the event by unsubscribing the user to the forum discussion.
2219
        \mod_forum\subscriptions::unsubscribe_user_from_discussion($user->id, $discussion);
2220
 
2221
        $events = $sink->get_events();
2222
        $this->assertCount(1, $events);
2223
        $event = reset($events);
2224
 
2225
        // Checking that the event contains the expected values.
2226
        $this->assertInstanceOf('\mod_forum\event\discussion_subscription_deleted', $event);
2227
 
2228
        $cm = get_coursemodule_from_instance('forum', $discussion->forum);
2229
        $context = \context_module::instance($cm->id);
2230
        $this->assertEquals($context, $event->get_context());
2231
 
2232
        $url = new \moodle_url('/mod/forum/subscribe.php', array(
2233
            'id' => $forum->id,
2234
            'd' => $discussion->id
2235
        ));
2236
 
2237
        $this->assertEquals($url, $event->get_url());
2238
        $this->assertEventContextNotUsed($event);
2239
        $this->assertNotEmpty($event->get_name());
2240
    }
2241
 
2242
    /**
2243
     * Test validation of discussion_subscription_deleted event.
2244
     */
11 efrain 2245
    public function test_discussion_subscription_deleted_validation(): void {
1 efrain 2246
        global $CFG, $DB;
2247
        require_once($CFG->dirroot . '/mod/forum/lib.php');
2248
 
2249
        // Setup test data.
2250
        $course = $this->getDataGenerator()->create_course();
2251
        $user = $this->getDataGenerator()->create_user();
2252
        $this->getDataGenerator()->enrol_user($user->id, $course->id);
2253
 
2254
        $options = array('course' => $course->id, 'forcesubscribe' => FORUM_INITIALSUBSCRIBE);
2255
        $forum = $this->getDataGenerator()->create_module('forum', $options);
2256
 
2257
        // Add a discussion.
2258
        $record = array();
2259
        $record['course'] = $course->id;
2260
        $record['forum'] = $forum->id;
2261
        $record['userid'] = $user->id;
2262
        $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
2263
 
2264
        // Add a post.
2265
        $record = array();
2266
        $record['discussion'] = $discussion->id;
2267
        $record['userid'] = $user->id;
2268
        $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
2269
 
2270
        // The user is not subscribed to the forum. Insert a new discussion subscription.
2271
        $subscription = new \stdClass();
2272
        $subscription->userid  = $user->id;
2273
        $subscription->forum = $forum->id;
2274
        $subscription->discussion = $discussion->id;
2275
        $subscription->preference = \mod_forum\subscriptions::FORUM_DISCUSSION_UNSUBSCRIBED;
2276
 
2277
        $subscription->id = $DB->insert_record('forum_discussion_subs', $subscription);
2278
 
2279
        $context = \context_module::instance($forum->cmid);
2280
 
2281
        $params = array(
2282
            'context' => $context,
2283
            'objectid' => $subscription->id,
2284
            'relateduserid' => $user->id,
2285
            'other' => array(
2286
                'forumid' => $forum->id,
2287
                'discussion' => $discussion->id,
2288
            )
2289
        );
2290
 
2291
        $event = \mod_forum\event\discussion_subscription_deleted::create($params);
2292
 
2293
        // Trigger and capturing the event.
2294
        $sink = $this->redirectEvents();
2295
        $event->trigger();
2296
        $events = $sink->get_events();
2297
        $this->assertCount(1, $events);
2298
        $event = reset($events);
2299
 
2300
        // Without an invalid context.
2301
        $params['context'] = \context_course::instance($course->id);
2302
        $this->expectException('coding_exception');
2303
        $this->expectExceptionMessage('Context level must be CONTEXT_MODULE.');
2304
        \mod_forum\event\discussion_deleted::create($params);
2305
 
2306
        // Without the discussion.
2307
        unset($params['discussion']);
2308
        $this->expectException('coding_exception');
2309
        $this->expectExceptionMessage('The \'discussion\' value must be set in other.');
2310
        \mod_forum\event\discussion_deleted::create($params);
2311
 
2312
        // Without the forumid.
2313
        unset($params['forumid']);
2314
        $this->expectException('coding_exception');
2315
        $this->expectExceptionMessage('The \'forumid\' value must be set in other.');
2316
        \mod_forum\event\discussion_deleted::create($params);
2317
 
2318
        // Without the relateduserid.
2319
        unset($params['relateduserid']);
2320
        $this->expectException('coding_exception');
2321
        $this->expectExceptionMessage('The \'relateduserid\' value must be set in other.');
2322
        \mod_forum\event\discussion_deleted::create($params);
2323
    }
2324
 
2325
    /**
2326
     * Test contextlevel validation of discussion_subscription_deleted event.
2327
     */
11 efrain 2328
    public function test_discussion_subscription_deleted_validation_contextlevel(): void {
1 efrain 2329
        global $CFG, $DB;
2330
        require_once($CFG->dirroot . '/mod/forum/lib.php');
2331
 
2332
        // Setup test data.
2333
        $course = $this->getDataGenerator()->create_course();
2334
        $user = $this->getDataGenerator()->create_user();
2335
        $this->getDataGenerator()->enrol_user($user->id, $course->id);
2336
 
2337
        $options = array('course' => $course->id, 'forcesubscribe' => FORUM_CHOOSESUBSCRIBE);
2338
        $forum = $this->getDataGenerator()->create_module('forum', $options);
2339
 
2340
        // Add a discussion.
2341
        $record = array();
2342
        $record['course'] = $course->id;
2343
        $record['forum'] = $forum->id;
2344
        $record['userid'] = $user->id;
2345
        $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
2346
 
2347
        // Add a post.
2348
        $record = array();
2349
        $record['discussion'] = $discussion->id;
2350
        $record['userid'] = $user->id;
2351
        $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
2352
 
2353
        // The user is not subscribed to the forum. Insert a new discussion subscription.
2354
        $subscription = new \stdClass();
2355
        $subscription->userid  = $user->id;
2356
        $subscription->forum = $forum->id;
2357
        $subscription->discussion = $discussion->id;
2358
        $subscription->preference = time();
2359
 
2360
        $subscription->id = $DB->insert_record('forum_discussion_subs', $subscription);
2361
 
2362
        $context = \context_module::instance($forum->cmid);
2363
 
2364
        $params = array(
2365
            'context' => \context_course::instance($course->id),
2366
            'objectid' => $subscription->id,
2367
            'relateduserid' => $user->id,
2368
            'other' => array(
2369
                'forumid' => $forum->id,
2370
                'discussion' => $discussion->id,
2371
            )
2372
        );
2373
 
2374
        // Without an invalid context.
2375
        $this->expectException(\coding_exception::class);
2376
        $this->expectExceptionMessage('Context level must be CONTEXT_MODULE.');
2377
        \mod_forum\event\discussion_subscription_deleted::create($params);
2378
    }
2379
 
2380
    /**
2381
     * Test discussion validation of discussion_subscription_deleted event.
2382
     */
11 efrain 2383
    public function test_discussion_subscription_deleted_validation_discussion(): void {
1 efrain 2384
        global $CFG, $DB;
2385
        require_once($CFG->dirroot . '/mod/forum/lib.php');
2386
 
2387
        // Setup test data.
2388
        $course = $this->getDataGenerator()->create_course();
2389
        $user = $this->getDataGenerator()->create_user();
2390
        $this->getDataGenerator()->enrol_user($user->id, $course->id);
2391
 
2392
        $options = array('course' => $course->id, 'forcesubscribe' => FORUM_CHOOSESUBSCRIBE);
2393
        $forum = $this->getDataGenerator()->create_module('forum', $options);
2394
 
2395
        // Add a discussion.
2396
        $record = array();
2397
        $record['course'] = $course->id;
2398
        $record['forum'] = $forum->id;
2399
        $record['userid'] = $user->id;
2400
        $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
2401
 
2402
        // Add a post.
2403
        $record = array();
2404
        $record['discussion'] = $discussion->id;
2405
        $record['userid'] = $user->id;
2406
        $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
2407
 
2408
        // The user is not subscribed to the forum. Insert a new discussion subscription.
2409
        $subscription = new \stdClass();
2410
        $subscription->userid  = $user->id;
2411
        $subscription->forum = $forum->id;
2412
        $subscription->discussion = $discussion->id;
2413
        $subscription->preference = time();
2414
 
2415
        $subscription->id = $DB->insert_record('forum_discussion_subs', $subscription);
2416
 
2417
        // Without the discussion.
2418
        $params = array(
2419
            'context' => \context_module::instance($forum->cmid),
2420
            'objectid' => $subscription->id,
2421
            'relateduserid' => $user->id,
2422
            'other' => array(
2423
                'forumid' => $forum->id,
2424
            )
2425
        );
2426
 
2427
        $this->expectException(\coding_exception::class);
2428
        $this->expectExceptionMessage("The 'discussion' value must be set in other.");
2429
        \mod_forum\event\discussion_subscription_deleted::create($params);
2430
    }
2431
 
2432
    /**
2433
     * Test forumid validation of discussion_subscription_deleted event.
2434
     */
11 efrain 2435
    public function test_discussion_subscription_deleted_validation_forumid(): void {
1 efrain 2436
        global $CFG, $DB;
2437
        require_once($CFG->dirroot . '/mod/forum/lib.php');
2438
 
2439
        // Setup test data.
2440
        $course = $this->getDataGenerator()->create_course();
2441
        $user = $this->getDataGenerator()->create_user();
2442
        $this->getDataGenerator()->enrol_user($user->id, $course->id);
2443
 
2444
        $options = array('course' => $course->id, 'forcesubscribe' => FORUM_CHOOSESUBSCRIBE);
2445
        $forum = $this->getDataGenerator()->create_module('forum', $options);
2446
 
2447
        // Add a discussion.
2448
        $record = array();
2449
        $record['course'] = $course->id;
2450
        $record['forum'] = $forum->id;
2451
        $record['userid'] = $user->id;
2452
        $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
2453
 
2454
        // Add a post.
2455
        $record = array();
2456
        $record['discussion'] = $discussion->id;
2457
        $record['userid'] = $user->id;
2458
        $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
2459
 
2460
        // The user is not subscribed to the forum. Insert a new discussion subscription.
2461
        $subscription = new \stdClass();
2462
        $subscription->userid  = $user->id;
2463
        $subscription->forum = $forum->id;
2464
        $subscription->discussion = $discussion->id;
2465
        $subscription->preference = time();
2466
 
2467
        $subscription->id = $DB->insert_record('forum_discussion_subs', $subscription);
2468
 
2469
        // Without the forumid.
2470
        $params = array(
2471
            'context' => \context_module::instance($forum->cmid),
2472
            'objectid' => $subscription->id,
2473
            'relateduserid' => $user->id,
2474
            'other' => array(
2475
                'discussion' => $discussion->id,
2476
            )
2477
        );
2478
 
2479
        $this->expectException(\coding_exception::class);
2480
        $this->expectExceptionMessage("The 'forumid' value must be set in other.");
2481
        \mod_forum\event\discussion_subscription_deleted::create($params);
2482
    }
2483
 
2484
    /**
2485
     * Test relateduserid validation of discussion_subscription_deleted event.
2486
     */
11 efrain 2487
    public function test_discussion_subscription_deleted_validation_relateduserid(): void {
1 efrain 2488
        global $CFG, $DB;
2489
        require_once($CFG->dirroot . '/mod/forum/lib.php');
2490
 
2491
        // Setup test data.
2492
        $course = $this->getDataGenerator()->create_course();
2493
        $user = $this->getDataGenerator()->create_user();
2494
        $this->getDataGenerator()->enrol_user($user->id, $course->id);
2495
 
2496
        $options = array('course' => $course->id, 'forcesubscribe' => FORUM_CHOOSESUBSCRIBE);
2497
        $forum = $this->getDataGenerator()->create_module('forum', $options);
2498
 
2499
        // Add a discussion.
2500
        $record = array();
2501
        $record['course'] = $course->id;
2502
        $record['forum'] = $forum->id;
2503
        $record['userid'] = $user->id;
2504
        $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
2505
 
2506
        // Add a post.
2507
        $record = array();
2508
        $record['discussion'] = $discussion->id;
2509
        $record['userid'] = $user->id;
2510
        $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
2511
 
2512
        // The user is not subscribed to the forum. Insert a new discussion subscription.
2513
        $subscription = new \stdClass();
2514
        $subscription->userid  = $user->id;
2515
        $subscription->forum = $forum->id;
2516
        $subscription->discussion = $discussion->id;
2517
        $subscription->preference = time();
2518
 
2519
        $subscription->id = $DB->insert_record('forum_discussion_subs', $subscription);
2520
 
2521
        $context = \context_module::instance($forum->cmid);
2522
 
2523
        // Without the relateduserid.
2524
        $params = array(
2525
            'context' => \context_module::instance($forum->cmid),
2526
            'objectid' => $subscription->id,
2527
            'other' => array(
2528
                'forumid' => $forum->id,
2529
                'discussion' => $discussion->id,
2530
            )
2531
        );
2532
 
2533
        $this->expectException(\coding_exception::class);
2534
        $this->expectExceptionMessage("The 'relateduserid' must be set.");
2535
        \mod_forum\event\discussion_subscription_deleted::create($params);
2536
    }
2537
 
2538
    /**
2539
     * Test that the correct context is used in the events when subscribing
2540
     * users.
2541
     */
11 efrain 2542
    public function test_forum_subscription_page_context_valid(): void {
1 efrain 2543
        global $CFG, $PAGE;
2544
        require_once($CFG->dirroot . '/mod/forum/lib.php');
2545
 
2546
        // Setup test data.
2547
        $course = $this->getDataGenerator()->create_course();
2548
        $user = $this->getDataGenerator()->create_user();
2549
        $this->getDataGenerator()->enrol_user($user->id, $course->id);
2550
 
2551
        $options = array('course' => $course->id, 'forcesubscribe' => FORUM_CHOOSESUBSCRIBE);
2552
        $forum = $this->getDataGenerator()->create_module('forum', $options);
2553
        $quiz = $this->getDataGenerator()->create_module('quiz', $options);
2554
 
2555
        // Add a discussion.
2556
        $record = array();
2557
        $record['course'] = $course->id;
2558
        $record['forum'] = $forum->id;
2559
        $record['userid'] = $user->id;
2560
        $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
2561
 
2562
        // Add a post.
2563
        $record = array();
2564
        $record['discussion'] = $discussion->id;
2565
        $record['userid'] = $user->id;
2566
        $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
2567
 
2568
        // Set up the default page event to use this forum.
2569
        $PAGE = new \moodle_page();
2570
        $cm = get_coursemodule_from_instance('forum', $discussion->forum);
2571
        $context = \context_module::instance($cm->id);
2572
        $PAGE->set_context($context);
2573
        $PAGE->set_cm($cm, $course, $forum);
2574
 
2575
        // Trigger and capturing the event.
2576
        $sink = $this->redirectEvents();
2577
 
2578
        // Trigger the event by subscribing the user to the forum.
2579
        \mod_forum\subscriptions::subscribe_user($user->id, $forum);
2580
 
2581
        $events = $sink->get_events();
2582
        $sink->clear();
2583
        $this->assertCount(1, $events);
2584
        $event = reset($events);
2585
 
2586
        // Checking that the event contains the expected values.
2587
        $this->assertInstanceOf('\mod_forum\event\subscription_created', $event);
2588
        $this->assertEquals($context, $event->get_context());
2589
 
2590
        // Trigger the event by unsubscribing the user to the forum.
2591
        \mod_forum\subscriptions::unsubscribe_user($user->id, $forum);
2592
 
2593
        $events = $sink->get_events();
2594
        $sink->clear();
2595
        $this->assertCount(1, $events);
2596
        $event = reset($events);
2597
 
2598
        // Checking that the event contains the expected values.
2599
        $this->assertInstanceOf('\mod_forum\event\subscription_deleted', $event);
2600
        $this->assertEquals($context, $event->get_context());
2601
 
2602
        // Trigger the event by subscribing the user to the discussion.
2603
        \mod_forum\subscriptions::subscribe_user_to_discussion($user->id, $discussion);
2604
 
2605
        $events = $sink->get_events();
2606
        $sink->clear();
2607
        $this->assertCount(1, $events);
2608
        $event = reset($events);
2609
 
2610
        // Checking that the event contains the expected values.
2611
        $this->assertInstanceOf('\mod_forum\event\discussion_subscription_created', $event);
2612
        $this->assertEquals($context, $event->get_context());
2613
 
2614
        // Trigger the event by unsubscribing the user from the discussion.
2615
        \mod_forum\subscriptions::unsubscribe_user_from_discussion($user->id, $discussion);
2616
 
2617
        $events = $sink->get_events();
2618
        $sink->clear();
2619
        $this->assertCount(1, $events);
2620
        $event = reset($events);
2621
 
2622
        // Checking that the event contains the expected values.
2623
        $this->assertInstanceOf('\mod_forum\event\discussion_subscription_deleted', $event);
2624
        $this->assertEquals($context, $event->get_context());
2625
 
2626
        // Now try with the context for a different module (quiz).
2627
        $PAGE = new \moodle_page();
2628
        $cm = get_coursemodule_from_instance('quiz', $quiz->id);
2629
        $quizcontext = \context_module::instance($cm->id);
2630
        $PAGE->set_context($quizcontext);
2631
        $PAGE->set_cm($cm, $course, $quiz);
2632
 
2633
        // Trigger and capturing the event.
2634
        $sink = $this->redirectEvents();
2635
 
2636
        // Trigger the event by subscribing the user to the forum.
2637
        \mod_forum\subscriptions::subscribe_user($user->id, $forum);
2638
 
2639
        $events = $sink->get_events();
2640
        $sink->clear();
2641
        $this->assertCount(1, $events);
2642
        $event = reset($events);
2643
 
2644
        // Checking that the event contains the expected values.
2645
        $this->assertInstanceOf('\mod_forum\event\subscription_created', $event);
2646
        $this->assertEquals($context, $event->get_context());
2647
 
2648
        // Trigger the event by unsubscribing the user to the forum.
2649
        \mod_forum\subscriptions::unsubscribe_user($user->id, $forum);
2650
 
2651
        $events = $sink->get_events();
2652
        $sink->clear();
2653
        $this->assertCount(1, $events);
2654
        $event = reset($events);
2655
 
2656
        // Checking that the event contains the expected values.
2657
        $this->assertInstanceOf('\mod_forum\event\subscription_deleted', $event);
2658
        $this->assertEquals($context, $event->get_context());
2659
 
2660
        // Trigger the event by subscribing the user to the discussion.
2661
        \mod_forum\subscriptions::subscribe_user_to_discussion($user->id, $discussion);
2662
 
2663
        $events = $sink->get_events();
2664
        $sink->clear();
2665
        $this->assertCount(1, $events);
2666
        $event = reset($events);
2667
 
2668
        // Checking that the event contains the expected values.
2669
        $this->assertInstanceOf('\mod_forum\event\discussion_subscription_created', $event);
2670
        $this->assertEquals($context, $event->get_context());
2671
 
2672
        // Trigger the event by unsubscribing the user from the discussion.
2673
        \mod_forum\subscriptions::unsubscribe_user_from_discussion($user->id, $discussion);
2674
 
2675
        $events = $sink->get_events();
2676
        $sink->clear();
2677
        $this->assertCount(1, $events);
2678
        $event = reset($events);
2679
 
2680
        // Checking that the event contains the expected values.
2681
        $this->assertInstanceOf('\mod_forum\event\discussion_subscription_deleted', $event);
2682
        $this->assertEquals($context, $event->get_context());
2683
 
2684
        // Now try with the course context - the module context should still be used.
2685
        $PAGE = new \moodle_page();
2686
        $coursecontext = \context_course::instance($course->id);
2687
        $PAGE->set_context($coursecontext);
2688
 
2689
        // Trigger the event by subscribing the user to the forum.
2690
        \mod_forum\subscriptions::subscribe_user($user->id, $forum);
2691
 
2692
        $events = $sink->get_events();
2693
        $sink->clear();
2694
        $this->assertCount(1, $events);
2695
        $event = reset($events);
2696
 
2697
        // Checking that the event contains the expected values.
2698
        $this->assertInstanceOf('\mod_forum\event\subscription_created', $event);
2699
        $this->assertEquals($context, $event->get_context());
2700
 
2701
        // Trigger the event by unsubscribing the user to the forum.
2702
        \mod_forum\subscriptions::unsubscribe_user($user->id, $forum);
2703
 
2704
        $events = $sink->get_events();
2705
        $sink->clear();
2706
        $this->assertCount(1, $events);
2707
        $event = reset($events);
2708
 
2709
        // Checking that the event contains the expected values.
2710
        $this->assertInstanceOf('\mod_forum\event\subscription_deleted', $event);
2711
        $this->assertEquals($context, $event->get_context());
2712
 
2713
        // Trigger the event by subscribing the user to the discussion.
2714
        \mod_forum\subscriptions::subscribe_user_to_discussion($user->id, $discussion);
2715
 
2716
        $events = $sink->get_events();
2717
        $sink->clear();
2718
        $this->assertCount(1, $events);
2719
        $event = reset($events);
2720
 
2721
        // Checking that the event contains the expected values.
2722
        $this->assertInstanceOf('\mod_forum\event\discussion_subscription_created', $event);
2723
        $this->assertEquals($context, $event->get_context());
2724
 
2725
        // Trigger the event by unsubscribing the user from the discussion.
2726
        \mod_forum\subscriptions::unsubscribe_user_from_discussion($user->id, $discussion);
2727
 
2728
        $events = $sink->get_events();
2729
        $sink->clear();
2730
        $this->assertCount(1, $events);
2731
        $event = reset($events);
2732
 
2733
        // Checking that the event contains the expected values.
2734
        $this->assertInstanceOf('\mod_forum\event\discussion_subscription_deleted', $event);
2735
        $this->assertEquals($context, $event->get_context());
2736
 
2737
    }
2738
 
2739
    /**
2740
     * Test mod_forum_observer methods.
2741
     */
11 efrain 2742
    public function test_observers(): void {
1 efrain 2743
        global $DB, $CFG;
2744
 
2745
        require_once($CFG->dirroot . '/mod/forum/lib.php');
2746
 
2747
        $forumgen = $this->getDataGenerator()->get_plugin_generator('mod_forum');
2748
 
2749
        $course = $this->getDataGenerator()->create_course();
2750
        $trackedrecord = array('course' => $course->id, 'type' => 'general', 'forcesubscribe' => FORUM_INITIALSUBSCRIBE);
2751
        $untrackedrecord = array('course' => $course->id, 'type' => 'general');
2752
        $trackedforum = $this->getDataGenerator()->create_module('forum', $trackedrecord);
2753
        $untrackedforum = $this->getDataGenerator()->create_module('forum', $untrackedrecord);
2754
 
2755
        // Used functions don't require these settings; adding
2756
        // them just in case there are APIs changes in future.
2757
        $user = $this->getDataGenerator()->create_user(array(
2758
            'maildigest' => 1,
2759
            'trackforums' => 1
2760
        ));
2761
 
2762
        $manplugin = enrol_get_plugin('manual');
2763
        $manualenrol = $DB->get_record('enrol', array('courseid' => $course->id, 'enrol' => 'manual'));
2764
        $student = $DB->get_record('role', array('shortname' => 'student'));
2765
 
2766
        // The role_assign observer does it's job adding the forum_subscriptions record.
2767
        $manplugin->enrol_user($manualenrol, $user->id, $student->id);
2768
 
2769
        // They are not required, but in a real environment they are supposed to be required;
2770
        // adding them just in case there are APIs changes in future.
2771
        set_config('forum_trackingtype', 1);
2772
        set_config('forum_trackreadposts', 1);
2773
 
2774
        $record = array();
2775
        $record['course'] = $course->id;
2776
        $record['forum'] = $trackedforum->id;
2777
        $record['userid'] = $user->id;
2778
        $discussion = $forumgen->create_discussion($record);
2779
 
2780
        $record = array();
2781
        $record['discussion'] = $discussion->id;
2782
        $record['userid'] = $user->id;
2783
        $post = $forumgen->create_post($record);
2784
 
2785
        forum_tp_add_read_record($user->id, $post->id);
2786
        forum_set_user_maildigest($trackedforum, 2, $user);
2787
        forum_tp_stop_tracking($untrackedforum->id, $user->id);
2788
 
2789
        $this->assertEquals(1, $DB->count_records('forum_subscriptions'));
2790
        $this->assertEquals(1, $DB->count_records('forum_digests'));
2791
        $this->assertEquals(1, $DB->count_records('forum_track_prefs'));
2792
        $this->assertEquals(1, $DB->count_records('forum_read'));
2793
 
2794
        // The course_module_created observer does it's job adding a subscription.
2795
        $forumrecord = array('course' => $course->id, 'type' => 'general', 'forcesubscribe' => FORUM_INITIALSUBSCRIBE);
2796
        $extraforum = $this->getDataGenerator()->create_module('forum', $forumrecord);
2797
        $this->assertEquals(2, $DB->count_records('forum_subscriptions'));
2798
 
2799
        $manplugin->unenrol_user($manualenrol, $user->id);
2800
 
2801
        $this->assertEquals(0, $DB->count_records('forum_digests'));
2802
        $this->assertEquals(0, $DB->count_records('forum_subscriptions'));
2803
        $this->assertEquals(0, $DB->count_records('forum_track_prefs'));
2804
        $this->assertEquals(0, $DB->count_records('forum_read'));
2805
    }
2806
 
2807
}