From 346894280719beff52fa60137588e96055ea9d67 Mon Sep 17 00:00:00 2001 From: Andrew Nicols Date: Tue, 9 Feb 2016 13:38:56 +0800 Subject: [PATCH] MDL-52928 message: Correct contact fetching in recent conversations --- message/lib.php | 2 +- message/tests/messagelib_test.php | 73 ++++++++++++++++++++++++++----- 2 files changed, 64 insertions(+), 11 deletions(-) diff --git a/message/lib.php b/message/lib.php index 9ded749f2dc..f7e76e4c104 100644 --- a/message/lib.php +++ b/message/lib.php @@ -764,7 +764,7 @@ function message_get_recent_conversations($user, $limitfrom=0, $limitto=100) { ) messagesubset ON messagesubset.messageid = message.id JOIN {user} otheruser ON (message.useridfrom = :userid4 AND message.useridto = otheruser.id) OR (message.useridto = :userid5 AND message.useridfrom = otheruser.id) - LEFT JOIN {message_contacts} contact ON contact.userid = :userid3 AND contact.userid = otheruser.id + LEFT JOIN {message_contacts} contact ON contact.userid = :userid3 AND contact.contactid = otheruser.id WHERE otheruser.deleted = 0 AND message.notification = 0 ORDER BY message.timecreated DESC"; $params = array( diff --git a/message/tests/messagelib_test.php b/message/tests/messagelib_test.php index 67d42a849f4..f250e033585 100644 --- a/message/tests/messagelib_test.php +++ b/message/tests/messagelib_test.php @@ -383,13 +383,14 @@ class core_message_messagelib_testcase extends advanced_testcase { */ public function message_get_recent_conversations_provider() { return array( - array( - // Test that conversations with multiple contacts is correctly ordered. + 'Test that conversations with messages contacts is correctly ordered.' => array( 'users' => array( 'user1', 'user2', 'user3', ), + 'contacts' => array( + ), 'messages' => array( array( 'from' => 'user1', @@ -465,11 +466,52 @@ class core_message_messagelib_testcase extends advanced_testcase { ), ), ), - array( - // Test conversations with a single user, where some messages are read and some are not. + 'Test that users with contacts and messages to self work as expected' => array( 'users' => array( 'user1', 'user2', + 'user3', + ), + 'contacts' => array( + 'user1' => array( + 'user2' => 0, + 'user3' => 0, + ), + 'user2' => array( + 'user3' => 0, + ), + ), + 'messages' => array( + array( + 'from' => 'user1', + 'to' => 'user1', + 'state' => 'unread', + 'subject' => 'S1', + ), + array( + 'from' => 'user1', + 'to' => 'user1', + 'state' => 'unread', + 'subject' => 'S2', + ), + ), + 'expectations' => array( + 'user1' => array( + // User1 has conversed most recently with user1. The most recent message is S2. + array( + 'messageposition' => 0, + 'with' => 'user1', + 'subject' => 'S2', + ), + ), + ), + ), + 'Test conversations with a single user, where some messages are read and some are not.' => array( + 'users' => array( + 'user1', + 'user2', + ), + 'contacts' => array( ), 'messages' => array( array( @@ -518,15 +560,16 @@ class core_message_messagelib_testcase extends advanced_testcase { ), ), ), - array( - // Test conversations with a single user, where some messages are read and some are not, and messages - // are out of order. - // This can happen through a combination of factors including multi-master DB replication with messages - // read somehow (e.g. API). + 'Test conversations with a single user, where some messages are read and some are not, and messages ' . + 'are out of order' => array( + // This can happen through a combination of factors including multi-master DB replication with messages + // read somehow (e.g. API). 'users' => array( 'user1', 'user2', ), + 'contacts' => array( + ), 'messages' => array( array( 'from' => 'user1', @@ -584,7 +627,7 @@ class core_message_messagelib_testcase extends advanced_testcase { * @param array $messagesdata The list of messages to create. * @param array $expectations The list of expected outcomes. */ - public function test_message_get_recent_conversations($usersdata, $messagesdata, $expectations) { + public function test_message_get_recent_conversations($usersdata, $contacts, $messagesdata, $expectations) { global $DB; // Create all of the users. @@ -593,6 +636,16 @@ class core_message_messagelib_testcase extends advanced_testcase { $users[$username] = $this->getDataGenerator()->create_user(array('username' => $username)); } + foreach ($contacts as $username => $contact) { + foreach ($contact as $contactname => $blocked) { + $record = new stdClass(); + $record->userid = $users[$username]->id; + $record->contactid = $users[$contactname]->id; + $record->blocked = $blocked; + $record->id = $DB->insert_record('message_contacts', $record); + } + } + $defaulttimecreated = time(); foreach ($messagesdata as $messagedata) { $from = $users[$messagedata['from']];