MDL-54708 message: notification popover respects message preferences
[moodle.git] / message / tests / messagelib_test.php
index 726bf30..906942f 100644 (file)
@@ -79,7 +79,7 @@ class core_message_messagelib_testcase extends advanced_testcase {
     }
 
     /**
-     * Send a fake unread notification.
+     * Send a fake unread popup notification.
      *
      * {@link message_send()} does not support transaction, this function will simulate a message
      * sent from a user to another. We should stop using it once {@link message_send()} will support
@@ -91,7 +91,7 @@ class core_message_messagelib_testcase extends advanced_testcase {
      * @param int $timecreated time the message was created.
      * @return int the id of the message
      */
-    protected function send_fake_unread_notification($userfrom, $userto, $message = 'Hello world!', $timecreated = 0) {
+    protected function send_fake_unread_popup_notification($userfrom, $userto, $message = 'Hello world!', $timecreated = 0) {
         global $DB;
 
         $record = new stdClass();
@@ -103,11 +103,19 @@ class core_message_messagelib_testcase extends advanced_testcase {
         $record->smallmessage = $message;
         $record->timecreated = $timecreated ? $timecreated : time();
 
-        return $DB->insert_record('message', $record);
+        $id = $DB->insert_record('message', $record);
+
+        $popup = new stdClass();
+        $popup->messageid = $id;
+        $popup->isread = 0;
+
+        $DB->insert_record('message_popup', $popup);
+
+        return $id;
     }
 
     /**
-     * Send a fake read notification.
+     * Send a fake read popup notification.
      *
      * {@link message_send()} does not support transaction, this function will simulate a message
      * sent from a user to another. We should stop using it once {@link message_send()} will support
@@ -119,7 +127,7 @@ class core_message_messagelib_testcase extends advanced_testcase {
      * @param int $timecreated time the message was created.
      * @return int the id of the message
      */
-    protected function send_fake_read_notification($userfrom, $userto, $message = 'Hello world!', $timecreated = 0, $timeread = 0) {
+    protected function send_fake_read_popup_notification($userfrom, $userto, $message = 'Hello world!', $timecreated = 0, $timeread = 0) {
         global $DB;
 
         $record = new stdClass();
@@ -132,7 +140,15 @@ class core_message_messagelib_testcase extends advanced_testcase {
         $record->timecreated = $timecreated ? $timecreated : time();
         $record->timeread = $timeread ? $timeread : time();
 
-        return $DB->insert_record('message_read', $record);
+        $id = $DB->insert_record('message_read', $record);
+
+        $popup = new stdClass();
+        $popup->messageid = $id;
+        $popup->isread = 1;
+
+        $DB->insert_record('message_popup', $popup);
+
+        return $id;
     }
 
     /**
@@ -929,66 +945,66 @@ class core_message_messagelib_testcase extends advanced_testcase {
     }
 
     /**
-     * Test that the message_get_notifications function will return only read notifications if requested.
+     * Test that the message_get_popup_notifications function will return only read notifications if requested.
      */
-    public function test_message_get_notifications_read_only() {
+    public function test_message_get_popup_notifications_read_only() {
         $sender = $this->getDataGenerator()->create_user(array('firstname' => 'Test1', 'lastname' => 'User1'));
         $recipient = $this->getDataGenerator()->create_user(array('firstname' => 'Test2', 'lastname' => 'User2'));
 
-        $this->send_fake_read_notification($sender, $recipient, 'Message 1', 2);
-        $this->send_fake_read_notification($sender, $recipient, 'Message 2', 4);
+        $this->send_fake_read_popup_notification($sender, $recipient, 'Message 1', 2);
+        $this->send_fake_read_popup_notification($sender, $recipient, 'Message 2', 4);
 
-        $notifications = message_get_notifications($recipient->id, 0, MESSAGE_READ);
+        $notifications = message_get_popup_notifications($recipient->id, MESSAGE_READ);
 
         $this->assertEquals($notifications[0]->fullmessage, 'Message 2');
         $this->assertEquals($notifications[1]->fullmessage, 'Message 1');
 
         // Check if we request read and unread but there are only read messages, it should
         // still return those correctly.
-        $notifications = message_get_notifications($recipient->id, 0, '');
+        $notifications = message_get_popup_notifications($recipient->id, '');
 
         $this->assertEquals($notifications[0]->fullmessage, 'Message 2');
         $this->assertEquals($notifications[1]->fullmessage, 'Message 1');
     }
 
     /**
-     * Test that the message_get_notifications function will return only unread notifications if requested.
+     * Test that the message_get_popup_notifications function will return only unread notifications if requested.
      */
-    public function test_message_get_notifications_unread_only() {
+    public function test_message_get_popup_notifications_unread_only() {
         $sender = $this->getDataGenerator()->create_user(array('firstname' => 'Test1', 'lastname' => 'User1'));
         $recipient = $this->getDataGenerator()->create_user(array('firstname' => 'Test2', 'lastname' => 'User2'));
 
-        $this->send_fake_unread_notification($sender, $recipient, 'Message 1', 2);
-        $this->send_fake_unread_notification($sender, $recipient, 'Message 2', 4);
+        $this->send_fake_unread_popup_notification($sender, $recipient, 'Message 1', 2);
+        $this->send_fake_unread_popup_notification($sender, $recipient, 'Message 2', 4);
 
-        $notifications = message_get_notifications($recipient->id, 0, MESSAGE_UNREAD);
+        $notifications = message_get_popup_notifications($recipient->id, MESSAGE_UNREAD);
 
         $this->assertEquals($notifications[0]->fullmessage, 'Message 2');
         $this->assertEquals($notifications[1]->fullmessage, 'Message 1');
 
         // Check if we request read and unread but there are only read messages, it should
         // still return those correctly.
-        $notifications = message_get_notifications($recipient->id, 0, '');
+        $notifications = message_get_popup_notifications($recipient->id, '');
 
         $this->assertEquals($notifications[0]->fullmessage, 'Message 2');
         $this->assertEquals($notifications[1]->fullmessage, 'Message 1');
     }
 
     /**
-     * Test that the message_get_notifications function will return the correct notifications when both
+     * Test that the message_get_popup_notifications function will return the correct notifications when both
      * read and unread notifications are included.
      */
-    public function test_message_get_notifications_mixed() {
+    public function test_message_get_popup_notifications_mixed() {
         $sender = $this->getDataGenerator()->create_user(array('firstname' => 'Test1', 'lastname' => 'User1'));
         $recipient = $this->getDataGenerator()->create_user(array('firstname' => 'Test2', 'lastname' => 'User2'));
 
-        $this->send_fake_read_notification($sender, $recipient, 'Message 1', 1);
-        $this->send_fake_unread_notification($sender, $recipient, 'Message 2', 2);
-        $this->send_fake_read_notification($sender, $recipient, 'Message 3', 3, 1);
-        $this->send_fake_read_notification($sender, $recipient, 'Message 4', 3, 2);
-        $this->send_fake_unread_notification($sender, $recipient, 'Message 5', 4);
+        $this->send_fake_read_popup_notification($sender, $recipient, 'Message 1', 1);
+        $this->send_fake_unread_popup_notification($sender, $recipient, 'Message 2', 2);
+        $this->send_fake_read_popup_notification($sender, $recipient, 'Message 3', 3, 1);
+        $this->send_fake_read_popup_notification($sender, $recipient, 'Message 4', 3, 2);
+        $this->send_fake_unread_popup_notification($sender, $recipient, 'Message 5', 4);
 
-        $notifications = message_get_notifications($recipient->id, 0);
+        $notifications = message_get_popup_notifications($recipient->id);
 
         $this->assertEquals($notifications[0]->fullmessage, 'Message 5');
         $this->assertEquals($notifications[1]->fullmessage, 'Message 4');
@@ -996,44 +1012,44 @@ class core_message_messagelib_testcase extends advanced_testcase {
         $this->assertEquals($notifications[3]->fullmessage, 'Message 2');
         $this->assertEquals($notifications[4]->fullmessage, 'Message 1');
 
-        $notifications = message_get_notifications($recipient->id, 0, MESSAGE_READ);
+        $notifications = message_get_popup_notifications($recipient->id, MESSAGE_READ);
 
         $this->assertEquals($notifications[0]->fullmessage, 'Message 4');
         $this->assertEquals($notifications[1]->fullmessage, 'Message 3');
         $this->assertEquals($notifications[2]->fullmessage, 'Message 1');
 
-        $notifications = message_get_notifications($recipient->id, 0, MESSAGE_UNREAD);
+        $notifications = message_get_popup_notifications($recipient->id, MESSAGE_UNREAD);
 
         $this->assertEquals($notifications[0]->fullmessage, 'Message 5');
         $this->assertEquals($notifications[1]->fullmessage, 'Message 2');
     }
 
     /**
-     * Test that the message_get_notifications function works correctly with limiting and offsetting
+     * Test that the message_get_popup_notifications function works correctly with limiting and offsetting
      * the result set if requested.
      */
-    public function test_message_get_notifications_all_with_limit_and_offset() {
+    public function test_message_get_popup_notifications_all_with_limit_and_offset() {
         $sender = $this->getDataGenerator()->create_user(array('firstname' => 'Test1', 'lastname' => 'User1'));
         $recipient = $this->getDataGenerator()->create_user(array('firstname' => 'Test2', 'lastname' => 'User2'));
 
-        $this->send_fake_read_notification($sender, $recipient, 'Message 1', 1);
-        $this->send_fake_unread_notification($sender, $recipient, 'Message 2', 2);
-        $this->send_fake_read_notification($sender, $recipient, 'Message 3', 3, 1);
-        $this->send_fake_read_notification($sender, $recipient, 'Message 4', 3, 2);
-        $this->send_fake_unread_notification($sender, $recipient, 'Message 5', 4);
-        $this->send_fake_unread_notification($sender, $recipient, 'Message 6', 5);
+        $this->send_fake_read_popup_notification($sender, $recipient, 'Message 1', 1);
+        $this->send_fake_unread_popup_notification($sender, $recipient, 'Message 2', 2);
+        $this->send_fake_read_popup_notification($sender, $recipient, 'Message 3', 3, 1);
+        $this->send_fake_read_popup_notification($sender, $recipient, 'Message 4', 3, 2);
+        $this->send_fake_unread_popup_notification($sender, $recipient, 'Message 5', 4);
+        $this->send_fake_unread_popup_notification($sender, $recipient, 'Message 6', 5);
 
-        $notifications = message_get_notifications($recipient->id, 0, '', false, false, 'DESC', 2, 0);
+        $notifications = message_get_popup_notifications($recipient->id, '', false, false, 'DESC', 2, 0);
 
         $this->assertEquals($notifications[0]->fullmessage, 'Message 6');
         $this->assertEquals($notifications[1]->fullmessage, 'Message 5');
 
-        $notifications = message_get_notifications($recipient->id, 0, '', false, false, 'DESC', 2, 2);
+        $notifications = message_get_popup_notifications($recipient->id, '', false, false, 'DESC', 2, 2);
 
         $this->assertEquals($notifications[0]->fullmessage, 'Message 4');
         $this->assertEquals($notifications[1]->fullmessage, 'Message 3');
 
-        $notifications = message_get_notifications($recipient->id, 0, '', false, false, 'DESC', 0, 3);
+        $notifications = message_get_popup_notifications($recipient->id, '', false, false, 'DESC', 0, 3);
 
         $this->assertEquals($notifications[0]->fullmessage, 'Message 3');
         $this->assertEquals($notifications[1]->fullmessage, 'Message 2');
@@ -1041,54 +1057,17 @@ class core_message_messagelib_testcase extends advanced_testcase {
     }
 
     /**
-     * Test that the message_get_notifications function returns correct values if specifying
-     * a sender.
-     */
-    public function test_message_get_notifications_multiple_senders() {
-        $sender1 = $this->getDataGenerator()->create_user(array('firstname' => 'Test1', 'lastname' => 'User1'));
-        $sender2 = $this->getDataGenerator()->create_user(array('firstname' => 'Test3', 'lastname' => 'User3'));
-        $recipient1 = $this->getDataGenerator()->create_user(array('firstname' => 'Test2', 'lastname' => 'User2'));
-        $recipient2 = $this->getDataGenerator()->create_user(array('firstname' => 'Test4', 'lastname' => 'User4'));
-
-        $this->send_fake_read_notification($sender1, $recipient1, 'Message 1', 1);
-        $this->send_fake_unread_notification($sender1, $recipient1, 'Message 2', 2);
-        $this->send_fake_read_notification($sender1, $recipient2, 'Message 3', 3);
-        $this->send_fake_unread_notification($sender1, $recipient2, 'Message 4', 4);
-        $this->send_fake_read_notification($sender2, $recipient1, 'Message 5', 5);
-        $this->send_fake_unread_notification($sender2, $recipient1, 'Message 6', 6);
-        $this->send_fake_read_notification($sender2, $recipient2, 'Message 7', 7);
-        $this->send_fake_unread_notification($sender2, $recipient2, 'Message 8', 8);
-
-        $notifications = message_get_notifications(0, $sender1->id, '', false, false, 'DESC');
-
-        $this->assertEquals($notifications[0]->fullmessage, 'Message 4');
-        $this->assertEquals($notifications[1]->fullmessage, 'Message 3');
-        $this->assertEquals($notifications[2]->fullmessage, 'Message 2');
-        $this->assertEquals($notifications[3]->fullmessage, 'Message 1');
-
-        $notifications = message_get_notifications(0, $sender1->id, '', false, false, 'DESC', 2, 2);
-
-        $this->assertEquals($notifications[0]->fullmessage, 'Message 2');
-        $this->assertEquals($notifications[1]->fullmessage, 'Message 1');
-
-        $notifications = message_get_notifications($recipient1->id, $sender1->id, '', false, false, 'DESC');
-
-        $this->assertEquals($notifications[0]->fullmessage, 'Message 2');
-        $this->assertEquals($notifications[1]->fullmessage, 'Message 1');
-    }
-
-    /**
-     * Test that the message_get_notifications function returns embedded user details for the
+     * Test that the message_get_popup_notifications function returns embedded user details for the
      * sender if requested.
      */
-    public function test_message_get_notifications_embed_sender() {
+    public function test_message_get_popup_notifications_embed_sender() {
         $sender = $this->getDataGenerator()->create_user(array('firstname' => 'Test1', 'lastname' => 'User1'));
         $recipient = $this->getDataGenerator()->create_user(array('firstname' => 'Test2', 'lastname' => 'User2'));
 
-        $this->send_fake_read_notification($sender, $recipient, 'Message 1', 1);
-        $this->send_fake_unread_notification($sender, $recipient, 'Message 2', 2);
+        $this->send_fake_read_popup_notification($sender, $recipient, 'Message 1', 1);
+        $this->send_fake_unread_popup_notification($sender, $recipient, 'Message 2', 2);
 
-        $notifications = message_get_notifications(0, $sender->id, '', false, true, 'DESC');
+        $notifications = message_get_popup_notifications($recipient->id, '', false, true, 'DESC');
 
         $func = function($type) {
             return function($notification) use ($type) {
@@ -1113,17 +1092,17 @@ class core_message_messagelib_testcase extends advanced_testcase {
     }
 
     /**
-     * Test that the message_get_notifications function returns embedded user details for the
+     * Test that the message_get_popup_notifications function returns embedded user details for the
      * recipient if requested.
      */
-    public function test_message_get_notifications_embed_recipient() {
+    public function test_message_get_popup_notifications_embed_recipient() {
         $sender = $this->getDataGenerator()->create_user(array('firstname' => 'Test1', 'lastname' => 'User1'));
         $recipient = $this->getDataGenerator()->create_user(array('firstname' => 'Test2', 'lastname' => 'User2'));
 
-        $this->send_fake_read_notification($sender, $recipient, 'Message 1', 1);
-        $this->send_fake_unread_notification($sender, $recipient, 'Message 2', 2);
+        $this->send_fake_read_popup_notification($sender, $recipient, 'Message 1', 1);
+        $this->send_fake_unread_popup_notification($sender, $recipient, 'Message 2', 2);
 
-        $notifications = message_get_notifications(0, $sender->id, '', true, false, 'DESC');
+        $notifications = message_get_popup_notifications($recipient->id, '', true, false, 'DESC');
 
         $func = function($type) {
             return function($notification) use ($type) {
@@ -1148,16 +1127,16 @@ class core_message_messagelib_testcase extends advanced_testcase {
     }
 
     /**
-     * Test that the message_get_notifications function returns embedded all user details.
+     * Test that the message_get_popup_notifications function returns embedded all user details.
      */
-    public function test_message_get_notifications_embed_both() {
+    public function test_message_get_popup_notifications_embed_both() {
         $sender = $this->getDataGenerator()->create_user(array('firstname' => 'Test1', 'lastname' => 'User1'));
         $recipient = $this->getDataGenerator()->create_user(array('firstname' => 'Test2', 'lastname' => 'User2'));
 
-        $this->send_fake_read_notification($sender, $recipient, 'Message 1', 1);
-        $this->send_fake_unread_notification($sender, $recipient, 'Message 2', 2);
+        $this->send_fake_read_popup_notification($sender, $recipient, 'Message 1', 1);
+        $this->send_fake_unread_popup_notification($sender, $recipient, 'Message 2', 2);
 
-        $notifications = message_get_notifications(0, $sender->id, '', true, true, 'DESC');
+        $notifications = message_get_popup_notifications($recipient->id, '', true, true, 'DESC');
 
         $func = function($type) {
             return function($notification) use ($type) {
@@ -1182,27 +1161,25 @@ class core_message_messagelib_testcase extends advanced_testcase {
     }
 
     /**
-     * Test message_count_unread_notifications.
+     * Test message_count_unread_popup_notifications.
      */
-    public function test_message_count_unread_notifications() {
+    public function test_message_count_unread_popup_notifications() {
         $sender1 = $this->getDataGenerator()->create_user(array('firstname' => 'Test1', 'lastname' => 'User1'));
         $sender2 = $this->getDataGenerator()->create_user(array('firstname' => 'Test2', 'lastname' => 'User2'));
         $recipient1 = $this->getDataGenerator()->create_user(array('firstname' => 'Test3', 'lastname' => 'User3'));
         $recipient2 = $this->getDataGenerator()->create_user(array('firstname' => 'Test4', 'lastname' => 'User4'));
 
-        $this->send_fake_unread_notification($sender1, $recipient1);
-        $this->send_fake_unread_notification($sender1, $recipient1);
-        $this->send_fake_unread_notification($sender1, $recipient2);
-        $this->send_fake_unread_notification($sender2, $recipient1);
-        $this->send_fake_unread_notification($sender2, $recipient2);
-        $this->send_fake_unread_notification($sender2, $recipient2);
-
-        $this->assertEquals(message_count_unread_notifications($recipient1->id, $sender1->id), 2);
-        $this->assertEquals(message_count_unread_notifications($recipient2->id, $sender1->id), 1);
-        $this->assertEquals(message_count_unread_notifications($recipient1->id, $sender2->id), 1);
-        $this->assertEquals(message_count_unread_notifications($recipient2->id, $sender2->id), 2);
-        $this->assertEquals(message_count_unread_notifications($recipient1->id, 0), 3);
-        $this->assertEquals(message_count_unread_notifications($recipient2->id, 0), 3);
+        $this->send_fake_unread_popup_notification($sender1, $recipient1);
+        $this->send_fake_unread_popup_notification($sender1, $recipient1);
+        $this->send_fake_unread_popup_notification($sender2, $recipient1);
+        $this->send_fake_unread_popup_notification($sender1, $recipient2);
+        $this->send_fake_unread_popup_notification($sender2, $recipient2);
+        $this->send_fake_unread_popup_notification($sender2, $recipient2);
+        $this->send_fake_unread_popup_notification($sender2, $recipient2);
+        $this->send_fake_unread_popup_notification($sender2, $recipient2);
+
+        $this->assertEquals(message_count_unread_popup_notifications($recipient1->id), 3);
+        $this->assertEquals(message_count_unread_popup_notifications($recipient2->id), 5);
     }
 
 
@@ -1210,9 +1187,9 @@ class core_message_messagelib_testcase extends advanced_testcase {
         $sender = $this->getDataGenerator()->create_user(array('firstname' => 'Test1', 'lastname' => 'User1'));
         $recipient = $this->getDataGenerator()->create_user(array('firstname' => 'Test2', 'lastname' => 'User2'));
 
-        $this->send_fake_unread_notification($sender, $recipient);
-        $this->send_fake_unread_notification($sender, $recipient);
-        $this->send_fake_unread_notification($sender, $recipient);
+        $this->send_fake_unread_popup_notification($sender, $recipient);
+        $this->send_fake_unread_popup_notification($sender, $recipient);
+        $this->send_fake_unread_popup_notification($sender, $recipient);
         $this->send_fake_message($sender, $recipient);
         $this->send_fake_message($sender, $recipient);
         $this->send_fake_message($sender, $recipient);
@@ -1226,15 +1203,15 @@ class core_message_messagelib_testcase extends advanced_testcase {
         $sender2 = $this->getDataGenerator()->create_user(array('firstname' => 'Test3', 'lastname' => 'User3'));
         $recipient = $this->getDataGenerator()->create_user(array('firstname' => 'Test2', 'lastname' => 'User2'));
 
-        $this->send_fake_unread_notification($sender1, $recipient);
-        $this->send_fake_unread_notification($sender1, $recipient);
-        $this->send_fake_unread_notification($sender1, $recipient);
+        $this->send_fake_unread_popup_notification($sender1, $recipient);
+        $this->send_fake_unread_popup_notification($sender1, $recipient);
+        $this->send_fake_unread_popup_notification($sender1, $recipient);
         $this->send_fake_message($sender1, $recipient);
         $this->send_fake_message($sender1, $recipient);
         $this->send_fake_message($sender1, $recipient);
-        $this->send_fake_unread_notification($sender2, $recipient);
-        $this->send_fake_unread_notification($sender2, $recipient);
-        $this->send_fake_unread_notification($sender2, $recipient);
+        $this->send_fake_unread_popup_notification($sender2, $recipient);
+        $this->send_fake_unread_popup_notification($sender2, $recipient);
+        $this->send_fake_unread_popup_notification($sender2, $recipient);
         $this->send_fake_message($sender2, $recipient);
         $this->send_fake_message($sender2, $recipient);
         $this->send_fake_message($sender2, $recipient);
@@ -1247,9 +1224,9 @@ class core_message_messagelib_testcase extends advanced_testcase {
         $sender = $this->getDataGenerator()->create_user(array('firstname' => 'Test1', 'lastname' => 'User1'));
         $recipient = $this->getDataGenerator()->create_user(array('firstname' => 'Test2', 'lastname' => 'User2'));
 
-        $this->send_fake_unread_notification($sender, $recipient);
-        $this->send_fake_unread_notification($sender, $recipient);
-        $this->send_fake_unread_notification($sender, $recipient);
+        $this->send_fake_unread_popup_notification($sender, $recipient);
+        $this->send_fake_unread_popup_notification($sender, $recipient);
+        $this->send_fake_unread_popup_notification($sender, $recipient);
         $this->send_fake_message($sender, $recipient);
         $this->send_fake_message($sender, $recipient);
         $this->send_fake_message($sender, $recipient);