Merge branch 'MDL-64047-master' of https://github.com/snake/moodle

This commit is contained in:
Andrew Nicols 2018-11-27 11:51:48 +08:00
commit 1add1cd644
3 changed files with 87 additions and 1 deletions

View File

@ -2553,7 +2553,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);
}
/**

View File

@ -4754,6 +4754,24 @@ class core_message_api_testcase extends core_message_messagelib_testcase {
$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.
*/
@ -4943,6 +4961,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.
*/

View File

@ -587,6 +587,28 @@ class core_message_externallib_testcase extends externallib_advanced_testcase {
$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.
*/
@ -704,6 +726,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.
*/