mirror of
https://github.com/moodle/moodle.git
synced 2025-04-25 10:26:17 +02:00
Merge branch 'MDL-63466-master' of https://github.com/snake/moodle
This commit is contained in:
commit
621e7eb022
@ -55,7 +55,7 @@ $string['cachedef_suspended_userids'] = 'List of suspended users per course';
|
||||
$string['cachedef_groupdata'] = 'Course group information';
|
||||
$string['cachedef_htmlpurifier'] = 'HTML Purifier - cleaned content';
|
||||
$string['cachedef_langmenu'] = 'List of available languages';
|
||||
$string['cachedef_message_time_last_message_between_users'] = 'Time created for most recent message between users';
|
||||
$string['cachedef_message_time_last_message_in_conversation'] = 'Time created for most recent message in a conversation';
|
||||
$string['cachedef_locking'] = 'Locking';
|
||||
$string['cachedef_message_processors_enabled'] = "Message processors enabled status";
|
||||
$string['cachedef_contextwithinsights'] = 'Context with insights';
|
||||
|
@ -38,6 +38,7 @@ $string['contactableprivacy_site'] = 'Anyone on the site';
|
||||
$string['contactblocked'] = 'Contact blocked';
|
||||
$string['contactrequests'] = 'Contact requests';
|
||||
$string['contacts'] = 'Contacts';
|
||||
$string['conversationdoesntexist'] = 'Conversation does not exist';
|
||||
$string['defaultmessageoutputs'] = 'Default message outputs';
|
||||
$string['defaults'] = 'Defaults';
|
||||
$string['deleteallconfirm'] = "Are you sure you would like to delete this entire conversation?";
|
||||
|
@ -34,13 +34,16 @@ defined('MOODLE_INTERNAL') || die();
|
||||
* component string Component name. must exist in message_providers
|
||||
* name string Message type name. must exist in message_providers
|
||||
* userfrom object|int The user sending the message
|
||||
* userto object|int The message recipient
|
||||
* userto object|int The message recipient. This is mandatory for NOTIFICACIONS and 1:1 personal messages.
|
||||
* subject string The message subject
|
||||
* fullmessage string The full message in a given format
|
||||
* fullmessageformat int The format if the full message (FORMAT_MOODLE, FORMAT_HTML, ..)
|
||||
* fullmessagehtml string The full version (the message processor will choose with one to use)
|
||||
* smallmessage string The small version of the message
|
||||
*
|
||||
* Required parameters of the $eventdata object for PERSONAL MESSAGES:
|
||||
* convid int The conversation identifier where this message will be sent
|
||||
*
|
||||
* Optional parameters of the $eventdata object:
|
||||
* notification bool Should the message be considered as a notification rather than a personal message
|
||||
* contexturl string If this is a notification then you can specify a url to view the event.
|
||||
@ -71,6 +74,9 @@ class message {
|
||||
/** @var object|int The user who is sending this message. */
|
||||
private $userfrom;
|
||||
|
||||
/** @var int The conversation id where userfrom is sending this message. */
|
||||
private $convid;
|
||||
|
||||
/** @var object|int The user who is receiving from which is sending this message. */
|
||||
private $userto;
|
||||
|
||||
@ -123,6 +129,7 @@ class message {
|
||||
'component',
|
||||
'name',
|
||||
'userfrom',
|
||||
'convid',
|
||||
'userto',
|
||||
'subject',
|
||||
'fullmessage',
|
||||
|
@ -342,12 +342,12 @@ $definitions = array(
|
||||
'staticaccelerationsize' => 3
|
||||
),
|
||||
|
||||
// Caches the time of the last message between two users.
|
||||
'message_time_last_message_between_users' => array(
|
||||
// Caches the time of the last message in a conversation.
|
||||
'message_time_last_message_in_conversation' => array(
|
||||
'mode' => cache_store::MODE_APPLICATION,
|
||||
'simplekeys' => true, // The id of the sender and recipient is used.
|
||||
'simplekeys' => true, // The conversation id is used.
|
||||
'simplevalues' => true,
|
||||
'datasource' => '\core_message\time_last_message_between_users',
|
||||
'datasource' => '\core_message\time_last_message_in_conversation',
|
||||
),
|
||||
|
||||
// Caches font awesome icons.
|
||||
|
@ -1127,6 +1127,15 @@ $functions = array(
|
||||
'ajax' => true,
|
||||
'services' => array(MOODLE_OFFICIAL_MOBILE_SERVICE),
|
||||
),
|
||||
'core_message_get_conversation_messages' => array(
|
||||
'classname' => 'core_message_external',
|
||||
'methodname' => 'get_conversation_messages',
|
||||
'classpath' => 'message/externallib.php',
|
||||
'description' => 'Retrieve the conversation messages and relevant member information',
|
||||
'type' => 'read',
|
||||
'ajax' => true,
|
||||
'services' => array(MOODLE_OFFICIAL_MOBILE_SERVICE),
|
||||
),
|
||||
'core_message_unblock_user' => array(
|
||||
'classname' => 'core_message_external',
|
||||
'methodname' => 'unblock_user',
|
||||
|
@ -292,11 +292,11 @@ function message_send(\core\message\message $eventdata) {
|
||||
|
||||
// Only cache messages, not notifications.
|
||||
if (!$eventdata->notification) {
|
||||
// Cache the timecreated value of the last message between these two users.
|
||||
$cache = cache::make('core', 'message_time_last_message_between_users');
|
||||
$key = \core_message\helper::get_last_message_time_created_cache_key($eventdata->userfrom->id,
|
||||
$eventdata->userto->id);
|
||||
$cache->set($key, $tabledata->timecreated);
|
||||
if (!empty($eventdata->convid)) {
|
||||
// Cache the timecreated value of the last message in this conversation.
|
||||
$cache = cache::make('core', 'message_time_last_message_in_conversation');
|
||||
$cache->set($eventdata->convid, $tabledata->timecreated);
|
||||
}
|
||||
}
|
||||
|
||||
// Store unread message just in case we get a fatal error any time later.
|
||||
@ -307,7 +307,6 @@ function message_send(\core\message\message $eventdata) {
|
||||
return \core\message\manager::send_message($eventdata, $tabledata, $processorlist);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Updates the message_providers table with the current set of message providers
|
||||
*
|
||||
|
@ -605,6 +605,7 @@ class api {
|
||||
/**
|
||||
* Returns the messages to display in the message area.
|
||||
*
|
||||
* @deprecated since 3.6
|
||||
* @param int $userid the current user
|
||||
* @param int $otheruserid the other user
|
||||
* @param int $limitfrom
|
||||
@ -615,13 +616,22 @@ class api {
|
||||
* @return array
|
||||
*/
|
||||
public static function get_messages($userid, $otheruserid, $limitfrom = 0, $limitnum = 0,
|
||||
$sort = 'timecreated ASC', $timefrom = 0, $timeto = 0) {
|
||||
$sort = 'timecreated ASC', $timefrom = 0, $timeto = 0) {
|
||||
debugging('\core_message\api::get_messages() is deprecated, please use ' .
|
||||
'\core_message\api::get_conversation_messages() instead.', DEBUG_DEVELOPER);
|
||||
|
||||
if (!empty($timefrom)) {
|
||||
// Get the conversation between userid and otheruserid.
|
||||
$userids = [$userid, $otheruserid];
|
||||
if (!$conversationid = self::get_conversation_between_users($userids)) {
|
||||
// This method was always used for individual conversations.
|
||||
$conversation = self::create_conversation(self::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL, $userids);
|
||||
$conversationid = $conversation->id;
|
||||
}
|
||||
|
||||
// Check the cache to see if we even need to do a DB query.
|
||||
$cache = \cache::make('core', 'message_time_last_message_between_users');
|
||||
$key = helper::get_last_message_time_created_cache_key($otheruserid, $userid);
|
||||
$lastcreated = $cache->get($key);
|
||||
$cache = \cache::make('core', 'message_time_last_message_in_conversation');
|
||||
$lastcreated = $cache->get($conversationid);
|
||||
|
||||
// The last known message time is earlier than the one being requested so we can
|
||||
// just return an empty result set rather than having to query the DB.
|
||||
@ -633,21 +643,59 @@ class api {
|
||||
$arrmessages = array();
|
||||
if ($messages = helper::get_messages($userid, $otheruserid, 0, $limitfrom, $limitnum,
|
||||
$sort, $timefrom, $timeto)) {
|
||||
|
||||
$arrmessages = helper::create_messages($userid, $messages);
|
||||
}
|
||||
|
||||
return $arrmessages;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the messages for the defined conversation.
|
||||
*
|
||||
* @param int $userid The current user.
|
||||
* @param int $convid The conversation where the messages belong. Could be an object or just the id.
|
||||
* @param int $limitfrom Return a subset of records, starting at this point (optional).
|
||||
* @param int $limitnum Return a subset comprising this many records in total (optional, required if $limitfrom is set).
|
||||
* @param string $sort The column name to order by including optionally direction.
|
||||
* @param int $timefrom The time from the message being sent.
|
||||
* @param int $timeto The time up until the message being sent.
|
||||
* @return array of messages
|
||||
*/
|
||||
public static function get_conversation_messages(int $userid, int $convid, int $limitfrom = 0, int $limitnum = 0,
|
||||
string $sort = 'timecreated ASC', int $timefrom = 0, int $timeto = 0) : array {
|
||||
|
||||
if (!empty($timefrom)) {
|
||||
// Check the cache to see if we even need to do a DB query.
|
||||
$cache = \cache::make('core', 'message_time_last_message_in_conversation');
|
||||
$lastcreated = $cache->get($convid);
|
||||
|
||||
// The last known message time is earlier than the one being requested so we can
|
||||
// just return an empty result set rather than having to query the DB.
|
||||
if ($lastcreated && $lastcreated < $timefrom) {
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
$arrmessages = array();
|
||||
if ($messages = helper::get_conversation_messages($userid, $convid, 0, $limitfrom, $limitnum, $sort, $timefrom, $timeto)) {
|
||||
$arrmessages = helper::format_conversation_messages($userid, $convid, $messages);
|
||||
}
|
||||
|
||||
return $arrmessages;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the most recent message between two users.
|
||||
*
|
||||
* @deprecated since 3.6
|
||||
* @param int $userid the current user
|
||||
* @param int $otheruserid the other user
|
||||
* @return \stdClass|null
|
||||
*/
|
||||
public static function get_most_recent_message($userid, $otheruserid) {
|
||||
debugging('\core_message\api::get_most_recent_message() is deprecated, please use ' .
|
||||
'\core_message\api::get_most_recent_conversation_message() instead.', DEBUG_DEVELOPER);
|
||||
|
||||
// We want two messages here so we get an accurate 'blocktime' value.
|
||||
if ($messages = helper::get_messages($userid, $otheruserid, 0, 0, 2, 'timecreated DESC')) {
|
||||
// Swap the order so we now have them in historical order.
|
||||
@ -659,6 +707,28 @@ class api {
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the most recent message in a conversation.
|
||||
*
|
||||
* @param int $convid The conversation identifier.
|
||||
* @param int $currentuserid The current user identifier.
|
||||
* @return \stdClass|null The most recent message.
|
||||
*/
|
||||
public static function get_most_recent_conversation_message(int $convid, int $currentuserid = 0) {
|
||||
global $USER;
|
||||
|
||||
if (empty($currentuserid)) {
|
||||
$currentuserid = $USER->id;
|
||||
}
|
||||
|
||||
if ($messages = helper::get_conversation_messages($currentuserid, $convid, 0, 0, 1, 'timecreated DESC')) {
|
||||
$convmessages = helper::format_conversation_messages($currentuserid, $convid, $messages);
|
||||
return array_pop($convmessages['messages']);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the profile information for a contact for a user.
|
||||
*
|
||||
|
@ -37,6 +37,7 @@ class helper {
|
||||
/**
|
||||
* Helper function to retrieve the messages between two users
|
||||
*
|
||||
* @deprecated since 3.6
|
||||
* @param int $userid the current user
|
||||
* @param int $otheruserid the other user
|
||||
* @param int $timedeleted the time the message was deleted
|
||||
@ -49,6 +50,9 @@ class helper {
|
||||
*/
|
||||
public static function get_messages($userid, $otheruserid, $timedeleted = 0, $limitfrom = 0, $limitnum = 0,
|
||||
$sort = 'timecreated ASC', $timefrom = 0, $timeto = 0) {
|
||||
debugging('\core_message\helper::get_messages() is deprecated, please use ' .
|
||||
'\core_message\helper::get_conversation_messages() instead.', DEBUG_DEVELOPER);
|
||||
|
||||
global $DB;
|
||||
|
||||
$hash = self::get_conversation_hash([$userid, $otheruserid]);
|
||||
@ -108,14 +112,132 @@ class helper {
|
||||
return $messages;
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper function to retrieve conversation messages.
|
||||
*
|
||||
* @param int $userid The current user.
|
||||
* @param int $convid The conversation identifier.
|
||||
* @param int $timedeleted The time the message was deleted
|
||||
* @param int $limitfrom Return a subset of records, starting at this point (optional).
|
||||
* @param int $limitnum Return a subset comprising this many records in total (optional, required if $limitfrom is set).
|
||||
* @param string $sort The column name to order by including optionally direction.
|
||||
* @param int $timefrom The time from the message being sent.
|
||||
* @param int $timeto The time up until the message being sent.
|
||||
* @return array of messages
|
||||
*/
|
||||
public static function get_conversation_messages(int $userid, int $convid, int $timedeleted = 0, int $limitfrom = 0,
|
||||
int $limitnum = 0, string $sort = 'timecreated ASC', int $timefrom = 0,
|
||||
int $timeto = 0) : array {
|
||||
global $DB;
|
||||
|
||||
$sql = "SELECT m.id, m.useridfrom, m.subject, m.fullmessage, m.fullmessagehtml,
|
||||
m.fullmessageformat, m.smallmessage, m.timecreated, muaread.timecreated AS timeread
|
||||
FROM {message_conversations} mc
|
||||
INNER JOIN {messages} m
|
||||
ON m.conversationid = mc.id
|
||||
LEFT JOIN {message_user_actions} muaread
|
||||
ON (muaread.messageid = m.id
|
||||
AND muaread.userid = :userid1
|
||||
AND muaread.action = :readaction)";
|
||||
$params = ['userid1' => $userid, 'readaction' => api::MESSAGE_ACTION_READ, 'convid' => $convid];
|
||||
|
||||
if (empty($timedeleted)) {
|
||||
$sql .= " LEFT JOIN {message_user_actions} mua
|
||||
ON (mua.messageid = m.id
|
||||
AND mua.userid = :userid2
|
||||
AND mua.action = :deleteaction
|
||||
AND mua.timecreated is NOT NULL)";
|
||||
} else {
|
||||
$sql .= " INNER JOIN {message_user_actions} mua
|
||||
ON (mua.messageid = m.id
|
||||
AND mua.userid = :userid2
|
||||
AND mua.action = :deleteaction
|
||||
AND mua.timecreated = :timedeleted)";
|
||||
$params['timedeleted'] = $timedeleted;
|
||||
}
|
||||
|
||||
$params['userid2'] = $userid;
|
||||
$params['deleteaction'] = api::MESSAGE_ACTION_DELETED;
|
||||
|
||||
$sql .= " WHERE mc.id = :convid";
|
||||
|
||||
if (!empty($timefrom)) {
|
||||
$sql .= " AND m.timecreated >= :timefrom";
|
||||
$params['timefrom'] = $timefrom;
|
||||
}
|
||||
|
||||
if (!empty($timeto)) {
|
||||
$sql .= " AND m.timecreated <= :timeto";
|
||||
$params['timeto'] = $timeto;
|
||||
}
|
||||
|
||||
if (empty($timedeleted)) {
|
||||
$sql .= " AND mua.id is NULL";
|
||||
}
|
||||
|
||||
$sql .= " ORDER BY m.$sort";
|
||||
|
||||
$messages = $DB->get_records_sql($sql, $params, $limitfrom, $limitnum);
|
||||
|
||||
return $messages;
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper function to return a conversation messages with the involved members (only the ones
|
||||
* who have sent any of these messages).
|
||||
*
|
||||
* @param int $userid The current userid.
|
||||
* @param int $convid The conversation id.
|
||||
* @param array $messages The formated array messages.
|
||||
* @return array A conversation array with the messages and the involved members.
|
||||
*/
|
||||
public static function format_conversation_messages(int $userid, int $convid, array $messages) : array {
|
||||
global $USER;
|
||||
|
||||
// Create the conversation array.
|
||||
$conversation = array(
|
||||
'id' => $convid,
|
||||
);
|
||||
|
||||
// Store the messages.
|
||||
$arrmessages = array();
|
||||
|
||||
foreach ($messages as $message) {
|
||||
// Store the message information.
|
||||
$msg = new \stdClass();
|
||||
$msg->id = $message->id;
|
||||
$msg->useridfrom = $message->useridfrom;
|
||||
$msg->text = message_format_message_text($message);
|
||||
$msg->timecreated = $message->timecreated;
|
||||
$arrmessages[] = $msg;
|
||||
}
|
||||
// Add the messages to the conversation.
|
||||
$conversation['messages'] = $arrmessages;
|
||||
|
||||
// Get the users who have sent any of the $messages.
|
||||
$memberids = array_unique(array_map(function($message) {
|
||||
return $message->useridfrom;
|
||||
}, $messages));
|
||||
// Get members information.
|
||||
$arrmembers = self::get_member_info($userid, $memberids);
|
||||
// Add the members to the conversation.
|
||||
$conversation['members'] = $arrmembers;
|
||||
|
||||
return $conversation;
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper function to return an array of messages.
|
||||
*
|
||||
* @deprecated since 3.6
|
||||
* @param int $userid
|
||||
* @param array $messages
|
||||
* @return array
|
||||
*/
|
||||
public static function create_messages($userid, $messages) {
|
||||
debugging('\core_message\helper::create_messages() is deprecated, please use ' .
|
||||
'\core_message\helper::create_conversation_messages() instead.', DEBUG_DEVELOPER);
|
||||
|
||||
// Store the messages.
|
||||
$arrmessages = array();
|
||||
|
||||
@ -318,19 +440,6 @@ class helper {
|
||||
return sha1(implode('-', $userids));
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the cache key for the time created value of the last message between two users.
|
||||
*
|
||||
* @param int $userid
|
||||
* @param int $user2id
|
||||
* @return string
|
||||
*/
|
||||
public static function get_last_message_time_created_cache_key($userid, $user2id) {
|
||||
$ids = [$userid, $user2id];
|
||||
sort($ids);
|
||||
return implode('_', $ids);
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if legacy messages exist for a given user.
|
||||
*
|
||||
@ -354,4 +463,59 @@ class helper {
|
||||
|
||||
return $messageexists || $messagereadexists;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns conversation member info for the supplied users, relative to the supplied referenceuserid.
|
||||
*
|
||||
* This is the basic structure used when returning members, and includes information about the relationship between each member
|
||||
* and the referenceuser, such as a whether the referenceuser has marked the member as a contact, or has blocked them.
|
||||
*
|
||||
* @param int $referenceuserid the id of the user which check contact and blocked status.
|
||||
* @param array $userids
|
||||
* @return array the array of objects containing member info, indexed by userid.
|
||||
* @throws \coding_exception
|
||||
* @throws \dml_exception
|
||||
*/
|
||||
public static function get_member_info(int $referenceuserid, array $userids) : array {
|
||||
global $DB, $PAGE;
|
||||
|
||||
list($useridsql, $usersparams) = $DB->get_in_or_equal($userids);
|
||||
$userfields = \user_picture::fields('u', array('lastaccess'));
|
||||
$userssql = "SELECT $userfields, mc.id AS contactid, mub.id AS blockedid
|
||||
FROM {user} u
|
||||
LEFT JOIN {message_contacts} mc
|
||||
ON (mc.userid = ? AND mc.contactid = u.id)
|
||||
LEFT JOIN {message_users_blocked} mub
|
||||
ON (mub.userid = ? AND mub.blockeduserid = u.id)
|
||||
WHERE u.id $useridsql
|
||||
AND u.deleted = 0";
|
||||
$usersparams = array_merge([$referenceuserid, $referenceuserid], $usersparams);
|
||||
$otherusers = $DB->get_records_sql($userssql, $usersparams);
|
||||
|
||||
$members = [];
|
||||
foreach ($otherusers as $member) {
|
||||
// Set basic data.
|
||||
$data = new \stdClass();
|
||||
$data->id = $member->id;
|
||||
$data->fullname = fullname($member);
|
||||
|
||||
// Set the user picture data.
|
||||
$userpicture = new \user_picture($member);
|
||||
$userpicture->size = 1; // Size f1.
|
||||
$data->profileimageurl = $userpicture->get_url($PAGE)->out(false);
|
||||
$userpicture->size = 0; // Size f2.
|
||||
$data->profileimageurlsmall = $userpicture->get_url($PAGE)->out(false);
|
||||
|
||||
// Set online status indicators.
|
||||
$data->isonline = self::show_online_status($member) ? self::is_online($member->lastaccess) : null;
|
||||
$data->showonlinestatus = is_null($data->isonline) ? false : true;
|
||||
|
||||
// Set contact and blocked status indicators.
|
||||
$data->iscontact = ($member->contactid) ? true : false;
|
||||
$data->isblocked = ($member->blockedid) ? true : false;
|
||||
|
||||
$members[$data->id] = $data;
|
||||
}
|
||||
return $members;
|
||||
}
|
||||
}
|
||||
|
61
message/classes/tests/helper.php
Normal file
61
message/classes/tests/helper.php
Normal file
@ -0,0 +1,61 @@
|
||||
<?php
|
||||
// This file is part of Moodle - http://moodle.org/
|
||||
//
|
||||
// Moodle is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// Moodle is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
/**
|
||||
* Contains a helper class providing util methods for testing.
|
||||
*
|
||||
* @package core_message
|
||||
* @copyright 2018 Jake Dallimore <markn@moodle.com>
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
|
||||
namespace core_message\tests;
|
||||
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
|
||||
/**
|
||||
* The helper class providing util methods for testing.
|
||||
*
|
||||
* @copyright 2018 Jake Dallimore <markn@moodle.com>
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
class helper {
|
||||
/**
|
||||
* Sends a message to a conversation.
|
||||
*
|
||||
* @param \stdClass $userfrom user object of the one sending the message.
|
||||
* @param int $convid id of the conversation in which we'll send the message.
|
||||
* @param string $message message to send.
|
||||
* @param int $time the time the message was sent.
|
||||
* @return int the id of the message which was sent.
|
||||
* @throws \dml_exception if the conversation doesn't exist.
|
||||
*/
|
||||
public static function send_fake_message_to_conversation(\stdClass $userfrom, int $convid, string $message = 'Hello world!',
|
||||
int $time = null) : int {
|
||||
global $DB;
|
||||
$conversationrec = $DB->get_record('message_conversations', ['id' => $convid], 'id', MUST_EXIST);
|
||||
$conversationid = $conversationrec->id;
|
||||
$time = $time ?? time();
|
||||
$record = new \stdClass();
|
||||
$record->useridfrom = $userfrom->id;
|
||||
$record->conversationid = $conversationid;
|
||||
$record->subject = 'No subject';
|
||||
$record->fullmessage = $message;
|
||||
$record->smallmessage = $message;
|
||||
$record->timecreated = $time;
|
||||
return $DB->insert_record('messages', $record);
|
||||
}
|
||||
}
|
@ -28,16 +28,16 @@ namespace core_message;
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
|
||||
/**
|
||||
* Cache data source for the time of the last message between users.
|
||||
* Cache data source for the time of the last message in a conversation.
|
||||
*
|
||||
* @package core_message
|
||||
* @category cache
|
||||
* @copyright 2016 Ryan Wyllie <ryan@moodle.com>
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
class time_last_message_between_users implements \cache_data_source {
|
||||
class time_last_message_in_conversation implements \cache_data_source {
|
||||
|
||||
/** @var time_last_message_between_users the singleton instance of this class. */
|
||||
/** @var time_last_message_in_conversation the singleton instance of this class. */
|
||||
protected static $instance = null;
|
||||
|
||||
/**
|
||||
@ -49,7 +49,7 @@ class time_last_message_between_users implements \cache_data_source {
|
||||
*/
|
||||
public static function get_instance_for_cache(\cache_definition $definition) {
|
||||
if (is_null(self::$instance)) {
|
||||
self::$instance = new time_last_message_between_users();
|
||||
self::$instance = new time_last_message_in_conversation();
|
||||
}
|
||||
return self::$instance;
|
||||
}
|
||||
@ -61,9 +61,7 @@ class time_last_message_between_users implements \cache_data_source {
|
||||
* @return mixed What ever data should be returned, or false if it can't be loaded.
|
||||
*/
|
||||
public function load_for_cache($key) {
|
||||
list($userid1, $userid2) = explode('_', $key);
|
||||
|
||||
$message = api::get_most_recent_message($userid1, $userid2);
|
||||
$message = api::get_most_recent_conversation_message($key);
|
||||
|
||||
if ($message) {
|
||||
return $message->timecreated;
|
@ -868,6 +868,44 @@ class core_message_external extends external_api {
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the structure of a conversation member.
|
||||
*
|
||||
* @return external_single_structure
|
||||
* @since Moodle 3.6
|
||||
*/
|
||||
private static function get_conversation_member_structure() {
|
||||
return new external_single_structure(
|
||||
array(
|
||||
'id' => new external_value(PARAM_INT, 'The user id'),
|
||||
'fullname' => new external_value(PARAM_NOTAGS, 'The user\'s name'),
|
||||
'profileimageurl' => new external_value(PARAM_URL, 'User picture URL'),
|
||||
'profileimageurlsmall' => new external_value(PARAM_URL, 'Small user picture URL'),
|
||||
'isonline' => new external_value(PARAM_BOOL, 'The user\'s online status'),
|
||||
'showonlinestatus' => new external_value(PARAM_BOOL, 'Show the user\'s online status?'),
|
||||
'isblocked' => new external_value(PARAM_BOOL, 'If the user has been blocked'),
|
||||
'iscontact' => new external_value(PARAM_BOOL, 'Is the user a contact?')
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the structure of a message area message.
|
||||
*
|
||||
* @return external_single_structure
|
||||
* @since Moodle 3.6
|
||||
*/
|
||||
private static function get_conversation_message_structure() {
|
||||
return new external_single_structure(
|
||||
array(
|
||||
'id' => new external_value(PARAM_INT, 'The id of the message'),
|
||||
'useridfrom' => new external_value(PARAM_INT, 'The id of the user who sent the message'),
|
||||
'text' => new external_value(PARAM_RAW, 'The text of the message'),
|
||||
'timecreated' => new external_value(PARAM_INT, 'The timecreated timestamp for the message'),
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the structure of a message area message.
|
||||
*
|
||||
@ -1373,6 +1411,108 @@ class core_message_external extends external_api {
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* The conversation messages parameters.
|
||||
*
|
||||
* @return external_function_parameters
|
||||
* @since 3.6
|
||||
*/
|
||||
public static function get_conversation_messages_parameters() {
|
||||
return new external_function_parameters(
|
||||
array(
|
||||
'currentuserid' => new external_value(PARAM_INT, 'The current user\'s id'),
|
||||
'convid' => new external_value(PARAM_INT, 'The conversation id'),
|
||||
'limitfrom' => new external_value(PARAM_INT, 'Limit from', VALUE_DEFAULT, 0),
|
||||
'limitnum' => new external_value(PARAM_INT, 'Limit number', VALUE_DEFAULT, 0),
|
||||
'newest' => new external_value(PARAM_BOOL, 'Newest first?', VALUE_DEFAULT, false),
|
||||
'timefrom' => new external_value(PARAM_INT,
|
||||
'The timestamp from which the messages were created', VALUE_DEFAULT, 0),
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get conversation messages.
|
||||
*
|
||||
* @param int $currentuserid The current user's id.
|
||||
* @param int $convid The conversation id.
|
||||
* @param int $limitfrom Return a subset of records, starting at this point (optional).
|
||||
* @param int $limitnum Return a subset comprising this many records in total (optional, required if $limitfrom is set).
|
||||
* @param bool $newest True for getting first newest messages, false otherwise.
|
||||
* @param int $timefrom The time from the conversation messages to get.
|
||||
* @return stdClass The messages and members who have sent some of these messages.
|
||||
* @throws moodle_exception
|
||||
* @since 3.6
|
||||
*/
|
||||
public static function get_conversation_messages(int $currentuserid, int $convid, int $limitfrom = 0, int $limitnum = 0,
|
||||
bool $newest = false, int $timefrom = 0) {
|
||||
global $CFG, $PAGE, $USER;
|
||||
|
||||
// Check if messaging is enabled.
|
||||
if (empty($CFG->messaging)) {
|
||||
throw new moodle_exception('disabled', 'message');
|
||||
}
|
||||
|
||||
$systemcontext = context_system::instance();
|
||||
|
||||
$params = array(
|
||||
'currentuserid' => $currentuserid,
|
||||
'convid' => $convid,
|
||||
'limitfrom' => $limitfrom,
|
||||
'limitnum' => $limitnum,
|
||||
'newest' => $newest,
|
||||
'timefrom' => $timefrom,
|
||||
);
|
||||
self::validate_parameters(self::get_conversation_messages_parameters(), $params);
|
||||
self::validate_context($systemcontext);
|
||||
|
||||
if (($USER->id != $currentuserid) && !has_capability('moodle/site:readallmessages', $systemcontext)) {
|
||||
throw new moodle_exception('You do not have permission to perform this action.');
|
||||
}
|
||||
|
||||
$sort = $newest ? 'timecreated DESC' : 'timecreated ASC';
|
||||
|
||||
// We need to enforce a one second delay on messages to avoid race conditions of current
|
||||
// messages still being sent.
|
||||
//
|
||||
// There is a chance that we could request messages before the current time's
|
||||
// second has elapsed and while other messages are being sent in that same second. In which
|
||||
// case those messages will be lost.
|
||||
//
|
||||
// Instead we ignore the current time in the result set to ensure that second is allowed to finish.
|
||||
$timeto = empty($timefrom) ? 0 : time() - 1;
|
||||
|
||||
// No requesting messages from the current time, as stated above.
|
||||
if ($timefrom == time()) {
|
||||
$messages = [];
|
||||
} else {
|
||||
$messages = \core_message\api::get_conversation_messages($currentuserid, $convid, $limitfrom,
|
||||
$limitnum, $sort, $timefrom, $timeto);
|
||||
}
|
||||
|
||||
return $messages;
|
||||
}
|
||||
|
||||
/**
|
||||
* The messagearea messages return structure.
|
||||
*
|
||||
* @return external_single_structure
|
||||
* @since 3.6
|
||||
*/
|
||||
public static function get_conversation_messages_returns() {
|
||||
return new external_single_structure(
|
||||
array(
|
||||
'id' => new external_value(PARAM_INT, 'The conversation id'),
|
||||
'members' => new external_multiple_structure(
|
||||
self::get_conversation_member_structure()
|
||||
),
|
||||
'messages' => new external_multiple_structure(
|
||||
self::get_conversation_message_structure()
|
||||
),
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* The get most recent message return parameters.
|
||||
*
|
||||
|
@ -135,7 +135,52 @@ if (!empty($user2->id)) {
|
||||
$conversations[$user2->id]->isread = 1;
|
||||
}
|
||||
|
||||
$messages = \core_message\api::get_messages($user1->id, $user2->id, 0, 20, 'timecreated DESC');
|
||||
// Get the conversationid.
|
||||
if (!isset($conversationid)) {
|
||||
if (!$conversationid = self::get_conversation_between_users($userids)) {
|
||||
// If the conversationid doesn't exist, throw an exception.
|
||||
throw new moodle_exception('conversationdoesntexist', 'core_message');
|
||||
}
|
||||
}
|
||||
|
||||
$convmessages = \core_message\api::get_conversation_messages($user1->id, $conversationid, 0, 20, 'timecreated DESC');
|
||||
$messages = $convmessages['messages'];
|
||||
|
||||
// Keeps track of the last day, month and year combo we were viewing.
|
||||
$day = '';
|
||||
$month = '';
|
||||
$year = '';
|
||||
|
||||
// Parse the messages to add missing fields for backward compatibility.
|
||||
$messages = array_map(function($message) use ($user1, $user2, $USER, $day, $month, $year) {
|
||||
// Add useridto.
|
||||
if (empty($message->useridto)) {
|
||||
if ($message->useridfrom == $user1->id) {
|
||||
$message->useridto = $user2->id;
|
||||
} else {
|
||||
$message->useridto = $user1->id;
|
||||
}
|
||||
}
|
||||
|
||||
// Add currentuserid.
|
||||
$message->currentuserid = $USER->id;
|
||||
|
||||
// Add displayblocktime.
|
||||
$date = usergetdate($message->timecreated);
|
||||
if ($day != $date['mday'] || $month != $date['month'] || $year != $date['year']) {
|
||||
$day = $date['mday'];
|
||||
$month = $date['month'];
|
||||
$year = $date['year'];
|
||||
$message->displayblocktime = true;
|
||||
} else {
|
||||
$message->displayblocktime = false;
|
||||
}
|
||||
// We don't have this information here so, for now, we leave an empty value.
|
||||
// This is a temporary solution because a new UI is being built in MDL-63303.
|
||||
$message->timeread = 0;
|
||||
|
||||
return $message;
|
||||
}, $messages);
|
||||
}
|
||||
|
||||
$pollmin = !empty($CFG->messagingminpoll) ? $CFG->messagingminpoll : MESSAGE_DEFAULT_MIN_POLL_IN_SECONDS;
|
||||
|
@ -29,6 +29,8 @@ global $CFG;
|
||||
|
||||
require_once($CFG->dirroot . '/message/tests/messagelib_test.php');
|
||||
|
||||
use \core_message\tests\helper as testhelper;
|
||||
|
||||
/**
|
||||
* Test message API.
|
||||
*
|
||||
@ -1321,6 +1323,7 @@ class core_message_api_testcase extends core_message_messagelib_testcase {
|
||||
|
||||
// Retrieve the messages.
|
||||
$messages = \core_message\api::get_messages($user1->id, $user2->id);
|
||||
$this->assertDebuggingCalledCount(3);
|
||||
|
||||
// Confirm the message data is correct.
|
||||
$this->assertEquals(4, count($messages));
|
||||
@ -1351,6 +1354,478 @@ class core_message_api_testcase extends core_message_messagelib_testcase {
|
||||
$this->assertContains('Word.', $message4->text);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests retrieving conversation messages.
|
||||
*/
|
||||
public function test_get_conversation_messages() {
|
||||
// Create some users.
|
||||
$user1 = self::getDataGenerator()->create_user();
|
||||
$user2 = self::getDataGenerator()->create_user();
|
||||
|
||||
// Create conversation.
|
||||
$conversation = \core_message\api::create_conversation(
|
||||
\core_message\api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL,
|
||||
[$user1->id, $user2->id]
|
||||
);
|
||||
|
||||
// The person doing the search.
|
||||
$this->setUser($user1);
|
||||
|
||||
// Send some messages back and forth.
|
||||
$time = 1;
|
||||
testhelper::send_fake_message_to_conversation($user1, $conversation->id, 'Yo!', $time + 1);
|
||||
testhelper::send_fake_message_to_conversation($user2, $conversation->id, 'Sup mang?', $time + 2);
|
||||
testhelper::send_fake_message_to_conversation($user1, $conversation->id, 'Writing PHPUnit tests!', $time + 3);
|
||||
testhelper::send_fake_message_to_conversation($user1, $conversation->id, 'Word.', $time + 4);
|
||||
|
||||
// Retrieve the messages.
|
||||
$convmessages = \core_message\api::get_conversation_messages($user1->id, $conversation->id);
|
||||
|
||||
// Confirm the conversation id is correct.
|
||||
$this->assertEquals($conversation->id, $convmessages['id']);
|
||||
|
||||
// Confirm the message data is correct.
|
||||
$messages = $convmessages['messages'];
|
||||
$this->assertEquals(4, count($messages));
|
||||
$message1 = $messages[0];
|
||||
$message2 = $messages[1];
|
||||
$message3 = $messages[2];
|
||||
$message4 = $messages[3];
|
||||
|
||||
$this->assertEquals($user1->id, $message1->useridfrom);
|
||||
$this->assertContains('Yo!', $message1->text);
|
||||
|
||||
$this->assertEquals($user2->id, $message2->useridfrom);
|
||||
$this->assertContains('Sup mang?', $message2->text);
|
||||
|
||||
$this->assertEquals($user1->id, $message3->useridfrom);
|
||||
$this->assertContains('Writing PHPUnit tests!', $message3->text);
|
||||
|
||||
$this->assertEquals($user1->id, $message4->useridfrom);
|
||||
$this->assertContains('Word.', $message4->text);
|
||||
|
||||
// Confirm the members data is correct.
|
||||
$members = $convmessages['members'];
|
||||
$this->assertEquals(2, count($members));
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests retrieving group conversation messages.
|
||||
*/
|
||||
public function test_get_group_conversation_messages() {
|
||||
// Create some users.
|
||||
$user1 = self::getDataGenerator()->create_user();
|
||||
$user2 = self::getDataGenerator()->create_user();
|
||||
$user3 = self::getDataGenerator()->create_user();
|
||||
$user4 = self::getDataGenerator()->create_user();
|
||||
|
||||
// Create group conversation.
|
||||
$conversation = \core_message\api::create_conversation(
|
||||
\core_message\api::MESSAGE_CONVERSATION_TYPE_GROUP,
|
||||
[$user1->id, $user2->id, $user3->id, $user4->id]
|
||||
);
|
||||
|
||||
// The person doing the search.
|
||||
$this->setUser($user1);
|
||||
|
||||
// Send some messages back and forth.
|
||||
$time = 1;
|
||||
testhelper::send_fake_message_to_conversation($user1, $conversation->id, 'Yo!', $time + 1);
|
||||
testhelper::send_fake_message_to_conversation($user2, $conversation->id, 'Sup mang?', $time + 2);
|
||||
testhelper::send_fake_message_to_conversation($user3, $conversation->id, 'Writing PHPUnit tests!', $time + 3);
|
||||
testhelper::send_fake_message_to_conversation($user1, $conversation->id, 'Word.', $time + 4);
|
||||
testhelper::send_fake_message_to_conversation($user2, $conversation->id, 'Yeah!', $time + 5);
|
||||
|
||||
// Retrieve the messages.
|
||||
$convmessages = \core_message\api::get_conversation_messages($user1->id, $conversation->id);
|
||||
|
||||
// Confirm the conversation id is correct.
|
||||
$this->assertEquals($conversation->id, $convmessages['id']);
|
||||
|
||||
// Confirm the message data is correct.
|
||||
$messages = $convmessages['messages'];
|
||||
$this->assertEquals(5, count($messages));
|
||||
|
||||
$message1 = $messages[0];
|
||||
$message2 = $messages[1];
|
||||
$message3 = $messages[2];
|
||||
$message4 = $messages[3];
|
||||
$message5 = $messages[4];
|
||||
|
||||
$this->assertEquals($user1->id, $message1->useridfrom);
|
||||
$this->assertContains('Yo!', $message1->text);
|
||||
|
||||
$this->assertEquals($user2->id, $message2->useridfrom);
|
||||
$this->assertContains('Sup mang?', $message2->text);
|
||||
|
||||
$this->assertEquals($user3->id, $message3->useridfrom);
|
||||
$this->assertContains('Writing PHPUnit tests!', $message3->text);
|
||||
|
||||
$this->assertEquals($user1->id, $message4->useridfrom);
|
||||
$this->assertContains('Word.', $message4->text);
|
||||
|
||||
$this->assertEquals($user2->id, $message5->useridfrom);
|
||||
$this->assertContains('Yeah!', $message5->text);
|
||||
|
||||
// Confirm the members data is correct.
|
||||
$members = $convmessages['members'];
|
||||
$this->assertEquals(3, count($members));
|
||||
}
|
||||
|
||||
/**
|
||||
* Test verifying the sorting param for get_conversation_messages is respected().
|
||||
*/
|
||||
public function test_get_conversation_messages_sorting() {
|
||||
// Create some users.
|
||||
$user1 = self::getDataGenerator()->create_user();
|
||||
$user2 = self::getDataGenerator()->create_user();
|
||||
$user3 = self::getDataGenerator()->create_user();
|
||||
|
||||
// Create conversations - 1 group and 1 individual.
|
||||
$conversation = \core_message\api::create_conversation(
|
||||
\core_message\api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL,
|
||||
[$user1->id, $user2->id]
|
||||
);
|
||||
$conversation2 = \core_message\api::create_conversation(
|
||||
\core_message\api::MESSAGE_CONVERSATION_TYPE_GROUP,
|
||||
[$user1->id, $user2->id, $user3->id]
|
||||
);
|
||||
|
||||
// Send some messages back and forth.
|
||||
$time = 1;
|
||||
$m1id = testhelper::send_fake_message_to_conversation($user1, $conversation->id, 'Yo!', $time + 1);
|
||||
$m2id = testhelper::send_fake_message_to_conversation($user2, $conversation->id, 'Sup mang?', $time + 2);
|
||||
$m3id = testhelper::send_fake_message_to_conversation($user1, $conversation->id, 'Writing PHPUnit tests!', $time + 3);
|
||||
$m4id = testhelper::send_fake_message_to_conversation($user1, $conversation->id, 'Word.', $time + 4);
|
||||
|
||||
$gm1id = testhelper::send_fake_message_to_conversation($user1, $conversation2->id, 'Yo!', $time + 1);
|
||||
$gm2id = testhelper::send_fake_message_to_conversation($user2, $conversation2->id, 'Sup mang?', $time + 2);
|
||||
$gm3id = testhelper::send_fake_message_to_conversation($user3, $conversation2->id, 'Writing PHPUnit tests!', $time + 3);
|
||||
$gm4id = testhelper::send_fake_message_to_conversation($user1, $conversation2->id, 'Word.', $time + 4);
|
||||
|
||||
// The person doing the search.
|
||||
$this->setUser($user1);
|
||||
|
||||
// Retrieve the messages using default sort ('timecreated ASC') and verify ordering.
|
||||
$convmessages = \core_message\api::get_conversation_messages($user1->id, $conversation->id);
|
||||
$messages = $convmessages['messages'];
|
||||
$this->assertEquals($m1id, $messages[0]->id);
|
||||
$this->assertEquals($m2id, $messages[1]->id);
|
||||
$this->assertEquals($m3id, $messages[2]->id);
|
||||
$this->assertEquals($m4id, $messages[3]->id);
|
||||
|
||||
// Retrieve the messages without specifying DESC sort ordering, and verify ordering.
|
||||
$convmessages = \core_message\api::get_conversation_messages($user1->id, $conversation->id, 0, 0, 'timecreated DESC');
|
||||
$messages = $convmessages['messages'];
|
||||
$this->assertEquals($m1id, $messages[3]->id);
|
||||
$this->assertEquals($m2id, $messages[2]->id);
|
||||
$this->assertEquals($m3id, $messages[1]->id);
|
||||
$this->assertEquals($m4id, $messages[0]->id);
|
||||
|
||||
// Retrieve the messages using default sort ('timecreated ASC') and verify ordering.
|
||||
$convmessages = \core_message\api::get_conversation_messages($user1->id, $conversation2->id);
|
||||
$messages = $convmessages['messages'];
|
||||
$this->assertEquals($gm1id, $messages[0]->id);
|
||||
$this->assertEquals($gm2id, $messages[1]->id);
|
||||
$this->assertEquals($gm3id, $messages[2]->id);
|
||||
$this->assertEquals($gm4id, $messages[3]->id);
|
||||
|
||||
// Retrieve the messages without specifying DESC sort ordering, and verify ordering.
|
||||
$convmessages = \core_message\api::get_conversation_messages($user1->id, $conversation2->id, 0, 0, 'timecreated DESC');
|
||||
$messages = $convmessages['messages'];
|
||||
$this->assertEquals($gm1id, $messages[3]->id);
|
||||
$this->assertEquals($gm2id, $messages[2]->id);
|
||||
$this->assertEquals($gm3id, $messages[1]->id);
|
||||
$this->assertEquals($gm4id, $messages[0]->id);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test retrieving conversation messages by providing a minimum timecreated value.
|
||||
*/
|
||||
public function test_get_conversation_messages_time_from_only() {
|
||||
// Create some users.
|
||||
$user1 = self::getDataGenerator()->create_user();
|
||||
$user2 = self::getDataGenerator()->create_user();
|
||||
$user3 = self::getDataGenerator()->create_user();
|
||||
$user4 = self::getDataGenerator()->create_user();
|
||||
|
||||
// Create group conversation.
|
||||
$conversation = \core_message\api::create_conversation(
|
||||
\core_message\api::MESSAGE_CONVERSATION_TYPE_GROUP,
|
||||
[$user1->id, $user2->id, $user3->id, $user4->id]
|
||||
);
|
||||
|
||||
// The person doing the search.
|
||||
$this->setUser($user1);
|
||||
|
||||
// Send some messages back and forth.
|
||||
$time = 1;
|
||||
testhelper::send_fake_message_to_conversation($user1, $conversation->id, 'Message 1', $time + 1);
|
||||
testhelper::send_fake_message_to_conversation($user2, $conversation->id, 'Message 2', $time + 2);
|
||||
testhelper::send_fake_message_to_conversation($user1, $conversation->id, 'Message 3', $time + 3);
|
||||
testhelper::send_fake_message_to_conversation($user3, $conversation->id, 'Message 4', $time + 4);
|
||||
|
||||
// Retrieve the messages from $time, which should be all of them.
|
||||
$convmessages = \core_message\api::get_conversation_messages($user1->id, $conversation->id, 0, 0, 'timecreated ASC', $time);
|
||||
|
||||
// Confirm the conversation id is correct.
|
||||
$this->assertEquals($conversation->id, $convmessages['id']);
|
||||
|
||||
// Confirm the message data is correct.
|
||||
$messages = $convmessages['messages'];
|
||||
$this->assertEquals(4, count($messages));
|
||||
|
||||
$message1 = $messages[0];
|
||||
$message2 = $messages[1];
|
||||
$message3 = $messages[2];
|
||||
$message4 = $messages[3];
|
||||
|
||||
$this->assertContains('Message 1', $message1->text);
|
||||
$this->assertContains('Message 2', $message2->text);
|
||||
$this->assertContains('Message 3', $message3->text);
|
||||
$this->assertContains('Message 4', $message4->text);
|
||||
|
||||
// Confirm the members data is correct.
|
||||
$members = $convmessages['members'];
|
||||
$this->assertEquals(3, count($members));
|
||||
|
||||
// Retrieve the messages from $time + 3, which should only be the 2 last messages.
|
||||
$convmessages = \core_message\api::get_conversation_messages($user1->id, $conversation->id, 0, 0,
|
||||
'timecreated ASC', $time + 3);
|
||||
|
||||
// Confirm the conversation id is correct.
|
||||
$this->assertEquals($conversation->id, $convmessages['id']);
|
||||
|
||||
// Confirm the message data is correct.
|
||||
$messages = $convmessages['messages'];
|
||||
$this->assertEquals(2, count($messages));
|
||||
|
||||
$message1 = $messages[0];
|
||||
$message2 = $messages[1];
|
||||
|
||||
$this->assertContains('Message 3', $message1->text);
|
||||
$this->assertContains('Message 4', $message2->text);
|
||||
|
||||
// Confirm the members data is correct.
|
||||
$members = $convmessages['members'];
|
||||
$this->assertEquals(2, count($members));
|
||||
}
|
||||
|
||||
/**
|
||||
* Test retrieving conversation messages by providing a maximum timecreated value.
|
||||
*/
|
||||
public function test_get_conversation_messages_time_to_only() {
|
||||
// Create some users.
|
||||
$user1 = self::getDataGenerator()->create_user();
|
||||
$user2 = self::getDataGenerator()->create_user();
|
||||
$user3 = self::getDataGenerator()->create_user();
|
||||
$user4 = self::getDataGenerator()->create_user();
|
||||
|
||||
// Create group conversation.
|
||||
$conversation = \core_message\api::create_conversation(
|
||||
\core_message\api::MESSAGE_CONVERSATION_TYPE_GROUP,
|
||||
[$user1->id, $user2->id, $user3->id, $user4->id]
|
||||
);
|
||||
|
||||
// The person doing the search.
|
||||
$this->setUser($user1);
|
||||
|
||||
// Send some messages back and forth.
|
||||
$time = 1;
|
||||
testhelper::send_fake_message_to_conversation($user1, $conversation->id, 'Message 1', $time + 1);
|
||||
testhelper::send_fake_message_to_conversation($user2, $conversation->id, 'Message 2', $time + 2);
|
||||
testhelper::send_fake_message_to_conversation($user1, $conversation->id, 'Message 3', $time + 3);
|
||||
testhelper::send_fake_message_to_conversation($user3, $conversation->id, 'Message 4', $time + 4);
|
||||
|
||||
// Retrieve the messages up until $time + 4, which should be all of them.
|
||||
$convmessages = \core_message\api::get_conversation_messages($user1->id, $conversation->id, 0, 0, 'timecreated ASC',
|
||||
0, $time + 4);
|
||||
|
||||
// Confirm the conversation id is correct.
|
||||
$this->assertEquals($conversation->id, $convmessages['id']);
|
||||
|
||||
// Confirm the message data is correct.
|
||||
$messages = $convmessages['messages'];
|
||||
$this->assertEquals(4, count($messages));
|
||||
|
||||
$message1 = $messages[0];
|
||||
$message2 = $messages[1];
|
||||
$message3 = $messages[2];
|
||||
$message4 = $messages[3];
|
||||
|
||||
$this->assertContains('Message 1', $message1->text);
|
||||
$this->assertContains('Message 2', $message2->text);
|
||||
$this->assertContains('Message 3', $message3->text);
|
||||
$this->assertContains('Message 4', $message4->text);
|
||||
|
||||
// Confirm the members data is correct.
|
||||
$members = $convmessages['members'];
|
||||
$this->assertEquals(3, count($members));
|
||||
|
||||
// Retrieve the messages up until $time + 2, which should be the first two.
|
||||
$convmessages = \core_message\api::get_conversation_messages($user1->id, $conversation->id, 0, 0, 'timecreated ASC',
|
||||
0, $time + 2);
|
||||
|
||||
// Confirm the conversation id is correct.
|
||||
$this->assertEquals($conversation->id, $convmessages['id']);
|
||||
|
||||
// Confirm the message data is correct.
|
||||
$messages = $convmessages['messages'];
|
||||
$this->assertEquals(2, count($messages));
|
||||
|
||||
$message1 = $messages[0];
|
||||
$message2 = $messages[1];
|
||||
|
||||
$this->assertContains('Message 1', $message1->text);
|
||||
$this->assertContains('Message 2', $message2->text);
|
||||
|
||||
// Confirm the members data is correct.
|
||||
$members = $convmessages['members'];
|
||||
$this->assertEquals(2, count($members));
|
||||
}
|
||||
|
||||
/**
|
||||
* Test retrieving conversation messages by providing a minimum and maximum timecreated value.
|
||||
*/
|
||||
public function test_get_conversation_messages_time_from_and_to() {
|
||||
// Create some users.
|
||||
$user1 = self::getDataGenerator()->create_user();
|
||||
$user2 = self::getDataGenerator()->create_user();
|
||||
$user3 = self::getDataGenerator()->create_user();
|
||||
$user4 = self::getDataGenerator()->create_user();
|
||||
|
||||
// Create group conversation.
|
||||
$conversation = \core_message\api::create_conversation(
|
||||
\core_message\api::MESSAGE_CONVERSATION_TYPE_GROUP,
|
||||
[$user1->id, $user2->id, $user3->id, $user4->id]
|
||||
);
|
||||
|
||||
// The person doing the search.
|
||||
$this->setUser($user1);
|
||||
|
||||
// Send some messages back and forth.
|
||||
$time = 1;
|
||||
testhelper::send_fake_message_to_conversation($user1, $conversation->id, 'Message 1', $time + 1);
|
||||
testhelper::send_fake_message_to_conversation($user2, $conversation->id, 'Message 2', $time + 2);
|
||||
testhelper::send_fake_message_to_conversation($user1, $conversation->id, 'Message 3', $time + 3);
|
||||
testhelper::send_fake_message_to_conversation($user3, $conversation->id, 'Message 4', $time + 4);
|
||||
|
||||
// Retrieve the messages from $time + 2 up until $time + 3, which should be 2nd and 3rd message.
|
||||
$convmessages = \core_message\api::get_conversation_messages($user1->id, $conversation->id, 0, 0,
|
||||
'timecreated ASC', $time + 2, $time + 3);
|
||||
|
||||
// Confirm the conversation id is correct.
|
||||
$this->assertEquals($conversation->id, $convmessages['id']);
|
||||
|
||||
// Confirm the message data is correct.
|
||||
$messages = $convmessages['messages'];
|
||||
$this->assertEquals(2, count($messages));
|
||||
|
||||
$message1 = $messages[0];
|
||||
$message2 = $messages[1];
|
||||
|
||||
$this->assertContains('Message 2', $message1->text);
|
||||
$this->assertContains('Message 3', $message2->text);
|
||||
|
||||
// Confirm the members data is correct.
|
||||
$members = $convmessages['members'];
|
||||
$this->assertEquals(2, count($members));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Test retrieving conversation messages by providing a limitfrom value.
|
||||
*/
|
||||
public function test_get_conversation_messages_limitfrom_only() {
|
||||
// Create some users.
|
||||
$user1 = self::getDataGenerator()->create_user();
|
||||
$user2 = self::getDataGenerator()->create_user();
|
||||
$user3 = self::getDataGenerator()->create_user();
|
||||
$user4 = self::getDataGenerator()->create_user();
|
||||
|
||||
// Create group conversation.
|
||||
$conversation = \core_message\api::create_conversation(
|
||||
\core_message\api::MESSAGE_CONVERSATION_TYPE_GROUP,
|
||||
[$user1->id, $user2->id, $user3->id, $user4->id]
|
||||
);
|
||||
|
||||
// The person doing the search.
|
||||
$this->setUser($user1);
|
||||
|
||||
// Send some messages back and forth.
|
||||
$time = 1;
|
||||
testhelper::send_fake_message_to_conversation($user1, $conversation->id, 'Message 1', $time + 1);
|
||||
testhelper::send_fake_message_to_conversation($user2, $conversation->id, 'Message 2', $time + 2);
|
||||
testhelper::send_fake_message_to_conversation($user1, $conversation->id, 'Message 3', $time + 3);
|
||||
testhelper::send_fake_message_to_conversation($user3, $conversation->id, 'Message 4', $time + 4);
|
||||
|
||||
// Retrieve the messages from $time, which should be all of them.
|
||||
$convmessages = \core_message\api::get_conversation_messages($user1->id, $conversation->id, 2);
|
||||
|
||||
// Confirm the conversation id is correct.
|
||||
$messages = $convmessages['messages'];
|
||||
$this->assertEquals($conversation->id, $convmessages['id']);
|
||||
|
||||
// Confirm the message data is correct.
|
||||
$this->assertEquals(2, count($messages));
|
||||
|
||||
$message1 = $messages[0];
|
||||
$message2 = $messages[1];
|
||||
|
||||
$this->assertContains('Message 3', $message1->text);
|
||||
$this->assertContains('Message 4', $message2->text);
|
||||
|
||||
// Confirm the members data is correct.
|
||||
$members = $convmessages['members'];
|
||||
$this->assertEquals(2, count($members));
|
||||
}
|
||||
|
||||
/**
|
||||
* Test retrieving conversation messages by providing a limitnum value.
|
||||
*/
|
||||
public function test_get_conversation_messages_limitnum() {
|
||||
// Create some users.
|
||||
$user1 = self::getDataGenerator()->create_user();
|
||||
$user2 = self::getDataGenerator()->create_user();
|
||||
$user3 = self::getDataGenerator()->create_user();
|
||||
$user4 = self::getDataGenerator()->create_user();
|
||||
|
||||
// Create group conversation.
|
||||
$conversation = \core_message\api::create_conversation(
|
||||
\core_message\api::MESSAGE_CONVERSATION_TYPE_GROUP,
|
||||
[$user1->id, $user2->id, $user3->id, $user4->id]
|
||||
);
|
||||
|
||||
// The person doing the search.
|
||||
$this->setUser($user1);
|
||||
|
||||
// Send some messages back and forth.
|
||||
$time = 1;
|
||||
testhelper::send_fake_message_to_conversation($user1, $conversation->id, 'Message 1', $time + 1);
|
||||
testhelper::send_fake_message_to_conversation($user2, $conversation->id, 'Message 2', $time + 2);
|
||||
testhelper::send_fake_message_to_conversation($user1, $conversation->id, 'Message 3', $time + 3);
|
||||
testhelper::send_fake_message_to_conversation($user3, $conversation->id, 'Message 4', $time + 4);
|
||||
|
||||
// Retrieve the messages from $time, which should be all of them.
|
||||
$convmessages = \core_message\api::get_conversation_messages($user1->id, $conversation->id, 2, 1);
|
||||
|
||||
// Confirm the conversation id is correct.
|
||||
$messages = $convmessages['messages'];
|
||||
$this->assertEquals($conversation->id, $convmessages['id']);
|
||||
|
||||
// Confirm the message data is correct.
|
||||
$messages = $convmessages['messages'];
|
||||
$this->assertEquals(1, count($messages));
|
||||
|
||||
$message1 = $messages[0];
|
||||
|
||||
$this->assertContains('Message 3', $message1->text);
|
||||
|
||||
// Confirm the members data is correct.
|
||||
$members = $convmessages['members'];
|
||||
$this->assertEquals(1, count($members));
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests retrieving most recent message.
|
||||
*/
|
||||
@ -1371,6 +1846,7 @@ class core_message_api_testcase extends core_message_messagelib_testcase {
|
||||
|
||||
// Retrieve the most recent messages.
|
||||
$message = \core_message\api::get_most_recent_message($user1->id, $user2->id);
|
||||
$this->assertDebuggingCalledCount(3);
|
||||
|
||||
// Check the results are correct.
|
||||
$this->assertEquals($user2->id, $message->useridfrom);
|
||||
@ -1378,6 +1854,39 @@ class core_message_api_testcase extends core_message_messagelib_testcase {
|
||||
$this->assertContains('Word.', $message->text);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests retrieving most recent conversation message.
|
||||
*/
|
||||
public function test_get_most_recent_conversation_message() {
|
||||
// Create some users.
|
||||
$user1 = self::getDataGenerator()->create_user();
|
||||
$user2 = self::getDataGenerator()->create_user();
|
||||
$user3 = self::getDataGenerator()->create_user();
|
||||
|
||||
// Create group conversation.
|
||||
$conversation = \core_message\api::create_conversation(
|
||||
\core_message\api::MESSAGE_CONVERSATION_TYPE_GROUP,
|
||||
[$user1->id, $user2->id, $user3->id]
|
||||
);
|
||||
|
||||
// The person getting the most recent conversation message.
|
||||
$this->setUser($user1);
|
||||
|
||||
// Send some messages back and forth.
|
||||
$time = 1;
|
||||
testhelper::send_fake_message_to_conversation($user1, $conversation->id, 'Yo!', $time + 1);
|
||||
testhelper::send_fake_message_to_conversation($user2, $conversation->id, 'Sup mang?', $time + 2);
|
||||
testhelper::send_fake_message_to_conversation($user1, $conversation->id, 'Writing PHPUnit tests!', $time + 3);
|
||||
testhelper::send_fake_message_to_conversation($user2, $conversation->id, 'Word.', $time + 4);
|
||||
|
||||
// Retrieve the most recent messages.
|
||||
$message = \core_message\api::get_most_recent_conversation_message($conversation->id, $user1->id);
|
||||
|
||||
// Check the results are correct.
|
||||
$this->assertEquals($user2->id, $message->useridfrom);
|
||||
$this->assertContains('Word.', $message->text);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests retrieving a user's profile.
|
||||
*/
|
||||
@ -2105,6 +2614,7 @@ class core_message_api_testcase extends core_message_messagelib_testcase {
|
||||
|
||||
// Retrieve the messages from $time, which should be all of them.
|
||||
$messages = \core_message\api::get_messages($user1->id, $user2->id, 0, 0, 'timecreated ASC', $time);
|
||||
$this->assertDebuggingCalledCount(3);
|
||||
|
||||
// Confirm the message data is correct.
|
||||
$this->assertEquals(4, count($messages));
|
||||
@ -2121,6 +2631,7 @@ class core_message_api_testcase extends core_message_messagelib_testcase {
|
||||
|
||||
// Retrieve the messages from $time + 3, which should only be the 2 last messages.
|
||||
$messages = \core_message\api::get_messages($user1->id, $user2->id, 0, 0, 'timecreated ASC', $time + 3);
|
||||
$this->assertDebuggingCalledCount(3);
|
||||
|
||||
// Confirm the message data is correct.
|
||||
$this->assertEquals(2, count($messages));
|
||||
@ -2152,6 +2663,7 @@ class core_message_api_testcase extends core_message_messagelib_testcase {
|
||||
|
||||
// Retrieve the messages up until $time + 4, which should be all of them.
|
||||
$messages = \core_message\api::get_messages($user1->id, $user2->id, 0, 0, 'timecreated ASC', 0, $time + 4);
|
||||
$this->assertDebuggingCalledCount(3);
|
||||
|
||||
// Confirm the message data is correct.
|
||||
$this->assertEquals(4, count($messages));
|
||||
@ -2168,6 +2680,7 @@ class core_message_api_testcase extends core_message_messagelib_testcase {
|
||||
|
||||
// Retrieve the messages up until $time + 2, which should be the first two.
|
||||
$messages = \core_message\api::get_messages($user1->id, $user2->id, 0, 0, 'timecreated ASC', 0, $time + 2);
|
||||
$this->assertDebuggingCalledCount(3);
|
||||
|
||||
// Confirm the message data is correct.
|
||||
$this->assertEquals(2, count($messages));
|
||||
@ -2199,6 +2712,7 @@ class core_message_api_testcase extends core_message_messagelib_testcase {
|
||||
|
||||
// Retrieve the messages from $time + 2 up until $time + 3, which should be 2nd and 3rd message.
|
||||
$messages = \core_message\api::get_messages($user1->id, $user2->id, 0, 0, 'timecreated ASC', $time + 2, $time + 3);
|
||||
$this->assertDebuggingCalledCount(3);
|
||||
|
||||
// Confirm the message data is correct.
|
||||
$this->assertEquals(2, count($messages));
|
||||
|
@ -30,6 +30,8 @@ global $CFG;
|
||||
require_once($CFG->dirroot . '/webservice/tests/helpers.php');
|
||||
require_once($CFG->dirroot . '/message/externallib.php');
|
||||
|
||||
use \core_message\tests\helper as testhelper;
|
||||
|
||||
class core_message_externallib_testcase extends externallib_advanced_testcase {
|
||||
|
||||
/**
|
||||
@ -2783,6 +2785,7 @@ class core_message_externallib_testcase extends externallib_advanced_testcase {
|
||||
|
||||
// Retrieve the messages.
|
||||
$result = core_message_external::data_for_messagearea_messages($user1->id, $user2->id);
|
||||
$this->assertDebuggingCalledCount(3);
|
||||
|
||||
// We need to execute the return values cleaning process to simulate the web service server.
|
||||
$result = external_api::clean_returnvalue(core_message_external::data_for_messagearea_messages_returns(),
|
||||
@ -2847,6 +2850,7 @@ class core_message_externallib_testcase extends externallib_advanced_testcase {
|
||||
|
||||
// Retrieve the messages from $time - 3, which should be the 3 most recent messages.
|
||||
$result = core_message_external::data_for_messagearea_messages($user1->id, $user2->id, 0, 0, false, $time - 3);
|
||||
$this->assertDebuggingCalledCount(3);
|
||||
|
||||
// We need to execute the return values cleaning process to simulate the web service server.
|
||||
$result = external_api::clean_returnvalue(core_message_external::data_for_messagearea_messages_returns(),
|
||||
@ -2887,6 +2891,7 @@ class core_message_externallib_testcase extends externallib_advanced_testcase {
|
||||
|
||||
// Retrieve the messages.
|
||||
$result = core_message_external::data_for_messagearea_messages($user1->id, $user2->id);
|
||||
$this->assertDebuggingCalledCount(3);
|
||||
|
||||
// We need to execute the return values cleaning process to simulate the web service server.
|
||||
$result = external_api::clean_returnvalue(core_message_external::data_for_messagearea_messages_returns(),
|
||||
@ -2971,6 +2976,263 @@ class core_message_externallib_testcase extends externallib_advanced_testcase {
|
||||
core_message_external::data_for_messagearea_messages($user1->id, $user2->id);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests get_conversation_messages for retrieving messages.
|
||||
*/
|
||||
public function test_get_conversation_messages() {
|
||||
$this->resetAfterTest(true);
|
||||
|
||||
// Create some users.
|
||||
$user1 = self::getDataGenerator()->create_user();
|
||||
$user2 = self::getDataGenerator()->create_user();
|
||||
$user3 = self::getDataGenerator()->create_user();
|
||||
$user4 = self::getDataGenerator()->create_user();
|
||||
$user5 = self::getDataGenerator()->create_user();
|
||||
|
||||
// Create group conversation.
|
||||
$conversation = \core_message\api::create_conversation(
|
||||
\core_message\api::MESSAGE_CONVERSATION_TYPE_GROUP,
|
||||
[$user1->id, $user2->id, $user3->id, $user4->id]
|
||||
);
|
||||
|
||||
// The person asking for the messages.
|
||||
$this->setUser($user1);
|
||||
|
||||
// Send some messages back and forth.
|
||||
$time = time();
|
||||
testhelper::send_fake_message_to_conversation($user1, $conversation->id, 'Yo!', $time);
|
||||
testhelper::send_fake_message_to_conversation($user3, $conversation->id, 'Sup mang?', $time + 1);
|
||||
testhelper::send_fake_message_to_conversation($user2, $conversation->id, 'Writing PHPUnit tests!', $time + 2);
|
||||
testhelper::send_fake_message_to_conversation($user1, $conversation->id, 'Word.', $time + 3);
|
||||
|
||||
// Retrieve the messages.
|
||||
$result = core_message_external::get_conversation_messages($user1->id, $conversation->id);
|
||||
|
||||
// We need to execute the return values cleaning process to simulate the web service server.
|
||||
$result = external_api::clean_returnvalue(core_message_external::get_conversation_messages_returns(),
|
||||
$result);
|
||||
|
||||
// Check the results are correct.
|
||||
$this->assertEquals($conversation->id, $result['id']);
|
||||
|
||||
// Confirm the members data is correct.
|
||||
$members = $result['members'];
|
||||
$this->assertCount(3, $members);
|
||||
$membersid = [$members[0]['id'], $members[1]['id'], $members[2]['id']];
|
||||
$this->assertContains($user1->id, $membersid);
|
||||
$this->assertContains($user2->id, $membersid);
|
||||
$this->assertContains($user3->id, $membersid);
|
||||
$this->assertNotContains($user4->id, $membersid);
|
||||
$this->assertNotContains($user5->id, $membersid);
|
||||
$membersfullnames = [$members[0]['fullname'], $members[1]['fullname'], $members[2]['fullname']];
|
||||
$this->assertContains(fullname($user1), $membersfullnames);
|
||||
$this->assertContains(fullname($user2), $membersfullnames);
|
||||
$this->assertContains(fullname($user3), $membersfullnames);
|
||||
$this->assertNotContains(fullname($user4), $membersfullnames);
|
||||
$this->assertNotContains(fullname($user5), $membersfullnames);
|
||||
|
||||
// Confirm the messages data is correct.
|
||||
$messages = $result['messages'];
|
||||
$this->assertCount(4, $messages);
|
||||
|
||||
$message1 = $messages[0];
|
||||
$message2 = $messages[1];
|
||||
$message3 = $messages[2];
|
||||
$message4 = $messages[3];
|
||||
|
||||
$this->assertEquals($user1->id, $message1['useridfrom']);
|
||||
$this->assertContains('Yo!', $message1['text']);
|
||||
|
||||
$this->assertEquals($user3->id, $message2['useridfrom']);
|
||||
$this->assertContains('Sup mang?', $message2['text']);
|
||||
|
||||
$this->assertEquals($user2->id, $message3['useridfrom']);
|
||||
$this->assertContains('Writing PHPUnit tests!', $message3['text']);
|
||||
|
||||
$this->assertEquals($user1->id, $message4['useridfrom']);
|
||||
$this->assertContains('Word.', $message4['text']);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests get_conversation_messages for retrieving messages using timefrom parameter.
|
||||
*/
|
||||
public function test_get_conversation_messages_timefrom() {
|
||||
$this->resetAfterTest(true);
|
||||
|
||||
// Create some users.
|
||||
$user1 = self::getDataGenerator()->create_user();
|
||||
$user2 = self::getDataGenerator()->create_user();
|
||||
$user3 = self::getDataGenerator()->create_user();
|
||||
$user4 = self::getDataGenerator()->create_user();
|
||||
|
||||
// Create group conversation.
|
||||
$conversation = \core_message\api::create_conversation(
|
||||
\core_message\api::MESSAGE_CONVERSATION_TYPE_GROUP,
|
||||
[$user1->id, $user2->id, $user3->id]
|
||||
);
|
||||
|
||||
// The person asking for the messages.
|
||||
$this->setUser($user1);
|
||||
|
||||
// Send some messages back and forth.
|
||||
$time = time();
|
||||
testhelper::send_fake_message_to_conversation($user1, $conversation->id, 'Message 1', $time - 4);
|
||||
testhelper::send_fake_message_to_conversation($user2, $conversation->id, 'Message 2', $time - 3);
|
||||
testhelper::send_fake_message_to_conversation($user2, $conversation->id, 'Message 3', $time - 2);
|
||||
testhelper::send_fake_message_to_conversation($user2, $conversation->id, 'Message 4', $time - 1);
|
||||
|
||||
// Retrieve the messages from $time - 3, which should be the 3 most recent messages.
|
||||
$result = core_message_external::get_conversation_messages($user1->id, $conversation->id, 0, 0, false, $time - 3);
|
||||
|
||||
// We need to execute the return values cleaning process to simulate the web service server.
|
||||
$result = external_api::clean_returnvalue(core_message_external::get_conversation_messages_returns(),
|
||||
$result);
|
||||
|
||||
// Check the results are correct.
|
||||
$this->assertEquals($conversation->id, $result['id']);
|
||||
|
||||
// Confirm the messages data is correct.
|
||||
$messages = $result['messages'];
|
||||
$this->assertCount(3, $messages);
|
||||
|
||||
$message1 = $messages[0];
|
||||
$message2 = $messages[1];
|
||||
$message3 = $messages[2];
|
||||
|
||||
$this->assertContains('Message 2', $message1['text']);
|
||||
$this->assertContains('Message 3', $message2['text']);
|
||||
$this->assertContains('Message 4', $message3['text']);
|
||||
|
||||
// Confirm the members data is correct.
|
||||
$members = $result['members'];
|
||||
$this->assertCount(1, $members);
|
||||
$this->assertEquals($user2->id, $members[0]['id']);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests get_conversation_messages for retrieving messages as another user.
|
||||
*/
|
||||
public function test_get_conversation_messages_as_other_user() {
|
||||
$this->resetAfterTest(true);
|
||||
|
||||
// Set as admin.
|
||||
$this->setAdminUser();
|
||||
|
||||
// Create some users.
|
||||
$user1 = self::getDataGenerator()->create_user();
|
||||
$user2 = self::getDataGenerator()->create_user();
|
||||
$user3 = self::getDataGenerator()->create_user();
|
||||
$user4 = self::getDataGenerator()->create_user();
|
||||
|
||||
// Create group conversation.
|
||||
$conversation = \core_message\api::create_conversation(
|
||||
\core_message\api::MESSAGE_CONVERSATION_TYPE_GROUP,
|
||||
[$user1->id, $user2->id, $user3->id, $user4->id]
|
||||
);
|
||||
|
||||
// Send some messages back and forth.
|
||||
$time = time();
|
||||
testhelper::send_fake_message_to_conversation($user1, $conversation->id, 'Yo!', $time);
|
||||
testhelper::send_fake_message_to_conversation($user3, $conversation->id, 'Sup mang?', $time + 1);
|
||||
testhelper::send_fake_message_to_conversation($user2, $conversation->id, 'Writing PHPUnit tests!', $time + 2);
|
||||
testhelper::send_fake_message_to_conversation($user1, $conversation->id, 'Word.', $time + 3);
|
||||
|
||||
// Retrieve the messages.
|
||||
$result = core_message_external::get_conversation_messages($user1->id, $conversation->id);
|
||||
|
||||
// We need to execute the return values cleaning process to simulate the web service server.
|
||||
$result = external_api::clean_returnvalue(core_message_external::get_conversation_messages_returns(),
|
||||
$result);
|
||||
|
||||
// Check the results are correct.
|
||||
$this->assertEquals($conversation->id, $result['id']);
|
||||
|
||||
// Confirm the members data is correct.
|
||||
$members = $result['members'];
|
||||
$this->assertCount(3, $members);
|
||||
$membersid = [$members[0]['id'], $members[1]['id'], $members[2]['id']];
|
||||
$this->assertContains($user1->id, $membersid);
|
||||
$this->assertContains($user2->id, $membersid);
|
||||
$this->assertContains($user3->id, $membersid);
|
||||
$this->assertNotContains($user4->id, $membersid);
|
||||
|
||||
// Confirm the message data is correct.
|
||||
$messages = $result['messages'];
|
||||
$this->assertCount(4, $messages);
|
||||
|
||||
$message1 = $messages[0];
|
||||
$message2 = $messages[1];
|
||||
$message3 = $messages[2];
|
||||
$message4 = $messages[3];
|
||||
|
||||
$this->assertEquals($user1->id, $message1['useridfrom']);
|
||||
$this->assertContains('Yo!', $message1['text']);
|
||||
|
||||
$this->assertEquals($user3->id, $message2['useridfrom']);
|
||||
$this->assertContains('Sup mang?', $message2['text']);
|
||||
|
||||
$this->assertEquals($user2->id, $message3['useridfrom']);
|
||||
$this->assertContains('Writing PHPUnit tests!', $message3['text']);
|
||||
|
||||
$this->assertEquals($user1->id, $message4['useridfrom']);
|
||||
$this->assertContains('Word.', $message4['text']);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests get_conversation_messages for retrieving messages as another user without the proper capabilities.
|
||||
*/
|
||||
public function test_get_conversation_messages_as_other_user_without_cap() {
|
||||
$this->resetAfterTest(true);
|
||||
|
||||
// Create some users.
|
||||
$user1 = self::getDataGenerator()->create_user();
|
||||
$user2 = self::getDataGenerator()->create_user();
|
||||
$user3 = self::getDataGenerator()->create_user();
|
||||
$user4 = self::getDataGenerator()->create_user();
|
||||
|
||||
// Create group conversation.
|
||||
$conversation = \core_message\api::create_conversation(
|
||||
\core_message\api::MESSAGE_CONVERSATION_TYPE_GROUP,
|
||||
[$user1->id, $user2->id, $user3->id, $user4->id]
|
||||
);
|
||||
|
||||
// The person asking for the messages for another user.
|
||||
$this->setUser($user1);
|
||||
|
||||
// Ensure an exception is thrown.
|
||||
$this->expectException('moodle_exception');
|
||||
core_message_external::get_conversation_messages($user2->id, $conversation->id);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests get_conversation_messages for retrieving messages with messaging disabled.
|
||||
*/
|
||||
public function test_get_conversation_messages_messaging_disabled() {
|
||||
$this->resetAfterTest(true);
|
||||
|
||||
// Create some skeleton data just so we can call the WS.
|
||||
$user1 = self::getDataGenerator()->create_user();
|
||||
$user2 = self::getDataGenerator()->create_user();
|
||||
$user3 = self::getDataGenerator()->create_user();
|
||||
$user4 = self::getDataGenerator()->create_user();
|
||||
|
||||
// Create group conversation.
|
||||
$conversation = \core_message\api::create_conversation(
|
||||
\core_message\api::MESSAGE_CONVERSATION_TYPE_GROUP,
|
||||
[$user1->id, $user2->id, $user3->id, $user4->id]
|
||||
);
|
||||
|
||||
// The person asking for the messages for another user.
|
||||
$this->setUser($user1);
|
||||
|
||||
// Disable messaging.
|
||||
set_config('messaging', 0);
|
||||
|
||||
// Ensure an exception is thrown.
|
||||
$this->expectException('moodle_exception');
|
||||
core_message_external::get_conversation_messages($user1->id, $conversation->id);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests retrieving most recent message.
|
||||
*/
|
||||
@ -2993,6 +3255,7 @@ class core_message_externallib_testcase extends externallib_advanced_testcase {
|
||||
|
||||
// Get the most recent message.
|
||||
$result = core_message_external::data_for_messagearea_get_most_recent_message($user1->id, $user2->id);
|
||||
$this->assertDebuggingCalledCount(3);
|
||||
|
||||
// We need to execute the return values cleaning process to simulate the web service server.
|
||||
$result = external_api::clean_returnvalue(core_message_external::data_for_messagearea_get_most_recent_message_returns(),
|
||||
@ -3026,6 +3289,7 @@ class core_message_externallib_testcase extends externallib_advanced_testcase {
|
||||
|
||||
// Get the most recent message.
|
||||
$result = core_message_external::data_for_messagearea_get_most_recent_message($user1->id, $user2->id);
|
||||
$this->assertDebuggingCalledCount(3);
|
||||
|
||||
// We need to execute the return values cleaning process to simulate the web service server.
|
||||
$result = external_api::clean_returnvalue(core_message_external::data_for_messagearea_get_most_recent_message_returns(),
|
||||
|
@ -31,6 +31,10 @@ information provided here is intended especially for developers.
|
||||
- \core_message\api::delete_conversation()
|
||||
- \core_message\api::is_user_non_contact_blocked()
|
||||
- \core_message\api::create_conversation_between_users()
|
||||
- \core_message\api::get_messages()
|
||||
- \core_message\api::get_most_recent_message()
|
||||
- \core_message\helper::get_messages()
|
||||
- \core_message\helper::create_messages()
|
||||
* The method \core_message\api::can_delete_conversation() now expects a 'conversationid' to be passed
|
||||
as the second parameter.
|
||||
* The following web services have been deprecated. Please do not call these any more.
|
||||
|
Loading…
x
Reference in New Issue
Block a user