MDL-64993 core_message: fix behat test failing with Oracle
[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\contextlist;
28 use \core_privacy\local\request\writer;
29 use \core_privacy\local\request\transform;
30 use \core_message\tests\helper as testhelper;
32 defined('MOODLE_INTERNAL') || die();
34 /**
35  * Privacy provider tests class.
36  *
37  * @package    core_message
38  * @copyright  2018 Mark Nelson <markn@moodle.com>
39  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
40  */
41 class core_message_privacy_provider_testcase extends \core_privacy\tests\provider_testcase {
43     /**
44      * Test for provider::get_metadata().
45      */
46     public function test_get_metadata() {
47         $collection = new collection('core_message');
48         $newcollection = provider::get_metadata($collection);
49         $itemcollection = $newcollection->get_collection();
50         $this->assertCount(10, $itemcollection);
52         $messagestable = array_shift($itemcollection);
53         $this->assertEquals('messages', $messagestable->get_name());
55         $messageuseractionstable = array_shift($itemcollection);
56         $this->assertEquals('message_user_actions', $messageuseractionstable->get_name());
58         $messageconversationmemberstable = array_shift($itemcollection);
59         $this->assertEquals('message_conversation_members', $messageconversationmemberstable->get_name());
61         $messageconversationactions = array_shift($itemcollection);
62         $this->assertEquals('message_conversation_actions', $messageconversationactions->get_name());
64         $messagecontacts = array_shift($itemcollection);
65         $this->assertEquals('message_contacts', $messagecontacts->get_name());
67         $messagecontactrequests = array_shift($itemcollection);
68         $this->assertEquals('message_contact_requests', $messagecontactrequests->get_name());
70         $messageusersblocked = array_shift($itemcollection);
71         $this->assertEquals('message_users_blocked', $messageusersblocked->get_name());
73         $notificationstable = array_shift($itemcollection);
74         $this->assertEquals('notifications', $notificationstable->get_name());
76         $usersettings = array_shift($itemcollection);
77         $this->assertEquals('core_message_messageprovider_settings', $usersettings->get_name());
79         $favouriteconversations = array_shift($itemcollection);
80         $this->assertEquals('core_favourites', $favouriteconversations->get_name());
81         $this->assertEquals('privacy:metadata:core_favourites', $favouriteconversations->get_summary());
83         $privacyfields = $messagestable->get_privacy_fields();
84         $this->assertArrayHasKey('useridfrom', $privacyfields);
85         $this->assertArrayHasKey('conversationid', $privacyfields);
86         $this->assertArrayHasKey('subject', $privacyfields);
87         $this->assertArrayHasKey('fullmessage', $privacyfields);
88         $this->assertArrayHasKey('fullmessageformat', $privacyfields);
89         $this->assertArrayHasKey('fullmessagehtml', $privacyfields);
90         $this->assertArrayHasKey('smallmessage', $privacyfields);
91         $this->assertArrayHasKey('timecreated', $privacyfields);
92         $this->assertArrayHasKey('customdata', $privacyfields);
93         $this->assertEquals('privacy:metadata:messages', $messagestable->get_summary());
95         $privacyfields = $messageuseractionstable->get_privacy_fields();
96         $this->assertArrayHasKey('userid', $privacyfields);
97         $this->assertArrayHasKey('messageid', $privacyfields);
98         $this->assertArrayHasKey('action', $privacyfields);
99         $this->assertArrayHasKey('timecreated', $privacyfields);
100         $this->assertEquals('privacy:metadata:message_user_actions', $messageuseractionstable->get_summary());
102         $privacyfields = $messageconversationmemberstable->get_privacy_fields();
103         $this->assertArrayHasKey('conversationid', $privacyfields);
104         $this->assertArrayHasKey('userid', $privacyfields);
105         $this->assertArrayHasKey('timecreated', $privacyfields);
106         $this->assertEquals('privacy:metadata:message_conversation_members', $messageconversationmemberstable->get_summary());
108         $privacyfields = $messagecontacts->get_privacy_fields();
109         $this->assertArrayHasKey('userid', $privacyfields);
110         $this->assertArrayHasKey('contactid', $privacyfields);
111         $this->assertArrayHasKey('timecreated', $privacyfields);
112         $this->assertEquals('privacy:metadata:message_contacts', $messagecontacts->get_summary());
114         $privacyfields = $messagecontactrequests->get_privacy_fields();
115         $this->assertArrayHasKey('userid', $privacyfields);
116         $this->assertArrayHasKey('requesteduserid', $privacyfields);
117         $this->assertArrayHasKey('timecreated', $privacyfields);
118         $this->assertEquals('privacy:metadata:message_contact_requests', $messagecontactrequests->get_summary());
120         $privacyfields = $messageusersblocked->get_privacy_fields();
121         $this->assertArrayHasKey('userid', $privacyfields);
122         $this->assertArrayHasKey('blockeduserid', $privacyfields);
123         $this->assertArrayHasKey('timecreated', $privacyfields);
124         $this->assertEquals('privacy:metadata:message_users_blocked', $messageusersblocked->get_summary());
126         $privacyfields = $notificationstable->get_privacy_fields();
127         $this->assertArrayHasKey('useridfrom', $privacyfields);
128         $this->assertArrayHasKey('useridto', $privacyfields);
129         $this->assertArrayHasKey('subject', $privacyfields);
130         $this->assertArrayHasKey('fullmessage', $privacyfields);
131         $this->assertArrayHasKey('fullmessageformat', $privacyfields);
132         $this->assertArrayHasKey('fullmessagehtml', $privacyfields);
133         $this->assertArrayHasKey('smallmessage', $privacyfields);
134         $this->assertArrayHasKey('component', $privacyfields);
135         $this->assertArrayHasKey('eventtype', $privacyfields);
136         $this->assertArrayHasKey('contexturl', $privacyfields);
137         $this->assertArrayHasKey('contexturlname', $privacyfields);
138         $this->assertArrayHasKey('timeread', $privacyfields);
139         $this->assertArrayHasKey('timecreated', $privacyfields);
140         $this->assertArrayHasKey('customdata', $privacyfields);
141         $this->assertEquals('privacy:metadata:notifications', $notificationstable->get_summary());
142     }
144     /**
145      * Test for provider::export_user_preferences().
146      */
147     public function test_export_user_preferences_no_pref() {
148         $this->resetAfterTest();
150         $user = $this->getDataGenerator()->create_user();
151         provider::export_user_preferences($user->id);
153         $writer = writer::with_context(\context_system::instance());
155         $this->assertFalse($writer->has_any_data());
156     }
158     /**
159      * Test for provider::export_user_preferences().
160      */
161     public function test_export_user_preferences() {
162         global $USER;
164         $this->resetAfterTest();
166         $this->setAdminUser();
168         // Create another user to set a preference for who we won't be exporting.
169         $user = $this->getDataGenerator()->create_user();
171         // Set some message user preferences.
172         set_user_preference('message_provider_moodle_instantmessage_loggedin', 'airnotifier', $USER->id);
173         set_user_preference('message_provider_moodle_instantmessage_loggedoff', 'popup', $USER->id);
174         set_user_preference('message_blocknoncontacts', \core_message\api::MESSAGE_PRIVACY_ONLYCONTACTS, $USER->id);
175         set_user_preference('message_entertosend', true, $USER->id);
176         set_user_preference('message_provider_moodle_instantmessage_loggedoff', 'inbound', $user->id);
178         // Set an unrelated preference.
179         set_user_preference('some_unrelated_preference', 'courses', $USER->id);
181         provider::export_user_preferences($USER->id);
183         $writer = writer::with_context(\context_system::instance());
185         $this->assertTrue($writer->has_any_data());
187         $prefs = (array) $writer->get_user_preferences('core_message');
189         // Check only 3 preferences exist.
190         $this->assertCount(4, $prefs);
191         $this->assertArrayHasKey('message_provider_moodle_instantmessage_loggedin', $prefs);
192         $this->assertArrayHasKey('message_provider_moodle_instantmessage_loggedoff', $prefs);
193         $this->assertArrayHasKey('message_blocknoncontacts', $prefs);
194         $this->assertArrayHasKey('message_entertosend', $prefs);
196         foreach ($prefs as $key => $pref) {
197             if ($key == 'message_provider_moodle_instantmessage_loggedin') {
198                 $this->assertEquals('airnotifier', $pref->value);
199             } else if ($key == 'message_provider_moodle_instantmessage_loggedoff') {
200                 $this->assertEquals('popup', $pref->value);
201             } else {
202                 $this->assertEquals(1, $pref->value);
203             }
204         }
205     }
207     /**
208      * Test for provider::get_contexts_for_userid() when there is no message or notification.
209      */
210     public function test_get_contexts_for_userid_no_data() {
211         $this->resetAfterTest();
213         $user = $this->getDataGenerator()->create_user();
215         $contextlist = provider::get_contexts_for_userid($user->id);
216         $this->assertCount(1, $contextlist);
218         $this->remove_user_self_conversation($user->id);
220         $contextlist = provider::get_contexts_for_userid($user->id);
221         $this->assertEmpty($contextlist);
222     }
224     /**
225      * Test for provider::get_contexts_for_userid() when there is a private message between users.
226      */
227     public function test_get_contexts_for_userid_with_private_messages() {
228         $this->resetAfterTest();
230         $user1 = $this->getDataGenerator()->create_user();
231         $user2 = $this->getDataGenerator()->create_user();
232         $user3 = $this->getDataGenerator()->create_user();
234         // Remove user self-conversations.
235         $this->remove_user_self_conversation($user1->id);
236         $this->remove_user_self_conversation($user2->id);
237         $this->remove_user_self_conversation($user3->id);
239         // Test nothing is found before group conversations is created or message is sent.
240         $contextlist = provider::get_contexts_for_userid($user1->id);
241         $this->assertCount(0, $contextlist);
242         $contextlist = provider::get_contexts_for_userid($user2->id);
243         $this->assertCount(0, $contextlist);
245         // Send some private messages.
246         $pm1id = $this->create_message($user1->id, $user2->id, time() - (9 * DAYSECS));
248         // Test for the sender (user1).
249         $contextlist = provider::get_contexts_for_userid($user1->id);
250         $this->assertCount(1, $contextlist);
251         $contextforuser = $contextlist->current();
252         $this->assertEquals(
253                 \context_user::instance($user1->id)->id,
254                 $contextforuser->id);
256         // Test for the receiver (user2).
257         $contextlist = provider::get_contexts_for_userid($user2->id);
258         $this->assertCount(1, $contextlist);
259         $contextforuser = $contextlist->current();
260         $this->assertEquals(
261                 \context_user::instance($user2->id)->id,
262                 $contextforuser->id);
264         // Test for user3 (no private messages).
265         $contextlist = provider::get_contexts_for_userid($user3->id);
266         $this->assertCount(0, $contextlist);
267     }
269     /**
270      * Test for provider::get_contexts_for_userid() when there is several messages (private and group).
271      */
272     public function test_get_contexts_for_userid_with_messages() {
273         $this->resetAfterTest();
274         $this->setAdminUser();
276         $user1 = $this->getDataGenerator()->create_user();
277         $user2 = $this->getDataGenerator()->create_user();
278         $user3 = $this->getDataGenerator()->create_user();
279         $user4 = $this->getDataGenerator()->create_user();
281         // Remove user self-conversations.
282         $this->remove_user_self_conversation($user1->id);
283         $this->remove_user_self_conversation($user2->id);
284         $this->remove_user_self_conversation($user3->id);
285         $this->remove_user_self_conversation($user4->id);
287         // Test nothing is found before group conversations is created or message is sent.
288         $contextlist = provider::get_contexts_for_userid($user1->id);
289         $this->assertCount(0, $contextlist);
290         $contextlist = provider::get_contexts_for_userid($user2->id);
291         $this->assertCount(0, $contextlist);
293         // Create course.
294         $course1 = $this->getDataGenerator()->create_course();
295         $coursecontext1 = context_course::instance($course1->id);
297         // Enrol users to courses.
298         $this->getDataGenerator()->enrol_user($user1->id, $course1->id);
299         $this->getDataGenerator()->enrol_user($user2->id, $course1->id);
300         $this->getDataGenerator()->enrol_user($user3->id, $course1->id);
302         // Create groups (only one with enablemessaging = 1).
303         $group1a = $this->getDataGenerator()->create_group(array('courseid' => $course1->id, 'enablemessaging' => 1));
305         // Add users to groups.
306         $this->getDataGenerator()->create_group_member(array('groupid' => $group1a->id, 'userid' => $user1->id));
307         $this->getDataGenerator()->create_group_member(array('groupid' => $group1a->id, 'userid' => $user2->id));
308         $this->getDataGenerator()->create_group_member(array('groupid' => $group1a->id, 'userid' => $user3->id));
310         // Get conversation.
311         $component = 'core_group';
312         $itemtype = 'groups';
313         $conversation1 = \core_message\api::get_conversation_by_area(
314             $component,
315             $itemtype,
316             $group1a->id,
317             $coursecontext1->id
318         );
320         // Send some messages to the group conversation.
321         $now = time();
322         $m1id = testhelper::send_fake_message_to_conversation($user1, $conversation1->id, 'Message 1', $now + 1);
323         $m2id = testhelper::send_fake_message_to_conversation($user1, $conversation1->id, 'Message 2', $now + 2);
324         $m3id = testhelper::send_fake_message_to_conversation($user2, $conversation1->id, 'Message 3', $now + 3);
326         // Test for user1 (although is member of the conversation, hasn't any private message).
327         $contextlist = provider::get_contexts_for_userid($user1->id);
328         $this->assertCount(0, $contextlist);
330         // Test for user2 (although is member of the conversation, hasn't any private message).
331         $contextlist = provider::get_contexts_for_userid($user2->id);
332         $this->assertCount(0, $contextlist);
334         // Test for user3 (although is member of the conversation, hasn't any private message).
335         $contextlist = provider::get_contexts_for_userid($user3->id);
336         $this->assertCount(0, $contextlist);
338         // Test for user4 (doesn't belong to the conversation).
339         $contextlist = provider::get_contexts_for_userid($user4->id);
340         $this->assertCount(0, $contextlist);
342         // Send some private messages.
343         $pm1id = $this->create_message($user1->id, $user2->id, time() - (9 * DAYSECS));
345         // Test user1 now has the user context because of the private message.
346         $contextlist = provider::get_contexts_for_userid($user1->id);
347         $this->assertCount(1, $contextlist);
348         $contextforuser = $contextlist->current();
349         $this->assertEquals(
350                 \context_user::instance($user1->id)->id,
351                 $contextforuser->id);
353         // Test user2 now has the user context because of the private message.
354         $contextlist = provider::get_contexts_for_userid($user2->id);
355         $this->assertCount(1, $contextlist);
356         $contextforuser = $contextlist->current();
357         $this->assertEquals(
358                 \context_user::instance($user2->id)->id,
359                 $contextforuser->id);
361         // Test for user3 (although is member of the conversation, hasn't still any private message).
362         $contextlist = provider::get_contexts_for_userid($user3->id);
363         $this->assertCount(0, $contextlist);
365         // Test for user4 (doesn't belong to the conversation and hasn't any private message).
366         $contextlist = provider::get_contexts_for_userid($user4->id);
367         $this->assertCount(0, $contextlist);
368     }
370     /**
371      * Test for provider::get_contexts_for_userid() when there is a notification between users.
372      */
373     public function test_get_contexts_for_userid_with_notification() {
374         $this->resetAfterTest();
376         $user1 = $this->getDataGenerator()->create_user();
377         $user2 = $this->getDataGenerator()->create_user();
379         // Remove user self-conversations.
380         $this->remove_user_self_conversation($user1->id);
381         $this->remove_user_self_conversation($user2->id);
383         // Test nothing is found before notification is created.
384         $contextlist = provider::get_contexts_for_userid($user1->id);
385         $this->assertCount(0, $contextlist);
386         $contextlist = provider::get_contexts_for_userid($user2->id);
387         $this->assertCount(0, $contextlist);
389         $this->create_notification($user1->id, $user2->id, time() - (9 * DAYSECS));
391         // Test for the sender.
392         $contextlist = provider::get_contexts_for_userid($user1->id);
393         $this->assertCount(1, $contextlist);
394         $contextforuser = $contextlist->current();
395         $this->assertEquals(
396                 context_user::instance($user1->id)->id,
397                 $contextforuser->id);
399         // Test for the receiver.
400         $contextlist = provider::get_contexts_for_userid($user2->id);
401         $this->assertCount(1, $contextlist);
402         $contextforuser = $contextlist->current();
403         $this->assertEquals(
404                 context_user::instance($user2->id)->id,
405                 $contextforuser->id);
406     }
408     /**
409      * Test for provider::get_contexts_for_userid() when a users has a contact.
410      */
411     public function test_get_contexts_for_userid_with_contact() {
412         $this->resetAfterTest();
414         $user1 = $this->getDataGenerator()->create_user();
415         $user2 = $this->getDataGenerator()->create_user();
417         // Remove user self-conversations.
418         $this->remove_user_self_conversation($user1->id);
419         $this->remove_user_self_conversation($user2->id);
421         // Test nothing is found before contact is created.
422         $contextlist = provider::get_contexts_for_userid($user1->id);
423         $this->assertCount(0, $contextlist);
424         $contextlist = provider::get_contexts_for_userid($user2->id);
425         $this->assertCount(0, $contextlist);
427         \core_message\api::add_contact($user1->id, $user2->id);
429         // Test for the user adding the contact.
430         $contextlist = provider::get_contexts_for_userid($user1->id);
431         $this->assertCount(1, $contextlist);
432         $contextforuser = $contextlist->current();
433         $this->assertEquals(
434                 context_user::instance($user1->id)->id,
435                 $contextforuser->id);
437         // Test for the user who is the contact.
438         $contextlist = provider::get_contexts_for_userid($user2->id);
439         $this->assertCount(1, $contextlist);
440         $contextforuser = $contextlist->current();
441         $this->assertEquals(
442                 context_user::instance($user2->id)->id,
443                 $contextforuser->id);
444     }
446     /**
447      * Test for provider::get_contexts_for_userid() when a user makes a contact request.
448      */
449     public function test_get_contexts_for_userid_with_contact_request() {
450         $this->resetAfterTest();
452         $user1 = $this->getDataGenerator()->create_user();
453         $user2 = $this->getDataGenerator()->create_user();
455         // Remove user self-conversations.
456         $this->remove_user_self_conversation($user1->id);
457         $this->remove_user_self_conversation($user2->id);
459         // Test nothing is found before request is created.
460         $contextlist = provider::get_contexts_for_userid($user1->id);
461         $this->assertCount(0, $contextlist);
462         $contextlist = provider::get_contexts_for_userid($user2->id);
463         $this->assertCount(0, $contextlist);
465         \core_message\api::create_contact_request($user1->id, $user2->id);
467         // Test for the user requesting the contact.
468         $contextlist = provider::get_contexts_for_userid($user1->id);
469         $this->assertCount(1, $contextlist);
470         $contextforuser = $contextlist->current();
471         $this->assertEquals(
472                 context_user::instance($user1->id)->id,
473                 $contextforuser->id);
475         // Test for the user receiving the contact request.
476         $contextlist = provider::get_contexts_for_userid($user2->id);
477         $this->assertCount(1, $contextlist);
478         $contextforuser = $contextlist->current();
479         $this->assertEquals(
480                 context_user::instance($user2->id)->id,
481                 $contextforuser->id);
482     }
484     /**
485      * Test for provider::get_contexts_for_userid() when a user is blocked.
486      */
487     public function test_get_contexts_for_userid_with_blocked_contact() {
488         $this->resetAfterTest();
490         $user1 = $this->getDataGenerator()->create_user();
491         $user2 = $this->getDataGenerator()->create_user();
493         // Remove user self-conversations.
494         $this->remove_user_self_conversation($user1->id);
495         $this->remove_user_self_conversation($user2->id);
497         // Test nothing is found before user is blocked.
498         $contextlist = provider::get_contexts_for_userid($user1->id);
499         $this->assertCount(0, $contextlist);
500         $contextlist = provider::get_contexts_for_userid($user2->id);
501         $this->assertCount(0, $contextlist);
503         \core_message\api::block_user($user1->id, $user2->id);
505         // Test for the blocking user.
506         $contextlist = provider::get_contexts_for_userid($user1->id);
507         $this->assertCount(1, $contextlist);
508         $contextforuser = $contextlist->current();
509         $this->assertEquals(
510                 context_user::instance($user1->id)->id,
511                 $contextforuser->id);
513         // Test for the user who is blocked.
514         $contextlist = provider::get_contexts_for_userid($user2->id);
515         $this->assertCount(1, $contextlist);
516         $contextforuser = $contextlist->current();
517         $this->assertEquals(
518                 context_user::instance($user2->id)->id,
519                 $contextforuser->id);
520     }
522     /**
523      * Test for provider::export_user_data().
524      */
525     public function test_export_for_context_with_contacts() {
526         $this->resetAfterTest();
528         // Create users to test with.
529         $user1 = $this->getDataGenerator()->create_user();
530         $user2 = $this->getDataGenerator()->create_user();
531         $user3 = $this->getDataGenerator()->create_user();
532         $user4 = $this->getDataGenerator()->create_user();
534         // Remove user self-conversations.
535         $this->remove_user_self_conversation($user1->id);
536         $this->remove_user_self_conversation($user2->id);
537         $this->remove_user_self_conversation($user3->id);
538         $this->remove_user_self_conversation($user4->id);
540         \core_message\api::add_contact($user1->id, $user2->id);
541         \core_message\api::add_contact($user1->id, $user3->id);
542         \core_message\api::add_contact($user1->id, $user4->id);
544         $user1context = context_user::instance($user1->id);
546         $this->export_context_data_for_user($user1->id, $user1context, 'core_message');
548         $writer = writer::with_context($user1context);
550         $contacts = (array) $writer->get_data([get_string('contacts', 'core_message')]);
551         usort($contacts, ['static', 'sort_contacts']);
553         $this->assertCount(3, $contacts);
555         $contact1 = array_shift($contacts);
556         $this->assertEquals($user2->id, $contact1->contact);
558         $contact2 = array_shift($contacts);
559         $this->assertEquals($user3->id, $contact2->contact);
561         $contact3 = array_shift($contacts);
562         $this->assertEquals($user4->id, $contact3->contact);
563     }
565     /**
566      * Test for provider::export_user_data().
567      */
568     public function test_export_for_context_with_contact_requests() {
569         $this->resetAfterTest();
571         // Create users to test with.
572         $user1 = $this->getDataGenerator()->create_user();
573         $user2 = $this->getDataGenerator()->create_user();
574         $user3 = $this->getDataGenerator()->create_user();
575         $user4 = $this->getDataGenerator()->create_user();
577         // Remove user self-conversations.
578         $this->remove_user_self_conversation($user1->id);
579         $this->remove_user_self_conversation($user2->id);
580         $this->remove_user_self_conversation($user3->id);
581         $this->remove_user_self_conversation($user4->id);
583         \core_message\api::create_contact_request($user1->id, $user2->id);
584         \core_message\api::create_contact_request($user3->id, $user1->id);
585         \core_message\api::create_contact_request($user1->id, $user4->id);
587         $user1context = context_user::instance($user1->id);
589         $this->export_context_data_for_user($user1->id, $user1context, 'core_message');
591         $writer = writer::with_context($user1context);
593         $contactrequests = (array) $writer->get_data([get_string('contactrequests', 'core_message')]);
595         $this->assertCount(3, $contactrequests);
597         $contactrequest1 = array_shift($contactrequests);
598         $this->assertEquals($user2->id, $contactrequest1->contactrequest);
599         $this->assertEquals(get_string('yes'), $contactrequest1->maderequest);
601         $contactrequest2 = array_shift($contactrequests);
602         $this->assertEquals($user3->id, $contactrequest2->contactrequest);
603         $this->assertEquals(get_string('no'), $contactrequest2->maderequest);
605         $contactrequest3 = array_shift($contactrequests);
606         $this->assertEquals($user4->id, $contactrequest3->contactrequest);
607         $this->assertEquals(get_string('yes'), $contactrequest3->maderequest);
608     }
610     /**
611      * Test for provider::export_user_data().
612      */
613     public function test_export_for_context_with_blocked_users() {
614         $this->resetAfterTest();
616         // Create users to test with.
617         $user1 = $this->getDataGenerator()->create_user();
618         $user2 = $this->getDataGenerator()->create_user();
619         $user3 = $this->getDataGenerator()->create_user();
620         $user4 = $this->getDataGenerator()->create_user();
622         // Remove user self-conversations.
623         $this->remove_user_self_conversation($user1->id);
624         $this->remove_user_self_conversation($user2->id);
625         $this->remove_user_self_conversation($user3->id);
626         $this->remove_user_self_conversation($user4->id);
628         \core_message\api::block_user($user1->id, $user2->id);
629         \core_message\api::block_user($user1->id, $user3->id);
630         \core_message\api::block_user($user1->id, $user4->id);
632         $user1context = context_user::instance($user1->id);
634         $this->export_context_data_for_user($user1->id, $user1context, 'core_message');
636         $writer = writer::with_context($user1context);
638         $blockedusers = (array) $writer->get_data([get_string('blockedusers', 'core_message')]);
640         $this->assertCount(3, $blockedusers);
642         $blockeduser1 = array_shift($blockedusers);
643         $this->assertEquals($user2->id, $blockeduser1->blockeduser);
645         $blockeduser2 = array_shift($blockedusers);
646         $this->assertEquals($user3->id, $blockeduser2->blockeduser);
648         $blockeduser3 = array_shift($blockedusers);
649         $this->assertEquals($user4->id, $blockeduser3->blockeduser);
650     }
652     /**
653      * Test for provider::export_user_data().
654      */
655     public function test_export_for_context_with_private_messages() {
656         global $DB;
658         $this->resetAfterTest();
660         // Create users to test with.
661         $user1 = $this->getDataGenerator()->create_user();
662         $user2 = $this->getDataGenerator()->create_user();
663         $user3 = $this->getDataGenerator()->create_user();
665         // Remove user self-conversations.
666         $this->remove_user_self_conversation($user1->id);
667         $this->remove_user_self_conversation($user2->id);
668         $this->remove_user_self_conversation($user3->id);
670         $now = time();
672         // Send messages from user 1 to user 2.
673         $m1 = $this->create_message($user1->id, $user2->id, $now - (9 * DAYSECS), true);
674         $m2 = $this->create_message($user2->id, $user1->id, $now - (8 * DAYSECS));
675         $m3 = $this->create_message($user1->id, $user2->id, $now - (7 * DAYSECS));
677         // Send messages from user 3 to user 1.
678         $m4 = $this->create_message($user3->id, $user1->id, $now - (6 * DAYSECS), true);
679         $m5 = $this->create_message($user1->id, $user3->id, $now - (5 * DAYSECS));
680         $m6 = $this->create_message($user3->id, $user1->id, $now - (4 * DAYSECS));
682         // Send messages from user 3 to user 2 - these should not be included in the export.
683         $m7 = $this->create_message($user3->id, $user2->id, $now - (3 * DAYSECS), true);
684         $m8 = $this->create_message($user2->id, $user3->id, $now - (2 * DAYSECS));
685         $m9 = $this->create_message($user3->id, $user2->id, $now - (1 * DAYSECS));
687         // Mark message 2 and 5 as deleted.
688         \core_message\api::delete_message($user1->id, $m2);
689         \core_message\api::delete_message($user1->id, $m5);
691         $user1context = context_user::instance($user1->id);
693         $this->export_context_data_for_user($user1->id, $user1context, 'core_message');
695         $writer = writer::with_context($user1context);
697         $this->assertTrue($writer->has_any_data());
699         // Confirm the messages with user 2 are correct.
700         $messages = (array) $writer->get_data([get_string('messages', 'core_message'), $user2->id]);
701         $this->assertCount(3, $messages);
703         $dbm1 = $DB->get_record('messages', ['id' => $m1]);
704         $dbm2 = $DB->get_record('messages', ['id' => $m2]);
705         $dbm3 = $DB->get_record('messages', ['id' => $m3]);
707         usort($messages, ['static', 'sort_messages']);
708         $m1 = array_shift($messages);
709         $m2 = array_shift($messages);
710         $m3 = array_shift($messages);
712         $this->assertEquals(get_string('yes'), $m1->issender);
713         $this->assertEquals(message_format_message_text($dbm1), $m1->message);
714         $this->assertEquals(transform::datetime($now - (9 * DAYSECS)), $m1->timecreated);
715         $this->assertEquals('-', $m1->timeread);
716         $this->assertArrayNotHasKey('timedeleted', (array) $m1);
718         $this->assertEquals(get_string('no'), $m2->issender);
719         $this->assertEquals(message_format_message_text($dbm2), $m2->message);
720         $this->assertEquals(transform::datetime($now - (8 * DAYSECS)), $m2->timecreated);
721         $this->assertEquals('-', $m2->timeread);
722         $this->assertArrayHasKey('timedeleted', (array) $m2);
724         $this->assertEquals(get_string('yes'), $m3->issender);
725         $this->assertEquals(message_format_message_text($dbm3), $m3->message);
726         $this->assertEquals(transform::datetime($now - (7 * DAYSECS)), $m3->timecreated);
727         $this->assertEquals('-', $m3->timeread);
729         // Confirm the messages with user 3 are correct.
730         $messages = (array) $writer->get_data([get_string('messages', 'core_message'), $user3->id]);
731         $this->assertCount(3, $messages);
733         $dbm4 = $DB->get_record('messages', ['id' => $m4]);
734         $dbm5 = $DB->get_record('messages', ['id' => $m5]);
735         $dbm6 = $DB->get_record('messages', ['id' => $m6]);
737         usort($messages, ['static', 'sort_messages']);
738         $m4 = array_shift($messages);
739         $m5 = array_shift($messages);
740         $m6 = array_shift($messages);
742         $this->assertEquals(get_string('no'), $m4->issender);
743         $this->assertEquals(message_format_message_text($dbm4), $m4->message);
744         $this->assertEquals(transform::datetime($now - (6 * DAYSECS)), $m4->timecreated);
745         $this->assertNotEquals('-', $m4->timeread);
746         $this->assertArrayNotHasKey('timedeleted', (array) $m4);
748         $this->assertEquals(get_string('yes'), $m5->issender);
749         $this->assertEquals(message_format_message_text($dbm5), $m5->message);
750         $this->assertEquals(transform::datetime($now - (5 * DAYSECS)), $m5->timecreated);
751         $this->assertEquals('-', $m5->timeread);
752         $this->assertArrayHasKey('timedeleted', (array) $m5);
754         $this->assertEquals(get_string('no'), $m6->issender);
755         $this->assertEquals(message_format_message_text($dbm6), $m6->message);
756         $this->assertEquals(transform::datetime($now - (4 * DAYSECS)), $m6->timecreated);
757         $this->assertEquals('-', $m6->timeread);
758     }
760     /**
761      * Test for provider::export_user_data().
762      */
763     public function test_export_for_context_with_messages() {
764         global $DB;
766         $this->resetAfterTest();
767         $this->setAdminUser();
768         $now = time();
769         $systemcontext = \context_system::instance();
771         // Create users to test with.
772         $user1 = $this->getDataGenerator()->create_user();
773         $user2 = $this->getDataGenerator()->create_user();
774         $user3 = $this->getDataGenerator()->create_user();
775         $user1context = \context_user::instance($user1->id);
777         // Remove user self-conversations.
778         $this->remove_user_self_conversation($user1->id);
779         $this->remove_user_self_conversation($user2->id);
780         $this->remove_user_self_conversation($user3->id);
782         $course1 = $this->getDataGenerator()->create_course();
783         $course2 = $this->getDataGenerator()->create_course();
784         $coursecontext1 = \context_course::instance($course1->id);
785         $coursecontext2 = \context_course::instance($course2->id);
787         // Enrol users to courses.
788         $this->getDataGenerator()->enrol_user($user1->id, $course1->id);
789         $this->getDataGenerator()->enrol_user($user2->id, $course1->id);
790         $this->getDataGenerator()->enrol_user($user3->id, $course1->id);
791         $this->getDataGenerator()->enrol_user($user1->id, $course2->id);
793         // Create course groups with group messaging enabled.
794         $group1a = $this->getDataGenerator()->create_group(array('courseid' => $course1->id, 'enablemessaging' => 1));
795         $group2a = $this->getDataGenerator()->create_group(array('courseid' => $course2->id, 'enablemessaging' => 1));
797         // Add users to groups.
798         $this->getDataGenerator()->create_group_member(array('groupid' => $group1a->id, 'userid' => $user1->id));
799         $this->getDataGenerator()->create_group_member(array('groupid' => $group1a->id, 'userid' => $user2->id));
800         $this->getDataGenerator()->create_group_member(array('groupid' => $group1a->id, 'userid' => $user3->id));
801         $this->getDataGenerator()->create_group_member(array('groupid' => $group2a->id, 'userid' => $user1->id));
803         // Get conversation.
804         $component = 'core_group';
805         $itemtype = 'groups';
806         $conversation = \core_message\api::get_conversation_by_area(
807             $component,
808             $itemtype,
809             $group1a->id,
810             $coursecontext1->id
811         );
813         // Send some private messages between user 1 and user 2.
814         $pm1id = $this->create_message($user1->id, $user2->id, $now);
816         $dbpm1 = $DB->get_record('messages', ['id' => $pm1id]);
818         // Send some messages to the conversation.
819         $m1 = testhelper::send_fake_message_to_conversation($user1, $conversation->id, 'Message 1', $now + 1);
820         $m2 = testhelper::send_fake_message_to_conversation($user1, $conversation->id, 'Message 2', $now + 2);
821         $m3 = testhelper::send_fake_message_to_conversation($user2, $conversation->id, 'Message 3', $now + 3);
823         $dbm1 = $DB->get_record('messages', ['id' => $m1]);
824         $dbm2 = $DB->get_record('messages', ['id' => $m2]);
825         $dbm3 = $DB->get_record('messages', ['id' => $m3]);
827         // Mark as read and delete some messages.
828         \core_message\api::mark_message_as_read($user2->id, $dbm1);
829         \core_message\api::delete_message($user1->id, $m2);
831         // Confirm the user1 has no data in any course context because private messages are related to user context.
832         $this->export_context_data_for_user($user1->id, $coursecontext2, 'core_message');
834         // Check that system context hasn't been exported.
835         $writer = writer::with_context($systemcontext);
836         $this->assertFalse($writer->has_any_data());
838         // Check that course1 context hasn't been exported.
839         $writer = writer::with_context($coursecontext1);
840         $this->assertFalse($writer->has_any_data());
842         // Check that course2 context has been exported and contains data.
843         $writer = writer::with_context($coursecontext2);
844         $this->assertFalse($writer->has_any_data());
846         // Confirm the user1 has only private messages in the user context.
847         $this->export_context_data_for_user($user1->id, $user1context, 'core_message');
848         $writer = writer::with_context($user1context);
849         $this->assertTrue($writer->has_any_data());
851         // Confirm the messages with user 2 are correct.
852         $messages = (array) $writer->get_data([get_string('messages', 'core_message'), $user2->id]);
853         $this->assertCount(1, $messages);
854         $m1 = reset($messages);
856         $this->assertEquals(get_string('yes'), $m1->issender);
857         $this->assertEquals(message_format_message_text($dbpm1), $m1->message);
858         $this->assertEquals(transform::datetime($now), $m1->timecreated);
859         $this->assertEquals('-', $m1->timeread);
860         $this->assertArrayNotHasKey('timedeleted', (array) $m1);
862         // Confirm the messages with user 3 are correct.
863         $messages = (array) $writer->get_data([get_string('messages', 'core_message'), fullname($user3)]);
864         $this->assertCount(0, $messages);
865     }
867     /**
868      * Test for provider::export_user_data().
869      */
870     public function test_export_for_context_with_notifications() {
871         $this->resetAfterTest();
873         // Create users to test with.
874         $user1 = $this->getDataGenerator()->create_user();
875         $user2 = $this->getDataGenerator()->create_user();
876         $user3 = $this->getDataGenerator()->create_user();
878         // Remove user self-conversations.
879         $this->remove_user_self_conversation($user1->id);
880         $this->remove_user_self_conversation($user2->id);
881         $this->remove_user_self_conversation($user3->id);
883         $now = time();
884         $timeread = $now - DAYSECS;
886         // Send notifications from user 1 to user 2.
887         $this->create_notification($user1->id, $user2->id, $now + (9 * DAYSECS), $timeread);
888         $this->create_notification($user2->id, $user1->id, $now + (8 * DAYSECS));
889         $this->create_notification($user1->id, $user2->id, $now + (7 * DAYSECS));
891         // Send notifications from user 3 to user 1.
892         $this->create_notification($user3->id, $user1->id, $now + (6 * DAYSECS), $timeread);
893         $this->create_notification($user1->id, $user3->id, $now + (5 * DAYSECS));
894         $this->create_notification($user3->id, $user1->id, $now + (4 * DAYSECS));
896         // Send notifications from user 3 to user 2 - should not be part of the export.
897         $this->create_notification($user3->id, $user2->id, $now + (3 * DAYSECS), $timeread);
898         $this->create_notification($user2->id, $user3->id, $now + (2 * DAYSECS));
899         $this->create_notification($user3->id, $user2->id, $now + (1 * DAYSECS));
901         $user1context = context_user::instance($user1->id);
903         $this->export_context_data_for_user($user1->id, $user1context, 'core_message');
905         $writer = writer::with_context($user1context);
907         $this->assertTrue($writer->has_any_data());
909         // Confirm the notifications.
910         $notifications = (array) $writer->get_data([get_string('notifications', 'core_message')]);
912         $this->assertCount(6, $notifications);
913     }
915     /**
916      * Test for provider::delete_data_for_all_users_in_context().
917      */
918     public function test_delete_data_for_all_users_in_context() {
919         global $DB;
921         $this->resetAfterTest();
923         // Create users to test with.
924         $user1 = $this->getDataGenerator()->create_user();
925         $user2 = $this->getDataGenerator()->create_user();
926         $user3 = $this->getDataGenerator()->create_user();
927         $user4 = $this->getDataGenerator()->create_user();
928         $user5 = $this->getDataGenerator()->create_user();
930         $now = time();
931         $timeread = $now - DAYSECS;
933         $user1context = context_user::instance($user1->id);
935         // Create contacts.
936         \core_message\api::add_contact($user1->id, $user2->id);
937         \core_message\api::add_contact($user2->id, $user3->id);
939         // Create contact requests.
940         \core_message\api::create_contact_request($user1->id, $user3->id);
941         \core_message\api::create_contact_request($user2->id, $user4->id);
943         // Block a user.
944         \core_message\api::block_user($user1->id, $user3->id);
945         \core_message\api::block_user($user3->id, $user4->id);
947         // Create messages.
948         $m1 = $this->create_message($user1->id, $user2->id, $now + (9 * DAYSECS), true);
949         $m2 = $this->create_message($user2->id, $user1->id, $now + (8 * DAYSECS));
950         $m3 = $this->create_message($user2->id, $user3->id, $now + (7 * DAYSECS));
952         // Create notifications.
953         $n1 = $this->create_notification($user1->id, $user2->id, $now + (9 * DAYSECS), $timeread);
954         $n2 = $this->create_notification($user2->id, $user1->id, $now + (8 * DAYSECS));
955         $n3 = $this->create_notification($user2->id, $user3->id, $now + (7 * DAYSECS));
957         // Delete one of the messages.
958         \core_message\api::delete_message($user1->id, $m2);
960         // There should be 2 contacts.
961         $this->assertEquals(2, $DB->count_records('message_contacts'));
963         // There should be 2 contact requests.
964         $this->assertEquals(2, $DB->count_records('message_contact_requests'));
966         // There should be 2 blocked users.
967         $this->assertEquals(2, $DB->count_records('message_users_blocked'));
969         // There should be 3 messages.
970         $this->assertEquals(3, $DB->count_records('messages'));
972         // There should be 2 user actions - one for reading the message, one for deleting.
973         $this->assertEquals(2, $DB->count_records('message_user_actions'));
975         // There should be 4 conversation members + 5 self-conversations.
976         $this->assertEquals(9, $DB->count_records('message_conversation_members'));
978         // There should be 5 notifications (3 from create_notification and 2 from create_contact_request).
979         $this->assertEquals(5, $DB->count_records('notifications'));
981         provider::delete_data_for_all_users_in_context($user1context);
983         // Confirm there is only 1 contact left.
984         $this->assertEquals(1, $DB->count_records('message_contacts'));
985         // And it is not related to user1.
986         $this->assertEquals(0,
987                 $DB->count_records_select('message_contacts', 'userid = ? OR contactid = ?', [$user1->id, $user1->id]));
989         // Confirm there is only 1 contact request left.
990         $this->assertEquals(1, $DB->count_records('message_contact_requests'));
991         // And it is not related to user1.
992         $this->assertEquals(0,
993                 $DB->count_records_select('message_contact_requests', 'userid = ? OR requesteduserid = ?',
994                         [$user1->id, $user1->id]));
996         // Confirm there is only 1 blocked user left.
997         $this->assertEquals(1, $DB->count_records('message_users_blocked'));
998         // And it is not related to user1.
999         $this->assertEquals(0,
1000                 $DB->count_records_select('message_users_blocked', 'userid = ? OR blockeduserid = ?', [$user1->id, $user1->id]));
1002         // Confirm there are only 2 messages left.
1003         $this->assertEquals(2, $DB->count_records('messages'));
1004         // And none of them are from user1.
1005         $this->assertEquals(0, $DB->count_records('messages', ['useridfrom' => $user1->id]));
1007         // Confirm there is 0 user action left.
1008         $this->assertEquals(0, $DB->count_records('message_user_actions'));
1009         // And it is not for user1.
1010         $this->assertEquals(0, $DB->count_records('message_user_actions', ['userid' => $user1->id]));
1012         // Confirm there are only 3 conversation members left + 4 self-conversations.
1013         $this->assertEquals(7, $DB->count_records('message_conversation_members'));
1014         // And user1 is not in any conversation.
1015         $this->assertEquals(0, $DB->count_records('message_conversation_members', ['userid' => $user1->id]));
1017         // Confirm there are only 2 notifications.
1018         $this->assertEquals(2, $DB->count_records('notifications'));
1019         // And it is not related to user1.
1020         $this->assertEquals(0,
1021                 $DB->count_records_select('notifications', 'useridfrom = ? OR useridto = ? ', [$user1->id, $user1->id]));
1023         // Delete user self-conversations.
1024         $this->remove_user_self_conversation($user2->id);
1025         $this->remove_user_self_conversation($user3->id);
1026         $this->remove_user_self_conversation($user4->id);
1027         $this->remove_user_self_conversation($user5->id);
1029         // Confirm there are only 3 conversation members left.
1030         $this->assertEquals(3, $DB->count_records('message_conversation_members'));
1031         // And user1 is not in any conversation.
1032         $this->assertEquals(0, $DB->count_records('message_conversation_members', ['userid' => $user1->id]));
1034     }
1036     /**
1037      * Test for provider::delete_data_for_user().
1038      */
1039     public function test_delete_data_for_user() {
1040         global $DB;
1042         $this->resetAfterTest();
1044         // Create users to test with.
1045         $user1 = $this->getDataGenerator()->create_user();
1046         $user2 = $this->getDataGenerator()->create_user();
1047         $user3 = $this->getDataGenerator()->create_user();
1048         $user4 = $this->getDataGenerator()->create_user();
1049         $user5 = $this->getDataGenerator()->create_user();
1050         $user6 = $this->getDataGenerator()->create_user();
1052         $now = time();
1053         $timeread = $now - DAYSECS;
1055         // Create contacts.
1056         \core_message\api::add_contact($user1->id, $user2->id);
1057         \core_message\api::add_contact($user2->id, $user3->id);
1059         // Create contact requests.
1060         \core_message\api::create_contact_request($user1->id, $user3->id);
1061         \core_message\api::create_contact_request($user2->id, $user4->id);
1063         // Block users.
1064         \core_message\api::block_user($user1->id, $user5->id);
1065         \core_message\api::block_user($user2->id, $user6->id);
1067         // Create messages.
1068         $m1 = $this->create_message($user1->id, $user2->id, $now + (9 * DAYSECS), $timeread);
1069         $m2 = $this->create_message($user2->id, $user1->id, $now + (8 * DAYSECS));
1071         // Create notifications.
1072         $n1 = $this->create_notification($user1->id, $user2->id, $now + (9 * DAYSECS), $timeread);
1073         $n2 = $this->create_notification($user2->id, $user1->id, $now + (8 * DAYSECS));
1074         $n2 = $this->create_notification($user2->id, $user3->id, $now + (8 * DAYSECS));
1076         // Delete one of the messages.
1077         \core_message\api::delete_message($user1->id, $m2);
1079         // There should be 2 contacts.
1080         $this->assertEquals(2, $DB->count_records('message_contacts'));
1082         // There should be 1 contact request.
1083         $this->assertEquals(2, $DB->count_records('message_contact_requests'));
1085         // There should be 1 blocked user.
1086         $this->assertEquals(2, $DB->count_records('message_users_blocked'));
1088         // There should be two messages.
1089         $this->assertEquals(2, $DB->count_records('messages'));
1091         // There should be two user actions - one for reading the message, one for deleting.
1092         $this->assertEquals(2, $DB->count_records('message_user_actions'));
1094         // There should be two conversation members + 6 self-conversations.
1095         $this->assertEquals(8, $DB->count_records('message_conversation_members'));
1097         // There should be 5 notifications (3 from create_notification and 2 from create_contact_request).
1098         $this->assertEquals(5, $DB->count_records('notifications'));
1100         $user1context = context_user::instance($user1->id);
1101         $contextlist = new \core_privacy\local\request\approved_contextlist($user1, 'core_message',
1102             [$user1context->id]);
1103         provider::delete_data_for_user($contextlist);
1105         // Confirm the user 2 data still exists.
1106         $contacts = $DB->get_records('message_contacts');
1107         $contactrequests = $DB->get_records('message_contact_requests');
1108         $blockedusers = $DB->get_records('message_users_blocked');
1109         $messages = $DB->get_records('messages');
1110         $muas = $DB->get_records('message_user_actions');
1111         $mcms = $DB->get_records('message_conversation_members');
1112         $notifications = $DB->get_records('notifications');
1114         $this->assertCount(1, $contacts);
1115         $contact = reset($contacts);
1116         $this->assertEquals($user2->id, $contact->userid);
1117         $this->assertEquals($user3->id, $contact->contactid);
1119         $this->assertCount(1, $contactrequests);
1120         $contactrequest = reset($contactrequests);
1121         $this->assertEquals($user2->id, $contactrequest->userid);
1122         $this->assertEquals($user4->id, $contactrequest->requesteduserid);
1124         $this->assertCount(1, $blockedusers);
1125         $blockeduser = reset($blockedusers);
1126         $this->assertEquals($user2->id, $blockeduser->userid);
1127         $this->assertEquals($user6->id, $blockeduser->blockeduserid);
1129         $this->assertCount(1, $messages);
1130         $message = reset($messages);
1131         $this->assertEquals($m2, $message->id);
1133         $this->assertCount(0, $muas);
1135         $this->assertCount(6, $mcms);
1136         $mcm = reset($mcms);
1137         $this->assertEquals($user2->id, $mcm->userid);
1139         $this->assertCount(2, $notifications);
1140         ksort($notifications);
1142         $notification = array_pop($notifications);
1143         $this->assertEquals($user2->id, $notification->useridfrom);
1144         $this->assertEquals($user3->id, $notification->useridto);
1145     }
1147     /**
1148      * Test for provider::get_users_in_context() when there is no message or notification.
1149      */
1150     public function test_get_users_in_context_no_data() {
1151         $this->resetAfterTest();
1153         $user = $this->getDataGenerator()->create_user();
1154         $usercontext = context_user::instance($user->id);
1155         $this->remove_user_self_conversation($user->id);
1157         $userlist = new \core_privacy\local\request\userlist($usercontext, 'core_message');
1158         \core_message\privacy\provider::get_users_in_context($userlist);
1160         $this->assertEmpty($userlist->get_userids());
1161     }
1163     /**
1164      * Test for provider::get_users_in_context() when there is a message between users.
1165      */
1166     public function test_get_users_in_context_with_message() {
1167         $this->resetAfterTest();
1169         $user1 = $this->getDataGenerator()->create_user();
1170         $user2 = $this->getDataGenerator()->create_user();
1172         $user1context = context_user::instance($user1->id);
1173         $user2context = context_user::instance($user2->id);
1175         // Delete user self-conversations.
1176         $this->remove_user_self_conversation($user1->id);
1177         $this->remove_user_self_conversation($user2->id);
1179         // Test nothing is found before message is sent.
1180         $userlist = new \core_privacy\local\request\userlist($user1context, 'core_message');
1181         \core_message\privacy\provider::get_users_in_context($userlist);
1182         $this->assertCount(0, $userlist);
1183         $userlist = new \core_privacy\local\request\userlist($user2context, 'core_message');
1184         \core_message\privacy\provider::get_users_in_context($userlist);
1185         $this->assertCount(0, $userlist);
1187         $this->create_message($user1->id, $user2->id, time() - (9 * DAYSECS));
1189         // Test for the sender.
1190         $userlist = new \core_privacy\local\request\userlist($user1context, 'core_message');
1191         \core_message\privacy\provider::get_users_in_context($userlist);
1192         $this->assertCount(1, $userlist);
1193         $userincontext = $userlist->current();
1194         $this->assertEquals($user1->id, $userincontext->id);
1196         // Test for the receiver.
1197         $userlist = new \core_privacy\local\request\userlist($user2context, 'core_message');
1198         \core_message\privacy\provider::get_users_in_context($userlist);
1199         $this->assertCount(1, $userlist);
1200         $userincontext = $userlist->current();
1201         $this->assertEquals($user2->id, $userincontext->id);
1202     }
1204     /**
1205      * Test for provider::get_users_in_context() when there is a notification between users.
1206      */
1207     public function test_get_users_in_context_with_notification() {
1208         $this->resetAfterTest();
1210         $user1 = $this->getDataGenerator()->create_user();
1211         $user2 = $this->getDataGenerator()->create_user();
1213         $user1context = context_user::instance($user1->id);
1214         $user2context = context_user::instance($user2->id);
1216         // Delete user self-conversations.
1217         $this->remove_user_self_conversation($user1->id);
1218         $this->remove_user_self_conversation($user2->id);
1220         // Test nothing is found before notification is created.
1221         $userlist = new \core_privacy\local\request\userlist($user1context, 'core_message');
1222         \core_message\privacy\provider::get_users_in_context($userlist);
1223         $this->assertCount(0, $userlist);
1224         $userlist = new \core_privacy\local\request\userlist($user2context, 'core_message');
1225         \core_message\privacy\provider::get_users_in_context($userlist);
1226         $this->assertCount(0, $userlist);
1228         $this->create_notification($user1->id, $user2->id, time() - (9 * DAYSECS));
1230         // Test for the sender.
1231         $userlist = new \core_privacy\local\request\userlist($user1context, 'core_message');
1232         \core_message\privacy\provider::get_users_in_context($userlist);
1233         $this->assertCount(1, $userlist);
1234         $userincontext = $userlist->current();
1235         $this->assertEquals($user1->id, $userincontext->id);
1237         // Test for the receiver.
1238         $userlist = new \core_privacy\local\request\userlist($user2context, 'core_message');
1239         \core_message\privacy\provider::get_users_in_context($userlist);
1240         $this->assertCount(1, $userlist);
1241         $userincontext = $userlist->current();
1242         $this->assertEquals($user2->id, $userincontext->id);
1243     }
1245     /**
1246      * Test for provider::get_users_in_context() when a users has a contact.
1247      */
1248     public function test_get_users_in_context_with_contact() {
1249         $this->resetAfterTest();
1251         $user1 = $this->getDataGenerator()->create_user();
1252         $user2 = $this->getDataGenerator()->create_user();
1254         $user1context = context_user::instance($user1->id);
1255         $user2context = context_user::instance($user2->id);
1257         // Delete user self-conversations.
1258         $this->remove_user_self_conversation($user1->id);
1259         $this->remove_user_self_conversation($user2->id);
1261         // Test nothing is found before contact is created.
1262         $userlist = new \core_privacy\local\request\userlist($user1context, 'core_message');
1263         \core_message\privacy\provider::get_users_in_context($userlist);
1264         $this->assertCount(0, $userlist);
1265         $userlist = new \core_privacy\local\request\userlist($user2context, 'core_message');
1266         \core_message\privacy\provider::get_users_in_context($userlist);
1267         $this->assertCount(0, $userlist);
1269         \core_message\api::add_contact($user1->id, $user2->id);
1271         // Test for the user adding the contact.
1272         $userlist = new \core_privacy\local\request\userlist($user1context, 'core_message');
1273         \core_message\privacy\provider::get_users_in_context($userlist);
1274         $this->assertCount(1, $userlist);
1275         $userincontext = $userlist->current();
1276         $this->assertEquals($user1->id, $userincontext->id);
1278         // Test for the user who is the contact.
1279         $userlist = new \core_privacy\local\request\userlist($user2context, 'core_message');
1280         \core_message\privacy\provider::get_users_in_context($userlist);
1281         $this->assertCount(1, $userlist);
1282         $userincontext = $userlist->current();
1283         $this->assertEquals($user2->id, $userincontext->id);
1284     }
1286     /**
1287      * Test for provider::get_users_in_context() when a user makes a contact request.
1288      */
1289     public function test_get_users_in_context_with_contact_request() {
1290         $this->resetAfterTest();
1292         $user1 = $this->getDataGenerator()->create_user();
1293         $user2 = $this->getDataGenerator()->create_user();
1295         $user1context = context_user::instance($user1->id);
1296         $user2context = context_user::instance($user2->id);
1298         // Delete user self-conversations.
1299         $this->remove_user_self_conversation($user1->id);
1300         $this->remove_user_self_conversation($user2->id);
1302         // Test nothing is found before request is created.
1303         $userlist = new \core_privacy\local\request\userlist($user1context, 'core_message');
1304         \core_message\privacy\provider::get_users_in_context($userlist);
1305         $this->assertCount(0, $userlist);
1306         $userlist = new \core_privacy\local\request\userlist($user2context, 'core_message');
1307         \core_message\privacy\provider::get_users_in_context($userlist);
1308         $this->assertCount(0, $userlist);
1310         \core_message\api::create_contact_request($user1->id, $user2->id);
1312         // Test for the user requesting the contact.
1313         $userlist = new \core_privacy\local\request\userlist($user1context, 'core_message');
1314         \core_message\privacy\provider::get_users_in_context($userlist);
1315         $this->assertCount(1, $userlist);
1316         $userincontext = $userlist->current();
1317         $this->assertEquals($user1->id, $userincontext->id);
1319         // Test for the user receiving the contact request.
1320         $userlist = new \core_privacy\local\request\userlist($user2context, 'core_message');
1321         \core_message\privacy\provider::get_users_in_context($userlist);
1322         $this->assertCount(1, $userlist);
1323         $userincontext = $userlist->current();
1324         $this->assertEquals($user2->id, $userincontext->id);
1325     }
1327     /**
1328      * Test for provider::get_users_in_context() when a user is blocked.
1329      */
1330     public function test_get_users_in_context_with_blocked_contact() {
1331         $this->resetAfterTest();
1333         $user1 = $this->getDataGenerator()->create_user();
1334         $user2 = $this->getDataGenerator()->create_user();
1336         $user1context = context_user::instance($user1->id);
1337         $user2context = context_user::instance($user2->id);
1339         // Delete user self-conversations.
1340         $this->remove_user_self_conversation($user1->id);
1341         $this->remove_user_self_conversation($user2->id);
1343         // Test nothing is found before user is blocked.
1344         $userlist = new \core_privacy\local\request\userlist($user1context, 'core_message');
1345         \core_message\privacy\provider::get_users_in_context($userlist);
1346         $this->assertCount(0, $userlist);
1347         $userlist = new \core_privacy\local\request\userlist($user2context, 'core_message');
1348         \core_message\privacy\provider::get_users_in_context($userlist);
1349         $this->assertCount(0, $userlist);
1351         \core_message\api::block_user($user1->id, $user2->id);
1353         // Test for the blocking user.
1354         $userlist = new \core_privacy\local\request\userlist($user1context, 'core_message');
1355         \core_message\privacy\provider::get_users_in_context($userlist);
1356         $this->assertCount(1, $userlist);
1357         $userincontext = $userlist->current();
1358         $this->assertEquals($user1->id, $userincontext->id);
1360         // Test for the user who is blocked.
1361         $userlist = new \core_privacy\local\request\userlist($user2context, 'core_message');
1362         \core_message\privacy\provider::get_users_in_context($userlist);
1363         $this->assertCount(1, $userlist);
1364         $userincontext = $userlist->current();
1365         $this->assertEquals($user2->id, $userincontext->id);
1366     }
1368     /**
1369      * Test for provider::delete_data_for_users().
1370      */
1371     public function test_delete_data_for_users() {
1372         global $DB;
1374         $this->resetAfterTest();
1376         // Create users to test with.
1377         $user1 = $this->getDataGenerator()->create_user();
1378         $user2 = $this->getDataGenerator()->create_user();
1379         $user3 = $this->getDataGenerator()->create_user();
1380         $user4 = $this->getDataGenerator()->create_user();
1381         $user5 = $this->getDataGenerator()->create_user();
1382         $user6 = $this->getDataGenerator()->create_user();
1384         $now = time();
1385         $timeread = $now - DAYSECS;
1387         // Create contacts.
1388         \core_message\api::add_contact($user1->id, $user2->id);
1389         \core_message\api::add_contact($user2->id, $user3->id);
1391         // Create contact requests.
1392         \core_message\api::create_contact_request($user1->id, $user3->id);
1393         \core_message\api::create_contact_request($user2->id, $user4->id);
1395         // Block users.
1396         \core_message\api::block_user($user1->id, $user5->id);
1397         \core_message\api::block_user($user2->id, $user6->id);
1399         // Create messages.
1400         $m1 = $this->create_message($user1->id, $user2->id, $now + (9 * DAYSECS), $timeread);
1401         $m2 = $this->create_message($user2->id, $user1->id, $now + (8 * DAYSECS));
1403         // Create notifications.
1404         $n1 = $this->create_notification($user1->id, $user2->id, $now + (9 * DAYSECS), $timeread);
1405         $n2 = $this->create_notification($user2->id, $user1->id, $now + (8 * DAYSECS));
1406         $n2 = $this->create_notification($user2->id, $user3->id, $now + (8 * DAYSECS));
1408         // Delete one of the messages.
1409         \core_message\api::delete_message($user1->id, $m2);
1411         // There should be 2 contacts.
1412         $this->assertEquals(2, $DB->count_records('message_contacts'));
1414         // There should be 1 contact request.
1415         $this->assertEquals(2, $DB->count_records('message_contact_requests'));
1417         // There should be 1 blocked user.
1418         $this->assertEquals(2, $DB->count_records('message_users_blocked'));
1420         // There should be two messages.
1421         $this->assertEquals(2, $DB->count_records('messages'));
1423         // There should be two user actions - one for reading the message, one for deleting.
1424         $this->assertEquals(2, $DB->count_records('message_user_actions'));
1426         // There should be two conversation members + 6 self-conversations.
1427         $this->assertEquals(8, $DB->count_records('message_conversation_members'));
1429         // There should be three notifications + two for the contact requests.
1430         $this->assertEquals(5, $DB->count_records('notifications'));
1432         $user1context = context_user::instance($user1->id);
1433         $approveduserlist = new \core_privacy\local\request\approved_userlist($user1context, 'core_message',
1434                 [$user1->id, $user2->id]);
1435         provider::delete_data_for_users($approveduserlist);
1437         // Only user1's data should be deleted. User2 should be skipped as user2 is an invalid user for user1context.
1439         // Confirm the user 2 data still exists.
1440         $contacts = $DB->get_records('message_contacts');
1441         $contactrequests = $DB->get_records('message_contact_requests');
1442         $blockedusers = $DB->get_records('message_users_blocked');
1443         $messages = $DB->get_records('messages');
1444         $muas = $DB->get_records('message_user_actions');
1445         $mcms = $DB->get_records('message_conversation_members');
1446         $notifications = $DB->get_records('notifications');
1448         $this->assertCount(1, $contacts);
1449         $contact = reset($contacts);
1450         $this->assertEquals($user2->id, $contact->userid);
1451         $this->assertEquals($user3->id, $contact->contactid);
1453         $this->assertCount(1, $contactrequests);
1454         $contactrequest = reset($contactrequests);
1455         $this->assertEquals($user2->id, $contactrequest->userid);
1456         $this->assertEquals($user4->id, $contactrequest->requesteduserid);
1458         $this->assertCount(1, $blockedusers);
1459         $blockeduser = reset($blockedusers);
1460         $this->assertEquals($user2->id, $blockeduser->userid);
1461         $this->assertEquals($user6->id, $blockeduser->blockeduserid);
1463         $this->assertCount(1, $messages);
1464         $message = reset($messages);
1465         $this->assertEquals($m2, $message->id);
1467         $this->assertCount(0, $muas);
1469         $this->assertCount(6, $mcms);
1470         $memberids = array_map(function($convmember) {
1471                 return $convmember->userid;
1472         }, $mcms);
1473         $this->assertContains($user2->id, $memberids);
1475         $this->assertCount(2, $notifications);
1476         ksort($notifications);
1478         $notification = array_pop($notifications);
1479         $this->assertEquals($user2->id, $notification->useridfrom);
1480         $this->assertEquals($user3->id, $notification->useridto);
1481     }
1483     /**
1484      * Test for provider::add_contexts_for_conversations().
1485      */
1486     public function test_add_contexts_for_conversations() {
1487         $this->resetAfterTest();
1488         $this->setAdminUser();
1489         $component = 'core_group';
1490         $itemtype = 'groups';
1492         $user1 = $this->getDataGenerator()->create_user();
1493         $user2 = $this->getDataGenerator()->create_user();
1494         $user3 = $this->getDataGenerator()->create_user();
1495         $user4 = $this->getDataGenerator()->create_user();
1497         // Delete user self-conversations.
1498         $this->remove_user_self_conversation($user1->id);
1499         $this->remove_user_self_conversation($user2->id);
1500         $this->remove_user_self_conversation($user3->id);
1501         $this->remove_user_self_conversation($user4->id);
1503         // Test nothing is found before group conversations is created or message is sent.
1504         $contextlist = new contextlist();
1505         provider::add_contexts_for_conversations($contextlist, $user1->id, $component, $itemtype);
1506         $this->assertCount(0, $contextlist);
1507         provider::add_contexts_for_conversations($contextlist, $user2->id, $component, $itemtype);
1508         $this->assertCount(0, $contextlist);
1510         // Create courses.
1511         $course1 = $this->getDataGenerator()->create_course();
1512         $course2 = $this->getDataGenerator()->create_course();
1513         $coursecontext1 = \context_course::instance($course1->id);
1514         $coursecontext2 = \context_course::instance($course2->id);
1516         // Enrol users to courses.
1517         $this->getDataGenerator()->enrol_user($user1->id, $course1->id);
1518         $this->getDataGenerator()->enrol_user($user2->id, $course1->id);
1519         $this->getDataGenerator()->enrol_user($user3->id, $course1->id);
1520         $this->getDataGenerator()->enrol_user($user1->id, $course2->id);
1521         $this->getDataGenerator()->enrol_user($user2->id, $course2->id);
1523         // Create course groups with messaging enabled.
1524         $group1a = $this->getDataGenerator()->create_group(array('courseid' => $course1->id, 'enablemessaging' => 1));
1525         $group2a = $this->getDataGenerator()->create_group(array('courseid' => $course2->id, 'enablemessaging' => 1));
1527         // Add users to groups.
1528         $this->getDataGenerator()->create_group_member(array('groupid' => $group1a->id, 'userid' => $user1->id));
1529         $this->getDataGenerator()->create_group_member(array('groupid' => $group1a->id, 'userid' => $user2->id));
1530         $this->getDataGenerator()->create_group_member(array('groupid' => $group1a->id, 'userid' => $user3->id));
1531         $this->getDataGenerator()->create_group_member(array('groupid' => $group2a->id, 'userid' => $user1->id));
1533         // Get conversation.
1534         $conversation1 = \core_message\api::get_conversation_by_area(
1535             $component,
1536             $itemtype,
1537             $group1a->id,
1538             $coursecontext1->id
1539         );
1541         // Send some messages to the group conversation.
1542         $now = time();
1543         $m1id = testhelper::send_fake_message_to_conversation($user1, $conversation1->id, 'Message 1', $now + 1);
1544         $m2id = testhelper::send_fake_message_to_conversation($user1, $conversation1->id, 'Message 2', $now + 2);
1545         $m3id = testhelper::send_fake_message_to_conversation($user2, $conversation1->id, 'Message 3', $now + 3);
1547         // Test for user1 (is member of the conversation and has sent a message).
1548         $contextlist = new contextlist();
1549         provider::add_contexts_for_conversations($contextlist, $user1->id, $component, $itemtype);
1550         $this->assertCount(2, $contextlist);
1551         $this->assertContains($coursecontext1->id, $contextlist->get_contextids());
1552         $this->assertContains($coursecontext2->id, $contextlist->get_contextids());
1554         // Test for user2 (is member of the conversation and has sent a message).
1555         $contextlist = new contextlist();
1556         provider::add_contexts_for_conversations($contextlist, $user2->id, $component, $itemtype);
1557         $this->assertCount(1, $contextlist);
1558         $this->assertEquals($coursecontext1, $contextlist->current());
1560         // Test for user3 (is member of the conversation).
1561         $contextlist = new contextlist();
1562         provider::add_contexts_for_conversations($contextlist, $user3->id, $component, $itemtype);
1563         $this->assertCount(1, $contextlist);
1564         $this->assertEquals($coursecontext1, $contextlist->current());
1566         // Test for user4 (doesn't belong to the conversation).
1567         $contextlist = new contextlist();
1568         provider::add_contexts_for_conversations($contextlist, $user4->id, $component, $itemtype);
1569         $this->assertCount(0, $contextlist);
1570     }
1572     /**
1573      * Test for provider::add_conversations_in_context().
1574      */
1575     public function test_add_conversations_in_context() {
1576         $this->resetAfterTest();
1577         $this->setAdminUser();
1578         $component = 'core_group';
1579         $itemtype = 'groups';
1581         $user1 = $this->getDataGenerator()->create_user();
1582         $user2 = $this->getDataGenerator()->create_user();
1583         $user3 = $this->getDataGenerator()->create_user();
1584         $user4 = $this->getDataGenerator()->create_user();
1586         // Create courses.
1587         $course1 = $this->getDataGenerator()->create_course();
1588         $course2 = $this->getDataGenerator()->create_course();
1589         $coursecontext1 = \context_course::instance($course1->id);
1590         $coursecontext2 = \context_course::instance($course2->id);
1592         // Test nothing is found before group conversations is created or message is sent.
1593         $userlist1 = new \core_privacy\local\request\userlist($coursecontext1, 'core_message');
1594         provider::add_conversations_in_context($userlist1, $component, $itemtype);
1595         $this->assertCount(0, $userlist1);
1597         // Enrol users to courses.
1598         $this->getDataGenerator()->enrol_user($user1->id, $course1->id);
1599         $this->getDataGenerator()->enrol_user($user2->id, $course1->id);
1600         $this->getDataGenerator()->enrol_user($user3->id, $course1->id);
1601         $this->getDataGenerator()->enrol_user($user4->id, $course1->id);
1602         $this->getDataGenerator()->enrol_user($user1->id, $course2->id);
1603         $this->getDataGenerator()->enrol_user($user2->id, $course2->id);
1605         // Create course groups with messaging enabled.
1606         $group1a = $this->getDataGenerator()->create_group(array('courseid' => $course1->id, 'enablemessaging' => 1));
1607         $group2a = $this->getDataGenerator()->create_group(array('courseid' => $course2->id, 'enablemessaging' => 1));
1609         // Add users to groups.
1610         $this->getDataGenerator()->create_group_member(array('groupid' => $group1a->id, 'userid' => $user1->id));
1611         $this->getDataGenerator()->create_group_member(array('groupid' => $group1a->id, 'userid' => $user2->id));
1612         $this->getDataGenerator()->create_group_member(array('groupid' => $group1a->id, 'userid' => $user3->id));
1613         $this->getDataGenerator()->create_group_member(array('groupid' => $group2a->id, 'userid' => $user1->id));
1615         // Get conversation.
1616         $conversation1 = \core_message\api::get_conversation_by_area(
1617             $component,
1618             $itemtype,
1619             $group1a->id,
1620             $coursecontext1->id
1621         );
1623         // Send some messages to the group conversation.
1624         $now = time();
1625         $m1id = testhelper::send_fake_message_to_conversation($user1, $conversation1->id, 'Message 1', $now + 1);
1626         $m2id = testhelper::send_fake_message_to_conversation($user1, $conversation1->id, 'Message 2', $now + 2);
1627         $m3id = testhelper::send_fake_message_to_conversation($user2, $conversation1->id, 'Message 3', $now + 3);
1629         // Test for users with any group conversation in course1.
1630         provider::add_conversations_in_context($userlist1, $component, $itemtype);
1631         $this->assertCount(3, $userlist1);
1632         $this->assertEquals([$user1->id, $user2->id, $user3->id], $userlist1->get_userids(), '', 0, 10, true);
1634         // Test for users with any group conversation in course2.
1635         $userlist2 = new \core_privacy\local\request\userlist($coursecontext2, 'core_message');
1636         provider::add_conversations_in_context($userlist2, $component, $itemtype);
1637         $this->assertCount(1, $userlist2);
1638         $this->assertEquals(
1639                 [$user1->id],
1640                 $userlist2->get_userids());
1641     }
1643     /**
1644      * Test for provider::export_conversations().
1645      */
1646     public function test_export_conversations() {
1647         global $DB;
1649         $this->resetAfterTest();
1650         $this->setAdminUser();
1651         $now = time();
1652         $systemcontext = \context_system::instance();
1654         // Create users to test with.
1655         $user1 = $this->getDataGenerator()->create_user();
1656         $user2 = $this->getDataGenerator()->create_user();
1657         $user3 = $this->getDataGenerator()->create_user();
1658         $user1context = \context_user::instance($user1->id);
1660         $course1 = $this->getDataGenerator()->create_course();
1661         $course2 = $this->getDataGenerator()->create_course();
1662         $coursecontext1 = \context_course::instance($course1->id);
1663         $coursecontext2 = \context_course::instance($course2->id);
1665         // Enrol users to courses.
1666         $this->getDataGenerator()->enrol_user($user1->id, $course1->id);
1667         $this->getDataGenerator()->enrol_user($user2->id, $course1->id);
1668         $this->getDataGenerator()->enrol_user($user3->id, $course1->id);
1669         $this->getDataGenerator()->enrol_user($user1->id, $course2->id);
1671         // Create course groups with group messaging enabled.
1672         $group1a = $this->getDataGenerator()->create_group(array('courseid' => $course1->id, 'enablemessaging' => 1));
1673         $group2a = $this->getDataGenerator()->create_group(array('courseid' => $course2->id, 'enablemessaging' => 1));
1675         // Add users to groups.
1676         $this->getDataGenerator()->create_group_member(array('groupid' => $group1a->id, 'userid' => $user1->id));
1677         $this->getDataGenerator()->create_group_member(array('groupid' => $group1a->id, 'userid' => $user2->id));
1678         $this->getDataGenerator()->create_group_member(array('groupid' => $group1a->id, 'userid' => $user3->id));
1679         $this->getDataGenerator()->create_group_member(array('groupid' => $group2a->id, 'userid' => $user1->id));
1681         // Send some private messages between user 1 and user 2.
1682         $pm1id = $this->create_message($user1->id, $user2->id, $now);
1684         // Get conversation.
1685         $iconversation1id = \core_message\api::get_conversation_between_users([$user1->id, $user2->id]);
1686         $component = 'core_group';
1687         $itemtype = 'groups';
1688         $conversation1 = \core_message\api::get_conversation_by_area(
1689             $component,
1690             $itemtype,
1691             $group1a->id,
1692             $coursecontext1->id
1693         );
1695         // Make favourite some conversations.
1696         \core_message\api::set_favourite_conversation($conversation1->id, $user1->id);
1697         \core_message\api::set_favourite_conversation($iconversation1id, $user2->id);
1699         // Mute some conversations.
1700         \core_message\api::mute_conversation($user1->id, $conversation1->id);
1701         \core_message\api::mute_conversation($user2->id, $iconversation1id);
1703         // Send some messages to the conversation.
1704         $m1 = testhelper::send_fake_message_to_conversation($user1, $conversation1->id, 'Message 1', $now + 1);
1705         $m2 = testhelper::send_fake_message_to_conversation($user1, $conversation1->id, 'Message 2', $now + 2);
1706         $m3 = testhelper::send_fake_message_to_conversation($user2, $conversation1->id, 'Message 3', $now + 3);
1708         $dbm1 = $DB->get_record('messages', ['id' => $m1]);
1709         $dbm2 = $DB->get_record('messages', ['id' => $m2]);
1710         $dbm3 = $DB->get_record('messages', ['id' => $m3]);
1712         // Mark as read and delete some messages.
1713         \core_message\api::mark_message_as_read($user1->id, $dbm3, $now + 5);
1714         \core_message\api::delete_message($user1->id, $m2);
1716         // Export all the conversations related to the groups in course1 for user1.
1717         provider::export_conversations($user1->id, 'core_group', 'groups', $coursecontext1);
1719         // Check that system context hasn't been exported.
1720         $writer = writer::with_context($systemcontext);
1721         $this->assertFalse($writer->has_any_data());
1723         // Check that course2 context hasn't been exported.
1724         $writer = writer::with_context($coursecontext2);
1725         $this->assertFalse($writer->has_any_data());
1727         // Check that course1 context has been exported for user1 and contains data.
1728         $writer = writer::with_context($coursecontext1);
1729         $this->assertTrue($writer->has_any_data());
1731         // Confirm the messages for conversation1 are correct.
1732         $messages = (array) $writer->get_data([
1733             get_string('messages', 'core_message'),
1734             get_string($conversation1->itemtype, $conversation1->component),
1735             get_string('privacy:export:conversationprefix', 'core_message') . $conversation1->name
1736         ]);
1737         $this->assertCount(3, $messages);
1739         usort($messages, ['static', 'sort_messages']);
1740         $m1 = array_shift($messages);
1741         $m2 = array_shift($messages);
1742         $m3 = array_shift($messages);
1744         // Check message 1 is correct.
1745         $this->assertEquals(get_string('yes'), $m1->issender);
1746         $this->assertEquals(message_format_message_text($dbm1), $m1->message);
1747         $this->assertEquals(transform::datetime($now + 1), $m1->timecreated);
1748         $this->assertEquals('-', $m1->timeread);
1749         $this->assertArrayNotHasKey('timedeleted', (array) $m1);
1751         // Check message 2 is correct.
1752         $this->assertEquals(get_string('yes'), $m2->issender);
1753         $this->assertEquals(message_format_message_text($dbm2), $m2->message);
1754         $this->assertEquals(transform::datetime($now + 2), $m2->timecreated);
1755         $this->assertEquals('-', $m2->timeread);
1756         $this->assertArrayHasKey('timedeleted', (array) $m2);
1758         // Check message 3 is correct.
1759         $this->assertEquals(get_string('no'), $m3->issender);
1760         $this->assertEquals(message_format_message_text($dbm3), $m3->message);
1761         $this->assertEquals(transform::datetime($now + 3), $m3->timecreated);
1762         $this->assertEquals(transform::datetime($now + 5), $m3->timeread);
1763         $this->assertArrayNotHasKey('timedeleted', (array) $m3);
1765         // Confirm the muted group conversation is correct.
1766         $mutedconversations = (array) $writer->get_related_data([
1767             get_string('messages', 'core_message'),
1768             get_string($conversation1->itemtype, $conversation1->component),
1769             get_string('privacy:export:conversationprefix', 'core_message') . $conversation1->name
1770         ], 'muted');
1771         $this->assertCount(2, $mutedconversations);
1772         $this->assertEquals(get_string('yes'), $mutedconversations['muted']);
1774         // Confirm the favourite group conversation is correct.
1775         $favourite = (array) $writer->get_related_data([
1776             get_string('messages', 'core_message'),
1777             get_string($conversation1->itemtype, $conversation1->component),
1778             get_string('privacy:export:conversationprefix', 'core_message') . $conversation1->name
1779         ], 'starred');
1780         $this->assertCount(4, $favourite);
1781         $this->assertEquals(get_string('yes'), $favourite['starred']);
1783         // Reset writer before exporting conversations for user2.
1784         writer::reset();
1786         // Export all the conversations related to the groups in course1 for user2.
1787         provider::export_conversations($user2->id, 'core_group', 'groups', $coursecontext1);
1789         // Check that system context hasn't been exported.
1790         $writer = writer::with_context($systemcontext);
1791         $this->assertFalse($writer->has_any_data());
1793         // Check that course2 context hasn't been exported.
1794         $writer = writer::with_context($coursecontext2);
1795         $this->assertFalse($writer->has_any_data());
1797         // Check that course1 context has been exported for user2 and contains data.
1798         $writer = writer::with_context($coursecontext1);
1799         $this->assertTrue($writer->has_any_data());
1801         // Confirm the messages for conversation1 are correct.
1802         $messages = (array) $writer->get_data([
1803             get_string('messages', 'core_message'),
1804             get_string($conversation1->itemtype, $conversation1->component),
1805             get_string('privacy:export:conversationprefix', 'core_message') . $conversation1->name
1806         ]);
1807         $this->assertCount(3, $messages);
1809         usort($messages, ['static', 'sort_messages']);
1810         $m1 = array_shift($messages);
1811         $m2 = array_shift($messages);
1812         $m3 = array_shift($messages);
1814         // Check message 1 is correct.
1815         $this->assertEquals(get_string('no'), $m1->issender);
1816         $this->assertEquals(message_format_message_text($dbm1), $m1->message);
1817         $this->assertEquals(transform::datetime($now + 1), $m1->timecreated);
1818         $this->assertEquals('-', $m1->timeread);
1819         $this->assertArrayNotHasKey('timedeleted', (array) $m1);
1821         // Check message 2 is correct.
1822         $this->assertEquals(get_string('no'), $m2->issender);
1823         $this->assertEquals(message_format_message_text($dbm2), $m2->message);
1824         $this->assertEquals(transform::datetime($now + 2), $m2->timecreated);
1825         $this->assertEquals('-', $m2->timeread);
1826         $this->assertArrayNotHasKey('timedeleted', (array) $m2);
1828         // Check message 3 is correct.
1829         $this->assertEquals(get_string('yes'), $m3->issender);
1830         $this->assertEquals(message_format_message_text($dbm3), $m3->message);
1831         $this->assertEquals(transform::datetime($now + 3), $m3->timecreated);
1832         $this->assertEquals('-', $m3->timeread);
1833         $this->assertArrayNotHasKey('timedeleted', (array) $m3);
1835         // Confirm the muted group conversation is correct.
1836         $mutedconversations = (array) $writer->get_related_data([
1837             get_string('messages', 'core_message'),
1838             get_string($conversation1->itemtype, $conversation1->component),
1839             $conversation1->name
1840         ], 'muted');
1841         $this->assertCount(0, $mutedconversations);
1843         // Confirm there are no favourite group conversation for user2.
1844         $favourite = (array) $writer->get_related_data([
1845             get_string('messages', 'core_message'),
1846             get_string($conversation1->itemtype, $conversation1->component),
1847             $conversation1->name
1848         ], 'starred');
1849         $this->assertCount(0, $favourite);
1850     }
1852     /**
1853      * Test for provider::delete_conversations_for_all_users().
1854      */
1855     public function test_delete_conversations_for_all_users() {
1856         global $DB;
1858         $this->resetAfterTest();
1859         $this->setAdminUser();
1860         $now = time();
1861         $timeread = $now - DAYSECS;
1862         $component = 'core_group';
1863         $itemtype = 'groups';
1865         // Create users to test with.
1866         $user1 = $this->getDataGenerator()->create_user();
1867         $user2 = $this->getDataGenerator()->create_user();
1868         $user3 = $this->getDataGenerator()->create_user();
1869         $user4 = $this->getDataGenerator()->create_user();
1870         $user5 = $this->getDataGenerator()->create_user();
1871         $user1context = \context_user::instance($user1->id);
1873         // Create contacts.
1874         \core_message\api::add_contact($user1->id, $user2->id);
1875         \core_message\api::add_contact($user2->id, $user3->id);
1877         // Create contact requests.
1878         \core_message\api::create_contact_request($user1->id, $user3->id);
1879         \core_message\api::create_contact_request($user2->id, $user4->id);
1881         // Block a user.
1882         \core_message\api::block_user($user1->id, $user3->id);
1883         \core_message\api::block_user($user3->id, $user4->id);
1885         // Create individual messages.
1886         $im1 = $this->create_message($user1->id, $user2->id, $now + (9 * DAYSECS), true);
1887         $im2 = $this->create_message($user2->id, $user1->id, $now + (8 * DAYSECS), true);
1888         $im3 = $this->create_message($user2->id, $user3->id, $now + (7 * DAYSECS));
1890         // Create notifications.
1891         $n1 = $this->create_notification($user1->id, $user2->id, $now + (9 * DAYSECS), $timeread);
1892         $n2 = $this->create_notification($user2->id, $user1->id, $now + (8 * DAYSECS));
1893         $n3 = $this->create_notification($user2->id, $user3->id, $now + (7 * DAYSECS));
1895         // Delete one of the messages.
1896         \core_message\api::delete_message($user1->id, $im2);
1898         // Create course2.
1899         $course1 = $this->getDataGenerator()->create_course();
1900         $course2 = $this->getDataGenerator()->create_course();
1901         $coursecontext1 = \context_course::instance($course1->id);
1902         $coursecontext2 = \context_course::instance($course2->id);
1904         // Enrol users to courses.
1905         $this->getDataGenerator()->enrol_user($user1->id, $course1->id);
1906         $this->getDataGenerator()->enrol_user($user2->id, $course1->id);
1907         $this->getDataGenerator()->enrol_user($user3->id, $course1->id);
1908         $this->getDataGenerator()->enrol_user($user1->id, $course2->id);
1909         $this->getDataGenerator()->enrol_user($user2->id, $course2->id);
1911         // Create course groups with group messaging enabled.
1912         $group1a = $this->getDataGenerator()->create_group(array('courseid' => $course1->id, 'enablemessaging' => 1));
1913         $group2a = $this->getDataGenerator()->create_group(array('courseid' => $course2->id, 'enablemessaging' => 1));
1915         // Add users to groups.
1916         $this->getDataGenerator()->create_group_member(array('groupid' => $group1a->id, 'userid' => $user1->id));
1917         $this->getDataGenerator()->create_group_member(array('groupid' => $group1a->id, 'userid' => $user2->id));
1918         $this->getDataGenerator()->create_group_member(array('groupid' => $group1a->id, 'userid' => $user3->id));
1919         $this->getDataGenerator()->create_group_member(array('groupid' => $group2a->id, 'userid' => $user1->id));
1920         $this->getDataGenerator()->create_group_member(array('groupid' => $group2a->id, 'userid' => $user2->id));
1922         // Get conversations.
1923         $iconversation1id = \core_message\api::get_conversation_between_users([$user1->id, $user2->id]);
1924         $conversation1 = \core_message\api::get_conversation_by_area(
1925             $component,
1926             $itemtype,
1927             $group1a->id,
1928             $coursecontext1->id
1929         );
1930         $conversation2 = \core_message\api::get_conversation_by_area(
1931             $component,
1932             $itemtype,
1933             $group2a->id,
1934             $coursecontext2->id
1935         );
1937         // Make favourite some conversations.
1938         \core_message\api::set_favourite_conversation($iconversation1id, $user1->id);
1939         \core_message\api::set_favourite_conversation($conversation1->id, $user1->id);
1940         \core_message\api::set_favourite_conversation($conversation1->id, $user2->id);
1942         // Send some messages to the conversation.
1943         $gm1 = testhelper::send_fake_message_to_conversation($user1, $conversation1->id, 'Message 1.1', $now + 1);
1944         $gm2 = testhelper::send_fake_message_to_conversation($user1, $conversation1->id, 'Message 1.2', $now + 2);
1945         $gm3 = testhelper::send_fake_message_to_conversation($user2, $conversation1->id, 'Message 1.3', $now + 3);
1946         $gm4 = testhelper::send_fake_message_to_conversation($user1, $conversation2->id, 'Message 2.1', $now + 4);
1947         $gm5 = testhelper::send_fake_message_to_conversation($user2, $conversation2->id, 'Message 2.2', $now + 5);
1949         $dbgm1 = $DB->get_record('messages', ['id' => $gm1]);
1950         $dbgm2 = $DB->get_record('messages', ['id' => $gm2]);
1951         $dbgm3 = $DB->get_record('messages', ['id' => $gm3]);
1952         $dbgm4 = $DB->get_record('messages', ['id' => $gm4]);
1953         $dbgm5 = $DB->get_record('messages', ['id' => $gm5]);
1955         // Mark as read one of the conversation messages.
1956         \core_message\api::mark_message_as_read($user1->id, $dbgm3, $now + 5);
1958         // Mark some conversations as muted by two users.
1959         \core_message\api::mute_conversation($user1->id, $iconversation1id);
1960         \core_message\api::mute_conversation($user1->id, $conversation1->id);
1961         \core_message\api::mute_conversation($user2->id, $conversation1->id);
1963         // There should be 2 contacts.
1964         $this->assertEquals(2, $DB->count_records('message_contacts'));
1966         // There should be 2 contact requests.
1967         $this->assertEquals(2, $DB->count_records('message_contact_requests'));
1969         // There should be 2 blocked users.
1970         $this->assertEquals(2, $DB->count_records('message_users_blocked'));
1972         // There should be 8 messages.
1973         $this->assertEquals(8, $DB->count_records('messages'));
1975         // There should be 4 user actions - 3 for reading the message, 1 for deleting.
1976         $this->assertEquals(4, $DB->count_records('message_user_actions'));
1978         // There should be 3 muted conversations.
1979         $this->assertEquals(3, $DB->count_records('message_conversation_actions'));
1981         // There should be 4 conversations - 2 individual + 2 group + 5 self-conversations.
1982         $this->assertEquals(9, $DB->count_records('message_conversations'));
1984         // There should be 9 conversation members - (2 + 2) individual + (3 + 2) group + 5 self-conversations.
1985         $this->assertEquals(14 , $DB->count_records('message_conversation_members'));
1987         // There should be 5 notifications (3 from create_notification and 2 from create_contact_request).
1988         $this->assertEquals(5, $DB->count_records('notifications'));
1990         // There should be 3 favourite conversations + 5 self-conversations.
1991         $this->assertEquals(8, $DB->count_records('favourite'));
1993         // Delete conversations for all users in course1.
1994         provider::delete_conversations_for_all_users($coursecontext1, $component, $itemtype);
1996         // There should be still 2 contacts.
1997         $this->assertEquals(2, $DB->count_records('message_contacts'));
1999         // There should be still 2 contact requests.
2000         $this->assertEquals(2, $DB->count_records('message_contact_requests'));
2002         // There should be still 2 blocked users.
2003         $this->assertEquals(2, $DB->count_records('message_users_blocked'));
2005         // There should be 1 muted conversation.
2006         $this->assertEquals(1, $DB->count_records('message_conversation_actions'));
2008         // There should be 3 notifications.
2009         $this->assertEquals(5, $DB->count_records('notifications'));
2011         // There should be 5 messages - 3 individual - 2 group (course2).
2012         $this->assertEquals(5, $DB->count_records('messages'));
2013         $messages = array_keys($DB->get_records('messages'));
2014         $this->assertContains($im1, $messages);
2015         $this->assertContains($im2, $messages);
2016         $this->assertContains($im3, $messages);
2017         $this->assertContains($gm4, $messages);
2018         $this->assertContains($gm5, $messages);
2020         // There should be 3 user actions - 2 for reading the message, 1 for deleting.
2021         $this->assertEquals(3, $DB->count_records('message_user_actions'));
2022         $useractions = $DB->get_records('message_user_actions');
2023         $useractions = array_map(function($action) {
2024                 return $action->messageid;
2025         }, $useractions);
2026         $this->assertNotContains($gm3, $useractions);
2028         // There should be 3 conversations - 2 individual + 1 group (course2) + 5 self-conversations.
2029         $this->assertEquals(8, $DB->count_records('message_conversations'));
2030         $conversations = $DB->get_records('message_conversations');
2031         $this->assertArrayNotHasKey($conversation1->id, $conversations);
2033         // There should be 6 conversation members - (2 + 2) individual + 2 group + 5 self-conversations.
2034         $this->assertEquals(11, $DB->count_records('message_conversation_members'));
2036         // There should be 1 favourite conversation - the individual one + 5 self-conversations.
2037         $this->assertEquals(6, $DB->count_records('favourite'));
2038     }
2040     /**
2041      * Test for provider::delete_conversations_for_all_users() in the system context.
2042      */
2043     public function test_delete_conversations_for_all_users_systemcontext() {
2044         global $DB;
2046         $this->resetAfterTest();
2047         $this->setAdminUser();
2048         $now = time();
2049         $timeread = $now - DAYSECS;
2050         $systemcontext = \context_system::instance();
2051         $component = 'core_group';
2052         $itemtype = 'groups';
2054         // Create users to test with.
2055         $user1 = $this->getDataGenerator()->create_user();
2056         $user2 = $this->getDataGenerator()->create_user();
2057         $user3 = $this->getDataGenerator()->create_user();
2058         $user4 = $this->getDataGenerator()->create_user();
2059         $user5 = $this->getDataGenerator()->create_user();
2061         // Create contacts.
2062         \core_message\api::add_contact($user1->id, $user2->id);
2063         \core_message\api::add_contact($user2->id, $user3->id);
2065         // Create contact requests.
2066         \core_message\api::create_contact_request($user1->id, $user3->id);
2067         \core_message\api::create_contact_request($user2->id, $user4->id);
2069         // Block a user.
2070         \core_message\api::block_user($user1->id, $user3->id);
2071         \core_message\api::block_user($user3->id, $user4->id);
2073         // Create individual messages.
2074         $im1 = $this->create_message($user1->id, $user2->id, $now + (9 * DAYSECS), true);
2075         $im2 = $this->create_message($user2->id, $user1->id, $now + (8 * DAYSECS), true);
2076         $im3 = $this->create_message($user2->id, $user3->id, $now + (7 * DAYSECS));
2078         // Create notifications.
2079         $n1 = $this->create_notification($user1->id, $user2->id, $now + (9 * DAYSECS), $timeread);
2080         $n2 = $this->create_notification($user2->id, $user1->id, $now + (8 * DAYSECS));
2081         $n3 = $this->create_notification($user2->id, $user3->id, $now + (7 * DAYSECS));
2083         // Delete one of the messages.
2084         \core_message\api::delete_message($user1->id, $im2);
2086         // Create course2.
2087         $course1 = $this->getDataGenerator()->create_course();
2088         $course2 = $this->getDataGenerator()->create_course();
2089         $coursecontext1 = \context_course::instance($course1->id);
2090         $coursecontext2 = \context_course::instance($course2->id);
2092         // Enrol users to courses.
2093         $this->getDataGenerator()->enrol_user($user1->id, $course1->id);
2094         $this->getDataGenerator()->enrol_user($user2->id, $course1->id);
2095         $this->getDataGenerator()->enrol_user($user3->id, $course1->id);
2096         $this->getDataGenerator()->enrol_user($user1->id, $course2->id);
2097         $this->getDataGenerator()->enrol_user($user2->id, $course2->id);
2099         // Create course groups with group messaging enabled.
2100         $group1a = $this->getDataGenerator()->create_group(array('courseid' => $course1->id, 'enablemessaging' => 1));
2101         $group2a = $this->getDataGenerator()->create_group(array('courseid' => $course2->id, 'enablemessaging' => 1));
2103         // Add users to groups.
2104         $this->getDataGenerator()->create_group_member(array('groupid' => $group1a->id, 'userid' => $user1->id));
2105         $this->getDataGenerator()->create_group_member(array('groupid' => $group1a->id, 'userid' => $user2->id));
2106         $this->getDataGenerator()->create_group_member(array('groupid' => $group1a->id, 'userid' => $user3->id));
2107         $this->getDataGenerator()->create_group_member(array('groupid' => $group2a->id, 'userid' => $user1->id));
2108         $this->getDataGenerator()->create_group_member(array('groupid' => $group2a->id, 'userid' => $user2->id));
2110         // Get conversations.
2111         $iconversation1id = \core_message\api::get_conversation_between_users([$user1->id, $user2->id]);
2112         $conversation1 = \core_message\api::get_conversation_by_area(
2113             $component,
2114             $itemtype,
2115             $group1a->id,
2116             $coursecontext1->id
2117         );
2118         $conversation2 = \core_message\api::get_conversation_by_area(
2119             $component,
2120             $itemtype,
2121             $group2a->id,
2122             $coursecontext2->id
2123         );
2125         // Make favourite some conversations.
2126         \core_message\api::set_favourite_conversation($iconversation1id, $user1->id);
2127         \core_message\api::set_favourite_conversation($conversation1->id, $user1->id);
2128         \core_message\api::set_favourite_conversation($conversation1->id, $user2->id);
2130         // Send some messages to the conversation.
2131         $gm1 = testhelper::send_fake_message_to_conversation($user1, $conversation1->id, 'Message 1.1', $now + 1);
2132         $gm2 = testhelper::send_fake_message_to_conversation($user1, $conversation1->id, 'Message 1.2', $now + 2);
2133         $gm3 = testhelper::send_fake_message_to_conversation($user2, $conversation1->id, 'Message 1.3', $now + 3);
2134         $gm4 = testhelper::send_fake_message_to_conversation($user1, $conversation2->id, 'Message 2.1', $now + 4);
2135         $gm5 = testhelper::send_fake_message_to_conversation($user2, $conversation2->id, 'Message 2.2', $now + 5);
2137         $dbgm3 = $DB->get_record('messages', ['id' => $gm3]);
2139         // Mark some conversations as muted by two users.
2140         \core_message\api::mute_conversation($user1->id, $iconversation1id);
2141         \core_message\api::mute_conversation($user1->id, $conversation1->id);
2142         \core_message\api::mute_conversation($user2->id, $conversation1->id);
2144         // Mark as read one of the conversation messages.
2145         \core_message\api::mark_message_as_read($user1->id, $dbgm3, $now + 5);
2147         // There should be 2 contacts.
2148         $this->assertEquals(2, $DB->count_records('message_contacts'));
2150         // There should be 2 contact requests.
2151         $this->assertEquals(2, $DB->count_records('message_contact_requests'));
2153         // There should be 2 blocked users.
2154         $this->assertEquals(2, $DB->count_records('message_users_blocked'));
2156         // There should be 8 messages.
2157         $this->assertEquals(8, $DB->count_records('messages'));
2159         // There should be 4 user actions - 3 for reading the message, 1 for deleting.
2160         $this->assertEquals(4, $DB->count_records('message_user_actions'));
2162         // There should be 3 muted conversations.
2163         $this->assertEquals(3, $DB->count_records('message_conversation_actions'));
2165         // There should be 4 conversations - 2 individual + 2 group + 5 self-conversations.
2166         $this->assertEquals(9, $DB->count_records('message_conversations'));
2168         // There should be 9 conversation members - (2 + 2) individual + (3 + 2) group + 5 self-conversations.
2169         $this->assertEquals(14, $DB->count_records('message_conversation_members'));
2171         // There should be 5 notifications (3 from create_notification and 2 from create_contact_request).
2172         $this->assertEquals(5, $DB->count_records('notifications'));
2174         // There should be 3 favourite conversations + 5 self-conversations.
2175         $this->assertEquals(8, $DB->count_records('favourite'));
2177         // Delete group conversations for all users in system context.
2178         provider::delete_conversations_for_all_users($systemcontext, $component, $itemtype);
2180         // No conversations should be removed, because they are in the course context.
2181         $this->assertEquals(2, $DB->count_records('message_contacts'));
2182         $this->assertEquals(2, $DB->count_records('message_contact_requests'));
2183         $this->assertEquals(2, $DB->count_records('message_users_blocked'));
2184         $this->assertEquals(8, $DB->count_records('messages'));
2185         $this->assertEquals(4, $DB->count_records('message_user_actions'));
2186         $this->assertEquals(3, $DB->count_records('message_conversation_actions'));
2187         $this->assertEquals(9, $DB->count_records('message_conversations'));
2188         $this->assertEquals(14, $DB->count_records('message_conversation_members'));
2189         $this->assertEquals(5, $DB->count_records('notifications'));
2190         $this->assertEquals(8, $DB->count_records('favourite'));
2192         // Delete individual conversations for all users in system context.
2193         provider::delete_conversations_for_all_users($systemcontext, '', '');
2195         // No conversations should be removed, because they've been moved to user context.
2196         $this->assertEquals(2, $DB->count_records('message_contacts'));
2197         $this->assertEquals(2, $DB->count_records('message_contact_requests'));
2198         $this->assertEquals(2, $DB->count_records('message_users_blocked'));
2199         $this->assertEquals(8, $DB->count_records('messages'));
2200         $this->assertEquals(4, $DB->count_records('message_user_actions'));
2201         $this->assertEquals(3, $DB->count_records('message_conversation_actions'));
2202         $this->assertEquals(9, $DB->count_records('message_conversations'));
2203         $this->assertEquals(14, $DB->count_records('message_conversation_members'));
2204         $this->assertEquals(5, $DB->count_records('notifications'));
2205         $this->assertEquals(8, $DB->count_records('favourite'));
2206     }
2208     /**
2209      * Test for provider::delete_conversations_for_all_users() in the user context.
2210      */
2211     public function test_delete_conversations_for_all_users_usercontext() {
2212         global $DB;
2214         $this->resetAfterTest();
2215         $this->setAdminUser();
2216         $now = time();
2217         $timeread = $now - DAYSECS;
2218         $component = 'core_group';
2219         $itemtype = 'groups';
2221         // Create users to test with.
2222         $user1 = $this->getDataGenerator()->create_user();
2223         $user2 = $this->getDataGenerator()->create_user();
2224         $user3 = $this->getDataGenerator()->create_user();
2225         $user4 = $this->getDataGenerator()->create_user();
2226         $user5 = $this->getDataGenerator()->create_user();
2227         $user1context = \context_user::instance($user1->id);
2229         // Create contacts.
2230         \core_message\api::add_contact($user1->id, $user2->id);
2231         \core_message\api::add_contact($user2->id, $user3->id);
2233         // Create contact requests.
2234         \core_message\api::create_contact_request($user1->id, $user3->id);
2235         \core_message\api::create_contact_request($user2->id, $user4->id);
2237         // Block a user.
2238         \core_message\api::block_user($user1->id, $user3->id);
2239         \core_message\api::block_user($user3->id, $user4->id);
2241         // Create individual messages.
2242         $im1 = $this->create_message($user1->id, $user2->id, $now + (9 * DAYSECS), true);
2243         $im2 = $this->create_message($user2->id, $user1->id, $now + (8 * DAYSECS), true);
2244         $im3 = $this->create_message($user2->id, $user3->id, $now + (7 * DAYSECS));
2246         // Create notifications.
2247         $n1 = $this->create_notification($user1->id, $user2->id, $now + (9 * DAYSECS), $timeread);
2248         $n2 = $this->create_notification($user2->id, $user1->id, $now + (8 * DAYSECS));
2249         $n3 = $this->create_notification($user2->id, $user3->id, $now + (7 * DAYSECS));
2251         // Delete one of the messages.
2252         \core_message\api::delete_message($user1->id, $im2);
2254         // Create course2.
2255         $course1 = $this->getDataGenerator()->create_course();
2256         $course2 = $this->getDataGenerator()->create_course();
2257         $coursecontext1 = \context_course::instance($course1->id);
2258         $coursecontext2 = \context_course::instance($course2->id);
2260         // Enrol users to courses.
2261         $this->getDataGenerator()->enrol_user($user1->id, $course1->id);
2262         $this->getDataGenerator()->enrol_user($user2->id, $course1->id);
2263         $this->getDataGenerator()->enrol_user($user3->id, $course1->id);
2264         $this->getDataGenerator()->enrol_user($user1->id, $course2->id);
2265         $this->getDataGenerator()->enrol_user($user2->id, $course2->id);
2267         // Create course groups with group messaging enabled.
2268         $group1a = $this->getDataGenerator()->create_group(array('courseid' => $course1->id, 'enablemessaging' => 1));
2269         $group2a = $this->getDataGenerator()->create_group(array('courseid' => $course2->id, 'enablemessaging' => 1));
2271         // Add users to groups.
2272         $this->getDataGenerator()->create_group_member(array('groupid' => $group1a->id, 'userid' => $user1->id));
2273         $this->getDataGenerator()->create_group_member(array('groupid' => $group1a->id, 'userid' => $user2->id));
2274         $this->getDataGenerator()->create_group_member(array('groupid' => $group1a->id, 'userid' => $user3->id));
2275         $this->getDataGenerator()->create_group_member(array('groupid' => $group2a->id, 'userid' => $user1->id));
2276         $this->getDataGenerator()->create_group_member(array('groupid' => $group2a->id, 'userid' => $user2->id));
2278         // Get conversation.
2279         $iconversation1id = \core_message\api::get_conversation_between_users([$user1->id, $user2->id]);
2280         $iconversation2id = \core_message\api::get_conversation_between_users([$user2->id, $user3->id]);
2281         $conversation1 = \core_message\api::get_conversation_by_area(
2282             $component,
2283             $itemtype,
2284             $group1a->id,
2285             $coursecontext1->id
2286         );
2287         $conversation2 = \core_message\api::get_conversation_by_area(
2288             $component,
2289             $itemtype,
2290             $group2a->id,
2291             $coursecontext2->id
2292         );
2294         // Make favourite some conversations.
2295         \core_message\api::set_favourite_conversation($iconversation1id, $user1->id);
2296         \core_message\api::set_favourite_conversation($conversation1->id, $user1->id);
2297         \core_message\api::set_favourite_conversation($conversation1->id, $user2->id);
2299         // Send some messages to the conversation.
2300         $gm1 = testhelper::send_fake_message_to_conversation($user1, $conversation1->id, 'Message 1.1', $now + 1);
2301         $gm2 = testhelper::send_fake_message_to_conversation($user1, $conversation1->id, 'Message 1.2', $now + 2);
2302         $gm3 = testhelper::send_fake_message_to_conversation($user2, $conversation1->id, 'Message 1.3', $now + 3);
2303         $gm4 = testhelper::send_fake_message_to_conversation($user1, $conversation2->id, 'Message 2.1', $now + 4);
2304         $gm5 = testhelper::send_fake_message_to_conversation($user2, $conversation2->id, 'Message 2.2', $now + 5);
2306         $dbgm3 = $DB->get_record('messages', ['id' => $gm3]);
2308         // Mark as read one of the conversation messages.
2309         \core_message\api::mark_message_as_read($user1->id, $dbgm3, $now + 5);
2311         // Mark some of the conversations as muted by two users.
2312         \core_message\api::mute_conversation($user1->id, $iconversation1id);
2313         \core_message\api::mute_conversation($user1->id, $conversation1->id);
2314         \core_message\api::mute_conversation($user2->id, $conversation1->id);
2316         // There should be 2 contacts.
2317         $this->assertEquals(2, $DB->count_records('message_contacts'));
2319         // There should be 2 contact requests.
2320         $this->assertEquals(2, $DB->count_records('message_contact_requests'));
2322         // There should be 2 blocked users.
2323         $this->assertEquals(2, $DB->count_records('message_users_blocked'));
2325         // There should be 8 messages - 3 individual + 5 group.
2326         $this->assertEquals(8, $DB->count_records('messages'));
2328         // There should be 4 user actions - 3 for reading the message, 1 for deleting.
2329         $this->assertEquals(4, $DB->count_records('message_user_actions'));
2331         // There should be 3 muted conversations.
2332         $this->assertEquals(3, $DB->count_records('message_conversation_actions'));
2334         // There should be 4 conversations - 2 individual + 2 group + 5 self-conversations.
2335         $this->assertEquals(9, $DB->count_records('message_conversations'));
2337         // There should be 9 conversation members - (2 + 2) individual + (3 + 2) group + 5 self-conversations.
2338         $this->assertEquals(14, $DB->count_records('message_conversation_members'));
2340         // There should be 5 notifications (3 from create_notification and 2 from create_contact_request).
2341         $this->assertEquals(5, $DB->count_records('notifications'));
2343         // There should be 3 favourite conversations + 5 self-conversations.
2344         $this->assertEquals(8, $DB->count_records('favourite'));
2346         // Delete group conversations for all users in user context.
2347         provider::delete_conversations_for_all_users($user1context, $component, $itemtype);
2349         // No conversations should be removed, because they are in the course context.
2350         $this->assertEquals(2, $DB->count_records('message_contacts'));
2351         $this->assertEquals(2, $DB->count_records('message_contact_requests'));
2352         $this->assertEquals(2, $DB->count_records('message_users_blocked'));
2353         $this->assertEquals(8, $DB->count_records('messages'));
2354         $this->assertEquals(4, $DB->count_records('message_user_actions'));
2355         $this->assertEquals(3, $DB->count_records('message_conversation_actions'));
2356         $this->assertEquals(9, $DB->count_records('message_conversations'));
2357         $this->assertEquals(14, $DB->count_records('message_conversation_members'));
2358         $this->assertEquals(5, $DB->count_records('notifications'));
2359         $this->assertEquals(8, $DB->count_records('favourite'));
2361         // Delete individual conversations for all users in user context.
2362         provider::delete_conversations_for_all_users($user1context, '', '');
2364         // No conversations should be removed, because they are in the course context.
2365         $this->assertEquals(2, $DB->count_records('message_contacts'));
2366         $this->assertEquals(2, $DB->count_records('message_contact_requests'));
2367         $this->assertEquals(2, $DB->count_records('message_users_blocked'));
2368         $this->assertEquals(8, $DB->count_records('messages'));
2369         $this->assertEquals(4, $DB->count_records('message_user_actions'));
2370         $this->assertEquals(3, $DB->count_records('message_conversation_actions'));
2371         $this->assertEquals(9, $DB->count_records('message_conversations'));
2372         $this->assertEquals(14, $DB->count_records('message_conversation_members'));
2373         $this->assertEquals(5, $DB->count_records('notifications'));
2374         $this->assertEquals(8, $DB->count_records('favourite'));
2375     }
2377     /**
2378      * Test for provider::delete_conversations_for_user().
2379      */
2380     public function test_delete_conversations_for_user() {
2381         global $DB;
2383         $this->resetAfterTest();
2384         $this->setAdminUser();
2385         $now = time();
2386         $timeread = $now - DAYSECS;
2387         $systemcontext = \context_system::instance();
2388         $component = 'core_group';
2389         $itemtype = 'groups';
2391         // Create users to test with.
2392         $user1 = $this->getDataGenerator()->create_user();
2393         $user2 = $this->getDataGenerator()->create_user();
2394         $user3 = $this->getDataGenerator()->create_user();
2395         $user4 = $this->getDataGenerator()->create_user();
2396         $user5 = $this->getDataGenerator()->create_user();
2397         $user1context = \context_user::instance($user1->id);
2399         // Create contacts.
2400         \core_message\api::add_contact($user1->id, $user2->id);
2401         \core_message\api::add_contact($user2->id, $user3->id);
2403         // Create contact requests.
2404         \core_message\api::create_contact_request($user1->id, $user3->id);
2405         \core_message\api::create_contact_request($user2->id, $user4->id);
2407         // Block a user.
2408         \core_message\api::block_user($user1->id, $user3->id);
2409         \core_message\api::block_user($user3->id, $user4->id);
2411         // Create private messages.
2412         $pm1 = $this->create_message($user1->id, $user2->id, $now + (9 * DAYSECS), true);
2413         $pm2 = $this->create_message($user2->id, $user1->id, $now + (8 * DAYSECS), true);
2414         $pm3 = $this->create_message($user2->id, $user3->id, $now + (7 * DAYSECS));
2416         // Create notifications.
2417         $n1 = $this->create_notification($user1->id, $user2->id, $now + (9 * DAYSECS), $timeread);
2418         $n2 = $this->create_notification($user2->id, $user1->id, $now + (8 * DAYSECS));
2419         $n3 = $this->create_notification($user2->id, $user3->id, $now + (7 * DAYSECS));
2421         // Delete one of the messages.
2422         \core_message\api::delete_message($user1->id, $pm2);
2424         // Create course.
2425         $course1 = $this->getDataGenerator()->create_course();
2426         $course2 = $this->getDataGenerator()->create_course();
2427         $coursecontext1 = \context_course::instance($course1->id);
2428         $coursecontext2 = \context_course::instance($course2->id);
2430         // Enrol users to courses.
2431         $this->getDataGenerator()->enrol_user($user1->id, $course1->id);
2432         $this->getDataGenerator()->enrol_user($user2->id, $course1->id);
2433         $this->getDataGenerator()->enrol_user($user3->id, $course1->id);
2434         $this->getDataGenerator()->enrol_user($user1->id, $course2->id);
2436         // Create course groups with group messaging enabled.
2437         $group1a = $this->getDataGenerator()->create_group(array('courseid' => $course1->id, 'enablemessaging' => 1));
2439         // Add users to groups.
2440         $this->getDataGenerator()->create_group_member(array('groupid' => $group1a->id, 'userid' => $user1->id));
2441         $this->getDataGenerator()->create_group_member(array('groupid' => $group1a->id, 'userid' => $user2->id));
2442         $this->getDataGenerator()->create_group_member(array('groupid' => $group1a->id, 'userid' => $user3->id));
2444         // Get conversation.
2445         $iconversation1id = \core_message\api::get_conversation_between_users([$user1->id, $user2->id]);
2446         $conversation1 = \core_message\api::get_conversation_by_area(
2447             $component,
2448             $itemtype,
2449             $group1a->id,
2450             $coursecontext1->id
2451         );
2453         // Make favourite some conversations.
2454         \core_message\api::set_favourite_conversation($iconversation1id, $user1->id);
2455         \core_message\api::set_favourite_conversation($conversation1->id, $user1->id);
2456         \core_message\api::set_favourite_conversation($conversation1->id, $user2->id);
2458         // Send some messages to the conversation.
2459         $gm1 = testhelper::send_fake_message_to_conversation($user1, $conversation1->id, 'Message 1', $now + 1);
2460         $gm2 = testhelper::send_fake_message_to_conversation($user1, $conversation1->id, 'Message 2', $now + 2);
2461         $gm3 = testhelper::send_fake_message_to_conversation($user2, $conversation1->id, 'Message 3', $now + 3);
2463         $dbm3 = $DB->get_record('messages', ['id' => $gm3]);
2465         // Mark as read one of the conversation messages.
2466         \core_message\api::mark_message_as_read($user1->id, $dbm3, $now + 5);
2468         // Mark some of the conversations as muted by two users.
2469         \core_message\api::mute_conversation($user1->id, $iconversation1id);
2470         \core_message\api::mute_conversation($user1->id, $conversation1->id);
2471         \core_message\api::mute_conversation($user2->id, $conversation1->id);
2473         // There should be 2 contacts.
2474         $this->assertEquals(2, $DB->count_records('message_contacts'));
2476         // There should be 2 contact requests.
2477         $this->assertEquals(2, $DB->count_records('message_contact_requests'));
2479         // There should be 2 blocked users.
2480         $this->assertEquals(2, $DB->count_records('message_users_blocked'));
2482         // There should be 5 notifications.
2483         $this->assertEquals(5, $DB->count_records('notifications'));
2485         // There should be 6 messages.
2486         $this->assertEquals(6, $DB->count_records('messages'));
2488         // There should be 4 user actions - 3 for reading the message, one for deleting.
2489         $this->assertEquals(4, $DB->count_records('message_user_actions'));
2491         // There should be 3 users muting a conversation.
2492         $this->assertEquals(3, $DB->count_records('message_conversation_actions'));
2494         // There should be 3 conversations - 2 private + 1 group + 5 self-conversations.
2495         $this->assertEquals(8, $DB->count_records('message_conversations'));
2497         // There should be 7 conversation members - 2 + 2 private conversations + 3 group conversation + 5 self-conversations.
2498         $this->assertEquals(12, $DB->count_records('message_conversation_members'));
2499         $members = $DB->get_records('message_conversation_members', ['conversationid' => $conversation1->id]);
2500         $members = array_map(function($member) {
2501                 return $member->userid;
2502         }, $members);
2503         $this->assertContains($user1->id, $members);
2505         // There should be three favourite conversations + 5 self-conversations.
2506         $this->assertEquals(8, $DB->count_records('favourite'));
2508         // Delete group conversations for user1 in course1 and course2.
2509         $approvedcontextlist = new \core_privacy\tests\request\approved_contextlist($user1, 'core_message',
2510                 [$coursecontext1->id, $coursecontext2->id]);
2511         provider::delete_conversations_for_user($approvedcontextlist, $component, $itemtype);
2513         // There should be still 2 contacts.
2514         $this->assertEquals(2, $DB->count_records('message_contacts'));
2516         // There should be still 2 contact requests.
2517         $this->assertEquals(2, $DB->count_records('message_contact_requests'));
2519         // There should be still 2 blocked users.
2520         $this->assertEquals(2, $DB->count_records('message_users_blocked'));
2522         // There should be 2 muted conversation.
2523         $this->assertEquals(2, $DB->count_records('message_conversation_actions'));
2525         // There should be 3 notifications.
2526         $this->assertEquals(5, $DB->count_records('notifications'));
2528         // There should be 4 messages - 3 private + 1 group sent by user2.
2529         $this->assertEquals(4, $DB->count_records('messages'));
2530         $messages = array_keys($DB->get_records('messages'));
2531         $this->assertContains($pm1, $messages);
2532         $this->assertContains($pm2, $messages);
2533         $this->assertContains($pm3, $messages);
2534         $this->assertContains($gm3, $messages);
2536         // There should be 3 user actions - 2 for reading the message, one for deleting.
2537         $this->assertEquals(3, $DB->count_records('message_user_actions'));
2538         $useractions = $DB->get_records('message_user_actions');
2539         $useractions = array_map(function($action) {
2540                 return $action->messageid;
2541         }, $useractions);
2542         $this->assertNotContains($gm3, $useractions);
2544         // There should be still 3 conversations - 2 private + 1 group + 5 self-conversations.
2545         $this->assertEquals(8, $DB->count_records('message_conversations'));
2547         // There should be 6 conversation members - 2 + 2 private conversations + 2 group conversation + 5 self-conversations.
2548         $this->assertEquals(11, $DB->count_records('message_conversation_members'));
2549         $members = $DB->get_records('message_conversation_members', ['conversationid' => $conversation1->id]);
2550         $members = array_map(function($member) {
2551                 return $member->userid;
2552         }, $members);
2553         $this->assertNotContains($user1->id, $members);
2555         // Unset favourite self-conversations.
2556         $this->remove_user_self_conversation($user1->id);
2557         $this->remove_user_self_conversation($user2->id);
2558         $this->remove_user_self_conversation($user3->id);
2559         $this->remove_user_self_conversation($user4->id);
2560         $this->remove_user_self_conversation($user5->id);
2562         // There should be 2 favourite conversations - 2 group.
2563         $this->assertEquals(2, $DB->count_records('favourite'));
2564         $favourites = $DB->get_records('favourite');
2565         foreach ($favourites as $favourite) {
2566             if ($favourite->userid == $user1->id) {
2567                 $this->assertEquals($iconversation1id, $favourite->itemid);
2568             } else if ($favourite->userid == $user2->id) {
2569                 $this->assertEquals($conversation1->id, $favourite->itemid);
2570             }
2571         }
2572     }
2575     /**
2576      * Test for provider::delete_conversations_for_users().
2577      */
2578     public function test_delete_conversations_for_users() {
2579         global $DB;
2581         $this->resetAfterTest();
2582         $this->setAdminUser();
2583         $now = time();
2584         $timeread = $now - DAYSECS;
2585         $systemcontext = \context_system::instance();
2586         $component = 'core_group';
2587         $itemtype = 'groups';
2589         // Create users to test with.
2590         $user1 = $this->getDataGenerator()->create_user();
2591         $user2 = $this->getDataGenerator()->create_user();
2592         $user3 = $this->getDataGenerator()->create_user();
2593         $user4 = $this->getDataGenerator()->create_user();
2594         $user5 = $this->getDataGenerator()->create_user();
2595         $user1context = \context_user::instance($user1->id);
2597         // Create contacts.
2598         \core_message\api::add_contact($user1->id, $user2->id);
2599         \core_message\api::add_contact($user2->id, $user3->id);
2601         // Create contact requests.
2602         \core_message\api::create_contact_request($user1->id, $user3->id);
2603         \core_message\api::create_contact_request($user2->id, $user4->id);
2605         // Block a user.
2606         \core_message\api::block_user($user1->id, $user3->id);
2607         \core_message\api::block_user($user3->id, $user4->id);
2609         // Create private messages.
2610         $pm1 = $this->create_message($user1->id, $user2->id, $now + (9 * DAYSECS), true);
2611         $pm2 = $this->create_message($user2->id, $user1->id, $now + (8 * DAYSECS), true);
2612         $pm3 = $this->create_message($user2->id, $user3->id, $now + (7 * DAYSECS));
2614         // Create notifications.
2615         $n1 = $this->create_notification($user1->id, $user2->id, $now + (9 * DAYSECS), $timeread);
2616         $n2 = $this->create_notification($user2->id, $user1->id, $now + (8 * DAYSECS));
2617         $n3 = $this->create_notification($user2->id, $user3->id, $now + (7 * DAYSECS));
2619         // Delete one of the messages.
2620         \core_message\api::delete_message($user1->id, $pm2);
2622         // Create course.
2623         $course1 = $this->getDataGenerator()->create_course();
2624         $course2 = $this->getDataGenerator()->create_course();
2625         $coursecontext1 = \context_course::instance($course1->id);
2626         $coursecontext2 = \context_course::instance($course2->id);
2628         // Enrol users to courses.
2629         $this->getDataGenerator()->enrol_user($user1->id, $course1->id);
2630         $this->getDataGenerator()->enrol_user($user2->id, $course1->id);
2631         $this->getDataGenerator()->enrol_user($user3->id, $course1->id);
2632         $this->getDataGenerator()->enrol_user($user4->id, $course1->id);
2633         $this->getDataGenerator()->enrol_user($user1->id, $course2->id);
2635         // Create course groups with group messaging enabled.
2636         $group1a = $this->getDataGenerator()->create_group(array('courseid' => $course1->id, 'enablemessaging' => 1));
2637         $group2a = $this->getDataGenerator()->create_group(array('courseid' => $course2->id, 'enablemessaging' => 1));
2639         // Add users to groups.
2640         $this->getDataGenerator()->create_group_member(array('groupid' => $group1a->id, 'userid' => $user1->id));
2641         $this->getDataGenerator()->create_group_member(array('groupid' => $group1a->id, 'userid' => $user2->id));
2642         $this->getDataGenerator()->create_group_member(array('groupid' => $group1a->id, 'userid' => $user3->id));
2643         $this->getDataGenerator()->create_group_member(array('groupid' => $group1a->id, 'userid' => $user4->id));
2645         // Get conversation.
2646         $iconversation1id = \core_message\api::get_conversation_between_users([$user1->id, $user2->id]);
2647         $conversation1 = \core_message\api::get_conversation_by_area(
2648             $component,
2649             $itemtype,
2650             $group1a->id,
2651             $coursecontext1->id
2652         );
2654         // Make favourite some conversations.
2655         \core_message\api::set_favourite_conversation($iconversation1id, $user1->id);
2656         \core_message\api::set_favourite_conversation($conversation1->id, $user1->id);
2657         \core_message\api::set_favourite_conversation($conversation1->id, $user3->id);
2659         // Send some messages to the conversation.
2660         $gm1 = testhelper::send_fake_message_to_conversation($user1, $conversation1->id, 'Message 1', $now + 1);
2661         $gm2 = testhelper::send_fake_message_to_conversation($user2, $conversation1->id, 'Message 2', $now + 2);
2662         $gm3 = testhelper::send_fake_message_to_conversation($user3, $conversation1->id, 'Message 3', $now + 3);
2664         $dbm3 = $DB->get_record('messages', ['id' => $gm3]);
2666         // Mark as read one of the conversation messages.
2667         \core_message\api::mark_message_as_read($user1->id, $dbm3, $now + 5);
2669         // Mark some of the conversations as muted by two users.
2670         \core_message\api::mute_conversation($user1->id, $iconversation1id);
2671         \core_message\api::mute_conversation($user1->id, $conversation1->id);
2672         \core_message\api::mute_conversation($user2->id, $conversation1->id);
2674         // There should be 2 contacts.
2675         $this->assertEquals(2, $DB->count_records('message_contacts'));
2677         // There should be 2 contact requests.
2678         $this->assertEquals(2, $DB->count_records('message_contact_requests'));
2680         // There should be 2 blocked users.
2681         $this->assertEquals(2, $DB->count_records('message_users_blocked'));
2683         // There should be 5 notifications (3 from create_notification and 2 from create_contact_request).
2684         $this->assertEquals(5, $DB->count_records('notifications'));
2686         // There should be 6 messages.
2687         $this->assertEquals(6, $DB->count_records('messages'));
2689         // There should be 4 user actions - 3 for reading the message, one for deleting.
2690         $this->assertEquals(4, $DB->count_records('message_user_actions'));
2692         // There should be 3 muted conversation.
2693         $this->assertEquals(3, $DB->count_records('message_conversation_actions'));
2695         // There should be 3 conversations - 2 private + 2 group + 5 self-conversations.
2696         $this->assertEquals(9, $DB->count_records('message_conversations'));
2698         // There should be 8 conversation members - (2 + 2) private + 4 group + 5 self-conversations.
2699         $this->assertEquals(13, $DB->count_records('message_conversation_members'));
2700         $members = $DB->get_records('message_conversation_members', ['conversationid' => $conversation1->id]);
2701         $members = array_map(function($member) {
2702                 return $member->userid;
2703         }, $members);
2704         $this->assertContains($user1->id, $members);
2705         $this->assertContains($user4->id, $members);
2707         // There should be 3 favourite conversations + 5 self-conversations.
2708         $this->assertEquals(8, $DB->count_records('favourite'));
2710         // Delete group conversations for user1 and user2 in course2 context.
2711         $approveduserlist = new \core_privacy\local\request\approved_userlist($coursecontext2, 'core_message',
2712                 [$user1->id, $user2->id]);
2713         provider::delete_conversations_for_users($approveduserlist, $component, $itemtype);
2715         // There should be exactly the same content, because $user1 and $user2 don't belong to any group in course2).
2716         $this->assertEquals(2, $DB->count_records('message_contacts'));
2717         $this->assertEquals(2, $DB->count_records('message_contact_requests'));
2718         $this->assertEquals(2, $DB->count_records('message_users_blocked'));
2719         $this->assertEquals(5, $DB->count_records('notifications'));
2720         $this->assertEquals(6, $DB->count_records('messages'));
2721         $this->assertEquals(4, $DB->count_records('message_user_actions'));
2722         $this->assertEquals(3, $DB->count_records('message_conversation_actions'));
2723         $this->assertEquals(9, $DB->count_records('message_conversations'));
2724         $this->assertEquals(13, $DB->count_records('message_conversation_members'));
2725         $this->assertEquals(8, $DB->count_records('favourite'));
2727         // Delete group conversations for user4 in course1 context.
2728         $approveduserlist = new \core_privacy\local\request\approved_userlist($coursecontext1, 'core_message',
2729                 [$user4->id]);
2730         provider::delete_conversations_for_users($approveduserlist, $component, $itemtype);
2732         // There should be the same content except for the members (to remove user4 from the group1 in course1).
2733         $this->assertEquals(2, $DB->count_records('message_contacts'));
2734         $this->assertEquals(2, $DB->count_records('message_contact_requests'));
2735         $this->assertEquals(2, $DB->count_records('message_users_blocked'));
2736         $this->assertEquals(5, $DB->count_records('notifications'));
2737         $this->assertEquals(6, $DB->count_records('messages'));
2738         $this->assertEquals(4, $DB->count_records('message_user_actions'));
2739         $this->assertEquals(3, $DB->count_records('message_conversation_actions'));
2740         $this->assertEquals(9, $DB->count_records('message_conversations'));
2741         $this->assertEquals(8, $DB->count_records('favourite'));
2742         // There should be 7 conversation members - (2 + 2) private + 3 group + 5 self-conversations.
2743         $this->assertEquals(12, $DB->count_records('message_conversation_members'));
2745         // Delete group conversations for user1 and user2 in course1 context.
2746         $approveduserlist = new \core_privacy\local\request\approved_userlist($coursecontext1, 'core_message',
2747                 [$user1->id, $user2->id]);
2748         provider::delete_conversations_for_users($approveduserlist, $component, $itemtype);
2750         // There should be still 2 contacts.
2751         $this->assertEquals(2, $DB->count_records('message_contacts'));
2753         // There should be still 2 contact requests.
2754         $this->assertEquals(2, $DB->count_records('message_contact_requests'));
2756         // There should be still 2 blocked users.
2757         $this->assertEquals(2, $DB->count_records('message_users_blocked'));
2759         // There should be 5 notifications.
2760         $this->assertEquals(5, $DB->count_records('notifications'));
2762         // There should be 4 messages - 3 private + 1 group sent by user3.
2763         $this->assertEquals(4, $DB->count_records('messages'));
2764         $messages = array_keys($DB->get_records('messages'));
2765         $this->assertContains($pm1, $messages);
2766         $this->assertContains($pm2, $messages);
2767         $this->assertContains($pm3, $messages);
2768         $this->assertContains($gm3, $messages);
2770         // There should be 3 user actions - 2 for reading the message, one for deleting.
2771         $this->assertEquals(3, $DB->count_records('message_user_actions'));
2772         $useractions = $DB->get_records('message_user_actions');
2773         $useractions = array_map(function($action) {
2774                 return $action->messageid;
2775         }, $useractions);
2776         $this->assertNotContains($gm3, $useractions);
2778         // There should be 1 muted conversation.
2779         $this->assertEquals(1, $DB->count_records('message_conversation_actions'));
2781         // There should be still 4 conversations - 2 private + 2 group + 5 self-conversations.
2782         $this->assertEquals(9, $DB->count_records('message_conversations'));
2784         // There should be 5 conversation members - (2 + 2) private + 1 group + 5 self-conversations.
2785         $this->assertEquals(10, $DB->count_records('message_conversation_members'));
2786         $members = $DB->get_records('message_conversation_members', ['conversationid' => $conversation1->id]);
2787         $members = array_map(function($member) {
2788                 return $member->userid;
2789         }, $members);
2790         $this->assertNotContains($user1->id, $members);
2791         $this->assertNotContains($user2->id, $members);
2793         // Unset favourite self-conversations.
2794         $this->remove_user_self_conversation($user1->id);
2795         $this->remove_user_self_conversation($user2->id);
2796         $this->remove_user_self_conversation($user3->id);
2797         $this->remove_user_self_conversation($user4->id);
2798         $this->remove_user_self_conversation($user5->id);
2800         // There should be 2 favourite conversations - user1 individual + user3 group.
2801         $this->assertEquals(2, $DB->count_records('favourite'));
2802         $favourites = $DB->get_records('favourite');
2803         foreach ($favourites as $favourite) {
2804             if ($favourite->userid == $user1->id) {
2805                 $this->assertEquals($iconversation1id, $favourite->itemid);
2806             } else if ($favourite->userid == $user3->id) {
2807                 $this->assertEquals($conversation1->id, $favourite->itemid);
2808             }
2809         }
2810     }
2812     /**
2813      * Creates a message to be used for testing.
2814      *
2815      * @param int $useridfrom The user id from
2816      * @param int $useridto The user id to
2817      * @param int $timecreated
2818      * @param bool $read Do we want to mark the message as read?
2819      * @return int The id of the message
2820      * @throws dml_exception
2821      */
2822     private function create_message(int $useridfrom, int $useridto, int $timecreated = null, bool $read = false) {
2823         global $DB;
2825         static $i = 1;
2827         if (is_null($timecreated)) {
2828             $timecreated = time();
2829         }
2831         if (!$conversationid = \core_message\api::get_conversation_between_users([$useridfrom, $useridto])) {
2832             $conversation = \core_message\api::create_conversation(
2833                 \core_message\api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL,
2834                 [
2835                     $useridfrom,
2836                     $useridto
2837                 ]
2838             );
2839             $conversationid = $conversation->id;
2840         }
2842         // Ok, send the message.
2843         $record = new stdClass();
2844         $record->useridfrom = $useridfrom;
2845         $record->conversationid = $conversationid;
2846         $record->subject = 'No subject';
2847         $record->fullmessage = 'A rad message ' . $i;
2848         $record->smallmessage = 'A rad message ' . $i;
2849         $record->timecreated = $timecreated;
2850         $record->customdata = json_encode(['akey' => 'avalue']);
2852         $i++;
2854         $record->id = $DB->insert_record('messages', $record);
2856         if ($read) {
2857             \core_message\api::mark_message_as_read($useridto, $record);
2858         }
2860         return $record->id;
2861     }
2863     /**
2864      * Creates a notification to be used for testing.
2865      *
2866      * @param int $useridfrom The user id from
2867      * @param int $useridto The user id to
2868      * @param int|null $timecreated The time the notification was created
2869      * @param int|null $timeread The time the notification was read, null if it hasn't been.
2870      * @return int The id of the notification
2871      * @throws dml_exception
2872      */
2873     private function create_notification(int $useridfrom, int $useridto, int $timecreated = null, int $timeread = null) {
2874         global $DB;
2876         static $i = 1;
2878         if (is_null($timecreated)) {
2879             $timecreated = time();
2880         }
2882         $record = new stdClass();
2883         $record->useridfrom = $useridfrom;
2884         $record->useridto = $useridto;
2885         $record->subject = 'No subject';
2886         $record->fullmessage = 'Some rad notification ' . $i;
2887         $record->smallmessage = 'Yo homie, you got some stuff to do, yolo. ' . $i;
2888         $record->timeread = $timeread;
2889         $record->timecreated = $timecreated;
2890         $record->customdata = json_encode(['akey' => 'avalue']);
2892         $i++;
2894         return $DB->insert_record('notifications', $record);
2895     }
2897     /**
2898      * Comparison function for sorting messages.
2899      *
2900      * @param   \stdClass $a
2901      * @param   \stdClass $b
2902      * @return  bool
2903      */
2904     protected static function sort_messages($a, $b) {
2905         return $a->message > $b->message;
2906     }
2908     /**
2909      * Comparison function for sorting contacts.
2910      *
2911      * @param   \stdClass $a
2912      * @param   \stdClass $b
2913      * @return  bool
2914      */
2915     protected static function sort_contacts($a, $b) {
2916         return $a->contact > $b->contact;
2917     }
2919     /**
2920      * Function to unset favourite and delete all conversation data for a user's self-conversation.
2921      *
2922      * @param int $userid The user id
2923      * @return  void
2924      * @throws moodle_exception
2925      */
2926     protected static function remove_user_self_conversation(int $userid) {
2927         $selfconversation = \core_message\api::get_self_conversation($userid);
2928         \core_message\api::unset_favourite_conversation($selfconversation->id, $userid);
2929         \core_message\api::delete_all_conversation_data($selfconversation->id);
2930     }