MDL-63969 core_course: Store favourite courses in the course context
[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;
27use \core_privacy\local\request\writer;
28use \core_privacy\local\request\transform;
29
30defined('MOODLE_INTERNAL') || die();
31
32/**
33 * Privacy provider tests class.
34 *
35 * @package core_message
36 * @copyright 2018 Mark Nelson <markn@moodle.com>
37 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
38 */
39class core_message_privacy_provider_testcase extends \core_privacy\tests\provider_testcase {
40
41 /**
42 * Test for provider::get_metadata().
43 */
44 public function test_get_metadata() {
45 $collection = new collection('core_message');
46 $newcollection = provider::get_metadata($collection);
47 $itemcollection = $newcollection->get_collection();
742c27b3 48 $this->assertCount(8, $itemcollection);
04cd1b3f
MN
49
50 $messagestable = array_shift($itemcollection);
51 $this->assertEquals('messages', $messagestable->get_name());
52
53 $messageuseractionstable = array_shift($itemcollection);
54 $this->assertEquals('message_user_actions', $messageuseractionstable->get_name());
55
56 $messageconversationmemberstable = array_shift($itemcollection);
57 $this->assertEquals('message_conversation_members', $messageconversationmemberstable->get_name());
58
59 $messagecontacts = array_shift($itemcollection);
60 $this->assertEquals('message_contacts', $messagecontacts->get_name());
61
742c27b3
MN
62 $messagecontactrequests = array_shift($itemcollection);
63 $this->assertEquals('message_contact_requests', $messagecontactrequests->get_name());
64
65 $messageusersblocked = array_shift($itemcollection);
66 $this->assertEquals('message_users_blocked', $messageusersblocked->get_name());
67
04cd1b3f
MN
68 $notificationstable = array_shift($itemcollection);
69 $this->assertEquals('notifications', $notificationstable->get_name());
70
71 $usersettings = array_shift($itemcollection);
72 $this->assertEquals('core_message_messageprovider_settings', $usersettings->get_name());
73
74 $privacyfields = $messagestable->get_privacy_fields();
75 $this->assertArrayHasKey('useridfrom', $privacyfields);
76 $this->assertArrayHasKey('conversationid', $privacyfields);
77 $this->assertArrayHasKey('subject', $privacyfields);
78 $this->assertArrayHasKey('fullmessage', $privacyfields);
79 $this->assertArrayHasKey('fullmessageformat', $privacyfields);
80 $this->assertArrayHasKey('fullmessagehtml', $privacyfields);
81 $this->assertArrayHasKey('smallmessage', $privacyfields);
82 $this->assertArrayHasKey('timecreated', $privacyfields);
83 $this->assertEquals('privacy:metadata:messages', $messagestable->get_summary());
84
85 $privacyfields = $messageuseractionstable->get_privacy_fields();
86 $this->assertArrayHasKey('userid', $privacyfields);
87 $this->assertArrayHasKey('messageid', $privacyfields);
88 $this->assertArrayHasKey('action', $privacyfields);
89 $this->assertArrayHasKey('timecreated', $privacyfields);
90 $this->assertEquals('privacy:metadata:message_user_actions', $messageuseractionstable->get_summary());
91
92 $privacyfields = $messageconversationmemberstable->get_privacy_fields();
93 $this->assertArrayHasKey('conversationid', $privacyfields);
94 $this->assertArrayHasKey('userid', $privacyfields);
95 $this->assertArrayHasKey('timecreated', $privacyfields);
96 $this->assertEquals('privacy:metadata:message_conversation_members', $messageconversationmemberstable->get_summary());
97
98 $privacyfields = $messagecontacts->get_privacy_fields();
99 $this->assertArrayHasKey('userid', $privacyfields);
100 $this->assertArrayHasKey('contactid', $privacyfields);
742c27b3 101 $this->assertArrayHasKey('timecreated', $privacyfields);
04cd1b3f
MN
102 $this->assertEquals('privacy:metadata:message_contacts', $messagecontacts->get_summary());
103
742c27b3
MN
104 $privacyfields = $messagecontactrequests->get_privacy_fields();
105 $this->assertArrayHasKey('userid', $privacyfields);
106 $this->assertArrayHasKey('requesteduserid', $privacyfields);
107 $this->assertArrayHasKey('timecreated', $privacyfields);
108 $this->assertEquals('privacy:metadata:message_contact_requests', $messagecontactrequests->get_summary());
109
110 $privacyfields = $messageusersblocked->get_privacy_fields();
111 $this->assertArrayHasKey('userid', $privacyfields);
112 $this->assertArrayHasKey('blockeduserid', $privacyfields);
113 $this->assertArrayHasKey('timecreated', $privacyfields);
114 $this->assertEquals('privacy:metadata:message_users_blocked', $messageusersblocked->get_summary());
115
04cd1b3f
MN
116 $privacyfields = $notificationstable->get_privacy_fields();
117 $this->assertArrayHasKey('useridfrom', $privacyfields);
118 $this->assertArrayHasKey('useridto', $privacyfields);
119 $this->assertArrayHasKey('subject', $privacyfields);
120 $this->assertArrayHasKey('fullmessage', $privacyfields);
121 $this->assertArrayHasKey('fullmessageformat', $privacyfields);
122 $this->assertArrayHasKey('fullmessagehtml', $privacyfields);
123 $this->assertArrayHasKey('smallmessage', $privacyfields);
124 $this->assertArrayHasKey('component', $privacyfields);
125 $this->assertArrayHasKey('eventtype', $privacyfields);
126 $this->assertArrayHasKey('contexturl', $privacyfields);
127 $this->assertArrayHasKey('contexturlname', $privacyfields);
128 $this->assertArrayHasKey('timeread', $privacyfields);
129 $this->assertArrayHasKey('timecreated', $privacyfields);
130 $this->assertEquals('privacy:metadata:notifications', $notificationstable->get_summary());
131 }
132
133 /**
134 * Test for provider::export_user_preferences().
135 */
136 public function test_export_user_preferences_no_pref() {
137 $this->resetAfterTest();
138
139 $user = $this->getDataGenerator()->create_user();
140 provider::export_user_preferences($user->id);
141
142 $writer = writer::with_context(\context_system::instance());
143
144 $this->assertFalse($writer->has_any_data());
145 }
146
147 /**
148 * Test for provider::export_user_preferences().
149 */
150 public function test_export_user_preferences() {
151 global $USER;
152
153 $this->resetAfterTest();
154
155 $this->setAdminUser();
156
157 // Create another user to set a preference for who we won't be exporting.
158 $user = $this->getDataGenerator()->create_user();
159
160 // Set some message user preferences.
161 set_user_preference('message_provider_moodle_instantmessage_loggedin', 'airnotifier', $USER->id);
162 set_user_preference('message_provider_moodle_instantmessage_loggedoff', 'popup', $USER->id);
f7dfa9ba 163 set_user_preference('message_blocknoncontacts', \core_message\api::MESSAGE_PRIVACY_ONLYCONTACTS, $USER->id);
04cd1b3f
MN
164 set_user_preference('message_provider_moodle_instantmessage_loggedoff', 'inbound', $user->id);
165
166 // Set an unrelated preference.
bbe6eadd 167 set_user_preference('some_unrelated_preference', 'courses', $USER->id);
04cd1b3f
MN
168
169 provider::export_user_preferences($USER->id);
170
171 $writer = writer::with_context(\context_system::instance());
172
173 $this->assertTrue($writer->has_any_data());
174
175 $prefs = (array) $writer->get_user_preferences('core_message');
176
177 // Check only 3 preferences exist.
178 $this->assertCount(3, $prefs);
179 $this->assertArrayHasKey('message_provider_moodle_instantmessage_loggedin', $prefs);
180 $this->assertArrayHasKey('message_provider_moodle_instantmessage_loggedoff', $prefs);
181 $this->assertArrayHasKey('message_blocknoncontacts', $prefs);
182
183 foreach ($prefs as $key => $pref) {
184 if ($key == 'message_provider_moodle_instantmessage_loggedin') {
185 $this->assertEquals('airnotifier', $pref->value);
186 } else if ($key == 'message_provider_moodle_instantmessage_loggedoff') {
187 $this->assertEquals('popup', $pref->value);
188 } else {
189 $this->assertEquals(1, $pref->value);
190 }
191 }
192 }
193
194 /**
52334296 195 * Test for provider::get_contexts_for_userid() when there is no message or notification.
04cd1b3f 196 */
52334296 197 public function test_get_contexts_for_userid_no_data() {
04cd1b3f
MN
198 $this->resetAfterTest();
199
200 $user = $this->getDataGenerator()->create_user();
201 $contextlist = provider::get_contexts_for_userid($user->id);
52334296
SR
202 $this->assertEmpty($contextlist);
203 }
204
205 /**
206 * Test for provider::get_contexts_for_userid() when there is a message between users.
207 */
208 public function test_get_contexts_for_userid_with_message() {
209 $this->resetAfterTest();
210
211 $user1 = $this->getDataGenerator()->create_user();
212 $user2 = $this->getDataGenerator()->create_user();
213
b8bbef0a
MH
214 // Test nothing is found before message is sent.
215 $contextlist = provider::get_contexts_for_userid($user1->id);
216 $this->assertCount(0, $contextlist);
217 $contextlist = provider::get_contexts_for_userid($user2->id);
218 $this->assertCount(0, $contextlist);
219
52334296
SR
220 $this->create_message($user1->id, $user2->id, time() - (9 * DAYSECS));
221
222 // Test for the sender.
223 $contextlist = provider::get_contexts_for_userid($user1->id);
224 $this->assertCount(1, $contextlist);
225 $contextforuser = $contextlist->current();
226 $this->assertEquals(
227 context_user::instance($user1->id)->id,
228 $contextforuser->id);
229
230 // Test for the receiver.
231 $contextlist = provider::get_contexts_for_userid($user2->id);
232 $this->assertCount(1, $contextlist);
233 $contextforuser = $contextlist->current();
234 $this->assertEquals(
235 context_user::instance($user2->id)->id,
236 $contextforuser->id);
237 }
238
239 /**
240 * Test for provider::get_contexts_for_userid() when there is a notification between users.
241 */
242 public function test_get_contexts_for_userid_with_notification() {
243 $this->resetAfterTest();
244
245 $user1 = $this->getDataGenerator()->create_user();
246 $user2 = $this->getDataGenerator()->create_user();
247
b8bbef0a
MH
248 // Test nothing is found before notification is created.
249 $contextlist = provider::get_contexts_for_userid($user1->id);
250 $this->assertCount(0, $contextlist);
251 $contextlist = provider::get_contexts_for_userid($user2->id);
252 $this->assertCount(0, $contextlist);
253
52334296
SR
254 $this->create_notification($user1->id, $user2->id, time() - (9 * DAYSECS));
255
256 // Test for the sender.
257 $contextlist = provider::get_contexts_for_userid($user1->id);
258 $this->assertCount(1, $contextlist);
259 $contextforuser = $contextlist->current();
260 $this->assertEquals(
261 context_user::instance($user1->id)->id,
262 $contextforuser->id);
263
264 // Test for the receiver.
265 $contextlist = provider::get_contexts_for_userid($user2->id);
04cd1b3f
MN
266 $this->assertCount(1, $contextlist);
267 $contextforuser = $contextlist->current();
52334296
SR
268 $this->assertEquals(
269 context_user::instance($user2->id)->id,
270 $contextforuser->id);
04cd1b3f
MN
271 }
272
b8bbef0a
MH
273 /**
274 * Test for provider::get_contexts_for_userid() when a users has a contact.
275 */
276 public function test_get_contexts_for_userid_with_contact() {
277 $this->resetAfterTest();
278
279 $user1 = $this->getDataGenerator()->create_user();
280 $user2 = $this->getDataGenerator()->create_user();
281
282 // Test nothing is found before contact is created.
283 $contextlist = provider::get_contexts_for_userid($user1->id);
284 $this->assertCount(0, $contextlist);
285 $contextlist = provider::get_contexts_for_userid($user2->id);
286 $this->assertCount(0, $contextlist);
287
288 \core_message\api::add_contact($user1->id, $user2->id);
289
290 // Test for the user adding the contact.
291 $contextlist = provider::get_contexts_for_userid($user1->id);
292 $this->assertCount(1, $contextlist);
293 $contextforuser = $contextlist->current();
294 $this->assertEquals(
295 context_user::instance($user1->id)->id,
296 $contextforuser->id);
297
298 // Test for the user who is the contact.
299 $contextlist = provider::get_contexts_for_userid($user2->id);
300 $this->assertCount(1, $contextlist);
301 $contextforuser = $contextlist->current();
302 $this->assertEquals(
303 context_user::instance($user2->id)->id,
304 $contextforuser->id);
305 }
306
307 /**
308 * Test for provider::get_contexts_for_userid() when a user makes a contact request.
309 */
310 public function test_get_contexts_for_userid_with_contact_request() {
311 $this->resetAfterTest();
312
313 $user1 = $this->getDataGenerator()->create_user();
314 $user2 = $this->getDataGenerator()->create_user();
315
316 // Test nothing is found before request is created.
317 $contextlist = provider::get_contexts_for_userid($user1->id);
318 $this->assertCount(0, $contextlist);
319 $contextlist = provider::get_contexts_for_userid($user2->id);
320 $this->assertCount(0, $contextlist);
321
322 \core_message\api::create_contact_request($user1->id, $user2->id);
323
324 // Test for the user requesting the contact.
325 $contextlist = provider::get_contexts_for_userid($user1->id);
326 $this->assertCount(1, $contextlist);
327 $contextforuser = $contextlist->current();
328 $this->assertEquals(
329 context_user::instance($user1->id)->id,
330 $contextforuser->id);
331
332 // Test for the user receiving the contact request.
333 $contextlist = provider::get_contexts_for_userid($user2->id);
334 $this->assertCount(1, $contextlist);
335 $contextforuser = $contextlist->current();
336 $this->assertEquals(
337 context_user::instance($user2->id)->id,
338 $contextforuser->id);
339 }
340
341 /**
342 * Test for provider::get_contexts_for_userid() when a user is blocked.
343 */
344 public function test_get_contexts_for_userid_with_blocked_contact() {
345 $this->resetAfterTest();
346
347 $user1 = $this->getDataGenerator()->create_user();
348 $user2 = $this->getDataGenerator()->create_user();
349
350 // Test nothing is found before user is blocked.
351 $contextlist = provider::get_contexts_for_userid($user1->id);
352 $this->assertCount(0, $contextlist);
353 $contextlist = provider::get_contexts_for_userid($user2->id);
354 $this->assertCount(0, $contextlist);
355
356 \core_message\api::block_user($user1->id, $user2->id);
357
358 // Test for the blocking user.
359 $contextlist = provider::get_contexts_for_userid($user1->id);
360 $this->assertCount(1, $contextlist);
361 $contextforuser = $contextlist->current();
362 $this->assertEquals(
363 context_user::instance($user1->id)->id,
364 $contextforuser->id);
365
366 // Test for the user who is blocked.
367 $contextlist = provider::get_contexts_for_userid($user2->id);
368 $this->assertCount(1, $contextlist);
369 $contextforuser = $contextlist->current();
370 $this->assertEquals(
371 context_user::instance($user2->id)->id,
372 $contextforuser->id);
373 }
374
04cd1b3f
MN
375 /**
376 * Test for provider::export_user_data().
377 */
378 public function test_export_for_context_with_contacts() {
379 $this->resetAfterTest();
380
381 // Create users to test with.
382 $user1 = $this->getDataGenerator()->create_user();
383 $user2 = $this->getDataGenerator()->create_user();
384 $user3 = $this->getDataGenerator()->create_user();
385 $user4 = $this->getDataGenerator()->create_user();
386
742c27b3
MN
387 \core_message\api::add_contact($user1->id, $user2->id);
388 \core_message\api::add_contact($user1->id, $user3->id);
389 \core_message\api::add_contact($user1->id, $user4->id);
04cd1b3f 390
52334296 391 $user1context = context_user::instance($user1->id);
04cd1b3f 392
52334296
SR
393 $this->export_context_data_for_user($user1->id, $user1context, 'core_message');
394
395 $writer = writer::with_context($user1context);
04cd1b3f
MN
396
397 $contacts = (array) $writer->get_data([get_string('contacts', 'core_message')]);
823e08bf 398 usort($contacts, ['static', 'sort_contacts']);
04cd1b3f
MN
399
400 $this->assertCount(3, $contacts);
401
402 $contact1 = array_shift($contacts);
403 $this->assertEquals($user2->id, $contact1->contact);
04cd1b3f
MN
404
405 $contact2 = array_shift($contacts);
406 $this->assertEquals($user3->id, $contact2->contact);
04cd1b3f
MN
407
408 $contact3 = array_shift($contacts);
409 $this->assertEquals($user4->id, $contact3->contact);
742c27b3
MN
410 }
411
412 /**
413 * Test for provider::export_user_data().
414 */
415 public function test_export_for_context_with_contact_requests() {
416 $this->resetAfterTest();
417
418 // Create users to test with.
419 $user1 = $this->getDataGenerator()->create_user();
420 $user2 = $this->getDataGenerator()->create_user();
421 $user3 = $this->getDataGenerator()->create_user();
422 $user4 = $this->getDataGenerator()->create_user();
423
424 \core_message\api::create_contact_request($user1->id, $user2->id);
425 \core_message\api::create_contact_request($user3->id, $user1->id);
426 \core_message\api::create_contact_request($user1->id, $user4->id);
427
52334296 428 $user1context = context_user::instance($user1->id);
742c27b3 429
52334296
SR
430 $this->export_context_data_for_user($user1->id, $user1context, 'core_message');
431
432 $writer = writer::with_context($user1context);
742c27b3
MN
433
434 $contactrequests = (array) $writer->get_data([get_string('contactrequests', 'core_message')]);
435
436 $this->assertCount(3, $contactrequests);
437
438 $contactrequest1 = array_shift($contactrequests);
439 $this->assertEquals($user2->id, $contactrequest1->contactrequest);
440 $this->assertEquals(get_string('yes'), $contactrequest1->maderequest);
441
442 $contactrequest2 = array_shift($contactrequests);
443 $this->assertEquals($user3->id, $contactrequest2->contactrequest);
444 $this->assertEquals(get_string('no'), $contactrequest2->maderequest);
445
446 $contactrequest3 = array_shift($contactrequests);
447 $this->assertEquals($user4->id, $contactrequest3->contactrequest);
448 $this->assertEquals(get_string('yes'), $contactrequest3->maderequest);
449 }
450
451 /**
452 * Test for provider::export_user_data().
453 */
454 public function test_export_for_context_with_blocked_users() {
455 $this->resetAfterTest();
456
457 // Create users to test with.
458 $user1 = $this->getDataGenerator()->create_user();
459 $user2 = $this->getDataGenerator()->create_user();
460 $user3 = $this->getDataGenerator()->create_user();
461 $user4 = $this->getDataGenerator()->create_user();
462
463 \core_message\api::block_user($user1->id, $user2->id);
464 \core_message\api::block_user($user1->id, $user3->id);
465 \core_message\api::block_user($user1->id, $user4->id);
466
52334296 467 $user1context = context_user::instance($user1->id);
742c27b3 468
52334296
SR
469 $this->export_context_data_for_user($user1->id, $user1context, 'core_message');
470
471 $writer = writer::with_context($user1context);
742c27b3
MN
472
473 $blockedusers = (array) $writer->get_data([get_string('blockedusers', 'core_message')]);
474
475 $this->assertCount(3, $blockedusers);
476
477 $blockeduser1 = array_shift($blockedusers);
478 $this->assertEquals($user2->id, $blockeduser1->blockeduser);
479
480 $blockeduser2 = array_shift($blockedusers);
481 $this->assertEquals($user3->id, $blockeduser2->blockeduser);
482
483 $blockeduser3 = array_shift($blockedusers);
484 $this->assertEquals($user4->id, $blockeduser3->blockeduser);
04cd1b3f
MN
485 }
486
487 /**
488 * Test for provider::export_user_data().
489 */
490 public function test_export_for_context_with_messages() {
491 global $DB;
492
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
500 $now = time();
501
502 // Send messages from user 1 to user 2.
503 $m1 = $this->create_message($user1->id, $user2->id, $now - (9 * DAYSECS), true);
504 $m2 = $this->create_message($user2->id, $user1->id, $now - (8 * DAYSECS));
505 $m3 = $this->create_message($user1->id, $user2->id, $now - (7 * DAYSECS));
506
507 // Send messages from user 3 to user 1.
508 $m4 = $this->create_message($user3->id, $user1->id, $now - (6 * DAYSECS), true);
509 $m5 = $this->create_message($user1->id, $user3->id, $now - (5 * DAYSECS));
510 $m6 = $this->create_message($user3->id, $user1->id, $now - (4 * DAYSECS));
511
512 // Send messages from user 3 to user 2 - these should not be included in the export.
513 $m7 = $this->create_message($user3->id, $user2->id, $now - (3 * DAYSECS), true);
514 $m8 = $this->create_message($user2->id, $user3->id, $now - (2 * DAYSECS));
515 $m9 = $this->create_message($user3->id, $user2->id, $now - (1 * DAYSECS));
516
517 // Mark message 2 and 5 as deleted.
518 \core_message\api::delete_message($user1->id, $m2);
519 \core_message\api::delete_message($user1->id, $m5);
520
52334296 521 $user1context = context_user::instance($user1->id);
04cd1b3f 522
52334296
SR
523 $this->export_context_data_for_user($user1->id, $user1context, 'core_message');
524
525 $writer = writer::with_context($user1context);
04cd1b3f
MN
526
527 $this->assertTrue($writer->has_any_data());
528
529 // Confirm the messages with user 2 are correct.
530 $messages = (array) $writer->get_data([get_string('messages', 'core_message'), fullname($user2)]);
531 $this->assertCount(3, $messages);
532
533 $dbm1 = $DB->get_record('messages', ['id' => $m1]);
534 $dbm2 = $DB->get_record('messages', ['id' => $m2]);
535 $dbm3 = $DB->get_record('messages', ['id' => $m3]);
536
823e08bf 537 usort($messages, ['static', 'sort_messages']);
04cd1b3f
MN
538 $m1 = array_shift($messages);
539 $m2 = array_shift($messages);
540 $m3 = array_shift($messages);
541
542 $this->assertEquals(get_string('yes'), $m1->sender);
543 $this->assertEquals(message_format_message_text($dbm1), $m1->message);
544 $this->assertEquals(transform::datetime($now - (9 * DAYSECS)), $m1->timecreated);
545 $this->assertNotEquals('-', $m1->timeread);
546 $this->assertArrayNotHasKey('timedeleted', (array) $m1);
547
548 $this->assertEquals(get_string('no'), $m2->sender);
549 $this->assertEquals(message_format_message_text($dbm2), $m2->message);
550 $this->assertEquals(transform::datetime($now - (8 * DAYSECS)), $m2->timecreated);
551 $this->assertEquals('-', $m2->timeread);
552 $this->assertArrayHasKey('timedeleted', (array) $m2);
553
554 $this->assertEquals(get_string('yes'), $m3->sender);
555 $this->assertEquals(message_format_message_text($dbm3), $m3->message);
556 $this->assertEquals(transform::datetime($now - (7 * DAYSECS)), $m3->timecreated);
557 $this->assertEquals('-', $m3->timeread);
558
559 // Confirm the messages with user 3 are correct.
560 $messages = (array) $writer->get_data([get_string('messages', 'core_message'), fullname($user3)]);
561 $this->assertCount(3, $messages);
562
563 $dbm4 = $DB->get_record('messages', ['id' => $m4]);
564 $dbm5 = $DB->get_record('messages', ['id' => $m5]);
565 $dbm6 = $DB->get_record('messages', ['id' => $m6]);
566
823e08bf 567 usort($messages, ['static', 'sort_messages']);
04cd1b3f
MN
568 $m4 = array_shift($messages);
569 $m5 = array_shift($messages);
570 $m6 = array_shift($messages);
571
572 $this->assertEquals(get_string('no'), $m4->sender);
573 $this->assertEquals(message_format_message_text($dbm4), $m4->message);
574 $this->assertEquals(transform::datetime($now - (6 * DAYSECS)), $m4->timecreated);
575 $this->assertNotEquals('-', $m4->timeread);
576 $this->assertArrayNotHasKey('timedeleted', (array) $m4);
577
578 $this->assertEquals(get_string('yes'), $m5->sender);
579 $this->assertEquals(message_format_message_text($dbm5), $m5->message);
580 $this->assertEquals(transform::datetime($now - (5 * DAYSECS)), $m5->timecreated);
581 $this->assertEquals('-', $m5->timeread);
582 $this->assertArrayHasKey('timedeleted', (array) $m5);
583
584 $this->assertEquals(get_string('no'), $m6->sender);
585 $this->assertEquals(message_format_message_text($dbm6), $m6->message);
586 $this->assertEquals(transform::datetime($now - (4 * DAYSECS)), $m6->timecreated);
587 $this->assertEquals('-', $m6->timeread);
588 }
589
590 /**
591 * Test for provider::export_user_data().
592 */
593 public function test_export_for_context_with_notifications() {
594 $this->resetAfterTest();
595
596 // Create users to test with.
597 $user1 = $this->getDataGenerator()->create_user();
598 $user2 = $this->getDataGenerator()->create_user();
599 $user3 = $this->getDataGenerator()->create_user();
600
601 $now = time();
602 $timeread = $now - DAYSECS;
603
604 // Send notifications from user 1 to user 2.
605 $this->create_notification($user1->id, $user2->id, $now + (9 * DAYSECS), $timeread);
606 $this->create_notification($user2->id, $user1->id, $now + (8 * DAYSECS));
607 $this->create_notification($user1->id, $user2->id, $now + (7 * DAYSECS));
608
609 // Send notifications from user 3 to user 1.
610 $this->create_notification($user3->id, $user1->id, $now + (6 * DAYSECS), $timeread);
611 $this->create_notification($user1->id, $user3->id, $now + (5 * DAYSECS));
612 $this->create_notification($user3->id, $user1->id, $now + (4 * DAYSECS));
613
614 // Send notifications from user 3 to user 2 - should not be part of the export.
615 $this->create_notification($user3->id, $user2->id, $now + (3 * DAYSECS), $timeread);
616 $this->create_notification($user2->id, $user3->id, $now + (2 * DAYSECS));
617 $this->create_notification($user3->id, $user2->id, $now + (1 * DAYSECS));
618
52334296 619 $user1context = context_user::instance($user1->id);
04cd1b3f 620
52334296
SR
621 $this->export_context_data_for_user($user1->id, $user1context, 'core_message');
622
623 $writer = writer::with_context($user1context);
04cd1b3f
MN
624
625 $this->assertTrue($writer->has_any_data());
626
627 // Confirm the notifications.
628 $notifications = (array) $writer->get_data([get_string('notifications', 'core_message')]);
629
630 $this->assertCount(6, $notifications);
631 }
632
633 /**
634 * Test for provider::delete_data_for_all_users_in_context().
635 */
636 public function test_delete_data_for_all_users_in_context() {
637 global $DB;
638
639 $this->resetAfterTest();
640
641 // Create users to test with.
642 $user1 = $this->getDataGenerator()->create_user();
643 $user2 = $this->getDataGenerator()->create_user();
742c27b3
MN
644 $user3 = $this->getDataGenerator()->create_user();
645 $user4 = $this->getDataGenerator()->create_user();
52334296 646 $user5 = $this->getDataGenerator()->create_user();
04cd1b3f
MN
647
648 $now = time();
649 $timeread = $now - DAYSECS;
650
52334296 651 $user1context = context_user::instance($user1->id);
04cd1b3f
MN
652
653 // Create contacts.
742c27b3 654 \core_message\api::add_contact($user1->id, $user2->id);
52334296 655 \core_message\api::add_contact($user2->id, $user3->id);
742c27b3
MN
656
657 // Create contact requests.
658 \core_message\api::create_contact_request($user1->id, $user3->id);
52334296 659 \core_message\api::create_contact_request($user2->id, $user4->id);
742c27b3
MN
660
661 // Block a user.
662 \core_message\api::block_user($user1->id, $user3->id);
52334296 663 \core_message\api::block_user($user3->id, $user4->id);
04cd1b3f
MN
664
665 // Create messages.
666 $m1 = $this->create_message($user1->id, $user2->id, $now + (9 * DAYSECS), true);
667 $m2 = $this->create_message($user2->id, $user1->id, $now + (8 * DAYSECS));
52334296 668 $m3 = $this->create_message($user2->id, $user3->id, $now + (7 * DAYSECS));
04cd1b3f
MN
669
670 // Create notifications.
671 $n1 = $this->create_notification($user1->id, $user2->id, $now + (9 * DAYSECS), $timeread);
672 $n2 = $this->create_notification($user2->id, $user1->id, $now + (8 * DAYSECS));
52334296 673 $n3 = $this->create_notification($user2->id, $user3->id, $now + (7 * DAYSECS));
04cd1b3f
MN
674
675 // Delete one of the messages.
676 \core_message\api::delete_message($user1->id, $m2);
677
52334296
SR
678 // There should be 2 contacts.
679 $this->assertEquals(2, $DB->count_records('message_contacts'));
742c27b3 680
52334296
SR
681 // There should be 2 contact requests.
682 $this->assertEquals(2, $DB->count_records('message_contact_requests'));
742c27b3 683
52334296
SR
684 // There should be 2 blocked users.
685 $this->assertEquals(2, $DB->count_records('message_users_blocked'));
04cd1b3f 686
52334296
SR
687 // There should be 3 messages.
688 $this->assertEquals(3, $DB->count_records('messages'));
04cd1b3f 689
52334296 690 // There should be 2 user actions - one for reading the message, one for deleting.
04cd1b3f
MN
691 $this->assertEquals(2, $DB->count_records('message_user_actions'));
692
52334296
SR
693 // There should be 4 conversation members.
694 $this->assertEquals(4, $DB->count_records('message_conversation_members'));
04cd1b3f 695
52334296
SR
696 // There should be 3 notifications + 2 for the contact request.
697 $this->assertEquals(5, $DB->count_records('notifications'));
04cd1b3f 698
52334296 699 provider::delete_data_for_all_users_in_context($user1context);
04cd1b3f 700
52334296
SR
701 // Confirm there is only 1 contact left.
702 $this->assertEquals(1, $DB->count_records('message_contacts'));
703 // And it is not related to user1.
704 $this->assertEquals(0,
705 $DB->count_records_select('message_contacts', 'userid = ? OR contactid = ?', [$user1->id, $user1->id]));
706
707 // Confirm there is only 1 contact request left.
708 $this->assertEquals(1, $DB->count_records('message_contact_requests'));
709 // And it is not related to user1.
710 $this->assertEquals(0,
711 $DB->count_records_select('message_contact_requests', 'userid = ? OR requesteduserid = ?',
712 [$user1->id, $user1->id]));
713
714 // Confirm there is only 1 blocked user left.
715 $this->assertEquals(1, $DB->count_records('message_users_blocked'));
716 // And it is not related to user1.
717 $this->assertEquals(0,
718 $DB->count_records_select('message_users_blocked', 'userid = ? OR blockeduserid = ?', [$user1->id, $user1->id]));
719
720 // Confirm there are only 2 messages left.
721 $this->assertEquals(2, $DB->count_records('messages'));
722 // And none of them are from user1.
723 $this->assertEquals(0, $DB->count_records('messages', ['useridfrom' => $user1->id]));
724
725 // Confirm there is only 1 user action left - the one that is for user2 reading the message.
726 $this->assertEquals(1, $DB->count_records('message_user_actions'));
727 // And it is not for user1.
728 $this->assertEquals(0, $DB->count_records('message_user_actions', ['userid' => $user1->id]));
729
730 // Confirm there are only 3 conversation members left.
731 $this->assertEquals(3, $DB->count_records('message_conversation_members'));
732 // And user1 is not in any conversation.
733 $this->assertEquals(0, $DB->count_records('message_conversation_members', ['userid' => $user1->id]));
734
735 // Confirm there is only 1 notification + 1 for the contact request.
736 $this->assertEquals(2, $DB->count_records('notifications'));
737 // And it is not related to user1.
738 $this->assertEquals(0,
739 $DB->count_records_select('notifications', 'useridfrom = ? OR useridto = ? ', [$user1->id, $user1->id]));
04cd1b3f
MN
740 }
741
742 /**
743 * Test for provider::delete_data_for_user().
744 */
745 public function test_delete_data_for_user() {
746 global $DB;
747
748 $this->resetAfterTest();
749
750 // Create users to test with.
751 $user1 = $this->getDataGenerator()->create_user();
752 $user2 = $this->getDataGenerator()->create_user();
753 $user3 = $this->getDataGenerator()->create_user();
742c27b3
MN
754 $user4 = $this->getDataGenerator()->create_user();
755 $user5 = $this->getDataGenerator()->create_user();
756 $user6 = $this->getDataGenerator()->create_user();
04cd1b3f
MN
757
758 $now = time();
759 $timeread = $now - DAYSECS;
760
761 // Create contacts.
742c27b3
MN
762 \core_message\api::add_contact($user1->id, $user2->id);
763 \core_message\api::add_contact($user2->id, $user3->id);
764
765 // Create contact requests.
766 \core_message\api::create_contact_request($user1->id, $user3->id);
767 \core_message\api::create_contact_request($user2->id, $user4->id);
768
769 // Block users.
770 \core_message\api::block_user($user1->id, $user5->id);
771 \core_message\api::block_user($user2->id, $user6->id);
04cd1b3f
MN
772
773 // Create messages.
774 $m1 = $this->create_message($user1->id, $user2->id, $now + (9 * DAYSECS), $timeread);
775 $m2 = $this->create_message($user2->id, $user1->id, $now + (8 * DAYSECS));
776
777 // Create notifications.
778 $n1 = $this->create_notification($user1->id, $user2->id, $now + (9 * DAYSECS), $timeread);
779 $n2 = $this->create_notification($user2->id, $user1->id, $now + (8 * DAYSECS));
780 $n2 = $this->create_notification($user2->id, $user3->id, $now + (8 * DAYSECS));
781
782 // Delete one of the messages.
783 \core_message\api::delete_message($user1->id, $m2);
784
742c27b3
MN
785 // There should be 2 contacts.
786 $this->assertEquals(2, $DB->count_records('message_contacts'));
787
788 // There should be 1 contact request.
789 $this->assertEquals(2, $DB->count_records('message_contact_requests'));
790
791 // There should be 1 blocked user.
792 $this->assertEquals(2, $DB->count_records('message_users_blocked'));
04cd1b3f
MN
793
794 // There should be two messages.
795 $this->assertEquals(2, $DB->count_records('messages'));
796
797 // There should be two user actions - one for reading the message, one for deleting.
798 $this->assertEquals(2, $DB->count_records('message_user_actions'));
799
800 // There should be two conversation members.
801 $this->assertEquals(2, $DB->count_records('message_conversation_members'));
802
be016b01
MN
803 // There should be three notifications + two for the contact requests.
804 $this->assertEquals(5, $DB->count_records('notifications'));
04cd1b3f 805
52334296 806 $user1context = context_user::instance($user1->id);
04cd1b3f 807 $contextlist = new \core_privacy\local\request\approved_contextlist($user1, 'core_message',
52334296 808 [$user1context->id]);
04cd1b3f
MN
809 provider::delete_data_for_user($contextlist);
810
811 // Confirm the user 2 data still exists.
812 $contacts = $DB->get_records('message_contacts');
742c27b3
MN
813 $contactrequests = $DB->get_records('message_contact_requests');
814 $blockedusers = $DB->get_records('message_users_blocked');
ba81c26c
SR
815 $messages = $DB->get_records('messages');
816 $muas = $DB->get_records('message_user_actions');
817 $mcms = $DB->get_records('message_conversation_members');
818 $notifications = $DB->get_records('notifications');
819
820 $this->assertCount(1, $contacts);
821 $contact = reset($contacts);
822 $this->assertEquals($user2->id, $contact->userid);
823 $this->assertEquals($user3->id, $contact->contactid);
824
825 $this->assertCount(1, $contactrequests);
826 $contactrequest = reset($contactrequests);
827 $this->assertEquals($user2->id, $contactrequest->userid);
828 $this->assertEquals($user4->id, $contactrequest->requesteduserid);
829
830 $this->assertCount(1, $blockedusers);
831 $blockeduser = reset($blockedusers);
832 $this->assertEquals($user2->id, $blockeduser->userid);
833 $this->assertEquals($user6->id, $blockeduser->blockeduserid);
834
835 $this->assertCount(1, $messages);
836 $message = reset($messages);
837 $this->assertEquals($m2, $message->id);
838
839 $this->assertCount(1, $muas);
840 $mua = reset($muas);
841 $this->assertEquals($user2->id, $mua->userid);
842 $this->assertEquals($m1, $mua->messageid);
843 $this->assertEquals(\core_message\api::MESSAGE_ACTION_READ, $mua->action);
844
845 $this->assertCount(1, $mcms);
846 $mcm = reset($mcms);
847 $this->assertEquals($user2->id, $mcm->userid);
848
849 $this->assertCount(2, $notifications);
850 ksort($notifications);
851
852 $notification = array_shift($notifications);
853 $this->assertEquals($user2->id, $notification->useridfrom);
854 $this->assertEquals($user4->id, $notification->useridto);
855
856 $notification = array_shift($notifications);
857 $this->assertEquals($user2->id, $notification->useridfrom);
858 $this->assertEquals($user3->id, $notification->useridto);
859 }
860
861 /**
862 * Test for provider::get_users_in_context() when there is no message or notification.
863 */
864 public function test_get_users_in_context_no_data() {
865 $this->resetAfterTest();
866
867 $user = $this->getDataGenerator()->create_user();
868 $usercontext = context_user::instance($user->id);
869
870 $userlist = new \core_privacy\local\request\userlist($usercontext, 'core_message');
871 \core_message\privacy\provider::get_users_in_context($userlist);
872
873 $this->assertEmpty($userlist->get_userids());
874 }
875
876 /**
877 * Test for provider::get_users_in_context() when there is a message between users.
878 */
879 public function test_get_users_in_context_with_message() {
880 $this->resetAfterTest();
881
882 $user1 = $this->getDataGenerator()->create_user();
883 $user2 = $this->getDataGenerator()->create_user();
884
885 $user1context = context_user::instance($user1->id);
886 $user2context = context_user::instance($user2->id);
887
b8bbef0a
MH
888 // Test nothing is found before message is sent.
889 $userlist = new \core_privacy\local\request\userlist($user1context, 'core_message');
890 \core_message\privacy\provider::get_users_in_context($userlist);
891 $this->assertCount(0, $userlist);
892 $userlist = new \core_privacy\local\request\userlist($user2context, 'core_message');
893 \core_message\privacy\provider::get_users_in_context($userlist);
894 $this->assertCount(0, $userlist);
895
ba81c26c
SR
896 $this->create_message($user1->id, $user2->id, time() - (9 * DAYSECS));
897
898 // Test for the sender.
899 $userlist = new \core_privacy\local\request\userlist($user1context, 'core_message');
900 \core_message\privacy\provider::get_users_in_context($userlist);
901 $this->assertCount(1, $userlist);
902 $userincontext = $userlist->current();
903 $this->assertEquals($user1->id, $userincontext->id);
904
905 // Test for the receiver.
906 $userlist = new \core_privacy\local\request\userlist($user2context, 'core_message');
907 \core_message\privacy\provider::get_users_in_context($userlist);
908 $this->assertCount(1, $userlist);
909 $userincontext = $userlist->current();
910 $this->assertEquals($user2->id, $userincontext->id);
911 }
912
913 /**
914 * Test for provider::get_users_in_context() when there is a notification between users.
915 */
916 public function test_get_users_in_context_with_notification() {
917 $this->resetAfterTest();
918
919 $user1 = $this->getDataGenerator()->create_user();
920 $user2 = $this->getDataGenerator()->create_user();
921
922 $user1context = context_user::instance($user1->id);
923 $user2context = context_user::instance($user2->id);
924
b8bbef0a
MH
925 // Test nothing is found before notification is created.
926 $userlist = new \core_privacy\local\request\userlist($user1context, 'core_message');
927 \core_message\privacy\provider::get_users_in_context($userlist);
928 $this->assertCount(0, $userlist);
929 $userlist = new \core_privacy\local\request\userlist($user2context, 'core_message');
930 \core_message\privacy\provider::get_users_in_context($userlist);
931 $this->assertCount(0, $userlist);
932
ba81c26c
SR
933 $this->create_notification($user1->id, $user2->id, time() - (9 * DAYSECS));
934
935 // Test for the sender.
936 $userlist = new \core_privacy\local\request\userlist($user1context, 'core_message');
937 \core_message\privacy\provider::get_users_in_context($userlist);
938 $this->assertCount(1, $userlist);
939 $userincontext = $userlist->current();
940 $this->assertEquals($user1->id, $userincontext->id);
941
942 // Test for the receiver.
943 $userlist = new \core_privacy\local\request\userlist($user2context, 'core_message');
944 \core_message\privacy\provider::get_users_in_context($userlist);
945 $this->assertCount(1, $userlist);
946 $userincontext = $userlist->current();
947 $this->assertEquals($user2->id, $userincontext->id);
948 }
949
b8bbef0a
MH
950 /**
951 * Test for provider::get_users_in_context() when a users has a contact.
952 */
953 public function test_get_users_in_context_with_contact() {
954 $this->resetAfterTest();
955
956 $user1 = $this->getDataGenerator()->create_user();
957 $user2 = $this->getDataGenerator()->create_user();
958
959 $user1context = context_user::instance($user1->id);
960 $user2context = context_user::instance($user2->id);
961
962 // Test nothing is found before contact is created.
963 $userlist = new \core_privacy\local\request\userlist($user1context, 'core_message');
964 \core_message\privacy\provider::get_users_in_context($userlist);
965 $this->assertCount(0, $userlist);
966 $userlist = new \core_privacy\local\request\userlist($user2context, 'core_message');
967 \core_message\privacy\provider::get_users_in_context($userlist);
968 $this->assertCount(0, $userlist);
969
970 \core_message\api::add_contact($user1->id, $user2->id);
971
972 // Test for the user adding the contact.
973 $userlist = new \core_privacy\local\request\userlist($user1context, 'core_message');
974 \core_message\privacy\provider::get_users_in_context($userlist);
975 $this->assertCount(1, $userlist);
976 $userincontext = $userlist->current();
977 $this->assertEquals($user1->id, $userincontext->id);
978
979 // Test for the user who is the contact.
980 $userlist = new \core_privacy\local\request\userlist($user2context, 'core_message');
981 \core_message\privacy\provider::get_users_in_context($userlist);
982 $this->assertCount(1, $userlist);
983 $userincontext = $userlist->current();
984 $this->assertEquals($user2->id, $userincontext->id);
985 }
986
987 /**
988 * Test for provider::get_users_in_context() when a user makes a contact request.
989 */
990 public function test_get_users_in_context_with_contact_request() {
991 $this->resetAfterTest();
992
993 $user1 = $this->getDataGenerator()->create_user();
994 $user2 = $this->getDataGenerator()->create_user();
995
996 $user1context = context_user::instance($user1->id);
997 $user2context = context_user::instance($user2->id);
998
999 // Test nothing is found before request is created.
1000 $userlist = new \core_privacy\local\request\userlist($user1context, 'core_message');
1001 \core_message\privacy\provider::get_users_in_context($userlist);
1002 $this->assertCount(0, $userlist);
1003 $userlist = new \core_privacy\local\request\userlist($user2context, 'core_message');
1004 \core_message\privacy\provider::get_users_in_context($userlist);
1005 $this->assertCount(0, $userlist);
1006
1007 \core_message\api::create_contact_request($user1->id, $user2->id);
1008
1009 // Test for the user requesting the contact.
1010 $userlist = new \core_privacy\local\request\userlist($user1context, 'core_message');
1011 \core_message\privacy\provider::get_users_in_context($userlist);
1012 $this->assertCount(1, $userlist);
1013 $userincontext = $userlist->current();
1014 $this->assertEquals($user1->id, $userincontext->id);
1015
1016 // Test for the user receiving the contact request.
1017 $userlist = new \core_privacy\local\request\userlist($user2context, 'core_message');
1018 \core_message\privacy\provider::get_users_in_context($userlist);
1019 $this->assertCount(1, $userlist);
1020 $userincontext = $userlist->current();
1021 $this->assertEquals($user2->id, $userincontext->id);
1022 }
1023
1024 /**
1025 * Test for provider::get_users_in_context() when a user is blocked.
1026 */
1027 public function test_get_users_in_context_with_blocked_contact() {
1028 $this->resetAfterTest();
1029
1030 $user1 = $this->getDataGenerator()->create_user();
1031 $user2 = $this->getDataGenerator()->create_user();
1032
1033 $user1context = context_user::instance($user1->id);
1034 $user2context = context_user::instance($user2->id);
1035
1036 // Test nothing is found before user is blocked.
1037 $userlist = new \core_privacy\local\request\userlist($user1context, 'core_message');
1038 \core_message\privacy\provider::get_users_in_context($userlist);
1039 $this->assertCount(0, $userlist);
1040 $userlist = new \core_privacy\local\request\userlist($user2context, 'core_message');
1041 \core_message\privacy\provider::get_users_in_context($userlist);
1042 $this->assertCount(0, $userlist);
1043
1044 \core_message\api::block_user($user1->id, $user2->id);
1045
1046 // Test for the blocking user.
1047 $userlist = new \core_privacy\local\request\userlist($user1context, 'core_message');
1048 \core_message\privacy\provider::get_users_in_context($userlist);
1049 $this->assertCount(1, $userlist);
1050 $userincontext = $userlist->current();
1051 $this->assertEquals($user1->id, $userincontext->id);
1052
1053 // Test for the user who is blocked.
1054 $userlist = new \core_privacy\local\request\userlist($user2context, 'core_message');
1055 \core_message\privacy\provider::get_users_in_context($userlist);
1056 $this->assertCount(1, $userlist);
1057 $userincontext = $userlist->current();
1058 $this->assertEquals($user2->id, $userincontext->id);
1059 }
1060
ba81c26c
SR
1061 /**
1062 * Test for provider::delete_data_for_users().
1063 */
1064 public function test_delete_data_for_users() {
1065 global $DB;
1066
1067 $this->resetAfterTest();
1068
1069 // Create users to test with.
1070 $user1 = $this->getDataGenerator()->create_user();
1071 $user2 = $this->getDataGenerator()->create_user();
1072 $user3 = $this->getDataGenerator()->create_user();
1073 $user4 = $this->getDataGenerator()->create_user();
1074 $user5 = $this->getDataGenerator()->create_user();
1075 $user6 = $this->getDataGenerator()->create_user();
1076
1077 $now = time();
1078 $timeread = $now - DAYSECS;
1079
1080 // Create contacts.
1081 \core_message\api::add_contact($user1->id, $user2->id);
1082 \core_message\api::add_contact($user2->id, $user3->id);
1083
1084 // Create contact requests.
1085 \core_message\api::create_contact_request($user1->id, $user3->id);
1086 \core_message\api::create_contact_request($user2->id, $user4->id);
1087
1088 // Block users.
1089 \core_message\api::block_user($user1->id, $user5->id);
1090 \core_message\api::block_user($user2->id, $user6->id);
1091
1092 // Create messages.
1093 $m1 = $this->create_message($user1->id, $user2->id, $now + (9 * DAYSECS), $timeread);
1094 $m2 = $this->create_message($user2->id, $user1->id, $now + (8 * DAYSECS));
1095
1096 // Create notifications.
1097 $n1 = $this->create_notification($user1->id, $user2->id, $now + (9 * DAYSECS), $timeread);
1098 $n2 = $this->create_notification($user2->id, $user1->id, $now + (8 * DAYSECS));
1099 $n2 = $this->create_notification($user2->id, $user3->id, $now + (8 * DAYSECS));
1100
1101 // Delete one of the messages.
1102 \core_message\api::delete_message($user1->id, $m2);
1103
1104 // There should be 2 contacts.
1105 $this->assertEquals(2, $DB->count_records('message_contacts'));
1106
1107 // There should be 1 contact request.
1108 $this->assertEquals(2, $DB->count_records('message_contact_requests'));
1109
1110 // There should be 1 blocked user.
1111 $this->assertEquals(2, $DB->count_records('message_users_blocked'));
1112
1113 // There should be two messages.
1114 $this->assertEquals(2, $DB->count_records('messages'));
1115
1116 // There should be two user actions - one for reading the message, one for deleting.
1117 $this->assertEquals(2, $DB->count_records('message_user_actions'));
1118
1119 // There should be two conversation members.
1120 $this->assertEquals(2, $DB->count_records('message_conversation_members'));
1121
1122 // There should be three notifications + two for the contact requests.
1123 $this->assertEquals(5, $DB->count_records('notifications'));
1124
1125 $user1context = context_user::instance($user1->id);
1126 $approveduserlist = new \core_privacy\local\request\approved_userlist($user1context, 'core_message',
1127 [$user1->id, $user2->id]);
1128 provider::delete_data_for_users($approveduserlist);
1129
1130 // Only user1's data should be deleted. User2 should be skipped as user2 is an invalid user for user1context.
1131
1132 // Confirm the user 2 data still exists.
1133 $contacts = $DB->get_records('message_contacts');
1134 $contactrequests = $DB->get_records('message_contact_requests');
1135 $blockedusers = $DB->get_records('message_users_blocked');
04cd1b3f
MN
1136 $messages = $DB->get_records('messages');
1137 $muas = $DB->get_records('message_user_actions');
1138 $mcms = $DB->get_records('message_conversation_members');
1139 $notifications = $DB->get_records('notifications');
1140
1141 $this->assertCount(1, $contacts);
1142 $contact = reset($contacts);
742c27b3
MN
1143 $this->assertEquals($user2->id, $contact->userid);
1144 $this->assertEquals($user3->id, $contact->contactid);
1145
1146 $this->assertCount(1, $contactrequests);
1147 $contactrequest = reset($contactrequests);
1148 $this->assertEquals($user2->id, $contactrequest->userid);
1149 $this->assertEquals($user4->id, $contactrequest->requesteduserid);
1150
1151 $this->assertCount(1, $blockedusers);
1152 $blockeduser = reset($blockedusers);
1153 $this->assertEquals($user2->id, $blockeduser->userid);
1154 $this->assertEquals($user6->id, $blockeduser->blockeduserid);
04cd1b3f
MN
1155
1156 $this->assertCount(1, $messages);
1157 $message = reset($messages);
1158 $this->assertEquals($m2, $message->id);
1159
1160 $this->assertCount(1, $muas);
1161 $mua = reset($muas);
1162 $this->assertEquals($user2->id, $mua->userid);
1163 $this->assertEquals($m1, $mua->messageid);
1164 $this->assertEquals(\core_message\api::MESSAGE_ACTION_READ, $mua->action);
1165
1166 $this->assertCount(1, $mcms);
1167 $mcm = reset($mcms);
1168 $this->assertEquals($user2->id, $mcm->userid);
1169
be016b01
MN
1170 $this->assertCount(2, $notifications);
1171 ksort($notifications);
1172
1173 $notification = array_shift($notifications);
1174 $this->assertEquals($user2->id, $notification->useridfrom);
1175 $this->assertEquals($user4->id, $notification->useridto);
1176
1177 $notification = array_shift($notifications);
04cd1b3f
MN
1178 $this->assertEquals($user2->id, $notification->useridfrom);
1179 $this->assertEquals($user3->id, $notification->useridto);
1180 }
1181
1182 /**
1183 * Creates a message to be used for testing.
1184 *
1185 * @param int $useridfrom The user id from
1186 * @param int $useridto The user id to
1187 * @param int $timecreated
1188 * @param bool $read Do we want to mark the message as read?
1189 * @return int The id of the message
1190 * @throws dml_exception
1191 */
1192 private function create_message(int $useridfrom, int $useridto, int $timecreated = null, bool $read = false) {
1193 global $DB;
1194
1195 static $i = 1;
1196
1197 if (is_null($timecreated)) {
1198 $timecreated = time();
1199 }
1200
1201 if (!$conversationid = \core_message\api::get_conversation_between_users([$useridfrom, $useridto])) {
f2ac0a3e
MN
1202 $conversation = \core_message\api::create_conversation(
1203 \core_message\api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL,
1204 [
1205 $useridfrom,
1206 $useridto
1207 ]
1208 );
1209 $conversationid = $conversation->id;
04cd1b3f
MN
1210 }
1211
1212 // Ok, send the message.
1213 $record = new stdClass();
1214 $record->useridfrom = $useridfrom;
1215 $record->conversationid = $conversationid;
1216 $record->subject = 'No subject';
1217 $record->fullmessage = 'A rad message ' . $i;
1218 $record->smallmessage = 'A rad message ' . $i;
1219 $record->timecreated = $timecreated;
1220
1221 $i++;
1222
1223 $record->id = $DB->insert_record('messages', $record);
1224
1225 if ($read) {
1226 \core_message\api::mark_message_as_read($useridto, $record);
1227 }
1228
1229 return $record->id;
1230 }
1231
1232 /**
1233 * Creates a notification to be used for testing.
1234 *
1235 * @param int $useridfrom The user id from
1236 * @param int $useridto The user id to
1237 * @param int|null $timecreated The time the notification was created
1238 * @param int|null $timeread The time the notification was read, null if it hasn't been.
1239 * @return int The id of the notification
1240 * @throws dml_exception
1241 */
1242 private function create_notification(int $useridfrom, int $useridto, int $timecreated = null, int $timeread = null) {
1243 global $DB;
1244
1245 static $i = 1;
1246
1247 if (is_null($timecreated)) {
1248 $timecreated = time();
1249 }
1250
1251 $record = new stdClass();
1252 $record->useridfrom = $useridfrom;
1253 $record->useridto = $useridto;
1254 $record->subject = 'No subject';
1255 $record->fullmessage = 'Some rad notification ' . $i;
1256 $record->smallmessage = 'Yo homie, you got some stuff to do, yolo. ' . $i;
1257 $record->timeread = $timeread;
1258 $record->timecreated = $timecreated;
1259
1260 $i++;
1261
1262 return $DB->insert_record('notifications', $record);
1263 }
823e08bf
AN
1264
1265 /**
1266 * Comparison function for sorting messages.
1267 *
1268 * @param \stdClass $a
1269 * @param \stdClass $b
1270 * @return bool
1271 */
1272 protected static function sort_messages($a, $b) {
1273 return $a->message > $b->message;
1274 }
1275
1276 /**
1277 * Comparison function for sorting contacts.
1278 *
1279 * @param \stdClass $a
1280 * @param \stdClass $b
1281 * @return bool
1282 */
1283 protected static function sort_contacts($a, $b) {
1284 return $a->contact > $b->contact;
1285 }
04cd1b3f 1286}