Merge branch 'MDL-63670-403' of https://github.com/andrewnicols/moodle into MOODLE_403_STABLE
This commit is contained in:
@@ -24,7 +24,7 @@
|
||||
|
||||
namespace core_message\tests;
|
||||
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
use stdClass;
|
||||
|
||||
/**
|
||||
* The helper class providing util methods for testing.
|
||||
@@ -33,6 +33,80 @@ defined('MOODLE_INTERNAL') || die();
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
class helper {
|
||||
/**
|
||||
* Send a fake message.
|
||||
*
|
||||
* {@link message_send()} does not support transaction, this function will simulate a message
|
||||
* sent from a user to another. We should stop using it once {@link message_send()} will support
|
||||
* transactions. This is not clean at all, this is just used to add rows to the table.
|
||||
*
|
||||
* @param \stdClass $userfrom user object of the one sending the message.
|
||||
* @param \stdClass $userto user object of the one receiving the message.
|
||||
* @param string $message message to send.
|
||||
* @param int $notification if the message is a notification.
|
||||
* @param int $time the time the message was sent
|
||||
* @return int the id of the message
|
||||
*/
|
||||
public static function send_fake_message(
|
||||
stdClass $userfrom,
|
||||
stdClass $userto,
|
||||
string $message = 'Hello world!',
|
||||
int $notification = 0,
|
||||
int $time = 0,
|
||||
): int {
|
||||
global $DB;
|
||||
|
||||
if (empty($time)) {
|
||||
$time = time();
|
||||
}
|
||||
|
||||
if ($notification) {
|
||||
$record = new \stdClass();
|
||||
$record->useridfrom = $userfrom->id;
|
||||
$record->useridto = $userto->id;
|
||||
$record->subject = 'No subject';
|
||||
$record->fullmessage = $message;
|
||||
$record->smallmessage = $message;
|
||||
$record->timecreated = $time;
|
||||
|
||||
return $DB->insert_record('notifications', $record);
|
||||
}
|
||||
|
||||
if ($userfrom->id == $userto->id) {
|
||||
// It's a self conversation.
|
||||
$conversation = \core_message\api::get_self_conversation($userfrom->id);
|
||||
if (empty($conversation)) {
|
||||
$conversation = \core_message\api::create_conversation(
|
||||
\core_message\api::MESSAGE_CONVERSATION_TYPE_SELF,
|
||||
[$userfrom->id],
|
||||
);
|
||||
}
|
||||
$conversationid = $conversation->id;
|
||||
} else if (!$conversationid = \core_message\api::get_conversation_between_users([$userfrom->id, $userto->id])) {
|
||||
// It's an individual conversation between two different users.
|
||||
$conversation = \core_message\api::create_conversation(
|
||||
\core_message\api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL,
|
||||
[
|
||||
$userfrom->id,
|
||||
$userto->id,
|
||||
]
|
||||
);
|
||||
$conversationid = $conversation->id;
|
||||
}
|
||||
|
||||
// Ok, send the message.
|
||||
$record = (object) [
|
||||
'useridfrom' => $userfrom->id,
|
||||
'conversationid' => $conversationid,
|
||||
'subject' => 'No subject',
|
||||
'fullmessage' => $message,
|
||||
'smallmessage' => $message,
|
||||
'timecreated' => $time,
|
||||
];
|
||||
|
||||
return $DB->insert_record('messages', $record);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sends a message to a conversation.
|
||||
*
|
||||
|
||||
+450
-224
File diff suppressed because it is too large
Load Diff
@@ -25,11 +25,7 @@
|
||||
|
||||
namespace core_message\event;
|
||||
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
|
||||
global $CFG;
|
||||
|
||||
require_once($CFG->dirroot . '/message/tests/messagelib_test.php');
|
||||
use core_message\tests\helper as testhelper;
|
||||
|
||||
/**
|
||||
* Class containing the tests for message related events.
|
||||
@@ -39,23 +35,15 @@ require_once($CFG->dirroot . '/message/tests/messagelib_test.php');
|
||||
* @copyright 2014 Mark Nelson <[email protected]>
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
class events_test extends \core_message\messagelib_test {
|
||||
|
||||
/**
|
||||
* Test set up.
|
||||
*
|
||||
* This is executed before running any test in this file.
|
||||
*/
|
||||
public function setUp(): void {
|
||||
$this->resetAfterTest();
|
||||
}
|
||||
|
||||
final class events_test extends \advanced_testcase {
|
||||
/**
|
||||
* Test the message contact added event.
|
||||
*/
|
||||
public function test_message_contact_added() {
|
||||
global $USER;
|
||||
|
||||
$this->resetAfterTest();
|
||||
|
||||
// Set this user as the admin.
|
||||
$this->setAdminUser();
|
||||
|
||||
@@ -81,6 +69,8 @@ class events_test extends \core_message\messagelib_test {
|
||||
public function test_message_contact_removed() {
|
||||
global $USER;
|
||||
|
||||
$this->resetAfterTest();
|
||||
|
||||
// Set this user as the admin.
|
||||
$this->setAdminUser();
|
||||
|
||||
@@ -109,6 +99,8 @@ class events_test extends \core_message\messagelib_test {
|
||||
public function test_message_user_blocked() {
|
||||
global $USER;
|
||||
|
||||
$this->resetAfterTest();
|
||||
|
||||
// Set this user as the admin.
|
||||
$this->setAdminUser();
|
||||
|
||||
@@ -135,6 +127,8 @@ class events_test extends \core_message\messagelib_test {
|
||||
public function test_message_user_unblocked() {
|
||||
global $USER;
|
||||
|
||||
$this->resetAfterTest();
|
||||
|
||||
// Set this user as the admin.
|
||||
$this->setAdminUser();
|
||||
|
||||
@@ -195,8 +189,7 @@ class events_test extends \core_message\messagelib_test {
|
||||
$this->assertEquals(4, $event->other['courseid']);
|
||||
}
|
||||
|
||||
public function test_mesage_sent_without_other_courseid() {
|
||||
|
||||
public function test_mesage_sent_without_other_courseid(): void {
|
||||
// Creating a message_sent event without other[courseid] leads to exception.
|
||||
$this->expectException('coding_exception');
|
||||
$this->expectExceptionMessage('The \'courseid\' value must be set in other');
|
||||
@@ -211,7 +204,9 @@ class events_test extends \core_message\messagelib_test {
|
||||
));
|
||||
}
|
||||
|
||||
public function test_mesage_sent_via_create_from_ids() {
|
||||
public function test_mesage_sent_via_create_from_ids(): void {
|
||||
$this->resetAfterTest();
|
||||
|
||||
// Containing courseid.
|
||||
$event = \core\event\message_sent::create_from_ids(1, 2, 3, 4);
|
||||
|
||||
@@ -237,7 +232,9 @@ class events_test extends \core_message\messagelib_test {
|
||||
* resulting in fake messages being generated and captured under test. As a result, none of the events code, nor message
|
||||
* processor code is called during testing.
|
||||
*/
|
||||
public function test_group_message_sent() {
|
||||
public function test_group_message_sent(): void {
|
||||
$this->resetAfterTest();
|
||||
|
||||
$event = \core\event\group_message_sent::create([
|
||||
'objectid' => 3,
|
||||
'userid' => 1,
|
||||
@@ -305,7 +302,9 @@ class events_test extends \core_message\messagelib_test {
|
||||
/**
|
||||
* Test the group message sent event using the create_from_ids() method.
|
||||
*/
|
||||
public function test_group_message_sent_via_create_from_ids() {
|
||||
public function test_group_message_sent_via_create_from_ids(): void {
|
||||
$this->resetAfterTest();
|
||||
|
||||
// Fields are: userfromid, conversationid, messageid, courseid.
|
||||
$event = \core\event\group_message_sent::create_from_ids(1, 2, 3, 4);
|
||||
|
||||
@@ -331,11 +330,13 @@ class events_test extends \core_message\messagelib_test {
|
||||
public function test_message_viewed() {
|
||||
global $DB;
|
||||
|
||||
$this->resetAfterTest();
|
||||
|
||||
// Create users to send messages between.
|
||||
$user1 = $this->getDataGenerator()->create_user();
|
||||
$user2 = $this->getDataGenerator()->create_user();
|
||||
|
||||
$messageid = $this->send_fake_message($user1, $user2);
|
||||
$messageid = testhelper::send_fake_message($user1, $user2);
|
||||
|
||||
// Trigger and capture the event.
|
||||
$sink = $this->redirectEvents();
|
||||
@@ -363,13 +364,15 @@ class events_test extends \core_message\messagelib_test {
|
||||
public function test_message_deleted() {
|
||||
global $DB, $USER;
|
||||
|
||||
$this->resetAfterTest();
|
||||
|
||||
$this->setAdminUser();
|
||||
|
||||
// Create users to send messages between.
|
||||
$user1 = $this->getDataGenerator()->create_user();
|
||||
$user2 = $this->getDataGenerator()->create_user();
|
||||
|
||||
$messageid = $this->send_fake_message($user1, $user2);
|
||||
$messageid = testhelper::send_fake_message($user1, $user2);
|
||||
|
||||
// Trigger and capture the event.
|
||||
$sink = $this->redirectEvents();
|
||||
@@ -391,7 +394,7 @@ class events_test extends \core_message\messagelib_test {
|
||||
$this->setUser($user1);
|
||||
|
||||
// Create a read message.
|
||||
$messageid = $this->send_fake_message($user1, $user2);
|
||||
$messageid = testhelper::send_fake_message($user1, $user2);
|
||||
$m = $DB->get_record('messages', ['id' => $messageid]);
|
||||
\core_message\api::mark_message_as_read($user2->id, $m);
|
||||
|
||||
@@ -419,6 +422,8 @@ class events_test extends \core_message\messagelib_test {
|
||||
public function test_message_deleted_whole_conversation() {
|
||||
global $DB;
|
||||
|
||||
$this->resetAfterTest();
|
||||
|
||||
// Create some users.
|
||||
$user1 = self::getDataGenerator()->create_user();
|
||||
$user2 = self::getDataGenerator()->create_user();
|
||||
@@ -429,14 +434,14 @@ class events_test extends \core_message\messagelib_test {
|
||||
// Send some messages back and forth.
|
||||
$time = 1;
|
||||
$messages = [];
|
||||
$messages[] = $this->send_fake_message($user1, $user2, 'Yo!', 0, $time + 1);
|
||||
$messages[] = $this->send_fake_message($user2, $user1, 'Sup mang?', 0, $time + 2);
|
||||
$messages[] = $this->send_fake_message($user1, $user2, 'Writing PHPUnit tests!', 0, $time + 3);
|
||||
$messages[] = $this->send_fake_message($user2, $user1, 'Word.', 0, $time + 4);
|
||||
$messages[] = $this->send_fake_message($user1, $user2, 'You doing much?', 0, $time + 5);
|
||||
$messages[] = $this->send_fake_message($user2, $user1, 'Nah', 0, $time + 6);
|
||||
$messages[] = $this->send_fake_message($user1, $user2, 'You nubz0r!', 0, $time + 7);
|
||||
$messages[] = $this->send_fake_message($user2, $user1, 'Ouch.', 0, $time + 8);
|
||||
$messages[] = testhelper::send_fake_message($user1, $user2, 'Yo!', 0, $time + 1);
|
||||
$messages[] = testhelper::send_fake_message($user2, $user1, 'Sup mang?', 0, $time + 2);
|
||||
$messages[] = testhelper::send_fake_message($user1, $user2, 'Writing PHPUnit tests!', 0, $time + 3);
|
||||
$messages[] = testhelper::send_fake_message($user2, $user1, 'Word.', 0, $time + 4);
|
||||
$messages[] = testhelper::send_fake_message($user1, $user2, 'You doing much?', 0, $time + 5);
|
||||
$messages[] = testhelper::send_fake_message($user2, $user1, 'Nah', 0, $time + 6);
|
||||
$messages[] = testhelper::send_fake_message($user1, $user2, 'You nubz0r!', 0, $time + 7);
|
||||
$messages[] = testhelper::send_fake_message($user2, $user1, 'Ouch.', 0, $time + 8);
|
||||
|
||||
// Mark the last 4 messages as read.
|
||||
$m5 = $DB->get_record('messages', ['id' => $messages[4]]);
|
||||
@@ -487,7 +492,9 @@ class events_test extends \core_message\messagelib_test {
|
||||
/**
|
||||
* Test the notification sent event.
|
||||
*/
|
||||
public function test_notification_sent() {
|
||||
public function test_notification_sent(): void {
|
||||
$this->resetAfterTest();
|
||||
|
||||
// Create a course.
|
||||
$course = $this->getDataGenerator()->create_course();
|
||||
|
||||
@@ -496,7 +503,7 @@ class events_test extends \core_message\messagelib_test {
|
||||
$user2 = $this->getDataGenerator()->create_user();
|
||||
|
||||
// Send a notification.
|
||||
$notificationid = $this->send_fake_message($user1, $user2, 'Hello world!', 1);
|
||||
$notificationid = testhelper::send_fake_message($user1, $user2, 'Hello world!', 1);
|
||||
|
||||
// Containing courseid.
|
||||
$event = \core\event\notification_sent::create_from_ids($user1->id, $user2->id, $notificationid, $course->id);
|
||||
@@ -524,6 +531,8 @@ class events_test extends \core_message\messagelib_test {
|
||||
public function test_notification_sent_with_null_course() {
|
||||
$event = \core\event\notification_sent::create_from_ids(1, 1, 1, null);
|
||||
|
||||
$this->resetAfterTest();
|
||||
|
||||
// Trigger and capture the event.
|
||||
$sink = $this->redirectEvents();
|
||||
$event->trigger();
|
||||
@@ -541,12 +550,14 @@ class events_test extends \core_message\messagelib_test {
|
||||
public function test_notification_viewed() {
|
||||
global $DB;
|
||||
|
||||
$this->resetAfterTest();
|
||||
|
||||
// Create users to send notifications between.
|
||||
$user1 = $this->getDataGenerator()->create_user();
|
||||
$user2 = $this->getDataGenerator()->create_user();
|
||||
|
||||
// Send a notification.
|
||||
$notificationid = $this->send_fake_message($user1, $user2, 'Hello world!', 1);
|
||||
$notificationid = testhelper::send_fake_message($user1, $user2, 'Hello world!', 1);
|
||||
|
||||
// Trigger and capture the event.
|
||||
$sink = $this->redirectEvents();
|
||||
|
||||
@@ -18,11 +18,6 @@ namespace core_message;
|
||||
|
||||
use core_message\tests\helper as testhelper;
|
||||
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
|
||||
global $CFG;
|
||||
require_once($CFG->dirroot . '/message/lib.php');
|
||||
|
||||
/**
|
||||
* Test api's in message lib.
|
||||
*
|
||||
@@ -31,87 +26,12 @@ require_once($CFG->dirroot . '/message/lib.php');
|
||||
* @copyright 2014 Rajesh Taneja <[email protected]>
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
class messagelib_test extends \advanced_testcase {
|
||||
final class messagelib_test extends \advanced_testcase {
|
||||
public static function setUpBeforeClass(): void {
|
||||
global $CFG;
|
||||
require_once($CFG->dirroot . '/message/lib.php');
|
||||
|
||||
/** @var phpunit_message_sink keep track of messages. */
|
||||
protected $messagesink = null;
|
||||
|
||||
/**
|
||||
* Test set up.
|
||||
*
|
||||
* This is executed before running any test in this file.
|
||||
*/
|
||||
public function setUp(): void {
|
||||
$this->preventResetByRollback(); // Messaging is not compatible with transactions.
|
||||
$this->messagesink = $this->redirectMessages();
|
||||
$this->resetAfterTest();
|
||||
}
|
||||
|
||||
/**
|
||||
* Send a fake message.
|
||||
*
|
||||
* {@link message_send()} does not support transaction, this function will simulate a message
|
||||
* sent from a user to another. We should stop using it once {@link message_send()} will support
|
||||
* transactions. This is not clean at all, this is just used to add rows to the table.
|
||||
*
|
||||
* @param \stdClass $userfrom user object of the one sending the message.
|
||||
* @param \stdClass $userto user object of the one receiving the message.
|
||||
* @param string $message message to send.
|
||||
* @param int $notification if the message is a notification.
|
||||
* @param int $time the time the message was sent
|
||||
* @return int the id of the message
|
||||
*/
|
||||
protected function send_fake_message($userfrom, $userto, $message = 'Hello world!', $notification = 0, $time = 0) {
|
||||
global $DB;
|
||||
|
||||
if (empty($time)) {
|
||||
$time = time();
|
||||
}
|
||||
|
||||
if ($notification) {
|
||||
$record = new \stdClass();
|
||||
$record->useridfrom = $userfrom->id;
|
||||
$record->useridto = $userto->id;
|
||||
$record->subject = 'No subject';
|
||||
$record->fullmessage = $message;
|
||||
$record->smallmessage = $message;
|
||||
$record->timecreated = $time;
|
||||
|
||||
return $DB->insert_record('notifications', $record);
|
||||
}
|
||||
|
||||
if ($userfrom->id == $userto->id) {
|
||||
// It's a self conversation.
|
||||
$conversation = \core_message\api::get_self_conversation($userfrom->id);
|
||||
if (empty($conversation)) {
|
||||
$conversation = \core_message\api::create_conversation(
|
||||
\core_message\api::MESSAGE_CONVERSATION_TYPE_SELF,
|
||||
[$userfrom->id]
|
||||
);
|
||||
}
|
||||
$conversationid = $conversation->id;
|
||||
} else if (!$conversationid = \core_message\api::get_conversation_between_users([$userfrom->id, $userto->id])) {
|
||||
// It's an individual conversation between two different users.
|
||||
$conversation = \core_message\api::create_conversation(
|
||||
\core_message\api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL,
|
||||
[
|
||||
$userfrom->id,
|
||||
$userto->id
|
||||
]
|
||||
);
|
||||
$conversationid = $conversation->id;
|
||||
}
|
||||
|
||||
// Ok, send the message.
|
||||
$record = new \stdClass();
|
||||
$record->useridfrom = $userfrom->id;
|
||||
$record->conversationid = $conversationid;
|
||||
$record->subject = 'No subject';
|
||||
$record->fullmessage = $message;
|
||||
$record->smallmessage = $message;
|
||||
$record->timecreated = $time;
|
||||
|
||||
return $DB->insert_record('messages', $record);
|
||||
parent::setUpBeforeClass();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -140,6 +60,8 @@ class messagelib_test extends \advanced_testcase {
|
||||
public function test_message_search_users() {
|
||||
global $USER;
|
||||
|
||||
$this->resetAfterTest();
|
||||
|
||||
// Set this user as the admin.
|
||||
$this->setAdminUser();
|
||||
|
||||
@@ -163,7 +85,7 @@ class messagelib_test extends \advanced_testcase {
|
||||
public function test_message_get_messages() {
|
||||
global $DB;
|
||||
|
||||
$this->resetAfterTest(true);
|
||||
$this->resetAfterTest();
|
||||
|
||||
// Set this user as the admin.
|
||||
$this->setAdminUser();
|
||||
@@ -234,8 +156,8 @@ class messagelib_test extends \advanced_testcase {
|
||||
/**
|
||||
* Test message_get_messages with only group conversations between users.
|
||||
*/
|
||||
public function test_message_get_messages_only_group_conversations() {
|
||||
$this->resetAfterTest(true);
|
||||
public function test_message_get_messages_only_group_conversations(): void {
|
||||
$this->resetAfterTest();
|
||||
|
||||
// Set this user as the admin.
|
||||
$this->setAdminUser();
|
||||
|
||||
Reference in New Issue
Block a user