From 79f6c36c12c8355c4190fb7a0e5abc9be3a17e88 Mon Sep 17 00:00:00 2001 From: Mark Nelson Date: Thu, 22 Sep 2016 12:30:28 +0800 Subject: [PATCH] MDL-55942 core_message: moved added functionality from message/lib.php --- lib/outputrenderers.php | 4 +- message/classes/api.php | 209 ++++++++++++++++++++++++ message/classes/helper.php | 65 ++++++++ message/externallib.php | 12 +- message/index.php | 4 +- message/lib.php | 261 +---------------------------- message/renderer.php | 4 +- message/tests/api_test.php | 334 ++++++++++++++++++++++++++++++++++++++ message/tests/messagelib_test.php | 292 --------------------------------- 9 files changed, 629 insertions(+), 556 deletions(-) create mode 100644 message/tests/api_test.php diff --git a/lib/outputrenderers.php b/lib/outputrenderers.php index 1b055a678dc..8363a562180 100644 --- a/lib/outputrenderers.php +++ b/lib/outputrenderers.php @@ -4176,7 +4176,7 @@ EOD; 'sesskey' => sesskey()) ), 'image' => $contactimage, - 'linkattributes' => message_togglecontact_link_params($user, $iscontact), + 'linkattributes' => \core_message\helper::togglecontact_link_params($user, $iscontact), 'page' => $this->page ), ); @@ -4238,7 +4238,7 @@ EOD; if (!isset($button->page)) { // Include js for messaging. if ($button['buttontype'] === 'togglecontact') { - message_togglecontact_requirejs(); + \core_message\helper::togglecontact_requirejs(); } $image = $this->pix_icon($button['formattedimage'], $button['title'], 'moodle', array( 'class' => 'iconsmall', diff --git a/message/classes/api.php b/message/classes/api.php index b3aa19d33fa..b863d26dedf 100644 --- a/message/classes/api.php +++ b/message/classes/api.php @@ -462,4 +462,213 @@ class api { return true; } + + /** + * Returns the count of unread conversations (collection of messages from a single user) for + * the given user. + * + * @param \stdClass $user the user who's conversations should be counted + * @return int the count of the user's unread conversations + */ + public static function count_unread_conversations($user = null) { + global $USER, $DB; + + if (empty($user)) { + $user = $USER; + } + + return $DB->count_records_select( + 'message', + 'useridto = ? AND timeusertodeleted = 0 AND notification = 0', + [$user->id], + "COUNT(DISTINCT(useridfrom))"); + } + + /** + * Marks ALL messages being sent from $fromuserid to $touserid as read. + * + * Can be filtered by type. + * + * @param int $touserid the id of the message recipient + * @param int $fromuserid the id of the message sender + * @param string $type filter the messages by type, either MESSAGE_TYPE_NOTIFICATION, MESSAGE_TYPE_MESSAGE or '' for all. + * @return void + */ + public static function mark_all_read_for_user($touserid, $fromuserid = 0, $type = '') { + global $DB; + + $params = array(); + + if (!empty($touserid)) { + $params['useridto'] = $touserid; + } + + if (!empty($fromuserid)) { + $params['useridfrom'] = $fromuserid; + } + + if (!empty($type)) { + if (strtolower($type) == MESSAGE_TYPE_NOTIFICATION) { + $params['notification'] = 1; + } else if (strtolower($type) == MESSAGE_TYPE_MESSAGE) { + $params['notification'] = 0; + } + } + + $sql = sprintf('SELECT m.* FROM {message} m WHERE m.%s = ?', implode('= ? AND m.', array_keys($params))); + $messages = $DB->get_recordset_sql($sql, array_values($params)); + + foreach ($messages as $message) { + message_mark_message_read($message, time()); + } + + $messages->close(); + } + + /** + * Get popup notifications for the specified users. + * + * @param int $useridto the user id who received the notification + * @param string $status MESSAGE_READ for retrieving read notifications, MESSAGE_UNREAD for unread, empty for both + * @param bool $embeduserto embed the to user details in the notification response + * @param bool $embeduserfrom embed the from user details in the notification response + * @param string $sort the column name to order by including optionally direction + * @param int $limit limit the number of result returned + * @param int $offset offset the result set by this amount + * @return array notification records + * @throws \moodle_exception + * @since 3.2 + */ + public static function get_popup_notifications($useridto = 0, $status = '', $embeduserto = false, $embeduserfrom = false, + $sort = 'DESC', $limit = 0, $offset = 0) { + global $DB, $USER; + + if (!empty($status) && $status != MESSAGE_READ && $status != MESSAGE_UNREAD) { + throw new \moodle_exception(sprintf('invalid parameter: status: must be "%s" or "%s"', + MESSAGE_READ, MESSAGE_UNREAD)); + } + + $sort = strtoupper($sort); + if ($sort != 'DESC' && $sort != 'ASC') { + throw new \moodle_exception('invalid parameter: sort: must be "DESC" or "ASC"'); + } + + if (empty($useridto)) { + $useridto = $USER->id; + } + + $params = array(); + + $buildtablesql = function($table, $prefix, $additionalfields, $messagestatus) + use ($status, $useridto, $embeduserto, $embeduserfrom) { + + $joinsql = ''; + $fields = "concat('$prefix', $prefix.id) as uniqueid, $prefix.id, $prefix.useridfrom, $prefix.useridto, + $prefix.subject, $prefix.fullmessage, $prefix.fullmessageformat, + $prefix.fullmessagehtml, $prefix.smallmessage, $prefix.notification, $prefix.contexturl, + $prefix.contexturlname, $prefix.timecreated, $prefix.timeuserfromdeleted, $prefix.timeusertodeleted, + $prefix.component, $prefix.eventtype, $additionalfields"; + $where = " AND $prefix.useridto = :{$prefix}useridto"; + $params = ["{$prefix}useridto" => $useridto]; + + if ($embeduserto) { + $embedprefix = "{$prefix}ut"; + $fields .= ", " . get_all_user_name_fields(true, $embedprefix, '', 'userto'); + $joinsql .= " LEFT JOIN {user} $embedprefix ON $embedprefix.id = $prefix.useridto"; + } + + if ($embeduserfrom) { + $embedprefix = "{$prefix}uf"; + $fields .= ", " . get_all_user_name_fields(true, $embedprefix, '', 'userfrom'); + $joinsql .= " LEFT JOIN {user} $embedprefix ON $embedprefix.id = $prefix.useridfrom"; + } + + if ($messagestatus == MESSAGE_READ) { + $isread = '1'; + } else { + $isread = '0'; + } + + return array( + sprintf( + "SELECT %s + FROM %s %s %s + WHERE %s.notification = 1 + AND %s.id IN (SELECT messageid FROM {message_popup} WHERE isread = %s) + %s", + $fields, $table, $prefix, $joinsql, $prefix, $prefix, $isread, $where + ), + $params + ); + }; + + switch ($status) { + case MESSAGE_READ: + list($sql, $readparams) = $buildtablesql('{message_read}', 'r', 'r.timeread', MESSAGE_READ); + $params = array_merge($params, $readparams); + break; + case MESSAGE_UNREAD: + list($sql, $unreadparams) = $buildtablesql('{message}', 'u', '0 as timeread', MESSAGE_UNREAD); + $params = array_merge($params, $unreadparams); + break; + default: + list($readsql, $readparams) = $buildtablesql('{message_read}', 'r', 'r.timeread', MESSAGE_READ); + list($unreadsql, $unreadparams) = $buildtablesql('{message}', 'u', '0 as timeread', MESSAGE_UNREAD); + $sql = sprintf("SELECT * FROM (%s UNION %s) f", $readsql, $unreadsql); + $params = array_merge($params, $readparams, $unreadparams); + } + + $sql .= " ORDER BY timecreated $sort, timeread $sort, id $sort"; + + return array_values($DB->get_records_sql($sql, $params, $offset, $limit)); + } + + /** + * Count the unread notifications for a user. + * + * @param int $useridto the user id who received the notification + * @return int count of the unread notifications + * @since 3.2 + */ + public static function count_unread_popup_notifications($useridto = 0) { + global $USER, $DB; + + if (empty($useridto)) { + $useridto = $USER->id; + } + + return $DB->count_records_sql( + "SELECT count(id) + FROM {message} + WHERE id IN (SELECT messageid FROM {message_popup} WHERE isread = 0) + AND useridto = ?", + [$useridto] + ); + } + + /** + * Returns message preferences. + * + * @param array $processors + * @param array $providers + * @param \stdClass $user + * @return \stdClass + * @since 3.2 + */ + public static function get_all_message_preferences($processors, $providers, $user) { + $preferences = helper::get_providers_preferences($providers, $user->id); + $preferences->userdefaultemail = $user->email; // May be displayed by the email processor. + + // For every processors put its options on the form (need to get function from processor's lib.php). + foreach ($processors as $processor) { + $processor->object->load_data($preferences, $user->id); + } + + // Load general messaging preferences. + $preferences->blocknoncontacts = get_user_preferences('message_blocknoncontacts', '', $user->id); + $preferences->mailformat = $user->mailformat; + $preferences->mailcharset = get_user_preferences('mailcharset', '', $user->id); + + return $preferences; + } } diff --git a/message/classes/helper.php b/message/classes/helper.php index df3def159f9..62ee8a031de 100644 --- a/message/classes/helper.php +++ b/message/classes/helper.php @@ -179,4 +179,69 @@ class helper { return $lastaccess >= $time; } + + /** + * Get providers preferences. + * + * @param array $providers + * @param int $userid + * @return \stdClass + */ + public static function get_providers_preferences($providers, $userid) { + $preferences = new \stdClass(); + + // Get providers preferences. + foreach ($providers as $provider) { + foreach (array('loggedin', 'loggedoff') as $state) { + $linepref = get_user_preferences('message_provider_' . $provider->component . '_' . $provider->name + . '_' . $state, '', $userid); + if ($linepref == '') { + continue; + } + $lineprefarray = explode(',', $linepref); + $preferences->{$provider->component.'_'.$provider->name.'_'.$state} = array(); + foreach ($lineprefarray as $pref) { + $preferences->{$provider->component.'_'.$provider->name.'_'.$state}[$pref] = 1; + } + } + } + + return $preferences; + } + + /** + * Requires the JS libraries for the toggle contact button. + * + * @return void + */ + public static function togglecontact_requirejs() { + global $PAGE; + + static $done = false; + if ($done) { + return; + } + + $PAGE->requires->js_call_amd('core_message/toggle_contact_button', 'enhance', array('#toggle-contact-button')); + $done = true; + } + + /** + * Returns the attributes to place on a contact button. + * + * @param object $user User object. + * @param bool $iscontact + * @return array + */ + public static function togglecontact_link_params($user, $iscontact = false) { + $params = array( + 'data-userid' => $user->id, + 'data-is-contact' => $iscontact, + 'id' => 'toggle-contact-button', + 'role' => 'button', + 'class' => 'ajax-contact-button', + ); + + return $params; + } } diff --git a/message/externallib.php b/message/externallib.php index 0fb84a87528..28472423bfe 100644 --- a/message/externallib.php +++ b/message/externallib.php @@ -1581,7 +1581,7 @@ class core_message_external extends external_api { } $sort = $newestfirst ? 'DESC' : 'ASC'; - $notifications = message_get_popup_notifications($useridto, $status, $embeduserto, $embeduserfrom, $sort, $limit, $offset); + $notifications = \core_message\api::get_popup_notifications($useridto, $status, $embeduserto, $embeduserfrom, $sort, $limit, $offset); $notificationcontexts = []; if ($notifications) { @@ -1609,7 +1609,7 @@ class core_message_external extends external_api { return array( 'notifications' => $notificationcontexts, - 'unreadcount' => message_count_unread_popup_notifications($useridto), + 'unreadcount' => \core_message\api::count_unread_popup_notifications($useridto), ); } @@ -1720,7 +1720,7 @@ class core_message_external extends external_api { throw new moodle_exception('accessdenied', 'admin'); } - message_mark_all_read_for_user($useridto, $useridfrom, MESSAGE_TYPE_NOTIFICATION); + \core_message\api::mark_all_read_for_user($useridto, $useridfrom, MESSAGE_TYPE_NOTIFICATION); return true; } @@ -1784,7 +1784,7 @@ class core_message_external extends external_api { throw new moodle_exception('accessdenied', 'admin'); } - return message_count_unread_popup_notifications($useridto); + return \core_message\api::count_unread_popup_notifications($useridto); } /** @@ -1848,7 +1848,7 @@ class core_message_external extends external_api { throw new moodle_exception('accessdenied', 'admin'); } - return message_count_unread_conversations($userto); + return \core_message\api::count_unread_conversations($userto); } /** @@ -2109,7 +2109,7 @@ class core_message_external extends external_api { throw new moodle_exception('accessdenied', 'admin'); } - message_mark_all_read_for_user($useridto, $useridfrom, MESSAGE_TYPE_MESSAGE); + \core_message\api::mark_all_read_for_user($useridto, $useridfrom, MESSAGE_TYPE_MESSAGE); return true; } diff --git a/message/index.php b/message/index.php index 0c8b93246ed..93760d73ef6 100644 --- a/message/index.php +++ b/message/index.php @@ -257,7 +257,7 @@ if (!$user2realuser) { // Mark the conversation as read. if ($currentuser) { $contact->isread = 1; - message_mark_all_read_for_user($user1->id, $otheruserid); + \core_message\api::mark_all_read_for_user($user1->id, $otheruserid); } $messages = \core_message\api::get_messages($user1->id, $otheruserid, 0, 20, 'timecreated DESC'); @@ -267,7 +267,7 @@ if (!$user2realuser) { } else { // Mark the conversation as read. if ($currentuser) { - message_mark_all_read_for_user($user1->id, $user2->id); + \core_message\api::mark_all_read_for_user($user1->id, $user2->id); } $conversations = \core_message\api::get_conversations($user1->id, $user2->id, 0, 20); diff --git a/message/lib.php b/message/lib.php index 87834e6c491..a53ee0579fb 100644 --- a/message/lib.php +++ b/message/lib.php @@ -290,27 +290,6 @@ function message_count_unread_messages($user1=null, $user2=null) { } /** - * Returns the count of unread conversations (collection of messages from a single user) for - * the given user. - * - * @param object $user the user who's conversations should be counted - * @return in the count of $user's unread conversations - */ -function message_count_unread_conversations($user = null) { - global $USER, $DB; - - if (empty($user)) { - $user = $USER; - } - - return $DB->count_records_select( - 'message', - 'useridto = ? AND timeusertodeleted = 0 AND notification = 0', - [$user->id], - "COUNT(DISTINCT(useridfrom))"); -} - -/** * Count the number of users blocked by $user1 * * @param object $user1 user object @@ -1519,49 +1498,9 @@ function message_move_userfrom_unread2read($userid) { * @return void */ function message_mark_messages_read($touserid, $fromuserid) { - return message_mark_all_read_for_user($touserid, $fromuserid); + return \core_message\api::mark_all_read_for_user($touserid, $fromuserid); } -/** - * marks ALL messages being sent from $fromuserid to $touserid as read. Can - * be filtered by type. - * - * @param int $touserid the id of the message recipient - * @param int $fromuserid the id of the message sender - * @param string $type filter the messages by type, either MESSAGE_TYPE_NOTIFICATION, MESSAGE_TYPE_MESSAGE or '' for all. - * @return void - */ -function message_mark_all_read_for_user($touserid, $fromuserid = 0, $type = '') { - global $DB; - - $params = array(); - $where = ''; - - if (!empty($touserid)) { - $params['useridto'] = $touserid; - } - - if (!empty($fromuserid)) { - $params['useridfrom'] = $fromuserid; - } - - if (!empty($type)) { - if (strtolower($type) == MESSAGE_TYPE_NOTIFICATION) { - $params['notification'] = 1; - } else if (strtolower($type) == MESSAGE_TYPE_MESSAGE) { - $params['notification'] = 0; - } - } - - $sql = sprintf('SELECT m.* FROM {message} m WHERE m.%s = ?', implode('= ? AND m.', array_keys($params))); - $messages = $DB->get_recordset_sql($sql, array_values($params)); - - foreach ($messages as $message) { - message_mark_message_read($message, time()); - } - - $messages->close(); -} /** * Mark a single message as read @@ -1854,162 +1793,11 @@ function message_get_messages($useridto, $useridfrom = 0, $notifications = -1, $ return $messages; } -/** - * Get popup notifications for the specified users. - * - * @param int $useridto the user id who received the notification - * @param bool $status MESSAGE_READ for retrieving read notifications, MESSAGE_UNREAD for unread, empty for both - * @param bool $embeduserto embed the to user details in the notification response - * @param bool $embeduserfrom embed the from user details in the notification response - * @param string $sort the column name to order by including optionally direction - * @param int $limit limit the number of result returned - * @param int $offset offset the result set by this amount - * @return array array of notification records - * @since 3.2 - */ -function message_get_popup_notifications($useridto = 0, $status = '', - $embeduserto = false, $embeduserfrom = false, $sort = 'DESC', $limit = 0, $offset = 0) { - global $DB; - - if (!empty($status) && $status != MESSAGE_READ && $status != MESSAGE_UNREAD) { - throw new moodle_exception(sprintf('invalid parameter: status: must be "%s" or "%s"', - MESSAGE_READ, MESSAGE_UNREAD)); - } - - $sort = strtoupper($sort); - if ($sort != 'DESC' && $sort != 'ASC') { - throw new moodle_exception('invalid parameter: sort: must be "DESC" or "ASC"'); - } - - if (empty($useridto)) { - $useridto = $USER->id; - } - $params = array(); - $buildtablesql = function($table, $prefix, $additionalfields, $messagestatus) - use ($status, $useridto, $embeduserto, $embeduserfrom) { - - $joinsql = ''; - $fields = "concat('$prefix', $prefix.id) as uniqueid, $prefix.id, $prefix.useridfrom, $prefix.useridto, - $prefix.subject, $prefix.fullmessage, $prefix.fullmessageformat, - $prefix.fullmessagehtml, $prefix.smallmessage, $prefix.notification, $prefix.contexturl, - $prefix.contexturlname, $prefix.timecreated, $prefix.timeuserfromdeleted, $prefix.timeusertodeleted, - $prefix.component, $prefix.eventtype, $additionalfields"; - $where = " AND $prefix.useridto = :{$prefix}useridto"; - $params = ["{$prefix}useridto" => $useridto]; - - if ($embeduserto) { - $embedprefix = "{$prefix}ut"; - $fields .= ", " . get_all_user_name_fields(true, $embedprefix, '', 'userto'); - $joinsql .= " LEFT JOIN {user} $embedprefix ON $embedprefix.id = $prefix.useridto"; - } - if ($embeduserfrom) { - $embedprefix = "{$prefix}uf"; - $fields .= ", " . get_all_user_name_fields(true, $embedprefix, '', 'userfrom'); - $joinsql .= " LEFT JOIN {user} $embedprefix ON $embedprefix.id = $prefix.useridfrom"; - } - if ($messagestatus == MESSAGE_READ) { - $isread = '1'; - } else { - $isread = '0'; - } - - return array( - sprintf( - "SELECT %s - FROM %s %s %s - WHERE %s.notification = 1 - AND %s.id IN (SELECT messageid FROM {message_popup} WHERE isread = %s) - %s", - $fields, $table, $prefix, $joinsql, $prefix, $prefix, $isread, $where - ), - $params - ); - }; - $sql = ''; - switch ($status) { - case MESSAGE_READ: - list($sql, $readparams) = $buildtablesql('{message_read}', 'r', 'r.timeread', MESSAGE_READ); - $params = array_merge($params, $readparams); - break; - case MESSAGE_UNREAD: - list($sql, $unreadparams) = $buildtablesql('{message}', 'u', '0 as timeread', MESSAGE_UNREAD); - $params = array_merge($params, $unreadparams); - break; - default: - list($readsql, $readparams) = $buildtablesql('{message_read}', 'r', 'r.timeread', MESSAGE_READ); - list($unreadsql, $unreadparams) = $buildtablesql('{message}', 'u', '0 as timeread', MESSAGE_UNREAD); - $sql = sprintf("SELECT * FROM (%s UNION %s) f", $readsql, $unreadsql); - $params = array_merge($params, $readparams, $unreadparams); - } - - $sql .= " ORDER BY timecreated $sort, timeread $sort, id $sort"; - - return array_values($DB->get_records_sql($sql, $params, $offset, $limit)); -} - -/** - * Count the unread notifications for a user. - * - * @param int $useridto the user id who received the notification - * @return int count of the unread notifications - * @since 3.2 - */ -function message_count_unread_popup_notifications($useridto = 0) { - global $USER, $DB; - - if (empty($useridto)) { - $useridto = $USER->id; - } - - return $DB->count_records_sql( - "SELECT count(id) - FROM {message} - WHERE id IN (SELECT messageid FROM {message_popup} WHERE isread = 0) - AND useridto = ?", - [$useridto] - ); -} - -/** - * Requires the JS libraries for the toggle contact button. - * - * @return void - */ -function message_togglecontact_requirejs() { - global $PAGE; - - static $done = false; - if ($done) { - return; - } - - $PAGE->requires->js_call_amd('core_message/toggle_contact_button', 'enhance', array('#toggle-contact-button')); - $done = true; -} - -/** - * Returns the attributes to place on a contact button. - * - * @param object $user User object. - * @param bool $iscontact - * @return void - */ -function message_togglecontact_link_params($user, $iscontact = false) { - $params = array( - 'data-userid' => $user->id, - 'data-is-contact' => $iscontact, - 'id' => 'toggle-contact-button', - 'role' => 'button', - 'class' => 'ajax-contact-button', - ); - - return $params; -} /** * Determines if a user is permitted to send another user a private message. @@ -2110,44 +1898,13 @@ function message_is_user_blocked($recipient, $sender = null) { return false; } -function get_providers_preferences($providers, $userid) { - $preferences = new stdClass(); - - /// Get providers preferences - foreach ($providers as $provider) { - foreach (array('loggedin', 'loggedoff') as $state) { - $linepref = get_user_preferences('message_provider_'.$provider->component.'_'.$provider->name.'_'.$state, '', $userid); - if ($linepref == ''){ - continue; - } - $lineprefarray = explode(',', $linepref); - $preferences->{$provider->component.'_'.$provider->name.'_'.$state} = array(); - foreach ($lineprefarray as $pref) { - $preferences->{$provider->component.'_'.$provider->name.'_'.$state}[$pref] = 1; - } - } - } - - return $preferences; -} - -function get_all_message_preferences($processors, $providers, $user) { - $preferences = get_providers_preferences($providers, $user->id); - $preferences->userdefaultemail = $user->email;//may be displayed by the email processor - - /// For every processors put its options on the form (need to get function from processor's lib.php) - foreach ($processors as $processor) { - $processor->object->load_data($preferences, $user->id); - } - - //load general messaging preferences - $preferences->blocknoncontacts = get_user_preferences('message_blocknoncontacts', '', $user->id); - $preferences->mailformat = $user->mailformat; - $preferences->mailcharset = get_user_preferences('mailcharset', '', $user->id); - - return $preferences; -} - +/** + * Handles displaying processor settings in a fragment. + * + * @param array $args + * @return bool|string + * @throws moodle_exception + */ function message_output_fragment_processor_settings($args = []) { global $PAGE; @@ -2167,7 +1924,7 @@ function message_output_fragment_processor_settings($args = []) { $providers = message_get_providers_for_user($userid); $processorwrapper = new stdClass(); $processorwrapper->object = $processor; - $preferences = get_all_message_preferences([$processorwrapper], $providers, $user); + $preferences = \core_message\api::get_all_message_preferences([$processorwrapper], $providers, $user); $processoroutput = new \core_message\output\preferences\processor($processor, $preferences, $user, $type); $renderer = $PAGE->get_renderer('core', 'message'); diff --git a/message/renderer.php b/message/renderer.php index 0b964fc375f..844f8769a7b 100644 --- a/message/renderer.php +++ b/message/renderer.php @@ -224,7 +224,7 @@ class core_message_renderer extends plugin_renderer_base { public function render_user_notification_preferences($user) { $processors = get_message_processors(); $providers = message_get_providers_for_user($user->id); - $preferences = get_all_message_preferences($processors, $providers, $user); + $preferences = \core_message\api::get_all_message_preferences($processors, $providers, $user); $notificationlistoutput = new \core_message\output\preferences\notification_list($processors, $providers, $preferences, $user); return $this->render_from_template('message/preferences_notifications_list', $notificationlistoutput->export_for_template($this)); @@ -250,7 +250,7 @@ class core_message_renderer extends plugin_renderer_base { $providers = array_filter(message_get_providers_for_user($user->id), function($provider) { return $provider->component === 'moodle'; }); - $preferences = get_all_message_preferences($readyprocessors, $providers, $user); + $preferences = \core_message\api::get_all_message_preferences($readyprocessors, $providers, $user); $notificationlistoutput = new \core_message\output\preferences\message_notification_list($readyprocessors, $providers, $preferences, $user); $context = $notificationlistoutput->export_for_template($this); $context['blocknoncontacts'] = get_user_preferences('message_blocknoncontacts', '', $user->id) ? true : false; diff --git a/message/tests/api_test.php b/message/tests/api_test.php new file mode 100644 index 00000000000..32ab2b2bef8 --- /dev/null +++ b/message/tests/api_test.php @@ -0,0 +1,334 @@ +. + +/** + * Test message API. + * + * @package core_message + * @category test + * @copyright 2016 Mark Nelson + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +defined('MOODLE_INTERNAL') || die(); + +global $CFG; + +require_once($CFG->dirroot . '/message/tests/messagelib_test.php'); + +/** + * Test message API. + * + * @package core_message + * @category test + * @copyright 2016 Mark Nelson + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class core_message_api_testcase extends core_message_messagelib_testcase { + + public function test_message_mark_all_read_for_user_touser() { + $sender = $this->getDataGenerator()->create_user(array('firstname' => 'Test1', 'lastname' => 'User1')); + $recipient = $this->getDataGenerator()->create_user(array('firstname' => 'Test2', 'lastname' => 'User2')); + + $this->send_fake_unread_popup_notification($sender, $recipient); + $this->send_fake_unread_popup_notification($sender, $recipient); + $this->send_fake_unread_popup_notification($sender, $recipient); + $this->send_fake_message($sender, $recipient); + $this->send_fake_message($sender, $recipient); + $this->send_fake_message($sender, $recipient); + + \core_message\api::mark_all_read_for_user($recipient->id); + $this->assertEquals(message_count_unread_messages($recipient), 0); + } + + public function test_message_mark_all_read_for_user_touser_with_fromuser() { + $sender1 = $this->getDataGenerator()->create_user(array('firstname' => 'Test1', 'lastname' => 'User1')); + $sender2 = $this->getDataGenerator()->create_user(array('firstname' => 'Test3', 'lastname' => 'User3')); + $recipient = $this->getDataGenerator()->create_user(array('firstname' => 'Test2', 'lastname' => 'User2')); + + $this->send_fake_unread_popup_notification($sender1, $recipient); + $this->send_fake_unread_popup_notification($sender1, $recipient); + $this->send_fake_unread_popup_notification($sender1, $recipient); + $this->send_fake_message($sender1, $recipient); + $this->send_fake_message($sender1, $recipient); + $this->send_fake_message($sender1, $recipient); + $this->send_fake_unread_popup_notification($sender2, $recipient); + $this->send_fake_unread_popup_notification($sender2, $recipient); + $this->send_fake_unread_popup_notification($sender2, $recipient); + $this->send_fake_message($sender2, $recipient); + $this->send_fake_message($sender2, $recipient); + $this->send_fake_message($sender2, $recipient); + + \core_message\api::mark_all_read_for_user($recipient->id, $sender1->id); + $this->assertEquals(message_count_unread_messages($recipient), 6); + } + + public function test_message_mark_all_read_for_user_touser_with_type() { + $sender = $this->getDataGenerator()->create_user(array('firstname' => 'Test1', 'lastname' => 'User1')); + $recipient = $this->getDataGenerator()->create_user(array('firstname' => 'Test2', 'lastname' => 'User2')); + + $this->send_fake_unread_popup_notification($sender, $recipient); + $this->send_fake_unread_popup_notification($sender, $recipient); + $this->send_fake_unread_popup_notification($sender, $recipient); + $this->send_fake_message($sender, $recipient); + $this->send_fake_message($sender, $recipient); + $this->send_fake_message($sender, $recipient); + + \core_message\api::mark_all_read_for_user($recipient->id, 0, MESSAGE_TYPE_NOTIFICATION); + $this->assertEquals(message_count_unread_messages($recipient), 3); + + \core_message\api::mark_all_read_for_user($recipient->id, 0, MESSAGE_TYPE_MESSAGE); + $this->assertEquals(message_count_unread_messages($recipient), 0); + } + + /** + * Test that the get_popup_notifications function will return only read notifications if requested. + */ + public function test_message_get_popup_notifications_read_only() { + $sender = $this->getDataGenerator()->create_user(array('firstname' => 'Test1', 'lastname' => 'User1')); + $recipient = $this->getDataGenerator()->create_user(array('firstname' => 'Test2', 'lastname' => 'User2')); + + $this->send_fake_read_popup_notification($sender, $recipient, 'Message 1', 2); + $this->send_fake_read_popup_notification($sender, $recipient, 'Message 2', 4); + + $notifications = \core_message\api::get_popup_notifications($recipient->id, MESSAGE_READ); + + $this->assertEquals($notifications[0]->fullmessage, 'Message 2'); + $this->assertEquals($notifications[1]->fullmessage, 'Message 1'); + + // Check if we request read and unread but there are only read messages, it should + // still return those correctly. + $notifications = \core_message\api::get_popup_notifications($recipient->id, ''); + + $this->assertEquals($notifications[0]->fullmessage, 'Message 2'); + $this->assertEquals($notifications[1]->fullmessage, 'Message 1'); + } + + /** + * Test that the get_popup_notifications function will return only unread notifications if requested. + */ + public function test_message_get_popup_notifications_unread_only() { + $sender = $this->getDataGenerator()->create_user(array('firstname' => 'Test1', 'lastname' => 'User1')); + $recipient = $this->getDataGenerator()->create_user(array('firstname' => 'Test2', 'lastname' => 'User2')); + + $this->send_fake_unread_popup_notification($sender, $recipient, 'Message 1', 2); + $this->send_fake_unread_popup_notification($sender, $recipient, 'Message 2', 4); + + $notifications = \core_message\api::get_popup_notifications($recipient->id, MESSAGE_UNREAD); + + $this->assertEquals($notifications[0]->fullmessage, 'Message 2'); + $this->assertEquals($notifications[1]->fullmessage, 'Message 1'); + + // Check if we request read and unread but there are only read messages, it should + // still return those correctly. + $notifications = \core_message\api::get_popup_notifications($recipient->id, ''); + + $this->assertEquals($notifications[0]->fullmessage, 'Message 2'); + $this->assertEquals($notifications[1]->fullmessage, 'Message 1'); + } + + /** + * Test that the get_popup_notifications function will return the correct notifications when both + * read and unread notifications are included. + */ + public function test_message_get_popup_notifications_mixed() { + $sender = $this->getDataGenerator()->create_user(array('firstname' => 'Test1', 'lastname' => 'User1')); + $recipient = $this->getDataGenerator()->create_user(array('firstname' => 'Test2', 'lastname' => 'User2')); + + $this->send_fake_read_popup_notification($sender, $recipient, 'Message 1', 1); + $this->send_fake_unread_popup_notification($sender, $recipient, 'Message 2', 2); + $this->send_fake_read_popup_notification($sender, $recipient, 'Message 3', 3, 1); + $this->send_fake_read_popup_notification($sender, $recipient, 'Message 4', 3, 2); + $this->send_fake_unread_popup_notification($sender, $recipient, 'Message 5', 4); + + $notifications = \core_message\api::get_popup_notifications($recipient->id); + + $this->assertEquals($notifications[0]->fullmessage, 'Message 5'); + $this->assertEquals($notifications[1]->fullmessage, 'Message 4'); + $this->assertEquals($notifications[2]->fullmessage, 'Message 3'); + $this->assertEquals($notifications[3]->fullmessage, 'Message 2'); + $this->assertEquals($notifications[4]->fullmessage, 'Message 1'); + + $notifications = \core_message\api::get_popup_notifications($recipient->id, MESSAGE_READ); + + $this->assertEquals($notifications[0]->fullmessage, 'Message 4'); + $this->assertEquals($notifications[1]->fullmessage, 'Message 3'); + $this->assertEquals($notifications[2]->fullmessage, 'Message 1'); + + $notifications = \core_message\api::get_popup_notifications($recipient->id, MESSAGE_UNREAD); + + $this->assertEquals($notifications[0]->fullmessage, 'Message 5'); + $this->assertEquals($notifications[1]->fullmessage, 'Message 2'); + } + + /** + * Test that the get_popup_notifications function works correctly with limiting and offsetting + * the result set if requested. + */ + public function test_message_get_popup_notifications_all_with_limit_and_offset() { + $sender = $this->getDataGenerator()->create_user(array('firstname' => 'Test1', 'lastname' => 'User1')); + $recipient = $this->getDataGenerator()->create_user(array('firstname' => 'Test2', 'lastname' => 'User2')); + + $this->send_fake_read_popup_notification($sender, $recipient, 'Message 1', 1); + $this->send_fake_unread_popup_notification($sender, $recipient, 'Message 2', 2); + $this->send_fake_read_popup_notification($sender, $recipient, 'Message 3', 3, 1); + $this->send_fake_read_popup_notification($sender, $recipient, 'Message 4', 3, 2); + $this->send_fake_unread_popup_notification($sender, $recipient, 'Message 5', 4); + $this->send_fake_unread_popup_notification($sender, $recipient, 'Message 6', 5); + + $notifications = \core_message\api::get_popup_notifications($recipient->id, '', false, false, 'DESC', 2, 0); + + $this->assertEquals($notifications[0]->fullmessage, 'Message 6'); + $this->assertEquals($notifications[1]->fullmessage, 'Message 5'); + + $notifications = \core_message\api::get_popup_notifications($recipient->id, '', false, false, 'DESC', 2, 2); + + $this->assertEquals($notifications[0]->fullmessage, 'Message 4'); + $this->assertEquals($notifications[1]->fullmessage, 'Message 3'); + + $notifications = \core_message\api::get_popup_notifications($recipient->id, '', false, false, 'DESC', 0, 3); + + $this->assertEquals($notifications[0]->fullmessage, 'Message 3'); + $this->assertEquals($notifications[1]->fullmessage, 'Message 2'); + $this->assertEquals($notifications[2]->fullmessage, 'Message 1'); + } + + /** + * Test that the get_popup_notifications function returns embedded user details for the + * sender if requested. + */ + public function test_message_get_popup_notifications_embed_sender() { + $sender = $this->getDataGenerator()->create_user(array('firstname' => 'Test1', 'lastname' => 'User1')); + $recipient = $this->getDataGenerator()->create_user(array('firstname' => 'Test2', 'lastname' => 'User2')); + + $this->send_fake_read_popup_notification($sender, $recipient, 'Message 1', 1); + $this->send_fake_unread_popup_notification($sender, $recipient, 'Message 2', 2); + + $notifications = \core_message\api::get_popup_notifications($recipient->id, '', false, true, 'DESC'); + + $func = function($type) { + return function($notification) use ($type) { + $user = new stdClass(); + $user = username_load_fields_from_object($user, $notification, $type); + return $user; + }; + }; + $senders = array_map($func('userfrom'), $notifications); + $recipients = array_map($func('userto'), $notifications); + + $this->assertEquals($senders[0]->firstname, 'Test1'); + $this->assertEquals($senders[0]->lastname, 'User1'); + $this->assertEquals($senders[1]->firstname, 'Test1'); + $this->assertEquals($senders[1]->lastname, 'User1'); + + // Make sure we didn't get recipient details when they weren't requested. + $this->assertEmpty($recipients[0]->firstname); + $this->assertEmpty($recipients[0]->lastname); + $this->assertEmpty($recipients[1]->firstname); + $this->assertEmpty($recipients[1]->lastname); + } + + /** + * Test that the get_popup_notifications function returns embedded user details for the + * recipient if requested. + */ + public function test_message_get_popup_notifications_embed_recipient() { + $sender = $this->getDataGenerator()->create_user(array('firstname' => 'Test1', 'lastname' => 'User1')); + $recipient = $this->getDataGenerator()->create_user(array('firstname' => 'Test2', 'lastname' => 'User2')); + + $this->send_fake_read_popup_notification($sender, $recipient, 'Message 1', 1); + $this->send_fake_unread_popup_notification($sender, $recipient, 'Message 2', 2); + + $notifications = \core_message\api::get_popup_notifications($recipient->id, '', true, false, 'DESC'); + + $func = function($type) { + return function($notification) use ($type) { + $user = new stdClass(); + $user = username_load_fields_from_object($user, $notification, $type); + return $user; + }; + }; + $senders = array_map($func('userfrom'), $notifications); + $recipients = array_map($func('userto'), $notifications); + + $this->assertEquals($recipients[0]->firstname, 'Test2'); + $this->assertEquals($recipients[0]->lastname, 'User2'); + $this->assertEquals($recipients[1]->firstname, 'Test2'); + $this->assertEquals($recipients[1]->lastname, 'User2'); + + // Make sure we didn't get sender details when they weren't requested. + $this->assertEmpty($senders[0]->firstname); + $this->assertEmpty($senders[0]->lastname); + $this->assertEmpty($senders[1]->firstname); + $this->assertEmpty($senders[1]->lastname); + } + + /** + * Test that the get_popup_notifications function returns embedded all user details. + */ + public function test_message_get_popup_notifications_embed_both() { + $sender = $this->getDataGenerator()->create_user(array('firstname' => 'Test1', 'lastname' => 'User1')); + $recipient = $this->getDataGenerator()->create_user(array('firstname' => 'Test2', 'lastname' => 'User2')); + + $this->send_fake_read_popup_notification($sender, $recipient, 'Message 1', 1); + $this->send_fake_unread_popup_notification($sender, $recipient, 'Message 2', 2); + + $notifications = \core_message\api::get_popup_notifications($recipient->id, '', true, true, 'DESC'); + + $func = function($type) { + return function($notification) use ($type) { + $user = new stdClass(); + $user = username_load_fields_from_object($user, $notification, $type); + return $user; + }; + }; + $senders = array_map($func('userfrom'), $notifications); + $recipients = array_map($func('userto'), $notifications); + + $this->assertEquals($recipients[0]->firstname, 'Test2'); + $this->assertEquals($recipients[0]->lastname, 'User2'); + $this->assertEquals($recipients[1]->firstname, 'Test2'); + $this->assertEquals($recipients[1]->lastname, 'User2'); + + // Make sure we didn't get sender details when they weren't requested. + $this->assertEquals($senders[0]->firstname, 'Test1'); + $this->assertEquals($senders[0]->lastname, 'User1'); + $this->assertEquals($senders[1]->firstname, 'Test1'); + $this->assertEquals($senders[1]->lastname, 'User1'); + } + + /** + * Test count_unread_popup_notifications. + */ + public function test_message_count_unread_popup_notifications() { + $sender1 = $this->getDataGenerator()->create_user(array('firstname' => 'Test1', 'lastname' => 'User1')); + $sender2 = $this->getDataGenerator()->create_user(array('firstname' => 'Test2', 'lastname' => 'User2')); + $recipient1 = $this->getDataGenerator()->create_user(array('firstname' => 'Test3', 'lastname' => 'User3')); + $recipient2 = $this->getDataGenerator()->create_user(array('firstname' => 'Test4', 'lastname' => 'User4')); + + $this->send_fake_unread_popup_notification($sender1, $recipient1); + $this->send_fake_unread_popup_notification($sender1, $recipient1); + $this->send_fake_unread_popup_notification($sender2, $recipient1); + $this->send_fake_unread_popup_notification($sender1, $recipient2); + $this->send_fake_unread_popup_notification($sender2, $recipient2); + $this->send_fake_unread_popup_notification($sender2, $recipient2); + $this->send_fake_unread_popup_notification($sender2, $recipient2); + $this->send_fake_unread_popup_notification($sender2, $recipient2); + + $this->assertEquals(\core_message\api::count_unread_popup_notifications($recipient1->id), 3); + $this->assertEquals(\core_message\api::count_unread_popup_notifications($recipient2->id), 5); + } +} \ No newline at end of file diff --git a/message/tests/messagelib_test.php b/message/tests/messagelib_test.php index 906942f48ed..2d0b17aaace 100644 --- a/message/tests/messagelib_test.php +++ b/message/tests/messagelib_test.php @@ -944,297 +944,5 @@ class core_message_messagelib_testcase extends advanced_testcase { $this->assertTrue(message_is_user_blocked($recipient, $sender)); } - /** - * Test that the message_get_popup_notifications function will return only read notifications if requested. - */ - public function test_message_get_popup_notifications_read_only() { - $sender = $this->getDataGenerator()->create_user(array('firstname' => 'Test1', 'lastname' => 'User1')); - $recipient = $this->getDataGenerator()->create_user(array('firstname' => 'Test2', 'lastname' => 'User2')); - - $this->send_fake_read_popup_notification($sender, $recipient, 'Message 1', 2); - $this->send_fake_read_popup_notification($sender, $recipient, 'Message 2', 4); - - $notifications = message_get_popup_notifications($recipient->id, MESSAGE_READ); - - $this->assertEquals($notifications[0]->fullmessage, 'Message 2'); - $this->assertEquals($notifications[1]->fullmessage, 'Message 1'); - - // Check if we request read and unread but there are only read messages, it should - // still return those correctly. - $notifications = message_get_popup_notifications($recipient->id, ''); - - $this->assertEquals($notifications[0]->fullmessage, 'Message 2'); - $this->assertEquals($notifications[1]->fullmessage, 'Message 1'); - } - - /** - * Test that the message_get_popup_notifications function will return only unread notifications if requested. - */ - public function test_message_get_popup_notifications_unread_only() { - $sender = $this->getDataGenerator()->create_user(array('firstname' => 'Test1', 'lastname' => 'User1')); - $recipient = $this->getDataGenerator()->create_user(array('firstname' => 'Test2', 'lastname' => 'User2')); - - $this->send_fake_unread_popup_notification($sender, $recipient, 'Message 1', 2); - $this->send_fake_unread_popup_notification($sender, $recipient, 'Message 2', 4); - - $notifications = message_get_popup_notifications($recipient->id, MESSAGE_UNREAD); - - $this->assertEquals($notifications[0]->fullmessage, 'Message 2'); - $this->assertEquals($notifications[1]->fullmessage, 'Message 1'); - - // Check if we request read and unread but there are only read messages, it should - // still return those correctly. - $notifications = message_get_popup_notifications($recipient->id, ''); - - $this->assertEquals($notifications[0]->fullmessage, 'Message 2'); - $this->assertEquals($notifications[1]->fullmessage, 'Message 1'); - } - - /** - * Test that the message_get_popup_notifications function will return the correct notifications when both - * read and unread notifications are included. - */ - public function test_message_get_popup_notifications_mixed() { - $sender = $this->getDataGenerator()->create_user(array('firstname' => 'Test1', 'lastname' => 'User1')); - $recipient = $this->getDataGenerator()->create_user(array('firstname' => 'Test2', 'lastname' => 'User2')); - - $this->send_fake_read_popup_notification($sender, $recipient, 'Message 1', 1); - $this->send_fake_unread_popup_notification($sender, $recipient, 'Message 2', 2); - $this->send_fake_read_popup_notification($sender, $recipient, 'Message 3', 3, 1); - $this->send_fake_read_popup_notification($sender, $recipient, 'Message 4', 3, 2); - $this->send_fake_unread_popup_notification($sender, $recipient, 'Message 5', 4); - - $notifications = message_get_popup_notifications($recipient->id); - - $this->assertEquals($notifications[0]->fullmessage, 'Message 5'); - $this->assertEquals($notifications[1]->fullmessage, 'Message 4'); - $this->assertEquals($notifications[2]->fullmessage, 'Message 3'); - $this->assertEquals($notifications[3]->fullmessage, 'Message 2'); - $this->assertEquals($notifications[4]->fullmessage, 'Message 1'); - - $notifications = message_get_popup_notifications($recipient->id, MESSAGE_READ); - - $this->assertEquals($notifications[0]->fullmessage, 'Message 4'); - $this->assertEquals($notifications[1]->fullmessage, 'Message 3'); - $this->assertEquals($notifications[2]->fullmessage, 'Message 1'); - - $notifications = message_get_popup_notifications($recipient->id, MESSAGE_UNREAD); - - $this->assertEquals($notifications[0]->fullmessage, 'Message 5'); - $this->assertEquals($notifications[1]->fullmessage, 'Message 2'); - } - - /** - * Test that the message_get_popup_notifications function works correctly with limiting and offsetting - * the result set if requested. - */ - public function test_message_get_popup_notifications_all_with_limit_and_offset() { - $sender = $this->getDataGenerator()->create_user(array('firstname' => 'Test1', 'lastname' => 'User1')); - $recipient = $this->getDataGenerator()->create_user(array('firstname' => 'Test2', 'lastname' => 'User2')); - - $this->send_fake_read_popup_notification($sender, $recipient, 'Message 1', 1); - $this->send_fake_unread_popup_notification($sender, $recipient, 'Message 2', 2); - $this->send_fake_read_popup_notification($sender, $recipient, 'Message 3', 3, 1); - $this->send_fake_read_popup_notification($sender, $recipient, 'Message 4', 3, 2); - $this->send_fake_unread_popup_notification($sender, $recipient, 'Message 5', 4); - $this->send_fake_unread_popup_notification($sender, $recipient, 'Message 6', 5); - - $notifications = message_get_popup_notifications($recipient->id, '', false, false, 'DESC', 2, 0); - - $this->assertEquals($notifications[0]->fullmessage, 'Message 6'); - $this->assertEquals($notifications[1]->fullmessage, 'Message 5'); - - $notifications = message_get_popup_notifications($recipient->id, '', false, false, 'DESC', 2, 2); - - $this->assertEquals($notifications[0]->fullmessage, 'Message 4'); - $this->assertEquals($notifications[1]->fullmessage, 'Message 3'); - - $notifications = message_get_popup_notifications($recipient->id, '', false, false, 'DESC', 0, 3); - $this->assertEquals($notifications[0]->fullmessage, 'Message 3'); - $this->assertEquals($notifications[1]->fullmessage, 'Message 2'); - $this->assertEquals($notifications[2]->fullmessage, 'Message 1'); - } - - /** - * Test that the message_get_popup_notifications function returns embedded user details for the - * sender if requested. - */ - public function test_message_get_popup_notifications_embed_sender() { - $sender = $this->getDataGenerator()->create_user(array('firstname' => 'Test1', 'lastname' => 'User1')); - $recipient = $this->getDataGenerator()->create_user(array('firstname' => 'Test2', 'lastname' => 'User2')); - - $this->send_fake_read_popup_notification($sender, $recipient, 'Message 1', 1); - $this->send_fake_unread_popup_notification($sender, $recipient, 'Message 2', 2); - - $notifications = message_get_popup_notifications($recipient->id, '', false, true, 'DESC'); - - $func = function($type) { - return function($notification) use ($type) { - $user = new stdClass(); - $user = username_load_fields_from_object($user, $notification, $type); - return $user; - }; - }; - $senders = array_map($func('userfrom'), $notifications); - $recipients = array_map($func('userto'), $notifications); - - $this->assertEquals($senders[0]->firstname, 'Test1'); - $this->assertEquals($senders[0]->lastname, 'User1'); - $this->assertEquals($senders[1]->firstname, 'Test1'); - $this->assertEquals($senders[1]->lastname, 'User1'); - - // Make sure we didn't get recipient details when they weren't requested. - $this->assertEmpty($recipients[0]->firstname); - $this->assertEmpty($recipients[0]->lastname); - $this->assertEmpty($recipients[1]->firstname); - $this->assertEmpty($recipients[1]->lastname); - } - - /** - * Test that the message_get_popup_notifications function returns embedded user details for the - * recipient if requested. - */ - public function test_message_get_popup_notifications_embed_recipient() { - $sender = $this->getDataGenerator()->create_user(array('firstname' => 'Test1', 'lastname' => 'User1')); - $recipient = $this->getDataGenerator()->create_user(array('firstname' => 'Test2', 'lastname' => 'User2')); - - $this->send_fake_read_popup_notification($sender, $recipient, 'Message 1', 1); - $this->send_fake_unread_popup_notification($sender, $recipient, 'Message 2', 2); - - $notifications = message_get_popup_notifications($recipient->id, '', true, false, 'DESC'); - - $func = function($type) { - return function($notification) use ($type) { - $user = new stdClass(); - $user = username_load_fields_from_object($user, $notification, $type); - return $user; - }; - }; - $senders = array_map($func('userfrom'), $notifications); - $recipients = array_map($func('userto'), $notifications); - - $this->assertEquals($recipients[0]->firstname, 'Test2'); - $this->assertEquals($recipients[0]->lastname, 'User2'); - $this->assertEquals($recipients[1]->firstname, 'Test2'); - $this->assertEquals($recipients[1]->lastname, 'User2'); - - // Make sure we didn't get sender details when they weren't requested. - $this->assertEmpty($senders[0]->firstname); - $this->assertEmpty($senders[0]->lastname); - $this->assertEmpty($senders[1]->firstname); - $this->assertEmpty($senders[1]->lastname); - } - - /** - * Test that the message_get_popup_notifications function returns embedded all user details. - */ - public function test_message_get_popup_notifications_embed_both() { - $sender = $this->getDataGenerator()->create_user(array('firstname' => 'Test1', 'lastname' => 'User1')); - $recipient = $this->getDataGenerator()->create_user(array('firstname' => 'Test2', 'lastname' => 'User2')); - - $this->send_fake_read_popup_notification($sender, $recipient, 'Message 1', 1); - $this->send_fake_unread_popup_notification($sender, $recipient, 'Message 2', 2); - - $notifications = message_get_popup_notifications($recipient->id, '', true, true, 'DESC'); - - $func = function($type) { - return function($notification) use ($type) { - $user = new stdClass(); - $user = username_load_fields_from_object($user, $notification, $type); - return $user; - }; - }; - $senders = array_map($func('userfrom'), $notifications); - $recipients = array_map($func('userto'), $notifications); - - $this->assertEquals($recipients[0]->firstname, 'Test2'); - $this->assertEquals($recipients[0]->lastname, 'User2'); - $this->assertEquals($recipients[1]->firstname, 'Test2'); - $this->assertEquals($recipients[1]->lastname, 'User2'); - - // Make sure we didn't get sender details when they weren't requested. - $this->assertEquals($senders[0]->firstname, 'Test1'); - $this->assertEquals($senders[0]->lastname, 'User1'); - $this->assertEquals($senders[1]->firstname, 'Test1'); - $this->assertEquals($senders[1]->lastname, 'User1'); - } - - /** - * Test message_count_unread_popup_notifications. - */ - public function test_message_count_unread_popup_notifications() { - $sender1 = $this->getDataGenerator()->create_user(array('firstname' => 'Test1', 'lastname' => 'User1')); - $sender2 = $this->getDataGenerator()->create_user(array('firstname' => 'Test2', 'lastname' => 'User2')); - $recipient1 = $this->getDataGenerator()->create_user(array('firstname' => 'Test3', 'lastname' => 'User3')); - $recipient2 = $this->getDataGenerator()->create_user(array('firstname' => 'Test4', 'lastname' => 'User4')); - - $this->send_fake_unread_popup_notification($sender1, $recipient1); - $this->send_fake_unread_popup_notification($sender1, $recipient1); - $this->send_fake_unread_popup_notification($sender2, $recipient1); - $this->send_fake_unread_popup_notification($sender1, $recipient2); - $this->send_fake_unread_popup_notification($sender2, $recipient2); - $this->send_fake_unread_popup_notification($sender2, $recipient2); - $this->send_fake_unread_popup_notification($sender2, $recipient2); - $this->send_fake_unread_popup_notification($sender2, $recipient2); - - $this->assertEquals(message_count_unread_popup_notifications($recipient1->id), 3); - $this->assertEquals(message_count_unread_popup_notifications($recipient2->id), 5); - } - - - public function test_message_mark_all_read_for_user_touser() { - $sender = $this->getDataGenerator()->create_user(array('firstname' => 'Test1', 'lastname' => 'User1')); - $recipient = $this->getDataGenerator()->create_user(array('firstname' => 'Test2', 'lastname' => 'User2')); - - $this->send_fake_unread_popup_notification($sender, $recipient); - $this->send_fake_unread_popup_notification($sender, $recipient); - $this->send_fake_unread_popup_notification($sender, $recipient); - $this->send_fake_message($sender, $recipient); - $this->send_fake_message($sender, $recipient); - $this->send_fake_message($sender, $recipient); - - message_mark_all_read_for_user($recipient->id); - $this->assertEquals(message_count_unread_messages($recipient), 0); - } - - public function test_message_mark_all_read_for_user_touser_with_fromuser() { - $sender1 = $this->getDataGenerator()->create_user(array('firstname' => 'Test1', 'lastname' => 'User1')); - $sender2 = $this->getDataGenerator()->create_user(array('firstname' => 'Test3', 'lastname' => 'User3')); - $recipient = $this->getDataGenerator()->create_user(array('firstname' => 'Test2', 'lastname' => 'User2')); - - $this->send_fake_unread_popup_notification($sender1, $recipient); - $this->send_fake_unread_popup_notification($sender1, $recipient); - $this->send_fake_unread_popup_notification($sender1, $recipient); - $this->send_fake_message($sender1, $recipient); - $this->send_fake_message($sender1, $recipient); - $this->send_fake_message($sender1, $recipient); - $this->send_fake_unread_popup_notification($sender2, $recipient); - $this->send_fake_unread_popup_notification($sender2, $recipient); - $this->send_fake_unread_popup_notification($sender2, $recipient); - $this->send_fake_message($sender2, $recipient); - $this->send_fake_message($sender2, $recipient); - $this->send_fake_message($sender2, $recipient); - - message_mark_all_read_for_user($recipient->id, $sender1->id); - $this->assertEquals(message_count_unread_messages($recipient), 6); - } - - public function test_message_mark_all_read_for_user_touser_with_type() { - $sender = $this->getDataGenerator()->create_user(array('firstname' => 'Test1', 'lastname' => 'User1')); - $recipient = $this->getDataGenerator()->create_user(array('firstname' => 'Test2', 'lastname' => 'User2')); - - $this->send_fake_unread_popup_notification($sender, $recipient); - $this->send_fake_unread_popup_notification($sender, $recipient); - $this->send_fake_unread_popup_notification($sender, $recipient); - $this->send_fake_message($sender, $recipient); - $this->send_fake_message($sender, $recipient); - $this->send_fake_message($sender, $recipient); - - message_mark_all_read_for_user($recipient->id, 0, MESSAGE_TYPE_NOTIFICATION); - $this->assertEquals(message_count_unread_messages($recipient), 3); - - message_mark_all_read_for_user($recipient->id, 0, MESSAGE_TYPE_MESSAGE); - $this->assertEquals(message_count_unread_messages($recipient), 0); - } } -- 2.11.4.GIT