MDL-48150 mod_forum: Generate message inbound keys for all posts

This code was previously misplaced so that only posts in discussion which
had not yet been processed in the current cron run would have keys
generated. If two posts were generated in the same discussion and sent out
on the same cron cycle, only the first post to be processed would have a
key generated.
This commit is contained in:
Andrew Nicols
2014-11-17 15:47:47 +08:00
parent 2611dd99d3
commit e7f0b4d3dd
2 changed files with 84 additions and 4 deletions
+4 -4
View File
@@ -544,15 +544,15 @@ function forum_cron() {
}
}
// Save the Inbound Message datakey here to reduce DB queries later.
$messageinboundgenerator->set_data($pid);
$messageinboundhandlers[$pid] = $messageinboundgenerator->fetch_data_key();
// Caching subscribed users of each forum.
if (!isset($subscribedusers[$forumid])) {
$modcontext = context_module::instance($coursemodules[$forumid]->id);
if ($subusers = \mod_forum\subscriptions::fetch_subscribed_users($forums[$forumid], 0, $modcontext, 'u.*', true)) {
// Save the Inbound Message datakey here to reduce DB queries later.
$messageinboundgenerator->set_data($pid);
$messageinboundhandlers[$pid] = $messageinboundgenerator->fetch_data_key();
foreach ($subusers as $postuser) {
// this user is subscribed to this forum
$subscribedusers[$forumid][$postuser->id] = $postuser->id;
+80
View File
@@ -78,6 +78,23 @@ class mod_forum_mail_testcase extends advanced_testcase {
$this->helper->mailsink->close();
}
/**
* Perform message inbound setup for the mod_forum reply handler.
*/
protected function helper_spoof_message_inbound_setup() {
global $CFG, $DB;
// Setup the default Inbound Message mailbox settings.
$CFG->messageinbound_domain = 'example.com';
$CFG->messageinbound_enabled = true;
// Must be no longer than 15 characters.
$CFG->messageinbound_mailbox = 'moodlemoodle123';
$record = $DB->get_record('messageinbound_handlers', array('classname' => '\mod_forum\message\inbound\reply_handler'));
$record->enabled = true;
$record->id = $DB->update_record('messageinbound_handlers', $record);
}
/**
* Helper to create the required number of users in the specified
* course.
@@ -730,4 +747,67 @@ class mod_forum_mail_testcase extends advanced_testcase {
// Run cron and check that the expected number of users received the notification.
$messages = $this->helper_run_cron_check_counts($expectedmessages, $expectedcount);
}
public function test_forum_message_inbound_multiple_posts() {
$this->resetAfterTest(true);
// Create a course, with a forum.
$course = $this->getDataGenerator()->create_course();
$options = array('course' => $course->id, 'forcesubscribe' => FORUM_FORCESUBSCRIBE);
$forum = $this->getDataGenerator()->create_module('forum', $options);
// Create a user enrolled in the course as a student.
list($author) = $this->helper_create_users($course, 1);
$expectedmessages = array();
// Post a discussion to the forum.
list($discussion, $post) = $this->helper_post_to_forum($forum, $author);
$this->helper_update_post_time($post, -90);
$expectedmessages[] = array(
'id' => $post->id,
'subject' => $post->subject,
'count' => 0,
);
// Then post a reply to the first discussion.
$reply = $this->helper_post_to_discussion($forum, $discussion, $author);
$this->helper_update_post_time($reply, -60);
$expectedmessages[] = array(
'id' => $reply->id,
'subject' => $reply->subject,
'count' => 1,
);
$expectedcount = 2;
// Ensure that messageinbound is enabled and configured for the forum handler.
$this->helper_spoof_message_inbound_setup();
$author->emailstop = '0';
set_user_preference('message_provider_mod_forum_posts_loggedoff', 'email', $author);
set_user_preference('message_provider_mod_forum_posts_loggedin', 'email', $author);
// Run cron and check that the expected number of users received the notification.
// Clear the mailsink, and close the messagesink.
$this->helper->mailsink->clear();
$this->helper->messagesink->close();
// Cron daily uses mtrace, turn on buffering to silence output.
foreach ($expectedmessages as $post) {
$this->expectOutputRegex("/{$post['count']} users were sent post {$post['id']}, '{$post['subject']}'/");
}
forum_cron();
$messages = $this->helper->mailsink->get_messages();
// There should be the expected number of messages.
$this->assertEquals($expectedcount, count($messages));
foreach ($messages as $message) {
$this->assertRegExp('/Reply-To: moodlemoodle123\+[^@]*@example.com/', $message->header);
}
}
}