2 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
18 * Privacy provider tests.
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
25 use core_privacy\local\metadata\collection;
26 use core_message\privacy\provider;
27 use \core_privacy\local\request\writer;
28 use \core_privacy\local\request\transform;
30 defined('MOODLE_INTERNAL') || die();
33 * Privacy provider tests class.
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
39 class core_message_privacy_provider_testcase extends \core_privacy\tests\provider_testcase {
42 * Test for provider::get_metadata().
44 public function test_get_metadata() {
45 $collection = new collection('core_message');
46 $newcollection = provider::get_metadata($collection);
47 $itemcollection = $newcollection->get_collection();
48 $this->assertCount(8, $itemcollection);
50 $messagestable = array_shift($itemcollection);
51 $this->assertEquals('messages', $messagestable->get_name());
53 $messageuseractionstable = array_shift($itemcollection);
54 $this->assertEquals('message_user_actions', $messageuseractionstable->get_name());
56 $messageconversationmemberstable = array_shift($itemcollection);
57 $this->assertEquals('message_conversation_members', $messageconversationmemberstable->get_name());
59 $messagecontacts = array_shift($itemcollection);
60 $this->assertEquals('message_contacts', $messagecontacts->get_name());
62 $messagecontactrequests = array_shift($itemcollection);
63 $this->assertEquals('message_contact_requests', $messagecontactrequests->get_name());
65 $messageusersblocked = array_shift($itemcollection);
66 $this->assertEquals('message_users_blocked', $messageusersblocked->get_name());
68 $notificationstable = array_shift($itemcollection);
69 $this->assertEquals('notifications', $notificationstable->get_name());
71 $usersettings = array_shift($itemcollection);
72 $this->assertEquals('core_message_messageprovider_settings', $usersettings->get_name());
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());
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());
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());
98 $privacyfields = $messagecontacts->get_privacy_fields();
99 $this->assertArrayHasKey('userid', $privacyfields);
100 $this->assertArrayHasKey('contactid', $privacyfields);
101 $this->assertArrayHasKey('timecreated', $privacyfields);
102 $this->assertEquals('privacy:metadata:message_contacts', $messagecontacts->get_summary());
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());
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());
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());
134 * Test for provider::export_user_preferences().
136 public function test_export_user_preferences_no_pref() {
137 $this->resetAfterTest();
139 $user = $this->getDataGenerator()->create_user();
140 provider::export_user_preferences($user->id);
142 $writer = writer::with_context(\context_system::instance());
144 $this->assertFalse($writer->has_any_data());
148 * Test for provider::export_user_preferences().
150 public function test_export_user_preferences() {
153 $this->resetAfterTest();
155 $this->setAdminUser();
157 // Create another user to set a preference for who we won't be exporting.
158 $user = $this->getDataGenerator()->create_user();
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);
163 set_user_preference('message_blocknoncontacts', \core_message\api::MESSAGE_PRIVACY_ONLYCONTACTS, $USER->id);
164 set_user_preference('message_provider_moodle_instantmessage_loggedoff', 'inbound', $user->id);
166 // Set an unrelated preference.
167 set_user_preference('some_unrelated_preference', 'courses', $USER->id);
169 provider::export_user_preferences($USER->id);
171 $writer = writer::with_context(\context_system::instance());
173 $this->assertTrue($writer->has_any_data());
175 $prefs = (array) $writer->get_user_preferences('core_message');
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);
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);
189 $this->assertEquals(1, $pref->value);
195 * Test for provider::get_contexts_for_userid() when there is no message or notification.
197 public function test_get_contexts_for_userid_no_data() {
198 $this->resetAfterTest();
200 $user = $this->getDataGenerator()->create_user();
201 $contextlist = provider::get_contexts_for_userid($user->id);
202 $this->assertEmpty($contextlist);
206 * Test for provider::get_contexts_for_userid() when there is a message between users.
208 public function test_get_contexts_for_userid_with_message() {
209 $this->resetAfterTest();
211 $user1 = $this->getDataGenerator()->create_user();
212 $user2 = $this->getDataGenerator()->create_user();
214 $this->create_message($user1->id, $user2->id, time() - (9 * DAYSECS));
216 // Test for the sender.
217 $contextlist = provider::get_contexts_for_userid($user1->id);
218 $this->assertCount(1, $contextlist);
219 $contextforuser = $contextlist->current();
221 context_user::instance($user1->id)->id,
222 $contextforuser->id);
224 // Test for the receiver.
225 $contextlist = provider::get_contexts_for_userid($user2->id);
226 $this->assertCount(1, $contextlist);
227 $contextforuser = $contextlist->current();
229 context_user::instance($user2->id)->id,
230 $contextforuser->id);
234 * Test for provider::get_contexts_for_userid() when there is a notification between users.
236 public function test_get_contexts_for_userid_with_notification() {
237 $this->resetAfterTest();
239 $user1 = $this->getDataGenerator()->create_user();
240 $user2 = $this->getDataGenerator()->create_user();
242 $this->create_notification($user1->id, $user2->id, time() - (9 * DAYSECS));
244 // Test for the sender.
245 $contextlist = provider::get_contexts_for_userid($user1->id);
246 $this->assertCount(1, $contextlist);
247 $contextforuser = $contextlist->current();
249 context_user::instance($user1->id)->id,
250 $contextforuser->id);
252 // Test for the receiver.
253 $contextlist = provider::get_contexts_for_userid($user2->id);
254 $this->assertCount(1, $contextlist);
255 $contextforuser = $contextlist->current();
257 context_user::instance($user2->id)->id,
258 $contextforuser->id);
262 * Test for provider::export_user_data().
264 public function test_export_for_context_with_contacts() {
265 $this->resetAfterTest();
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();
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);
277 $user1context = context_user::instance($user1->id);
279 $this->export_context_data_for_user($user1->id, $user1context, 'core_message');
281 $writer = writer::with_context($user1context);
283 $contacts = (array) $writer->get_data([get_string('contacts', 'core_message')]);
284 usort($contacts, ['static', 'sort_contacts']);
286 $this->assertCount(3, $contacts);
288 $contact1 = array_shift($contacts);
289 $this->assertEquals($user2->id, $contact1->contact);
291 $contact2 = array_shift($contacts);
292 $this->assertEquals($user3->id, $contact2->contact);
294 $contact3 = array_shift($contacts);
295 $this->assertEquals($user4->id, $contact3->contact);
299 * Test for provider::export_user_data().
301 public function test_export_for_context_with_contact_requests() {
302 $this->resetAfterTest();
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();
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);
314 $user1context = context_user::instance($user1->id);
316 $this->export_context_data_for_user($user1->id, $user1context, 'core_message');
318 $writer = writer::with_context($user1context);
320 $contactrequests = (array) $writer->get_data([get_string('contactrequests', 'core_message')]);
322 $this->assertCount(3, $contactrequests);
324 $contactrequest1 = array_shift($contactrequests);
325 $this->assertEquals($user2->id, $contactrequest1->contactrequest);
326 $this->assertEquals(get_string('yes'), $contactrequest1->maderequest);
328 $contactrequest2 = array_shift($contactrequests);
329 $this->assertEquals($user3->id, $contactrequest2->contactrequest);
330 $this->assertEquals(get_string('no'), $contactrequest2->maderequest);
332 $contactrequest3 = array_shift($contactrequests);
333 $this->assertEquals($user4->id, $contactrequest3->contactrequest);
334 $this->assertEquals(get_string('yes'), $contactrequest3->maderequest);
338 * Test for provider::export_user_data().
340 public function test_export_for_context_with_blocked_users() {
341 $this->resetAfterTest();
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();
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);
353 $user1context = context_user::instance($user1->id);
355 $this->export_context_data_for_user($user1->id, $user1context, 'core_message');
357 $writer = writer::with_context($user1context);
359 $blockedusers = (array) $writer->get_data([get_string('blockedusers', 'core_message')]);
361 $this->assertCount(3, $blockedusers);
363 $blockeduser1 = array_shift($blockedusers);
364 $this->assertEquals($user2->id, $blockeduser1->blockeduser);
366 $blockeduser2 = array_shift($blockedusers);
367 $this->assertEquals($user3->id, $blockeduser2->blockeduser);
369 $blockeduser3 = array_shift($blockedusers);
370 $this->assertEquals($user4->id, $blockeduser3->blockeduser);
374 * Test for provider::export_user_data().
376 public function test_export_for_context_with_messages() {
379 $this->resetAfterTest();
381 // Create users to test with.
382 $user1 = $this->getDataGenerator()->create_user();
383 $user2 = $this->getDataGenerator()->create_user();
384 $user3 = $this->getDataGenerator()->create_user();
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));
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));
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));
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);
407 $user1context = context_user::instance($user1->id);
409 $this->export_context_data_for_user($user1->id, $user1context, 'core_message');
411 $writer = writer::with_context($user1context);
413 $this->assertTrue($writer->has_any_data());
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);
419 $dbm1 = $DB->get_record('messages', ['id' => $m1]);
420 $dbm2 = $DB->get_record('messages', ['id' => $m2]);
421 $dbm3 = $DB->get_record('messages', ['id' => $m3]);
423 usort($messages, ['static', 'sort_messages']);
424 $m1 = array_shift($messages);
425 $m2 = array_shift($messages);
426 $m3 = array_shift($messages);
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);
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);
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);
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);
449 $dbm4 = $DB->get_record('messages', ['id' => $m4]);
450 $dbm5 = $DB->get_record('messages', ['id' => $m5]);
451 $dbm6 = $DB->get_record('messages', ['id' => $m6]);
453 usort($messages, ['static', 'sort_messages']);
454 $m4 = array_shift($messages);
455 $m5 = array_shift($messages);
456 $m6 = array_shift($messages);
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);
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);
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);
477 * Test for provider::export_user_data().
479 public function test_export_for_context_with_notifications() {
480 $this->resetAfterTest();
482 // Create users to test with.
483 $user1 = $this->getDataGenerator()->create_user();
484 $user2 = $this->getDataGenerator()->create_user();
485 $user3 = $this->getDataGenerator()->create_user();
488 $timeread = $now - DAYSECS;
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));
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));
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));
505 $user1context = context_user::instance($user1->id);
507 $this->export_context_data_for_user($user1->id, $user1context, 'core_message');
509 $writer = writer::with_context($user1context);
511 $this->assertTrue($writer->has_any_data());
513 // Confirm the notifications.
514 $notifications = (array) $writer->get_data([get_string('notifications', 'core_message')]);
516 $this->assertCount(6, $notifications);
520 * Test for provider::delete_data_for_all_users_in_context().
522 public function test_delete_data_for_all_users_in_context() {
525 $this->resetAfterTest();
527 // Create users to test with.
528 $user1 = $this->getDataGenerator()->create_user();
529 $user2 = $this->getDataGenerator()->create_user();
530 $user3 = $this->getDataGenerator()->create_user();
531 $user4 = $this->getDataGenerator()->create_user();
532 $user5 = $this->getDataGenerator()->create_user();
535 $timeread = $now - DAYSECS;
537 $user1context = context_user::instance($user1->id);
540 \core_message\api::add_contact($user1->id, $user2->id);
541 \core_message\api::add_contact($user2->id, $user3->id);
543 // Create contact requests.
544 \core_message\api::create_contact_request($user1->id, $user3->id);
545 \core_message\api::create_contact_request($user2->id, $user4->id);
548 \core_message\api::block_user($user1->id, $user3->id);
549 \core_message\api::block_user($user3->id, $user4->id);
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));
554 $m3 = $this->create_message($user2->id, $user3->id, $now + (7 * DAYSECS));
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));
559 $n3 = $this->create_notification($user2->id, $user3->id, $now + (7 * DAYSECS));
561 // Delete one of the messages.
562 \core_message\api::delete_message($user1->id, $m2);
564 // There should be 2 contacts.
565 $this->assertEquals(2, $DB->count_records('message_contacts'));
567 // There should be 2 contact requests.
568 $this->assertEquals(2, $DB->count_records('message_contact_requests'));
570 // There should be 2 blocked users.
571 $this->assertEquals(2, $DB->count_records('message_users_blocked'));
573 // There should be 3 messages.
574 $this->assertEquals(3, $DB->count_records('messages'));
576 // There should be 2 user actions - one for reading the message, one for deleting.
577 $this->assertEquals(2, $DB->count_records('message_user_actions'));
579 // There should be 4 conversation members.
580 $this->assertEquals(4, $DB->count_records('message_conversation_members'));
582 // There should be 3 notifications + 2 for the contact request.
583 $this->assertEquals(5, $DB->count_records('notifications'));
585 provider::delete_data_for_all_users_in_context($user1context);
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]));
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]));
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]));
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]));
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]));
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]));
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]));
629 * Test for provider::delete_data_for_user().
631 public function test_delete_data_for_user() {
634 $this->resetAfterTest();
636 // Create users to test with.
637 $user1 = $this->getDataGenerator()->create_user();
638 $user2 = $this->getDataGenerator()->create_user();
639 $user3 = $this->getDataGenerator()->create_user();
640 $user4 = $this->getDataGenerator()->create_user();
641 $user5 = $this->getDataGenerator()->create_user();
642 $user6 = $this->getDataGenerator()->create_user();
645 $timeread = $now - DAYSECS;
648 \core_message\api::add_contact($user1->id, $user2->id);
649 \core_message\api::add_contact($user2->id, $user3->id);
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);
656 \core_message\api::block_user($user1->id, $user5->id);
657 \core_message\api::block_user($user2->id, $user6->id);
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));
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));
668 // Delete one of the messages.
669 \core_message\api::delete_message($user1->id, $m2);
671 // There should be 2 contacts.
672 $this->assertEquals(2, $DB->count_records('message_contacts'));
674 // There should be 1 contact request.
675 $this->assertEquals(2, $DB->count_records('message_contact_requests'));
677 // There should be 1 blocked user.
678 $this->assertEquals(2, $DB->count_records('message_users_blocked'));
680 // There should be two messages.
681 $this->assertEquals(2, $DB->count_records('messages'));
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'));
686 // There should be two conversation members.
687 $this->assertEquals(2, $DB->count_records('message_conversation_members'));
689 // There should be three notifications + two for the contact requests.
690 $this->assertEquals(5, $DB->count_records('notifications'));
692 $user1context = context_user::instance($user1->id);
693 $contextlist = new \core_privacy\local\request\approved_contextlist($user1, 'core_message',
694 [$user1context->id]);
695 provider::delete_data_for_user($contextlist);
697 // Confirm the user 2 data still exists.
698 $contacts = $DB->get_records('message_contacts');
699 $contactrequests = $DB->get_records('message_contact_requests');
700 $blockedusers = $DB->get_records('message_users_blocked');
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');
706 $this->assertCount(1, $contacts);
707 $contact = reset($contacts);
708 $this->assertEquals($user2->id, $contact->userid);
709 $this->assertEquals($user3->id, $contact->contactid);
711 $this->assertCount(1, $contactrequests);
712 $contactrequest = reset($contactrequests);
713 $this->assertEquals($user2->id, $contactrequest->userid);
714 $this->assertEquals($user4->id, $contactrequest->requesteduserid);
716 $this->assertCount(1, $blockedusers);
717 $blockeduser = reset($blockedusers);
718 $this->assertEquals($user2->id, $blockeduser->userid);
719 $this->assertEquals($user6->id, $blockeduser->blockeduserid);
721 $this->assertCount(1, $messages);
722 $message = reset($messages);
723 $this->assertEquals($m2, $message->id);
725 $this->assertCount(1, $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);
731 $this->assertCount(1, $mcms);
733 $this->assertEquals($user2->id, $mcm->userid);
735 $this->assertCount(2, $notifications);
736 ksort($notifications);
738 $notification = array_shift($notifications);
739 $this->assertEquals($user2->id, $notification->useridfrom);
740 $this->assertEquals($user4->id, $notification->useridto);
742 $notification = array_shift($notifications);
743 $this->assertEquals($user2->id, $notification->useridfrom);
744 $this->assertEquals($user3->id, $notification->useridto);
748 * Test for provider::get_users_in_context() when there is no message or notification.
750 public function test_get_users_in_context_no_data() {
751 $this->resetAfterTest();
753 $user = $this->getDataGenerator()->create_user();
754 $usercontext = context_user::instance($user->id);
756 $userlist = new \core_privacy\local\request\userlist($usercontext, 'core_message');
757 \core_message\privacy\provider::get_users_in_context($userlist);
759 $this->assertEmpty($userlist->get_userids());
763 * Test for provider::get_users_in_context() when there is a message between users.
765 public function test_get_users_in_context_with_message() {
766 $this->resetAfterTest();
768 $user1 = $this->getDataGenerator()->create_user();
769 $user2 = $this->getDataGenerator()->create_user();
771 $user1context = context_user::instance($user1->id);
772 $user2context = context_user::instance($user2->id);
774 $this->create_message($user1->id, $user2->id, time() - (9 * DAYSECS));
776 // Test for the sender.
777 $userlist = new \core_privacy\local\request\userlist($user1context, 'core_message');
778 \core_message\privacy\provider::get_users_in_context($userlist);
779 $this->assertCount(1, $userlist);
780 $userincontext = $userlist->current();
781 $this->assertEquals($user1->id, $userincontext->id);
783 // Test for the receiver.
784 $userlist = new \core_privacy\local\request\userlist($user2context, 'core_message');
785 \core_message\privacy\provider::get_users_in_context($userlist);
786 $this->assertCount(1, $userlist);
787 $userincontext = $userlist->current();
788 $this->assertEquals($user2->id, $userincontext->id);
792 * Test for provider::get_users_in_context() when there is a notification between users.
794 public function test_get_users_in_context_with_notification() {
795 $this->resetAfterTest();
797 $user1 = $this->getDataGenerator()->create_user();
798 $user2 = $this->getDataGenerator()->create_user();
800 $user1context = context_user::instance($user1->id);
801 $user2context = context_user::instance($user2->id);
803 $this->create_notification($user1->id, $user2->id, time() - (9 * DAYSECS));
805 // Test for the sender.
806 $userlist = new \core_privacy\local\request\userlist($user1context, 'core_message');
807 \core_message\privacy\provider::get_users_in_context($userlist);
808 $this->assertCount(1, $userlist);
809 $userincontext = $userlist->current();
810 $this->assertEquals($user1->id, $userincontext->id);
812 // Test for the receiver.
813 $userlist = new \core_privacy\local\request\userlist($user2context, 'core_message');
814 \core_message\privacy\provider::get_users_in_context($userlist);
815 $this->assertCount(1, $userlist);
816 $userincontext = $userlist->current();
817 $this->assertEquals($user2->id, $userincontext->id);
821 * Test for provider::delete_data_for_users().
823 public function test_delete_data_for_users() {
826 $this->resetAfterTest();
828 // Create users to test with.
829 $user1 = $this->getDataGenerator()->create_user();
830 $user2 = $this->getDataGenerator()->create_user();
831 $user3 = $this->getDataGenerator()->create_user();
832 $user4 = $this->getDataGenerator()->create_user();
833 $user5 = $this->getDataGenerator()->create_user();
834 $user6 = $this->getDataGenerator()->create_user();
837 $timeread = $now - DAYSECS;
840 \core_message\api::add_contact($user1->id, $user2->id);
841 \core_message\api::add_contact($user2->id, $user3->id);
843 // Create contact requests.
844 \core_message\api::create_contact_request($user1->id, $user3->id);
845 \core_message\api::create_contact_request($user2->id, $user4->id);
848 \core_message\api::block_user($user1->id, $user5->id);
849 \core_message\api::block_user($user2->id, $user6->id);
852 $m1 = $this->create_message($user1->id, $user2->id, $now + (9 * DAYSECS), $timeread);
853 $m2 = $this->create_message($user2->id, $user1->id, $now + (8 * DAYSECS));
855 // Create notifications.
856 $n1 = $this->create_notification($user1->id, $user2->id, $now + (9 * DAYSECS), $timeread);
857 $n2 = $this->create_notification($user2->id, $user1->id, $now + (8 * DAYSECS));
858 $n2 = $this->create_notification($user2->id, $user3->id, $now + (8 * DAYSECS));
860 // Delete one of the messages.
861 \core_message\api::delete_message($user1->id, $m2);
863 // There should be 2 contacts.
864 $this->assertEquals(2, $DB->count_records('message_contacts'));
866 // There should be 1 contact request.
867 $this->assertEquals(2, $DB->count_records('message_contact_requests'));
869 // There should be 1 blocked user.
870 $this->assertEquals(2, $DB->count_records('message_users_blocked'));
872 // There should be two messages.
873 $this->assertEquals(2, $DB->count_records('messages'));
875 // There should be two user actions - one for reading the message, one for deleting.
876 $this->assertEquals(2, $DB->count_records('message_user_actions'));
878 // There should be two conversation members.
879 $this->assertEquals(2, $DB->count_records('message_conversation_members'));
881 // There should be three notifications + two for the contact requests.
882 $this->assertEquals(5, $DB->count_records('notifications'));
884 $user1context = context_user::instance($user1->id);
885 $approveduserlist = new \core_privacy\local\request\approved_userlist($user1context, 'core_message',
886 [$user1->id, $user2->id]);
887 provider::delete_data_for_users($approveduserlist);
889 // Only user1's data should be deleted. User2 should be skipped as user2 is an invalid user for user1context.
891 // Confirm the user 2 data still exists.
892 $contacts = $DB->get_records('message_contacts');
893 $contactrequests = $DB->get_records('message_contact_requests');
894 $blockedusers = $DB->get_records('message_users_blocked');
895 $messages = $DB->get_records('messages');
896 $muas = $DB->get_records('message_user_actions');
897 $mcms = $DB->get_records('message_conversation_members');
898 $notifications = $DB->get_records('notifications');
900 $this->assertCount(1, $contacts);
901 $contact = reset($contacts);
902 $this->assertEquals($user2->id, $contact->userid);
903 $this->assertEquals($user3->id, $contact->contactid);
905 $this->assertCount(1, $contactrequests);
906 $contactrequest = reset($contactrequests);
907 $this->assertEquals($user2->id, $contactrequest->userid);
908 $this->assertEquals($user4->id, $contactrequest->requesteduserid);
910 $this->assertCount(1, $blockedusers);
911 $blockeduser = reset($blockedusers);
912 $this->assertEquals($user2->id, $blockeduser->userid);
913 $this->assertEquals($user6->id, $blockeduser->blockeduserid);
915 $this->assertCount(1, $messages);
916 $message = reset($messages);
917 $this->assertEquals($m2, $message->id);
919 $this->assertCount(1, $muas);
921 $this->assertEquals($user2->id, $mua->userid);
922 $this->assertEquals($m1, $mua->messageid);
923 $this->assertEquals(\core_message\api::MESSAGE_ACTION_READ, $mua->action);
925 $this->assertCount(1, $mcms);
927 $this->assertEquals($user2->id, $mcm->userid);
929 $this->assertCount(2, $notifications);
930 ksort($notifications);
932 $notification = array_shift($notifications);
933 $this->assertEquals($user2->id, $notification->useridfrom);
934 $this->assertEquals($user4->id, $notification->useridto);
936 $notification = array_shift($notifications);
937 $this->assertEquals($user2->id, $notification->useridfrom);
938 $this->assertEquals($user3->id, $notification->useridto);
942 * Creates a message to be used for testing.
944 * @param int $useridfrom The user id from
945 * @param int $useridto The user id to
946 * @param int $timecreated
947 * @param bool $read Do we want to mark the message as read?
948 * @return int The id of the message
949 * @throws dml_exception
951 private function create_message(int $useridfrom, int $useridto, int $timecreated = null, bool $read = false) {
956 if (is_null($timecreated)) {
957 $timecreated = time();
960 if (!$conversationid = \core_message\api::get_conversation_between_users([$useridfrom, $useridto])) {
961 $conversation = \core_message\api::create_conversation(
962 \core_message\api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL,
968 $conversationid = $conversation->id;
971 // Ok, send the message.
972 $record = new stdClass();
973 $record->useridfrom = $useridfrom;
974 $record->conversationid = $conversationid;
975 $record->subject = 'No subject';
976 $record->fullmessage = 'A rad message ' . $i;
977 $record->smallmessage = 'A rad message ' . $i;
978 $record->timecreated = $timecreated;
982 $record->id = $DB->insert_record('messages', $record);
985 \core_message\api::mark_message_as_read($useridto, $record);
992 * Creates a notification to be used for testing.
994 * @param int $useridfrom The user id from
995 * @param int $useridto The user id to
996 * @param int|null $timecreated The time the notification was created
997 * @param int|null $timeread The time the notification was read, null if it hasn't been.
998 * @return int The id of the notification
999 * @throws dml_exception
1001 private function create_notification(int $useridfrom, int $useridto, int $timecreated = null, int $timeread = null) {
1006 if (is_null($timecreated)) {
1007 $timecreated = time();
1010 $record = new stdClass();
1011 $record->useridfrom = $useridfrom;
1012 $record->useridto = $useridto;
1013 $record->subject = 'No subject';
1014 $record->fullmessage = 'Some rad notification ' . $i;
1015 $record->smallmessage = 'Yo homie, you got some stuff to do, yolo. ' . $i;
1016 $record->timeread = $timeread;
1017 $record->timecreated = $timecreated;
1021 return $DB->insert_record('notifications', $record);
1025 * Comparison function for sorting messages.
1027 * @param \stdClass $a
1028 * @param \stdClass $b
1031 protected static function sort_messages($a, $b) {
1032 return $a->message > $b->message;
1036 * Comparison function for sorting contacts.
1038 * @param \stdClass $a
1039 * @param \stdClass $b
1042 protected static function sort_contacts($a, $b) {
1043 return $a->contact > $b->contact;