diff --git a/message/lib.php b/message/lib.php index d79fdc70421..8367e8170a5 100644 --- a/message/lib.php +++ b/message/lib.php @@ -220,10 +220,12 @@ function message_count_unread_messages($user1=null, $user2=null) { } if (!empty($user2)) { - return $DB->count_records_select('message', "useridto = ? AND useridfrom = ?", + return $DB->count_records_select('message', "useridto = ? AND useridfrom = ? AND notification = 0 + AND timeusertodeleted = 0", array($user1->id, $user2->id), "COUNT('id')"); } else { - return $DB->count_records_select('message', "useridto = ?", + return $DB->count_records_select('message', "useridto = ? AND notification = 0 + AND timeusertodeleted = 0", array($user1->id), "COUNT('id')"); } } diff --git a/message/tests/api_test.php b/message/tests/api_test.php index e0c7c7039c7..35660fc941e 100644 --- a/message/tests/api_test.php +++ b/message/tests/api_test.php @@ -73,7 +73,7 @@ class core_message_api_testcase extends core_message_messagelib_testcase { $this->send_fake_message($sender2, $recipient); \core_message\api::mark_all_read_for_user($recipient->id, $sender1->id); - $this->assertEquals(message_count_unread_messages($recipient), 6); + $this->assertEquals(message_count_unread_messages($recipient), 3); } public function test_mark_all_read_for_user_touser_with_type() { diff --git a/message/tests/messagelib_test.php b/message/tests/messagelib_test.php index 351a337015a..bd4a329b080 100644 --- a/message/tests/messagelib_test.php +++ b/message/tests/messagelib_test.php @@ -194,6 +194,59 @@ class core_message_messagelib_testcase extends advanced_testcase { $this->assertEquals(1, message_count_unread_messages($userto, $userfrom1)); } + /** + * Test message_count_unread_messages with notifications. + */ + public function test_message_count_unread_messages_with_notifications() { + // Create users to send and receive messages. + $userfrom1 = $this->getDataGenerator()->create_user(); + $userfrom2 = $this->getDataGenerator()->create_user(); + $userto = $this->getDataGenerator()->create_user(); + + $this->assertEquals(0, message_count_unread_messages($userto)); + + // Send fake messages. + $this->send_fake_message($userfrom1, $userto); + $this->send_fake_message($userfrom2, $userto); + + // Send fake notifications. + $this->send_fake_message($userfrom1, $userto, 'Notification', 1); + $this->send_fake_message($userfrom2, $userto, 'Notification', 1); + + // Should only count the messages. + $this->assertEquals(2, message_count_unread_messages($userto)); + $this->assertEquals(1, message_count_unread_messages($userto, $userfrom1)); + } + + /** + * Test message_count_unread_messages with deleted messages. + */ + public function test_message_count_unread_messages_with_deleted_messages() { + global $DB; + + // Create users to send and receive messages. + $userfrom1 = $this->getDataGenerator()->create_user(); + $userfrom2 = $this->getDataGenerator()->create_user(); + $userto = $this->getDataGenerator()->create_user(); + + $this->assertEquals(0, message_count_unread_messages($userto)); + + // Send fake messages. + $messageid = $this->send_fake_message($userfrom1, $userto); + $this->send_fake_message($userfrom2, $userto); + + // Send fake notifications. + $this->send_fake_message($userfrom1, $userto, 'Notification', 1); + $this->send_fake_message($userfrom2, $userto, 'Notification', 1); + + // Delete a message. + $message = $DB->get_record('message', array('id' => $messageid)); + message_delete_message($message, $userto->id); + + // Should only count the messages that weren't deleted by the current user. + $this->assertEquals(1, message_count_unread_messages($userto)); + $this->assertEquals(0, message_count_unread_messages($userto, $userfrom1)); + } /** * Test message_add_contact.