on-demand release 3.6dev+
[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 /**
195 * Test for provider::get_contexts_for_userid().
196 */
197 public function test_get_contexts_for_userid() {
198 $this->resetAfterTest();
199
200 $user = $this->getDataGenerator()->create_user();
201 $contextlist = provider::get_contexts_for_userid($user->id);
202 $this->assertCount(1, $contextlist);
203 $contextforuser = $contextlist->current();
204 $this->assertEquals(SYSCONTEXTID, $contextforuser->id);
205 }
206
207 /**
208 * Test for provider::export_user_data().
209 */
210 public function test_export_for_context_with_contacts() {
211 $this->resetAfterTest();
212
213 // Create users to test with.
214 $user1 = $this->getDataGenerator()->create_user();
215 $user2 = $this->getDataGenerator()->create_user();
216 $user3 = $this->getDataGenerator()->create_user();
217 $user4 = $this->getDataGenerator()->create_user();
218
742c27b3
MN
219 \core_message\api::add_contact($user1->id, $user2->id);
220 \core_message\api::add_contact($user1->id, $user3->id);
221 \core_message\api::add_contact($user1->id, $user4->id);
04cd1b3f
MN
222
223 $this->export_context_data_for_user($user1->id, \context_system::instance(), 'core_message');
224
225 $writer = writer::with_context(\context_system::instance());
226
227 $contacts = (array) $writer->get_data([get_string('contacts', 'core_message')]);
823e08bf 228 usort($contacts, ['static', 'sort_contacts']);
04cd1b3f
MN
229
230 $this->assertCount(3, $contacts);
231
232 $contact1 = array_shift($contacts);
233 $this->assertEquals($user2->id, $contact1->contact);
04cd1b3f
MN
234
235 $contact2 = array_shift($contacts);
236 $this->assertEquals($user3->id, $contact2->contact);
04cd1b3f
MN
237
238 $contact3 = array_shift($contacts);
239 $this->assertEquals($user4->id, $contact3->contact);
742c27b3
MN
240 }
241
242 /**
243 * Test for provider::export_user_data().
244 */
245 public function test_export_for_context_with_contact_requests() {
246 $this->resetAfterTest();
247
248 // Create users to test with.
249 $user1 = $this->getDataGenerator()->create_user();
250 $user2 = $this->getDataGenerator()->create_user();
251 $user3 = $this->getDataGenerator()->create_user();
252 $user4 = $this->getDataGenerator()->create_user();
253
254 \core_message\api::create_contact_request($user1->id, $user2->id);
255 \core_message\api::create_contact_request($user3->id, $user1->id);
256 \core_message\api::create_contact_request($user1->id, $user4->id);
257
258 $this->export_context_data_for_user($user1->id, \context_system::instance(), 'core_message');
259
260 $writer = writer::with_context(\context_system::instance());
261
262 $contactrequests = (array) $writer->get_data([get_string('contactrequests', 'core_message')]);
263
264 $this->assertCount(3, $contactrequests);
265
266 $contactrequest1 = array_shift($contactrequests);
267 $this->assertEquals($user2->id, $contactrequest1->contactrequest);
268 $this->assertEquals(get_string('yes'), $contactrequest1->maderequest);
269
270 $contactrequest2 = array_shift($contactrequests);
271 $this->assertEquals($user3->id, $contactrequest2->contactrequest);
272 $this->assertEquals(get_string('no'), $contactrequest2->maderequest);
273
274 $contactrequest3 = array_shift($contactrequests);
275 $this->assertEquals($user4->id, $contactrequest3->contactrequest);
276 $this->assertEquals(get_string('yes'), $contactrequest3->maderequest);
277 }
278
279 /**
280 * Test for provider::export_user_data().
281 */
282 public function test_export_for_context_with_blocked_users() {
283 $this->resetAfterTest();
284
285 // Create users to test with.
286 $user1 = $this->getDataGenerator()->create_user();
287 $user2 = $this->getDataGenerator()->create_user();
288 $user3 = $this->getDataGenerator()->create_user();
289 $user4 = $this->getDataGenerator()->create_user();
290
291 \core_message\api::block_user($user1->id, $user2->id);
292 \core_message\api::block_user($user1->id, $user3->id);
293 \core_message\api::block_user($user1->id, $user4->id);
294
295 $this->export_context_data_for_user($user1->id, \context_system::instance(), 'core_message');
296
297 $writer = writer::with_context(\context_system::instance());
298
299 $blockedusers = (array) $writer->get_data([get_string('blockedusers', 'core_message')]);
300
301 $this->assertCount(3, $blockedusers);
302
303 $blockeduser1 = array_shift($blockedusers);
304 $this->assertEquals($user2->id, $blockeduser1->blockeduser);
305
306 $blockeduser2 = array_shift($blockedusers);
307 $this->assertEquals($user3->id, $blockeduser2->blockeduser);
308
309 $blockeduser3 = array_shift($blockedusers);
310 $this->assertEquals($user4->id, $blockeduser3->blockeduser);
04cd1b3f
MN
311 }
312
313 /**
314 * Test for provider::export_user_data().
315 */
316 public function test_export_for_context_with_messages() {
317 global $DB;
318
319 $this->resetAfterTest();
320
321 // Create users to test with.
322 $user1 = $this->getDataGenerator()->create_user();
323 $user2 = $this->getDataGenerator()->create_user();
324 $user3 = $this->getDataGenerator()->create_user();
325
326 $now = time();
327
328 // Send messages from user 1 to user 2.
329 $m1 = $this->create_message($user1->id, $user2->id, $now - (9 * DAYSECS), true);
330 $m2 = $this->create_message($user2->id, $user1->id, $now - (8 * DAYSECS));
331 $m3 = $this->create_message($user1->id, $user2->id, $now - (7 * DAYSECS));
332
333 // Send messages from user 3 to user 1.
334 $m4 = $this->create_message($user3->id, $user1->id, $now - (6 * DAYSECS), true);
335 $m5 = $this->create_message($user1->id, $user3->id, $now - (5 * DAYSECS));
336 $m6 = $this->create_message($user3->id, $user1->id, $now - (4 * DAYSECS));
337
338 // Send messages from user 3 to user 2 - these should not be included in the export.
339 $m7 = $this->create_message($user3->id, $user2->id, $now - (3 * DAYSECS), true);
340 $m8 = $this->create_message($user2->id, $user3->id, $now - (2 * DAYSECS));
341 $m9 = $this->create_message($user3->id, $user2->id, $now - (1 * DAYSECS));
342
343 // Mark message 2 and 5 as deleted.
344 \core_message\api::delete_message($user1->id, $m2);
345 \core_message\api::delete_message($user1->id, $m5);
346
347 $this->export_context_data_for_user($user1->id, \context_system::instance(), 'core_message');
348
349 $writer = writer::with_context(\context_system::instance());
350
351 $this->assertTrue($writer->has_any_data());
352
353 // Confirm the messages with user 2 are correct.
354 $messages = (array) $writer->get_data([get_string('messages', 'core_message'), fullname($user2)]);
355 $this->assertCount(3, $messages);
356
357 $dbm1 = $DB->get_record('messages', ['id' => $m1]);
358 $dbm2 = $DB->get_record('messages', ['id' => $m2]);
359 $dbm3 = $DB->get_record('messages', ['id' => $m3]);
360
823e08bf 361 usort($messages, ['static', 'sort_messages']);
04cd1b3f
MN
362 $m1 = array_shift($messages);
363 $m2 = array_shift($messages);
364 $m3 = array_shift($messages);
365
366 $this->assertEquals(get_string('yes'), $m1->sender);
367 $this->assertEquals(message_format_message_text($dbm1), $m1->message);
368 $this->assertEquals(transform::datetime($now - (9 * DAYSECS)), $m1->timecreated);
369 $this->assertNotEquals('-', $m1->timeread);
370 $this->assertArrayNotHasKey('timedeleted', (array) $m1);
371
372 $this->assertEquals(get_string('no'), $m2->sender);
373 $this->assertEquals(message_format_message_text($dbm2), $m2->message);
374 $this->assertEquals(transform::datetime($now - (8 * DAYSECS)), $m2->timecreated);
375 $this->assertEquals('-', $m2->timeread);
376 $this->assertArrayHasKey('timedeleted', (array) $m2);
377
378 $this->assertEquals(get_string('yes'), $m3->sender);
379 $this->assertEquals(message_format_message_text($dbm3), $m3->message);
380 $this->assertEquals(transform::datetime($now - (7 * DAYSECS)), $m3->timecreated);
381 $this->assertEquals('-', $m3->timeread);
382
383 // Confirm the messages with user 3 are correct.
384 $messages = (array) $writer->get_data([get_string('messages', 'core_message'), fullname($user3)]);
385 $this->assertCount(3, $messages);
386
387 $dbm4 = $DB->get_record('messages', ['id' => $m4]);
388 $dbm5 = $DB->get_record('messages', ['id' => $m5]);
389 $dbm6 = $DB->get_record('messages', ['id' => $m6]);
390
823e08bf 391 usort($messages, ['static', 'sort_messages']);
04cd1b3f
MN
392 $m4 = array_shift($messages);
393 $m5 = array_shift($messages);
394 $m6 = array_shift($messages);
395
396 $this->assertEquals(get_string('no'), $m4->sender);
397 $this->assertEquals(message_format_message_text($dbm4), $m4->message);
398 $this->assertEquals(transform::datetime($now - (6 * DAYSECS)), $m4->timecreated);
399 $this->assertNotEquals('-', $m4->timeread);
400 $this->assertArrayNotHasKey('timedeleted', (array) $m4);
401
402 $this->assertEquals(get_string('yes'), $m5->sender);
403 $this->assertEquals(message_format_message_text($dbm5), $m5->message);
404 $this->assertEquals(transform::datetime($now - (5 * DAYSECS)), $m5->timecreated);
405 $this->assertEquals('-', $m5->timeread);
406 $this->assertArrayHasKey('timedeleted', (array) $m5);
407
408 $this->assertEquals(get_string('no'), $m6->sender);
409 $this->assertEquals(message_format_message_text($dbm6), $m6->message);
410 $this->assertEquals(transform::datetime($now - (4 * DAYSECS)), $m6->timecreated);
411 $this->assertEquals('-', $m6->timeread);
412 }
413
414 /**
415 * Test for provider::export_user_data().
416 */
417 public function test_export_for_context_with_notifications() {
418 $this->resetAfterTest();
419
420 // Create users to test with.
421 $user1 = $this->getDataGenerator()->create_user();
422 $user2 = $this->getDataGenerator()->create_user();
423 $user3 = $this->getDataGenerator()->create_user();
424
425 $now = time();
426 $timeread = $now - DAYSECS;
427
428 // Send notifications from user 1 to user 2.
429 $this->create_notification($user1->id, $user2->id, $now + (9 * DAYSECS), $timeread);
430 $this->create_notification($user2->id, $user1->id, $now + (8 * DAYSECS));
431 $this->create_notification($user1->id, $user2->id, $now + (7 * DAYSECS));
432
433 // Send notifications from user 3 to user 1.
434 $this->create_notification($user3->id, $user1->id, $now + (6 * DAYSECS), $timeread);
435 $this->create_notification($user1->id, $user3->id, $now + (5 * DAYSECS));
436 $this->create_notification($user3->id, $user1->id, $now + (4 * DAYSECS));
437
438 // Send notifications from user 3 to user 2 - should not be part of the export.
439 $this->create_notification($user3->id, $user2->id, $now + (3 * DAYSECS), $timeread);
440 $this->create_notification($user2->id, $user3->id, $now + (2 * DAYSECS));
441 $this->create_notification($user3->id, $user2->id, $now + (1 * DAYSECS));
442
443 $this->export_context_data_for_user($user1->id, \context_system::instance(), 'core_message');
444
445 $writer = writer::with_context(\context_system::instance());
446
447 $this->assertTrue($writer->has_any_data());
448
449 // Confirm the notifications.
450 $notifications = (array) $writer->get_data([get_string('notifications', 'core_message')]);
451
452 $this->assertCount(6, $notifications);
453 }
454
455 /**
456 * Test for provider::delete_data_for_all_users_in_context().
457 */
458 public function test_delete_data_for_all_users_in_context() {
459 global $DB;
460
461 $this->resetAfterTest();
462
463 // Create users to test with.
464 $user1 = $this->getDataGenerator()->create_user();
465 $user2 = $this->getDataGenerator()->create_user();
742c27b3
MN
466 $user3 = $this->getDataGenerator()->create_user();
467 $user4 = $this->getDataGenerator()->create_user();
04cd1b3f
MN
468
469 $now = time();
470 $timeread = $now - DAYSECS;
471
472 $systemcontext = \context_system::instance();
473
474 // Create contacts.
742c27b3
MN
475 \core_message\api::add_contact($user1->id, $user2->id);
476
477 // Create contact requests.
478 \core_message\api::create_contact_request($user1->id, $user3->id);
479
480 // Block a user.
481 \core_message\api::block_user($user1->id, $user3->id);
04cd1b3f
MN
482
483 // Create messages.
484 $m1 = $this->create_message($user1->id, $user2->id, $now + (9 * DAYSECS), true);
485 $m2 = $this->create_message($user2->id, $user1->id, $now + (8 * DAYSECS));
486
487 // Create notifications.
488 $n1 = $this->create_notification($user1->id, $user2->id, $now + (9 * DAYSECS), $timeread);
489 $n2 = $this->create_notification($user2->id, $user1->id, $now + (8 * DAYSECS));
490
491 // Delete one of the messages.
492 \core_message\api::delete_message($user1->id, $m2);
493
742c27b3
MN
494 // There should be 1 contact.
495 $this->assertEquals(1, $DB->count_records('message_contacts'));
496
497 // There should be 1 contact request.
498 $this->assertEquals(1, $DB->count_records('message_contact_requests'));
499
500 // There should be 1 blocked user.
501 $this->assertEquals(1, $DB->count_records('message_users_blocked'));
04cd1b3f
MN
502
503 // There should be two messages.
504 $this->assertEquals(2, $DB->count_records('messages'));
505
506 // There should be two user actions - one for reading the message, one for deleting.
507 $this->assertEquals(2, $DB->count_records('message_user_actions'));
508
509 // There should be two conversation members.
510 $this->assertEquals(2, $DB->count_records('message_conversation_members'));
511
be016b01
MN
512 // There should be two notifications + one for the contact request.
513 $this->assertEquals(3, $DB->count_records('notifications'));
04cd1b3f
MN
514
515 provider::delete_data_for_all_users_in_context($systemcontext);
516
517 // Confirm all has been deleted.
518 $this->assertEquals(0, $DB->count_records('message_contacts'));
742c27b3
MN
519 $this->assertEquals(0, $DB->count_records('message_contact_requests'));
520 $this->assertEquals(0, $DB->count_records('message_users_blocked'));
04cd1b3f
MN
521 $this->assertEquals(0, $DB->count_records('messages'));
522 $this->assertEquals(0, $DB->count_records('message_user_actions'));
523 $this->assertEquals(0, $DB->count_records('message_conversation_members'));
524 $this->assertEquals(0, $DB->count_records('notifications'));
525 }
526
527 /**
528 * Test for provider::delete_data_for_user().
529 */
530 public function test_delete_data_for_user() {
531 global $DB;
532
533 $this->resetAfterTest();
534
535 // Create users to test with.
536 $user1 = $this->getDataGenerator()->create_user();
537 $user2 = $this->getDataGenerator()->create_user();
538 $user3 = $this->getDataGenerator()->create_user();
742c27b3
MN
539 $user4 = $this->getDataGenerator()->create_user();
540 $user5 = $this->getDataGenerator()->create_user();
541 $user6 = $this->getDataGenerator()->create_user();
04cd1b3f
MN
542
543 $now = time();
544 $timeread = $now - DAYSECS;
545
546 // Create contacts.
742c27b3
MN
547 \core_message\api::add_contact($user1->id, $user2->id);
548 \core_message\api::add_contact($user2->id, $user3->id);
549
550 // Create contact requests.
551 \core_message\api::create_contact_request($user1->id, $user3->id);
552 \core_message\api::create_contact_request($user2->id, $user4->id);
553
554 // Block users.
555 \core_message\api::block_user($user1->id, $user5->id);
556 \core_message\api::block_user($user2->id, $user6->id);
04cd1b3f
MN
557
558 // Create messages.
559 $m1 = $this->create_message($user1->id, $user2->id, $now + (9 * DAYSECS), $timeread);
560 $m2 = $this->create_message($user2->id, $user1->id, $now + (8 * DAYSECS));
561
562 // Create notifications.
563 $n1 = $this->create_notification($user1->id, $user2->id, $now + (9 * DAYSECS), $timeread);
564 $n2 = $this->create_notification($user2->id, $user1->id, $now + (8 * DAYSECS));
565 $n2 = $this->create_notification($user2->id, $user3->id, $now + (8 * DAYSECS));
566
567 // Delete one of the messages.
568 \core_message\api::delete_message($user1->id, $m2);
569
742c27b3
MN
570 // There should be 2 contacts.
571 $this->assertEquals(2, $DB->count_records('message_contacts'));
572
573 // There should be 1 contact request.
574 $this->assertEquals(2, $DB->count_records('message_contact_requests'));
575
576 // There should be 1 blocked user.
577 $this->assertEquals(2, $DB->count_records('message_users_blocked'));
04cd1b3f
MN
578
579 // There should be two messages.
580 $this->assertEquals(2, $DB->count_records('messages'));
581
582 // There should be two user actions - one for reading the message, one for deleting.
583 $this->assertEquals(2, $DB->count_records('message_user_actions'));
584
585 // There should be two conversation members.
586 $this->assertEquals(2, $DB->count_records('message_conversation_members'));
587
be016b01
MN
588 // There should be three notifications + two for the contact requests.
589 $this->assertEquals(5, $DB->count_records('notifications'));
04cd1b3f
MN
590
591 $systemcontext = \context_system::instance();
592 $contextlist = new \core_privacy\local\request\approved_contextlist($user1, 'core_message',
593 [$systemcontext->id]);
594 provider::delete_data_for_user($contextlist);
595
596 // Confirm the user 2 data still exists.
597 $contacts = $DB->get_records('message_contacts');
742c27b3
MN
598 $contactrequests = $DB->get_records('message_contact_requests');
599 $blockedusers = $DB->get_records('message_users_blocked');
04cd1b3f
MN
600 $messages = $DB->get_records('messages');
601 $muas = $DB->get_records('message_user_actions');
602 $mcms = $DB->get_records('message_conversation_members');
603 $notifications = $DB->get_records('notifications');
604
605 $this->assertCount(1, $contacts);
606 $contact = reset($contacts);
742c27b3
MN
607 $this->assertEquals($user2->id, $contact->userid);
608 $this->assertEquals($user3->id, $contact->contactid);
609
610 $this->assertCount(1, $contactrequests);
611 $contactrequest = reset($contactrequests);
612 $this->assertEquals($user2->id, $contactrequest->userid);
613 $this->assertEquals($user4->id, $contactrequest->requesteduserid);
614
615 $this->assertCount(1, $blockedusers);
616 $blockeduser = reset($blockedusers);
617 $this->assertEquals($user2->id, $blockeduser->userid);
618 $this->assertEquals($user6->id, $blockeduser->blockeduserid);
04cd1b3f
MN
619
620 $this->assertCount(1, $messages);
621 $message = reset($messages);
622 $this->assertEquals($m2, $message->id);
623
624 $this->assertCount(1, $muas);
625 $mua = reset($muas);
626 $this->assertEquals($user2->id, $mua->userid);
627 $this->assertEquals($m1, $mua->messageid);
628 $this->assertEquals(\core_message\api::MESSAGE_ACTION_READ, $mua->action);
629
630 $this->assertCount(1, $mcms);
631 $mcm = reset($mcms);
632 $this->assertEquals($user2->id, $mcm->userid);
633
be016b01
MN
634 $this->assertCount(2, $notifications);
635 ksort($notifications);
636
637 $notification = array_shift($notifications);
638 $this->assertEquals($user2->id, $notification->useridfrom);
639 $this->assertEquals($user4->id, $notification->useridto);
640
641 $notification = array_shift($notifications);
04cd1b3f
MN
642 $this->assertEquals($user2->id, $notification->useridfrom);
643 $this->assertEquals($user3->id, $notification->useridto);
644 }
645
646 /**
647 * Creates a message to be used for testing.
648 *
649 * @param int $useridfrom The user id from
650 * @param int $useridto The user id to
651 * @param int $timecreated
652 * @param bool $read Do we want to mark the message as read?
653 * @return int The id of the message
654 * @throws dml_exception
655 */
656 private function create_message(int $useridfrom, int $useridto, int $timecreated = null, bool $read = false) {
657 global $DB;
658
659 static $i = 1;
660
661 if (is_null($timecreated)) {
662 $timecreated = time();
663 }
664
665 if (!$conversationid = \core_message\api::get_conversation_between_users([$useridfrom, $useridto])) {
f2ac0a3e
MN
666 $conversation = \core_message\api::create_conversation(
667 \core_message\api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL,
668 [
669 $useridfrom,
670 $useridto
671 ]
672 );
673 $conversationid = $conversation->id;
04cd1b3f
MN
674 }
675
676 // Ok, send the message.
677 $record = new stdClass();
678 $record->useridfrom = $useridfrom;
679 $record->conversationid = $conversationid;
680 $record->subject = 'No subject';
681 $record->fullmessage = 'A rad message ' . $i;
682 $record->smallmessage = 'A rad message ' . $i;
683 $record->timecreated = $timecreated;
684
685 $i++;
686
687 $record->id = $DB->insert_record('messages', $record);
688
689 if ($read) {
690 \core_message\api::mark_message_as_read($useridto, $record);
691 }
692
693 return $record->id;
694 }
695
696 /**
697 * Creates a notification to be used for testing.
698 *
699 * @param int $useridfrom The user id from
700 * @param int $useridto The user id to
701 * @param int|null $timecreated The time the notification was created
702 * @param int|null $timeread The time the notification was read, null if it hasn't been.
703 * @return int The id of the notification
704 * @throws dml_exception
705 */
706 private function create_notification(int $useridfrom, int $useridto, int $timecreated = null, int $timeread = null) {
707 global $DB;
708
709 static $i = 1;
710
711 if (is_null($timecreated)) {
712 $timecreated = time();
713 }
714
715 $record = new stdClass();
716 $record->useridfrom = $useridfrom;
717 $record->useridto = $useridto;
718 $record->subject = 'No subject';
719 $record->fullmessage = 'Some rad notification ' . $i;
720 $record->smallmessage = 'Yo homie, you got some stuff to do, yolo. ' . $i;
721 $record->timeread = $timeread;
722 $record->timecreated = $timecreated;
723
724 $i++;
725
726 return $DB->insert_record('notifications', $record);
727 }
823e08bf
AN
728
729 /**
730 * Comparison function for sorting messages.
731 *
732 * @param \stdClass $a
733 * @param \stdClass $b
734 * @return bool
735 */
736 protected static function sort_messages($a, $b) {
737 return $a->message > $b->message;
738 }
739
740 /**
741 * Comparison function for sorting contacts.
742 *
743 * @param \stdClass $a
744 * @param \stdClass $b
745 * @return bool
746 */
747 protected static function sort_contacts($a, $b) {
748 return $a->contact > $b->contact;
749 }
04cd1b3f 750}