MDL-63712 core_message: Data should be in user context, not system
[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() when there is no message or notification.
196      */
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);
203     }
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();
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();
220         $this->assertEquals(
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();
228         $this->assertEquals(
229                 context_user::instance($user2->id)->id,
230                 $contextforuser->id);
231     }
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();
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();
248         $this->assertEquals(
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();
256         $this->assertEquals(
257                 context_user::instance($user2->id)->id,
258                 $contextforuser->id);
259     }
261     /**
262      * Test for provider::export_user_data().
263      */
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);
296     }
298     /**
299      * Test for provider::export_user_data().
300      */
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);
335     }
337     /**
338      * Test for provider::export_user_data().
339      */
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);
371     }
373     /**
374      * Test for provider::export_user_data().
375      */
376     public function test_export_for_context_with_messages() {
377         global $DB;
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();
386         $now = time();
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);
474     }
476     /**
477      * Test for provider::export_user_data().
478      */
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();
487         $now = time();
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);
517     }
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;
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();
534         $now = time();
535         $timeread = $now - DAYSECS;
537         $user1context = context_user::instance($user1->id);
539         // Create contacts.
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);
547         // Block a user.
548         \core_message\api::block_user($user1->id, $user3->id);
549         \core_message\api::block_user($user3->id, $user4->id);
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));
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]));
626     }
628     /**
629      * Test for provider::delete_data_for_user().
630      */
631     public function test_delete_data_for_user() {
632         global $DB;
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();
644         $now = time();
645         $timeread = $now - DAYSECS;
647         // Create contacts.
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);
655         // Block users.
656         \core_message\api::block_user($user1->id, $user5->id);
657         \core_message\api::block_user($user2->id, $user6->id);
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));
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);
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);
731         $this->assertCount(1, $mcms);
732         $mcm = reset($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);
745     }
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;
760         static $i = 1;
762         if (is_null($timecreated)) {
763             $timecreated = time();
764         }
766         if (!$conversationid = \core_message\api::get_conversation_between_users([$useridfrom, $useridto])) {
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;
775         }
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;
786         $i++;
788         $record->id = $DB->insert_record('messages', $record);
790         if ($read) {
791             \core_message\api::mark_message_as_read($useridto, $record);
792         }
794         return $record->id;
795     }
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;
810         static $i = 1;
812         if (is_null($timecreated)) {
813             $timecreated = time();
814         }
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;
825         $i++;
827         return $DB->insert_record('notifications', $record);
828     }
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     }
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     }