From e492a5540d59a56c30ffbc8be76c3b40547a76a4 Mon Sep 17 00:00:00 2001 From: Jake Dallimore Date: Mon, 26 Nov 2018 15:14:54 +0800 Subject: [PATCH 1/2] MDL-64047 core_message: contact request count must exclude blocked users --- message/classes/api.php | 9 ++++++++- message/tests/api_test.php | 17 +++++++++++++++++ message/tests/externallib_test.php | 22 ++++++++++++++++++++++ 3 files changed, 47 insertions(+), 1 deletion(-) diff --git a/message/classes/api.php b/message/classes/api.php index d5792239e13a..dd4e22f15e3a 100644 --- a/message/classes/api.php +++ b/message/classes/api.php @@ -2511,7 +2511,14 @@ public static function get_contact_requests(int $userid, int $limitfrom = 0, int */ 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); } /** diff --git a/message/tests/api_test.php b/message/tests/api_test.php index 6f8d26699985..703fb60bebfb 100644 --- a/message/tests/api_test.php +++ b/message/tests/api_test.php @@ -4918,6 +4918,23 @@ public function test_get_received_contact_requests_count() { $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. */ diff --git a/message/tests/externallib_test.php b/message/tests/externallib_test.php index 2d7dea85ac57..938541c8619c 100644 --- a/message/tests/externallib_test.php +++ b/message/tests/externallib_test.php @@ -704,6 +704,28 @@ public function test_get_received_contact_requests_count() { $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. */ From e4088b758209f6fcf6b177034fc8b56b524194dd Mon Sep 17 00:00:00 2001 From: Jake Dallimore Date: Tue, 27 Nov 2018 08:51:40 +0800 Subject: [PATCH 2/2] MDL-64047 core_message: test get_contact_requests() with blocked users --- message/tests/api_test.php | 18 ++++++++++++++++++ message/tests/externallib_test.php | 22 ++++++++++++++++++++++ 2 files changed, 40 insertions(+) diff --git a/message/tests/api_test.php b/message/tests/api_test.php index 703fb60bebfb..f3fe0c4b741e 100644 --- a/message/tests/api_test.php +++ b/message/tests/api_test.php @@ -4729,6 +4729,24 @@ public function test_get_contact_requests() { $this->assertObjectHasAttribute('iscontact', $request); } + /** + * Test the get_contact_requests() function when the user has blocked the sender of the request. + */ + public function test_get_contact_requests_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 requests for User1. + $requests = \core_message\api::get_contact_requests($user1->id); + $this->assertEmpty($requests); + } + /** * Test getting contact requests when there are none. */ diff --git a/message/tests/externallib_test.php b/message/tests/externallib_test.php index 938541c8619c..2cc5436438d6 100644 --- a/message/tests/externallib_test.php +++ b/message/tests/externallib_test.php @@ -587,6 +587,28 @@ public function test_get_contact_requests() { $this->assertArrayHasKey('iscontact', $request); } + /** + * Test the get_contact_requests() function when the user has blocked the sender of the request. + */ + public function test_get_contact_requests_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 requests for User1. + $this->setUser($user1); + $requests = core_message_external::get_contact_requests($user1->id); + $requests = external_api::clean_returnvalue(core_message_external::get_contact_requests_returns(), $requests); + + $this->assertCount(0, $requests); + } + /** * Test getting contact requests when there are none. */