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