MDL-64035 core_message: get_conversations() excludes disabled chats
authorJake Dallimore <jake@moodle.com>
Fri, 16 Nov 2018 00:23:34 +0000 (08:23 +0800)
committerJake Dallimore <jake@moodle.com>
Fri, 16 Nov 2018 04:04:24 +0000 (12:04 +0800)
Linked conversations which have been disabled must be excluded from
the results.

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

index 291e012..7d09a56 100644 (file)
@@ -551,7 +551,8 @@ class api {
                     ON lastmessage.conversationid = mc.id
             LEFT JOIN {messages} m
                    ON m.id = lastmessage.messageid
-                WHERE mc.id IS NOT NULL $typesql $favouritesql
+                WHERE mc.id IS NOT NULL
+                  AND mc.enabled = 1 $typesql $favouritesql
               ORDER BY (CASE WHEN m.timecreated IS NULL THEN 0 ELSE 1 END) DESC, m.timecreated DESC, id DESC";
 
         $params = array_merge($favouriteparams, ['userid' => $userid, 'action' => self::MESSAGE_ACTION_DELETED,
index 145ec68..672b814 100644 (file)
@@ -1440,7 +1440,7 @@ class core_message_api_testcase extends core_message_messagelib_testcase {
      * Test verifying that group linked conversations are returned and contain a subname matching the course name.
      */
     public function test_get_conversations_group_linked() {
-        global $CFG;
+        global $CFG, $DB;
 
         // Create some users.
         $user1 = self::getDataGenerator()->create_user();
@@ -1487,6 +1487,11 @@ class core_message_api_testcase extends core_message_messagelib_testcase {
         $this->assertEquals($course1->shortname, $conversations[0]->subname);
         $groupimageurl = get_group_picture_url($group2, $group2->courseid, true);
         $this->assertEquals($groupimageurl, $conversations[0]->imageurl);
+
+        // Now, disable the conversation linked to the group and verify it's no longer returned.
+        $DB->set_field('message_conversations', 'enabled', 0, ['id' => $conversations[0]->id]);
+        $conversations = \core_message\api::get_conversations($user3->id);
+        $this->assertCount(0, $conversations);
     }
 
    /**
index 54fe35d..6961134 100644 (file)
@@ -5077,7 +5077,7 @@ class core_message_externallib_testcase extends externallib_advanced_testcase {
      */
     public function test_get_conversations_group_linked() {
         $this->resetAfterTest();
-        global $CFG;
+        global $CFG, $DB;
 
         // Create some users.
         $user1 = self::getDataGenerator()->create_user();
@@ -5109,6 +5109,13 @@ class core_message_externallib_testcase extends externallib_advanced_testcase {
         $this->assertEquals($course1->shortname, $conversations[0]['subname']);
         $groupimageurl = get_group_picture_url($group1, $group1->courseid, true);
         $this->assertEquals($groupimageurl, $conversations[0]['imageurl']);
+
+        // Now, disable the conversation linked to the group and verify it's no longer returned.
+        $DB->set_field('message_conversations', 'enabled', 0, ['id' => $conversations[0]['id']]);
+        $result = core_message_external::get_conversations($user1->id, 0, 20, null, false);
+        $result = external_api::clean_returnvalue(core_message_external::get_conversations_returns(), $result);
+        $conversations = $result['conversations'];
+        $this->assertCount(0, $conversations);
     }
 
     /**