[ Index ] |
PHP Cross Reference of moodle-2.8 |
[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 <[email protected]> 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 // Now blocking a user that is not a contact. 123 $sink->clear(); 124 message_block_contact($user2->id); 125 $events = $sink->get_events(); 126 $event = reset($events); 127 128 // Check that the event data is valid. 129 $this->assertInstanceOf('\core\event\message_contact_blocked', $event); 130 $this->assertEquals(context_user::instance(2), $event->get_context()); 131 $expected = array(SITEID, 'message', 'block contact', 'index.php?user1=' . $user2->id . '&user2=2', $user2->id); 132 $this->assertEventLegacyLogData($expected, $event); 133 $url = new moodle_url('/message/index.php', array('user1' => $event->userid, 'user2' => $event->relateduserid)); 134 $this->assertEquals($url, $event->get_url()); 135 } 136 137 /** 138 * Test the message contact unblocked event. 139 */ 140 public function test_message_contact_unblocked() { 141 // Set this user as the admin. 142 $this->setAdminUser(); 143 144 // Create a user to add to the admin's contact list. 145 $user = $this->getDataGenerator()->create_user(); 146 147 // Add the user to the admin's contact list. 148 message_add_contact($user->id); 149 150 // Trigger and capture the event when unblocking a contact. 151 $sink = $this->redirectEvents(); 152 message_unblock_contact($user->id); 153 $events = $sink->get_events(); 154 $event = reset($events); 155 156 // Check that the event data is valid. 157 $this->assertInstanceOf('\core\event\message_contact_unblocked', $event); 158 $this->assertEquals(context_user::instance(2), $event->get_context()); 159 $expected = array(SITEID, 'message', 'unblock contact', 'index.php?user1=' . $user->id . '&user2=2', $user->id); 160 $this->assertEventLegacyLogData($expected, $event); 161 $url = new moodle_url('/message/index.php', array('user1' => $event->userid, 'user2' => $event->relateduserid)); 162 $this->assertEquals($url, $event->get_url()); 163 } 164 165 /** 166 * Test the message sent event. 167 * 168 * We can not use the message_send() function in the unit test to check that the event was fired as there is a 169 * conditional check to ensure a fake message is sent during unit tests when calling that particular function. 170 */ 171 public function test_message_sent() { 172 $event = \core\event\message_sent::create(array( 173 'userid' => 1, 174 'context' => context_system::instance(), 175 'relateduserid' => 2, 176 'other' => array( 177 'messageid' => 3 178 ) 179 )); 180 181 // Trigger and capturing the event. 182 $sink = $this->redirectEvents(); 183 $event->trigger(); 184 $events = $sink->get_events(); 185 $event = reset($events); 186 187 // Check that the event data is valid. 188 $this->assertInstanceOf('\core\event\message_sent', $event); 189 $this->assertEquals(context_system::instance(), $event->get_context()); 190 $expected = array(SITEID, 'message', 'write', 'index.php?user=1&id=2&history=1#m3', 1); 191 $this->assertEventLegacyLogData($expected, $event); 192 $url = new moodle_url('/message/index.php', array('user1' => $event->userid, 'user2' => $event->relateduserid)); 193 $this->assertEquals($url, $event->get_url()); 194 } 195 196 /** 197 * Test the message viewed event. 198 */ 199 public function test_message_viewed() { 200 global $DB; 201 202 // Create a message to mark as read. 203 $message = new stdClass(); 204 $message->useridfrom = '1'; 205 $message->useridto = '2'; 206 $message->subject = 'Subject'; 207 $message->message = 'Message'; 208 $message->id = $DB->insert_record('message', $message); 209 210 // Trigger and capture the event. 211 $sink = $this->redirectEvents(); 212 message_mark_message_read($message, time()); 213 $events = $sink->get_events(); 214 $event = reset($events); 215 216 // Check that the event data is valid. 217 $this->assertInstanceOf('\core\event\message_viewed', $event); 218 $this->assertEquals(context_user::instance(2), $event->get_context()); 219 $url = new moodle_url('/message/index.php', array('user1' => $event->userid, 'user2' => $event->relateduserid)); 220 $this->assertEquals($url, $event->get_url()); 221 } 222 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Fri Nov 28 20:29:05 2014 | Cross-referenced by PHPXref 0.7.1 |