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