Skip to content

Commit

Permalink
Merge branch 'MDL-64047-master' of https://github.com/snake/moodle
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewnicols committed Nov 27, 2018
2 parents e8eae37 + e4088b7 commit 1add1cd
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 1 deletion.
9 changes: 8 additions & 1 deletion message/classes/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -2553,7 +2553,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);
}

/**
Expand Down
35 changes: 35 additions & 0 deletions message/tests/api_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -4754,6 +4754,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.
*/
Expand Down Expand Up @@ -4943,6 +4961,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.
*/
Expand Down
44 changes: 44 additions & 0 deletions message/tests/externallib_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down Expand Up @@ -704,6 +726,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.
*/
Expand Down

0 comments on commit 1add1cd

Please sign in to comment.