MDL-64047 core_message: contact request count must exclude blocked users
authorJake Dallimore <jake@moodle.com>
Mon, 26 Nov 2018 07:14:54 +0000 (15:14 +0800)
committerJake Dallimore <jake@moodle.com>
Tue, 27 Nov 2018 00:46:25 +0000 (08:46 +0800)
message/classes/api.php
message/tests/api_test.php
message/tests/externallib_test.php

index d579223..dd4e22f 100644 (file)
@@ -2511,7 +2511,14 @@ class api {
      */
     public static function get_received_contact_requests_count(int $userid) : int {
         global $DB;
-        return $DB->count_records('message_contact_requests', ['requesteduserid' => $userid]);
+        $sql = "SELECT COUNT(mcr.id)
+                  FROM {message_contact_requests} mcr
+             LEFT JOIN {message_users_blocked} mub
+                    ON mub.userid = mcr.requesteduserid AND mub.blockeduserid = mcr.userid
+                 WHERE mcr.requesteduserid = :requesteduserid
+                   AND mub.id IS NULL";
+        $params = ['requesteduserid' => $userid];
+        return $DB->count_records_sql($sql, $params);
     }
 
     /**
index 6f8d266..703fb60 100644 (file)
@@ -4918,6 +4918,23 @@ class core_message_api_testcase extends core_message_messagelib_testcase {
         $this->assertEquals(2, \core_message\api::get_received_contact_requests_count($user1->id));
     }
 
+    /**
+     * Test the get_received_contact_requests_count() function when the user has blocked the sender of the request.
+     */
+    public function test_get_received_contact_requests_count_blocked_sender() {
+        $user1 = self::getDataGenerator()->create_user();
+        $user2 = self::getDataGenerator()->create_user();
+
+        // User1 blocks User2.
+        \core_message\api::block_user($user1->id, $user2->id);
+
+        // User2 tries to add User1 as a contact.
+        \core_message\api::create_contact_request($user2->id, $user1->id);
+
+        // Verify we don't see the contact request from the blocked user User2 in the count for User1.
+        $this->assertEquals(0, \core_message\api::get_received_contact_requests_count($user1->id));
+    }
+
     /**
      * Test the get_contact_requests_between_users() function.
      */
index 2d7dea8..938541c 100644 (file)
@@ -704,6 +704,28 @@ class core_message_externallib_testcase extends externallib_advanced_testcase {
         $this->assertEquals(2, $contactrequestnumber);
     }
 
+    /**
+     * Test the get_received_contact_requests_count() function when the user has blocked the sender of the request.
+     */
+    public function test_get_received_contact_requests_count_blocked_sender() {
+        $this->resetAfterTest();
+        $user1 = self::getDataGenerator()->create_user();
+        $user2 = self::getDataGenerator()->create_user();
+
+        // User1 blocks User2.
+        \core_message\api::block_user($user1->id, $user2->id);
+
+        // User2 tries to add User1 as a contact.
+        \core_message\api::create_contact_request($user2->id, $user1->id);
+
+        // Verify we don't see the contact request from the blocked user User2 in the count for User1.
+        $this->setUser($user1);
+        $contactrequestnumber = core_message_external::get_received_contact_requests_count($user1->id);
+        $contactrequestnumber = external_api::clean_returnvalue(
+            core_message_external::get_received_contact_requests_count_returns(), $contactrequestnumber);
+        $this->assertEquals(0, $contactrequestnumber);
+    }
+
     /**
      * Test getting the number of received contact requests with no permissions.
      */