From 0f38e629708ee4ff1d2d357357268a46ca9c19ec Mon Sep 17 00:00:00 2001 From: Amaia Anabitarte Date: Wed, 19 Dec 2018 11:40:16 +0100 Subject: [PATCH] MDL-64400 core_messages: Mark notifications sent via email as read --- message/output/email/message_output_email.php | 6 +++++- mod/assign/tests/locallib_test.php | 7 +++++++ mod/forum/tests/mail_test.php | 5 +++++ 3 files changed, 17 insertions(+), 1 deletion(-) diff --git a/message/output/email/message_output_email.php b/message/output/email/message_output_email.php index 1a7a222d33b..96536ec5d89 100644 --- a/message/output/email/message_output_email.php +++ b/message/output/email/message_output_email.php @@ -37,7 +37,7 @@ class message_output_email extends message_output { * @param object $eventdata the event data submitted by the message sender plus $eventdata->savedmessageid */ function send_message($eventdata) { - global $CFG; + global $CFG, $DB; // skip any messaging suspended and deleted users if ($eventdata->userto->auth === 'nologin' or $eventdata->userto->suspended or $eventdata->userto->deleted) { @@ -93,6 +93,10 @@ class message_output_email extends message_output { $result = email_to_user($recipient, $eventdata->userfrom, $eventdata->subject, $eventdata->fullmessage, $eventdata->fullmessagehtml, $attachment, $attachname, true, $replyto, $replytoname); + if ($result && $notification = $DB->get_record('notifications', ['id' => $eventdata->savedmessageid])) { + \core_message\api::mark_notification_as_read($notification); + } + // Remove an attachment file if any. if (!empty($attachment) && file_exists($attachment)) { unlink($attachment); diff --git a/mod/assign/tests/locallib_test.php b/mod/assign/tests/locallib_test.php index 602d0d94247..c0bce967e11 100644 --- a/mod/assign/tests/locallib_test.php +++ b/mod/assign/tests/locallib_test.php @@ -1678,9 +1678,16 @@ class mod_assign_locallib_testcase extends advanced_testcase { assign::cron(); $events = $sink->get_events(); + // Notification has been marked as read, so now first event should be a 'notification_viewed' one. For student. $event = reset($events); + $this->assertInstanceOf('\core\event\notification_viewed', $event); + $this->assertEquals($student->id, $event->userid); + + // And next event should be the 'notification_sent' one. For teacher. + $event = $events[1]; $this->assertInstanceOf('\core\event\notification_sent', $event); $this->assertEquals($assign->get_course()->id, $event->other['courseid']); + $this->assertEquals($teacher->id, $event->userid); $sink->close(); } diff --git a/mod/forum/tests/mail_test.php b/mod/forum/tests/mail_test.php index 088af18c872..d233252f84d 100644 --- a/mod/forum/tests/mail_test.php +++ b/mod/forum/tests/mail_test.php @@ -286,8 +286,13 @@ class mod_forum_mail_testcase extends advanced_testcase { // Reset the message sink for other tests. $this->helper->messagesink = $this->redirectMessages(); + // Notification has been marked as read, so now first event should be a 'notification_viewed' one. $event = reset($events); + $this->assertInstanceOf('\core\event\notification_viewed', $event); + // And next event should be the 'notification_sent' one. + $event = $events[1]; + $this->assertInstanceOf('\core\event\notification_sent', $event); $this->assertEquals($course->id, $event->other['courseid']); } -- 2.43.0