MDL-48196 phpunit: fixed the fail after this change
[moodle.git] / mod / forum / tests / mail_test.php
CommitLineData
a7a84903
AN
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 * The forum module mail generation tests.
19 *
20 * @package mod_forum
21 * @category external
22 * @copyright 2013 Andrew Nicols
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 */
25
26defined('MOODLE_INTERNAL') || die();
27
28global $CFG;
29
30class mod_forum_mail_testcase extends advanced_testcase {
31
32 protected $helper;
33
34 public function setUp() {
59075a43
AN
35 // We must clear the subscription caches. This has to be done both before each test, and after in case of other
36 // tests using these functions.
37 \mod_forum\subscriptions::reset_forum_cache();
a54702b2 38 \mod_forum\subscriptions::reset_discussion_cache();
59075a43 39
a7a84903
AN
40 global $CFG;
41 require_once($CFG->dirroot . '/mod/forum/lib.php');
42
43 $helper = new stdClass();
44
45 // Messaging is not compatible with transactions...
46 $this->preventResetByRollback();
47
48 // Catch all messages.
49 $helper->messagesink = $this->redirectMessages();
50 $helper->mailsink = $this->redirectEmails();
51
52 // Confirm that we have an empty message sink so far.
53 $messages = $helper->messagesink->get_messages();
54 $this->assertEquals(0, count($messages));
55
56 $messages = $helper->mailsink->get_messages();
57 $this->assertEquals(0, count($messages));
58
555837c5
AN
59 // Forcibly reduce the maxeditingtime to a second in the past to
60 // ensure that messages are sent out.
61 $CFG->maxeditingtime = -1;
a7a84903
AN
62
63 // Ensure that we don't prevent e-mail as this will cause unit test failures.
64 $CFG->noemailever = false;
65
66 $this->helper = $helper;
67 }
68
69 public function tearDown() {
59075a43
AN
70 // We must clear the subscription caches. This has to be done both before each test, and after in case of other
71 // tests using these functions.
72 \mod_forum\subscriptions::reset_forum_cache();
73
a7a84903
AN
74 $this->helper->messagesink->clear();
75 $this->helper->messagesink->close();
76
77 $this->helper->mailsink->clear();
78 $this->helper->mailsink->close();
79 }
80
81 /**
82 * Helper to create the required number of users in the specified
83 * course.
84 * Users are enrolled as students.
85 *
86 * @param stdClass $course The course object
87 * @param integer $count The number of users to create
88 * @return array The users created
89 */
90 protected function helper_create_users($course, $count) {
91 $users = array();
92
93 for ($i = 0; $i < $count; $i++) {
94 $user = $this->getDataGenerator()->create_user();
95 $this->getDataGenerator()->enrol_user($user->id, $course->id);
96 $users[] = $user;
97 }
98
99 return $users;
100 }
101
102 /**
103 * Create a new discussion and post within the specified forum, as the
104 * specified author.
105 *
106 * @param stdClass $forum The forum to post in
107 * @param stdClass $author The author to post as
108 * @param array An array containing the discussion object, and the post object
109 */
110 protected function helper_post_to_forum($forum, $author) {
555837c5 111 global $DB;
a7a84903
AN
112 $generator = $this->getDataGenerator()->get_plugin_generator('mod_forum');
113
114 // Create a discussion in the forum, and then add a post to that discussion.
115 $record = new stdClass();
116 $record->course = $forum->course;
117 $record->userid = $author->id;
118 $record->forum = $forum->id;
119 $discussion = $generator->create_discussion($record);
120
555837c5
AN
121 // Retrieve the post which was created by create_discussion.
122 $post = $DB->get_record('forum_posts', array('discussion' => $discussion->id));
a7a84903
AN
123
124 return array($discussion, $post);
125 }
126
7122e154
AN
127 /**
128 * Update the post time for the specified post by $factor.
129 *
130 * @param stdClass $post The post to update
131 * @param int $factor The amount to update by
132 */
133 protected function helper_update_post_time($post, $factor) {
134 global $DB;
135
136 // Update the post to have a created in the past.
137 $DB->set_field('forum_posts', 'created', $post->created + $factor, array('id' => $post->id));
138 }
139
140 /**
141 * Update the subscription time for the specified user/discussion by $factor.
142 *
143 * @param stdClass $user The user to update
144 * @param stdClass $discussion The discussion to update for this user
145 * @param int $factor The amount to update by
146 */
147 protected function helper_update_subscription_time($user, $discussion, $factor) {
148 global $DB;
149
150 $sub = $DB->get_record('forum_discussion_subs', array('userid' => $user->id, 'discussion' => $discussion->id));
151
152 // Update the subscription to have a preference in the past.
153 $DB->set_field('forum_discussion_subs', 'preference', $sub->preference + $factor, array('id' => $sub->id));
154 }
155
156 /**
157 * Create a new post within an existing discussion, as the specified author.
158 *
159 * @param stdClass $forum The forum to post in
160 * @param stdClass $discussion The discussion to post in
161 * @param stdClass $author The author to post as
162 * @return stdClass The forum post
163 */
164 protected function helper_post_to_discussion($forum, $discussion, $author) {
165 global $DB;
166
167 $generator = $this->getDataGenerator()->get_plugin_generator('mod_forum');
168
169 // Add a post to the discussion.
170 $record = new stdClass();
171 $record->course = $forum->course;
172 $record->userid = $author->id;
173 $record->forum = $forum->id;
174 $record->discussion = $discussion->id;
175 $record->mailnow = 1;
176
177 $post = $generator->create_post($record);
178
179 return $post;
180 }
181
a7a84903
AN
182 /**
183 * Run the forum cron, and check that the specified post was sent the
184 * specified number of times.
185 *
186 * @param stdClass $post The forum post object
187 * @param integer $expected The number of times that the post should have been sent
188 * @return array An array of the messages caught by the message sink
189 */
190 protected function helper_run_cron_check_count($post, $expected) {
eb451c79 191
a7a84903
AN
192 // Clear the sinks before running cron.
193 $this->helper->messagesink->clear();
194 $this->helper->mailsink->clear();
195
196 // Cron daily uses mtrace, turn on buffering to silence output.
197 $this->expectOutputRegex("/{$expected} users were sent post {$post->id}, '{$post->subject}'/");
198 forum_cron();
199
200 // Now check the results in the message sink.
201 $messages = $this->helper->messagesink->get_messages();
202
203 // There should be the expected number of messages.
204 $this->assertEquals($expected, count($messages));
205
206 return $messages;
207 }
208
eb451c79
AN
209 /**
210 * Run the forum cron, and check that the specified posts were sent the
211 * specified number of times.
212 *
213 * @param stdClass $post The forum post object
214 * @param integer $expected The number of times that the post should have been sent
215 * @return array An array of the messages caught by the message sink
216 */
217 protected function helper_run_cron_check_counts($posts, $expected) {
218
219 // Clear the sinks before running cron.
220 $this->helper->messagesink->clear();
221 $this->helper->mailsink->clear();
222
223 // Cron daily uses mtrace, turn on buffering to silence output.
224 foreach ($posts as $post) {
225 $this->expectOutputRegex("/{$post['count']} users were sent post {$post['id']}, '{$post['subject']}'/");
226 }
227 forum_cron();
228
229 // Now check the results in the message sink.
230 $messages = $this->helper->messagesink->get_messages();
231
232 // There should be the expected number of messages.
233 $this->assertEquals($expected, count($messages));
234
235 return $messages;
236 }
237
a7a84903
AN
238 public function test_forced_subscription() {
239 $this->resetAfterTest(true);
240
241 // Create a course, with a forum.
242 $course = $this->getDataGenerator()->create_course();
243
244 $options = array('course' => $course->id, 'forcesubscribe' => FORUM_FORCESUBSCRIBE);
245 $forum = $this->getDataGenerator()->create_module('forum', $options);
246
247 // Create two users enrolled in the course as students.
248 list($author, $recipient) = $this->helper_create_users($course, 2);
249
250 // Post a discussion to the forum.
251 list($discussion, $post) = $this->helper_post_to_forum($forum, $author);
252
253 // We expect both users to receive this post.
254 $expected = 2;
255
256 // Run cron and check that the expected number of users received the notification.
257 $messages = $this->helper_run_cron_check_count($post, $expected);
258
259 $seenauthor = false;
260 $seenrecipient = false;
261 foreach ($messages as $message) {
262 // They should both be from our user.
263 $this->assertEquals($author->id, $message->useridfrom);
264
265 if ($message->useridto == $author->id) {
266 $seenauthor = true;
267 } else if ($message->useridto = $recipient->id) {
268 $seenrecipient = true;
269 }
270 }
271
272 // Check we saw messages for both users.
273 $this->assertTrue($seenauthor);
274 $this->assertTrue($seenrecipient);
275 }
276
277 public function test_subscription_disabled() {
278 global $DB;
279
280 $this->resetAfterTest(true);
281
282 // Create a course, with a forum.
283 $course = $this->getDataGenerator()->create_course();
284
285 $options = array('course' => $course->id, 'forcesubscribe' => FORUM_DISALLOWSUBSCRIBE);
286 $forum = $this->getDataGenerator()->create_module('forum', $options);
287
288 // Create two users enrolled in the course as students.
289 list($author, $recipient) = $this->helper_create_users($course, 2);
290
291 // Post a discussion to the forum.
292 list($discussion, $post) = $this->helper_post_to_forum($forum, $author);
293
294 // We expect both users to receive this post.
295 $expected = 0;
296
297 // Run cron and check that the expected number of users received the notification.
298 $messages = $this->helper_run_cron_check_count($post, $expected);
299
300 // A user with the manageactivities capability within the course can subscribe.
301 $expected = 1;
302 $roleids = $DB->get_records_menu('role', null, '', 'shortname, id');
303 assign_capability('moodle/course:manageactivities', CAP_ALLOW, $roleids['student'], context_course::instance($course->id));
7122e154 304 \mod_forum\subscriptions::subscribe_user($recipient->id, $forum);
a7a84903
AN
305
306 $this->assertEquals($expected, $DB->count_records('forum_subscriptions', array(
7122e154 307 'userid' => $recipient->id,
a7a84903
AN
308 'forum' => $forum->id,
309 )));
310
311 // Run cron and check that the expected number of users received the notification.
7122e154 312 list($discussion, $post) = $this->helper_post_to_forum($forum, $recipient);
a7a84903
AN
313 $messages = $this->helper_run_cron_check_count($post, $expected);
314
315 // Unsubscribe the user again.
7122e154 316 \mod_forum\subscriptions::unsubscribe_user($recipient->id, $forum);
a7a84903
AN
317
318 $expected = 0;
319 $this->assertEquals($expected, $DB->count_records('forum_subscriptions', array(
7122e154 320 'userid' => $recipient->id,
a7a84903
AN
321 'forum' => $forum->id,
322 )));
323
324 // Run cron and check that the expected number of users received the notification.
325 list($discussion, $post) = $this->helper_post_to_forum($forum, $author);
326 $messages = $this->helper_run_cron_check_count($post, $expected);
327
7122e154
AN
328 // Subscribe the user to the discussion.
329 \mod_forum\subscriptions::subscribe_user_to_discussion($recipient->id, $discussion);
330 $this->helper_update_subscription_time($recipient, $discussion, -60);
331
332 $reply = $this->helper_post_to_discussion($forum, $discussion, $author);
333 $this->helper_update_post_time($reply, -30);
334
335 $messages = $this->helper_run_cron_check_count($reply, $expected);
a7a84903
AN
336 }
337
338 public function test_automatic() {
339 $this->resetAfterTest(true);
340
341 // Create a course, with a forum.
342 $course = $this->getDataGenerator()->create_course();
343
344 $options = array('course' => $course->id, 'forcesubscribe' => FORUM_INITIALSUBSCRIBE);
345 $forum = $this->getDataGenerator()->create_module('forum', $options);
346
347 // Create two users enrolled in the course as students.
348 list($author, $recipient) = $this->helper_create_users($course, 2);
349
350 // Post a discussion to the forum.
351 list($discussion, $post) = $this->helper_post_to_forum($forum, $author);
352
353 // We expect both users to receive this post.
354 $expected = 2;
355
356 // Run cron and check that the expected number of users received the notification.
357 $messages = $this->helper_run_cron_check_count($post, $expected);
358
359 $seenauthor = false;
360 $seenrecipient = false;
361 foreach ($messages as $message) {
362 // They should both be from our user.
363 $this->assertEquals($author->id, $message->useridfrom);
364
365 if ($message->useridto == $author->id) {
366 $seenauthor = true;
367 } else if ($message->useridto = $recipient->id) {
368 $seenrecipient = true;
369 }
370 }
371
372 // Check we saw messages for both users.
373 $this->assertTrue($seenauthor);
374 $this->assertTrue($seenrecipient);
375 }
376
377 public function test_optional() {
378 $this->resetAfterTest(true);
379
380 // Create a course, with a forum.
381 $course = $this->getDataGenerator()->create_course();
382
383 $options = array('course' => $course->id, 'forcesubscribe' => FORUM_CHOOSESUBSCRIBE);
384 $forum = $this->getDataGenerator()->create_module('forum', $options);
385
386 // Create two users enrolled in the course as students.
387 list($author, $recipient) = $this->helper_create_users($course, 2);
388
389 // Post a discussion to the forum.
390 list($discussion, $post) = $this->helper_post_to_forum($forum, $author);
391
392 // We expect both users to receive this post.
393 $expected = 0;
394
395 // Run cron and check that the expected number of users received the notification.
396 $messages = $this->helper_run_cron_check_count($post, $expected);
397 }
398
399 public function test_automatic_with_unsubscribed_user() {
400 $this->resetAfterTest(true);
401
402 // Create a course, with a forum.
403 $course = $this->getDataGenerator()->create_course();
404
405 $options = array('course' => $course->id, 'forcesubscribe' => FORUM_INITIALSUBSCRIBE);
406 $forum = $this->getDataGenerator()->create_module('forum', $options);
407
408 // Create two users enrolled in the course as students.
409 list($author, $recipient) = $this->helper_create_users($course, 2);
410
411 // Unsubscribe the 'author' user from the forum.
59075a43 412 \mod_forum\subscriptions::unsubscribe_user($author->id, $forum);
a7a84903
AN
413
414 // Post a discussion to the forum.
415 list($discussion, $post) = $this->helper_post_to_forum($forum, $author);
416
417 // We expect only one user to receive this post.
418 $expected = 1;
419
420 // Run cron and check that the expected number of users received the notification.
421 $messages = $this->helper_run_cron_check_count($post, $expected);
422
423 $seenauthor = false;
424 $seenrecipient = false;
425 foreach ($messages as $message) {
426 // They should both be from our user.
427 $this->assertEquals($author->id, $message->useridfrom);
428
429 if ($message->useridto == $author->id) {
430 $seenauthor = true;
431 } else if ($message->useridto = $recipient->id) {
432 $seenrecipient = true;
433 }
434 }
435
436 // Check we only saw one user.
437 $this->assertFalse($seenauthor);
438 $this->assertTrue($seenrecipient);
439 }
440
441 public function test_optional_with_subscribed_user() {
442 $this->resetAfterTest(true);
443
444 // Create a course, with a forum.
445 $course = $this->getDataGenerator()->create_course();
446
447 $options = array('course' => $course->id, 'forcesubscribe' => FORUM_CHOOSESUBSCRIBE);
448 $forum = $this->getDataGenerator()->create_module('forum', $options);
449
450 // Create two users enrolled in the course as students.
451 list($author, $recipient) = $this->helper_create_users($course, 2);
452
453 // Subscribe the 'recipient' user from the forum.
59075a43 454 \mod_forum\subscriptions::subscribe_user($recipient->id, $forum);
a7a84903
AN
455
456 // Post a discussion to the forum.
457 list($discussion, $post) = $this->helper_post_to_forum($forum, $author);
458
459 // We expect only one user to receive this post.
460 $expected = 1;
461
462 // Run cron and check that the expected number of users received the notification.
463 $messages = $this->helper_run_cron_check_count($post, $expected);
464
465 $seenauthor = false;
466 $seenrecipient = false;
467 foreach ($messages as $message) {
468 // They should both be from our user.
469 $this->assertEquals($author->id, $message->useridfrom);
470
471 if ($message->useridto == $author->id) {
472 $seenauthor = true;
473 } else if ($message->useridto = $recipient->id) {
474 $seenrecipient = true;
475 }
476 }
477
478 // Check we only saw one user.
479 $this->assertFalse($seenauthor);
480 $this->assertTrue($seenrecipient);
481 }
482
49566c8a
AN
483 public function test_automatic_with_unsubscribed_discussion() {
484 $this->resetAfterTest(true);
485
486 // Create a course, with a forum.
487 $course = $this->getDataGenerator()->create_course();
488
489 $options = array('course' => $course->id, 'forcesubscribe' => FORUM_INITIALSUBSCRIBE);
490 $forum = $this->getDataGenerator()->create_module('forum', $options);
491
492 // Create two users enrolled in the course as students.
493 list($author, $recipient) = $this->helper_create_users($course, 2);
494
495 // Post a discussion to the forum.
496 list($discussion, $post) = $this->helper_post_to_forum($forum, $author);
497
498 // Unsubscribe the 'author' user from the discussion.
499 \mod_forum\subscriptions::unsubscribe_user_from_discussion($author->id, $discussion);
500
eb451c79
AN
501 $this->assertFalse(\mod_forum\subscriptions::is_subscribed($author->id, $forum, $discussion->id));
502 $this->assertTrue(\mod_forum\subscriptions::is_subscribed($recipient->id, $forum, $discussion->id));
503
49566c8a
AN
504 // We expect only one user to receive this post.
505 $expected = 1;
506
507 // Run cron and check that the expected number of users received the notification.
508 $messages = $this->helper_run_cron_check_count($post, $expected);
509
510 $seenauthor = false;
511 $seenrecipient = false;
512 foreach ($messages as $message) {
513 // They should both be from our user.
514 $this->assertEquals($author->id, $message->useridfrom);
515
516 if ($message->useridto == $author->id) {
517 $seenauthor = true;
518 } else if ($message->useridto = $recipient->id) {
519 $seenrecipient = true;
520 }
521 }
522
523 // Check we only saw one user.
524 $this->assertFalse($seenauthor);
525 $this->assertTrue($seenrecipient);
526 }
527
528 public function test_optional_with_subscribed_discussion() {
529 $this->resetAfterTest(true);
530
531 // Create a course, with a forum.
532 $course = $this->getDataGenerator()->create_course();
533
534 $options = array('course' => $course->id, 'forcesubscribe' => FORUM_CHOOSESUBSCRIBE);
535 $forum = $this->getDataGenerator()->create_module('forum', $options);
536
537 // Create two users enrolled in the course as students.
538 list($author, $recipient) = $this->helper_create_users($course, 2);
539
540 // Post a discussion to the forum.
541 list($discussion, $post) = $this->helper_post_to_forum($forum, $author);
7122e154 542 $this->helper_update_post_time($post, -90);
49566c8a
AN
543
544 // Subscribe the 'recipient' user to the discussion.
545 \mod_forum\subscriptions::subscribe_user_to_discussion($recipient->id, $discussion);
7122e154
AN
546 $this->helper_update_subscription_time($recipient, $discussion, -60);
547
548 // Initially we don't expect any user to receive this post as you cannot subscribe to a discussion until after
549 // you have read it.
550 $expected = 0;
551
552 // Run cron and check that the expected number of users received the notification.
553 $messages = $this->helper_run_cron_check_count($post, $expected);
554
555 // Have a user reply to the discussion.
556 $reply = $this->helper_post_to_discussion($forum, $discussion, $author);
557 $this->helper_update_post_time($reply, -30);
49566c8a
AN
558
559 // We expect only one user to receive this post.
560 $expected = 1;
561
562 // Run cron and check that the expected number of users received the notification.
7122e154 563 $messages = $this->helper_run_cron_check_count($reply, $expected);
49566c8a
AN
564
565 $seenauthor = false;
566 $seenrecipient = false;
567 foreach ($messages as $message) {
568 // They should both be from our user.
569 $this->assertEquals($author->id, $message->useridfrom);
570
571 if ($message->useridto == $author->id) {
572 $seenauthor = true;
573 } else if ($message->useridto = $recipient->id) {
574 $seenrecipient = true;
575 }
576 }
577
578 // Check we only saw one user.
579 $this->assertFalse($seenauthor);
580 $this->assertTrue($seenrecipient);
581 }
582
583 public function test_automatic_with_subscribed_discussion_in_unsubscribed_forum() {
584 $this->resetAfterTest(true);
585
586 // Create a course, with a forum.
587 $course = $this->getDataGenerator()->create_course();
588
589 $options = array('course' => $course->id, 'forcesubscribe' => FORUM_INITIALSUBSCRIBE);
590 $forum = $this->getDataGenerator()->create_module('forum', $options);
591
592 // Create two users enrolled in the course as students.
593 list($author, $recipient) = $this->helper_create_users($course, 2);
594
595 // Post a discussion to the forum.
596 list($discussion, $post) = $this->helper_post_to_forum($forum, $author);
7122e154 597 $this->helper_update_post_time($post, -90);
49566c8a 598
7122e154 599 // Unsubscribe the 'author' user from the forum.
49566c8a
AN
600 \mod_forum\subscriptions::unsubscribe_user($author->id, $forum);
601
602 // Then re-subscribe them to the discussion.
603 \mod_forum\subscriptions::subscribe_user_to_discussion($author->id, $discussion);
7122e154 604 $this->helper_update_subscription_time($author, $discussion, -60);
49566c8a 605
7122e154
AN
606 // We expect just the user subscribed to the forum to receive this post at the moment as the discussion
607 // subscription time is after the post time.
608 $expected = 1;
49566c8a
AN
609
610 // Run cron and check that the expected number of users received the notification.
611 $messages = $this->helper_run_cron_check_count($post, $expected);
612
613 $seenauthor = false;
614 $seenrecipient = false;
615 foreach ($messages as $message) {
616 // They should both be from our user.
617 $this->assertEquals($author->id, $message->useridfrom);
618
619 if ($message->useridto == $author->id) {
620 $seenauthor = true;
621 } else if ($message->useridto = $recipient->id) {
622 $seenrecipient = true;
623 }
624 }
625
626 // Check we only saw one user.
7122e154
AN
627 $this->assertFalse($seenauthor);
628 $this->assertTrue($seenrecipient);
629
630 // Now post a reply to the original post.
631 $reply = $this->helper_post_to_discussion($forum, $discussion, $author);
632 $this->helper_update_post_time($reply, -30);
633
634 // We expect two users to receive this post.
635 $expected = 2;
636
637 // Run cron and check that the expected number of users received the notification.
638 $messages = $this->helper_run_cron_check_count($reply, $expected);
639
640 $seenauthor = false;
641 $seenrecipient = false;
642 foreach ($messages as $message) {
643 // They should both be from our user.
644 $this->assertEquals($author->id, $message->useridfrom);
645
646 if ($message->useridto == $author->id) {
647 $seenauthor = true;
648 } else if ($message->useridto = $recipient->id) {
649 $seenrecipient = true;
650 }
651 }
652
653 // Check we saw both users.
49566c8a
AN
654 $this->assertTrue($seenauthor);
655 $this->assertTrue($seenrecipient);
656 }
657
658 public function test_optional_with_unsubscribed_discussion_in_subscribed_forum() {
659 $this->resetAfterTest(true);
660
661 // Create a course, with a forum.
662 $course = $this->getDataGenerator()->create_course();
663
664 $options = array('course' => $course->id, 'forcesubscribe' => FORUM_CHOOSESUBSCRIBE);
665 $forum = $this->getDataGenerator()->create_module('forum', $options);
666
667 // Create two users enrolled in the course as students.
668 list($author, $recipient) = $this->helper_create_users($course, 2);
669
670 // Post a discussion to the forum.
671 list($discussion, $post) = $this->helper_post_to_forum($forum, $author);
672
673 // Unsubscribe the 'recipient' user from the discussion.
674 \mod_forum\subscriptions::subscribe_user($recipient->id, $forum);
675
676 // Then unsubscribe them from the discussion.
677 \mod_forum\subscriptions::unsubscribe_user_from_discussion($recipient->id, $discussion);
678
679 // We don't expect any users to receive this post.
680 $expected = 0;
681
682 // Run cron and check that the expected number of users received the notification.
683 $messages = $this->helper_run_cron_check_count($post, $expected);
684 }
eb451c79
AN
685
686 /**
687 * Test that a user unsubscribed from a forum who has subscribed to a discussion, only receives posts made after
688 * they subscribed to the discussion.
689 */
690 public function test_forum_discussion_subscription_forum_unsubscribed_discussion_subscribed_after_post() {
691 $this->resetAfterTest(true);
eb451c79
AN
692
693 // Create a course, with a forum.
694 $course = $this->getDataGenerator()->create_course();
695
696 $options = array('course' => $course->id, 'forcesubscribe' => FORUM_CHOOSESUBSCRIBE);
697 $forum = $this->getDataGenerator()->create_module('forum', $options);
698
699 $expectedmessages = array();
700
701 // Create a user enrolled in the course as a student.
702 list($author) = $this->helper_create_users($course, 1);
703
704 // Post a discussion to the forum.
705 list($discussion, $post) = $this->helper_post_to_forum($forum, $author);
7122e154 706 $this->helper_update_post_time($post, -90);
eb451c79
AN
707
708 $expectedmessages[] = array(
709 'id' => $post->id,
710 'subject' => $post->subject,
711 'count' => 0,
712 );
713
714 // Then subscribe the user to the discussion.
715 $this->assertTrue(\mod_forum\subscriptions::subscribe_user_to_discussion($author->id, $discussion));
7122e154 716 $this->helper_update_subscription_time($author, $discussion, -60);
eb451c79
AN
717
718 // Then post a reply to the first discussion.
7122e154
AN
719 $reply = $this->helper_post_to_discussion($forum, $discussion, $author);
720 $this->helper_update_post_time($reply, -30);
eb451c79
AN
721
722 $expectedmessages[] = array(
723 'id' => $reply->id,
724 'subject' => $reply->subject,
725 'count' => 1,
726 );
727
eb451c79
AN
728 $expectedcount = 1;
729
730 // Run cron and check that the expected number of users received the notification.
731 $messages = $this->helper_run_cron_check_counts($expectedmessages, $expectedcount);
eb451c79 732 }
a7a84903 733}