Skip to content

Commit

Permalink
MDL-64017 message_email: fixed incorrect unread count
Browse files Browse the repository at this point in the history
  • Loading branch information
mdjnelson committed Apr 23, 2019
1 parent 168d782 commit c015336
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 17 deletions.
3 changes: 2 additions & 1 deletion message/output/email/classes/task/send_email_task.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,8 @@ private function get_conversations_for_user(int $userid) : moodle_recordset {
// isn't related to an actual group in a course. However, for
// now this will have to do before 3.7 code freeze.
// See related MDL-63814.
$sql = "SELECT mc.id, mc.name, c.id as courseid, c.fullname as coursename, g.id as groupid, g.picture, g.hidepicture
$sql = "SELECT DISTINCT mc.id, mc.name, c.id as courseid, c.fullname as coursename, g.id as groupid,
g.picture, g.hidepicture
FROM {message_conversations} mc
JOIN {groups} g
ON mc.itemid = g.id
Expand Down
47 changes: 31 additions & 16 deletions message/output/email/tests/send_email_task_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,31 +91,46 @@ public function test_sending_email_task() {
foreach ($conversations as $conversation) {
$conversationid = $conversation->id;

$message = new \core\message\message();
$message->courseid = 1;
$message->component = 'moodle';
$message->name = 'instantmessage';
$message->userfrom = $user1;
$message->convid = $conversationid;
$message->subject = 'message subject';
$message->fullmessage = 'message body';
$message->fullmessageformat = FORMAT_MARKDOWN;
$message->fullmessagehtml = '<p>message body</p>';
$message->smallmessage = 'small message';
$message->notification = '0';

message_send($message);
// Let's send 5 messages.
for ($i = 1; $i <= 5; $i++) {
$message = new \core\message\message();
$message->courseid = 1;
$message->component = 'moodle';
$message->name = 'instantmessage';
$message->userfrom = $user1;
$message->convid = $conversationid;
$message->subject = 'message subject';
$message->fullmessage = 'message body';
$message->fullmessageformat = FORMAT_MARKDOWN;
$message->fullmessagehtml = '<p>message body</p>';
$message->smallmessage = 'small message';
$message->notification = '0';

message_send($message);
}
}
}

$this->assertEquals(2, $DB->count_records('message_email_messages'));
$this->assertEquals(10, $DB->count_records('message_email_messages'));

// Only 1 email is sent as the 2 messages are included in it at a digest.
// Only 1 email is sent as the messages are included in it at a digest.
$sink = $this->redirectEmails();
$task = new \message_email\task\send_email_task();
$task->execute();
$this->assertEquals(1, $sink->count());

// Confirm it contains the correct data.
$emails = $sink->get_messages();
$email = reset($emails);
$this->assertSame(get_string('emaildigestsubject', 'message_email'), $email->subject);
$this->assertSame($user2->email, $email->to);
$this->assertNotEmpty($email->header);
$emailbody = quoted_printable_decode($email->body);
$this->assertContains('Group 1', $emailbody);
$this->assertContains('Group 2', $emailbody);
// 5 unread messages per conversation, this will be listed twice.
$this->assertRegExp("/<span\b[^>]*>5<\/span> Unread message\w+/", $emailbody);

// Confirm table was emptied after task was run.
$this->assertEquals(0, $DB->count_records('message_email_messages'));

Expand Down

0 comments on commit c015336

Please sign in to comment.