on-demand release 3.6dev+
[moodle.git] / message / tests / privacy_provider_test.php
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/>.
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  */
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();
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  */
39 class core_message_privacy_provider_testcase extends \core_privacy\tests\provider_testcase {
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();
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());
131     }
133     /**
134      * Test for provider::export_user_preferences().
135      */
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());
145     }
147     /**
148      * Test for provider::export_user_preferences().
149      */
150     public function test_export_user_preferences() {
151         global $USER;
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);
188             } else {
189                 $this->assertEquals(1, $pref->value);
190             }
191         }
192     }
194     /**
195      * Test for provider::get_contexts_for_userid().
196      */
197     public function test_get_contexts_for_userid() {
198         $this->resetAfterTest();
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     }
207     /**
208      * Test for provider::export_user_data().
209      */
210     public function test_export_for_context_with_contacts() {
211         $this->resetAfterTest();
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();
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);
223         $this->export_context_data_for_user($user1->id, \context_system::instance(), 'core_message');
225         $writer = writer::with_context(\context_system::instance());
227         $contacts = (array) $writer->get_data([get_string('contacts', 'core_message')]);
228         usort($contacts, ['static', 'sort_contacts']);
230         $this->assertCount(3, $contacts);
232         $contact1 = array_shift($contacts);
233         $this->assertEquals($user2->id, $contact1->contact);
235         $contact2 = array_shift($contacts);
236         $this->assertEquals($user3->id, $contact2->contact);
238         $contact3 = array_shift($contacts);
239         $this->assertEquals($user4->id, $contact3->contact);
240     }
242     /**
243      * Test for provider::export_user_data().
244      */
245     public function test_export_for_context_with_contact_requests() {
246         $this->resetAfterTest();
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();
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);
258         $this->export_context_data_for_user($user1->id, \context_system::instance(), 'core_message');
260         $writer = writer::with_context(\context_system::instance());
262         $contactrequests = (array) $writer->get_data([get_string('contactrequests', 'core_message')]);
264         $this->assertCount(3, $contactrequests);
266         $contactrequest1 = array_shift($contactrequests);
267         $this->assertEquals($user2->id, $contactrequest1->contactrequest);
268         $this->assertEquals(get_string('yes'), $contactrequest1->maderequest);
270         $contactrequest2 = array_shift($contactrequests);
271         $this->assertEquals($user3->id, $contactrequest2->contactrequest);
272         $this->assertEquals(get_string('no'), $contactrequest2->maderequest);
274         $contactrequest3 = array_shift($contactrequests);
275         $this->assertEquals($user4->id, $contactrequest3->contactrequest);
276         $this->assertEquals(get_string('yes'), $contactrequest3->maderequest);
277     }
279     /**
280      * Test for provider::export_user_data().
281      */
282     public function test_export_for_context_with_blocked_users() {
283         $this->resetAfterTest();
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();
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);
295         $this->export_context_data_for_user($user1->id, \context_system::instance(), 'core_message');
297         $writer = writer::with_context(\context_system::instance());
299         $blockedusers = (array) $writer->get_data([get_string('blockedusers', 'core_message')]);
301         $this->assertCount(3, $blockedusers);
303         $blockeduser1 = array_shift($blockedusers);
304         $this->assertEquals($user2->id, $blockeduser1->blockeduser);
306         $blockeduser2 = array_shift($blockedusers);
307         $this->assertEquals($user3->id, $blockeduser2->blockeduser);
309         $blockeduser3 = array_shift($blockedusers);
310         $this->assertEquals($user4->id, $blockeduser3->blockeduser);
311     }
313     /**
314      * Test for provider::export_user_data().
315      */
316     public function test_export_for_context_with_messages() {
317         global $DB;
319         $this->resetAfterTest();
321         // Create users to test with.
322         $user1 = $this->getDataGenerator()->create_user();
323         $user2 = $this->getDataGenerator()->create_user();
324         $user3 = $this->getDataGenerator()->create_user();
326         $now = time();
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));
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));
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));
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);
347         $this->export_context_data_for_user($user1->id, \context_system::instance(), 'core_message');
349         $writer = writer::with_context(\context_system::instance());
351         $this->assertTrue($writer->has_any_data());
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);
357         $dbm1 = $DB->get_record('messages', ['id' => $m1]);
358         $dbm2 = $DB->get_record('messages', ['id' => $m2]);
359         $dbm3 = $DB->get_record('messages', ['id' => $m3]);
361         usort($messages, ['static', 'sort_messages']);
362         $m1 = array_shift($messages);
363         $m2 = array_shift($messages);
364         $m3 = array_shift($messages);
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);
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);
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);
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);
387         $dbm4 = $DB->get_record('messages', ['id' => $m4]);
388         $dbm5 = $DB->get_record('messages', ['id' => $m5]);
389         $dbm6 = $DB->get_record('messages', ['id' => $m6]);
391         usort($messages, ['static', 'sort_messages']);
392         $m4 = array_shift($messages);
393         $m5 = array_shift($messages);
394         $m6 = array_shift($messages);
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);
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);
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     }
414     /**
415      * Test for provider::export_user_data().
416      */
417     public function test_export_for_context_with_notifications() {
418         $this->resetAfterTest();
420         // Create users to test with.
421         $user1 = $this->getDataGenerator()->create_user();
422         $user2 = $this->getDataGenerator()->create_user();
423         $user3 = $this->getDataGenerator()->create_user();
425         $now = time();
426         $timeread = $now - DAYSECS;
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));
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));
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));
443         $this->export_context_data_for_user($user1->id, \context_system::instance(), 'core_message');
445         $writer = writer::with_context(\context_system::instance());
447         $this->assertTrue($writer->has_any_data());
449         // Confirm the notifications.
450         $notifications = (array) $writer->get_data([get_string('notifications', 'core_message')]);
452         $this->assertCount(6, $notifications);
453     }
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;
461         $this->resetAfterTest();
463         // Create users to test with.
464         $user1 = $this->getDataGenerator()->create_user();
465         $user2 = $this->getDataGenerator()->create_user();
466         $user3 = $this->getDataGenerator()->create_user();
467         $user4 = $this->getDataGenerator()->create_user();
469         $now = time();
470         $timeread = $now - DAYSECS;
472         $systemcontext = \context_system::instance();
474         // Create contacts.
475         \core_message\api::add_contact($user1->id, $user2->id);
477         // Create contact requests.
478         \core_message\api::create_contact_request($user1->id, $user3->id);
480         // Block a user.
481         \core_message\api::block_user($user1->id, $user3->id);
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));
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));
491         // Delete one of the messages.
492         \core_message\api::delete_message($user1->id, $m2);
494         // There should be 1 contact.
495         $this->assertEquals(1, $DB->count_records('message_contacts'));
497         // There should be 1 contact request.
498         $this->assertEquals(1, $DB->count_records('message_contact_requests'));
500         // There should be 1 blocked user.
501         $this->assertEquals(1, $DB->count_records('message_users_blocked'));
503         // There should be two messages.
504         $this->assertEquals(2, $DB->count_records('messages'));
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'));
509         // There should be two conversation members.
510         $this->assertEquals(2, $DB->count_records('message_conversation_members'));
512         // There should be two notifications + one for the contact request.
513         $this->assertEquals(3, $DB->count_records('notifications'));
515         provider::delete_data_for_all_users_in_context($systemcontext);
517         // Confirm all has been deleted.
518         $this->assertEquals(0, $DB->count_records('message_contacts'));
519         $this->assertEquals(0, $DB->count_records('message_contact_requests'));
520         $this->assertEquals(0, $DB->count_records('message_users_blocked'));
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     }
527     /**
528      * Test for provider::delete_data_for_user().
529      */
530     public function test_delete_data_for_user() {
531         global $DB;
533         $this->resetAfterTest();
535         // Create users to test with.
536         $user1 = $this->getDataGenerator()->create_user();
537         $user2 = $this->getDataGenerator()->create_user();
538         $user3 = $this->getDataGenerator()->create_user();
539         $user4 = $this->getDataGenerator()->create_user();
540         $user5 = $this->getDataGenerator()->create_user();
541         $user6 = $this->getDataGenerator()->create_user();
543         $now = time();
544         $timeread = $now - DAYSECS;
546         // Create contacts.
547         \core_message\api::add_contact($user1->id, $user2->id);
548         \core_message\api::add_contact($user2->id, $user3->id);
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);
554         // Block users.
555         \core_message\api::block_user($user1->id, $user5->id);
556         \core_message\api::block_user($user2->id, $user6->id);
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));
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));
567         // Delete one of the messages.
568         \core_message\api::delete_message($user1->id, $m2);
570         // There should be 2 contacts.
571         $this->assertEquals(2, $DB->count_records('message_contacts'));
573         // There should be 1 contact request.
574         $this->assertEquals(2, $DB->count_records('message_contact_requests'));
576         // There should be 1 blocked user.
577         $this->assertEquals(2, $DB->count_records('message_users_blocked'));
579         // There should be two messages.
580         $this->assertEquals(2, $DB->count_records('messages'));
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'));
585         // There should be two conversation members.
586         $this->assertEquals(2, $DB->count_records('message_conversation_members'));
588         // There should be three notifications + two for the contact requests.
589         $this->assertEquals(5, $DB->count_records('notifications'));
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);
596         // Confirm the user 2 data still exists.
597         $contacts = $DB->get_records('message_contacts');
598         $contactrequests = $DB->get_records('message_contact_requests');
599         $blockedusers = $DB->get_records('message_users_blocked');
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');
605         $this->assertCount(1, $contacts);
606         $contact = reset($contacts);
607         $this->assertEquals($user2->id, $contact->userid);
608         $this->assertEquals($user3->id, $contact->contactid);
610         $this->assertCount(1, $contactrequests);
611         $contactrequest = reset($contactrequests);
612         $this->assertEquals($user2->id, $contactrequest->userid);
613         $this->assertEquals($user4->id, $contactrequest->requesteduserid);
615         $this->assertCount(1, $blockedusers);
616         $blockeduser = reset($blockedusers);
617         $this->assertEquals($user2->id, $blockeduser->userid);
618         $this->assertEquals($user6->id, $blockeduser->blockeduserid);
620         $this->assertCount(1, $messages);
621         $message = reset($messages);
622         $this->assertEquals($m2, $message->id);
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);
630         $this->assertCount(1, $mcms);
631         $mcm = reset($mcms);
632         $this->assertEquals($user2->id, $mcm->userid);
634         $this->assertCount(2, $notifications);
635         ksort($notifications);
637         $notification = array_shift($notifications);
638         $this->assertEquals($user2->id, $notification->useridfrom);
639         $this->assertEquals($user4->id, $notification->useridto);
641         $notification = array_shift($notifications);
642         $this->assertEquals($user2->id, $notification->useridfrom);
643         $this->assertEquals($user3->id, $notification->useridto);
644     }
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;
659         static $i = 1;
661         if (is_null($timecreated)) {
662             $timecreated = time();
663         }
665         if (!$conversationid = \core_message\api::get_conversation_between_users([$useridfrom, $useridto])) {
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;
674         }
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;
685         $i++;
687         $record->id = $DB->insert_record('messages', $record);
689         if ($read) {
690             \core_message\api::mark_message_as_read($useridto, $record);
691         }
693         return $record->id;
694     }
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;
709         static $i = 1;
711         if (is_null($timecreated)) {
712             $timecreated = time();
713         }
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;
724         $i++;
726         return $DB->insert_record('notifications', $record);
727     }
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     }
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     }