Skip to content

Commit

Permalink
MDL-52928 message: Correct contact fetching in recent conversations
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewnicols committed Feb 9, 2016
1 parent 9d5d9c6 commit a0bc890
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 11 deletions.
2 changes: 1 addition & 1 deletion message/lib.php
Expand Up @@ -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(
Expand Down
73 changes: 63 additions & 10 deletions message/tests/messagelib_test.php
Expand Up @@ -383,13 +383,14 @@ public function test_message_search() {
*/
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',
Expand Down Expand Up @@ -465,12 +466,53 @@ public function message_get_recent_conversations_provider() {
),
),
),
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(
'from' => 'user1',
Expand Down Expand Up @@ -518,15 +560,16 @@ public function message_get_recent_conversations_provider() {
),
),
),
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',
Expand Down Expand Up @@ -584,7 +627,7 @@ public function message_get_recent_conversations_provider() {
* @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.
Expand All @@ -593,6 +636,16 @@ public function test_message_get_recent_conversations($usersdata, $messagesdata,
$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']];
Expand Down

0 comments on commit a0bc890

Please sign in to comment.