MDL-63913 core_message: fix to ensure favourites is exclusive
authorJake Dallimore <jake@moodle.com>
Mon, 19 Nov 2018 05:51:40 +0000 (13:51 +0800)
committerJake Dallimore <jake@moodle.com>
Mon, 19 Nov 2018 05:51:40 +0000 (13:51 +0800)
If a conversation is included in the favourites unread count, it
cannot be included in any other type-based counts.

message/classes/api.php
message/tests/api_test.php

index 6424ad0..4b485ee 100644 (file)
@@ -2968,10 +2968,11 @@ class api {
             self::MESSAGE_CONVERSATION_TYPE_GROUP => 0
         ]];
         foreach ($unreadcounts as $convid => $info) {
-            $counts['types'][$info->type]++;
             if (isset($favouriteconvids[$convid])) {
                 $counts['favourites']++;
+                continue;
             }
+            $counts['types'][$info->type]++;
         }
 
         return $counts;
index f21039c..5827a34 100644 (file)
@@ -5873,22 +5873,27 @@ class core_message_api_testcase extends core_message_messagelib_testcase {
         list($user1, $user2, $user3, $user4, $ic1, $ic2, $ic3,
             $gc1, $gc2, $gc3, $gc4, $gc5, $gc6) = $this->create_conversation_test_data();
 
+        // Without favourites.
+        $counts = \core_message\api::get_unread_conversation_counts($user1->id);
+        $this->assertEquals(0, $counts['favourites']);
+        $this->assertEquals(2, $counts['types'][\core_message\api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL]);
+        $this->assertEquals(2, $counts['types'][\core_message\api::MESSAGE_CONVERSATION_TYPE_GROUP]);
+
         // Mark a couple as favourites.
         \core_message\api::set_favourite_conversation($ic1->id, $user1->id);
         \core_message\api::set_favourite_conversation($gc2->id, $user1->id);
 
         $counts = \core_message\api::get_unread_conversation_counts($user1->id);
         $this->assertEquals(2, $counts['favourites']);
-        $this->assertEquals(2, $counts['types'][\core_message\api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL]);
-        $this->assertEquals(2, $counts['types'][\core_message\api::MESSAGE_CONVERSATION_TYPE_GROUP]);
-
-        \core_message\api::mark_all_messages_as_read($user1->id, $ic1->id);
+        $this->assertEquals(1, $counts['types'][\core_message\api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL]);
+        $this->assertEquals(1, $counts['types'][\core_message\api::MESSAGE_CONVERSATION_TYPE_GROUP]);
 
         // Mark a conversation as read and confirm it's not included in the unread counts for its respective type.
+        \core_message\api::mark_all_messages_as_read($user1->id, $ic1->id);
         $counts = \core_message\api::get_unread_conversation_counts($user1->id);
         $this->assertEquals(1, $counts['favourites']);
         $this->assertEquals(1, $counts['types'][\core_message\api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL]);
-        $this->assertEquals(2, $counts['types'][\core_message\api::MESSAGE_CONVERSATION_TYPE_GROUP]);
+        $this->assertEquals(1, $counts['types'][\core_message\api::MESSAGE_CONVERSATION_TYPE_GROUP]);
     }
 
     /**