MDL-63692 core_message: Fix the context for favourite conversations
[moodle.git] / message / tests / privacy_provider_test.php
CommitLineData
04cd1b3f
MN
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 * Privacy provider tests.
19 *
20 * @package core_message
21 * @copyright 2018 Mark Nelson <markn@moodle.com>
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
24
25use core_privacy\local\metadata\collection;
26use core_message\privacy\provider;
201cebea 27use \core_privacy\local\request\contextlist;
04cd1b3f
MN
28use \core_privacy\local\request\writer;
29use \core_privacy\local\request\transform;
201cebea 30use \core_message\tests\helper as testhelper;
04cd1b3f
MN
31
32defined('MOODLE_INTERNAL') || die();
33
34/**
35 * Privacy provider tests class.
36 *
37 * @package core_message
38 * @copyright 2018 Mark Nelson <markn@moodle.com>
39 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
40 */
41class core_message_privacy_provider_testcase extends \core_privacy\tests\provider_testcase {
42
43 /**
44 * Test for provider::get_metadata().
45 */
46 public function test_get_metadata() {
47 $collection = new collection('core_message');
48 $newcollection = provider::get_metadata($collection);
49 $itemcollection = $newcollection->get_collection();
742c27b3 50 $this->assertCount(8, $itemcollection);
04cd1b3f
MN
51
52 $messagestable = array_shift($itemcollection);
53 $this->assertEquals('messages', $messagestable->get_name());
54
55 $messageuseractionstable = array_shift($itemcollection);
56 $this->assertEquals('message_user_actions', $messageuseractionstable->get_name());
57
58 $messageconversationmemberstable = array_shift($itemcollection);
59 $this->assertEquals('message_conversation_members', $messageconversationmemberstable->get_name());
60
61 $messagecontacts = array_shift($itemcollection);
62 $this->assertEquals('message_contacts', $messagecontacts->get_name());
63
742c27b3
MN
64 $messagecontactrequests = array_shift($itemcollection);
65 $this->assertEquals('message_contact_requests', $messagecontactrequests->get_name());
66
67 $messageusersblocked = array_shift($itemcollection);
68 $this->assertEquals('message_users_blocked', $messageusersblocked->get_name());
69
04cd1b3f
MN
70 $notificationstable = array_shift($itemcollection);
71 $this->assertEquals('notifications', $notificationstable->get_name());
72
73 $usersettings = array_shift($itemcollection);
74 $this->assertEquals('core_message_messageprovider_settings', $usersettings->get_name());
75
76 $privacyfields = $messagestable->get_privacy_fields();
77 $this->assertArrayHasKey('useridfrom', $privacyfields);
78 $this->assertArrayHasKey('conversationid', $privacyfields);
79 $this->assertArrayHasKey('subject', $privacyfields);
80 $this->assertArrayHasKey('fullmessage', $privacyfields);
81 $this->assertArrayHasKey('fullmessageformat', $privacyfields);
82 $this->assertArrayHasKey('fullmessagehtml', $privacyfields);
83 $this->assertArrayHasKey('smallmessage', $privacyfields);
84 $this->assertArrayHasKey('timecreated', $privacyfields);
85 $this->assertEquals('privacy:metadata:messages', $messagestable->get_summary());
86
87 $privacyfields = $messageuseractionstable->get_privacy_fields();
88 $this->assertArrayHasKey('userid', $privacyfields);
89 $this->assertArrayHasKey('messageid', $privacyfields);
90 $this->assertArrayHasKey('action', $privacyfields);
91 $this->assertArrayHasKey('timecreated', $privacyfields);
92 $this->assertEquals('privacy:metadata:message_user_actions', $messageuseractionstable->get_summary());
93
94 $privacyfields = $messageconversationmemberstable->get_privacy_fields();
95 $this->assertArrayHasKey('conversationid', $privacyfields);
96 $this->assertArrayHasKey('userid', $privacyfields);
97 $this->assertArrayHasKey('timecreated', $privacyfields);
98 $this->assertEquals('privacy:metadata:message_conversation_members', $messageconversationmemberstable->get_summary());
99
100 $privacyfields = $messagecontacts->get_privacy_fields();
101 $this->assertArrayHasKey('userid', $privacyfields);
102 $this->assertArrayHasKey('contactid', $privacyfields);
742c27b3 103 $this->assertArrayHasKey('timecreated', $privacyfields);
04cd1b3f
MN
104 $this->assertEquals('privacy:metadata:message_contacts', $messagecontacts->get_summary());
105
742c27b3
MN
106 $privacyfields = $messagecontactrequests->get_privacy_fields();
107 $this->assertArrayHasKey('userid', $privacyfields);
108 $this->assertArrayHasKey('requesteduserid', $privacyfields);
109 $this->assertArrayHasKey('timecreated', $privacyfields);
110 $this->assertEquals('privacy:metadata:message_contact_requests', $messagecontactrequests->get_summary());
111
112 $privacyfields = $messageusersblocked->get_privacy_fields();
113 $this->assertArrayHasKey('userid', $privacyfields);
114 $this->assertArrayHasKey('blockeduserid', $privacyfields);
115 $this->assertArrayHasKey('timecreated', $privacyfields);
116 $this->assertEquals('privacy:metadata:message_users_blocked', $messageusersblocked->get_summary());
117
04cd1b3f
MN
118 $privacyfields = $notificationstable->get_privacy_fields();
119 $this->assertArrayHasKey('useridfrom', $privacyfields);
120 $this->assertArrayHasKey('useridto', $privacyfields);
121 $this->assertArrayHasKey('subject', $privacyfields);
122 $this->assertArrayHasKey('fullmessage', $privacyfields);
123 $this->assertArrayHasKey('fullmessageformat', $privacyfields);
124 $this->assertArrayHasKey('fullmessagehtml', $privacyfields);
125 $this->assertArrayHasKey('smallmessage', $privacyfields);
126 $this->assertArrayHasKey('component', $privacyfields);
127 $this->assertArrayHasKey('eventtype', $privacyfields);
128 $this->assertArrayHasKey('contexturl', $privacyfields);
129 $this->assertArrayHasKey('contexturlname', $privacyfields);
130 $this->assertArrayHasKey('timeread', $privacyfields);
131 $this->assertArrayHasKey('timecreated', $privacyfields);
132 $this->assertEquals('privacy:metadata:notifications', $notificationstable->get_summary());
133 }
134
135 /**
136 * Test for provider::export_user_preferences().
137 */
138 public function test_export_user_preferences_no_pref() {
139 $this->resetAfterTest();
140
141 $user = $this->getDataGenerator()->create_user();
142 provider::export_user_preferences($user->id);
143
144 $writer = writer::with_context(\context_system::instance());
145
146 $this->assertFalse($writer->has_any_data());
147 }
148
149 /**
150 * Test for provider::export_user_preferences().
151 */
152 public function test_export_user_preferences() {
153 global $USER;
154
155 $this->resetAfterTest();
156
157 $this->setAdminUser();
158
159 // Create another user to set a preference for who we won't be exporting.
160 $user = $this->getDataGenerator()->create_user();
161
162 // Set some message user preferences.
163 set_user_preference('message_provider_moodle_instantmessage_loggedin', 'airnotifier', $USER->id);
164 set_user_preference('message_provider_moodle_instantmessage_loggedoff', 'popup', $USER->id);
f7dfa9ba 165 set_user_preference('message_blocknoncontacts', \core_message\api::MESSAGE_PRIVACY_ONLYCONTACTS, $USER->id);
04cd1b3f
MN
166 set_user_preference('message_provider_moodle_instantmessage_loggedoff', 'inbound', $user->id);
167
168 // Set an unrelated preference.
bbe6eadd 169 set_user_preference('some_unrelated_preference', 'courses', $USER->id);
04cd1b3f
MN
170
171 provider::export_user_preferences($USER->id);
172
173 $writer = writer::with_context(\context_system::instance());
174
175 $this->assertTrue($writer->has_any_data());
176
177 $prefs = (array) $writer->get_user_preferences('core_message');
178
179 // Check only 3 preferences exist.
180 $this->assertCount(3, $prefs);
181 $this->assertArrayHasKey('message_provider_moodle_instantmessage_loggedin', $prefs);
182 $this->assertArrayHasKey('message_provider_moodle_instantmessage_loggedoff', $prefs);
183 $this->assertArrayHasKey('message_blocknoncontacts', $prefs);
184
185 foreach ($prefs as $key => $pref) {
186 if ($key == 'message_provider_moodle_instantmessage_loggedin') {
187 $this->assertEquals('airnotifier', $pref->value);
188 } else if ($key == 'message_provider_moodle_instantmessage_loggedoff') {
189 $this->assertEquals('popup', $pref->value);
190 } else {
191 $this->assertEquals(1, $pref->value);
192 }
193 }
194 }
195
196 /**
52334296 197 * Test for provider::get_contexts_for_userid() when there is no message or notification.
04cd1b3f 198 */
52334296 199 public function test_get_contexts_for_userid_no_data() {
04cd1b3f
MN
200 $this->resetAfterTest();
201
202 $user = $this->getDataGenerator()->create_user();
203 $contextlist = provider::get_contexts_for_userid($user->id);
52334296
SR
204 $this->assertEmpty($contextlist);
205 }
206
207 /**
201cebea 208 * Test for provider::get_contexts_for_userid() when there is a private message between users.
52334296 209 */
201cebea 210 public function test_get_contexts_for_userid_with_private_messages() {
52334296
SR
211 $this->resetAfterTest();
212
213 $user1 = $this->getDataGenerator()->create_user();
214 $user2 = $this->getDataGenerator()->create_user();
201cebea 215 $user3 = $this->getDataGenerator()->create_user();
52334296 216
201cebea 217 // Test nothing is found before group conversations is created or message is sent.
b8bbef0a
MH
218 $contextlist = provider::get_contexts_for_userid($user1->id);
219 $this->assertCount(0, $contextlist);
220 $contextlist = provider::get_contexts_for_userid($user2->id);
221 $this->assertCount(0, $contextlist);
222
201cebea
SA
223 // Send some private messages.
224 $pm1id = $this->create_message($user1->id, $user2->id, time() - (9 * DAYSECS));
52334296 225
201cebea 226 // Test for the sender (user1).
52334296
SR
227 $contextlist = provider::get_contexts_for_userid($user1->id);
228 $this->assertCount(1, $contextlist);
229 $contextforuser = $contextlist->current();
230 $this->assertEquals(
201cebea 231 \context_user::instance($user1->id)->id,
52334296
SR
232 $contextforuser->id);
233
201cebea 234 // Test for the receiver (user2).
52334296
SR
235 $contextlist = provider::get_contexts_for_userid($user2->id);
236 $this->assertCount(1, $contextlist);
237 $contextforuser = $contextlist->current();
238 $this->assertEquals(
201cebea
SA
239 \context_user::instance($user2->id)->id,
240 $contextforuser->id);
241
242 // Test for user3 (no private messages).
243 $contextlist = provider::get_contexts_for_userid($user3->id);
244 $this->assertCount(0, $contextlist);
245 }
246
247 /**
248 * Test for provider::get_contexts_for_userid() when there is several messages (private and group).
249 */
250 public function test_get_contexts_for_userid_with_messages() {
251 $this->resetAfterTest();
252 $this->setAdminUser();
253
254 $user1 = $this->getDataGenerator()->create_user();
255 $user2 = $this->getDataGenerator()->create_user();
256 $user3 = $this->getDataGenerator()->create_user();
257 $user4 = $this->getDataGenerator()->create_user();
258
259 // Test nothing is found before group conversations is created or message is sent.
260 $contextlist = provider::get_contexts_for_userid($user1->id);
261 $this->assertCount(0, $contextlist);
262 $contextlist = provider::get_contexts_for_userid($user2->id);
263 $this->assertCount(0, $contextlist);
264
265 // Create course.
266 $course1 = $this->getDataGenerator()->create_course();
267 $coursecontext1 = context_course::instance($course1->id);
268
269 // Enrol users to courses.
270 $this->getDataGenerator()->enrol_user($user1->id, $course1->id);
271 $this->getDataGenerator()->enrol_user($user2->id, $course1->id);
272 $this->getDataGenerator()->enrol_user($user3->id, $course1->id);
273
274 // Create groups (only one with enablemessaging = 1).
275 $group1a = $this->getDataGenerator()->create_group(array('courseid' => $course1->id, 'enablemessaging' => 1));
276
277 // Add users to groups.
278 $this->getDataGenerator()->create_group_member(array('groupid' => $group1a->id, 'userid' => $user1->id));
279 $this->getDataGenerator()->create_group_member(array('groupid' => $group1a->id, 'userid' => $user2->id));
280 $this->getDataGenerator()->create_group_member(array('groupid' => $group1a->id, 'userid' => $user3->id));
281
282 // Get conversation.
283 $component = 'core_group';
284 $itemtype = 'groups';
285 $conversation1 = \core_message\api::get_conversation_by_area(
286 $component,
287 $itemtype,
288 $group1a->id,
289 $coursecontext1->id
290 );
291
292 // Send some messages to the group conversation.
293 $now = time();
294 $m1id = testhelper::send_fake_message_to_conversation($user1, $conversation1->id, 'Message 1', $now + 1);
295 $m2id = testhelper::send_fake_message_to_conversation($user1, $conversation1->id, 'Message 2', $now + 2);
296 $m3id = testhelper::send_fake_message_to_conversation($user2, $conversation1->id, 'Message 3', $now + 3);
297
298 // Test for user1 (although is member of the conversation, hasn't any private message).
299 $contextlist = provider::get_contexts_for_userid($user1->id);
300 $this->assertCount(0, $contextlist);
301
302 // Test for user2 (although is member of the conversation, hasn't any private message).
303 $contextlist = provider::get_contexts_for_userid($user2->id);
304 $this->assertCount(0, $contextlist);
305
306 // Test for user3 (although is member of the conversation, hasn't any private message).
307 $contextlist = provider::get_contexts_for_userid($user3->id);
308 $this->assertCount(0, $contextlist);
309
310 // Test for user4 (doesn't belong to the conversation).
311 $contextlist = provider::get_contexts_for_userid($user4->id);
312 $this->assertCount(0, $contextlist);
313
314 // Send some private messages.
315 $pm1id = $this->create_message($user1->id, $user2->id, time() - (9 * DAYSECS));
316
317 // Test user1 now has the user context because of the private message.
318 $contextlist = provider::get_contexts_for_userid($user1->id);
319 $this->assertCount(1, $contextlist);
320 $contextforuser = $contextlist->current();
321 $this->assertEquals(
322 \context_user::instance($user1->id)->id,
323 $contextforuser->id);
324
325 // Test user2 now has the user context because of the private message.
326 $contextlist = provider::get_contexts_for_userid($user2->id);
327 $this->assertCount(1, $contextlist);
328 $contextforuser = $contextlist->current();
329 $this->assertEquals(
330 \context_user::instance($user2->id)->id,
52334296 331 $contextforuser->id);
201cebea
SA
332
333 // Test for user3 (although is member of the conversation, hasn't still any private message).
334 $contextlist = provider::get_contexts_for_userid($user3->id);
335 $this->assertCount(0, $contextlist);
336
337 // Test for user4 (doesn't belong to the conversation and hasn't any private message).
338 $contextlist = provider::get_contexts_for_userid($user4->id);
339 $this->assertCount(0, $contextlist);
52334296
SR
340 }
341
342 /**
343 * Test for provider::get_contexts_for_userid() when there is a notification between users.
344 */
345 public function test_get_contexts_for_userid_with_notification() {
346 $this->resetAfterTest();
347
348 $user1 = $this->getDataGenerator()->create_user();
349 $user2 = $this->getDataGenerator()->create_user();
350
b8bbef0a
MH
351 // Test nothing is found before notification is created.
352 $contextlist = provider::get_contexts_for_userid($user1->id);
353 $this->assertCount(0, $contextlist);
354 $contextlist = provider::get_contexts_for_userid($user2->id);
355 $this->assertCount(0, $contextlist);
356
52334296
SR
357 $this->create_notification($user1->id, $user2->id, time() - (9 * DAYSECS));
358
359 // Test for the sender.
360 $contextlist = provider::get_contexts_for_userid($user1->id);
361 $this->assertCount(1, $contextlist);
362 $contextforuser = $contextlist->current();
363 $this->assertEquals(
364 context_user::instance($user1->id)->id,
365 $contextforuser->id);
366
367 // Test for the receiver.
368 $contextlist = provider::get_contexts_for_userid($user2->id);
04cd1b3f
MN
369 $this->assertCount(1, $contextlist);
370 $contextforuser = $contextlist->current();
52334296
SR
371 $this->assertEquals(
372 context_user::instance($user2->id)->id,
373 $contextforuser->id);
04cd1b3f
MN
374 }
375
b8bbef0a
MH
376 /**
377 * Test for provider::get_contexts_for_userid() when a users has a contact.
378 */
379 public function test_get_contexts_for_userid_with_contact() {
380 $this->resetAfterTest();
381
382 $user1 = $this->getDataGenerator()->create_user();
383 $user2 = $this->getDataGenerator()->create_user();
384
385 // Test nothing is found before contact is created.
386 $contextlist = provider::get_contexts_for_userid($user1->id);
387 $this->assertCount(0, $contextlist);
388 $contextlist = provider::get_contexts_for_userid($user2->id);
389 $this->assertCount(0, $contextlist);
390
391 \core_message\api::add_contact($user1->id, $user2->id);
392
393 // Test for the user adding the contact.
394 $contextlist = provider::get_contexts_for_userid($user1->id);
395 $this->assertCount(1, $contextlist);
396 $contextforuser = $contextlist->current();
397 $this->assertEquals(
398 context_user::instance($user1->id)->id,
399 $contextforuser->id);
400
401 // Test for the user who is the contact.
402 $contextlist = provider::get_contexts_for_userid($user2->id);
403 $this->assertCount(1, $contextlist);
404 $contextforuser = $contextlist->current();
405 $this->assertEquals(
406 context_user::instance($user2->id)->id,
407 $contextforuser->id);
408 }
409
410 /**
411 * Test for provider::get_contexts_for_userid() when a user makes a contact request.
412 */
413 public function test_get_contexts_for_userid_with_contact_request() {
414 $this->resetAfterTest();
415
416 $user1 = $this->getDataGenerator()->create_user();
417 $user2 = $this->getDataGenerator()->create_user();
418
419 // Test nothing is found before request is created.
420 $contextlist = provider::get_contexts_for_userid($user1->id);
421 $this->assertCount(0, $contextlist);
422 $contextlist = provider::get_contexts_for_userid($user2->id);
423 $this->assertCount(0, $contextlist);
424
425 \core_message\api::create_contact_request($user1->id, $user2->id);
426
427 // Test for the user requesting the contact.
428 $contextlist = provider::get_contexts_for_userid($user1->id);
429 $this->assertCount(1, $contextlist);
430 $contextforuser = $contextlist->current();
431 $this->assertEquals(
432 context_user::instance($user1->id)->id,
433 $contextforuser->id);
434
435 // Test for the user receiving the contact request.
436 $contextlist = provider::get_contexts_for_userid($user2->id);
437 $this->assertCount(1, $contextlist);
438 $contextforuser = $contextlist->current();
439 $this->assertEquals(
440 context_user::instance($user2->id)->id,
441 $contextforuser->id);
442 }
443
444 /**
445 * Test for provider::get_contexts_for_userid() when a user is blocked.
446 */
447 public function test_get_contexts_for_userid_with_blocked_contact() {
448 $this->resetAfterTest();
449
450 $user1 = $this->getDataGenerator()->create_user();
451 $user2 = $this->getDataGenerator()->create_user();
452
453 // Test nothing is found before user is blocked.
454 $contextlist = provider::get_contexts_for_userid($user1->id);
455 $this->assertCount(0, $contextlist);
456 $contextlist = provider::get_contexts_for_userid($user2->id);
457 $this->assertCount(0, $contextlist);
458
459 \core_message\api::block_user($user1->id, $user2->id);
460
461 // Test for the blocking user.
462 $contextlist = provider::get_contexts_for_userid($user1->id);
463 $this->assertCount(1, $contextlist);
464 $contextforuser = $contextlist->current();
465 $this->assertEquals(
466 context_user::instance($user1->id)->id,
467 $contextforuser->id);
468
469 // Test for the user who is blocked.
470 $contextlist = provider::get_contexts_for_userid($user2->id);
471 $this->assertCount(1, $contextlist);
472 $contextforuser = $contextlist->current();
473 $this->assertEquals(
474 context_user::instance($user2->id)->id,
475 $contextforuser->id);
476 }
477
04cd1b3f
MN
478 /**
479 * Test for provider::export_user_data().
480 */
481 public function test_export_for_context_with_contacts() {
482 $this->resetAfterTest();
483
484 // Create users to test with.
485 $user1 = $this->getDataGenerator()->create_user();
486 $user2 = $this->getDataGenerator()->create_user();
487 $user3 = $this->getDataGenerator()->create_user();
488 $user4 = $this->getDataGenerator()->create_user();
489
742c27b3
MN
490 \core_message\api::add_contact($user1->id, $user2->id);
491 \core_message\api::add_contact($user1->id, $user3->id);
492 \core_message\api::add_contact($user1->id, $user4->id);
04cd1b3f 493
52334296 494 $user1context = context_user::instance($user1->id);
04cd1b3f 495
52334296
SR
496 $this->export_context_data_for_user($user1->id, $user1context, 'core_message');
497
498 $writer = writer::with_context($user1context);
04cd1b3f
MN
499
500 $contacts = (array) $writer->get_data([get_string('contacts', 'core_message')]);
823e08bf 501 usort($contacts, ['static', 'sort_contacts']);
04cd1b3f
MN
502
503 $this->assertCount(3, $contacts);
504
505 $contact1 = array_shift($contacts);
506 $this->assertEquals($user2->id, $contact1->contact);
04cd1b3f
MN
507
508 $contact2 = array_shift($contacts);
509 $this->assertEquals($user3->id, $contact2->contact);
04cd1b3f
MN
510
511 $contact3 = array_shift($contacts);
512 $this->assertEquals($user4->id, $contact3->contact);
742c27b3
MN
513 }
514
515 /**
516 * Test for provider::export_user_data().
517 */
518 public function test_export_for_context_with_contact_requests() {
519 $this->resetAfterTest();
520
521 // Create users to test with.
522 $user1 = $this->getDataGenerator()->create_user();
523 $user2 = $this->getDataGenerator()->create_user();
524 $user3 = $this->getDataGenerator()->create_user();
525 $user4 = $this->getDataGenerator()->create_user();
526
527 \core_message\api::create_contact_request($user1->id, $user2->id);
528 \core_message\api::create_contact_request($user3->id, $user1->id);
529 \core_message\api::create_contact_request($user1->id, $user4->id);
530
52334296 531 $user1context = context_user::instance($user1->id);
742c27b3 532
52334296
SR
533 $this->export_context_data_for_user($user1->id, $user1context, 'core_message');
534
535 $writer = writer::with_context($user1context);
742c27b3
MN
536
537 $contactrequests = (array) $writer->get_data([get_string('contactrequests', 'core_message')]);
538
539 $this->assertCount(3, $contactrequests);
540
541 $contactrequest1 = array_shift($contactrequests);
542 $this->assertEquals($user2->id, $contactrequest1->contactrequest);
543 $this->assertEquals(get_string('yes'), $contactrequest1->maderequest);
544
545 $contactrequest2 = array_shift($contactrequests);
546 $this->assertEquals($user3->id, $contactrequest2->contactrequest);
547 $this->assertEquals(get_string('no'), $contactrequest2->maderequest);
548
549 $contactrequest3 = array_shift($contactrequests);
550 $this->assertEquals($user4->id, $contactrequest3->contactrequest);
551 $this->assertEquals(get_string('yes'), $contactrequest3->maderequest);
552 }
553
554 /**
555 * Test for provider::export_user_data().
556 */
557 public function test_export_for_context_with_blocked_users() {
558 $this->resetAfterTest();
559
560 // Create users to test with.
561 $user1 = $this->getDataGenerator()->create_user();
562 $user2 = $this->getDataGenerator()->create_user();
563 $user3 = $this->getDataGenerator()->create_user();
564 $user4 = $this->getDataGenerator()->create_user();
565
566 \core_message\api::block_user($user1->id, $user2->id);
567 \core_message\api::block_user($user1->id, $user3->id);
568 \core_message\api::block_user($user1->id, $user4->id);
569
52334296 570 $user1context = context_user::instance($user1->id);
742c27b3 571
52334296
SR
572 $this->export_context_data_for_user($user1->id, $user1context, 'core_message');
573
574 $writer = writer::with_context($user1context);
742c27b3
MN
575
576 $blockedusers = (array) $writer->get_data([get_string('blockedusers', 'core_message')]);
577
578 $this->assertCount(3, $blockedusers);
579
580 $blockeduser1 = array_shift($blockedusers);
581 $this->assertEquals($user2->id, $blockeduser1->blockeduser);
582
583 $blockeduser2 = array_shift($blockedusers);
584 $this->assertEquals($user3->id, $blockeduser2->blockeduser);
585
586 $blockeduser3 = array_shift($blockedusers);
587 $this->assertEquals($user4->id, $blockeduser3->blockeduser);
04cd1b3f
MN
588 }
589
590 /**
591 * Test for provider::export_user_data().
592 */
201cebea 593 public function test_export_for_context_with_private_messages() {
04cd1b3f
MN
594 global $DB;
595
596 $this->resetAfterTest();
597
598 // Create users to test with.
599 $user1 = $this->getDataGenerator()->create_user();
600 $user2 = $this->getDataGenerator()->create_user();
601 $user3 = $this->getDataGenerator()->create_user();
602
603 $now = time();
604
605 // Send messages from user 1 to user 2.
606 $m1 = $this->create_message($user1->id, $user2->id, $now - (9 * DAYSECS), true);
607 $m2 = $this->create_message($user2->id, $user1->id, $now - (8 * DAYSECS));
608 $m3 = $this->create_message($user1->id, $user2->id, $now - (7 * DAYSECS));
609
610 // Send messages from user 3 to user 1.
611 $m4 = $this->create_message($user3->id, $user1->id, $now - (6 * DAYSECS), true);
612 $m5 = $this->create_message($user1->id, $user3->id, $now - (5 * DAYSECS));
613 $m6 = $this->create_message($user3->id, $user1->id, $now - (4 * DAYSECS));
614
615 // Send messages from user 3 to user 2 - these should not be included in the export.
616 $m7 = $this->create_message($user3->id, $user2->id, $now - (3 * DAYSECS), true);
617 $m8 = $this->create_message($user2->id, $user3->id, $now - (2 * DAYSECS));
618 $m9 = $this->create_message($user3->id, $user2->id, $now - (1 * DAYSECS));
619
620 // Mark message 2 and 5 as deleted.
621 \core_message\api::delete_message($user1->id, $m2);
622 \core_message\api::delete_message($user1->id, $m5);
623
52334296 624 $user1context = context_user::instance($user1->id);
04cd1b3f 625
52334296
SR
626 $this->export_context_data_for_user($user1->id, $user1context, 'core_message');
627
628 $writer = writer::with_context($user1context);
04cd1b3f
MN
629
630 $this->assertTrue($writer->has_any_data());
631
632 // Confirm the messages with user 2 are correct.
201cebea 633 $messages = (array) $writer->get_data([get_string('messages', 'core_message'), $user2->id]);
04cd1b3f
MN
634 $this->assertCount(3, $messages);
635
636 $dbm1 = $DB->get_record('messages', ['id' => $m1]);
637 $dbm2 = $DB->get_record('messages', ['id' => $m2]);
638 $dbm3 = $DB->get_record('messages', ['id' => $m3]);
639
823e08bf 640 usort($messages, ['static', 'sort_messages']);
04cd1b3f
MN
641 $m1 = array_shift($messages);
642 $m2 = array_shift($messages);
643 $m3 = array_shift($messages);
644
201cebea 645 $this->assertEquals(get_string('yes'), $m1->issender);
04cd1b3f
MN
646 $this->assertEquals(message_format_message_text($dbm1), $m1->message);
647 $this->assertEquals(transform::datetime($now - (9 * DAYSECS)), $m1->timecreated);
201cebea 648 $this->assertEquals('-', $m1->timeread);
04cd1b3f
MN
649 $this->assertArrayNotHasKey('timedeleted', (array) $m1);
650
201cebea 651 $this->assertEquals(get_string('no'), $m2->issender);
04cd1b3f
MN
652 $this->assertEquals(message_format_message_text($dbm2), $m2->message);
653 $this->assertEquals(transform::datetime($now - (8 * DAYSECS)), $m2->timecreated);
654 $this->assertEquals('-', $m2->timeread);
655 $this->assertArrayHasKey('timedeleted', (array) $m2);
656
201cebea 657 $this->assertEquals(get_string('yes'), $m3->issender);
04cd1b3f
MN
658 $this->assertEquals(message_format_message_text($dbm3), $m3->message);
659 $this->assertEquals(transform::datetime($now - (7 * DAYSECS)), $m3->timecreated);
660 $this->assertEquals('-', $m3->timeread);
661
662 // Confirm the messages with user 3 are correct.
201cebea 663 $messages = (array) $writer->get_data([get_string('messages', 'core_message'), $user3->id]);
04cd1b3f
MN
664 $this->assertCount(3, $messages);
665
666 $dbm4 = $DB->get_record('messages', ['id' => $m4]);
667 $dbm5 = $DB->get_record('messages', ['id' => $m5]);
668 $dbm6 = $DB->get_record('messages', ['id' => $m6]);
669
823e08bf 670 usort($messages, ['static', 'sort_messages']);
04cd1b3f
MN
671 $m4 = array_shift($messages);
672 $m5 = array_shift($messages);
673 $m6 = array_shift($messages);
674
201cebea 675 $this->assertEquals(get_string('no'), $m4->issender);
04cd1b3f
MN
676 $this->assertEquals(message_format_message_text($dbm4), $m4->message);
677 $this->assertEquals(transform::datetime($now - (6 * DAYSECS)), $m4->timecreated);
678 $this->assertNotEquals('-', $m4->timeread);
679 $this->assertArrayNotHasKey('timedeleted', (array) $m4);
680
201cebea 681 $this->assertEquals(get_string('yes'), $m5->issender);
04cd1b3f
MN
682 $this->assertEquals(message_format_message_text($dbm5), $m5->message);
683 $this->assertEquals(transform::datetime($now - (5 * DAYSECS)), $m5->timecreated);
684 $this->assertEquals('-', $m5->timeread);
685 $this->assertArrayHasKey('timedeleted', (array) $m5);
686
201cebea 687 $this->assertEquals(get_string('no'), $m6->issender);
04cd1b3f
MN
688 $this->assertEquals(message_format_message_text($dbm6), $m6->message);
689 $this->assertEquals(transform::datetime($now - (4 * DAYSECS)), $m6->timecreated);
690 $this->assertEquals('-', $m6->timeread);
691 }
692
201cebea
SA
693 /**
694 * Test for provider::export_user_data().
695 */
696 public function test_export_for_context_with_messages() {
697 global $DB;
698
699 $this->resetAfterTest();
700 $this->setAdminUser();
701 $now = time();
702 $systemcontext = \context_system::instance();
703
704 // Create users to test with.
705 $user1 = $this->getDataGenerator()->create_user();
706 $user2 = $this->getDataGenerator()->create_user();
707 $user3 = $this->getDataGenerator()->create_user();
708 $user1context = \context_user::instance($user1->id);
709
710 $course1 = $this->getDataGenerator()->create_course();
711 $course2 = $this->getDataGenerator()->create_course();
712 $coursecontext1 = \context_course::instance($course1->id);
713 $coursecontext2 = \context_course::instance($course2->id);
714
715 // Enrol users to courses.
716 $this->getDataGenerator()->enrol_user($user1->id, $course1->id);
717 $this->getDataGenerator()->enrol_user($user2->id, $course1->id);
718 $this->getDataGenerator()->enrol_user($user3->id, $course1->id);
719 $this->getDataGenerator()->enrol_user($user1->id, $course2->id);
720
721 // Create course groups with group messaging enabled.
722 $group1a = $this->getDataGenerator()->create_group(array('courseid' => $course1->id, 'enablemessaging' => 1));
723 $group2a = $this->getDataGenerator()->create_group(array('courseid' => $course2->id, 'enablemessaging' => 1));
724
725 // Add users to groups.
726 $this->getDataGenerator()->create_group_member(array('groupid' => $group1a->id, 'userid' => $user1->id));
727 $this->getDataGenerator()->create_group_member(array('groupid' => $group1a->id, 'userid' => $user2->id));
728 $this->getDataGenerator()->create_group_member(array('groupid' => $group1a->id, 'userid' => $user3->id));
729 $this->getDataGenerator()->create_group_member(array('groupid' => $group2a->id, 'userid' => $user1->id));
730
731 // Get conversation.
732 $component = 'core_group';
733 $itemtype = 'groups';
734 $conversation = \core_message\api::get_conversation_by_area(
735 $component,
736 $itemtype,
737 $group1a->id,
738 $coursecontext1->id
739 );
740
741 // Send some private messages between user 1 and user 2.
742 $pm1id = $this->create_message($user1->id, $user2->id, $now);
743
744 $dbpm1 = $DB->get_record('messages', ['id' => $pm1id]);
745
746 // Send some messages to the conversation.
747 $m1 = testhelper::send_fake_message_to_conversation($user1, $conversation->id, 'Message 1', $now + 1);
748 $m2 = testhelper::send_fake_message_to_conversation($user1, $conversation->id, 'Message 2', $now + 2);
749 $m3 = testhelper::send_fake_message_to_conversation($user2, $conversation->id, 'Message 3', $now + 3);
750
751 $dbm1 = $DB->get_record('messages', ['id' => $m1]);
752 $dbm2 = $DB->get_record('messages', ['id' => $m2]);
753 $dbm3 = $DB->get_record('messages', ['id' => $m3]);
754
755 // Mark as read and delete some messages.
756 \core_message\api::mark_message_as_read($user2->id, $dbm1);
757 \core_message\api::delete_message($user1->id, $m2);
758
759 // Confirm the user1 has no data in any course context because private messages are related to user context.
760 $this->export_context_data_for_user($user1->id, $coursecontext2, 'core_message');
761
762 // Check that system context hasn't been exported.
763 $writer = writer::with_context($systemcontext);
764 $this->assertFalse($writer->has_any_data());
765
766 // Check that course1 context hasn't been exported.
767 $writer = writer::with_context($coursecontext1);
768 $this->assertFalse($writer->has_any_data());
769
770 // Check that course2 context has been exported and contains data.
771 $writer = writer::with_context($coursecontext2);
772 $this->assertFalse($writer->has_any_data());
773
774 // Confirm the user1 has only private messages in the user context.
775 $this->export_context_data_for_user($user1->id, $user1context, 'core_message');
776 $writer = writer::with_context($user1context);
777 $this->assertTrue($writer->has_any_data());
778
779 // Confirm the messages with user 2 are correct.
780 $messages = (array) $writer->get_data([get_string('messages', 'core_message'), $user2->id]);
781 $this->assertCount(1, $messages);
782 $m1 = reset($messages);
783
784 $this->assertEquals(get_string('yes'), $m1->issender);
785 $this->assertEquals(message_format_message_text($dbpm1), $m1->message);
786 $this->assertEquals(transform::datetime($now), $m1->timecreated);
787 $this->assertEquals('-', $m1->timeread);
788 $this->assertArrayNotHasKey('timedeleted', (array) $m1);
789
790 // Confirm the messages with user 3 are correct.
791 $messages = (array) $writer->get_data([get_string('messages', 'core_message'), fullname($user3)]);
792 $this->assertCount(0, $messages);
793 }
794
04cd1b3f
MN
795 /**
796 * Test for provider::export_user_data().
797 */
798 public function test_export_for_context_with_notifications() {
799 $this->resetAfterTest();
800
801 // Create users to test with.
802 $user1 = $this->getDataGenerator()->create_user();
803 $user2 = $this->getDataGenerator()->create_user();
804 $user3 = $this->getDataGenerator()->create_user();
805
806 $now = time();
807 $timeread = $now - DAYSECS;
808
809 // Send notifications from user 1 to user 2.
810 $this->create_notification($user1->id, $user2->id, $now + (9 * DAYSECS), $timeread);
811 $this->create_notification($user2->id, $user1->id, $now + (8 * DAYSECS));
812 $this->create_notification($user1->id, $user2->id, $now + (7 * DAYSECS));
813
814 // Send notifications from user 3 to user 1.
815 $this->create_notification($user3->id, $user1->id, $now + (6 * DAYSECS), $timeread);
816 $this->create_notification($user1->id, $user3->id, $now + (5 * DAYSECS));
817 $this->create_notification($user3->id, $user1->id, $now + (4 * DAYSECS));
818
819 // Send notifications from user 3 to user 2 - should not be part of the export.
820 $this->create_notification($user3->id, $user2->id, $now + (3 * DAYSECS), $timeread);
821 $this->create_notification($user2->id, $user3->id, $now + (2 * DAYSECS));
822 $this->create_notification($user3->id, $user2->id, $now + (1 * DAYSECS));
823
52334296 824 $user1context = context_user::instance($user1->id);
04cd1b3f 825
52334296
SR
826 $this->export_context_data_for_user($user1->id, $user1context, 'core_message');
827
828 $writer = writer::with_context($user1context);
04cd1b3f
MN
829
830 $this->assertTrue($writer->has_any_data());
831
832 // Confirm the notifications.
833 $notifications = (array) $writer->get_data([get_string('notifications', 'core_message')]);
834
835 $this->assertCount(6, $notifications);
836 }
837
838 /**
839 * Test for provider::delete_data_for_all_users_in_context().
840 */
841 public function test_delete_data_for_all_users_in_context() {
842 global $DB;
843
844 $this->resetAfterTest();
845
846 // Create users to test with.
847 $user1 = $this->getDataGenerator()->create_user();
848 $user2 = $this->getDataGenerator()->create_user();
742c27b3
MN
849 $user3 = $this->getDataGenerator()->create_user();
850 $user4 = $this->getDataGenerator()->create_user();
52334296 851 $user5 = $this->getDataGenerator()->create_user();
04cd1b3f
MN
852
853 $now = time();
854 $timeread = $now - DAYSECS;
855
52334296 856 $user1context = context_user::instance($user1->id);
04cd1b3f
MN
857
858 // Create contacts.
742c27b3 859 \core_message\api::add_contact($user1->id, $user2->id);
52334296 860 \core_message\api::add_contact($user2->id, $user3->id);
742c27b3
MN
861
862 // Create contact requests.
863 \core_message\api::create_contact_request($user1->id, $user3->id);
52334296 864 \core_message\api::create_contact_request($user2->id, $user4->id);
742c27b3
MN
865
866 // Block a user.
867 \core_message\api::block_user($user1->id, $user3->id);
52334296 868 \core_message\api::block_user($user3->id, $user4->id);
04cd1b3f
MN
869
870 // Create messages.
871 $m1 = $this->create_message($user1->id, $user2->id, $now + (9 * DAYSECS), true);
872 $m2 = $this->create_message($user2->id, $user1->id, $now + (8 * DAYSECS));
52334296 873 $m3 = $this->create_message($user2->id, $user3->id, $now + (7 * DAYSECS));
04cd1b3f
MN
874
875 // Create notifications.
876 $n1 = $this->create_notification($user1->id, $user2->id, $now + (9 * DAYSECS), $timeread);
877 $n2 = $this->create_notification($user2->id, $user1->id, $now + (8 * DAYSECS));
52334296 878 $n3 = $this->create_notification($user2->id, $user3->id, $now + (7 * DAYSECS));
04cd1b3f
MN
879
880 // Delete one of the messages.
881 \core_message\api::delete_message($user1->id, $m2);
882
52334296
SR
883 // There should be 2 contacts.
884 $this->assertEquals(2, $DB->count_records('message_contacts'));
742c27b3 885
52334296
SR
886 // There should be 2 contact requests.
887 $this->assertEquals(2, $DB->count_records('message_contact_requests'));
742c27b3 888
52334296
SR
889 // There should be 2 blocked users.
890 $this->assertEquals(2, $DB->count_records('message_users_blocked'));
04cd1b3f 891
52334296
SR
892 // There should be 3 messages.
893 $this->assertEquals(3, $DB->count_records('messages'));
04cd1b3f 894
52334296 895 // There should be 2 user actions - one for reading the message, one for deleting.
04cd1b3f
MN
896 $this->assertEquals(2, $DB->count_records('message_user_actions'));
897
52334296
SR
898 // There should be 4 conversation members.
899 $this->assertEquals(4, $DB->count_records('message_conversation_members'));
04cd1b3f 900
52334296
SR
901 // There should be 3 notifications + 2 for the contact request.
902 $this->assertEquals(5, $DB->count_records('notifications'));
04cd1b3f 903
52334296 904 provider::delete_data_for_all_users_in_context($user1context);
04cd1b3f 905
52334296
SR
906 // Confirm there is only 1 contact left.
907 $this->assertEquals(1, $DB->count_records('message_contacts'));
908 // And it is not related to user1.
909 $this->assertEquals(0,
910 $DB->count_records_select('message_contacts', 'userid = ? OR contactid = ?', [$user1->id, $user1->id]));
911
912 // Confirm there is only 1 contact request left.
913 $this->assertEquals(1, $DB->count_records('message_contact_requests'));
914 // And it is not related to user1.
915 $this->assertEquals(0,
916 $DB->count_records_select('message_contact_requests', 'userid = ? OR requesteduserid = ?',
917 [$user1->id, $user1->id]));
918
919 // Confirm there is only 1 blocked user left.
920 $this->assertEquals(1, $DB->count_records('message_users_blocked'));
921 // And it is not related to user1.
922 $this->assertEquals(0,
923 $DB->count_records_select('message_users_blocked', 'userid = ? OR blockeduserid = ?', [$user1->id, $user1->id]));
924
925 // Confirm there are only 2 messages left.
926 $this->assertEquals(2, $DB->count_records('messages'));
927 // And none of them are from user1.
928 $this->assertEquals(0, $DB->count_records('messages', ['useridfrom' => $user1->id]));
929
201cebea
SA
930 // Confirm there is 0 user action left.
931 $this->assertEquals(0, $DB->count_records('message_user_actions'));
52334296
SR
932 // And it is not for user1.
933 $this->assertEquals(0, $DB->count_records('message_user_actions', ['userid' => $user1->id]));
934
935 // Confirm there are only 3 conversation members left.
936 $this->assertEquals(3, $DB->count_records('message_conversation_members'));
937 // And user1 is not in any conversation.
938 $this->assertEquals(0, $DB->count_records('message_conversation_members', ['userid' => $user1->id]));
939
940 // Confirm there is only 1 notification + 1 for the contact request.
941 $this->assertEquals(2, $DB->count_records('notifications'));
942 // And it is not related to user1.
943 $this->assertEquals(0,
944 $DB->count_records_select('notifications', 'useridfrom = ? OR useridto = ? ', [$user1->id, $user1->id]));
04cd1b3f
MN
945 }
946
947 /**
948 * Test for provider::delete_data_for_user().
949 */
950 public function test_delete_data_for_user() {
951 global $DB;
952
953 $this->resetAfterTest();
954
955 // Create users to test with.
956 $user1 = $this->getDataGenerator()->create_user();
957 $user2 = $this->getDataGenerator()->create_user();
958 $user3 = $this->getDataGenerator()->create_user();
742c27b3
MN
959 $user4 = $this->getDataGenerator()->create_user();
960 $user5 = $this->getDataGenerator()->create_user();
961 $user6 = $this->getDataGenerator()->create_user();
04cd1b3f
MN
962
963 $now = time();
964 $timeread = $now - DAYSECS;
965
966 // Create contacts.
742c27b3
MN
967 \core_message\api::add_contact($user1->id, $user2->id);
968 \core_message\api::add_contact($user2->id, $user3->id);
969
970 // Create contact requests.
971 \core_message\api::create_contact_request($user1->id, $user3->id);
972 \core_message\api::create_contact_request($user2->id, $user4->id);
973
974 // Block users.
975 \core_message\api::block_user($user1->id, $user5->id);
976 \core_message\api::block_user($user2->id, $user6->id);
04cd1b3f
MN
977
978 // Create messages.
979 $m1 = $this->create_message($user1->id, $user2->id, $now + (9 * DAYSECS), $timeread);
980 $m2 = $this->create_message($user2->id, $user1->id, $now + (8 * DAYSECS));
981
982 // Create notifications.
983 $n1 = $this->create_notification($user1->id, $user2->id, $now + (9 * DAYSECS), $timeread);
984 $n2 = $this->create_notification($user2->id, $user1->id, $now + (8 * DAYSECS));
985 $n2 = $this->create_notification($user2->id, $user3->id, $now + (8 * DAYSECS));
986
987 // Delete one of the messages.
988 \core_message\api::delete_message($user1->id, $m2);
989
742c27b3
MN
990 // There should be 2 contacts.
991 $this->assertEquals(2, $DB->count_records('message_contacts'));
992
993 // There should be 1 contact request.
994 $this->assertEquals(2, $DB->count_records('message_contact_requests'));
995
996 // There should be 1 blocked user.
997 $this->assertEquals(2, $DB->count_records('message_users_blocked'));
04cd1b3f
MN
998
999 // There should be two messages.
1000 $this->assertEquals(2, $DB->count_records('messages'));
1001
1002 // There should be two user actions - one for reading the message, one for deleting.
1003 $this->assertEquals(2, $DB->count_records('message_user_actions'));
1004
1005 // There should be two conversation members.
1006 $this->assertEquals(2, $DB->count_records('message_conversation_members'));
1007
be016b01
MN
1008 // There should be three notifications + two for the contact requests.
1009 $this->assertEquals(5, $DB->count_records('notifications'));
04cd1b3f 1010
52334296 1011 $user1context = context_user::instance($user1->id);
04cd1b3f 1012 $contextlist = new \core_privacy\local\request\approved_contextlist($user1, 'core_message',
52334296 1013 [$user1context->id]);
04cd1b3f
MN
1014 provider::delete_data_for_user($contextlist);
1015
1016 // Confirm the user 2 data still exists.
1017 $contacts = $DB->get_records('message_contacts');
742c27b3
MN
1018 $contactrequests = $DB->get_records('message_contact_requests');
1019 $blockedusers = $DB->get_records('message_users_blocked');
ba81c26c
SR
1020 $messages = $DB->get_records('messages');
1021 $muas = $DB->get_records('message_user_actions');
1022 $mcms = $DB->get_records('message_conversation_members');
1023 $notifications = $DB->get_records('notifications');
1024
1025 $this->assertCount(1, $contacts);
1026 $contact = reset($contacts);
1027 $this->assertEquals($user2->id, $contact->userid);
1028 $this->assertEquals($user3->id, $contact->contactid);
1029
1030 $this->assertCount(1, $contactrequests);
1031 $contactrequest = reset($contactrequests);
1032 $this->assertEquals($user2->id, $contactrequest->userid);
1033 $this->assertEquals($user4->id, $contactrequest->requesteduserid);
1034
1035 $this->assertCount(1, $blockedusers);
1036 $blockeduser = reset($blockedusers);
1037 $this->assertEquals($user2->id, $blockeduser->userid);
1038 $this->assertEquals($user6->id, $blockeduser->blockeduserid);
1039
1040 $this->assertCount(1, $messages);
1041 $message = reset($messages);
1042 $this->assertEquals($m2, $message->id);
1043
201cebea 1044 $this->assertCount(0, $muas);
ba81c26c
SR
1045
1046 $this->assertCount(1, $mcms);
1047 $mcm = reset($mcms);
1048 $this->assertEquals($user2->id, $mcm->userid);
1049
1050 $this->assertCount(2, $notifications);
1051 ksort($notifications);
1052
1053 $notification = array_shift($notifications);
1054 $this->assertEquals($user2->id, $notification->useridfrom);
1055 $this->assertEquals($user4->id, $notification->useridto);
1056
1057 $notification = array_shift($notifications);
1058 $this->assertEquals($user2->id, $notification->useridfrom);
1059 $this->assertEquals($user3->id, $notification->useridto);
1060 }
1061
1062 /**
1063 * Test for provider::get_users_in_context() when there is no message or notification.
1064 */
1065 public function test_get_users_in_context_no_data() {
1066 $this->resetAfterTest();
1067
1068 $user = $this->getDataGenerator()->create_user();
1069 $usercontext = context_user::instance($user->id);
1070
1071 $userlist = new \core_privacy\local\request\userlist($usercontext, 'core_message');
1072 \core_message\privacy\provider::get_users_in_context($userlist);
1073
1074 $this->assertEmpty($userlist->get_userids());
1075 }
1076
1077 /**
1078 * Test for provider::get_users_in_context() when there is a message between users.
1079 */
1080 public function test_get_users_in_context_with_message() {
1081 $this->resetAfterTest();
1082
1083 $user1 = $this->getDataGenerator()->create_user();
1084 $user2 = $this->getDataGenerator()->create_user();
1085
1086 $user1context = context_user::instance($user1->id);
1087 $user2context = context_user::instance($user2->id);
1088
b8bbef0a
MH
1089 // Test nothing is found before message is sent.
1090 $userlist = new \core_privacy\local\request\userlist($user1context, 'core_message');
1091 \core_message\privacy\provider::get_users_in_context($userlist);
1092 $this->assertCount(0, $userlist);
1093 $userlist = new \core_privacy\local\request\userlist($user2context, 'core_message');
1094 \core_message\privacy\provider::get_users_in_context($userlist);
1095 $this->assertCount(0, $userlist);
1096
ba81c26c
SR
1097 $this->create_message($user1->id, $user2->id, time() - (9 * DAYSECS));
1098
1099 // Test for the sender.
1100 $userlist = new \core_privacy\local\request\userlist($user1context, 'core_message');
1101 \core_message\privacy\provider::get_users_in_context($userlist);
1102 $this->assertCount(1, $userlist);
1103 $userincontext = $userlist->current();
1104 $this->assertEquals($user1->id, $userincontext->id);
1105
1106 // Test for the receiver.
1107 $userlist = new \core_privacy\local\request\userlist($user2context, 'core_message');
1108 \core_message\privacy\provider::get_users_in_context($userlist);
1109 $this->assertCount(1, $userlist);
1110 $userincontext = $userlist->current();
1111 $this->assertEquals($user2->id, $userincontext->id);
1112 }
1113
1114 /**
1115 * Test for provider::get_users_in_context() when there is a notification between users.
1116 */
1117 public function test_get_users_in_context_with_notification() {
1118 $this->resetAfterTest();
1119
1120 $user1 = $this->getDataGenerator()->create_user();
1121 $user2 = $this->getDataGenerator()->create_user();
1122
1123 $user1context = context_user::instance($user1->id);
1124 $user2context = context_user::instance($user2->id);
1125
b8bbef0a
MH
1126 // Test nothing is found before notification is created.
1127 $userlist = new \core_privacy\local\request\userlist($user1context, 'core_message');
1128 \core_message\privacy\provider::get_users_in_context($userlist);
1129 $this->assertCount(0, $userlist);
1130 $userlist = new \core_privacy\local\request\userlist($user2context, 'core_message');
1131 \core_message\privacy\provider::get_users_in_context($userlist);
1132 $this->assertCount(0, $userlist);
1133
ba81c26c
SR
1134 $this->create_notification($user1->id, $user2->id, time() - (9 * DAYSECS));
1135
1136 // Test for the sender.
1137 $userlist = new \core_privacy\local\request\userlist($user1context, 'core_message');
1138 \core_message\privacy\provider::get_users_in_context($userlist);
1139 $this->assertCount(1, $userlist);
1140 $userincontext = $userlist->current();
1141 $this->assertEquals($user1->id, $userincontext->id);
1142
1143 // Test for the receiver.
1144 $userlist = new \core_privacy\local\request\userlist($user2context, 'core_message');
1145 \core_message\privacy\provider::get_users_in_context($userlist);
1146 $this->assertCount(1, $userlist);
1147 $userincontext = $userlist->current();
1148 $this->assertEquals($user2->id, $userincontext->id);
1149 }
1150
b8bbef0a
MH
1151 /**
1152 * Test for provider::get_users_in_context() when a users has a contact.
1153 */
1154 public function test_get_users_in_context_with_contact() {
1155 $this->resetAfterTest();
1156
1157 $user1 = $this->getDataGenerator()->create_user();
1158 $user2 = $this->getDataGenerator()->create_user();
1159
1160 $user1context = context_user::instance($user1->id);
1161 $user2context = context_user::instance($user2->id);
1162
1163 // Test nothing is found before contact is created.
1164 $userlist = new \core_privacy\local\request\userlist($user1context, 'core_message');
1165 \core_message\privacy\provider::get_users_in_context($userlist);
1166 $this->assertCount(0, $userlist);
1167 $userlist = new \core_privacy\local\request\userlist($user2context, 'core_message');
1168 \core_message\privacy\provider::get_users_in_context($userlist);
1169 $this->assertCount(0, $userlist);
1170
1171 \core_message\api::add_contact($user1->id, $user2->id);
1172
1173 // Test for the user adding the contact.
1174 $userlist = new \core_privacy\local\request\userlist($user1context, 'core_message');
1175 \core_message\privacy\provider::get_users_in_context($userlist);
1176 $this->assertCount(1, $userlist);
1177 $userincontext = $userlist->current();
1178 $this->assertEquals($user1->id, $userincontext->id);
1179
1180 // Test for the user who is the contact.
1181 $userlist = new \core_privacy\local\request\userlist($user2context, 'core_message');
1182 \core_message\privacy\provider::get_users_in_context($userlist);
1183 $this->assertCount(1, $userlist);
1184 $userincontext = $userlist->current();
1185 $this->assertEquals($user2->id, $userincontext->id);
1186 }
1187
1188 /**
1189 * Test for provider::get_users_in_context() when a user makes a contact request.
1190 */
1191 public function test_get_users_in_context_with_contact_request() {
1192 $this->resetAfterTest();
1193
1194 $user1 = $this->getDataGenerator()->create_user();
1195 $user2 = $this->getDataGenerator()->create_user();
1196
1197 $user1context = context_user::instance($user1->id);
1198 $user2context = context_user::instance($user2->id);
1199
1200 // Test nothing is found before request is created.
1201 $userlist = new \core_privacy\local\request\userlist($user1context, 'core_message');
1202 \core_message\privacy\provider::get_users_in_context($userlist);
1203 $this->assertCount(0, $userlist);
1204 $userlist = new \core_privacy\local\request\userlist($user2context, 'core_message');
1205 \core_message\privacy\provider::get_users_in_context($userlist);
1206 $this->assertCount(0, $userlist);
1207
1208 \core_message\api::create_contact_request($user1->id, $user2->id);
1209
1210 // Test for the user requesting the contact.
1211 $userlist = new \core_privacy\local\request\userlist($user1context, 'core_message');
1212 \core_message\privacy\provider::get_users_in_context($userlist);
1213 $this->assertCount(1, $userlist);
1214 $userincontext = $userlist->current();
1215 $this->assertEquals($user1->id, $userincontext->id);
1216
1217 // Test for the user receiving the contact request.
1218 $userlist = new \core_privacy\local\request\userlist($user2context, 'core_message');
1219 \core_message\privacy\provider::get_users_in_context($userlist);
1220 $this->assertCount(1, $userlist);
1221 $userincontext = $userlist->current();
1222 $this->assertEquals($user2->id, $userincontext->id);
1223 }
1224
1225 /**
1226 * Test for provider::get_users_in_context() when a user is blocked.
1227 */
1228 public function test_get_users_in_context_with_blocked_contact() {
1229 $this->resetAfterTest();
1230
1231 $user1 = $this->getDataGenerator()->create_user();
1232 $user2 = $this->getDataGenerator()->create_user();
1233
1234 $user1context = context_user::instance($user1->id);
1235 $user2context = context_user::instance($user2->id);
1236
1237 // Test nothing is found before user is blocked.
1238 $userlist = new \core_privacy\local\request\userlist($user1context, 'core_message');
1239 \core_message\privacy\provider::get_users_in_context($userlist);
1240 $this->assertCount(0, $userlist);
1241 $userlist = new \core_privacy\local\request\userlist($user2context, 'core_message');
1242 \core_message\privacy\provider::get_users_in_context($userlist);
1243 $this->assertCount(0, $userlist);
1244
1245 \core_message\api::block_user($user1->id, $user2->id);
1246
1247 // Test for the blocking user.
1248 $userlist = new \core_privacy\local\request\userlist($user1context, 'core_message');
1249 \core_message\privacy\provider::get_users_in_context($userlist);
1250 $this->assertCount(1, $userlist);
1251 $userincontext = $userlist->current();
1252 $this->assertEquals($user1->id, $userincontext->id);
1253
1254 // Test for the user who is blocked.
1255 $userlist = new \core_privacy\local\request\userlist($user2context, 'core_message');
1256 \core_message\privacy\provider::get_users_in_context($userlist);
1257 $this->assertCount(1, $userlist);
1258 $userincontext = $userlist->current();
1259 $this->assertEquals($user2->id, $userincontext->id);
1260 }
1261
ba81c26c
SR
1262 /**
1263 * Test for provider::delete_data_for_users().
1264 */
1265 public function test_delete_data_for_users() {
1266 global $DB;
1267
1268 $this->resetAfterTest();
1269
1270 // Create users to test with.
1271 $user1 = $this->getDataGenerator()->create_user();
1272 $user2 = $this->getDataGenerator()->create_user();
1273 $user3 = $this->getDataGenerator()->create_user();
1274 $user4 = $this->getDataGenerator()->create_user();
1275 $user5 = $this->getDataGenerator()->create_user();
1276 $user6 = $this->getDataGenerator()->create_user();
1277
1278 $now = time();
1279 $timeread = $now - DAYSECS;
1280
1281 // Create contacts.
1282 \core_message\api::add_contact($user1->id, $user2->id);
1283 \core_message\api::add_contact($user2->id, $user3->id);
1284
1285 // Create contact requests.
1286 \core_message\api::create_contact_request($user1->id, $user3->id);
1287 \core_message\api::create_contact_request($user2->id, $user4->id);
1288
1289 // Block users.
1290 \core_message\api::block_user($user1->id, $user5->id);
1291 \core_message\api::block_user($user2->id, $user6->id);
1292
1293 // Create messages.
1294 $m1 = $this->create_message($user1->id, $user2->id, $now + (9 * DAYSECS), $timeread);
1295 $m2 = $this->create_message($user2->id, $user1->id, $now + (8 * DAYSECS));
1296
1297 // Create notifications.
1298 $n1 = $this->create_notification($user1->id, $user2->id, $now + (9 * DAYSECS), $timeread);
1299 $n2 = $this->create_notification($user2->id, $user1->id, $now + (8 * DAYSECS));
1300 $n2 = $this->create_notification($user2->id, $user3->id, $now + (8 * DAYSECS));
1301
1302 // Delete one of the messages.
1303 \core_message\api::delete_message($user1->id, $m2);
1304
1305 // There should be 2 contacts.
1306 $this->assertEquals(2, $DB->count_records('message_contacts'));
1307
1308 // There should be 1 contact request.
1309 $this->assertEquals(2, $DB->count_records('message_contact_requests'));
1310
1311 // There should be 1 blocked user.
1312 $this->assertEquals(2, $DB->count_records('message_users_blocked'));
1313
1314 // There should be two messages.
1315 $this->assertEquals(2, $DB->count_records('messages'));
1316
1317 // There should be two user actions - one for reading the message, one for deleting.
1318 $this->assertEquals(2, $DB->count_records('message_user_actions'));
1319
1320 // There should be two conversation members.
1321 $this->assertEquals(2, $DB->count_records('message_conversation_members'));
1322
1323 // There should be three notifications + two for the contact requests.
1324 $this->assertEquals(5, $DB->count_records('notifications'));
1325
1326 $user1context = context_user::instance($user1->id);
1327 $approveduserlist = new \core_privacy\local\request\approved_userlist($user1context, 'core_message',
1328 [$user1->id, $user2->id]);
1329 provider::delete_data_for_users($approveduserlist);
1330
1331 // Only user1's data should be deleted. User2 should be skipped as user2 is an invalid user for user1context.
1332
1333 // Confirm the user 2 data still exists.
1334 $contacts = $DB->get_records('message_contacts');
1335 $contactrequests = $DB->get_records('message_contact_requests');
1336 $blockedusers = $DB->get_records('message_users_blocked');
04cd1b3f
MN
1337 $messages = $DB->get_records('messages');
1338 $muas = $DB->get_records('message_user_actions');
1339 $mcms = $DB->get_records('message_conversation_members');
1340 $notifications = $DB->get_records('notifications');
1341
1342 $this->assertCount(1, $contacts);
1343 $contact = reset($contacts);
742c27b3
MN
1344 $this->assertEquals($user2->id, $contact->userid);
1345 $this->assertEquals($user3->id, $contact->contactid);
1346
1347 $this->assertCount(1, $contactrequests);
1348 $contactrequest = reset($contactrequests);
1349 $this->assertEquals($user2->id, $contactrequest->userid);
1350 $this->assertEquals($user4->id, $contactrequest->requesteduserid);
1351
1352 $this->assertCount(1, $blockedusers);
1353 $blockeduser = reset($blockedusers);
1354 $this->assertEquals($user2->id, $blockeduser->userid);
1355 $this->assertEquals($user6->id, $blockeduser->blockeduserid);
04cd1b3f
MN
1356
1357 $this->assertCount(1, $messages);
1358 $message = reset($messages);
1359 $this->assertEquals($m2, $message->id);
1360
201cebea 1361 $this->assertCount(0, $muas);
04cd1b3f
MN
1362
1363 $this->assertCount(1, $mcms);
1364 $mcm = reset($mcms);
1365 $this->assertEquals($user2->id, $mcm->userid);
1366
be016b01
MN
1367 $this->assertCount(2, $notifications);
1368 ksort($notifications);
1369
1370 $notification = array_shift($notifications);
1371 $this->assertEquals($user2->id, $notification->useridfrom);
1372 $this->assertEquals($user4->id, $notification->useridto);
1373
1374 $notification = array_shift($notifications);
04cd1b3f
MN
1375 $this->assertEquals($user2->id, $notification->useridfrom);
1376 $this->assertEquals($user3->id, $notification->useridto);
1377 }
1378
201cebea
SA
1379 /**
1380 * Test for provider::add_contexts_for_conversations().
1381 */
1382 public function test_add_contexts_for_conversations() {
1383 $this->resetAfterTest();
1384 $this->setAdminUser();
1385 $component = 'core_group';
1386 $itemtype = 'groups';
1387
1388 $user1 = $this->getDataGenerator()->create_user();
1389 $user2 = $this->getDataGenerator()->create_user();
1390 $user3 = $this->getDataGenerator()->create_user();
1391 $user4 = $this->getDataGenerator()->create_user();
1392
1393 // Test nothing is found before group conversations is created or message is sent.
1394 $contextlist = new contextlist();
1395 provider::add_contexts_for_conversations($contextlist, $user1->id, $component, $itemtype);
1396 $this->assertCount(0, $contextlist);
1397 provider::add_contexts_for_conversations($contextlist, $user2->id, $component, $itemtype);
1398 $this->assertCount(0, $contextlist);
1399
1400 // Create courses.
1401 $course1 = $this->getDataGenerator()->create_course();
1402 $course2 = $this->getDataGenerator()->create_course();
1403 $coursecontext1 = \context_course::instance($course1->id);
1404 $coursecontext2 = \context_course::instance($course2->id);
1405
1406 // Enrol users to courses.
1407 $this->getDataGenerator()->enrol_user($user1->id, $course1->id);
1408 $this->getDataGenerator()->enrol_user($user2->id, $course1->id);
1409 $this->getDataGenerator()->enrol_user($user3->id, $course1->id);
1410 $this->getDataGenerator()->enrol_user($user1->id, $course2->id);
1411 $this->getDataGenerator()->enrol_user($user2->id, $course2->id);
1412
1413 // Create course groups with messaging enabled.
1414 $group1a = $this->getDataGenerator()->create_group(array('courseid' => $course1->id, 'enablemessaging' => 1));
1415 $group2a = $this->getDataGenerator()->create_group(array('courseid' => $course2->id, 'enablemessaging' => 1));
1416
1417 // Add users to groups.
1418 $this->getDataGenerator()->create_group_member(array('groupid' => $group1a->id, 'userid' => $user1->id));
1419 $this->getDataGenerator()->create_group_member(array('groupid' => $group1a->id, 'userid' => $user2->id));
1420 $this->getDataGenerator()->create_group_member(array('groupid' => $group1a->id, 'userid' => $user3->id));
1421 $this->getDataGenerator()->create_group_member(array('groupid' => $group2a->id, 'userid' => $user1->id));
1422
1423 // Get conversation.
1424 $conversation1 = \core_message\api::get_conversation_by_area(
1425 $component,
1426 $itemtype,
1427 $group1a->id,
1428 $coursecontext1->id
1429 );
1430
1431 // Send some messages to the group conversation.
1432 $now = time();
1433 $m1id = testhelper::send_fake_message_to_conversation($user1, $conversation1->id, 'Message 1', $now + 1);
1434 $m2id = testhelper::send_fake_message_to_conversation($user1, $conversation1->id, 'Message 2', $now + 2);
1435 $m3id = testhelper::send_fake_message_to_conversation($user2, $conversation1->id, 'Message 3', $now + 3);
1436
1437 // Test for user1 (is member of the conversation and has sent a message).
1438 $contextlist = new contextlist();
1439 provider::add_contexts_for_conversations($contextlist, $user1->id, $component, $itemtype);
1440 $this->assertCount(2, $contextlist);
1441 $this->assertContains($coursecontext1->id, $contextlist->get_contextids());
1442 $this->assertContains($coursecontext2->id, $contextlist->get_contextids());
1443
1444 // Test for user2 (is member of the conversation and has sent a message).
1445 $contextlist = new contextlist();
1446 provider::add_contexts_for_conversations($contextlist, $user2->id, $component, $itemtype);
1447 $this->assertCount(1, $contextlist);
1448 $this->assertEquals($coursecontext1, $contextlist->current());
1449
1450 // Test for user3 (is member of the conversation).
1451 $contextlist = new contextlist();
1452 provider::add_contexts_for_conversations($contextlist, $user3->id, $component, $itemtype);
1453 $this->assertCount(1, $contextlist);
1454 $this->assertEquals($coursecontext1, $contextlist->current());
1455
1456 // Test for user4 (doesn't belong to the conversation).
1457 $contextlist = new contextlist();
1458 provider::add_contexts_for_conversations($contextlist, $user4->id, $component, $itemtype);
1459 $this->assertCount(0, $contextlist);
1460 }
1461
1462 /**
1463 * Test for provider::add_conversations_in_context().
1464 */
1465 public function test_add_conversations_in_context() {
1466 $this->resetAfterTest();
1467 $this->setAdminUser();
1468 $component = 'core_group';
1469 $itemtype = 'groups';
1470
1471 $user1 = $this->getDataGenerator()->create_user();
1472 $user2 = $this->getDataGenerator()->create_user();
1473 $user3 = $this->getDataGenerator()->create_user();
1474 $user4 = $this->getDataGenerator()->create_user();
1475
1476 // Create courses.
1477 $course1 = $this->getDataGenerator()->create_course();
1478 $course2 = $this->getDataGenerator()->create_course();
1479 $coursecontext1 = \context_course::instance($course1->id);
1480 $coursecontext2 = \context_course::instance($course2->id);
1481
1482 // Test nothing is found before group conversations is created or message is sent.
1483 $userlist1 = new \core_privacy\local\request\userlist($coursecontext1, 'core_message');
1484 provider::add_conversations_in_context($userlist1, $component, $itemtype);
1485 $this->assertCount(0, $userlist1);
1486
1487 // Enrol users to courses.
1488 $this->getDataGenerator()->enrol_user($user1->id, $course1->id);
1489 $this->getDataGenerator()->enrol_user($user2->id, $course1->id);
1490 $this->getDataGenerator()->enrol_user($user3->id, $course1->id);
1491 $this->getDataGenerator()->enrol_user($user4->id, $course1->id);
1492 $this->getDataGenerator()->enrol_user($user1->id, $course2->id);
1493 $this->getDataGenerator()->enrol_user($user2->id, $course2->id);
1494
1495 // Create course groups with messaging enabled.
1496 $group1a = $this->getDataGenerator()->create_group(array('courseid' => $course1->id, 'enablemessaging' => 1));
1497 $group2a = $this->getDataGenerator()->create_group(array('courseid' => $course2->id, 'enablemessaging' => 1));
1498
1499 // Add users to groups.
1500 $this->getDataGenerator()->create_group_member(array('groupid' => $group1a->id, 'userid' => $user1->id));
1501 $this->getDataGenerator()->create_group_member(array('groupid' => $group1a->id, 'userid' => $user2->id));
1502 $this->getDataGenerator()->create_group_member(array('groupid' => $group1a->id, 'userid' => $user3->id));
1503 $this->getDataGenerator()->create_group_member(array('groupid' => $group2a->id, 'userid' => $user1->id));
1504
1505 // Get conversation.
1506 $conversation1 = \core_message\api::get_conversation_by_area(
1507 $component,
1508 $itemtype,
1509 $group1a->id,
1510 $coursecontext1->id
1511 );
1512
1513 // Send some messages to the group conversation.
1514 $now = time();
1515 $m1id = testhelper::send_fake_message_to_conversation($user1, $conversation1->id, 'Message 1', $now + 1);
1516 $m2id = testhelper::send_fake_message_to_conversation($user1, $conversation1->id, 'Message 2', $now + 2);
1517 $m3id = testhelper::send_fake_message_to_conversation($user2, $conversation1->id, 'Message 3', $now + 3);
1518
1519 // Test for users with any group conversation in course1.
1520 provider::add_conversations_in_context($userlist1, $component, $itemtype);
1521 $this->assertCount(3, $userlist1);
1522 $this->assertEquals(
1523 [$user1->id, $user2->id, $user3->id],
1524 $userlist1->get_userids());
1525
1526 // Test for users with any group conversation in course2.
1527 $userlist2 = new \core_privacy\local\request\userlist($coursecontext2, 'core_message');
1528 provider::add_conversations_in_context($userlist2, $component, $itemtype);
1529 $this->assertCount(1, $userlist2);
1530 $this->assertEquals(
1531 [$user1->id],
1532 $userlist2->get_userids());
1533 }
1534
1535 /**
1536 * Test for provider::export_conversations().
1537 */
1538 public function test_export_conversations() {
1539 global $DB;
1540
1541 $this->resetAfterTest();
1542 $this->setAdminUser();
1543 $now = time();
1544 $systemcontext = \context_system::instance();
1545
1546 // Create users to test with.
1547 $user1 = $this->getDataGenerator()->create_user();
1548 $user2 = $this->getDataGenerator()->create_user();
1549 $user3 = $this->getDataGenerator()->create_user();
1550 $user1context = \context_user::instance($user1->id);
1551
1552 $course1 = $this->getDataGenerator()->create_course();
1553 $course2 = $this->getDataGenerator()->create_course();
1554 $coursecontext1 = \context_course::instance($course1->id);
1555 $coursecontext2 = \context_course::instance($course2->id);
1556
1557 // Enrol users to courses.
1558 $this->getDataGenerator()->enrol_user($user1->id, $course1->id);
1559 $this->getDataGenerator()->enrol_user($user2->id, $course1->id);
1560 $this->getDataGenerator()->enrol_user($user3->id, $course1->id);
1561 $this->getDataGenerator()->enrol_user($user1->id, $course2->id);
1562
1563 // Create course groups with group messaging enabled.
1564 $group1a = $this->getDataGenerator()->create_group(array('courseid' => $course1->id, 'enablemessaging' => 1));
1565 $group2a = $this->getDataGenerator()->create_group(array('courseid' => $course2->id, 'enablemessaging' => 1));
1566
1567 // Add users to groups.
1568 $this->getDataGenerator()->create_group_member(array('groupid' => $group1a->id, 'userid' => $user1->id));
1569 $this->getDataGenerator()->create_group_member(array('groupid' => $group1a->id, 'userid' => $user2->id));
1570 $this->getDataGenerator()->create_group_member(array('groupid' => $group1a->id, 'userid' => $user3->id));
1571 $this->getDataGenerator()->create_group_member(array('groupid' => $group2a->id, 'userid' => $user1->id));
1572
1573 // Get conversation.
1574 $component = 'core_group';
1575 $itemtype = 'groups';
1576 $conversation1 = \core_message\api::get_conversation_by_area(
1577 $component,
1578 $itemtype,
1579 $group1a->id,
1580 $coursecontext1->id
1581 );
1582
1583 // Send some messages to the conversation.
1584 $m1 = testhelper::send_fake_message_to_conversation($user1, $conversation1->id, 'Message 1', $now + 1);
1585 $m2 = testhelper::send_fake_message_to_conversation($user1, $conversation1->id, 'Message 2', $now + 2);
1586 $m3 = testhelper::send_fake_message_to_conversation($user2, $conversation1->id, 'Message 3', $now + 3);
1587
1588 $dbm1 = $DB->get_record('messages', ['id' => $m1]);
1589 $dbm2 = $DB->get_record('messages', ['id' => $m2]);
1590 $dbm3 = $DB->get_record('messages', ['id' => $m3]);
1591
1592 // Send some private messages between user 1 and user 2.
1593 $pm1id = $this->create_message($user1->id, $user2->id, $now);
1594
1595 // Mark as read and delete some messages.
1596 \core_message\api::mark_message_as_read($user1->id, $dbm3, $now + 5);
1597 \core_message\api::delete_message($user1->id, $m2);
1598
1599 // Export all the conversations related to the groups in course1 for user1.
1600 provider::export_conversations($user1->id, 'core_group', 'groups', $coursecontext1);
1601
1602 // Check that system context hasn't been exported.
1603 $writer = writer::with_context($systemcontext);
1604 $this->assertFalse($writer->has_any_data());
1605
1606 // Check that course2 context hasn't been exported.
1607 $writer = writer::with_context($coursecontext2);
1608 $this->assertFalse($writer->has_any_data());
1609
1610 // Check that course1 context has been exported for user1 and contains data.
1611 $writer = writer::with_context($coursecontext1);
1612 $this->assertTrue($writer->has_any_data());
1613
1614 // Confirm the messages for conversation1 are correct.
1615 $messages = (array) $writer->get_data([
1616 get_string('messages', 'core_message'),
1617 get_string($conversation1->itemtype, $conversation1->component),
1618 get_string('privacy:export:conversationprefix', 'core_message') . $conversation1->name
1619 ]);
1620 $this->assertCount(3, $messages);
1621
1622 usort($messages, ['static', 'sort_messages']);
1623 $m1 = array_shift($messages);
1624 $m2 = array_shift($messages);
1625 $m3 = array_shift($messages);
1626
1627 // Check message 1 is correct.
1628 $this->assertEquals(get_string('yes'), $m1->issender);
1629 $this->assertEquals(message_format_message_text($dbm1), $m1->message);
1630 $this->assertEquals(transform::datetime($now + 1), $m1->timecreated);
1631 $this->assertEquals('-', $m1->timeread);
1632 $this->assertArrayNotHasKey('timedeleted', (array) $m1);
1633
1634 // Check message 2 is correct.
1635 $this->assertEquals(get_string('yes'), $m2->issender);
1636 $this->assertEquals(message_format_message_text($dbm2), $m2->message);
1637 $this->assertEquals(transform::datetime($now + 2), $m2->timecreated);
1638 $this->assertEquals('-', $m2->timeread);
1639 $this->assertArrayHasKey('timedeleted', (array) $m2);
1640
1641 // Check message 3 is correct.
1642 $this->assertEquals(get_string('no'), $m3->issender);
1643 $this->assertEquals(message_format_message_text($dbm3), $m3->message);
1644 $this->assertEquals(transform::datetime($now + 3), $m3->timecreated);
1645 $this->assertEquals(transform::datetime($now + 5), $m3->timeread);
1646 $this->assertArrayNotHasKey('timedeleted', (array) $m3);
1647
1648 // Export all the conversations related to the groups in course1 for user2.
1649 provider::export_conversations($user2->id, 'core_group', 'groups', $coursecontext1);
1650
1651 // Check that system context hasn't been exported.
1652 $writer = writer::with_context($systemcontext);
1653 $this->assertFalse($writer->has_any_data());
1654
1655 // Check that course2 context hasn't been exported.
1656 $writer = writer::with_context($coursecontext2);
1657 $this->assertFalse($writer->has_any_data());
1658
1659 // Check that course1 context has been exported for user2 and contains data.
1660 $writer = writer::with_context($coursecontext1);
1661 $this->assertTrue($writer->has_any_data());
1662
1663 // Confirm the messages for conversation1 are correct.
1664 $messages = (array) $writer->get_data([
1665 get_string('messages', 'core_message'),
1666 get_string($conversation1->itemtype, $conversation1->component),
1667 get_string('privacy:export:conversationprefix', 'core_message') . $conversation1->name
1668 ]);
1669 $this->assertCount(3, $messages);
1670
1671 usort($messages, ['static', 'sort_messages']);
1672 $m1 = array_shift($messages);
1673 $m2 = array_shift($messages);
1674 $m3 = array_shift($messages);
1675
1676 // Check message 1 is correct.
1677 $this->assertEquals(get_string('no'), $m1->issender);
1678 $this->assertEquals(message_format_message_text($dbm1), $m1->message);
1679 $this->assertEquals(transform::datetime($now + 1), $m1->timecreated);
1680 $this->assertEquals('-', $m1->timeread);
1681 $this->assertArrayNotHasKey('timedeleted', (array) $m1);
1682
1683 // Check message 2 is correct.
1684 $this->assertEquals(get_string('no'), $m2->issender);
1685 $this->assertEquals(message_format_message_text($dbm2), $m2->message);
1686 $this->assertEquals(transform::datetime($now + 2), $m2->timecreated);
1687 $this->assertEquals('-', $m2->timeread);
1688 $this->assertArrayNotHasKey('timedeleted', (array) $m2);
1689
1690 // Check message 3 is correct.
1691 $this->assertEquals(get_string('yes'), $m3->issender);
1692 $this->assertEquals(message_format_message_text($dbm3), $m3->message);
1693 $this->assertEquals(transform::datetime($now + 3), $m3->timecreated);
1694 $this->assertEquals('-', $m3->timeread);
1695 $this->assertArrayNotHasKey('timedeleted', (array) $m3);
1696 }
1697
1698 /**
1699 * Test for provider::delete_conversations_for_all_users().
1700 */
1701 public function test_delete_conversations_for_all_users() {
1702 global $DB;
1703
1704 $this->resetAfterTest();
1705 $this->setAdminUser();
1706 $now = time();
1707 $timeread = $now - DAYSECS;
1708 $component = 'core_group';
1709 $itemtype = 'groups';
1710
1711 // Create users to test with.
1712 $user1 = $this->getDataGenerator()->create_user();
1713 $user2 = $this->getDataGenerator()->create_user();
1714 $user3 = $this->getDataGenerator()->create_user();
1715 $user4 = $this->getDataGenerator()->create_user();
1716 $user5 = $this->getDataGenerator()->create_user();
1717 $user1context = \context_user::instance($user1->id);
1718
1719 // Create contacts.
1720 \core_message\api::add_contact($user1->id, $user2->id);
1721 \core_message\api::add_contact($user2->id, $user3->id);
1722
1723 // Create contact requests.
1724 \core_message\api::create_contact_request($user1->id, $user3->id);
1725 \core_message\api::create_contact_request($user2->id, $user4->id);
1726
1727 // Block a user.
1728 \core_message\api::block_user($user1->id, $user3->id);
1729 \core_message\api::block_user($user3->id, $user4->id);
1730
1731 // Create individual messages.
1732 $im1 = $this->create_message($user1->id, $user2->id, $now + (9 * DAYSECS), true);
1733 $im2 = $this->create_message($user2->id, $user1->id, $now + (8 * DAYSECS), true);
1734 $im3 = $this->create_message($user2->id, $user3->id, $now + (7 * DAYSECS));
1735
1736 // Create notifications.
1737 $n1 = $this->create_notification($user1->id, $user2->id, $now + (9 * DAYSECS), $timeread);
1738 $n2 = $this->create_notification($user2->id, $user1->id, $now + (8 * DAYSECS));
1739 $n3 = $this->create_notification($user2->id, $user3->id, $now + (7 * DAYSECS));
1740
1741 // Delete one of the messages.
1742 \core_message\api::delete_message($user1->id, $im2);
1743
1744 // Create course2.
1745 $course1 = $this->getDataGenerator()->create_course();
1746 $course2 = $this->getDataGenerator()->create_course();
1747 $coursecontext1 = \context_course::instance($course1->id);
1748 $coursecontext2 = \context_course::instance($course2->id);
1749
1750 // Enrol users to courses.
1751 $this->getDataGenerator()->enrol_user($user1->id, $course1->id);
1752 $this->getDataGenerator()->enrol_user($user2->id, $course1->id);
1753 $this->getDataGenerator()->enrol_user($user3->id, $course1->id);
1754 $this->getDataGenerator()->enrol_user($user1->id, $course2->id);
1755 $this->getDataGenerator()->enrol_user($user2->id, $course2->id);
1756
1757 // Create course groups with group messaging enabled.
1758 $group1a = $this->getDataGenerator()->create_group(array('courseid' => $course1->id, 'enablemessaging' => 1));
1759 $group2a = $this->getDataGenerator()->create_group(array('courseid' => $course2->id, 'enablemessaging' => 1));
1760
1761 // Add users to groups.
1762 $this->getDataGenerator()->create_group_member(array('groupid' => $group1a->id, 'userid' => $user1->id));
1763 $this->getDataGenerator()->create_group_member(array('groupid' => $group1a->id, 'userid' => $user2->id));
1764 $this->getDataGenerator()->create_group_member(array('groupid' => $group1a->id, 'userid' => $user3->id));
1765 $this->getDataGenerator()->create_group_member(array('groupid' => $group2a->id, 'userid' => $user1->id));
1766 $this->getDataGenerator()->create_group_member(array('groupid' => $group2a->id, 'userid' => $user2->id));
1767
1768 // Get conversation.
1769 $conversation1 = \core_message\api::get_conversation_by_area(
1770 $component,
1771 $itemtype,
1772 $group1a->id,
1773 $coursecontext1->id
1774 );
1775 $conversation2 = \core_message\api::get_conversation_by_area(
1776 $component,
1777 $itemtype,
1778 $group2a->id,
1779 $coursecontext2->id
1780 );
1781
1782 // Send some messages to the conversation.
1783 $gm1 = testhelper::send_fake_message_to_conversation($user1, $conversation1->id, 'Message 1.1', $now + 1);
1784 $gm2 = testhelper::send_fake_message_to_conversation($user1, $conversation1->id, 'Message 1.2', $now + 2);
1785 $gm3 = testhelper::send_fake_message_to_conversation($user2, $conversation1->id, 'Message 1.3', $now + 3);
1786 $gm4 = testhelper::send_fake_message_to_conversation($user1, $conversation2->id, 'Message 2.1', $now + 4);
1787 $gm5 = testhelper::send_fake_message_to_conversation($user2, $conversation2->id, 'Message 2.2', $now + 5);
1788
1789 $dbgm1 = $DB->get_record('messages', ['id' => $gm1]);
1790 $dbgm2 = $DB->get_record('messages', ['id' => $gm2]);
1791 $dbgm3 = $DB->get_record('messages', ['id' => $gm3]);
1792 $dbgm4 = $DB->get_record('messages', ['id' => $gm4]);
1793 $dbgm5 = $DB->get_record('messages', ['id' => $gm5]);
1794
1795 // Mark as read one of the conversation messages.
1796 \core_message\api::mark_message_as_read($user1->id, $dbgm3, $now + 5);
1797
1798 // There should be 2 contacts.
1799 $this->assertEquals(2, $DB->count_records('message_contacts'));
1800
1801 // There should be 2 contact requests.
1802 $this->assertEquals(2, $DB->count_records('message_contact_requests'));
1803
1804 // There should be 2 blocked users.
1805 $this->assertEquals(2, $DB->count_records('message_users_blocked'));
1806
1807 // There should be 8 messages.
1808 $this->assertEquals(8, $DB->count_records('messages'));
1809
1810 // There should be 4 user actions - 3 for reading the message, 1 for deleting.
1811 $this->assertEquals(4, $DB->count_records('message_user_actions'));
1812
1813 // There should be 4 conversations - 2 individual + 2 group.
1814 $this->assertEquals(4, $DB->count_records('message_conversations'));
1815
1816 // There should be 9 conversation members - (2 + 2) individual + (3 + 2) group.
1817 $this->assertEquals(9, $DB->count_records('message_conversation_members'));
1818
1819 // There should be 5 notifications - 3 notifications + 2 for the contact request.
1820 $this->assertEquals(5, $DB->count_records('notifications'));
1821
1822 // Delete conversations for all users in course1.
1823 provider::delete_conversations_for_all_users($coursecontext1, $component, $itemtype);
1824
1825 // There should be still 2 contacts.
1826 $this->assertEquals(2, $DB->count_records('message_contacts'));
1827
1828 // There should be still 2 contact requests.
1829 $this->assertEquals(2, $DB->count_records('message_contact_requests'));
1830
1831 // There should be still 2 blocked users.
1832 $this->assertEquals(2, $DB->count_records('message_users_blocked'));
1833
1834 // There should be still 5 notifications - 3 notifications + 2 for the contact request.
1835 $this->assertEquals(5, $DB->count_records('notifications'));
1836
1837 // There should be 5 messages - 3 individual - 2 group (course2).
1838 $this->assertEquals(5, $DB->count_records('messages'));
1839 $messages = array_keys($DB->get_records('messages'));
1840 $this->assertContains($im1, $messages);
1841 $this->assertContains($im2, $messages);
1842 $this->assertContains($im3, $messages);
1843 $this->assertContains($gm4, $messages);
1844 $this->assertContains($gm5, $messages);
1845
1846 // There should be 3 user actions - 2 for reading the message, 1 for deleting.
1847 $this->assertEquals(3, $DB->count_records('message_user_actions'));
1848 $useractions = $DB->get_records('message_user_actions');
1849 $useractions = array_map(function($action) {
1850 return $action->messageid;
1851 }, $useractions);
1852 $this->assertNotContains($gm3, $useractions);
1853
1854 // There should be 3 conversations - 2 individual + 1 group (course2).
1855 $this->assertEquals(3, $DB->count_records('message_conversations'));
1856 $conversations = $DB->get_records('message_conversations');
1857 $this->assertArrayNotHasKey($conversation1->id, $conversations);
1858
1859 // There should be 6 conversation members - (2 + 2) individual + 2 group.
1860 $this->assertEquals(6, $DB->count_records('message_conversation_members'));
1861 }
1862
1863 /**
1864 * Test for provider::delete_conversations_for_all_users() in the system context.
1865 */
1866 public function test_delete_conversations_for_all_users_systemcontext() {
1867 global $DB;
1868
1869 $this->resetAfterTest();
1870 $this->setAdminUser();
1871 $now = time();
1872 $timeread = $now - DAYSECS;
1873 $systemcontext = \context_system::instance();
1874 $component = 'core_group';
1875 $itemtype = 'groups';
1876
1877 // Create users to test with.
1878 $user1 = $this->getDataGenerator()->create_user();
1879 $user2 = $this->getDataGenerator()->create_user();
1880 $user3 = $this->getDataGenerator()->create_user();
1881 $user4 = $this->getDataGenerator()->create_user();
1882 $user5 = $this->getDataGenerator()->create_user();
1883
1884 // Create contacts.
1885 \core_message\api::add_contact($user1->id, $user2->id);
1886 \core_message\api::add_contact($user2->id, $user3->id);
1887
1888 // Create contact requests.
1889 \core_message\api::create_contact_request($user1->id, $user3->id);
1890 \core_message\api::create_contact_request($user2->id, $user4->id);
1891
1892 // Block a user.
1893 \core_message\api::block_user($user1->id, $user3->id);
1894 \core_message\api::block_user($user3->id, $user4->id);
1895
1896 // Create individual messages.
1897 $im1 = $this->create_message($user1->id, $user2->id, $now + (9 * DAYSECS), true);
1898 $im2 = $this->create_message($user2->id, $user1->id, $now + (8 * DAYSECS), true);
1899 $im3 = $this->create_message($user2->id, $user3->id, $now + (7 * DAYSECS));
1900
1901 // Create notifications.
1902 $n1 = $this->create_notification($user1->id, $user2->id, $now + (9 * DAYSECS), $timeread);
1903 $n2 = $this->create_notification($user2->id, $user1->id, $now + (8 * DAYSECS));
1904 $n3 = $this->create_notification($user2->id, $user3->id, $now + (7 * DAYSECS));
1905
1906 // Delete one of the messages.
1907 \core_message\api::delete_message($user1->id, $im2);
1908
1909 // Create course2.
1910 $course1 = $this->getDataGenerator()->create_course();
1911 $course2 = $this->getDataGenerator()->create_course();
1912 $coursecontext1 = \context_course::instance($course1->id);
1913 $coursecontext2 = \context_course::instance($course2->id);
1914
1915 // Enrol users to courses.
1916 $this->getDataGenerator()->enrol_user($user1->id, $course1->id);
1917 $this->getDataGenerator()->enrol_user($user2->id, $course1->id);
1918 $this->getDataGenerator()->enrol_user($user3->id, $course1->id);
1919 $this->getDataGenerator()->enrol_user($user1->id, $course2->id);
1920 $this->getDataGenerator()->enrol_user($user2->id, $course2->id);
1921
1922 // Create course groups with group messaging enabled.
1923 $group1a = $this->getDataGenerator()->create_group(array('courseid' => $course1->id, 'enablemessaging' => 1));
1924 $group2a = $this->getDataGenerator()->create_group(array('courseid' => $course2->id, 'enablemessaging' => 1));
1925
1926 // Add users to groups.
1927 $this->getDataGenerator()->create_group_member(array('groupid' => $group1a->id, 'userid' => $user1->id));
1928 $this->getDataGenerator()->create_group_member(array('groupid' => $group1a->id, 'userid' => $user2->id));
1929 $this->getDataGenerator()->create_group_member(array('groupid' => $group1a->id, 'userid' => $user3->id));
1930 $this->getDataGenerator()->create_group_member(array('groupid' => $group2a->id, 'userid' => $user1->id));
1931 $this->getDataGenerator()->create_group_member(array('groupid' => $group2a->id, 'userid' => $user2->id));
1932
1933 // Get conversation.
1934 $conversation1 = \core_message\api::get_conversation_by_area(
1935 $component,
1936 $itemtype,
1937 $group1a->id,
1938 $coursecontext1->id
1939 );
1940 $conversation2 = \core_message\api::get_conversation_by_area(
1941 $component,
1942 $itemtype,
1943 $group2a->id,
1944 $coursecontext2->id
1945 );
1946
1947 // Send some messages to the conversation.
1948 $gm1 = testhelper::send_fake_message_to_conversation($user1, $conversation1->id, 'Message 1.1', $now + 1);
1949 $gm2 = testhelper::send_fake_message_to_conversation($user1, $conversation1->id, 'Message 1.2', $now + 2);
1950 $gm3 = testhelper::send_fake_message_to_conversation($user2, $conversation1->id, 'Message 1.3', $now + 3);
1951 $gm4 = testhelper::send_fake_message_to_conversation($user1, $conversation2->id, 'Message 2.1', $now + 4);
1952 $gm5 = testhelper::send_fake_message_to_conversation($user2, $conversation2->id, 'Message 2.2', $now + 5);
1953
1954 $dbgm3 = $DB->get_record('messages', ['id' => $gm3]);
1955
1956 // Mark as read one of the conversation messages.
1957 \core_message\api::mark_message_as_read($user1->id, $dbgm3, $now + 5);
1958
1959 // There should be 2 contacts.
1960 $this->assertEquals(2, $DB->count_records('message_contacts'));
1961
1962 // There should be 2 contact requests.
1963 $this->assertEquals(2, $DB->count_records('message_contact_requests'));
1964
1965 // There should be 2 blocked users.
1966 $this->assertEquals(2, $DB->count_records('message_users_blocked'));
1967
1968 // There should be 8 messages.
1969 $this->assertEquals(8, $DB->count_records('messages'));
1970
1971 // There should be 4 user actions - 3 for reading the message, 1 for deleting.
1972 $this->assertEquals(4, $DB->count_records('message_user_actions'));
1973
1974 // There should be 4 conversations - 2 individual + 2 group.
1975 $this->assertEquals(4, $DB->count_records('message_conversations'));
1976
1977 // There should be 9 conversation members - (2 + 2) individual + (3 + 2) group.
1978 $this->assertEquals(9, $DB->count_records('message_conversation_members'));
1979
1980 // There should be 5 notifications - 3 notifications + 2 for the contact request.
1981 $this->assertEquals(5, $DB->count_records('notifications'));
1982
1983 // Delete group conversations for all users in system context.
1984 provider::delete_conversations_for_all_users($systemcontext, $component, $itemtype);
1985
1986 // No conversations should be removed, because they are in the course context.
1987 $this->assertEquals(2, $DB->count_records('message_contacts'));
1988 $this->assertEquals(2, $DB->count_records('message_contact_requests'));
1989 $this->assertEquals(2, $DB->count_records('message_users_blocked'));
1990 $this->assertEquals(8, $DB->count_records('messages'));
1991 $this->assertEquals(4, $DB->count_records('message_user_actions'));
1992 $this->assertEquals(4, $DB->count_records('message_conversations'));
1993 $this->assertEquals(9, $DB->count_records('message_conversation_members'));
1994 $this->assertEquals(5, $DB->count_records('notifications'));
1995
1996 // Delete individual conversations for all users in system context.
1997 provider::delete_conversations_for_all_users($systemcontext, '', '');
1998
1999 // No conversations should be removed, because they've been moved to user context.
2000 $this->assertEquals(2, $DB->count_records('message_contacts'));
2001 $this->assertEquals(2, $DB->count_records('message_contact_requests'));
2002 $this->assertEquals(2, $DB->count_records('message_users_blocked'));
2003 $this->assertEquals(8, $DB->count_records('messages'));
2004 $this->assertEquals(4, $DB->count_records('message_user_actions'));
2005 $this->assertEquals(4, $DB->count_records('message_conversations'));
2006 $this->assertEquals(9, $DB->count_records('message_conversation_members'));
2007 $this->assertEquals(5, $DB->count_records('notifications'));
2008 }
2009
2010 /**
2011 * Test for provider::delete_conversations_for_all_users() in the user context.
2012 */
2013 public function test_delete_conversations_for_all_users_usercontext() {
2014 global $DB;
2015
2016 $this->resetAfterTest();
2017 $this->setAdminUser();
2018 $now = time();
2019 $timeread = $now - DAYSECS;
2020 $component = 'core_group';
2021 $itemtype = 'groups';
2022
2023 // Create users to test with.
2024 $user1 = $this->getDataGenerator()->create_user();
2025 $user2 = $this->getDataGenerator()->create_user();
2026 $user3 = $this->getDataGenerator()->create_user();
2027 $user4 = $this->getDataGenerator()->create_user();
2028 $user5 = $this->getDataGenerator()->create_user();
2029 $user1context = \context_user::instance($user1->id);
2030
2031 // Create contacts.
2032 \core_message\api::add_contact($user1->id, $user2->id);
2033 \core_message\api::add_contact($user2->id, $user3->id);
2034
2035 // Create contact requests.
2036 \core_message\api::create_contact_request($user1->id, $user3->id);
2037 \core_message\api::create_contact_request($user2->id, $user4->id);
2038
2039 // Block a user.
2040 \core_message\api::block_user($user1->id, $user3->id);
2041 \core_message\api::block_user($user3->id, $user4->id);
2042
2043 // Create individual messages.
2044 $im1 = $this->create_message($user1->id, $user2->id, $now + (9 * DAYSECS), true);
2045 $im2 = $this->create_message($user2->id, $user1->id, $now + (8 * DAYSECS), true);
2046 $im3 = $this->create_message($user2->id, $user3->id, $now + (7 * DAYSECS));
2047
2048 // Create notifications.
2049 $n1 = $this->create_notification($user1->id, $user2->id, $now + (9 * DAYSECS), $timeread);
2050 $n2 = $this->create_notification($user2->id, $user1->id, $now + (8 * DAYSECS));
2051 $n3 = $this->create_notification($user2->id, $user3->id, $now + (7 * DAYSECS));
2052
2053 // Delete one of the messages.
2054 \core_message\api::delete_message($user1->id, $im2);
2055
2056 // Create course2.
2057 $course1 = $this->getDataGenerator()->create_course();
2058 $course2 = $this->getDataGenerator()->create_course();
2059 $coursecontext1 = \context_course::instance($course1->id);
2060 $coursecontext2 = \context_course::instance($course2->id);
2061
2062 // Enrol users to courses.
2063 $this->getDataGenerator()->enrol_user($user1->id, $course1->id);
2064 $this->getDataGenerator()->enrol_user($user2->id, $course1->id);
2065 $this->getDataGenerator()->enrol_user($user3->id, $course1->id);
2066 $this->getDataGenerator()->enrol_user($user1->id, $course2->id);
2067 $this->getDataGenerator()->enrol_user($user2->id, $course2->id);
2068
2069 // Create course groups with group messaging enabled.
2070 $group1a = $this->getDataGenerator()->create_group(array('courseid' => $course1->id, 'enablemessaging' => 1));
2071 $group2a = $this->getDataGenerator()->create_group(array('courseid' => $course2->id, 'enablemessaging' => 1));
2072
2073 // Add users to groups.
2074 $this->getDataGenerator()->create_group_member(array('groupid' => $group1a->id, 'userid' => $user1->id));
2075 $this->getDataGenerator()->create_group_member(array('groupid' => $group1a->id, 'userid' => $user2->id));
2076 $this->getDataGenerator()->create_group_member(array('groupid' => $group1a->id, 'userid' => $user3->id));
2077 $this->getDataGenerator()->create_group_member(array('groupid' => $group2a->id, 'userid' => $user1->id));
2078 $this->getDataGenerator()->create_group_member(array('groupid' => $group2a->id, 'userid' => $user2->id));
2079
2080 // Get conversation.
2081 $iconversation1id = \core_message\api::get_conversation_between_users([$user1->id, $user2->id]);
2082 $iconversation2id = \core_message\api::get_conversation_between_users([$user2->id, $user3->id]);
2083 $conversation1 = \core_message\api::get_conversation_by_area(
2084 $component,
2085 $itemtype,
2086 $group1a->id,
2087 $coursecontext1->id
2088 );
2089 $conversation2 = \core_message\api::get_conversation_by_area(
2090 $component,
2091 $itemtype,
2092 $group2a->id,
2093 $coursecontext2->id
2094 );
2095
2096 // Send some messages to the conversation.
2097 $gm1 = testhelper::send_fake_message_to_conversation($user1, $conversation1->id, 'Message 1.1', $now + 1);
2098 $gm2 = testhelper::send_fake_message_to_conversation($user1, $conversation1->id, 'Message 1.2', $now + 2);
2099 $gm3 = testhelper::send_fake_message_to_conversation($user2, $conversation1->id, 'Message 1.3', $now + 3);
2100 $gm4 = testhelper::send_fake_message_to_conversation($user1, $conversation2->id, 'Message 2.1', $now + 4);
2101 $gm5 = testhelper::send_fake_message_to_conversation($user2, $conversation2->id, 'Message 2.2', $now + 5);
2102
2103 $dbgm3 = $DB->get_record('messages', ['id' => $gm3]);
2104
2105 // Mark as read one of the conversation messages.
2106 \core_message\api::mark_message_as_read($user1->id, $dbgm3, $now + 5);
2107
2108 // There should be 2 contacts.
2109 $this->assertEquals(2, $DB->count_records('message_contacts'));
2110
2111 // There should be 2 contact requests.
2112 $this->assertEquals(2, $DB->count_records('message_contact_requests'));
2113
2114 // There should be 2 blocked users.
2115 $this->assertEquals(2, $DB->count_records('message_users_blocked'));
2116
2117 // There should be 8 messages - 3 individual + 5 group.
2118 $this->assertEquals(8, $DB->count_records('messages'));
2119
2120 // There should be 4 user actions - 3 for reading the message, 1 for deleting.
2121 $this->assertEquals(4, $DB->count_records('message_user_actions'));
2122
2123 // There should be 4 conversations - 2 individual + 2 group.
2124 $this->assertEquals(4, $DB->count_records('message_conversations'));
2125
2126 // There should be 9 conversation members - (2 + 2) individual + (3 + 2) group.
2127 $this->assertEquals(9, $DB->count_records('message_conversation_members'));
2128
2129 // There should be 5 notifications - 3 notifications + 2 for the contact request.
2130 $this->assertEquals(5, $DB->count_records('notifications'));
2131
2132 // Delete group conversations for all users in user context.
2133 provider::delete_conversations_for_all_users($user1context, $component, $itemtype);
2134
2135 // No conversations should be removed, because they are in the course context.
2136 $this->assertEquals(2, $DB->count_records('message_contacts'));
2137 $this->assertEquals(2, $DB->count_records('message_contact_requests'));
2138 $this->assertEquals(2, $DB->count_records('message_users_blocked'));
2139 $this->assertEquals(8, $DB->count_records('messages'));
2140 $this->assertEquals(4, $DB->count_records('message_user_actions'));
2141 $this->assertEquals(4, $DB->count_records('message_conversations'));
2142 $this->assertEquals(9, $DB->count_records('message_conversation_members'));
2143 $this->assertEquals(5, $DB->count_records('notifications'));
2144
2145 // Delete individual conversations for all users in user context.
2146 provider::delete_conversations_for_all_users($user1context, '', '');
2147
2148 // No conversations should be removed, because they are in the course context.
2149 $this->assertEquals(2, $DB->count_records('message_contacts'));
2150 $this->assertEquals(2, $DB->count_records('message_contact_requests'));
2151 $this->assertEquals(2, $DB->count_records('message_users_blocked'));
2152 $this->assertEquals(8, $DB->count_records('messages'));
2153 $this->assertEquals(4, $DB->count_records('message_user_actions'));
2154 $this->assertEquals(4, $DB->count_records('message_conversations'));
2155 $this->assertEquals(9, $DB->count_records('message_conversation_members'));
2156 $this->assertEquals(5, $DB->count_records('notifications'));
2157 }
2158
2159 /**
2160 * Test for provider::delete_conversations_for_user().
2161 */
2162 public function test_delete_conversations_for_user() {
2163 global $DB;
2164
2165 $this->resetAfterTest();
2166 $this->setAdminUser();
2167 $now = time();
2168 $timeread = $now - DAYSECS;
2169 $systemcontext = \context_system::instance();
2170 $component = 'core_group';
2171 $itemtype = 'groups';
2172
2173 // Create users to test with.
2174 $user1 = $this->getDataGenerator()->create_user();
2175 $user2 = $this->getDataGenerator()->create_user();
2176 $user3 = $this->getDataGenerator()->create_user();
2177 $user4 = $this->getDataGenerator()->create_user();
2178 $user5 = $this->getDataGenerator()->create_user();
2179 $user1context = \context_user::instance($user1->id);
2180
2181 // Create contacts.
2182 \core_message\api::add_contact($user1->id, $user2->id);
2183 \core_message\api::add_contact($user2->id, $user3->id);
2184
2185 // Create contact requests.
2186 \core_message\api::create_contact_request($user1->id, $user3->id);
2187 \core_message\api::create_contact_request($user2->id, $user4->id);
2188
2189 // Block a user.
2190 \core_message\api::block_user($user1->id, $user3->id);
2191 \core_message\api::block_user($user3->id, $user4->id);
2192
2193 // Create private messages.
2194 $pm1 = $this->create_message($user1->id, $user2->id, $now + (9 * DAYSECS), true);
2195 $pm2 = $this->create_message($user2->id, $user1->id, $now + (8 * DAYSECS), true);
2196 $pm3 = $this->create_message($user2->id, $user3->id, $now + (7 * DAYSECS));
2197
2198 // Create notifications.
2199 $n1 = $this->create_notification($user1->id, $user2->id, $now + (9 * DAYSECS), $timeread);
2200 $n2 = $this->create_notification($user2->id, $user1->id, $now + (8 * DAYSECS));
2201 $n3 = $this->create_notification($user2->id, $user3->id, $now + (7 * DAYSECS));
2202
2203 // Delete one of the messages.
2204 \core_message\api::delete_message($user1->id, $pm2);
2205
2206 // Create course.
2207 $course1 = $this->getDataGenerator()->create_course();
2208 $course2 = $this->getDataGenerator()->create_course();
2209 $coursecontext1 = \context_course::instance($course1->id);
2210 $coursecontext2 = \context_course::instance($course2->id);
2211
2212 // Enrol users to courses.
2213 $this->getDataGenerator()->enrol_user($user1->id, $course1->id);
2214 $this->getDataGenerator()->enrol_user($user2->id, $course1->id);
2215 $this->getDataGenerator()->enrol_user($user3->id, $course1->id);
2216 $this->getDataGenerator()->enrol_user($user1->id, $course2->id);
2217
2218 // Create course groups with group messaging enabled.
2219 $group1a = $this->getDataGenerator()->create_group(array('courseid' => $course1->id, 'enablemessaging' => 1));
2220
2221 // Add users to groups.
2222 $this->getDataGenerator()->create_group_member(array('groupid' => $group1a->id, 'userid' => $user1->id));
2223 $this->getDataGenerator()->create_group_member(array('groupid' => $group1a->id, 'userid' => $user2->id));
2224 $this->getDataGenerator()->create_group_member(array('groupid' => $group1a->id, 'userid' => $user3->id));
2225
2226 // Get conversation.
2227 $conversation1 = \core_message\api::get_conversation_by_area(
2228 $component,
2229 $itemtype,
2230 $group1a->id,
2231 $coursecontext1->id
2232 );
2233
2234 // Send some messages to the conversation.
2235 $gm1 = testhelper::send_fake_message_to_conversation($user1, $conversation1->id, 'Message 1', $now + 1);
2236 $gm2 = testhelper::send_fake_message_to_conversation($user1, $conversation1->id, 'Message 2', $now + 2);
2237 $gm3 = testhelper::send_fake_message_to_conversation($user2, $conversation1->id, 'Message 3', $now + 3);
2238
2239 $dbm3 = $DB->get_record('messages', ['id' => $gm3]);
2240
2241 // Mark as read one of the conversation messages.
2242 \core_message\api::mark_message_as_read($user1->id, $dbm3, $now + 5);
2243
2244 // There should be 2 contacts.
2245 $this->assertEquals(2, $DB->count_records('message_contacts'));
2246
2247 // There should be 2 contact requests.
2248 $this->assertEquals(2, $DB->count_records('message_contact_requests'));
2249
2250 // There should be 2 blocked users.
2251 $this->assertEquals(2, $DB->count_records('message_users_blocked'));
2252
2253 // There should be 3 notifications + 2 for the contact request.
2254 $this->assertEquals(5, $DB->count_records('notifications'));
2255
2256 // There should be 6 messages.
2257 $this->assertEquals(6, $DB->count_records('messages'));
2258
2259 // There should be 4 user actions - 3 for reading the message, one for deleting.
2260 $this->assertEquals(4, $DB->count_records('message_user_actions'));
2261
2262 // There should be 3 conversations - 2 private + 1 group.
2263 $this->assertEquals(3, $DB->count_records('message_conversations'));
2264
2265 // There should be 7 conversation members - 2 + 2 private conversations + 3 group conversation.
2266 $this->assertEquals(7, $DB->count_records('message_conversation_members'));
2267 $members = $DB->get_records('message_conversation_members', ['conversationid' => $conversation1->id]);
2268 $members = array_map(function($member) {
2269 return $member->userid;
2270 }, $members);
2271 $this->assertContains($user1->id, $members);
2272
2273 // Delete group conversations for user1 in course1 and course2.
2274 $approvedcontextlist = new \core_privacy\tests\request\approved_contextlist($user1, 'core_message',
2275 [$coursecontext1->id, $coursecontext2->id]);
2276 provider::delete_conversations_for_user($approvedcontextlist, $component, $itemtype);
2277
2278 // There should be still 2 contacts.
2279 $this->assertEquals(2, $DB->count_records('message_contacts'));
2280
2281 // There should be still 2 contact requests.
2282 $this->assertEquals(2, $DB->count_records('message_contact_requests'));
2283
2284 // There should be still 2 blocked users.
2285 $this->assertEquals(2, $DB->count_records('message_users_blocked'));
2286
2287 // There should be still 3 notifications + 2 for the contact request.
2288 $this->assertEquals(5, $DB->count_records('notifications'));
2289
2290 // There should be 4 messages - 3 private + 1 group sent by user2.
2291 $this->assertEquals(4, $DB->count_records('messages'));
2292 $messages = array_keys($DB->get_records('messages'));
2293 $this->assertContains($pm1, $messages);
2294 $this->assertContains($pm2, $messages);
2295 $this->assertContains($pm3, $messages);
2296 $this->assertContains($gm3, $messages);
2297
2298 // There should be 3 user actions - 2 for reading the message, one for deleting.
2299 $this->assertEquals(3, $DB->count_records('message_user_actions'));
2300 $useractions = $DB->get_records('message_user_actions');
2301 $useractions = array_map(function($action) {
2302 return $action->messageid;
2303 }, $useractions);
2304 $this->assertNotContains($gm3, $useractions);
2305
2306 // There should be still 3 conversations - 2 private + 1 group.
2307 $this->assertEquals(3, $DB->count_records('message_conversations'));
2308
2309 // There should be 6 conversation members - 2 + 2 private conversations + 2 group conversation.
2310 $this->assertEquals(6, $DB->count_records('message_conversation_members'));
2311 $members = $DB->get_records('message_conversation_members', ['conversationid' => $conversation1->id]);
2312 $members = array_map(function($member) {
2313 return $member->userid;
2314 }, $members);
2315 $this->assertNotContains($user1->id, $members);
2316 }
2317
2318
2319 /**
2320 * Test for provider::delete_conversations_for_users().
2321 */
2322 public function test_delete_conversations_for_users() {
2323 global $DB;
2324
2325 $this->resetAfterTest();
2326 $this->setAdminUser();
2327 $now = time();
2328 $timeread = $now - DAYSECS;
2329 $systemcontext = \context_system::instance();
2330 $component = 'core_group';
2331 $itemtype = 'groups';
2332
2333 // Create users to test with.
2334 $user1 = $this->getDataGenerator()->create_user();
2335 $user2 = $this->getDataGenerator()->create_user();
2336 $user3 = $this->getDataGenerator()->create_user();
2337 $user4 = $this->getDataGenerator()->create_user();
2338 $user5 = $this->getDataGenerator()->create_user();
2339 $user1context = \context_user::instance($user1->id);
2340
2341 // Create contacts.
2342 \core_message\api::add_contact($user1->id, $user2->id);
2343 \core_message\api::add_contact($user2->id, $user3->id);
2344
2345 // Create contact requests.
2346 \core_message\api::create_contact_request($user1->id, $user3->id);
2347 \core_message\api::create_contact_request($user2->id, $user4->id);
2348
2349 // Block a user.
2350 \core_message\api::block_user($user1->id, $user3->id);
2351 \core_message\api::block_user($user3->id, $user4->id);
2352
2353 // Create private messages.
2354 $pm1 = $this->create_message($user1->id, $user2->id, $now + (9 * DAYSECS), true);
2355 $pm2 = $this->create_message($user2->id, $user1->id, $now + (8 * DAYSECS), true);
2356 $pm3 = $this->create_message($user2->id, $user3->id, $now + (7 * DAYSECS));
2357
2358 // Create notifications.
2359 $n1 = $this->create_notification($user1->id, $user2->id, $now + (9 * DAYSECS), $timeread);
2360 $n2 = $this->create_notification($user2->id, $user1->id, $now + (8 * DAYSECS));
2361 $n3 = $this->create_notification($user2->id, $user3->id, $now + (7 * DAYSECS));
2362
2363 // Delete one of the messages.
2364 \core_message\api::delete_message($user1->id, $pm2);
2365
2366 // Create course.
2367 $course1 = $this->getDataGenerator()->create_course();
2368 $course2 = $this->getDataGenerator()->create_course();
2369 $coursecontext1 = \context_course::instance($course1->id);
2370 $coursecontext2 = \context_course::instance($course2->id);
2371
2372 // Enrol users to courses.
2373 $this->getDataGenerator()->enrol_user($user1->id, $course1->id);
2374 $this->getDataGenerator()->enrol_user($user2->id, $course1->id);
2375 $this->getDataGenerator()->enrol_user($user3->id, $course1->id);
2376 $this->getDataGenerator()->enrol_user($user4->id, $course1->id);
2377 $this->getDataGenerator()->enrol_user($user1->id, $course2->id);
2378
2379 // Create course groups with group messaging enabled.
2380 $group1a = $this->getDataGenerator()->create_group(array('courseid' => $course1->id, 'enablemessaging' => 1));
2381 $group2a = $this->getDataGenerator()->create_group(array('courseid' => $course2->id, 'enablemessaging' => 1));
2382
2383 // Add users to groups.
2384 $this->getDataGenerator()->create_group_member(array('groupid' => $group1a->id, 'userid' => $user1->id));
2385 $this->getDataGenerator()->create_group_member(array('groupid' => $group1a->id, 'userid' => $user2->id));
2386 $this->getDataGenerator()->create_group_member(array('groupid' => $group1a->id, 'userid' => $user3->id));
2387 $this->getDataGenerator()->create_group_member(array('groupid' => $group1a->id, 'userid' => $user4->id));
2388
2389 // Get conversation.
2390 $conversation1 = \core_message\api::get_conversation_by_area(
2391 $component,
2392 $itemtype,
2393 $group1a->id,
2394 $coursecontext1->id
2395 );
2396
2397 // Send some messages to the conversation.
2398 $gm1 = testhelper::send_fake_message_to_conversation($user1, $conversation1->id, 'Message 1', $now + 1);
2399 $gm2 = testhelper::send_fake_message_to_conversation($user2, $conversation1->id, 'Message 2', $now + 2);
2400 $gm3 = testhelper::send_fake_message_to_conversation($user3, $conversation1->id, 'Message 3', $now + 3);
2401
2402 $dbm3 = $DB->get_record('messages', ['id' => $gm3]);
2403
2404 // Mark as read one of the conversation messages.
2405 \core_message\api::mark_message_as_read($user1->id, $dbm3, $now + 5);
2406
2407 // There should be 2 contacts.
2408 $this->assertEquals(2, $DB->count_records('message_contacts'));
2409
2410 // There should be 2 contact requests.
2411 $this->assertEquals(2, $DB->count_records('message_contact_requests'));
2412
2413 // There should be 2 blocked users.
2414 $this->assertEquals(2, $DB->count_records('message_users_blocked'));
2415
2416 // There should be 3 notifications + 2 for the contact request.
2417 $this->assertEquals(5, $DB->count_records('notifications'));
2418
2419 // There should be 6 messages.
2420 $this->assertEquals(6, $DB->count_records('messages'));
2421
2422 // There should be 4 user actions - 3 for reading the message, one for deleting.
2423 $this->assertEquals(4, $DB->count_records('message_user_actions'));
2424
2425 // There should be 3 conversations - 2 private + 2 group.
2426 $this->assertEquals(4, $DB->count_records('message_conversations'));
2427
2428 // There should be 8 conversation members - (2 + 2) private + 4 group.
2429 $this->assertEquals(8, $DB->count_records('message_conversation_members'));
2430 $members = $DB->get_records('message_conversation_members', ['conversationid' => $conversation1->id]);
2431 $members = array_map(function($member) {
2432 return $member->userid;
2433 }, $members);
2434 $this->assertContains($user1->id, $members);
2435 $this->assertContains($user4->id, $members);
2436
2437 // Delete group conversations for user1 and user2 in course2 context.
2438 $approveduserlist = new \core_privacy\local\request\approved_userlist($coursecontext2, 'core_message',
2439 [$user1->id, $user2->id]);
2440 provider::delete_conversations_for_users($approveduserlist, $component, $itemtype);
2441
2442 // There should be exactly the same content, because $user1 and $user2 don't belong to any group in course2).
2443 $this->assertEquals(2, $DB->count_records('message_contacts'));
2444 $this->assertEquals(2, $DB->count_records('message_contact_requests'));
2445 $this->assertEquals(2, $DB->count_records('message_users_blocked'));
2446 $this->assertEquals(5, $DB->count_records('notifications'));
2447 $this->assertEquals(6, $DB->count_records('messages'));
2448 $this->assertEquals(4, $DB->count_records('message_user_actions'));
2449 $this->assertEquals(4, $DB->count_records('message_conversations'));
2450 $this->assertEquals(8, $DB->count_records('message_conversation_members'));
2451
2452 // Delete group conversations for user4 in course1 context.
2453 $approveduserlist = new \core_privacy\local\request\approved_userlist($coursecontext1, 'core_message',
2454 [$user4->id]);
2455 provider::delete_conversations_for_users($approveduserlist, $component, $itemtype);
2456
2457 // There should be the same content except for the members (to remove user4 from the group1 in course1).
2458 $this->assertEquals(2, $DB->count_records('message_contacts'));
2459 $this->assertEquals(2, $DB->count_records('message_contact_requests'));
2460 $this->assertEquals(2, $DB->count_records('message_users_blocked'));
2461 $this->assertEquals(5, $DB->count_records('notifications'));
2462 $this->assertEquals(6, $DB->count_records('messages'));
2463 $this->assertEquals(4, $DB->count_records('message_user_actions'));
2464 $this->assertEquals(4, $DB->count_records('message_conversations'));
2465 // There should be 7 conversation members - (2 + 2) private + 3 group.
2466 $this->assertEquals(7, $DB->count_records('message_conversation_members'));
2467
2468 // Delete group conversations for user1 and user2 in course1 context.
2469 $approveduserlist = new \core_privacy\local\request\approved_userlist($coursecontext1, 'core_message',
2470 [$user1->id, $user2->id]);
2471 provider::delete_conversations_for_users($approveduserlist, $component, $itemtype);
2472
2473 // There should be still 2 contacts.
2474 $this->assertEquals(2, $DB->count_records('message_contacts'));
2475
2476 // There should be still 2 contact requests.
2477 $this->assertEquals(2, $DB->count_records('message_contact_requests'));
2478
2479 // There should be still 2 blocked users.
2480 $this->assertEquals(2, $DB->count_records('message_users_blocked'));
2481
2482 // There should be still 3 notifications + 2 for the contact request.
2483 $this->assertEquals(5, $DB->count_records('notifications'));
2484
2485 // There should be 4 messages - 3 private + 1 group sent by user3.
2486 $this->assertEquals(4, $DB->count_records('messages'));
2487 $messages = array_keys($DB->get_records('messages'));
2488 $this->assertContains($pm1, $messages);
2489 $this->assertContains($pm2, $messages);
2490 $this->assertContains($pm3, $messages);
2491 $this->assertContains($gm3, $messages);
2492
2493 // There should be 3 user actions - 2 for reading the message, one for deleting.
2494 $this->assertEquals(3, $DB->count_records('message_user_actions'));
2495 $useractions = $DB->get_records('message_user_actions');
2496 $useractions = array_map(function($action) {
2497 return $action->messageid;
2498 }, $useractions);
2499 $this->assertNotContains($gm3, $useractions);
2500
2501 // There should be still 4 conversations - 2 private + 2 group.
2502 $this->assertEquals(4, $DB->count_records('message_conversations'));
2503
2504 // There should be 5 conversation members - (2 + 2) private + 1 group.
2505 $this->assertEquals(5, $DB->count_records('message_conversation_members'));
2506 $members = $DB->get_records('message_conversation_members', ['conversationid' => $conversation1->id]);
2507 $members = array_map(function($member) {
2508 return $member->userid;
2509 }, $members);
2510 $this->assertNotContains($user1->id, $members);
2511 $this->assertNotContains($user2->id, $members);
2512 }
2513
04cd1b3f
MN
2514 /**
2515 * Creates a message to be used for testing.
2516 *
2517 * @param int $useridfrom The user id from
2518 * @param int $useridto The user id to
2519 * @param int $timecreated
2520 * @param bool $read Do we want to mark the message as read?
2521 * @return int The id of the message
2522 * @throws dml_exception
2523 */
2524 private function create_message(int $useridfrom, int $useridto, int $timecreated = null, bool $read = false) {
2525 global $DB;
2526
2527 static $i = 1;
2528
2529 if (is_null($timecreated)) {
2530 $timecreated = time();
2531 }
2532
2533 if (!$conversationid = \core_message\api::get_conversation_between_users([$useridfrom, $useridto])) {
f2ac0a3e
MN
2534 $conversation = \core_message\api::create_conversation(
2535 \core_message\api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL,
2536 [
2537 $useridfrom,
2538 $useridto
2539 ]
2540 );
2541 $conversationid = $conversation->id;
04cd1b3f
MN
2542 }
2543
2544 // Ok, send the message.
2545 $record = new stdClass();
2546 $record->useridfrom = $useridfrom;
2547 $record->conversationid = $conversationid;
2548 $record->subject = 'No subject';
2549 $record->fullmessage = 'A rad message ' . $i;
2550 $record->smallmessage = 'A rad message ' . $i;
2551 $record->timecreated = $timecreated;
2552
2553 $i++;
2554
2555 $record->id = $DB->insert_record('messages', $record);
2556
2557 if ($read) {
2558 \core_message\api::mark_message_as_read($useridto, $record);
2559 }
2560
2561 return $record->id;
2562 }
2563
2564 /**
2565 * Creates a notification to be used for testing.
2566 *
2567 * @param int $useridfrom The user id from
2568 * @param int $useridto The user id to
2569 * @param int|null $timecreated The time the notification was created
2570 * @param int|null $timeread The time the notification was read, null if it hasn't been.
2571 * @return int The id of the notification
2572 * @throws dml_exception
2573 */
2574 private function create_notification(int $useridfrom, int $useridto, int $timecreated = null, int $timeread = null) {
2575 global $DB;
2576
2577 static $i = 1;
2578
2579 if (is_null($timecreated)) {
2580 $timecreated = time();
2581 }
2582
2583 $record = new stdClass();
2584 $record->useridfrom = $useridfrom;
2585 $record->useridto = $useridto;
2586 $record->subject = 'No subject';
2587 $record->fullmessage = 'Some rad notification ' . $i;
2588 $record->smallmessage = 'Yo homie, you got some stuff to do, yolo. ' . $i;
2589 $record->timeread = $timeread;
2590 $record->timecreated = $timecreated;
2591
2592 $i++;
2593
2594 return $DB->insert_record('notifications', $record);
2595 }
823e08bf
AN
2596
2597 /**
2598 * Comparison function for sorting messages.
2599 *
2600 * @param \stdClass $a
2601 * @param \stdClass $b
2602 * @return bool
2603 */
2604 protected static function sort_messages($a, $b) {
2605 return $a->message > $b->message;
2606 }
2607
2608 /**
2609 * Comparison function for sorting contacts.
2610 *
2611 * @param \stdClass $a
2612 * @param \stdClass $b
2613 * @return bool
2614 */
2615 protected static function sort_contacts($a, $b) {
2616 return $a->contact > $b->contact;
2617 }
04cd1b3f 2618}