[ Index ] |
PHP Cross Reference of Unnamed Project |
[Summary view] [Print] [Text view]
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 /** 18 * Events tests. 19 * 20 * @package core_message 21 * @category test 22 * @copyright 2014 Mark Nelson <markn@moodle.com> 23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 24 */ 25 26 defined('MOODLE_INTERNAL') || die(); 27 28 class core_message_events_testcase extends advanced_testcase { 29 30 /** 31 * Test set up. 32 * 33 * This is executed before running any test in this file. 34 */ 35 public function setUp() { 36 $this->resetAfterTest(); 37 } 38 39 /** 40 * Test the message contact added event. 41 */ 42 public function test_message_contact_added() { 43 // Set this user as the admin. 44 $this->setAdminUser(); 45 46 // Create a user to add to the admin's contact list. 47 $user = $this->getDataGenerator()->create_user(); 48 49 // Trigger and capture the event when adding a contact. 50 $sink = $this->redirectEvents(); 51 message_add_contact($user->id); 52 $events = $sink->get_events(); 53 $event = reset($events); 54 55 // Check that the event data is valid. 56 $this->assertInstanceOf('\core\event\message_contact_added', $event); 57 $this->assertEquals(context_user::instance(2), $event->get_context()); 58 $expected = array(SITEID, 'message', 'add contact', 'index.php?user1=' . $user->id . 59 '&user2=2', $user->id); 60 $this->assertEventLegacyLogData($expected, $event); 61 $url = new moodle_url('/message/index.php', array('user1' => $event->userid, 'user2' => $event->relateduserid)); 62 $this->assertEquals($url, $event->get_url()); 63 } 64 65 /** 66 * Test the message contact removed event. 67 */ 68 public function test_message_contact_removed() { 69 // Set this user as the admin. 70 $this->setAdminUser(); 71 72 // Create a user to add to the admin's contact list. 73 $user = $this->getDataGenerator()->create_user(); 74 75 // Add the user to the admin's contact list. 76 message_add_contact($user->id); 77 78 // Trigger and capture the event when adding a contact. 79 $sink = $this->redirectEvents(); 80 message_remove_contact($user->id); 81 $events = $sink->get_events(); 82 $event = reset($events); 83 84 // Check that the event data is valid. 85 $this->assertInstanceOf('\core\event\message_contact_removed', $event); 86 $this->assertEquals(context_user::instance(2), $event->get_context()); 87 $expected = array(SITEID, 'message', 'remove contact', 'index.php?user1=' . $user->id . 88 '&user2=2', $user->id); 89 $this->assertEventLegacyLogData($expected, $event); 90 $url = new moodle_url('/message/index.php', array('user1' => $event->userid, 'user2' => $event->relateduserid)); 91 $this->assertEquals($url, $event->get_url()); 92 } 93 94 /** 95 * Test the message contact blocked event. 96 */ 97 public function test_message_contact_blocked() { 98 // Set this user as the admin. 99 $this->setAdminUser(); 100 101 // Create a user to add to the admin's contact list. 102 $user = $this->getDataGenerator()->create_user(); 103 $user2 = $this->getDataGenerator()->create_user(); 104 105 // Add the user to the admin's contact list. 106 message_add_contact($user->id); 107 108 // Trigger and capture the event when blocking a contact. 109 $sink = $this->redirectEvents(); 110 message_block_contact($user->id); 111 $events = $sink->get_events(); 112 $event = reset($events); 113 114 // Check that the event data is valid. 115 $this->assertInstanceOf('\core\event\message_contact_blocked', $event); 116 $this->assertEquals(context_user::instance(2), $event->get_context()); 117 $expected = array(SITEID, 'message', 'block contact', 'index.php?user1=' . $user->id . '&user2=2', $user->id); 118 $this->assertEventLegacyLogData($expected, $event); 119 $url = new moodle_url('/message/index.php', array('user1' => $event->userid, 'user2' => $event->relateduserid)); 120 $this->assertEquals($url, $event->get_url()); 121 122 // Make sure that the contact blocked event is not triggered again. 123 $sink->clear(); 124 message_block_contact($user->id); 125 $events = $sink->get_events(); 126 $event = reset($events); 127 $this->assertEmpty($event); 128 // Make sure that we still have 1 blocked user. 129 $this->assertEquals(1, message_count_blocked_users()); 130 131 // Now blocking a user that is not a contact. 132 $sink->clear(); 133 message_block_contact($user2->id); 134 $events = $sink->get_events(); 135 $event = reset($events); 136 137 // Check that the event data is valid. 138 $this->assertInstanceOf('\core\event\message_contact_blocked', $event); 139 $this->assertEquals(context_user::instance(2), $event->get_context()); 140 $expected = array(SITEID, 'message', 'block contact', 'index.php?user1=' . $user2->id . '&user2=2', $user2->id); 141 $this->assertEventLegacyLogData($expected, $event); 142 $url = new moodle_url('/message/index.php', array('user1' => $event->userid, 'user2' => $event->relateduserid)); 143 $this->assertEquals($url, $event->get_url()); 144 } 145 146 /** 147 * Test the message contact unblocked event. 148 */ 149 public function test_message_contact_unblocked() { 150 // Set this user as the admin. 151 $this->setAdminUser(); 152 153 // Create a user to add to the admin's contact list. 154 $user = $this->getDataGenerator()->create_user(); 155 156 // Add the user to the admin's contact list. 157 message_add_contact($user->id); 158 159 // Block the user. 160 message_block_contact($user->id); 161 // Make sure that we have 1 blocked user. 162 $this->assertEquals(1, message_count_blocked_users()); 163 164 // Trigger and capture the event when unblocking a contact. 165 $sink = $this->redirectEvents(); 166 message_unblock_contact($user->id); 167 $events = $sink->get_events(); 168 $event = reset($events); 169 170 // Check that the event data is valid. 171 $this->assertInstanceOf('\core\event\message_contact_unblocked', $event); 172 $this->assertEquals(context_user::instance(2), $event->get_context()); 173 $expected = array(SITEID, 'message', 'unblock contact', 'index.php?user1=' . $user->id . '&user2=2', $user->id); 174 $this->assertEventLegacyLogData($expected, $event); 175 $url = new moodle_url('/message/index.php', array('user1' => $event->userid, 'user2' => $event->relateduserid)); 176 $this->assertEquals($url, $event->get_url()); 177 178 // Make sure that we have no blocked users. 179 $this->assertEmpty(message_count_blocked_users()); 180 181 // Make sure that the contact unblocked event is not triggered again. 182 $sink->clear(); 183 message_unblock_contact($user->id); 184 $events = $sink->get_events(); 185 $event = reset($events); 186 $this->assertEmpty($event); 187 188 // Make sure that we still have no blocked users. 189 $this->assertEmpty(message_count_blocked_users()); 190 } 191 192 /** 193 * Test the message sent event. 194 * 195 * We can not use the message_send() function in the unit test to check that the event was fired as there is a 196 * conditional check to ensure a fake message is sent during unit tests when calling that particular function. 197 */ 198 public function test_message_sent() { 199 $event = \core\event\message_sent::create(array( 200 'userid' => 1, 201 'context' => context_system::instance(), 202 'relateduserid' => 2, 203 'other' => array( 204 'messageid' => 3 205 ) 206 )); 207 208 // Trigger and capturing the event. 209 $sink = $this->redirectEvents(); 210 $event->trigger(); 211 $events = $sink->get_events(); 212 $event = reset($events); 213 214 // Check that the event data is valid. 215 $this->assertInstanceOf('\core\event\message_sent', $event); 216 $this->assertEquals(context_system::instance(), $event->get_context()); 217 $expected = array(SITEID, 'message', 'write', 'index.php?user=1&id=2&history=1#m3', 1); 218 $this->assertEventLegacyLogData($expected, $event); 219 $url = new moodle_url('/message/index.php', array('user1' => $event->userid, 'user2' => $event->relateduserid)); 220 $this->assertEquals($url, $event->get_url()); 221 } 222 223 /** 224 * Test the message viewed event. 225 */ 226 public function test_message_viewed() { 227 global $DB; 228 229 // Create a message to mark as read. 230 $message = new stdClass(); 231 $message->useridfrom = '1'; 232 $message->useridto = '2'; 233 $message->subject = 'Subject'; 234 $message->message = 'Message'; 235 $message->id = $DB->insert_record('message', $message); 236 237 // Trigger and capture the event. 238 $sink = $this->redirectEvents(); 239 message_mark_message_read($message, time()); 240 $events = $sink->get_events(); 241 $event = reset($events); 242 243 // Check that the event data is valid. 244 $this->assertInstanceOf('\core\event\message_viewed', $event); 245 $this->assertEquals(context_user::instance(2), $event->get_context()); 246 $url = new moodle_url('/message/index.php', array('user1' => $event->userid, 'user2' => $event->relateduserid)); 247 $this->assertEquals($url, $event->get_url()); 248 } 249 250 /** 251 * Test the message deleted event. 252 */ 253 public function test_message_deleted() { 254 global $DB; 255 256 // Create a message. 257 $message = new stdClass(); 258 $message->useridfrom = '1'; 259 $message->useridto = '2'; 260 $message->subject = 'Subject'; 261 $message->message = 'Message'; 262 $message->timeuserfromdeleted = 0; 263 $message->timeusertodeleted = 0; 264 $message->id = $DB->insert_record('message', $message); 265 266 // Trigger and capture the event. 267 $sink = $this->redirectEvents(); 268 message_delete_message($message, $message->useridfrom); 269 $events = $sink->get_events(); 270 $event = reset($events); 271 272 // Check that the event data is valid. 273 $this->assertInstanceOf('\core\event\message_deleted', $event); 274 $this->assertEquals($message->useridfrom, $event->userid); // The user who deleted it. 275 $this->assertEquals($message->useridto, $event->relateduserid); 276 $this->assertEquals('message', $event->other['messagetable']); 277 $this->assertEquals($message->id, $event->other['messageid']); 278 $this->assertEquals($message->useridfrom, $event->other['useridfrom']); 279 $this->assertEquals($message->useridto, $event->other['useridto']); 280 281 // Create a read message. 282 $message = new stdClass(); 283 $message->useridfrom = '2'; 284 $message->useridto = '1'; 285 $message->subject = 'Subject'; 286 $message->message = 'Message'; 287 $message->timeuserfromdeleted = 0; 288 $message->timeusertodeleted = 0; 289 $message->timeread = time(); 290 $message->id = $DB->insert_record('message_read', $message); 291 292 // Trigger and capture the event. 293 $sink = $this->redirectEvents(); 294 message_delete_message($message, $message->useridto); 295 $events = $sink->get_events(); 296 $event = reset($events); 297 298 // Check that the event data is valid. 299 $this->assertInstanceOf('\core\event\message_deleted', $event); 300 $this->assertEquals($message->useridto, $event->userid); 301 $this->assertEquals($message->useridfrom, $event->relateduserid); 302 $this->assertEquals('message_read', $event->other['messagetable']); 303 $this->assertEquals($message->id, $event->other['messageid']); 304 $this->assertEquals($message->useridfrom, $event->other['useridfrom']); 305 $this->assertEquals($message->useridto, $event->other['useridto']); 306 } 307 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Thu Aug 11 10:00:09 2016 | Cross-referenced by PHPXref 0.7.1 |