2 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
20 * @package core_message
22 * @copyright 2014 Mark Nelson <markn@moodle.com>
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 defined('MOODLE_INTERNAL') ||
die();
28 class core_message_events_testcase
extends advanced_testcase
{
33 * This is executed before running any test in this file.
35 public function setUp() {
36 $this->resetAfterTest();
40 * Test the message contact added event.
42 public function test_message_contact_added() {
43 // Set this user as the admin.
44 $this->setAdminUser();
46 // Create a user to add to the admin's contact list.
47 $user = $this->getDataGenerator()->create_user();
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);
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());
66 * Test the message contact removed event.
68 public function test_message_contact_removed() {
69 // Set this user as the admin.
70 $this->setAdminUser();
72 // Create a user to add to the admin's contact list.
73 $user = $this->getDataGenerator()->create_user();
75 // Add the user to the admin's contact list.
76 message_add_contact($user->id
);
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);
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());
95 * Test the message contact blocked event.
97 public function test_message_contact_blocked() {
98 // Set this user as the admin.
99 $this->setAdminUser();
101 // Create a user to add to the admin's contact list.
102 $user = $this->getDataGenerator()->create_user();
103 $user2 = $this->getDataGenerator()->create_user();
105 // Add the user to the admin's contact list.
106 message_add_contact($user->id
);
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);
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());
122 // Now blocking a user that is not a contact.
124 message_block_contact($user2->id
);
125 $events = $sink->get_events();
126 $event = reset($events);
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());
138 * Test the message contact unblocked event.
140 public function test_message_contact_unblocked() {
141 // Set this user as the admin.
142 $this->setAdminUser();
144 // Create a user to add to the admin's contact list.
145 $user = $this->getDataGenerator()->create_user();
147 // Add the user to the admin's contact list.
148 message_add_contact($user->id
);
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);
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());
166 * Test the message sent event.
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.
171 public function test_message_sent() {
172 $event = \core\event\message_sent
::create(array(
174 'context' => context_system
::instance(),
175 'relateduserid' => 2,
181 // Trigger and capturing the event.
182 $sink = $this->redirectEvents();
184 $events = $sink->get_events();
185 $event = reset($events);
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());
197 * Test the message viewed event.
199 public function test_message_viewed() {
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);
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);
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());
224 * Test the message deleted event.
226 public function test_message_deleted() {
230 $message = new stdClass();
231 $message->useridfrom
= '1';
232 $message->useridto
= '2';
233 $message->subject
= 'Subject';
234 $message->message
= 'Message';
235 $message->timeuserfromdeleted
= 0;
236 $message->timeusertodeleted
= 0;
237 $message->id
= $DB->insert_record('message', $message);
239 // Trigger and capture the event.
240 $sink = $this->redirectEvents();
241 message_delete_message($message, $message->useridfrom
);
242 $events = $sink->get_events();
243 $event = reset($events);
245 // Check that the event data is valid.
246 $this->assertInstanceOf('\core\event\message_deleted', $event);
247 $this->assertEquals($message->useridfrom
, $event->userid
); // The user who deleted it.
248 $this->assertEquals($message->useridto
, $event->relateduserid
);
249 $this->assertEquals('message', $event->other
['messagetable']);
250 $this->assertEquals($message->id
, $event->other
['messageid']);
251 $this->assertEquals($message->useridfrom
, $event->other
['useridfrom']);
252 $this->assertEquals($message->useridto
, $event->other
['useridto']);
254 // Create a read message.
255 $message = new stdClass();
256 $message->useridfrom
= '2';
257 $message->useridto
= '1';
258 $message->subject
= 'Subject';
259 $message->message
= 'Message';
260 $message->timeuserfromdeleted
= 0;
261 $message->timeusertodeleted
= 0;
262 $message->timeread
= time();
263 $message->id
= $DB->insert_record('message_read', $message);
265 // Trigger and capture the event.
266 $sink = $this->redirectEvents();
267 message_delete_message($message, $message->useridto
);
268 $events = $sink->get_events();
269 $event = reset($events);
271 // Check that the event data is valid.
272 $this->assertInstanceOf('\core\event\message_deleted', $event);
273 $this->assertEquals($message->useridto
, $event->userid
);
274 $this->assertEquals($message->useridfrom
, $event->relateduserid
);
275 $this->assertEquals('message_read', $event->other
['messagetable']);
276 $this->assertEquals($message->id
, $event->other
['messageid']);
277 $this->assertEquals($message->useridfrom
, $event->other
['useridfrom']);
278 $this->assertEquals($message->useridto
, $event->other
['useridto']);