From 23aab8e6941b17c08cf1bef4795ca51fe49dce15 Mon Sep 17 00:00:00 2001 From: Mark Nelson Date: Thu, 19 Jun 2014 17:29:42 -0700 Subject: [PATCH] MDL-45594 core_message: added unit tests --- message/tests/messagelib_test.php | 81 ++++++++++++++++++++++++++++++- 1 file changed, 80 insertions(+), 1 deletion(-) diff --git a/message/tests/messagelib_test.php b/message/tests/messagelib_test.php index 126e6afd02a..0259d5781e0 100644 --- a/message/tests/messagelib_test.php +++ b/message/tests/messagelib_test.php @@ -62,6 +62,7 @@ class core_message_messagelib_testcase extends advanced_testcase { * @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. + * @return int the id of the message */ protected function send_fake_message($userfrom, $userto, $message = 'Hello world!') { global $DB; @@ -72,7 +73,8 @@ class core_message_messagelib_testcase extends advanced_testcase { $record->subject = 'No subject'; $record->fullmessage = $message; $record->timecreated = time(); - $insert = $DB->insert_record('message', $record); + + return $DB->insert_record('message', $record); } /** @@ -358,4 +360,81 @@ class core_message_messagelib_testcase extends advanced_testcase { $this->assertEquals(false, message_search(array('Message'), true, true, 2)); $this->assertCount(5, message_search(array('Message'), true, true, SITEID)); } + + /** + * Test message_get_recent_conversations. + */ + public function test_message_get_recent_conversations() { + global $DB, $USER; + + // Set this user as the admin. + $this->setAdminUser(); + + // Create user's to send messages to/from. + $user1 = $this->getDataGenerator()->create_user(array('firstname' => 'Test1', 'lastname' => 'user1')); + $user2 = $this->getDataGenerator()->create_user(array('firstname' => 'Test2', 'lastname' => 'user2')); + + // Add a few messages that have been read and some that are unread. + $m1 = $this->send_fake_message($USER, $user1, 'Message 1'); // An unread message. + $m2 = $this->send_fake_message($user1, $USER, 'Message 2'); // An unread message. + $m3 = $this->send_fake_message($USER, $user1, 'Message 3'); // An unread message. + $m4 = message_post_message($USER, $user2, 'Message 4', FORMAT_PLAIN); + $m5 = message_post_message($user2, $USER, 'Message 5', FORMAT_PLAIN); + $m6 = message_post_message($USER, $user2, 'Message 6', FORMAT_PLAIN); + + // We want to alter the timecreated values so we can ensure message_get_recent_conversations orders by timecreated. + $updatemessage = new stdClass(); + $updatemessage->id = $m3; + $updatemessage->timecreated = 0; + $DB->update_record('message', $updatemessage); + + $updatemessage->id = $m6; + $DB->update_record('message_read', $updatemessage); + + // Get the recent conversations for the current user. + $conversations = message_get_recent_conversations($USER); + + // Confirm that we have received the messages with the maximum timecreated, rather than the max id. + $this->assertEquals('Message 2', $conversations[0]->fullmessage); + $this->assertEquals('Message 5', $conversations[1]->smallmessage); + } + + /** + * Test message_get_recent_notifications. + */ + public function test_message_get_recent_notifications() { + global $DB, $USER; + + // Set this user as the admin. + $this->setAdminUser(); + + // Create a user to send messages from. + $user1 = $this->getDataGenerator()->create_user(array('firstname' => 'Test1', 'lastname' => 'user1')); + + // Add two messages - will mark them as notifications later. + $m1 = message_post_message($user1, $USER, 'Message 1', FORMAT_PLAIN); + $m2 = message_post_message($user1, $USER, 'Message 2', FORMAT_PLAIN); + + // Mark the second message as a notification. + $updatemessage = new stdClass(); + $updatemessage->id = $m2; + $updatemessage->notification = 1; + $DB->update_record('message_read', $updatemessage); + + // Mark the first message as a notification and change the timecreated to 0. + $updatemessage->id = $m1; + $updatemessage->notification = 1; + $updatemessage->timecreated = 0; + $DB->update_record('message_read', $updatemessage); + + $notifications = message_get_recent_notifications($USER); + + // Get the messages. + $firstmessage = array_shift($notifications); + $secondmessage = array_shift($notifications); + + // Confirm that we have received the notifications with the maximum timecreated, rather than the max id. + $this->assertEquals('Message 2', $firstmessage->smallmessage); + $this->assertEquals('Message 1', $secondmessage->smallmessage); + } }