3 // This file is part of Moodle - http://moodle.org/
5 // Moodle is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
10 // Moodle is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
19 * The module forums external functions unit tests
23 * @copyright 2013 Andrew Nicols
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27 defined('MOODLE_INTERNAL') ||
die();
31 class mod_forum_maildigest_testcase
extends advanced_testcase
{
34 * Keep track of the message and mail sinks that we set up for each
37 * @var stdClass $helper
42 * Set up message and mail sinks, and set up other requirements for the
43 * cron to be tested here.
45 public function setUp() {
48 $this->helper
= new stdClass();
50 // Messaging is not compatible with transactions...
51 $this->preventResetByRollback();
54 $this->helper
->messagesink
= $this->redirectMessages();
55 $this->helper
->mailsink
= $this->redirectEmails();
57 // Confirm that we have an empty message sink so far.
58 $messages = $this->helper
->messagesink
->get_messages();
59 $this->assertEquals(0, count($messages));
61 $messages = $this->helper
->mailsink
->get_messages();
62 $this->assertEquals(0, count($messages));
64 // Tell Moodle that we've not sent any digest messages out recently.
65 $CFG->digestmailtimelast
= 0;
67 // And set the digest sending time to a negative number - this has
68 // the effect of making it 11pm the previous day.
69 $CFG->digestmailtime
= -1;
71 // Forcibly reduce the maxeditingtime to a one second to ensure that
72 // messages are sent out.
73 $CFG->maxeditingtime
= 1;
75 // Ensure that we don't prevent e-mail as this will cause unit test failures.
76 $CFG->noemailever
= false;
80 * Clear the message sinks set up in this test.
82 public function tearDown() {
83 $this->helper
->messagesink
->clear();
84 $this->helper
->messagesink
->close();
86 $this->helper
->mailsink
->clear();
87 $this->helper
->mailsink
->close();
91 * Setup a user, course, and forums.
93 * @return stdClass containing the list of forums, courses, forumids,
94 * and the user enrolled in them.
96 protected function helper_setup_user_in_course() {
99 $return = new stdClass();
100 $return->courses
= new stdClass();
101 $return->forums
= new stdClass();
102 $return->forumids
= array();
105 $user = $this->getDataGenerator()->create_user();
106 $return->user
= $user;
108 // Create courses to add the modules.
109 $return->courses
->course1
= $this->getDataGenerator()->create_course();
112 $record = new stdClass();
113 $record->course
= $return->courses
->course1
->id
;
114 $record->forcesubscribe
= 1;
116 $return->forums
->forum1
= $this->getDataGenerator()->create_module('forum', $record);
117 $return->forumsids
[] = $return->forums
->forum1
->id
;
119 $return->forums
->forum2
= $this->getDataGenerator()->create_module('forum', $record);
120 $return->forumsids
[] = $return->forums
->forum2
->id
;
122 // Check the forum was correctly created.
123 list ($test, $params) = $DB->get_in_or_equal($return->forumsids
);
125 // Enrol the user in the courses.
126 // DataGenerator->enrol_user automatically sets a role for the user
127 $this->getDataGenerator()->enrol_user($return->user
->id
, $return->courses
->course1
->id
);
133 * Helper to falsify all forum post records for a digest run.
135 protected function helper_force_digest_mail_times() {
137 // Fake all of the post editing times because digests aren't sent until
138 // the start of an hour where the modification time on the message is before
139 // the start of that hour
140 $digesttime = usergetmidnight(time(), $CFG->timezone
) +
($CFG->digestmailtime
* 3600) - (60 * 60);
141 $DB->set_field('forum_posts', 'modified', $digesttime, array('mailed' => 0));
142 $DB->set_field('forum_posts', 'created', $digesttime, array('mailed' => 0));
146 * Run the forum cron, and check that the specified post was sent the
147 * specified number of times.
149 * @param integer $expected The number of times that the post should have been sent
150 * @return array An array of the messages caught by the message sink
152 protected function helper_run_cron_check_count($expected, $messagecount, $mailcount) {
153 if ($expected === 0) {
154 $this->expectOutputRegex('/(Email digests successfully sent to .* users.){0}/');
156 $this->expectOutputRegex("/Email digests successfully sent to {$expected} users/");
160 // Now check the results in the message sink.
161 $messages = $this->helper
->messagesink
->get_messages();
162 // There should be the expected number of messages.
163 $this->assertEquals($messagecount, count($messages));
165 // Now check the results in the mail sink.
166 $messages = $this->helper
->mailsink
->get_messages();
167 // There should be the expected number of messages.
168 $this->assertEquals($mailcount, count($messages));
173 public function test_set_maildigest() {
176 $this->resetAfterTest(true);
178 $helper = $this->helper_setup_user_in_course();
179 $user = $helper->user
;
180 $course1 = $helper->courses
->course1
;
181 $forum1 = $helper->forums
->forum1
;
184 self
::setUser($helper->user
);
186 // Confirm that there is no current value.
187 $currentsetting = $DB->get_record('forum_digests', array(
188 'forum' => $forum1->id
,
189 'userid' => $user->id
,
191 $this->assertFalse($currentsetting);
193 // Test with each of the valid values:
194 // 0, 1, and 2 are valid values.
195 forum_set_user_maildigest($forum1, 0, $user);
196 $currentsetting = $DB->get_record('forum_digests', array(
197 'forum' => $forum1->id
,
198 'userid' => $user->id
,
200 $this->assertEquals($currentsetting->maildigest
, 0);
202 forum_set_user_maildigest($forum1, 1, $user);
203 $currentsetting = $DB->get_record('forum_digests', array(
204 'forum' => $forum1->id
,
205 'userid' => $user->id
,
207 $this->assertEquals($currentsetting->maildigest
, 1);
209 forum_set_user_maildigest($forum1, 2, $user);
210 $currentsetting = $DB->get_record('forum_digests', array(
211 'forum' => $forum1->id
,
212 'userid' => $user->id
,
214 $this->assertEquals($currentsetting->maildigest
, 2);
216 // And the default value - this should delete the record again
217 forum_set_user_maildigest($forum1, -1, $user);
218 $currentsetting = $DB->get_record('forum_digests', array(
219 'forum' => $forum1->id
,
220 'userid' => $user->id
,
222 $this->assertFalse($currentsetting);
224 // Try with an invalid value.
225 $this->setExpectedException('moodle_exception');
226 forum_set_user_maildigest($forum1, 42, $user);
229 public function test_get_user_digest_options_default() {
232 $this->resetAfterTest(true);
234 // Set up a basic user enrolled in a course.
235 $helper = $this->helper_setup_user_in_course();
236 $user = $helper->user
;
237 $course1 = $helper->courses
->course1
;
238 $forum1 = $helper->forums
->forum1
;
241 self
::setUser($helper->user
);
243 // We test against these options.
244 $digestoptions = array(
245 '0' => get_string('emaildigestoffshort', 'mod_forum'),
246 '1' => get_string('emaildigestcompleteshort', 'mod_forum'),
247 '2' => get_string('emaildigestsubjectsshort', 'mod_forum'),
250 // The default settings is 0.
251 $this->assertEquals(0, $user->maildigest
);
252 $options = forum_get_user_digest_options();
253 $this->assertEquals($options[-1], get_string('emaildigestdefault', 'mod_forum', $digestoptions[0]));
255 // Update the setting to 1.
256 $USER->maildigest
= 1;
257 $this->assertEquals(1, $USER->maildigest
);
258 $options = forum_get_user_digest_options();
259 $this->assertEquals($options[-1], get_string('emaildigestdefault', 'mod_forum', $digestoptions[1]));
261 // Update the setting to 2.
262 $USER->maildigest
= 2;
263 $this->assertEquals(2, $USER->maildigest
);
264 $options = forum_get_user_digest_options();
265 $this->assertEquals($options[-1], get_string('emaildigestdefault', 'mod_forum', $digestoptions[2]));
268 public function test_get_user_digest_options_sorting() {
271 $this->resetAfterTest(true);
273 // Set up a basic user enrolled in a course.
274 $helper = $this->helper_setup_user_in_course();
275 $user = $helper->user
;
276 $course1 = $helper->courses
->course1
;
277 $forum1 = $helper->forums
->forum1
;
280 self
::setUser($helper->user
);
282 // Retrieve the list of applicable options.
283 $options = forum_get_user_digest_options();
285 // The default option must always be at the top of the list.
287 foreach ($options as $value => $description) {
288 $this->assertGreaterThan($lastoption, $value);
289 $lastoption = $value;
293 public function test_cron_no_posts() {
296 $this->resetAfterTest(true);
298 $this->helper_force_digest_mail_times();
300 // Initially the forum cron should generate no messages as we've made no posts.
301 $this->helper_run_cron_check_count(0, 0, 0);
305 * Sends several notifications to one user as:
306 * * single messages based on a user profile setting.
308 public function test_cron_profile_single_mails() {
311 $this->resetAfterTest(true);
313 // Set up a basic user enrolled in a course.
314 $userhelper = $this->helper_setup_user_in_course();
315 $user = $userhelper->user
;
316 $course1 = $userhelper->courses
->course1
;
317 $forum1 = $userhelper->forums
->forum1
;
318 $forum2 = $userhelper->forums
->forum2
;
320 // Add some discussions to the forums.
321 $record = new stdClass();
322 $record->course
= $course1->id
;
323 $record->userid
= $user->id
;
324 $record->mailnow
= 1;
326 // Add 5 discussions to forum 1.
327 $record->forum
= $forum1->id
;
328 for ($i = 0; $i < 5; $i++
) {
329 $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
332 // Add 5 discussions to forum 2.
333 $record->forum
= $forum2->id
;
334 for ($i = 0; $i < 5; $i++
) {
335 $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
338 // Ensure that the creation times mean that the messages will be sent.
339 $this->helper_force_digest_mail_times();
341 // Set the tested user's default maildigest setting.
342 $DB->set_field('user', 'maildigest', 0, array('id' => $user->id
));
344 // Set the maildigest preference for forum1 to default.
345 forum_set_user_maildigest($forum1, -1, $user);
347 // Set the maildigest preference for forum2 to default.
348 forum_set_user_maildigest($forum2, -1, $user);
350 // No digests mails should be sent, but 10 forum mails will be sent.
351 $this->helper_run_cron_check_count(0, 10, 0);
355 * Sends several notifications to one user as:
356 * * daily digests coming from the user profile setting.
358 public function test_cron_profile_digest_email() {
361 $this->resetAfterTest(true);
363 // Set up a basic user enrolled in a course.
364 $userhelper = $this->helper_setup_user_in_course();
365 $user = $userhelper->user
;
366 $course1 = $userhelper->courses
->course1
;
367 $forum1 = $userhelper->forums
->forum1
;
368 $forum2 = $userhelper->forums
->forum2
;
370 // Add a discussion to the forums.
371 $record = new stdClass();
372 $record->course
= $course1->id
;
373 $record->userid
= $user->id
;
374 $record->mailnow
= 1;
376 // Add 5 discussions to forum 1.
377 $record->forum
= $forum1->id
;
378 for ($i = 0; $i < 5; $i++
) {
379 $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
382 // Add 5 discussions to forum 2.
383 $record->forum
= $forum2->id
;
384 for ($i = 0; $i < 5; $i++
) {
385 $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
388 // Ensure that the creation times mean that the messages will be sent.
389 $this->helper_force_digest_mail_times();
391 // Set the tested user's default maildigest setting.
392 $DB->set_field('user', 'maildigest', 1, array('id' => $user->id
));
394 // Set the maildigest preference for forum1 to default.
395 forum_set_user_maildigest($forum1, -1, $user);
397 // Set the maildigest preference for forum2 to default.
398 forum_set_user_maildigest($forum2, -1, $user);
400 // One digest mail should be sent, with no notifications, and one e-mail.
401 $this->helper_run_cron_check_count(1, 0, 1);
405 * Sends several notifications to one user as:
406 * * daily digests coming from the per-forum setting; and
407 * * single e-mails from the profile setting.
409 public function test_cron_mixed_email_1() {
412 $this->resetAfterTest(true);
414 // Set up a basic user enrolled in a course.
415 $userhelper = $this->helper_setup_user_in_course();
416 $user = $userhelper->user
;
417 $course1 = $userhelper->courses
->course1
;
418 $forum1 = $userhelper->forums
->forum1
;
419 $forum2 = $userhelper->forums
->forum2
;
421 // Add a discussion to the forums.
422 $record = new stdClass();
423 $record->course
= $course1->id
;
424 $record->userid
= $user->id
;
425 $record->mailnow
= 1;
427 // Add 5 discussions to forum 1.
428 $record->forum
= $forum1->id
;
429 for ($i = 0; $i < 5; $i++
) {
430 $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
433 // Add 5 discussions to forum 2.
434 $record->forum
= $forum2->id
;
435 for ($i = 0; $i < 5; $i++
) {
436 $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
439 // Ensure that the creation times mean that the messages will be sent.
440 $this->helper_force_digest_mail_times();
442 // Set the tested user's default maildigest setting.
443 $DB->set_field('user', 'maildigest', 0, array('id' => $user->id
));
445 // Set the maildigest preference for forum1 to digest.
446 forum_set_user_maildigest($forum1, 1, $user);
448 // Set the maildigest preference for forum2 to default (single).
449 forum_set_user_maildigest($forum2, -1, $user);
451 // One digest e-mail should be sent, and five individual notifications.
452 $this->helper_run_cron_check_count(1, 5, 1);
456 * Sends several notifications to one user as:
457 * * single e-mails from the per-forum setting; and
458 * * daily digests coming from the per-user setting.
460 public function test_cron_mixed_email_2() {
463 $this->resetAfterTest(true);
465 // Set up a basic user enrolled in a course.
466 $userhelper = $this->helper_setup_user_in_course();
467 $user = $userhelper->user
;
468 $course1 = $userhelper->courses
->course1
;
469 $forum1 = $userhelper->forums
->forum1
;
470 $forum2 = $userhelper->forums
->forum2
;
472 // Add a discussion to the forums.
473 $record = new stdClass();
474 $record->course
= $course1->id
;
475 $record->userid
= $user->id
;
476 $record->mailnow
= 1;
478 // Add 5 discussions to forum 1.
479 $record->forum
= $forum1->id
;
480 for ($i = 0; $i < 5; $i++
) {
481 $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
484 // Add 5 discussions to forum 2.
485 $record->forum
= $forum2->id
;
486 for ($i = 0; $i < 5; $i++
) {
487 $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
490 // Ensure that the creation times mean that the messages will be sent.
491 $this->helper_force_digest_mail_times();
493 // Set the tested user's default maildigest setting.
494 $DB->set_field('user', 'maildigest', 1, array('id' => $user->id
));
496 // Set the maildigest preference for forum1 to digest.
497 forum_set_user_maildigest($forum1, -1, $user);
499 // Set the maildigest preference for forum2 to single.
500 forum_set_user_maildigest($forum2, 0, $user);
502 // One digest e-mail should be sent, and five individual notifications.
503 $this->helper_run_cron_check_count(1, 5, 1);
507 * Sends several notifications to one user as:
508 * * daily digests coming from the per-forum setting.
510 public function test_cron_forum_digest_email() {
513 $this->resetAfterTest(true);
515 // Set up a basic user enrolled in a course.
516 $userhelper = $this->helper_setup_user_in_course();
517 $user = $userhelper->user
;
518 $course1 = $userhelper->courses
->course1
;
519 $forum1 = $userhelper->forums
->forum1
;
520 $forum2 = $userhelper->forums
->forum2
;
522 // Add a discussion to the forums.
523 $record = new stdClass();
524 $record->course
= $course1->id
;
525 $record->userid
= $user->id
;
526 $record->mailnow
= 1;
528 // Add 5 discussions to forum 1.
529 $record->forum
= $forum1->id
;
530 for ($i = 0; $i < 5; $i++
) {
531 $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
534 // Add 5 discussions to forum 2.
535 $record->forum
= $forum2->id
;
536 for ($i = 0; $i < 5; $i++
) {
537 $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
540 // Ensure that the creation times mean that the messages will be sent.
541 $this->helper_force_digest_mail_times();
543 // Set the tested user's default maildigest setting.
544 $DB->set_field('user', 'maildigest', 0, array('id' => $user->id
));
546 // Set the maildigest preference for forum1 to digest (complete).
547 forum_set_user_maildigest($forum1, 1, $user);
549 // Set the maildigest preference for forum2 to digest (short).
550 forum_set_user_maildigest($forum2, 2, $user);
552 // One digest e-mail should be sent, and no individual notifications.
553 $this->helper_run_cron_check_count(1, 0, 1);