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