This commit is contained in:
Andrew Nicols
2023-01-19 09:05:46 +08:00
6 changed files with 26 additions and 459 deletions
+8 -247
View File
@@ -45,11 +45,13 @@ class api_test extends messagelib_test {
$this->send_fake_message($sender, $recipient);
$this->send_fake_message($sender, $recipient);
$this->assertEquals(1, api::count_unread_conversations($recipient));
api::mark_all_notifications_as_read($recipient->id);
api::mark_all_messages_as_read($recipient->id);
$this->assertEquals(message_count_unread_messages($recipient), 0);
$this->assertDebuggingCalled();
// We've marked all of recipients conversation messages as read.
$this->assertEquals(0, api::count_unread_conversations($recipient));
}
public function test_mark_all_read_for_user_touser_with_fromuser() {
@@ -70,32 +72,14 @@ class api_test extends messagelib_test {
$this->send_fake_message($sender2, $recipient);
$this->send_fake_message($sender2, $recipient);
$this->assertEquals(2, api::count_unread_conversations($recipient));
api::mark_all_notifications_as_read($recipient->id, $sender1->id);
$conversationid = api::get_conversation_between_users([$recipient->id, $sender1->id]);
api::mark_all_messages_as_read($recipient->id, $conversationid);
$this->assertEquals(message_count_unread_messages($recipient), 3);
$this->assertDebuggingCalled();
}
public function test_mark_all_read_for_user_touser_with_type() {
$sender = $this->getDataGenerator()->create_user(array('firstname' => 'Test1', 'lastname' => 'User1'));
$recipient = $this->getDataGenerator()->create_user(array('firstname' => 'Test2', 'lastname' => 'User2'));
$this->send_fake_message($sender, $recipient, 'Notification', 1);
$this->send_fake_message($sender, $recipient, 'Notification', 1);
$this->send_fake_message($sender, $recipient, 'Notification', 1);
$this->send_fake_message($sender, $recipient);
$this->send_fake_message($sender, $recipient);
$this->send_fake_message($sender, $recipient);
api::mark_all_notifications_as_read($recipient->id);
$this->assertEquals(message_count_unread_messages($recipient), 3);
$this->assertDebuggingCalled();
api::mark_all_messages_as_read($recipient->id);
$this->assertEquals(message_count_unread_messages($recipient), 0);
$this->assertDebuggingCalled();
// We've marked only the conversation with sender1 messages as read.
$this->assertEquals(1, api::count_unread_conversations($recipient));
}
/**
@@ -3806,229 +3790,6 @@ class api_test extends messagelib_test {
$this->assertCount(1, api::get_blocked_users($USER->id));
}
/**
* Test returning contacts with unread message count.
* MDL-69643
*/
public function test_get_contacts_with_unread_message_count() {
global $DB;
$user1 = self::getDataGenerator()->create_user();
$user2 = self::getDataGenerator()->create_user();
$user3 = self::getDataGenerator()->create_user();
$user4 = self::getDataGenerator()->create_user();
// Add the users to each of their contacts.
api::add_contact($user1->id, $user2->id);
api::add_contact($user2->id, $user3->id);
$this->send_fake_message($user1, $user2);
$this->send_fake_message($user1, $user2);
$this->send_fake_message($user1, $user2);
$message4id = $this->send_fake_message($user1, $user2);
$this->send_fake_message($user3, $user2);
$message6id = $this->send_fake_message($user3, $user2);
$this->send_fake_message($user3, $user2);
$this->send_fake_message($user3, $user2);
$this->send_fake_message($user3, $user2);
// Send a message that should never be included as the user is not a contact.
$this->send_fake_message($user4, $user2);
// Get the contacts and the unread message count.
$messages = api::get_contacts_with_unread_message_count($user2->id);
$this->assertDebuggingCalled();
// Confirm the size is correct.
$this->assertCount(2, $messages);
ksort($messages);
$messageinfo1 = array_shift($messages);
$messageinfo2 = array_shift($messages);
$this->assertEquals($user1->id, $messageinfo1->id);
$this->assertEquals(4, $messageinfo1->messagecount);
$this->assertEquals($user3->id, $messageinfo2->id);
$this->assertEquals(5, $messageinfo2->messagecount);
// Mark some of the messages as read.
$m4 = $DB->get_record('messages', ['id' => $message4id]);
$m6 = $DB->get_record('messages', ['id' => $message6id]);
api::mark_message_as_read($user2->id, $m4);
api::mark_message_as_read($user2->id, $m6);
// Get the contacts and the unread message count.
$messages = api::get_contacts_with_unread_message_count($user2->id);
$this->assertDebuggingCalled();
// Confirm the size is correct.
$this->assertCount(2, $messages);
ksort($messages);
// Confirm read messages are not included.
$messageinfo1 = array_shift($messages);
$messageinfo2 = array_shift($messages);
$this->assertEquals($user1->id, $messageinfo1->id);
$this->assertEquals(3, $messageinfo1->messagecount);
$this->assertEquals($user3->id, $messageinfo2->id);
$this->assertEquals(4, $messageinfo2->messagecount);
// Now, let's populate the database with messages from user2 to user 1.
$this->send_fake_message($user2, $user1);
$this->send_fake_message($user2, $user1);
$messageid = $this->send_fake_message($user2, $user1);
// Send a message that should never be included as the user is not a contact.
$this->send_fake_message($user4, $user1);
// Get the contacts and the unread message count.
$messages = api::get_contacts_with_unread_message_count($user1->id);
$this->assertDebuggingCalled();
// Confirm the size is correct.
$this->assertCount(1, $messages);
$messageinfo1 = array_shift($messages);
$this->assertEquals($user2->id, $messageinfo1->id);
$this->assertEquals(3, $messageinfo1->messagecount);
// Mark the last message as read.
$m = $DB->get_record('messages', ['id' => $messageid]);
api::mark_message_as_read($user1->id, $m);
$messages = api::get_contacts_with_unread_message_count($user1->id);
$this->assertDebuggingCalled();
// Confirm the size is correct.
$this->assertCount(1, $messages);
// Confirm read messages are not included.
$messageinfo1 = array_shift($messages);
$this->assertEquals($user2->id, $messageinfo1->id);
$this->assertEquals(2, $messageinfo1->messagecount);
}
/**
* Test returning contacts with unread message count when there are no messages.
*/
public function test_get_contacts_with_unread_message_count_no_messages() {
$user1 = self::getDataGenerator()->create_user();
$user2 = self::getDataGenerator()->create_user();
// Add the users to each of their contacts.
api::add_contact($user2->id, $user1->id);
// Check we get the correct message count.
$messages = api::get_contacts_with_unread_message_count($user2->id);
$this->assertDebuggingCalled();
// Confirm the size is correct.
$this->assertCount(1, $messages);
$messageinfo = array_shift($messages);
$this->assertEquals($user1->id, $messageinfo->id);
$this->assertEquals(0, $messageinfo->messagecount);
}
/**
* Test returning non-contacts with unread message count.
* MDL-69643
*/
public function test_get_non_contacts_with_unread_message_count() {
global $DB;
$user1 = self::getDataGenerator()->create_user();
$user2 = self::getDataGenerator()->create_user();
$user3 = self::getDataGenerator()->create_user();
$user4 = self::getDataGenerator()->create_user();
// Add a user to the contact list of the users we are testing this function with.
api::add_contact($user1->id, $user4->id);
api::add_contact($user2->id, $user4->id);
$this->send_fake_message($user1, $user2);
$this->send_fake_message($user1, $user2);
$this->send_fake_message($user1, $user2);
$message4id = $this->send_fake_message($user1, $user2);
$this->send_fake_message($user3, $user2);
$message6id = $this->send_fake_message($user3, $user2);
$this->send_fake_message($user3, $user2);
$this->send_fake_message($user3, $user2);
$this->send_fake_message($user3, $user2);
// Send a message that should never be included as the user is a contact.
$this->send_fake_message($user4, $user2);
// Get the non-contacts and the unread message count.
$messages = api::get_non_contacts_with_unread_message_count($user2->id);
$this->assertDebuggingCalled();
// Check we get the correct message count.
ksort($messages);
$this->assertCount(2, $messages);
$messageinfo1 = array_shift($messages);
$messageinfo2 = array_shift($messages);
$this->assertEquals($user1->id, $messageinfo1->id);
$this->assertEquals(4, $messageinfo1->messagecount);
$this->assertEquals($user3->id, $messageinfo2->id);
$this->assertEquals(5, $messageinfo2->messagecount);
// Mark some of the messages as read.
$m4 = $DB->get_record('messages', ['id' => $message4id]);
$m6 = $DB->get_record('messages', ['id' => $message6id]);
api::mark_message_as_read($user2->id, $m4);
api::mark_message_as_read($user2->id, $m6);
// Get the non-contacts and the unread message count.
$messages = api::get_non_contacts_with_unread_message_count($user2->id);
$this->assertDebuggingCalled();
// Check the marked message is not returned in the message count.
ksort($messages);
$this->assertCount(2, $messages);
$messageinfo1 = array_shift($messages);
$messageinfo2 = array_shift($messages);
$this->assertEquals($user1->id, $messageinfo1->id);
$this->assertEquals(3, $messageinfo1->messagecount);
$this->assertEquals($user3->id, $messageinfo2->id);
$this->assertEquals(4, $messageinfo2->messagecount);
// Now, let's populate the database with messages from user2 to user 1.
$this->send_fake_message($user2, $user1);
$this->send_fake_message($user2, $user1);
$messageid = $this->send_fake_message($user2, $user1);
// Send a message that should never be included as the user is a contact.
$this->send_fake_message($user4, $user1);
// Get the non-contacts and the unread message count.
$messages = api::get_non_contacts_with_unread_message_count($user1->id);
$this->assertDebuggingCalled();
// Confirm the size is correct.
$this->assertCount(1, $messages);
$messageinfo1 = array_shift($messages);
$this->assertEquals($user2->id, $messageinfo1->id);
$this->assertEquals(3, $messageinfo1->messagecount);
// Mark the last message as read.
$m = $DB->get_record('messages', ['id' => $messageid]);
api::mark_message_as_read($user1->id, $m);
// Get the non-contacts and the unread message count.
$messages = api::get_non_contacts_with_unread_message_count($user1->id);
$this->assertDebuggingCalled();
// Check the marked message is not returned in the message count.
$this->assertCount(1, $messages);
$messageinfo1 = array_shift($messages);
$this->assertEquals($user2->id, $messageinfo1->id);
$this->assertEquals(2, $messageinfo1->messagecount);
}
/**
* Test marking a message as read.
*/