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