Skip to content

Commit

Permalink
MDL-64307 core_message: do not include disabled conversations in count
Browse files Browse the repository at this point in the history
  • Loading branch information
mdjnelson committed Dec 4, 2018
1 parent 7bb22a2 commit 7c8ba89
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
4 changes: 3 additions & 1 deletion message/classes/api.php
Expand Up @@ -1482,10 +1482,12 @@ public static function count_unread_conversations($user = null) {
LEFT JOIN {message_user_actions} mua
ON (mua.messageid = m.id AND mua.userid = ? AND mua.action = ?)
WHERE mcm.userid = ?
AND mc.enabled = ?
AND mcm.userid != m.useridfrom
AND mua.id is NULL";

return $DB->count_records_sql($sql, [$user->id, self::MESSAGE_ACTION_READ, $user->id]);
return $DB->count_records_sql($sql, [$user->id, self::MESSAGE_ACTION_READ, $user->id,
self::MESSAGE_CONVERSATION_ENABLED]);
}

/**
Expand Down
38 changes: 38 additions & 0 deletions message/tests/api_test.php
Expand Up @@ -3043,6 +3043,44 @@ public function test_count_unread_conversations() {
$this->assertEquals(1, core_message\api::count_unread_conversations($user2));
}

/**
* Tests counting unread conversations where one conversation is disabled.
*/
public function test_count_unread_conversations_disabled() {
$this->resetAfterTest(true);

// Create some users.
$user1 = self::getDataGenerator()->create_user();
$user2 = self::getDataGenerator()->create_user();
$user3 = self::getDataGenerator()->create_user();
$user4 = self::getDataGenerator()->create_user();

// The person wanting the conversation count.
$this->setUser($user1);

// Send some messages back and forth, have some different conversations with different users.
$this->send_fake_message($user1, $user2, 'Yo!');
$this->send_fake_message($user2, $user1, 'Sup mang?');
$this->send_fake_message($user1, $user2, 'Writing PHPUnit tests!');
$this->send_fake_message($user2, $user1, 'Word.');

$this->send_fake_message($user1, $user3, 'Booyah');
$this->send_fake_message($user3, $user1, 'Whaaat?');
$this->send_fake_message($user1, $user3, 'Nothing.');
$this->send_fake_message($user3, $user1, 'Cool.');

$this->send_fake_message($user1, $user4, 'Hey mate, you see the new messaging UI in Moodle?');
$this->send_fake_message($user4, $user1, 'Yah brah, it\'s pretty rad.');
$this->send_fake_message($user1, $user4, 'Dope.');

// Let's disable the last conversation.
$conversationid = core_message\api::get_conversation_between_users([$user1->id, $user4->id]);
core_message\api::disable_conversation($conversationid);

// Check that the disabled conversation was not included.
$this->assertEquals(2, core_message\api::count_unread_conversations());
}

/**
* Tests deleting a conversation.
*/
Expand Down

0 comments on commit 7c8ba89

Please sign in to comment.