From cd029f936df79cc9f4ecd5d77f207dc02040aa16 Mon Sep 17 00:00:00 2001 From: Andrew Nicols Date: Wed, 30 Jan 2019 08:10:10 +0800 Subject: [PATCH] MDL-46881 forum: Only skip post, not discussion --- mod/forum/classes/task/cron_task.php | 2 +- mod/forum/tests/mail_test.php | 46 ++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+), 1 deletion(-) diff --git a/mod/forum/classes/task/cron_task.php b/mod/forum/classes/task/cron_task.php index 5b88606c059..ca3b41e3a8e 100644 --- a/mod/forum/classes/task/cron_task.php +++ b/mod/forum/classes/task/cron_task.php @@ -449,7 +449,7 @@ class cron_task extends \core\task\scheduled_task { $subscribedafter = $subscribedafter && ($subscriptiontime[$post->discussion] > $post->created); if ($subscribedafter) { // The user subscribed to the discussion/forum after this post was created. - unset($poststructure[$courseid][$forumid][$discussionid]); + unset($poststructure[$courseid][$forumid][$discussionid][$postid]); continue; } } diff --git a/mod/forum/tests/mail_test.php b/mod/forum/tests/mail_test.php index 8bf818f94ba..9e1afdfceef 100644 --- a/mod/forum/tests/mail_test.php +++ b/mod/forum/tests/mail_test.php @@ -529,6 +529,52 @@ class mod_forum_mail_testcase extends advanced_testcase { $this->send_notifications_and_assert($recipient, [$reply]); } + public function test_optional_with_subscribed_discussion_and_post() { + $this->resetAfterTest(true); + + // Create a course, with a forum. + $course = $this->getDataGenerator()->create_course(); + + $options = array('course' => $course->id, 'forcesubscribe' => FORUM_CHOOSESUBSCRIBE); + $forum = $this->getDataGenerator()->create_module('forum', $options); + + // Create two users enrolled in the course as students. + list($author, $recipient) = $this->helper_create_users($course, 2); + + // Post a discussion to the forum. + list($discussion, $post) = $this->helper_post_to_forum($forum, $author); + $this->helper_update_post_time($post, -90); + + // Have a user reply to the discussion before we subscribed. + $reply = $this->helper_post_to_discussion($forum, $discussion, $author); + $this->helper_update_post_time($reply, -75); + + // Subscribe the 'recipient' user to the discussion. + \mod_forum\subscriptions::subscribe_user_to_discussion($recipient->id, $discussion); + $this->helper_update_subscription_time($recipient, $discussion, -60); + + // Have a user reply to the discussion. + $reply = $this->helper_post_to_discussion($forum, $discussion, $author); + $this->helper_update_post_time($reply, -30); + + // We expect only one user to receive this post. + // The original post won't be received as it was written before the user subscribed. + $expect = [ + (object) [ + 'userid' => $author->id, + 'messages' => 0, + ], + (object) [ + 'userid' => $recipient->id, + 'messages' => 1, + ], + ]; + $this->queue_tasks_and_assert($expect); + + $this->send_notifications_and_assert($author, []); + $this->send_notifications_and_assert($recipient, [$reply]); + } + public function test_automatic_with_subscribed_discussion_in_unsubscribed_forum() { $this->resetAfterTest(true);