mirror of
https://github.com/moodle/moodle.git
synced 2025-01-31 12:45:04 +01:00
Merge branch 'wip_MDL-45941_m28_message' of git://github.com/skodak/moodle
This commit is contained in:
commit
65f4667348
@ -41,6 +41,35 @@ defined('MOODLE_INTERNAL') || die();
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
class message_sent extends base {
|
||||
/**
|
||||
* Create event using ids.
|
||||
* @param int $userfromid
|
||||
* @param int $usertoid
|
||||
* @param int $messageid
|
||||
* @return message_sent
|
||||
*/
|
||||
public static function create_from_ids($userfromid, $usertoid, $messageid) {
|
||||
// We may be sending a message from the 'noreply' address, which means we are not actually sending a
|
||||
// message from a valid user. In this case, we will set the userid to 0.
|
||||
// Check if the userid is valid.
|
||||
if (!\core_user::is_real_user($userfromid)) {
|
||||
$userfromid = 0;
|
||||
}
|
||||
|
||||
$event = self::create(array(
|
||||
'userid' => $userfromid,
|
||||
'context' => \context_system::instance(),
|
||||
'relateduserid' => $usertoid,
|
||||
'other' => array(
|
||||
// In earlier versions it can either be the id in the 'message_read' or 'message' table.
|
||||
// Now it is always the id from 'message' table. Please note that the record is still moved
|
||||
// to the 'message_read' table later when message marked as read.
|
||||
'messageid' => $messageid
|
||||
)
|
||||
));
|
||||
|
||||
return $event;
|
||||
}
|
||||
|
||||
/**
|
||||
* Init method.
|
||||
|
184
lib/classes/message/manager.php
Normal file
184
lib/classes/message/manager.php
Normal file
@ -0,0 +1,184 @@
|
||||
<?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/>.
|
||||
|
||||
/**
|
||||
* New messaging manager class.
|
||||
*
|
||||
* @package core_message
|
||||
* @since Moodle 2.8
|
||||
* @copyright 2014 Totara Learning Solutions Ltd {@link http://www.totaralms.com/}
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
* @author Petr Skoda <petr.skoda@totaralms.com>
|
||||
*/
|
||||
|
||||
namespace core\message;
|
||||
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
|
||||
/**
|
||||
* Class used for various messaging related stuff.
|
||||
*
|
||||
* Note: Do NOT use directly in your code, it is intended to be used from core code only.
|
||||
*
|
||||
* @access private
|
||||
*
|
||||
* @package core_message
|
||||
* @since Moodle 2.8
|
||||
* @copyright 2014 Totara Learning Solutions Ltd {@link http://www.totaralms.com/}
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
* @author Petr Skoda <petr.skoda@totaralms.com>
|
||||
*/
|
||||
class manager {
|
||||
/** @var array buffer of pending messages */
|
||||
protected static $buffer = array();
|
||||
|
||||
/**
|
||||
* Do the message sending.
|
||||
*
|
||||
* NOTE: to be used from message_send() only.
|
||||
*
|
||||
* @param \stdClass $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)
|
||||
*/
|
||||
public static function send_message(\stdClass $eventdata, \stdClass $savemessage, array $processorlist) {
|
||||
global $CFG;
|
||||
require_once($CFG->dirroot.'/message/lib.php'); // This is most probably already included from messagelib.php file.
|
||||
|
||||
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)->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;
|
||||
}
|
||||
|
||||
// Let the manager do the sending or buffering when db transaction in progress.
|
||||
return self::send_message_to_processors($eventdata, $savemessage, $processorlist);
|
||||
}
|
||||
|
||||
/**
|
||||
* Send message to message processors.
|
||||
*
|
||||
* @param \stdClass $eventdata
|
||||
* @param \stdClass $savemessage
|
||||
* @param array $processorlist
|
||||
* @return int $messageid
|
||||
*/
|
||||
protected static function send_message_to_processors(\stdClass $eventdata, \stdClass $savemessage, array $processorlist) {
|
||||
global $CFG, $DB;
|
||||
|
||||
// We cannot communicate with external systems in DB transactions,
|
||||
// buffer the messages if necessary.
|
||||
|
||||
if ($DB->is_transaction_started()) {
|
||||
// We need to clone all objects so that devs may not modify it from outside later.
|
||||
$eventdata = clone($eventdata);
|
||||
$eventdata->userto = clone($eventdata->userto);
|
||||
$eventdata->userfrom = clone($eventdata->userfrom);
|
||||
|
||||
// Conserve some memory the same was as $USER setup does.
|
||||
unset($eventdata->userto->description);
|
||||
unset($eventdata->userfrom->description);
|
||||
|
||||
self::$buffer[] = array($eventdata, $savemessage, $processorlist);
|
||||
return $savemessage->id;
|
||||
}
|
||||
|
||||
$processors = get_message_processors(true);
|
||||
|
||||
$failed = false;
|
||||
foreach ($processorlist as $procname) {
|
||||
if (!$processors[$procname]->object->send_message($eventdata)) {
|
||||
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)->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;
|
||||
}
|
||||
|
||||
/**
|
||||
* Notification from DML layer.
|
||||
*
|
||||
* Note: to be used from DML layer only.
|
||||
*/
|
||||
public static function database_transaction_commited() {
|
||||
if (!self::$buffer) {
|
||||
return;
|
||||
}
|
||||
self::process_buffer();
|
||||
}
|
||||
|
||||
/**
|
||||
* Notification from DML layer.
|
||||
*
|
||||
* Note: to be used from DML layer only.
|
||||
*/
|
||||
public static function database_transaction_rolledback() {
|
||||
self::$buffer = array();
|
||||
}
|
||||
|
||||
/**
|
||||
* Sent out any buffered messages if necessary.
|
||||
*/
|
||||
protected static function process_buffer() {
|
||||
// Reset the buffer first in case we get exception from processor.
|
||||
$messages = self::$buffer;
|
||||
self::$buffer = array();
|
||||
|
||||
foreach ($messages as $message) {
|
||||
list($eventdata, $savemessage, $processorlist) = $message;
|
||||
self::send_message_to_processors($eventdata, $savemessage, $processorlist);
|
||||
}
|
||||
}
|
||||
}
|
@ -2386,6 +2386,7 @@ abstract class moodle_database {
|
||||
|
||||
if (empty($this->transactions)) {
|
||||
\core\event\manager::database_transaction_commited();
|
||||
\core\message\manager::database_transaction_commited();
|
||||
}
|
||||
}
|
||||
|
||||
@ -2433,6 +2434,7 @@ abstract class moodle_database {
|
||||
// finally top most level rolled back
|
||||
$this->force_rollback = false;
|
||||
\core\event\manager::database_transaction_rolledback();
|
||||
\core\message\manager::database_transaction_rolledback();
|
||||
}
|
||||
throw $e;
|
||||
}
|
||||
|
@ -47,9 +47,12 @@ require_once(dirname(dirname(__FILE__)) . '/message/lib.php');
|
||||
* contexturl string if this is a notification then you can specify a url to view the event. For example the forum post the user is being notified of.
|
||||
* contexturlname string the display text for contexturl
|
||||
*
|
||||
* Note: processor failure is is not reported as false return value,
|
||||
* earlier versions did not do it consistently either.
|
||||
*
|
||||
* @category message
|
||||
* @param object $eventdata information about the message (component, userfrom, userto, ...)
|
||||
* @return mixed the integer ID of the new message or false if there was a problem with a processor
|
||||
* @param stdClass $eventdata information about the message (component, userfrom, userto, ...)
|
||||
* @return mixed the integer ID of the new message or false if there was a problem with submitted data
|
||||
*/
|
||||
function message_send($eventdata) {
|
||||
global $CFG, $DB;
|
||||
@ -65,20 +68,33 @@ function message_send($eventdata) {
|
||||
return $messageid;
|
||||
}
|
||||
|
||||
//TODO: we need to solve problems with database transactions here somehow, for now we just prevent transactions - sorry
|
||||
$DB->transactions_forbidden();
|
||||
|
||||
// By default a message is a notification. Only personal/private messages aren't notifications.
|
||||
if (!isset($eventdata->notification)) {
|
||||
$eventdata->notification = 1;
|
||||
}
|
||||
|
||||
if (is_number($eventdata->userto)) {
|
||||
if (!is_object($eventdata->userto)) {
|
||||
$eventdata->userto = core_user::get_user($eventdata->userto);
|
||||
}
|
||||
if (is_int($eventdata->userfrom)) {
|
||||
if (!is_object($eventdata->userfrom)) {
|
||||
$eventdata->userfrom = core_user::get_user($eventdata->userfrom);
|
||||
}
|
||||
if (!$eventdata->userto) {
|
||||
debugging('Attempt to send msg to unknown user', DEBUG_NORMAL);
|
||||
return false;
|
||||
}
|
||||
if (!$eventdata->userfrom) {
|
||||
debugging('Attempt to send msg from unknown user', DEBUG_NORMAL);
|
||||
return false;
|
||||
}
|
||||
|
||||
// Verify all necessary data fields are present.
|
||||
if (!isset($eventdata->userto->auth) or !isset($eventdata->userto->suspended)
|
||||
or !isset($eventdata->userto->deleted) or !isset($eventdata->userto->emailstop)) {
|
||||
|
||||
debugging('Necessary properties missing in userto object, fetching full record', DEBUG_DEVELOPER);
|
||||
$eventdata->userto = core_user::get_user($eventdata->userto->id);
|
||||
}
|
||||
|
||||
$usertoisrealuser = (core_user::is_real_user($eventdata->userto->id) != false);
|
||||
// If recipient is internal user (noreply user), and emailstop is set then don't send any msg.
|
||||
@ -87,10 +103,6 @@ function message_send($eventdata) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!isset($eventdata->userto->auth) or !isset($eventdata->userto->suspended) or !isset($eventdata->userto->deleted)) {
|
||||
$eventdata->userto = core_user::get_user($eventdata->userto->id);
|
||||
}
|
||||
|
||||
//after how long inactive should the user be considered logged off?
|
||||
if (isset($CFG->block_online_users_timetosee)) {
|
||||
$timetoshowusers = $CFG->block_online_users_timetosee * 60;
|
||||
@ -117,13 +129,13 @@ function message_send($eventdata) {
|
||||
$savemessage->notification = $eventdata->notification;
|
||||
|
||||
if (!empty($eventdata->contexturl)) {
|
||||
$savemessage->contexturl = $eventdata->contexturl;
|
||||
$savemessage->contexturl = (string)$eventdata->contexturl;
|
||||
} else {
|
||||
$savemessage->contexturl = null;
|
||||
}
|
||||
|
||||
if (!empty($eventdata->contexturlname)) {
|
||||
$savemessage->contexturlname = $eventdata->contexturlname;
|
||||
$savemessage->contexturlname = (string)$eventdata->contexturlname;
|
||||
} else {
|
||||
$savemessage->contexturlname = null;
|
||||
}
|
||||
@ -189,12 +201,6 @@ function message_send($eventdata) {
|
||||
debugging('Attempt to force message delivery to user who has "'.$processor->name.'" output unconfigured', DEBUG_NORMAL);
|
||||
}
|
||||
|
||||
// Warn developers that necessary data is missing regardless of how the processors are configured
|
||||
if (!isset($eventdata->userto->emailstop)) {
|
||||
debugging('userto->emailstop is not set. Retrieving it from the user table');
|
||||
$eventdata->userto->emailstop = $DB->get_field('user', 'emailstop', array('id'=>$eventdata->userto->id));
|
||||
}
|
||||
|
||||
// Populate the list of processors we will be using
|
||||
if ($permitted == 'forced' && $userisconfigured) {
|
||||
// An admin is forcing users to use this message processor. Use this processor unconditionally.
|
||||
@ -203,7 +209,7 @@ function message_send($eventdata) {
|
||||
// User has not disabled notifications
|
||||
// See if user set any notification preferences, otherwise use site default ones
|
||||
$userpreferencename = 'message_provider_'.$preferencebase.'_'.$userstate;
|
||||
if ($userpreference = get_user_preferences($userpreferencename, null, $eventdata->userto->id)) {
|
||||
if ($userpreference = get_user_preferences($userpreferencename, null, $eventdata->userto)) {
|
||||
if (in_array($processor->name, explode(',', $userpreference))) {
|
||||
$processorlist[] = $processor->name;
|
||||
}
|
||||
@ -215,60 +221,12 @@ function message_send($eventdata) {
|
||||
}
|
||||
}
|
||||
|
||||
if (empty($processorlist) && $savemessage->notification) {
|
||||
//if they have deselected all processors and its a notification mark it read. The user doesnt want to be bothered
|
||||
$savemessage->timeread = time();
|
||||
$messageid = $DB->insert_record('message_read', $savemessage);
|
||||
} else { // Process the message
|
||||
// Store unread message just in case we can not send it
|
||||
$messageid = $savemessage->id = $DB->insert_record('message', $savemessage);
|
||||
$eventdata->savedmessageid = $savemessage->id;
|
||||
// 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;
|
||||
|
||||
// Try to deliver the message to each processor
|
||||
if (!empty($processorlist)) {
|
||||
foreach ($processorlist as $procname) {
|
||||
if (!$processors[$procname]->object->send_message($eventdata)) {
|
||||
debugging('Error calling message processor '.$procname);
|
||||
$messageid = false;
|
||||
}
|
||||
}
|
||||
|
||||
//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)) {
|
||||
require_once($CFG->dirroot.'/message/lib.php');
|
||||
$messageid = message_mark_message_read($savemessage, time());
|
||||
} 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
|
||||
require_once($CFG->dirroot.'/message/lib.php');
|
||||
$messageid = message_mark_message_read($savemessage, time(), true);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// We may be sending a message from the 'noreply' address, which means we are not actually sending a
|
||||
// message from a valid user. In this case, we will set the userid to 0.
|
||||
// Check if the userid is valid.
|
||||
if (core_user::is_real_user($eventdata->userfrom->id)) {
|
||||
$userfromid = $eventdata->userfrom->id;
|
||||
} else {
|
||||
$userfromid = 0;
|
||||
}
|
||||
|
||||
// Trigger event for sending a message.
|
||||
$event = \core\event\message_sent::create(array(
|
||||
'userid' => $userfromid,
|
||||
'context' => context_system::instance(),
|
||||
'relateduserid' => $eventdata->userto->id,
|
||||
'other' => array(
|
||||
'messageid' => $messageid // Can't use this as the objectid as it can either be the id in the 'message_read'
|
||||
// or 'message' table.
|
||||
)
|
||||
));
|
||||
$event->trigger();
|
||||
|
||||
return $messageid;
|
||||
// Let the manager do the sending or buffering when db transaction in progress.
|
||||
return \core\message\manager::send_message($eventdata, $savemessage, $processorlist);
|
||||
}
|
||||
|
||||
|
||||
|
@ -183,6 +183,535 @@ class core_messagelib_testcase extends advanced_testcase {
|
||||
// $this->assertFalse($this->message_type_present('moodle', 'backup', $providers));
|
||||
}
|
||||
|
||||
public function test_send_message_redirection() {
|
||||
global $DB;
|
||||
|
||||
$this->resetAfterTest();
|
||||
|
||||
$user1 = $this->getDataGenerator()->create_user();
|
||||
$user2 = $this->getDataGenerator()->create_user();
|
||||
|
||||
// Test basic message redirection.
|
||||
$message = new stdClass();
|
||||
$message->component = 'moodle';
|
||||
$message->name = 'instantmessage';
|
||||
$message->userfrom = $user1;
|
||||
$message->userto = $user2;
|
||||
$message->subject = 'message subject 1';
|
||||
$message->fullmessage = 'message body';
|
||||
$message->fullmessageformat = FORMAT_MARKDOWN;
|
||||
$message->fullmessagehtml = '<p>message body</p>';
|
||||
$message->smallmessage = 'small message';
|
||||
$message->notification = '0';
|
||||
|
||||
$sink = $this->redirectMessages();
|
||||
$this->setCurrentTimeStart();
|
||||
$messageid = message_send($message);
|
||||
$savedmessages = $sink->get_messages();
|
||||
$this->assertCount(1, $savedmessages);
|
||||
$savedmessage = reset($savedmessages);
|
||||
$this->assertEquals($messageid, $savedmessage->id);
|
||||
$this->assertEquals($user1->id, $savedmessage->useridfrom);
|
||||
$this->assertEquals($user2->id, $savedmessage->useridto);
|
||||
$this->assertEquals($message->fullmessage, $savedmessage->fullmessage);
|
||||
$this->assertEquals($message->fullmessageformat, $savedmessage->fullmessageformat);
|
||||
$this->assertEquals($message->fullmessagehtml, $savedmessage->fullmessagehtml);
|
||||
$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);
|
||||
$this->assertEquals($record, $savedmessage);
|
||||
$sink->clear();
|
||||
$this->assertFalse($DB->record_exists('message', array()));
|
||||
$DB->delete_records('message_read', array());
|
||||
|
||||
$message = new stdClass();
|
||||
$message->component = 'moodle';
|
||||
$message->name = 'instantmessage';
|
||||
$message->userfrom = $user1->id;
|
||||
$message->userto = $user2->id;
|
||||
$message->subject = 'message subject 1';
|
||||
$message->fullmessage = 'message body';
|
||||
$message->fullmessageformat = FORMAT_MARKDOWN;
|
||||
$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();
|
||||
$this->assertCount(1, $savedmessages);
|
||||
$savedmessage = reset($savedmessages);
|
||||
$this->assertEquals($messageid, $savedmessage->id);
|
||||
$this->assertEquals($user1->id, $savedmessage->useridfrom);
|
||||
$this->assertEquals($user2->id, $savedmessage->useridto);
|
||||
$this->assertEquals($message->fullmessage, $savedmessage->fullmessage);
|
||||
$this->assertEquals($message->fullmessageformat, $savedmessage->fullmessageformat);
|
||||
$this->assertEquals($message->fullmessagehtml, $savedmessage->fullmessagehtml);
|
||||
$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);
|
||||
$this->assertEquals($record, $savedmessage);
|
||||
$sink->clear();
|
||||
$this->assertFalse($DB->record_exists('message', array()));
|
||||
$DB->delete_records('message_read', array());
|
||||
|
||||
// Test phpunit problem detection.
|
||||
|
||||
$message = new stdClass();
|
||||
$message->component = 'xxxxx';
|
||||
$message->name = 'instantmessage';
|
||||
$message->userfrom = $user1;
|
||||
$message->userto = $user2;
|
||||
$message->subject = 'message subject 1';
|
||||
$message->fullmessage = 'message body';
|
||||
$message->fullmessageformat = FORMAT_MARKDOWN;
|
||||
$message->fullmessagehtml = '<p>message body</p>';
|
||||
$message->smallmessage = 'small message';
|
||||
$message->notification = '0';
|
||||
|
||||
$sink = $this->redirectMessages();
|
||||
try {
|
||||
message_send($message);
|
||||
} catch (moodle_exception $e) {
|
||||
$this->assertInstanceOf('coding_exception', $e);
|
||||
}
|
||||
$this->assertCount(0, $sink->get_messages());
|
||||
|
||||
$message->component = 'moodle';
|
||||
$message->name = 'xxx';
|
||||
$sink = $this->redirectMessages();
|
||||
try {
|
||||
message_send($message);
|
||||
} catch (moodle_exception $e) {
|
||||
$this->assertInstanceOf('coding_exception', $e);
|
||||
}
|
||||
$this->assertCount(0, $sink->get_messages());
|
||||
$sink->close();
|
||||
$this->assertFalse($DB->record_exists('message', array()));
|
||||
$this->assertFalse($DB->record_exists('message_read', array()));
|
||||
|
||||
// Invalid users.
|
||||
|
||||
$message = new stdClass();
|
||||
$message->component = 'moodle';
|
||||
$message->name = 'instantmessage';
|
||||
$message->userfrom = $user1;
|
||||
$message->userto = -1;
|
||||
$message->subject = 'message subject 1';
|
||||
$message->fullmessage = 'message body';
|
||||
$message->fullmessageformat = FORMAT_MARKDOWN;
|
||||
$message->fullmessagehtml = '<p>message body</p>';
|
||||
$message->smallmessage = 'small message';
|
||||
$message->notification = '0';
|
||||
|
||||
$messageid = message_send($message);
|
||||
$this->assertFalse($messageid);
|
||||
$this->assertDebuggingCalled('Attempt to send msg to unknown user');
|
||||
|
||||
$message = new stdClass();
|
||||
$message->component = 'moodle';
|
||||
$message->name = 'instantmessage';
|
||||
$message->userfrom = -1;
|
||||
$message->userto = $user2;
|
||||
$message->subject = 'message subject 1';
|
||||
$message->fullmessage = 'message body';
|
||||
$message->fullmessageformat = FORMAT_MARKDOWN;
|
||||
$message->fullmessagehtml = '<p>message body</p>';
|
||||
$message->smallmessage = 'small message';
|
||||
$message->notification = '0';
|
||||
|
||||
$messageid = message_send($message);
|
||||
$this->assertFalse($messageid);
|
||||
$this->assertDebuggingCalled('Attempt to send msg from unknown user');
|
||||
|
||||
$message = new stdClass();
|
||||
$message->component = 'moodle';
|
||||
$message->name = 'instantmessage';
|
||||
$message->userfrom = $user1;
|
||||
$message->userto = core_user::NOREPLY_USER;
|
||||
$message->subject = 'message subject 1';
|
||||
$message->fullmessage = 'message body';
|
||||
$message->fullmessageformat = FORMAT_MARKDOWN;
|
||||
$message->fullmessagehtml = '<p>message body</p>';
|
||||
$message->smallmessage = 'small message';
|
||||
$message->notification = '0';
|
||||
|
||||
$messageid = message_send($message);
|
||||
$this->assertFalse($messageid);
|
||||
$this->assertDebuggingCalled('Attempt to send msg to internal (noreply) user');
|
||||
|
||||
// Some debugging hints for devs.
|
||||
|
||||
unset($user2->emailstop);
|
||||
$message = new stdClass();
|
||||
$message->component = 'moodle';
|
||||
$message->name = 'instantmessage';
|
||||
$message->userfrom = $user1;
|
||||
$message->userto = $user2;
|
||||
$message->subject = 'message subject 1';
|
||||
$message->fullmessage = 'message body';
|
||||
$message->fullmessageformat = FORMAT_MARKDOWN;
|
||||
$message->fullmessagehtml = '<p>message body</p>';
|
||||
$message->smallmessage = 'small message';
|
||||
$message->notification = '0';
|
||||
|
||||
$sink = $this->redirectMessages();
|
||||
$messageid = message_send($message);
|
||||
$savedmessages = $sink->get_messages();
|
||||
$this->assertCount(1, $savedmessages);
|
||||
$savedmessage = reset($savedmessages);
|
||||
$this->assertEquals($messageid, $savedmessage->id);
|
||||
$this->assertEquals($user1->id, $savedmessage->useridfrom);
|
||||
$this->assertEquals($user2->id, $savedmessage->useridto);
|
||||
$this->assertDebuggingCalled('Necessary properties missing in userto object, fetching full record');
|
||||
$sink->clear();
|
||||
$user2->emailstop = '0';
|
||||
}
|
||||
|
||||
public function test_send_message() {
|
||||
global $DB, $CFG;
|
||||
$this->preventResetByRollback();
|
||||
$this->resetAfterTest();
|
||||
|
||||
unset_config('noemailever');
|
||||
|
||||
$user1 = $this->getDataGenerator()->create_user();
|
||||
$user2 = $this->getDataGenerator()->create_user();
|
||||
|
||||
// Test basic email redirection.
|
||||
$this->assertFileExists("$CFG->dirroot/message/output/email/version.php");
|
||||
$this->assertFileExists("$CFG->dirroot/message/output/popup/version.php");
|
||||
|
||||
$DB->set_field_select('message_processors', 'enabled', 0, "name <> 'email' AND name <> 'popup'");
|
||||
get_message_processors(true, true);
|
||||
|
||||
$eventsink = $this->redirectEvents();
|
||||
|
||||
set_user_preference('message_provider_moodle_instantmessage_loggedoff', 'none', $user2);
|
||||
|
||||
$message = new stdClass();
|
||||
$message->component = 'moodle';
|
||||
$message->name = 'instantmessage';
|
||||
$message->userfrom = $user1;
|
||||
$message->userto = $user2;
|
||||
$message->subject = 'message subject 1';
|
||||
$message->fullmessage = 'message body';
|
||||
$message->fullmessageformat = FORMAT_MARKDOWN;
|
||||
$message->fullmessagehtml = '<p>message body</p>';
|
||||
$message->smallmessage = 'small message';
|
||||
$message->notification = '0';
|
||||
|
||||
$sink = $this->redirectEmails();
|
||||
$messageid = message_send($message);
|
||||
$emails = $sink->get_messages();
|
||||
$this->assertCount(0, $emails);
|
||||
$savedmessage = $DB->get_record('message', array('id' => $messageid), '*', MUST_EXIST);
|
||||
$sink->clear();
|
||||
$this->assertFalse($DB->record_exists('message_read', array()));
|
||||
$DB->delete_records('message', array());
|
||||
$events = $eventsink->get_events();
|
||||
$this->assertCount(1, $events);
|
||||
$this->assertInstanceOf('\core\event\message_sent', $events[0]);
|
||||
$eventsink->clear();
|
||||
|
||||
$CFG->messaging = 0;
|
||||
|
||||
$message = new stdClass();
|
||||
$message->component = 'moodle';
|
||||
$message->name = 'instantmessage';
|
||||
$message->userfrom = $user1;
|
||||
$message->userto = $user2;
|
||||
$message->subject = 'message subject 1';
|
||||
$message->fullmessage = 'message body';
|
||||
$message->fullmessageformat = FORMAT_MARKDOWN;
|
||||
$message->fullmessagehtml = '<p>message body</p>';
|
||||
$message->smallmessage = 'small message';
|
||||
$message->notification = '0';
|
||||
|
||||
$messageid = message_send($message);
|
||||
$emails = $sink->get_messages();
|
||||
$this->assertCount(0, $emails);
|
||||
$savedmessage = $DB->get_record('message_read', array('id' => $messageid), '*', MUST_EXIST);
|
||||
$sink->clear();
|
||||
$this->assertFalse($DB->record_exists('message', array()));
|
||||
$DB->delete_records('message_read', 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]);
|
||||
$eventsink->clear();
|
||||
|
||||
$CFG->messaging = 1;
|
||||
|
||||
$message = new stdClass();
|
||||
$message->component = 'moodle';
|
||||
$message->name = 'instantmessage';
|
||||
$message->userfrom = $user1;
|
||||
$message->userto = $user2;
|
||||
$message->subject = 'message subject 1';
|
||||
$message->fullmessage = 'message body';
|
||||
$message->fullmessageformat = FORMAT_MARKDOWN;
|
||||
$message->fullmessagehtml = '<p>message body</p>';
|
||||
$message->smallmessage = 'small message';
|
||||
$message->notification = '1';
|
||||
|
||||
$messageid = message_send($message);
|
||||
$emails = $sink->get_messages();
|
||||
$this->assertCount(0, $emails);
|
||||
$savedmessage = $DB->get_record('message_read', array('id' => $messageid), '*', MUST_EXIST);
|
||||
$sink->clear();
|
||||
$this->assertFalse($DB->record_exists('message', array()));
|
||||
$DB->delete_records('message_read', 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]);
|
||||
$eventsink->clear();
|
||||
|
||||
set_user_preference('message_provider_moodle_instantmessage_loggedoff', 'email', $user2);
|
||||
|
||||
$message = new stdClass();
|
||||
$message->component = 'moodle';
|
||||
$message->name = 'instantmessage';
|
||||
$message->userfrom = $user1;
|
||||
$message->userto = $user2;
|
||||
$message->subject = 'message subject 1';
|
||||
$message->fullmessage = 'message body';
|
||||
$message->fullmessageformat = FORMAT_MARKDOWN;
|
||||
$message->fullmessagehtml = '<p>message body</p>';
|
||||
$message->smallmessage = 'small message';
|
||||
$message->notification = '0';
|
||||
|
||||
$user2->emailstop = '1';
|
||||
|
||||
$sink = $this->redirectEmails();
|
||||
$messageid = message_send($message);
|
||||
$emails = $sink->get_messages();
|
||||
$this->assertCount(0, $emails);
|
||||
$savedmessage = $DB->get_record('message', array('id' => $messageid), '*', MUST_EXIST);
|
||||
$sink->clear();
|
||||
$this->assertFalse($DB->record_exists('message_read', array()));
|
||||
$DB->delete_records('message', array());
|
||||
$events = $eventsink->get_events();
|
||||
$this->assertCount(1, $events);
|
||||
$this->assertInstanceOf('\core\event\message_sent', $events[0]);
|
||||
$eventsink->clear();
|
||||
$user2->emailstop = '0';
|
||||
|
||||
set_user_preference('message_provider_moodle_instantmessage_loggedoff', 'email', $user2);
|
||||
|
||||
$message = new stdClass();
|
||||
$message->component = 'moodle';
|
||||
$message->name = 'instantmessage';
|
||||
$message->userfrom = $user1;
|
||||
$message->userto = $user2;
|
||||
$message->subject = 'message subject 1';
|
||||
$message->fullmessage = 'message body';
|
||||
$message->fullmessageformat = FORMAT_MARKDOWN;
|
||||
$message->fullmessagehtml = '<p>message body</p>';
|
||||
$message->smallmessage = 'small message';
|
||||
$message->notification = '0';
|
||||
|
||||
$messageid = message_send($message);
|
||||
$emails = $sink->get_messages();
|
||||
$this->assertCount(1, $emails);
|
||||
$email = reset($emails);
|
||||
$savedmessage = $DB->get_record('message_read', 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', array()));
|
||||
$DB->delete_records('message_read', 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]);
|
||||
$eventsink->clear();
|
||||
|
||||
set_user_preference('message_provider_moodle_instantmessage_loggedoff', 'email,popup', $user2);
|
||||
|
||||
$message = new stdClass();
|
||||
$message->component = 'moodle';
|
||||
$message->name = 'instantmessage';
|
||||
$message->userfrom = $user1;
|
||||
$message->userto = $user2;
|
||||
$message->subject = 'message subject 1';
|
||||
$message->fullmessage = 'message body';
|
||||
$message->fullmessageformat = FORMAT_MARKDOWN;
|
||||
$message->fullmessagehtml = '<p>message body</p>';
|
||||
$message->smallmessage = 'small message';
|
||||
$message->notification = '0';
|
||||
|
||||
$messageid = message_send($message);
|
||||
$emails = $sink->get_messages();
|
||||
$this->assertCount(1, $emails);
|
||||
$email = reset($emails);
|
||||
$savedmessage = $DB->get_record('message', 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);
|
||||
$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', array());
|
||||
$events = $eventsink->get_events();
|
||||
$this->assertCount(1, $events);
|
||||
$this->assertInstanceOf('\core\event\message_sent', $events[0]);
|
||||
$eventsink->clear();
|
||||
|
||||
set_user_preference('message_provider_moodle_instantmessage_loggedoff', 'popup', $user2);
|
||||
|
||||
$message = new stdClass();
|
||||
$message->component = 'moodle';
|
||||
$message->name = 'instantmessage';
|
||||
$message->userfrom = $user1;
|
||||
$message->userto = $user2;
|
||||
$message->subject = 'message subject 1';
|
||||
$message->fullmessage = 'message body';
|
||||
$message->fullmessageformat = FORMAT_MARKDOWN;
|
||||
$message->fullmessagehtml = '<p>message body</p>';
|
||||
$message->smallmessage = 'small message';
|
||||
$message->notification = '0';
|
||||
|
||||
$messageid = message_send($message);
|
||||
$emails = $sink->get_messages();
|
||||
$this->assertCount(0, $emails);
|
||||
$savedmessage = $DB->get_record('message', 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());
|
||||
$events = $eventsink->get_events();
|
||||
$this->assertCount(1, $events);
|
||||
$this->assertInstanceOf('\core\event\message_sent', $events[0]);
|
||||
$eventsink->clear();
|
||||
|
||||
$this->assertFalse($DB->is_transaction_started());
|
||||
$transaction = $DB->start_delegated_transaction();
|
||||
if (!$DB->is_transaction_started()) {
|
||||
$this->markTestSkipped('Databases that do not support transactions should not be used at all!');
|
||||
}
|
||||
$transaction->allow_commit();
|
||||
|
||||
set_user_preference('message_provider_moodle_instantmessage_loggedoff', 'none', $user2);
|
||||
|
||||
$message = new stdClass();
|
||||
$message->component = 'moodle';
|
||||
$message->name = 'instantmessage';
|
||||
$message->userfrom = $user1;
|
||||
$message->userto = $user2;
|
||||
$message->subject = 'message subject 1';
|
||||
$message->fullmessage = 'message body';
|
||||
$message->fullmessageformat = FORMAT_MARKDOWN;
|
||||
$message->fullmessagehtml = '<p>message body</p>';
|
||||
$message->smallmessage = 'small message';
|
||||
$message->notification = '0';
|
||||
|
||||
$transaction = $DB->start_delegated_transaction();
|
||||
$sink = $this->redirectEmails();
|
||||
$messageid = message_send($message);
|
||||
$emails = $sink->get_messages();
|
||||
$this->assertCount(0, $emails);
|
||||
$savedmessage = $DB->get_record('message', array('id' => $messageid), '*', MUST_EXIST);
|
||||
$sink->clear();
|
||||
$this->assertFalse($DB->record_exists('message_read', array()));
|
||||
$DB->delete_records('message', array());
|
||||
$events = $eventsink->get_events();
|
||||
$this->assertCount(1, $events);
|
||||
$this->assertInstanceOf('\core\event\message_sent', $events[0]);
|
||||
$eventsink->clear();
|
||||
$transaction->allow_commit();
|
||||
$events = $eventsink->get_events();
|
||||
$this->assertCount(0, $events);
|
||||
|
||||
set_user_preference('message_provider_moodle_instantmessage_loggedoff', 'email', $user2);
|
||||
|
||||
$message = new stdClass();
|
||||
$message->component = 'moodle';
|
||||
$message->name = 'instantmessage';
|
||||
$message->userfrom = $user1;
|
||||
$message->userto = $user2;
|
||||
$message->subject = 'message subject 1';
|
||||
$message->fullmessage = 'message body';
|
||||
$message->fullmessageformat = FORMAT_MARKDOWN;
|
||||
$message->fullmessagehtml = '<p>message body</p>';
|
||||
$message->smallmessage = 'small message';
|
||||
$message->notification = '0';
|
||||
|
||||
$transaction = $DB->start_delegated_transaction();
|
||||
$sink = $this->redirectEmails();
|
||||
$messageid = message_send($message);
|
||||
$emails = $sink->get_messages();
|
||||
$this->assertCount(0, $emails);
|
||||
$savedmessage = $DB->get_record('message', array('id' => $messageid), '*', MUST_EXIST);
|
||||
$sink->clear();
|
||||
$this->assertFalse($DB->record_exists('message_read', array()));
|
||||
$events = $eventsink->get_events();
|
||||
$this->assertCount(0, $events);
|
||||
$transaction->allow_commit();
|
||||
$events = $eventsink->get_events();
|
||||
$this->assertCount(2, $events);
|
||||
$this->assertInstanceOf('\core\event\message_sent', $events[0]);
|
||||
$this->assertInstanceOf('\core\event\message_viewed', $events[1]);
|
||||
$eventsink->clear();
|
||||
|
||||
$transaction = $DB->start_delegated_transaction();
|
||||
message_send($message);
|
||||
message_send($message);
|
||||
$this->assertCount(2, $DB->get_records('message'));
|
||||
$this->assertCount(1, $DB->get_records('message_read'));
|
||||
$events = $eventsink->get_events();
|
||||
$this->assertCount(0, $events);
|
||||
$transaction->allow_commit();
|
||||
$events = $eventsink->get_events();
|
||||
$this->assertCount(4, $events);
|
||||
$this->assertInstanceOf('\core\event\message_sent', $events[0]);
|
||||
$this->assertInstanceOf('\core\event\message_viewed', $events[1]);
|
||||
$this->assertInstanceOf('\core\event\message_sent', $events[2]);
|
||||
$this->assertInstanceOf('\core\event\message_viewed', $events[3]);
|
||||
$eventsink->clear();
|
||||
$DB->delete_records('message', array());
|
||||
$DB->delete_records('message_read', 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'));
|
||||
$events = $eventsink->get_events();
|
||||
$this->assertCount(0, $events);
|
||||
try {
|
||||
$transaction->rollback(new Exception('ignore'));
|
||||
} catch (Exception $e) {
|
||||
$this->assertSame('ignore', $e->getMessage());
|
||||
}
|
||||
$events = $eventsink->get_events();
|
||||
$this->assertCount(0, $events);
|
||||
$this->assertCount(0, $DB->get_records('message'));
|
||||
$this->assertCount(0, $DB->get_records('message_read'));
|
||||
message_send($message);
|
||||
$this->assertCount(0, $DB->get_records('message'));
|
||||
$this->assertCount(1, $DB->get_records('message_read'));
|
||||
$events = $eventsink->get_events();
|
||||
$this->assertCount(2, $events);
|
||||
$sink->clear();
|
||||
$DB->delete_records('message_read', array());
|
||||
}
|
||||
|
||||
public function test_message_attachment_send() {
|
||||
global $CFG;
|
||||
$this->preventResetByRollback();
|
||||
|
Loading…
x
Reference in New Issue
Block a user