See Release Notes
Long Term Support Release
Differences Between: [Versions 310 and 401] [Versions 39 and 401] [Versions 401 and 402] [Versions 401 and 403]
1 <?php 2 // This file is part of Moodle - http://moodle.org/ 3 // 4 // Moodle is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // Moodle is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU General Public License for more details. 13 // 14 // You should have received a copy of the GNU General Public License 15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>. 16 17 namespace message_popup; 18 19 use message_popup_external; 20 use message_popup_test_helper; 21 22 defined('MOODLE_INTERNAL') || die(); 23 24 global $CFG; 25 26 require_once($CFG->dirroot . '/webservice/tests/helpers.php'); 27 require_once($CFG->dirroot . '/message/output/popup/externallib.php'); 28 require_once($CFG->dirroot . '/message/output/popup/tests/base.php'); 29 30 /** 31 * Class for external message popup functions unit tests. 32 * 33 * @package message_popup 34 * @copyright 2016 Ryan Wyllie <ryan@moodle.com> 35 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 36 */ 37 class externallib_test extends \advanced_testcase { 38 use message_popup_test_helper; 39 40 /** 41 * Test set up. 42 * 43 * This is executed before running any test in this file. 44 */ 45 public function setUp(): void { 46 $this->preventResetByRollback(); // Messaging is not compatible with transactions. 47 $this->messagesink = $this->redirectMessages(); 48 $this->resetAfterTest(); 49 } 50 51 /** 52 * Test that get_popup_notifications throws an exception if the user 53 * doesn't exist. 54 */ 55 public function test_get_popup_notifications_no_user_exception() { 56 $this->resetAfterTest(true); 57 58 $this->expectException('moodle_exception'); 59 $result = message_popup_external::get_popup_notifications(-2132131, false, 0, 0); 60 } 61 62 /** 63 * get_popup_notifications should throw exception if user isn't logged in 64 * user. 65 */ 66 public function test_get_popup_notifications_access_denied_exception() { 67 $this->resetAfterTest(true); 68 69 $sender = $this->getDataGenerator()->create_user(); 70 $user = $this->getDataGenerator()->create_user(); 71 72 $this->setUser($user); 73 $this->expectException('moodle_exception'); 74 $result = message_popup_external::get_popup_notifications($sender->id, false, 0, 0); 75 } 76 77 /** 78 * get_popup_notifications should return notifications for the recipient. 79 */ 80 public function test_get_popup_notifications_as_recipient() { 81 $this->resetAfterTest(true); 82 83 $sender = $this->getDataGenerator()->create_user(array('firstname' => 'Sendy', 'lastname' => 'Sender')); 84 $recipient = $this->getDataGenerator()->create_user(array('firstname' => 'Recipy', 'lastname' => 'Recipient')); 85 86 $notificationids = array( 87 $this->send_fake_unread_popup_notification($sender, $recipient), 88 $this->send_fake_unread_popup_notification($sender, $recipient), 89 $this->send_fake_read_popup_notification($sender, $recipient), 90 $this->send_fake_read_popup_notification($sender, $recipient), 91 ); 92 93 // Confirm that admin has super powers to retrieve any notifications. 94 $this->setAdminUser(); 95 $result = message_popup_external::get_popup_notifications($recipient->id, false, 0, 0); 96 $this->assertCount(4, $result['notifications']); 97 // Check we receive custom data as a unserialisable json. 98 $found = 0; 99 foreach ($result['notifications'] as $notification) { 100 if (!empty($notification->customdata)) { 101 $this->assertObjectHasAttribute('datakey', json_decode($notification->customdata)); 102 $found++; 103 } 104 } 105 $this->assertEquals(2, $found); 106 107 $this->setUser($recipient); 108 $result = message_popup_external::get_popup_notifications($recipient->id, false, 0, 0); 109 $this->assertCount(4, $result['notifications']); 110 } 111 112 /** 113 * get_popup_notifications result set should work with limit and offset. 114 */ 115 public function test_get_popup_notification_limit_offset() { 116 $this->resetAfterTest(true); 117 118 $sender = $this->getDataGenerator()->create_user(array('firstname' => 'Sendy', 'lastname' => 'Sender')); 119 $recipient = $this->getDataGenerator()->create_user(array('firstname' => 'Recipy', 'lastname' => 'Recipient')); 120 121 $this->setUser($recipient); 122 123 $notificationids = array( 124 $this->send_fake_unread_popup_notification($sender, $recipient, 'Notification 1', 1), 125 $this->send_fake_unread_popup_notification($sender, $recipient, 'Notification 2', 2), 126 $this->send_fake_unread_popup_notification($sender, $recipient, 'Notification 3', 3), 127 $this->send_fake_unread_popup_notification($sender, $recipient, 'Notification 4', 4), 128 $this->send_fake_read_popup_notification($sender, $recipient, 'Notification 5', 5), 129 $this->send_fake_read_popup_notification($sender, $recipient, 'Notification 6', 6), 130 $this->send_fake_read_popup_notification($sender, $recipient, 'Notification 7', 7), 131 $this->send_fake_read_popup_notification($sender, $recipient, 'Notification 8', 8), 132 ); 133 134 $result = message_popup_external::get_popup_notifications($recipient->id, true, 2, 0); 135 136 $this->assertEquals($result['notifications'][0]->id, $notificationids[7]); 137 $this->assertEquals($result['notifications'][1]->id, $notificationids[6]); 138 139 $result = message_popup_external::get_popup_notifications($recipient->id, true, 2, 2); 140 141 $this->assertEquals($result['notifications'][0]->id, $notificationids[5]); 142 $this->assertEquals($result['notifications'][1]->id, $notificationids[4]); 143 } 144 145 /** 146 * get_unread_popup_notification should throw an exception for an invalid user. 147 */ 148 public function test_get_unread_popup_notification_count_invalid_user_exception() { 149 $this->resetAfterTest(true); 150 151 $this->expectException('moodle_exception'); 152 $result = message_popup_external::get_unread_popup_notification_count(-2132131, 0); 153 } 154 155 /** 156 * get_unread_popup_notification_count should throw exception if being requested for 157 * non-logged in user. 158 */ 159 public function test_get_unread_popup_notification_count_access_denied_exception() { 160 $this->resetAfterTest(true); 161 162 $sender = $this->getDataGenerator()->create_user(); 163 $user = $this->getDataGenerator()->create_user(); 164 165 $this->setUser($user); 166 $this->expectException('moodle_exception'); 167 $result = message_popup_external::get_unread_popup_notification_count($sender->id, 0); 168 } 169 170 /** 171 * Test get_unread_popup_notification_count. 172 */ 173 public function test_get_unread_popup_notification_count() { 174 $this->resetAfterTest(true); 175 176 $sender1 = $this->getDataGenerator()->create_user(); 177 $sender2 = $this->getDataGenerator()->create_user(); 178 $sender3 = $this->getDataGenerator()->create_user(); 179 $recipient = $this->getDataGenerator()->create_user(); 180 181 $this->setUser($recipient); 182 183 $notificationids = array( 184 $this->send_fake_unread_popup_notification($sender1, $recipient, 'Notification', 1), 185 $this->send_fake_unread_popup_notification($sender1, $recipient, 'Notification', 2), 186 $this->send_fake_unread_popup_notification($sender2, $recipient, 'Notification', 3), 187 $this->send_fake_unread_popup_notification($sender2, $recipient, 'Notification', 4), 188 $this->send_fake_unread_popup_notification($sender3, $recipient, 'Notification', 5), 189 $this->send_fake_unread_popup_notification($sender3, $recipient, 'Notification', 6), 190 ); 191 192 $count = message_popup_external::get_unread_popup_notification_count($recipient->id); 193 $this->assertEquals($count, 6); 194 } 195 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body