MDL-64167 core_message: get_conversations() excludes self conversations
authorJake Dallimore <jake@moodle.com>
Fri, 23 Nov 2018 04:39:23 +0000 (12:39 +0800)
committerJake Dallimore <jake@moodle.com>
Mon, 26 Nov 2018 00:52:55 +0000 (08:52 +0800)
The UI can't handle these, so for now, let's not return them.

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

index 776ca92..b4422be 100644 (file)
@@ -759,6 +759,11 @@ class api {
                 continue;
             }
 
+            // Exclude 'self' conversations for now.
+            if (isset($selfconversations[$conversation->id])) {
+                continue;
+            }
+
             $conv = new \stdClass();
             $conv->id = $conversation->id;
             $conv->name = $conversation->conversationname;
index 6f8d266..65dfb4c 100644 (file)
@@ -1364,6 +1364,31 @@ class core_message_api_testcase extends core_message_messagelib_testcase {
         $conversations = \core_message\api::get_conversations($user1->id, 0, 20, 0);
     }
 
+    /**
+     * Tests retrieving conversations when a legacy 'self' conversation exists.
+     */
+    public function test_get_conversations_legacy_self_conversations() {
+        global $DB;
+
+        // Create a legacy conversation between one user and themself.
+        $user1 = self::getDataGenerator()->create_user();
+        $conversation = \core_message\api::create_conversation(\core_message\api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL,
+            [$user1->id, $user1->id]);
+        testhelper::send_fake_message_to_conversation($user1, $conversation->id, 'Test message to self!');
+
+        // Verify we are in a 'self' conversation state.
+        $members = $DB->get_records('message_conversation_members', ['conversationid' => $conversation->id]);
+        $this->assertCount(2, $members);
+        $member = array_pop($members);
+        $this->assertEquals($user1->id, $member->userid);
+        $member = array_pop($members);
+        $this->assertEquals($user1->id, $member->userid);
+
+        // Verify this conversation is not returned by the method.
+        $conversations = \core_message\api::get_conversations($user1->id);
+        $this->assertCount(0, $conversations);
+    }
+
     /**
      * Tests retrieving conversations when a conversation contains a deleted user.
      */
index 2d7dea8..cb1959e 100644 (file)
@@ -5289,6 +5289,35 @@ class core_message_externallib_testcase extends externallib_advanced_testcase {
         core_message_external::get_conversations($user1->id, 0, 20, 0);
     }
 
+    /**
+     * Tests retrieving conversations when a legacy 'self' conversation exists.
+     */
+    public function test_get_conversations_legacy_self_conversations() {
+        global $DB;
+        $this->resetAfterTest();
+
+        // Create a legacy conversation between one user and themself.
+        $user1 = self::getDataGenerator()->create_user();
+        $conversation = \core_message\api::create_conversation(\core_message\api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL,
+            [$user1->id, $user1->id]);
+        testhelper::send_fake_message_to_conversation($user1, $conversation->id, 'Test message to self!');
+
+        // Verify we are in a 'self' conversation state.
+        $members = $DB->get_records('message_conversation_members', ['conversationid' => $conversation->id]);
+        $this->assertCount(2, $members);
+        $member = array_pop($members);
+        $this->assertEquals($user1->id, $member->userid);
+        $member = array_pop($members);
+        $this->assertEquals($user1->id, $member->userid);
+
+        // Verify this conversation is not returned by the method.
+        $this->setUser($user1);
+        $result = core_message_external::get_conversations($user1->id, 0, 20);
+        $result = external_api::clean_returnvalue(core_message_external::get_conversations_returns(), $result);
+        $conversations = $result['conversations'];
+        $this->assertCount(0, $conversations);
+    }
+
     /**
      * Tests retrieving conversations when a conversation contains a deleted user.
      */