Skip to content

Commit

Permalink
MDL-66266 core_message: Final deprecation of 3.8 api functions
Browse files Browse the repository at this point in the history
Final deprecation of:
- can_post_message
- get_individual_conversations_between_users
  • Loading branch information
cescobedo committed Jul 22, 2021
1 parent 8453fe0 commit 4631be3
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 236 deletions.
75 changes: 8 additions & 67 deletions message/classes/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -1538,27 +1538,13 @@ public static function count_blocked_users($user = null) {
}

/**
* Determines if a user is permitted to send another user a private message.
* If no sender is provided then it defaults to the logged in user.
*
* @deprecated since 3.8
* @todo Final deprecation in MDL-66266
* @param \stdClass $recipient The user object.
* @param \stdClass|null $sender The user object.
* @return bool true if user is permitted, false otherwise.
*/
public static function can_post_message($recipient, $sender = null) {
global $USER;

debugging('\core_message\api::can_post_message is deprecated, please use ' .
'\core_message\api::can_send_message instead.', DEBUG_DEVELOPER);

if (is_null($sender)) {
// The message is from the logged in user, unless otherwise specified.
$sender = $USER;
}

return self::can_send_message($recipient->id, $sender->id);
public static function can_post_message() {
throw new \coding_exception('
\core_message\api::can_post_message is deprecated and
no longer used, please use
\core_message\api::can_send_message instead.');
}

/**
Expand Down Expand Up @@ -2063,56 +2049,11 @@ public static function get_conversation_between_users(array $userids) {
}

/**
* Returns the conversations between sets of users.
*
* The returned array of results will be in the same order as the requested
* arguments, null will be returned if there is no conversation for that user
* pair.
*
* For example:
* If we have 6 users with ids 1, 2, 3, 4, 5, 6 where only 2 conversations
* exist. One between 1 and 2 and another between 5 and 6.
*
* Then if we call:
* $conversations = get_individual_conversations_between_users([[1,2], [3,4], [5,6]]);
*
* The conversations array will look like:
* [<conv_record>, null, <conv_record>];
*
* Where null is returned for the pairing of [3, 4] since no record exists.
*
* @deprecated since 3.8
* @param array $useridsets An array of arrays where the inner array is the set of user ids
* @return stdClass[] Array of conversation records
*/
public static function get_individual_conversations_between_users(array $useridsets) : array {
global $DB;

debugging('\core_message\api::get_individual_conversations_between_users is deprecated and no longer used',
DEBUG_DEVELOPER);

if (empty($useridsets)) {
return [];
}

$hashes = array_map(function($userids) {
return helper::get_conversation_hash($userids);
}, $useridsets);

list($inorequalsql, $params) = $DB->get_in_or_equal($hashes);
array_unshift($params, self::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL);
$where = "type = ? AND convhash ${inorequalsql}";
$conversations = array_fill(0, count($hashes), null);
$records = $DB->get_records_select('message_conversations', $where, $params);

foreach (array_values($records) as $record) {
$index = array_search($record->convhash, $hashes);
if ($index !== false) {
$conversations[$index] = $record;
}
}

return $conversations;
public static function get_individual_conversations_between_users() {
throw new \coding_exception('\core_message\api::get_individual_conversations_between_users ' .
' is deprecated and no longer used.');
}

/**
Expand Down
170 changes: 1 addition & 169 deletions message/tests/api_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
* @copyright 2016 Mark Nelson <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class core_message_api_testcase extends core_message_messagelib_testcase {
class core_message_api_test extends core_message_messagelib_testcase {

public function test_mark_all_read_for_user_touser() {
$sender = $this->getDataGenerator()->create_user(array('firstname' => 'Test1', 'lastname' => 'User1'));
Expand Down Expand Up @@ -3265,26 +3265,6 @@ public function test_can_send_message_even_if_blocked_shared_course_with_message
$this->assertTrue(\core_message\api::can_send_message($student->id, $teacher->id, true));
}

/**
* Test that calling to can_post_message() now shows debugging. MDL-65093.
*
* @deprecated since 3.8
* @todo Final deprecation in MDL-66266
*/
public function test_can_post_emits_debugging() {
// Create some users.
$user1 = self::getDataGenerator()->create_user();
$user2 = self::getDataGenerator()->create_user();

// Set as the first user.
$this->setUser($user1);

// With the default privacy setting, users can't message them.
$this->assertFalse(\core_message\api::can_post_message($user2));
$this->assertDebuggingCalled('\core_message\api::can_post_message is deprecated, please use ' .
'\core_message\api::can_send_message instead.', DEBUG_DEVELOPER);
}

/**
* Verify the expected behaviour of the can_send_message_to_conversation() method for authenticated users with default settings.
*/
Expand Down Expand Up @@ -5197,154 +5177,6 @@ public function test_update_conversation_name() {
);
}


/**
* Test an empty array returned when no args given.
*/
public function test_get_individual_conversations_between_users_no_user_sets() {
$this->assertEmpty(\core_message\api::get_individual_conversations_between_users([]));
$this->assertDebuggingCalled();
}

/**
* Test a conversation is not returned if there is none.
*/
public function test_get_individual_conversations_between_users_no_conversation() {
$generator = $this->getDataGenerator();
$user1 = $generator->create_user();
$user2 = $generator->create_user();

$this->assertEquals(
[null],
\core_message\api::get_individual_conversations_between_users([[$user1->id, $user2->id]])
);
$this->assertDebuggingCalled();
}

/**
* Test the result set includes null if there is no conversation between users.
*/
public function test_get_individual_conversations_between_users_partial_conversations() {
$generator = $this->getDataGenerator();
$user1 = $generator->create_user();
$user2 = $generator->create_user();
$user3 = $generator->create_user();
$type = \core_message\api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL;

$conversation1 = \core_message\api::create_conversation($type, [$user1->id, $user2->id]);
$conversation2 = \core_message\api::create_conversation($type, [$user1->id, $user3->id]);

$results = \core_message\api::get_individual_conversations_between_users([
[$user1->id, $user2->id],
[$user2->id, $user3->id],
[$user1->id, $user3->id]
]);
$this->assertDebuggingCalled();

$result = array_map(function($result) {
if ($result) {
return $result->id;
} else {
return $result;
}
}, $results);

$this->assertEquals(
[$conversation1->id, null, $conversation2->id],
$result
);
}

/**
* Test all conversations are returned if each set has a conversation.
*/
public function test_get_individual_conversations_between_users_all_conversations() {
$generator = $this->getDataGenerator();
$user1 = $generator->create_user();
$user2 = $generator->create_user();
$user3 = $generator->create_user();
$type = \core_message\api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL;

$conversation1 = \core_message\api::create_conversation($type, [$user1->id, $user2->id]);
$conversation2 = \core_message\api::create_conversation($type, [$user2->id, $user3->id]);
$conversation3 = \core_message\api::create_conversation($type, [$user1->id, $user3->id]);

$results = \core_message\api::get_individual_conversations_between_users([
[$user1->id, $user2->id],
[$user2->id, $user3->id],
[$user1->id, $user3->id]
]);
$this->assertDebuggingCalled();

$result = array_map(function($result) {
if ($result) {
return $result->id;
} else {
return $result;
}
}, $results);

$this->assertEquals(
[$conversation1->id, $conversation2->id, $conversation3->id],
$result
);
}

/**
* Test that the results are ordered to match the order of the parameters.
*/
public function test_get_individual_conversations_between_users_ordering() {
$generator = $this->getDataGenerator();
$user1 = $generator->create_user();
$user2 = $generator->create_user();
$user3 = $generator->create_user();
$type = \core_message\api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL;

$conversation1 = \core_message\api::create_conversation($type, [$user1->id, $user2->id]);
$conversation2 = \core_message\api::create_conversation($type, [$user2->id, $user3->id]);
$conversation3 = \core_message\api::create_conversation($type, [$user1->id, $user3->id]);

$results = \core_message\api::get_individual_conversations_between_users([
[$user1->id, $user2->id],
[$user2->id, $user3->id],
[$user1->id, $user3->id]
]);
$this->assertDebuggingCalled();

$result = array_map(function($result) {
if ($result) {
return $result->id;
} else {
return $result;
}
}, $results);

$this->assertEquals(
[$conversation1->id, $conversation2->id, $conversation3->id],
$result
);

$results = \core_message\api::get_individual_conversations_between_users([
[$user2->id, $user3->id],
[$user1->id, $user2->id],
[$user1->id, $user3->id]
]);
$this->assertDebuggingCalled();

$result = array_map(function($result) {
if ($result) {
return $result->id;
} else {
return $result;
}
}, $results);

$this->assertEquals(
[$conversation2->id, $conversation1->id, $conversation3->id],
$result
);
}

/**
* Test returning members in a conversation with no contact requests.
*/
Expand Down
6 changes: 6 additions & 0 deletions message/upgrade.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
This files describes API changes in /message/ messaging system,
information provided here is intended especially for developers.

=== 4.0 ===

The following functions have been finally deprecated and can not be used anymore:
* can_post_message()
* get_individual_conversations_between_users()

=== 3.11.2 ===

* The `message_page_type_list` method was previouly deprecated, however it was still
Expand Down

0 comments on commit 4631be3

Please sign in to comment.