MDL-36941 core: convert existing api to use new table structure

Also deprecated the following functions -

1. message_move_userfrom_unread2read - It is not necessary
   for us to mark a message as read on user deletion.
2. message_get_blocked_users - Horrible logic used to
   determine if a user is blocked via reference on some
   randomly chosen 'isblocked' variable.
3. message_get_contacts - The same as above. This can be
   done in a much nicer way.
4. message_mark_message_read - We want two functions to do
   this to avoid confusing messages and notifications.
5. message_can_delete_message - This assumed the variable
   $message contained the 'useridto' property, which
   was present in the old table structure. We do not want
   future usages where a query is done on the new table
   and is simply passed as this won't contain this property.
6. message_delete_message - Same as above.
This commit is contained in:
Mark Nelson 2018-01-04 15:01:37 +08:00
parent 4cd439887a
commit 883ce42127
29 changed files with 1714 additions and 1096 deletions

View file

@ -54,7 +54,7 @@ class manager {
* @param \core\message\message $eventdata fully prepared event data for processors
* @param \stdClass $savemessage the message saved in 'message' table
* @param array $processorlist list of processors for target user
* @return int $messageid the id from 'message' or 'message_read' table (false is not returned)
* @return int $messageid the id from 'messages' (false is not returned)
*/
public static function send_message($eventdata, \stdClass $savemessage, array $processorlist) {
global $CFG;
@ -78,26 +78,26 @@ class manager {
if (empty($processorlist)) {
// Trigger event for sending a message - we need to do this before marking as read!
\core\event\message_sent::create_from_ids(
$eventdata->userfrom->id,
$eventdata->userto->id,
$savemessage->id,
$eventdata->courseid
if (!$eventdata->notification) {
\core\event\message_sent::create_from_ids(
$eventdata->userfrom->id,
$eventdata->userto->id,
$savemessage->id,
$eventdata->courseid
)->trigger();
if ($savemessage->notification or empty($CFG->messaging)) {
// If they have deselected all processors and its a notification mark it read. The user doesn't want to be bothered.
// The same goes if the messaging is completely disabled.
// We cannot insert directly to the message_read table because we want to get all events in proper order!
$messageid = message_mark_message_read($savemessage, time(), true);
} else {
// Just add it to the list of unread messages, there is no way it could be delivered to them,
// but they can read it via the messaging UI later.
$messageid = $savemessage->id;
}
return $messageid;
if ($eventdata->notification or empty($CFG->messaging)) {
// If they have deselected all processors and its a notification mark it read. The user doesn't want to be bothered.
// The same goes if the messaging is completely disabled.
if ($eventdata->notification) {
\core_message\api::mark_notification_as_read($eventdata->userto->id, $savemessage->id);
} else {
\core_message\api::mark_message_as_read($eventdata->userto->id, $savemessage->id);
}
}
return $savemessage->id;
}
// Let the manager do the sending or buffering when db transaction in progress.
@ -133,7 +133,6 @@ class manager {
return $savemessage->id;
}
$failed = false;
foreach ($processorlist as $procname) {
// Let new messaging class add custom content based on the processor.
$proceventdata = ($eventdata instanceof message) ? $eventdata->get_eventobject_for_processor($procname) : $eventdata;
@ -142,40 +141,36 @@ class manager {
$processor = \core_message\api::get_processed_processor_object($stdproc);
if (!$processor->object->send_message($proceventdata)) {
debugging('Error calling message processor ' . $procname);
$failed = true;
// Previously the $messageid = false here was overridden
// by other processors and message_mark_message_read() below.
}
}
// Trigger event for sending a message - must be done before marking as read.
\core\event\message_sent::create_from_ids(
$eventdata->userfrom->id,
$eventdata->userto->id,
$savemessage->id,
$eventdata->courseid
if (!$eventdata->notification) {
\core\event\message_sent::create_from_ids(
$eventdata->userfrom->id,
$eventdata->userto->id,
$savemessage->id,
$eventdata->courseid
)->trigger();
if (empty($CFG->messaging)) {
// If messaging is disabled and they previously had forum notifications handled by the popup processor
// or any processor that puts a row in message_working then the notification will remain forever
// unread. To prevent this mark the message read if messaging is disabled.
$messageid = message_mark_message_read($savemessage, time());
} else if ($failed) {
// Something failed, better keep it as unread then.
$messageid = $savemessage->id;
} else if ($DB->count_records('message_working', array('unreadmessageid' => $savemessage->id)) == 0) {
// If there is no more processors that want to process this we can move message to message_read.
$messageid = message_mark_message_read($savemessage, time(), true);
} else {
// Some processor is still working on the data, let's keep it unread.
$messageid = $savemessage->id;
}
return $messageid;
// If messaging is disabled and they previously had forum notifications handled by the popup processor
// or any processor that puts a row in message_working then the notification will remain forever
// unread. To prevent this mark the message read if messaging is disabled.
if (empty($CFG->messaging) && $eventdata->notification) {
\core_message\api::mark_notification_as_read($eventdata->userto->id, $savemessage->id);
}
// If there is no more processors that want to process this we can mark the message as read.
if ($DB->count_records('message_working', array('unreadmessageid' => $savemessage->id)) == 0) {
if ($eventdata->notification) {
\core_message\api::mark_notification_as_read($eventdata->userto->id, $savemessage->id);
} else {
\core_message\api::mark_message_as_read($eventdata->userto->id, $savemessage->id);
}
}
return $savemessage->id;
}
/**

View file

@ -50,7 +50,7 @@ class messaging_cleanup_task extends scheduled_task {
if (!empty($CFG->messagingdeletereadnotificationsdelay)) {
$notificationdeletetime = $timenow - $CFG->messagingdeletereadnotificationsdelay;
$params = array('notificationdeletetime' => $notificationdeletetime);
$DB->delete_records_select('message_read', 'notification=1 AND timeread<:notificationdeletetime', $params);
$DB->delete_records_select('notifications', 'timeread < :notificationdeletetime', $params);
}
}

View file

@ -6577,3 +6577,192 @@ function question_is_only_toplevel_category_in_context($categoryid) {
return question_is_only_child_of_top_category_in_context($categoryid);
}
/**
* Moves messages from a particular user from the message table (unread messages) to message_read
* This is typically only used when a user is deleted
*
* @param object $userid User id
* @return boolean success
* @deprecated since Moodle 3.5
*/
function message_move_userfrom_unread2read($userid) {
debugging('message_move_userfrom_unread2read() is deprecated and is no longer used.', DEBUG_DEVELOPER);
global $DB;
// Move all unread messages from message table to message_read.
if ($messages = $DB->get_records_select('message', 'useridfrom = ?', array($userid), 'timecreated')) {
foreach ($messages as $message) {
message_mark_message_read($message, 0); // Set timeread to 0 as the message was never read.
}
}
return true;
}
/**
* Retrieve users blocked by $user1
*
* @param object $user1 the user whose messages are being viewed
* @param object $user2 the user $user1 is talking to. If they are being blocked
* they will have a variable called 'isblocked' added to their user object
* @return array the users blocked by $user1
* @deprecated since Moodle 3.5
*/
function message_get_blocked_users($user1=null, $user2=null) {
debugging('message_get_blocked_users() is deprecated, please use \core_message\api::get_blocked_users() instead.',
DEBUG_DEVELOPER);
global $USER;
if (empty($user1)) {
$user1 = new stdClass();
$user1->id = $USER->id;
}
return \core_message\api::get_blocked_users($user1->id);
}
/**
* Retrieve $user1's contacts (online, offline and strangers)
*
* @param object $user1 the user whose messages are being viewed
* @param object $user2 the user $user1 is talking to. If they are a contact
* they will have a variable called 'iscontact' added to their user object
* @return array containing 3 arrays. array($onlinecontacts, $offlinecontacts, $strangers)
* @deprecated since Moodle 3.5
*/
function message_get_contacts($user1=null, $user2=null) {
debugging('message_get_contacts() is deprecated and is no longer used.', DEBUG_DEVELOPER);
global $DB, $CFG, $USER;
if (empty($user1)) {
$user1 = $USER;
}
if (!empty($user2)) {
$user2->iscontact = false;
}
$timetoshowusers = 300; // Seconds default.
if (isset($CFG->block_online_users_timetosee)) {
$timetoshowusers = $CFG->block_online_users_timetosee * 60;
}
// Rime which a user is counting as being active since.
$timefrom = time() - $timetoshowusers;
// People in our contactlist who are online.
$onlinecontacts = array();
// People in our contactlist who are offline.
$offlinecontacts = array();
// People who are not in our contactlist but have sent us a message.
$strangers = array();
// Get all in our contact list who are not blocked in our and count messages we have waiting from each of them.
$rs = \core_message\api::get_contacts_with_unread_message_count($user1->id);
foreach ($rs as $rd) {
if ($rd->lastaccess >= $timefrom) {
// They have been active recently, so are counted online.
$onlinecontacts[] = $rd;
} else {
$offlinecontacts[] = $rd;
}
if (!empty($user2) && $user2->id == $rd->id) {
$user2->iscontact = true;
}
}
// Get messages from anyone who isn't in our contact list and count the number of messages we have from each of them.
$rs = \core_message\api::get_non_contacts_with_unread_message_count($user1->id);
// Add user id as array index, so supportuser and noreply user don't get duplicated (if they are real users).
foreach ($rs as $rd) {
$strangers[$rd->id] = $rd;
}
// Add noreply user and support user to the list, if they don't exist.
$supportuser = core_user::get_support_user();
if (!isset($strangers[$supportuser->id]) && !$supportuser->deleted) {
$supportuser->messagecount = message_count_unread_messages($USER, $supportuser);
if ($supportuser->messagecount > 0) {
$strangers[$supportuser->id] = $supportuser;
}
}
$noreplyuser = core_user::get_noreply_user();
if (!isset($strangers[$noreplyuser->id]) && !$noreplyuser->deleted) {
$noreplyuser->messagecount = message_count_unread_messages($USER, $noreplyuser);
if ($noreplyuser->messagecount > 0) {
$strangers[$noreplyuser->id] = $noreplyuser;
}
}
return array($onlinecontacts, $offlinecontacts, $strangers);
}
/**
* Mark a single message as read
*
* @param stdClass $message An object with an object property ie $message->id which is an id in the message table
* @param int $timeread the timestamp for when the message should be marked read. Usually time().
* @param bool $messageworkingempty Is the message_working table already confirmed empty for this message?
* @return int the ID of the message in the messags table
* @deprecated since Moodle 3.5
*/
function message_mark_message_read($message, $timeread, $messageworkingempty=false) {
debugging('message_mark_message_read() is deprecated, please use \core_message\api::mark_message_as_read()
or \core_message\api::mark_notification_as_read().', DEBUG_DEVELOPER);
global $DB;
if (!empty($message->notification)) {
\core_message\api::mark_notification_as_read($message->useridto, $message->id, $timeread);
} else {
\core_message\api::mark_message_as_read($message->useridto, $message->id, $timeread);
}
// If any processors have pending actions abort them.
if (!$messageworkingempty) {
$DB->delete_records('message_working', array('unreadmessageid' => $message->id));
}
return $message->id;
}
/**
* Checks if a user can delete a message.
*
* @param stdClass $message the message to delete
* @param string $userid the user id of who we want to delete the message for (this may be done by the admin
* but will still seem as if it was by the user)
* @return bool Returns true if a user can delete the message, false otherwise.
* @deprecated since Moodle 3.5
*/
function message_can_delete_message($message, $userid) {
debugging('message_can_delete_message() is deprecated, please use \core_message\api::can_delete_message() instead.',
DEBUG_DEVELOPER);
return \core_message\api::can_delete_message($userid, $message->id);
}
/**
* Deletes a message.
*
* This function does not verify any permissions.
*
* @param stdClass $message the message to delete
* @param string $userid the user id of who we want to delete the message for (this may be done by the admin
* but will still seem as if it was by the user)
* @return bool
* @deprecated since Moodle 3.5
*/
function message_delete_message($message, $userid) {
debugging('message_delete_message() is deprecated, please use \core_message\api::delete_message() instead.',
DEBUG_DEVELOPER);
return \core_message\api::delete_message($userid, $message->id);
}

View file

@ -127,33 +127,53 @@ function message_send($eventdata) {
$userstate = 'loggedoff';
}
// Create the message object
$savemessage = new stdClass();
$savemessage->courseid = $eventdata->courseid;
$savemessage->useridfrom = $eventdata->userfrom->id;
$savemessage->useridto = $eventdata->userto->id;
$savemessage->subject = $eventdata->subject;
$savemessage->fullmessage = $eventdata->fullmessage;
$savemessage->fullmessageformat = $eventdata->fullmessageformat;
$savemessage->fullmessagehtml = $eventdata->fullmessagehtml;
$savemessage->smallmessage = $eventdata->smallmessage;
$savemessage->notification = $eventdata->notification;
$savemessage->eventtype = $eventdata->name;
$savemessage->component = $eventdata->component;
// Check if we are creating a notification or message.
if ($eventdata->notification) {
$table = 'notifications';
if (!empty($eventdata->contexturl)) {
$savemessage->contexturl = (string)$eventdata->contexturl;
$tabledata = new stdClass();
$tabledata->useridfrom = $eventdata->userfrom->id;
$tabledata->useridto = $eventdata->userto->id;
$tabledata->subject = $eventdata->subject;
$tabledata->fullmessage = $eventdata->fullmessage;
$tabledata->fullmessageformat = $eventdata->fullmessageformat;
$tabledata->fullmessagehtml = $eventdata->fullmessagehtml;
$tabledata->smallmessage = $eventdata->smallmessage;
$tabledata->eventtype = $eventdata->name;
$tabledata->component = $eventdata->component;
if (!empty($eventdata->contexturl)) {
$tabledata->contexturl = (string)$eventdata->contexturl;
} else {
$tabledata->contexturl = null;
}
if (!empty($eventdata->contexturlname)) {
$tabledata->contexturlname = (string)$eventdata->contexturlname;
} else {
$tabledata->contexturlname = null;
}
} else {
$savemessage->contexturl = null;
$table = 'messages';
if (!$conversationid = \core_message\api::get_conversation_between_users($eventdata->userfrom->id,
$eventdata->userto->id)) {
$conversationid = \core_message\api::create_conversation_between_users($eventdata->userfrom->id,
$eventdata->userto->id);
}
$tabledata = new stdClass();
$tabledata->courseid = $eventdata->courseid;
$tabledata->useridfrom = $eventdata->userfrom->id;
$tabledata->conversationid = $conversationid;
$tabledata->subject = $eventdata->subject;
$tabledata->fullmessage = $eventdata->fullmessage;
$tabledata->fullmessageformat = $eventdata->fullmessageformat;
$tabledata->fullmessagehtml = $eventdata->fullmessagehtml;
$tabledata->smallmessage = $eventdata->smallmessage;
}
if (!empty($eventdata->contexturlname)) {
$savemessage->contexturlname = (string)$eventdata->contexturlname;
} else {
$savemessage->contexturlname = null;
}
$savemessage->timecreated = time();
$tabledata->timecreated = time();
if (PHPUNIT_TEST and class_exists('phpunit_util')) {
// Add some more tests to make sure the normal code can actually work.
@ -173,9 +193,21 @@ function message_send($eventdata) {
unset($messageproviders);
// Now ask phpunit if it wants to catch this message.
if (phpunit_util::is_redirecting_messages()) {
$savemessage->timeread = time();
$messageid = $DB->insert_record('message_read', $savemessage);
$message = $DB->get_record('message_read', array('id'=>$messageid));
$messageid = $DB->insert_record($table, $tabledata);
$message = $DB->get_record($table, array('id' => $messageid));
// Add the useridto attribute for BC.
$message->useridto = $eventdata->userto->id;
// Mark the message/notification as read.
if ($eventdata->notification) {
\core_message\api::mark_notification_as_read($eventdata->userto->id, $message->id);
} else {
\core_message\api::mark_message_as_read($eventdata->userto->id, $message->id);
}
// Unit tests need this detail.
$message->notification = $eventdata->notification;
phpunit_util::message_sent($message);
return $messageid;
}
@ -183,7 +215,7 @@ function message_send($eventdata) {
// Fetch enabled processors.
// If we are dealing with a message some processors may want to handle it regardless of user and site settings.
if (empty($savemessage->notification)) {
if (!$eventdata->notification) {
$processors = array_filter(get_message_processors(false), function($processor) {
if ($processor->object->force_process_messages()) {
return true;
@ -226,7 +258,7 @@ function message_send($eventdata) {
}
// Populate the list of processors we will be using
if (empty($savemessage->notification) && $processor->object->force_process_messages()) {
if (!$eventdata->notification && $processor->object->force_process_messages()) {
$processorlist[] = $processor->name;
} else if ($permitted == 'forced' && $userisconfigured) {
// An admin is forcing users to use this message processor. Use this processor unconditionally.
@ -248,20 +280,20 @@ function message_send($eventdata) {
}
// Only cache messages, not notifications.
if (empty($savemessage->notification)) {
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($savemessage->useridfrom,
$savemessage->useridto);
$cache->set($key, $savemessage->timecreated);
$key = \core_message\helper::get_last_message_time_created_cache_key($eventdata->userfrom->id,
$eventdata->userto->id);
$cache->set($key, $tabledata->timecreated);
}
// Store unread message just in case we get a fatal error any time later.
$savemessage->id = $DB->insert_record('message', $savemessage);
$eventdata->savedmessageid = $savemessage->id;
$tabledata->id = $DB->insert_record($table, $tabledata);
$eventdata->savedmessageid = $tabledata->id;
// Let the manager do the sending or buffering when db transaction in progress.
return \core\message\manager::send_message($eventdata, $savemessage, $processorlist);
return \core\message\manager::send_message($eventdata, $tabledata, $processorlist);
}

View file

@ -4071,9 +4071,6 @@ function delete_user(stdClass $user) {
// Delete all grades - backup is kept in grade_grades_history table.
grade_user_delete($user->id);
// Move unread messages from this user to read.
message_move_userfrom_unread2read($user->id);
// TODO: remove from cohorts using standard API here.
// Remove user tags.

View file

@ -33,7 +33,7 @@
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class phpunit_message_sink {
/** @var array of records from message_read table */
/** @var array of records from messages table */
protected $messages = array();
/**
@ -48,7 +48,7 @@ class phpunit_message_sink {
/**
* To be called from phpunit_util only!
*
* @param stdClass $message record from message_read table
* @param stdClass $message record from messages table
*/
public function add_message($message) {
/* Number messages from 0. */
@ -58,7 +58,7 @@ class phpunit_message_sink {
/**
* Returns all redirected messages.
*
* The instances are records form the message_read table.
* The instances are records from the messages table.
* The array indexes are numbered from 0 and the order is matching
* the creation of events.
*

View file

@ -50,7 +50,7 @@ class phpunit_phpmailer_sink {
/**
* To be called from phpunit_util only!
*
* @param stdClass $message record from message_read table
* @param stdClass $message record from messages table
*/
public function add_message($message) {
/* Number messages from 0. */
@ -60,7 +60,7 @@ class phpunit_phpmailer_sink {
/**
* Returns all redirected messages.
*
* The instances are records form the message_read table.
* The instances are records from the messages table.
* The array indexes are numbered from 0 and the order is matching
* the creation of events.
*

View file

@ -714,7 +714,7 @@ class phpunit_util extends testing_util {
/**
* To be called from messagelib.php only!
*
* @param stdClass $message record from message_read table
* @param stdClass $message record from messages table
* @return bool true means send message, false means message "sent" to sink.
*/
public static function message_sent($message) {
@ -765,7 +765,7 @@ class phpunit_util extends testing_util {
/**
* To be called from messagelib.php only!
*
* @param stdClass $message record from message_read table
* @param stdClass $message record from messages table
* @return bool true means send message, false means message "sent" to sink.
*/
public static function phpmailer_sent($message) {

View file

@ -167,7 +167,7 @@ class core_message_testcase extends advanced_testcase {
$emails = $sink->get_messages();
$this->assertCount(1, $emails);
$email = reset($emails);
$recordexists = $DB->record_exists('message', array('id' => $messageid));
$recordexists = $DB->record_exists('messages', array('id' => $messageid));
$this->assertSame(true, $recordexists);
$this->assertSame($user1->email, $email->from);
$this->assertSame($user2->email, $email->to);
@ -207,7 +207,7 @@ class core_message_testcase extends advanced_testcase {
$emails = $sink->get_messages();
$this->assertCount(1, $emails);
$email = reset($emails);
$recordexists = $DB->record_exists('message', array('id' => $messageid));
$recordexists = $DB->record_exists('messages', array('id' => $messageid));
$this->assertSame(true, $recordexists);
$this->assertSame($user1->email, $email->from);
$this->assertSame($user2->email, $email->to);

View file

@ -218,14 +218,15 @@ class core_messagelib_testcase extends advanced_testcase {
$this->assertEquals($message->smallmessage, $savedmessage->smallmessage);
$this->assertEquals($message->smallmessage, $savedmessage->smallmessage);
$this->assertEquals($message->notification, $savedmessage->notification);
$this->assertNull($savedmessage->contexturl);
$this->assertNull($savedmessage->contexturlname);
$this->assertTimeCurrent($savedmessage->timecreated);
$record = $DB->get_record('message_read', array('id' => $savedmessage->id), '*', MUST_EXIST);
$record = $DB->get_record('messages', array('id' => $savedmessage->id), '*', MUST_EXIST);
unset($savedmessage->useridto);
unset($savedmessage->notification);
$this->assertEquals($record, $savedmessage);
$sink->clear();
$this->assertFalse($DB->record_exists('message', array()));
$DB->delete_records('message_read', array());
$this->assertTrue($DB->record_exists('message_user_actions', array('userid' => $user2->id, 'messageid' => $messageid,
'action' => \core_message\api::MESSAGE_ACTION_READ)));
$DB->delete_records('messages', array());
$message = new \core\message\message();
$message->courseid = 1;
@ -239,8 +240,7 @@ class core_messagelib_testcase extends advanced_testcase {
$message->fullmessagehtml = '<p>message body</p>';
$message->smallmessage = 'small message';
$message->notification = '0';
$message->contexturl = new moodle_url('/');
$message->contexturlname = 'front';
$sink = $this->redirectMessages();
$messageid = message_send($message);
$savedmessages = $sink->get_messages();
@ -255,14 +255,15 @@ class core_messagelib_testcase extends advanced_testcase {
$this->assertEquals($message->smallmessage, $savedmessage->smallmessage);
$this->assertEquals($message->smallmessage, $savedmessage->smallmessage);
$this->assertEquals($message->notification, $savedmessage->notification);
$this->assertEquals($message->contexturl->out(), $savedmessage->contexturl);
$this->assertEquals($message->contexturlname, $savedmessage->contexturlname);
$this->assertTimeCurrent($savedmessage->timecreated);
$record = $DB->get_record('message_read', array('id' => $savedmessage->id), '*', MUST_EXIST);
$record = $DB->get_record('messages', array('id' => $savedmessage->id), '*', MUST_EXIST);
unset($savedmessage->useridto);
unset($savedmessage->notification);
$this->assertEquals($record, $savedmessage);
$sink->clear();
$this->assertFalse($DB->record_exists('message', array()));
$DB->delete_records('message_read', array());
$this->assertTrue($DB->record_exists('message_user_actions', array('userid' => $user2->id, 'messageid' => $messageid,
'action' => \core_message\api::MESSAGE_ACTION_READ)));
$DB->delete_records('messages', array());
// Test phpunit problem detection.
@ -297,8 +298,7 @@ class core_messagelib_testcase extends advanced_testcase {
}
$this->assertCount(0, $sink->get_messages());
$sink->close();
$this->assertFalse($DB->record_exists('message', array()));
$this->assertFalse($DB->record_exists('message_read', array()));
$this->assertFalse($DB->record_exists('messages', array()));
// Invalid users.
@ -420,10 +420,11 @@ class core_messagelib_testcase extends advanced_testcase {
$messageid = message_send($message);
$emails = $sink->get_messages();
$this->assertCount(0, $emails);
$savedmessage = $DB->get_record('message', array('id' => $messageid), '*', MUST_EXIST);
$savedmessage = $DB->get_record('messages', array('id' => $messageid), '*', MUST_EXIST);
$sink->clear();
$this->assertFalse($DB->record_exists('message_read', array()));
$DB->delete_records('message', array());
$this->assertFalse($DB->record_exists('message_user_actions', array()));
$DB->delete_records('messages', array());
$DB->delete_records('message_user_actions', array());
$events = $eventsink->get_events();
$this->assertCount(1, $events);
$this->assertInstanceOf('\core\event\message_sent', $events[0]);
@ -447,10 +448,12 @@ class core_messagelib_testcase extends advanced_testcase {
$messageid = message_send($message);
$emails = $sink->get_messages();
$this->assertCount(0, $emails);
$savedmessage = $DB->get_record('message_read', array('id' => $messageid), '*', MUST_EXIST);
$savedmessage = $DB->get_record('messages', array('id' => $messageid), '*', MUST_EXIST);
$sink->clear();
$this->assertFalse($DB->record_exists('message', array()));
$DB->delete_records('message_read', array());
$this->assertTrue($DB->record_exists('message_user_actions', array('userid' => $user2->id, 'messageid' => $messageid,
'action' => \core_message\api::MESSAGE_ACTION_READ)));
$DB->delete_records('messages', array());
$DB->delete_records('message_user_actions', array());
$events = $eventsink->get_events();
$this->assertCount(2, $events);
$this->assertInstanceOf('\core\event\message_sent', $events[0]);
@ -475,14 +478,12 @@ class core_messagelib_testcase extends advanced_testcase {
$messageid = message_send($message);
$emails = $sink->get_messages();
$this->assertCount(0, $emails);
$savedmessage = $DB->get_record('message_read', array('id' => $messageid), '*', MUST_EXIST);
$savedmessage = $DB->get_record('notifications', array('id' => $messageid), '*', MUST_EXIST);
$sink->clear();
$this->assertFalse($DB->record_exists('message', array()));
$DB->delete_records('message_read', array());
$this->assertFalse($DB->record_exists('messages', array()));
$DB->delete_records('notifications', array());
$events = $eventsink->get_events();
$this->assertCount(2, $events);
$this->assertInstanceOf('\core\event\message_sent', $events[0]);
$this->assertInstanceOf('\core\event\message_viewed', $events[1]);
$this->assertCount(0, $events);
$eventsink->clear();
// Will always use the pop-up processor.
@ -507,10 +508,11 @@ class core_messagelib_testcase extends advanced_testcase {
$messageid = message_send($message);
$emails = $sink->get_messages();
$this->assertCount(0, $emails);
$savedmessage = $DB->get_record('message', array('id' => $messageid), '*', MUST_EXIST);
$savedmessage = $DB->get_record('messages', array('id' => $messageid), '*', MUST_EXIST);
$sink->clear();
$this->assertFalse($DB->record_exists('message_read', array()));
$DB->delete_records('message', array());
$this->assertFalse($DB->record_exists('message_user_actions', array()));
$DB->delete_records('messages', array());
$DB->delete_records('message_user_actions', array());
$events = $eventsink->get_events();
$this->assertCount(1, $events);
$this->assertInstanceOf('\core\event\message_sent', $events[0]);
@ -537,15 +539,15 @@ class core_messagelib_testcase extends advanced_testcase {
$emails = $sink->get_messages();
$this->assertCount(1, $emails);
$email = reset($emails);
$savedmessage = $DB->get_record('message', array('id' => $messageid), '*', MUST_EXIST);
$savedmessage = $DB->get_record('messages', array('id' => $messageid), '*', MUST_EXIST);
$this->assertSame($user1->email, $email->from);
$this->assertSame($user2->email, $email->to);
$this->assertSame($message->subject, $email->subject);
$this->assertNotEmpty($email->header);
$this->assertNotEmpty($email->body);
$sink->clear();
$this->assertFalse($DB->record_exists('message_read', array()));
$DB->delete_records('message_read', array());
$this->assertFalse($DB->record_exists('message_user_actions', array()));
$DB->delete_records('message_user_actions', array());
$events = $eventsink->get_events();
$this->assertCount(1, $events);
$this->assertInstanceOf('\core\event\message_sent', $events[0]);
@ -570,7 +572,7 @@ class core_messagelib_testcase extends advanced_testcase {
$emails = $sink->get_messages();
$this->assertCount(1, $emails);
$email = reset($emails);
$savedmessage = $DB->get_record('message', array('id' => $messageid), '*', MUST_EXIST);
$savedmessage = $DB->get_record('messages', array('id' => $messageid), '*', MUST_EXIST);
$working = $DB->get_record('message_working', array('unreadmessageid' => $messageid), '*', MUST_EXIST);
$this->assertSame($user1->email, $email->from);
$this->assertSame($user2->email, $email->to);
@ -578,8 +580,9 @@ class core_messagelib_testcase extends advanced_testcase {
$this->assertNotEmpty($email->header);
$this->assertNotEmpty($email->body);
$sink->clear();
$this->assertFalse($DB->record_exists('message_read', array()));
$DB->delete_records('message', array());
$this->assertFalse($DB->record_exists('message_user_actions', array()));
$DB->delete_records('messages', array());
$DB->delete_records('message_user_actions', array());
$events = $eventsink->get_events();
$this->assertCount(1, $events);
$this->assertInstanceOf('\core\event\message_sent', $events[0]);
@ -603,11 +606,11 @@ class core_messagelib_testcase extends advanced_testcase {
$messageid = message_send($message);
$emails = $sink->get_messages();
$this->assertCount(0, $emails);
$savedmessage = $DB->get_record('message', array('id' => $messageid), '*', MUST_EXIST);
$savedmessage = $DB->get_record('messages', array('id' => $messageid), '*', MUST_EXIST);
$working = $DB->get_record('message_working', array('unreadmessageid' => $messageid), '*', MUST_EXIST);
$sink->clear();
$this->assertFalse($DB->record_exists('message_read', array()));
$DB->delete_records('message', array());
$this->assertFalse($DB->record_exists('message_user_actions', array()));
$DB->delete_records('messages', array());
$events = $eventsink->get_events();
$this->assertCount(1, $events);
$this->assertInstanceOf('\core\event\message_sent', $events[0]);
@ -641,10 +644,10 @@ class core_messagelib_testcase extends advanced_testcase {
$messageid = message_send($message);
$emails = $sink->get_messages();
$this->assertCount(0, $emails);
$savedmessage = $DB->get_record('message', array('id' => $messageid), '*', MUST_EXIST);
$savedmessage = $DB->get_record('messages', array('id' => $messageid), '*', MUST_EXIST);
$sink->clear();
$this->assertFalse($DB->record_exists('message_read', array()));
$DB->delete_records('message', array());
$this->assertFalse($DB->record_exists('message_user_actions', array()));
$DB->delete_records('messages', array());
$events = $eventsink->get_events();
$this->assertCount(0, $events);
$eventsink->clear();
@ -674,9 +677,9 @@ class core_messagelib_testcase extends advanced_testcase {
$messageid = message_send($message);
$emails = $sink->get_messages();
$this->assertCount(0, $emails);
$savedmessage = $DB->get_record('message', array('id' => $messageid), '*', MUST_EXIST);
$savedmessage = $DB->get_record('messages', array('id' => $messageid), '*', MUST_EXIST);
$sink->clear();
$this->assertFalse($DB->record_exists('message_read', array()));
$this->assertFalse($DB->record_exists('message_user_actions', array()));
$events = $eventsink->get_events();
$this->assertCount(1, $events);
$this->assertInstanceOf('\core\event\message_sent', $events[0]);
@ -689,8 +692,8 @@ class core_messagelib_testcase extends advanced_testcase {
$transaction = $DB->start_delegated_transaction();
message_send($message);
message_send($message);
$this->assertCount(3, $DB->get_records('message'));
$this->assertFalse($DB->record_exists('message_read', array()));
$this->assertCount(3, $DB->get_records('messages'));
$this->assertFalse($DB->record_exists('message_user_actions', array()));
$events = $eventsink->get_events();
$this->assertCount(0, $events);
$transaction->allow_commit();
@ -699,14 +702,13 @@ class core_messagelib_testcase extends advanced_testcase {
$this->assertInstanceOf('\core\event\message_sent', $events[0]);
$this->assertInstanceOf('\core\event\message_sent', $events[1]);
$eventsink->clear();
$DB->delete_records('message', array());
$DB->delete_records('message_read', array());
$DB->delete_records('messages', array());
$transaction = $DB->start_delegated_transaction();
message_send($message);
message_send($message);
$this->assertCount(2, $DB->get_records('message'));
$this->assertCount(0, $DB->get_records('message_read'));
$this->assertCount(2, $DB->get_records('messages'));
$this->assertCount(0, $DB->get_records('message_user_actions'));
$events = $eventsink->get_events();
$this->assertCount(0, $events);
try {
@ -716,16 +718,14 @@ class core_messagelib_testcase extends advanced_testcase {
}
$events = $eventsink->get_events();
$this->assertCount(0, $events);
$this->assertCount(0, $DB->get_records('message'));
$this->assertCount(0, $DB->get_records('message_read'));
$this->assertCount(0, $DB->get_records('messages'));
message_send($message);
$this->assertCount(1, $DB->get_records('message'));
$this->assertCount(0, $DB->get_records('message_read'));
$this->assertCount(1, $DB->get_records('messages'));
$this->assertCount(0, $DB->get_records('message_user_actions'));
$events = $eventsink->get_events();
$this->assertCount(1, $events);
$this->assertInstanceOf('\core\event\message_sent', $events[0]);
$sink->clear();
$DB->delete_records('message_read', array());
}
public function test_rollback() {