MDL-54708 message: notification popover respects message preferences
[moodle.git] / message / tests / externallib_test.php
index 7454ae3..8827f01 100644 (file)
@@ -63,8 +63,8 @@ class core_message_externallib_testcase extends externallib_advanced_testcase {
         $insert = $DB->insert_record('message', $record);
     }
 
         $insert = $DB->insert_record('message', $record);
     }
 
-        /**
-     * 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
      *
      * {@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
@@ -76,7 +76,7 @@ class core_message_externallib_testcase extends externallib_advanced_testcase {
      * @param int $timecreated time the message was created.
      * @return int the id of the message
      */
      * @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();
         global $DB;
 
         $record = new stdClass();
@@ -88,11 +88,19 @@ class core_message_externallib_testcase extends externallib_advanced_testcase {
         $record->smallmessage = $message;
         $record->timecreated = $timecreated ? $timecreated : time();
 
         $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
      *
      * {@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
@@ -104,7 +112,7 @@ class core_message_externallib_testcase extends externallib_advanced_testcase {
      * @param int $timecreated time the message was created.
      * @return int the id of the message
      */
      * @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();
         global $DB;
 
         $record = new stdClass();
@@ -117,7 +125,15 @@ class core_message_externallib_testcase extends externallib_advanced_testcase {
         $record->timecreated = $timecreated ? $timecreated : time();
         $record->timeread = $timeread ? $timeread : time();
 
         $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;
     }
 
     /**
     }
 
     /**
@@ -878,14 +894,14 @@ class core_message_externallib_testcase extends externallib_advanced_testcase {
 
     }
 
 
     }
 
-    public function test_get_notifications_no_user_exception() {
+    public function test_get_popup_notifications_no_user_exception() {
         $this->resetAfterTest(true);
 
         $this->setExpectedException('moodle_exception');
         $this->resetAfterTest(true);
 
         $this->setExpectedException('moodle_exception');
-        $result = core_message_external::get_notifications(-2132131, 0, '', false, false, true, false, 0, 0);
+        $result = core_message_external::get_popup_notifications(-2132131, '', false, false, false, true, false, 0, 0);
     }
 
     }
 
-    public function test_get_notifications_access_denied_exception() {
+    public function test_get_popup_notifications_access_denied_exception() {
         $this->resetAfterTest(true);
 
         $sender = $this->getDataGenerator()->create_user();
         $this->resetAfterTest(true);
 
         $sender = $this->getDataGenerator()->create_user();
@@ -893,57 +909,57 @@ class core_message_externallib_testcase extends externallib_advanced_testcase {
 
         $this->setUser($user);
         $this->setExpectedException('moodle_exception');
 
         $this->setUser($user);
         $this->setExpectedException('moodle_exception');
-        $result = core_message_external::get_notifications($sender->id, 0, '', false, false, true, false, 0, 0);
+        $result = core_message_external::get_popup_notifications($sender->id, '', false, false, false, true, false, 0, 0);
     }
 
     }
 
-    public function test_get_notifications_as_recipient() {
+    public function test_get_popup_notifications_as_recipient() {
         $this->resetAfterTest(true);
 
         $sender = $this->getDataGenerator()->create_user(array('firstname' => 'Sendy', 'lastname' => 'Sender'));
         $recipient = $this->getDataGenerator()->create_user(array('firstname' => 'Recipy', 'lastname' => 'Recipient'));
 
         $notificationids = array(
         $this->resetAfterTest(true);
 
         $sender = $this->getDataGenerator()->create_user(array('firstname' => 'Sendy', 'lastname' => 'Sender'));
         $recipient = $this->getDataGenerator()->create_user(array('firstname' => 'Recipy', 'lastname' => 'Recipient'));
 
         $notificationids = array(
-            $this->send_fake_unread_notification($sender, $recipient),
-            $this->send_fake_unread_notification($sender, $recipient),
-            $this->send_fake_read_notification($sender, $recipient),
-            $this->send_fake_read_notification($sender, $recipient),
+            $this->send_fake_unread_popup_notification($sender, $recipient),
+            $this->send_fake_unread_popup_notification($sender, $recipient),
+            $this->send_fake_read_popup_notification($sender, $recipient),
+            $this->send_fake_read_popup_notification($sender, $recipient),
         );
 
         // Confirm that admin has super powers to retrieve any notifications.
         $this->setAdminUser();
         );
 
         // Confirm that admin has super powers to retrieve any notifications.
         $this->setAdminUser();
-        $result = core_message_external::get_notifications($recipient->id, 0, '', false, false, true, false, 0, 0);
+        $result = core_message_external::get_popup_notifications($recipient->id, '', false, false, false, true, false, 0, 0);
         $this->assertCount(4, $result['notifications']);
 
         $this->setUser($recipient);
         $this->assertCount(4, $result['notifications']);
 
         $this->setUser($recipient);
-        $result = core_message_external::get_notifications($recipient->id, 0, '', false, false, true, false, 0, 0);
+        $result = core_message_external::get_popup_notifications($recipient->id, '', false, false, false, true, false, 0, 0);
         $this->assertCount(4, $result['notifications']);
 
         $this->assertCount(4, $result['notifications']);
 
-        $result = core_message_external::get_notifications($recipient->id, 0, MESSAGE_UNREAD, false, true, true, false, 0, 0);
+        $result = core_message_external::get_popup_notifications($recipient->id, MESSAGE_UNREAD, false, false, true, true, false, 0, 0);
         $this->assertCount(2, $result['notifications']);
         $this->assertObjectHasAttribute('userfromfullname', $result['notifications'][0]);
         $this->assertObjectNotHasAttribute('usertofullname', $result['notifications'][0]);
         $this->assertObjectHasAttribute('userfromfullname', $result['notifications'][1]);
         $this->assertObjectNotHasAttribute('usertofullname', $result['notifications'][1]);
 
         $this->assertCount(2, $result['notifications']);
         $this->assertObjectHasAttribute('userfromfullname', $result['notifications'][0]);
         $this->assertObjectNotHasAttribute('usertofullname', $result['notifications'][0]);
         $this->assertObjectHasAttribute('userfromfullname', $result['notifications'][1]);
         $this->assertObjectNotHasAttribute('usertofullname', $result['notifications'][1]);
 
-        $result = core_message_external::get_notifications($recipient->id, 0, MESSAGE_UNREAD, true, true, true, false, 0, 0);
+        $result = core_message_external::get_popup_notifications($recipient->id, MESSAGE_UNREAD, false, true, true, true, false, 0, 0);
         $this->assertCount(2, $result['notifications']);
         $this->assertObjectHasAttribute('userfromfullname', $result['notifications'][0]);
         $this->assertObjectHasAttribute('usertofullname', $result['notifications'][0]);
         $this->assertObjectHasAttribute('userfromfullname', $result['notifications'][1]);
         $this->assertObjectHasAttribute('usertofullname', $result['notifications'][1]);
 
         $this->assertCount(2, $result['notifications']);
         $this->assertObjectHasAttribute('userfromfullname', $result['notifications'][0]);
         $this->assertObjectHasAttribute('usertofullname', $result['notifications'][0]);
         $this->assertObjectHasAttribute('userfromfullname', $result['notifications'][1]);
         $this->assertObjectHasAttribute('usertofullname', $result['notifications'][1]);
 
-        $result = core_message_external::get_notifications($recipient->id, 0, MESSAGE_UNREAD, true, true, true, true, 0, 0);
+        $result = core_message_external::get_popup_notifications($recipient->id, MESSAGE_UNREAD, false, true, true, true, true, 0, 0);
         $this->assertCount(2, $result['notifications']);
         $this->assertEquals(0, $result['unreadcount']);
 
         $this->assertCount(2, $result['notifications']);
         $this->assertEquals(0, $result['unreadcount']);
 
-        $result = core_message_external::get_notifications($recipient->id, 0, MESSAGE_UNREAD, true, true, true, true, 0, 0);
+        $result = core_message_external::get_popup_notifications($recipient->id, MESSAGE_UNREAD, false, true, true, true, true, 0, 0);
         $this->assertCount(0, $result['notifications']);
 
         $this->assertCount(0, $result['notifications']);
 
-        $result = core_message_external::get_notifications($recipient->id, 0, MESSAGE_READ, true, true, true, true, 0, 0);
+        $result = core_message_external::get_popup_notifications($recipient->id, MESSAGE_READ, false, true, true, true, true, 0, 0);
         $this->assertCount(4, $result['notifications']);
     }
 
         $this->assertCount(4, $result['notifications']);
     }
 
-    public function test_get_notification_limit_offset() {
+    public function test_get_popup_notification_limit_offset() {
         $this->resetAfterTest(true);
 
         $sender = $this->getDataGenerator()->create_user(array('firstname' => 'Sendy', 'lastname' => 'Sender'));
         $this->resetAfterTest(true);
 
         $sender = $this->getDataGenerator()->create_user(array('firstname' => 'Sendy', 'lastname' => 'Sender'));
@@ -952,22 +968,22 @@ class core_message_externallib_testcase extends externallib_advanced_testcase {
         $this->setUser($recipient);
 
         $notificationids = array(
         $this->setUser($recipient);
 
         $notificationids = array(
-            $this->send_fake_unread_notification($sender, $recipient, 'Notification', 1),
-            $this->send_fake_unread_notification($sender, $recipient, 'Notification', 2),
-            $this->send_fake_unread_notification($sender, $recipient, 'Notification', 3),
-            $this->send_fake_unread_notification($sender, $recipient, 'Notification', 4),
-            $this->send_fake_read_notification($sender, $recipient, 'Notification', 5),
-            $this->send_fake_read_notification($sender, $recipient, 'Notification', 6),
-            $this->send_fake_read_notification($sender, $recipient, 'Notification', 7),
-            $this->send_fake_read_notification($sender, $recipient, 'Notification', 8),
+            $this->send_fake_unread_popup_notification($sender, $recipient, 'Notification 1', 1),
+            $this->send_fake_unread_popup_notification($sender, $recipient, 'Notification 2', 2),
+            $this->send_fake_unread_popup_notification($sender, $recipient, 'Notification 3', 3),
+            $this->send_fake_unread_popup_notification($sender, $recipient, 'Notification 4', 4),
+            $this->send_fake_read_popup_notification($sender, $recipient, 'Notification 5', 5),
+            $this->send_fake_read_popup_notification($sender, $recipient, 'Notification 6', 6),
+            $this->send_fake_read_popup_notification($sender, $recipient, 'Notification 7', 7),
+            $this->send_fake_read_popup_notification($sender, $recipient, 'Notification 8', 8),
         );
 
         );
 
-        $result = core_message_external::get_notifications($recipient->id, 0, '', false, false, true, false, 2, 0);
+        $result = core_message_external::get_popup_notifications($recipient->id, '', false, false, false, true, false, 2, 0);
 
         $this->assertEquals($result['notifications'][0]->id, $notificationids[7]);
         $this->assertEquals($result['notifications'][1]->id, $notificationids[6]);
 
 
         $this->assertEquals($result['notifications'][0]->id, $notificationids[7]);
         $this->assertEquals($result['notifications'][1]->id, $notificationids[6]);
 
-        $result = core_message_external::get_notifications($recipient->id, 0, '', false, false, true, false, 2, 2);
+        $result = core_message_external::get_popup_notifications($recipient->id, '', false, false, false, true, false, 2, 2);
 
         $this->assertEquals($result['notifications'][0]->id, $notificationids[5]);
         $this->assertEquals($result['notifications'][1]->id, $notificationids[4]);
 
         $this->assertEquals($result['notifications'][0]->id, $notificationids[5]);
         $this->assertEquals($result['notifications'][1]->id, $notificationids[4]);
@@ -1012,37 +1028,37 @@ class core_message_externallib_testcase extends externallib_advanced_testcase {
         $this->setUser($recipient);
 
         $notificationids = array(
         $this->setUser($recipient);
 
         $notificationids = array(
-            $this->send_fake_unread_notification($sender1, $recipient, 'Notification', 1),
-            $this->send_fake_unread_notification($sender1, $recipient, 'Notification', 2),
-            $this->send_fake_unread_notification($sender2, $recipient, 'Notification', 3),
-            $this->send_fake_unread_notification($sender2, $recipient, 'Notification', 4),
-            $this->send_fake_unread_notification($sender3, $recipient, 'Notification', 5),
-            $this->send_fake_unread_notification($sender3, $recipient, 'Notification', 6),
+            $this->send_fake_unread_popup_notification($sender1, $recipient, 'Notification', 1),
+            $this->send_fake_unread_popup_notification($sender1, $recipient, 'Notification', 2),
+            $this->send_fake_unread_popup_notification($sender2, $recipient, 'Notification', 3),
+            $this->send_fake_unread_popup_notification($sender2, $recipient, 'Notification', 4),
+            $this->send_fake_unread_popup_notification($sender3, $recipient, 'Notification', 5),
+            $this->send_fake_unread_popup_notification($sender3, $recipient, 'Notification', 6),
         );
 
         core_message_external::mark_all_notifications_as_read($recipient->id, $sender1->id);
         );
 
         core_message_external::mark_all_notifications_as_read($recipient->id, $sender1->id);
-        $readresult = core_message_external::get_notifications($recipient->id, 0, 'read', false, false, true, false, 0, 0);
-        $unreadresult = core_message_external::get_notifications($recipient->id, 0, 'unread', false, false, true, false, 0, 0);
+        $readresult = core_message_external::get_popup_notifications($recipient->id, 'read', false, false, false, true, false, 0, 0);
+        $unreadresult = core_message_external::get_popup_notifications($recipient->id, 'unread', false, false, false, true, false, 0, 0);
 
         $this->assertCount(2, $readresult['notifications']);
         $this->assertCount(4, $unreadresult['notifications']);
 
         core_message_external::mark_all_notifications_as_read($recipient->id, 0);
 
         $this->assertCount(2, $readresult['notifications']);
         $this->assertCount(4, $unreadresult['notifications']);
 
         core_message_external::mark_all_notifications_as_read($recipient->id, 0);
-        $readresult = core_message_external::get_notifications($recipient->id, 0, 'read', false, false, true, false, 0, 0);
-        $unreadresult = core_message_external::get_notifications($recipient->id, 0, 'unread', false, false, true, false, 0, 0);
+        $readresult = core_message_external::get_popup_notifications($recipient->id, 'read', false, false, false, true, false, 0, 0);
+        $unreadresult = core_message_external::get_popup_notifications($recipient->id, 'unread', false, false, false, true, false, 0, 0);
 
         $this->assertCount(6, $readresult['notifications']);
         $this->assertCount(0, $unreadresult['notifications']);
     }
 
 
         $this->assertCount(6, $readresult['notifications']);
         $this->assertCount(0, $unreadresult['notifications']);
     }
 
-    public function test_get_unread_notification_count_invalid_user_exception() {
+    public function test_get_unread_popup_notification_count_invalid_user_exception() {
         $this->resetAfterTest(true);
 
         $this->setExpectedException('moodle_exception');
         $this->resetAfterTest(true);
 
         $this->setExpectedException('moodle_exception');
-        $result = core_message_external::get_unread_notification_count(-2132131, 0);
+        $result = core_message_external::get_unread_popup_notification_count(-2132131, 0);
     }
 
     }
 
-    public function test_get_unread_notification_count_access_denied_exception() {
+    public function test_get_unread_popup_notification_count_access_denied_exception() {
         $this->resetAfterTest(true);
 
         $sender = $this->getDataGenerator()->create_user();
         $this->resetAfterTest(true);
 
         $sender = $this->getDataGenerator()->create_user();
@@ -1050,20 +1066,10 @@ class core_message_externallib_testcase extends externallib_advanced_testcase {
 
         $this->setUser($user);
         $this->setExpectedException('moodle_exception');
 
         $this->setUser($user);
         $this->setExpectedException('moodle_exception');
-        $result = core_message_external::get_unread_notification_count($sender->id, 0);
-    }
-
-    public function test_get_unread_notification_count_missing_from_user_exception() {
-        $this->resetAfterTest(true);
-
-        $sender = $this->getDataGenerator()->create_user();
-
-        $this->setUser($sender);
-        $this->setExpectedException('moodle_exception');
-        $result = core_message_external::get_unread_notification_count($sender->id, 99999);
+        $result = core_message_external::get_unread_popup_notification_count($sender->id, 0);
     }
 
     }
 
-    public function test_get_unread_notification_count() {
+    public function test_get_unread_popup_notification_count() {
         $this->resetAfterTest(true);
 
         $sender1 = $this->getDataGenerator()->create_user();
         $this->resetAfterTest(true);
 
         $sender1 = $this->getDataGenerator()->create_user();
@@ -1074,18 +1080,15 @@ class core_message_externallib_testcase extends externallib_advanced_testcase {
         $this->setUser($recipient);
 
         $notificationids = array(
         $this->setUser($recipient);
 
         $notificationids = array(
-            $this->send_fake_unread_notification($sender1, $recipient, 'Notification', 1),
-            $this->send_fake_unread_notification($sender1, $recipient, 'Notification', 2),
-            $this->send_fake_unread_notification($sender2, $recipient, 'Notification', 3),
-            $this->send_fake_unread_notification($sender2, $recipient, 'Notification', 4),
-            $this->send_fake_unread_notification($sender3, $recipient, 'Notification', 5),
-            $this->send_fake_unread_notification($sender3, $recipient, 'Notification', 6),
+            $this->send_fake_unread_popup_notification($sender1, $recipient, 'Notification', 1),
+            $this->send_fake_unread_popup_notification($sender1, $recipient, 'Notification', 2),
+            $this->send_fake_unread_popup_notification($sender2, $recipient, 'Notification', 3),
+            $this->send_fake_unread_popup_notification($sender2, $recipient, 'Notification', 4),
+            $this->send_fake_unread_popup_notification($sender3, $recipient, 'Notification', 5),
+            $this->send_fake_unread_popup_notification($sender3, $recipient, 'Notification', 6),
         );
 
         );
 
-        $count = core_message_external::get_unread_notification_count($recipient->id, $sender1->id);
-        $this->assertEquals($count, 2);
-
-        $count = core_message_external::get_unread_notification_count($recipient->id, 0);
+        $count = core_message_external::get_unread_popup_notification_count($recipient->id);
         $this->assertEquals($count, 6);
     }
 }
         $this->assertEquals($count, 6);
     }
 }