Skip to content

Commit

Permalink
MDL-68731 forum: fix digests not correctly updating post read statuses
Browse files Browse the repository at this point in the history
  • Loading branch information
usqfowlerj committed Jul 29, 2020
1 parent f001dc4 commit 8351210
Show file tree
Hide file tree
Showing 2 changed files with 115 additions and 1 deletion.
2 changes: 1 addition & 1 deletion mod/forum/classes/task/send_user_digests.php
Expand Up @@ -506,7 +506,7 @@ protected function add_post_body($author, $post, $discussion, $forum, $cm, $cour
$this->log("Adding post {$post->id} in format {$maildigest} without HTML", 2);
}

if ($maildigest == 1 && $CFG->forum_usermarksread) {
if ($maildigest == 1 && !$CFG->forum_usermarksread) {
// Create an array of postid's for this user to mark as read.
$this->markpostsasread[] = $post->id;
}
Expand Down
114 changes: 114 additions & 0 deletions mod/forum/tests/maildigest_test.php
Expand Up @@ -694,4 +694,118 @@ public function test_cron_digest_same_day() {
$digesttime = usergetmidnight(time(), \core_date::get_server_timezone()) + ($CFG->digestmailtime * 3600);
$this->assertLessThanOrEqual($digesttime, $task->nextruntime);
}

/**
* The sending of a digest marks posts as read if automatic message read marking is set.
*/
public function test_cron_digest_marks_posts_read() {
global $DB, $CFG;

$this->resetAfterTest(true);

// Disable the 'Manual message read marking' option.
$CFG->forum_usermarksread = false;

// Set up a basic user enrolled in a course.
$userhelper = $this->helper_setup_user_in_course();
$user = $userhelper->user;
$course1 = $userhelper->courses->course1;
$forum1 = $userhelper->forums->forum1;
$posts = [];

// Set the tested user's default maildigest, trackforums, read tracking settings.
$DB->set_field('user', 'maildigest', 1, ['id' => $user->id]);
$DB->set_field('user', 'trackforums', 1, ['id' => $user->id]);
set_user_preference('forum_markasreadonnotification', 1, $user->id);

// Set the maildigest preference for forum1 to default.
forum_set_user_maildigest($forum1, -1, $user);

// Add 5 discussions to forum 1.
for ($i = 0; $i < 5; $i++) {
list($discussion, $post) = $this->helper_post_to_forum($forum1, $user, ['mailnow' => 1]);
$posts[] = $post;
}

// There should be unread posts for the forum.
$expectedposts = [
$forum1->id => (object) [
'id' => $forum1->id,
'unread' => count($posts),
],
];
$this->assertEquals($expectedposts, forum_tp_get_course_unread_posts($user->id, $course1->id));

// One digest mail should be sent and no other messages.
$expect = [
(object) [
'userid' => $user->id,
'messages' => 0,
'digests' => 1,
],
];
$this->queue_tasks_and_assert($expect);

$this->send_digests_and_assert($user, $posts);

// Verify that there are no unread posts for any forums.
$this->assertEmpty(forum_tp_get_course_unread_posts($user->id, $course1->id));
}

/**
* The sending of a digest does not mark posts as read when manual message read marking is set.
*/
public function test_cron_digest_leaves_posts_unread() {
global $DB, $CFG;

$this->resetAfterTest(true);

// Enable the 'Manual message read marking' option.
$CFG->forum_usermarksread = true;

// Set up a basic user enrolled in a course.
$userhelper = $this->helper_setup_user_in_course();
$user = $userhelper->user;
$course1 = $userhelper->courses->course1;
$forum1 = $userhelper->forums->forum1;
$posts = [];

// Set the tested user's default maildigest, trackforums, read tracking settings.
$DB->set_field('user', 'maildigest', 1, ['id' => $user->id]);
$DB->set_field('user', 'trackforums', 1, ['id' => $user->id]);
set_user_preference('forum_markasreadonnotification', 1, $user->id);

// Set the maildigest preference for forum1 to default.
forum_set_user_maildigest($forum1, -1, $user);

// Add 5 discussions to forum 1.
for ($i = 0; $i < 5; $i++) {
list($discussion, $post) = $this->helper_post_to_forum($forum1, $user, ['mailnow' => 1]);
$posts[] = $post;
}

// There should be unread posts for the forum.
$expectedposts = [
$forum1->id => (object) [
'id' => $forum1->id,
'unread' => count($posts),
],
];
$this->assertEquals($expectedposts, forum_tp_get_course_unread_posts($user->id, $course1->id));

// One digest mail should be sent and no other messages.
$expect = [
(object) [
'userid' => $user->id,
'messages' => 0,
'digests' => 1,
],
];
$this->queue_tasks_and_assert($expect);

$this->send_digests_and_assert($user, $posts);

// Verify that there are still the same unread posts for the forum.
$this->assertEquals($expectedposts, forum_tp_get_course_unread_posts($user->id, $course1->id));
}
}

0 comments on commit 8351210

Please sign in to comment.