Proyectos de Subversion Moodle

Rev

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