MDL-64017 message_email: fixed incorrect unread count
authorMark Nelson <markn@moodle.com>
Tue, 23 Apr 2019 03:28:01 +0000 (11:28 +0800)
committerMark Nelson <markn@moodle.com>
Tue, 23 Apr 2019 04:15:35 +0000 (12:15 +0800)
message/output/email/classes/task/send_email_task.php
message/output/email/tests/send_email_task_test.php

index 33c4238..c909044 100644 (file)
@@ -133,7 +133,8 @@ class send_email_task extends scheduled_task {
         // 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.
         // 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
                   FROM {message_conversations} mc
                   JOIN {groups} g
                     ON mc.itemid = g.id
index cd59f5d..e292f76 100644 (file)
@@ -91,31 +91,46 @@ class core_message_send_email_task_testcase extends advanced_testcase {
             foreach ($conversations as $conversation) {
                 $conversationid = $conversation->id;
 
             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 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());
 
         $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'));
 
         // Confirm table was emptied after task was run.
         $this->assertEquals(0, $DB->count_records('message_email_messages'));