From 7c8ba89d5e77449296073ded9b8eaf8b15efbe27 Mon Sep 17 00:00:00 2001 From: Mark Nelson Date: Tue, 4 Dec 2018 10:46:28 +0800 Subject: [PATCH] MDL-64307 core_message: do not include disabled conversations in count --- message/classes/api.php | 4 +++- message/tests/api_test.php | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 1 deletion(-) diff --git a/message/classes/api.php b/message/classes/api.php index c6d280dbd82..c0de3dc164a 100644 --- a/message/classes/api.php +++ b/message/classes/api.php @@ -1482,10 +1482,12 @@ class api { LEFT JOIN {message_user_actions} mua ON (mua.messageid = m.id AND mua.userid = ? AND mua.action = ?) WHERE mcm.userid = ? + AND mc.enabled = ? AND mcm.userid != m.useridfrom AND mua.id is NULL"; - return $DB->count_records_sql($sql, [$user->id, self::MESSAGE_ACTION_READ, $user->id]); + return $DB->count_records_sql($sql, [$user->id, self::MESSAGE_ACTION_READ, $user->id, + self::MESSAGE_CONVERSATION_ENABLED]); } /** diff --git a/message/tests/api_test.php b/message/tests/api_test.php index 1465a553798..02b91ee5d57 100644 --- a/message/tests/api_test.php +++ b/message/tests/api_test.php @@ -3043,6 +3043,44 @@ class core_message_api_testcase extends core_message_messagelib_testcase { $this->assertEquals(1, core_message\api::count_unread_conversations($user2)); } + /** + * Tests counting unread conversations where one conversation is disabled. + */ + public function test_count_unread_conversations_disabled() { + $this->resetAfterTest(true); + + // Create some users. + $user1 = self::getDataGenerator()->create_user(); + $user2 = self::getDataGenerator()->create_user(); + $user3 = self::getDataGenerator()->create_user(); + $user4 = self::getDataGenerator()->create_user(); + + // The person wanting the conversation count. + $this->setUser($user1); + + // Send some messages back and forth, have some different conversations with different users. + $this->send_fake_message($user1, $user2, 'Yo!'); + $this->send_fake_message($user2, $user1, 'Sup mang?'); + $this->send_fake_message($user1, $user2, 'Writing PHPUnit tests!'); + $this->send_fake_message($user2, $user1, 'Word.'); + + $this->send_fake_message($user1, $user3, 'Booyah'); + $this->send_fake_message($user3, $user1, 'Whaaat?'); + $this->send_fake_message($user1, $user3, 'Nothing.'); + $this->send_fake_message($user3, $user1, 'Cool.'); + + $this->send_fake_message($user1, $user4, 'Hey mate, you see the new messaging UI in Moodle?'); + $this->send_fake_message($user4, $user1, 'Yah brah, it\'s pretty rad.'); + $this->send_fake_message($user1, $user4, 'Dope.'); + + // Let's disable the last conversation. + $conversationid = core_message\api::get_conversation_between_users([$user1->id, $user4->id]); + core_message\api::disable_conversation($conversationid); + + // Check that the disabled conversation was not included. + $this->assertEquals(2, core_message\api::count_unread_conversations()); + } + /** * Tests deleting a conversation. */