MDL-60680 notifications: Support privacy API
[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();
214         $contextlist = provider::get_contexts_for_userid($user->id);
215         $this->assertEmpty($contextlist);
216     }
218     /**
219      * Test for provider::get_contexts_for_userid() when there is a private message between users.
220      */
221     public function test_get_contexts_for_userid_with_private_messages() {
222         $this->resetAfterTest();
224         $user1 = $this->getDataGenerator()->create_user();
225         $user2 = $this->getDataGenerator()->create_user();
226         $user3 = $this->getDataGenerator()->create_user();
228         // Test nothing is found before group conversations is created or message is sent.
229         $contextlist = provider::get_contexts_for_userid($user1->id);
230         $this->assertCount(0, $contextlist);
231         $contextlist = provider::get_contexts_for_userid($user2->id);
232         $this->assertCount(0, $contextlist);
234         // Send some private messages.
235         $pm1id = $this->create_message($user1->id, $user2->id, time() - (9 * DAYSECS));
237         // Test for the sender (user1).
238         $contextlist = provider::get_contexts_for_userid($user1->id);
239         $this->assertCount(1, $contextlist);
240         $contextforuser = $contextlist->current();
241         $this->assertEquals(
242                 \context_user::instance($user1->id)->id,
243                 $contextforuser->id);
245         // Test for the receiver (user2).
246         $contextlist = provider::get_contexts_for_userid($user2->id);
247         $this->assertCount(1, $contextlist);
248         $contextforuser = $contextlist->current();
249         $this->assertEquals(
250                 \context_user::instance($user2->id)->id,
251                 $contextforuser->id);
253         // Test for user3 (no private messages).
254         $contextlist = provider::get_contexts_for_userid($user3->id);
255         $this->assertCount(0, $contextlist);
256     }
258     /**
259      * Test for provider::get_contexts_for_userid() when there is several messages (private and group).
260      */
261     public function test_get_contexts_for_userid_with_messages() {
262         $this->resetAfterTest();
263         $this->setAdminUser();
265         $user1 = $this->getDataGenerator()->create_user();
266         $user2 = $this->getDataGenerator()->create_user();
267         $user3 = $this->getDataGenerator()->create_user();
268         $user4 = $this->getDataGenerator()->create_user();
270         // Test nothing is found before group conversations is created or message is sent.
271         $contextlist = provider::get_contexts_for_userid($user1->id);
272         $this->assertCount(0, $contextlist);
273         $contextlist = provider::get_contexts_for_userid($user2->id);
274         $this->assertCount(0, $contextlist);
276         // Create course.
277         $course1 = $this->getDataGenerator()->create_course();
278         $coursecontext1 = context_course::instance($course1->id);
280         // Enrol users to courses.
281         $this->getDataGenerator()->enrol_user($user1->id, $course1->id);
282         $this->getDataGenerator()->enrol_user($user2->id, $course1->id);
283         $this->getDataGenerator()->enrol_user($user3->id, $course1->id);
285         // Create groups (only one with enablemessaging = 1).
286         $group1a = $this->getDataGenerator()->create_group(array('courseid' => $course1->id, 'enablemessaging' => 1));
288         // Add users to groups.
289         $this->getDataGenerator()->create_group_member(array('groupid' => $group1a->id, 'userid' => $user1->id));
290         $this->getDataGenerator()->create_group_member(array('groupid' => $group1a->id, 'userid' => $user2->id));
291         $this->getDataGenerator()->create_group_member(array('groupid' => $group1a->id, 'userid' => $user3->id));
293         // Get conversation.
294         $component = 'core_group';
295         $itemtype = 'groups';
296         $conversation1 = \core_message\api::get_conversation_by_area(
297             $component,
298             $itemtype,
299             $group1a->id,
300             $coursecontext1->id
301         );
303         // Send some messages to the group conversation.
304         $now = time();
305         $m1id = testhelper::send_fake_message_to_conversation($user1, $conversation1->id, 'Message 1', $now + 1);
306         $m2id = testhelper::send_fake_message_to_conversation($user1, $conversation1->id, 'Message 2', $now + 2);
307         $m3id = testhelper::send_fake_message_to_conversation($user2, $conversation1->id, 'Message 3', $now + 3);
309         // Test for user1 (although is member of the conversation, hasn't any private message).
310         $contextlist = provider::get_contexts_for_userid($user1->id);
311         $this->assertCount(0, $contextlist);
313         // Test for user2 (although is member of the conversation, hasn't any private message).
314         $contextlist = provider::get_contexts_for_userid($user2->id);
315         $this->assertCount(0, $contextlist);
317         // Test for user3 (although is member of the conversation, hasn't any private message).
318         $contextlist = provider::get_contexts_for_userid($user3->id);
319         $this->assertCount(0, $contextlist);
321         // Test for user4 (doesn't belong to the conversation).
322         $contextlist = provider::get_contexts_for_userid($user4->id);
323         $this->assertCount(0, $contextlist);
325         // Send some private messages.
326         $pm1id = $this->create_message($user1->id, $user2->id, time() - (9 * DAYSECS));
328         // Test user1 now has the user context because of the private message.
329         $contextlist = provider::get_contexts_for_userid($user1->id);
330         $this->assertCount(1, $contextlist);
331         $contextforuser = $contextlist->current();
332         $this->assertEquals(
333                 \context_user::instance($user1->id)->id,
334                 $contextforuser->id);
336         // Test user2 now has the user context because of the private message.
337         $contextlist = provider::get_contexts_for_userid($user2->id);
338         $this->assertCount(1, $contextlist);
339         $contextforuser = $contextlist->current();
340         $this->assertEquals(
341                 \context_user::instance($user2->id)->id,
342                 $contextforuser->id);
344         // Test for user3 (although is member of the conversation, hasn't still any private message).
345         $contextlist = provider::get_contexts_for_userid($user3->id);
346         $this->assertCount(0, $contextlist);
348         // Test for user4 (doesn't belong to the conversation and hasn't any private message).
349         $contextlist = provider::get_contexts_for_userid($user4->id);
350         $this->assertCount(0, $contextlist);
351     }
353     /**
354      * Test for provider::get_contexts_for_userid() when there is a notification between users.
355      */
356     public function test_get_contexts_for_userid_with_notification() {
357         $this->resetAfterTest();
359         $user1 = $this->getDataGenerator()->create_user();
360         $user2 = $this->getDataGenerator()->create_user();
362         // Test nothing is found before notification is created.
363         $contextlist = provider::get_contexts_for_userid($user1->id);
364         $this->assertCount(0, $contextlist);
365         $contextlist = provider::get_contexts_for_userid($user2->id);
366         $this->assertCount(0, $contextlist);
368         $this->create_notification($user1->id, $user2->id, time() - (9 * DAYSECS));
370         // Test for the sender.
371         $contextlist = provider::get_contexts_for_userid($user1->id);
372         $this->assertCount(1, $contextlist);
373         $contextforuser = $contextlist->current();
374         $this->assertEquals(
375                 context_user::instance($user1->id)->id,
376                 $contextforuser->id);
378         // Test for the receiver.
379         $contextlist = provider::get_contexts_for_userid($user2->id);
380         $this->assertCount(1, $contextlist);
381         $contextforuser = $contextlist->current();
382         $this->assertEquals(
383                 context_user::instance($user2->id)->id,
384                 $contextforuser->id);
385     }
387     /**
388      * Test for provider::get_contexts_for_userid() when a users has a contact.
389      */
390     public function test_get_contexts_for_userid_with_contact() {
391         $this->resetAfterTest();
393         $user1 = $this->getDataGenerator()->create_user();
394         $user2 = $this->getDataGenerator()->create_user();
396         // Test nothing is found before contact is created.
397         $contextlist = provider::get_contexts_for_userid($user1->id);
398         $this->assertCount(0, $contextlist);
399         $contextlist = provider::get_contexts_for_userid($user2->id);
400         $this->assertCount(0, $contextlist);
402         \core_message\api::add_contact($user1->id, $user2->id);
404         // Test for the user adding the contact.
405         $contextlist = provider::get_contexts_for_userid($user1->id);
406         $this->assertCount(1, $contextlist);
407         $contextforuser = $contextlist->current();
408         $this->assertEquals(
409                 context_user::instance($user1->id)->id,
410                 $contextforuser->id);
412         // Test for the user who is the contact.
413         $contextlist = provider::get_contexts_for_userid($user2->id);
414         $this->assertCount(1, $contextlist);
415         $contextforuser = $contextlist->current();
416         $this->assertEquals(
417                 context_user::instance($user2->id)->id,
418                 $contextforuser->id);
419     }
421     /**
422      * Test for provider::get_contexts_for_userid() when a user makes a contact request.
423      */
424     public function test_get_contexts_for_userid_with_contact_request() {
425         $this->resetAfterTest();
427         $user1 = $this->getDataGenerator()->create_user();
428         $user2 = $this->getDataGenerator()->create_user();
430         // Test nothing is found before request is created.
431         $contextlist = provider::get_contexts_for_userid($user1->id);
432         $this->assertCount(0, $contextlist);
433         $contextlist = provider::get_contexts_for_userid($user2->id);
434         $this->assertCount(0, $contextlist);
436         \core_message\api::create_contact_request($user1->id, $user2->id);
438         // Test for the user requesting the contact.
439         $contextlist = provider::get_contexts_for_userid($user1->id);
440         $this->assertCount(1, $contextlist);
441         $contextforuser = $contextlist->current();
442         $this->assertEquals(
443                 context_user::instance($user1->id)->id,
444                 $contextforuser->id);
446         // Test for the user receiving the contact request.
447         $contextlist = provider::get_contexts_for_userid($user2->id);
448         $this->assertCount(1, $contextlist);
449         $contextforuser = $contextlist->current();
450         $this->assertEquals(
451                 context_user::instance($user2->id)->id,
452                 $contextforuser->id);
453     }
455     /**
456      * Test for provider::get_contexts_for_userid() when a user is blocked.
457      */
458     public function test_get_contexts_for_userid_with_blocked_contact() {
459         $this->resetAfterTest();
461         $user1 = $this->getDataGenerator()->create_user();
462         $user2 = $this->getDataGenerator()->create_user();
464         // Test nothing is found before user is blocked.
465         $contextlist = provider::get_contexts_for_userid($user1->id);
466         $this->assertCount(0, $contextlist);
467         $contextlist = provider::get_contexts_for_userid($user2->id);
468         $this->assertCount(0, $contextlist);
470         \core_message\api::block_user($user1->id, $user2->id);
472         // Test for the blocking user.
473         $contextlist = provider::get_contexts_for_userid($user1->id);
474         $this->assertCount(1, $contextlist);
475         $contextforuser = $contextlist->current();
476         $this->assertEquals(
477                 context_user::instance($user1->id)->id,
478                 $contextforuser->id);
480         // Test for the user who is blocked.
481         $contextlist = provider::get_contexts_for_userid($user2->id);
482         $this->assertCount(1, $contextlist);
483         $contextforuser = $contextlist->current();
484         $this->assertEquals(
485                 context_user::instance($user2->id)->id,
486                 $contextforuser->id);
487     }
489     /**
490      * Test for provider::export_user_data().
491      */
492     public function test_export_for_context_with_contacts() {
493         $this->resetAfterTest();
495         // Create users to test with.
496         $user1 = $this->getDataGenerator()->create_user();
497         $user2 = $this->getDataGenerator()->create_user();
498         $user3 = $this->getDataGenerator()->create_user();
499         $user4 = $this->getDataGenerator()->create_user();
501         \core_message\api::add_contact($user1->id, $user2->id);
502         \core_message\api::add_contact($user1->id, $user3->id);
503         \core_message\api::add_contact($user1->id, $user4->id);
505         $user1context = context_user::instance($user1->id);
507         $this->export_context_data_for_user($user1->id, $user1context, 'core_message');
509         $writer = writer::with_context($user1context);
511         $contacts = (array) $writer->get_data([get_string('contacts', 'core_message')]);
512         usort($contacts, ['static', 'sort_contacts']);
514         $this->assertCount(3, $contacts);
516         $contact1 = array_shift($contacts);
517         $this->assertEquals($user2->id, $contact1->contact);
519         $contact2 = array_shift($contacts);
520         $this->assertEquals($user3->id, $contact2->contact);
522         $contact3 = array_shift($contacts);
523         $this->assertEquals($user4->id, $contact3->contact);
524     }
526     /**
527      * Test for provider::export_user_data().
528      */
529     public function test_export_for_context_with_contact_requests() {
530         $this->resetAfterTest();
532         // Create users to test with.
533         $user1 = $this->getDataGenerator()->create_user();
534         $user2 = $this->getDataGenerator()->create_user();
535         $user3 = $this->getDataGenerator()->create_user();
536         $user4 = $this->getDataGenerator()->create_user();
538         \core_message\api::create_contact_request($user1->id, $user2->id);
539         \core_message\api::create_contact_request($user3->id, $user1->id);
540         \core_message\api::create_contact_request($user1->id, $user4->id);
542         $user1context = context_user::instance($user1->id);
544         $this->export_context_data_for_user($user1->id, $user1context, 'core_message');
546         $writer = writer::with_context($user1context);
548         $contactrequests = (array) $writer->get_data([get_string('contactrequests', 'core_message')]);
550         $this->assertCount(3, $contactrequests);
552         $contactrequest1 = array_shift($contactrequests);
553         $this->assertEquals($user2->id, $contactrequest1->contactrequest);
554         $this->assertEquals(get_string('yes'), $contactrequest1->maderequest);
556         $contactrequest2 = array_shift($contactrequests);
557         $this->assertEquals($user3->id, $contactrequest2->contactrequest);
558         $this->assertEquals(get_string('no'), $contactrequest2->maderequest);
560         $contactrequest3 = array_shift($contactrequests);
561         $this->assertEquals($user4->id, $contactrequest3->contactrequest);
562         $this->assertEquals(get_string('yes'), $contactrequest3->maderequest);
563     }
565     /**
566      * Test for provider::export_user_data().
567      */
568     public function test_export_for_context_with_blocked_users() {
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         \core_message\api::block_user($user1->id, $user2->id);
578         \core_message\api::block_user($user1->id, $user3->id);
579         \core_message\api::block_user($user1->id, $user4->id);
581         $user1context = context_user::instance($user1->id);
583         $this->export_context_data_for_user($user1->id, $user1context, 'core_message');
585         $writer = writer::with_context($user1context);
587         $blockedusers = (array) $writer->get_data([get_string('blockedusers', 'core_message')]);
589         $this->assertCount(3, $blockedusers);
591         $blockeduser1 = array_shift($blockedusers);
592         $this->assertEquals($user2->id, $blockeduser1->blockeduser);
594         $blockeduser2 = array_shift($blockedusers);
595         $this->assertEquals($user3->id, $blockeduser2->blockeduser);
597         $blockeduser3 = array_shift($blockedusers);
598         $this->assertEquals($user4->id, $blockeduser3->blockeduser);
599     }
601     /**
602      * Test for provider::export_user_data().
603      */
604     public function test_export_for_context_with_private_messages() {
605         global $DB;
607         $this->resetAfterTest();
609         // Create users to test with.
610         $user1 = $this->getDataGenerator()->create_user();
611         $user2 = $this->getDataGenerator()->create_user();
612         $user3 = $this->getDataGenerator()->create_user();
614         $now = time();
616         // Send messages from user 1 to user 2.
617         $m1 = $this->create_message($user1->id, $user2->id, $now - (9 * DAYSECS), true);
618         $m2 = $this->create_message($user2->id, $user1->id, $now - (8 * DAYSECS));
619         $m3 = $this->create_message($user1->id, $user2->id, $now - (7 * DAYSECS));
621         // Send messages from user 3 to user 1.
622         $m4 = $this->create_message($user3->id, $user1->id, $now - (6 * DAYSECS), true);
623         $m5 = $this->create_message($user1->id, $user3->id, $now - (5 * DAYSECS));
624         $m6 = $this->create_message($user3->id, $user1->id, $now - (4 * DAYSECS));
626         // Send messages from user 3 to user 2 - these should not be included in the export.
627         $m7 = $this->create_message($user3->id, $user2->id, $now - (3 * DAYSECS), true);
628         $m8 = $this->create_message($user2->id, $user3->id, $now - (2 * DAYSECS));
629         $m9 = $this->create_message($user3->id, $user2->id, $now - (1 * DAYSECS));
631         // Mark message 2 and 5 as deleted.
632         \core_message\api::delete_message($user1->id, $m2);
633         \core_message\api::delete_message($user1->id, $m5);
635         $user1context = context_user::instance($user1->id);
637         $this->export_context_data_for_user($user1->id, $user1context, 'core_message');
639         $writer = writer::with_context($user1context);
641         $this->assertTrue($writer->has_any_data());
643         // Confirm the messages with user 2 are correct.
644         $messages = (array) $writer->get_data([get_string('messages', 'core_message'), $user2->id]);
645         $this->assertCount(3, $messages);
647         $dbm1 = $DB->get_record('messages', ['id' => $m1]);
648         $dbm2 = $DB->get_record('messages', ['id' => $m2]);
649         $dbm3 = $DB->get_record('messages', ['id' => $m3]);
651         usort($messages, ['static', 'sort_messages']);
652         $m1 = array_shift($messages);
653         $m2 = array_shift($messages);
654         $m3 = array_shift($messages);
656         $this->assertEquals(get_string('yes'), $m1->issender);
657         $this->assertEquals(message_format_message_text($dbm1), $m1->message);
658         $this->assertEquals(transform::datetime($now - (9 * DAYSECS)), $m1->timecreated);
659         $this->assertEquals('-', $m1->timeread);
660         $this->assertArrayNotHasKey('timedeleted', (array) $m1);
662         $this->assertEquals(get_string('no'), $m2->issender);
663         $this->assertEquals(message_format_message_text($dbm2), $m2->message);
664         $this->assertEquals(transform::datetime($now - (8 * DAYSECS)), $m2->timecreated);
665         $this->assertEquals('-', $m2->timeread);
666         $this->assertArrayHasKey('timedeleted', (array) $m2);
668         $this->assertEquals(get_string('yes'), $m3->issender);
669         $this->assertEquals(message_format_message_text($dbm3), $m3->message);
670         $this->assertEquals(transform::datetime($now - (7 * DAYSECS)), $m3->timecreated);
671         $this->assertEquals('-', $m3->timeread);
673         // Confirm the messages with user 3 are correct.
674         $messages = (array) $writer->get_data([get_string('messages', 'core_message'), $user3->id]);
675         $this->assertCount(3, $messages);
677         $dbm4 = $DB->get_record('messages', ['id' => $m4]);
678         $dbm5 = $DB->get_record('messages', ['id' => $m5]);
679         $dbm6 = $DB->get_record('messages', ['id' => $m6]);
681         usort($messages, ['static', 'sort_messages']);
682         $m4 = array_shift($messages);
683         $m5 = array_shift($messages);
684         $m6 = array_shift($messages);
686         $this->assertEquals(get_string('no'), $m4->issender);
687         $this->assertEquals(message_format_message_text($dbm4), $m4->message);
688         $this->assertEquals(transform::datetime($now - (6 * DAYSECS)), $m4->timecreated);
689         $this->assertNotEquals('-', $m4->timeread);
690         $this->assertArrayNotHasKey('timedeleted', (array) $m4);
692         $this->assertEquals(get_string('yes'), $m5->issender);
693         $this->assertEquals(message_format_message_text($dbm5), $m5->message);
694         $this->assertEquals(transform::datetime($now - (5 * DAYSECS)), $m5->timecreated);
695         $this->assertEquals('-', $m5->timeread);
696         $this->assertArrayHasKey('timedeleted', (array) $m5);
698         $this->assertEquals(get_string('no'), $m6->issender);
699         $this->assertEquals(message_format_message_text($dbm6), $m6->message);
700         $this->assertEquals(transform::datetime($now - (4 * DAYSECS)), $m6->timecreated);
701         $this->assertEquals('-', $m6->timeread);
702     }
704     /**
705      * Test for provider::export_user_data().
706      */
707     public function test_export_for_context_with_messages() {
708         global $DB;
710         $this->resetAfterTest();
711         $this->setAdminUser();
712         $now = time();
713         $systemcontext = \context_system::instance();
715         // Create users to test with.
716         $user1 = $this->getDataGenerator()->create_user();
717         $user2 = $this->getDataGenerator()->create_user();
718         $user3 = $this->getDataGenerator()->create_user();
719         $user1context = \context_user::instance($user1->id);
721         $course1 = $this->getDataGenerator()->create_course();
722         $course2 = $this->getDataGenerator()->create_course();
723         $coursecontext1 = \context_course::instance($course1->id);
724         $coursecontext2 = \context_course::instance($course2->id);
726         // Enrol users to courses.
727         $this->getDataGenerator()->enrol_user($user1->id, $course1->id);
728         $this->getDataGenerator()->enrol_user($user2->id, $course1->id);
729         $this->getDataGenerator()->enrol_user($user3->id, $course1->id);
730         $this->getDataGenerator()->enrol_user($user1->id, $course2->id);
732         // Create course groups with group messaging enabled.
733         $group1a = $this->getDataGenerator()->create_group(array('courseid' => $course1->id, 'enablemessaging' => 1));
734         $group2a = $this->getDataGenerator()->create_group(array('courseid' => $course2->id, 'enablemessaging' => 1));
736         // Add users to groups.
737         $this->getDataGenerator()->create_group_member(array('groupid' => $group1a->id, 'userid' => $user1->id));
738         $this->getDataGenerator()->create_group_member(array('groupid' => $group1a->id, 'userid' => $user2->id));
739         $this->getDataGenerator()->create_group_member(array('groupid' => $group1a->id, 'userid' => $user3->id));
740         $this->getDataGenerator()->create_group_member(array('groupid' => $group2a->id, 'userid' => $user1->id));
742         // Get conversation.
743         $component = 'core_group';
744         $itemtype = 'groups';
745         $conversation = \core_message\api::get_conversation_by_area(
746             $component,
747             $itemtype,
748             $group1a->id,
749             $coursecontext1->id
750         );
752         // Send some private messages between user 1 and user 2.
753         $pm1id = $this->create_message($user1->id, $user2->id, $now);
755         $dbpm1 = $DB->get_record('messages', ['id' => $pm1id]);
757         // Send some messages to the conversation.
758         $m1 = testhelper::send_fake_message_to_conversation($user1, $conversation->id, 'Message 1', $now + 1);
759         $m2 = testhelper::send_fake_message_to_conversation($user1, $conversation->id, 'Message 2', $now + 2);
760         $m3 = testhelper::send_fake_message_to_conversation($user2, $conversation->id, 'Message 3', $now + 3);
762         $dbm1 = $DB->get_record('messages', ['id' => $m1]);
763         $dbm2 = $DB->get_record('messages', ['id' => $m2]);
764         $dbm3 = $DB->get_record('messages', ['id' => $m3]);
766         // Mark as read and delete some messages.
767         \core_message\api::mark_message_as_read($user2->id, $dbm1);
768         \core_message\api::delete_message($user1->id, $m2);
770         // Confirm the user1 has no data in any course context because private messages are related to user context.
771         $this->export_context_data_for_user($user1->id, $coursecontext2, 'core_message');
773         // Check that system context hasn't been exported.
774         $writer = writer::with_context($systemcontext);
775         $this->assertFalse($writer->has_any_data());
777         // Check that course1 context hasn't been exported.
778         $writer = writer::with_context($coursecontext1);
779         $this->assertFalse($writer->has_any_data());
781         // Check that course2 context has been exported and contains data.
782         $writer = writer::with_context($coursecontext2);
783         $this->assertFalse($writer->has_any_data());
785         // Confirm the user1 has only private messages in the user context.
786         $this->export_context_data_for_user($user1->id, $user1context, 'core_message');
787         $writer = writer::with_context($user1context);
788         $this->assertTrue($writer->has_any_data());
790         // Confirm the messages with user 2 are correct.
791         $messages = (array) $writer->get_data([get_string('messages', 'core_message'), $user2->id]);
792         $this->assertCount(1, $messages);
793         $m1 = reset($messages);
795         $this->assertEquals(get_string('yes'), $m1->issender);
796         $this->assertEquals(message_format_message_text($dbpm1), $m1->message);
797         $this->assertEquals(transform::datetime($now), $m1->timecreated);
798         $this->assertEquals('-', $m1->timeread);
799         $this->assertArrayNotHasKey('timedeleted', (array) $m1);
801         // Confirm the messages with user 3 are correct.
802         $messages = (array) $writer->get_data([get_string('messages', 'core_message'), fullname($user3)]);
803         $this->assertCount(0, $messages);
804     }
806     /**
807      * Test for provider::export_user_data().
808      */
809     public function test_export_for_context_with_notifications() {
810         $this->resetAfterTest();
812         // Create users to test with.
813         $user1 = $this->getDataGenerator()->create_user();
814         $user2 = $this->getDataGenerator()->create_user();
815         $user3 = $this->getDataGenerator()->create_user();
817         $now = time();
818         $timeread = $now - DAYSECS;
820         // Send notifications from user 1 to user 2.
821         $this->create_notification($user1->id, $user2->id, $now + (9 * DAYSECS), $timeread);
822         $this->create_notification($user2->id, $user1->id, $now + (8 * DAYSECS));
823         $this->create_notification($user1->id, $user2->id, $now + (7 * DAYSECS));
825         // Send notifications from user 3 to user 1.
826         $this->create_notification($user3->id, $user1->id, $now + (6 * DAYSECS), $timeread);
827         $this->create_notification($user1->id, $user3->id, $now + (5 * DAYSECS));
828         $this->create_notification($user3->id, $user1->id, $now + (4 * DAYSECS));
830         // Send notifications from user 3 to user 2 - should not be part of the export.
831         $this->create_notification($user3->id, $user2->id, $now + (3 * DAYSECS), $timeread);
832         $this->create_notification($user2->id, $user3->id, $now + (2 * DAYSECS));
833         $this->create_notification($user3->id, $user2->id, $now + (1 * DAYSECS));
835         $user1context = context_user::instance($user1->id);
837         $this->export_context_data_for_user($user1->id, $user1context, 'core_message');
839         $writer = writer::with_context($user1context);
841         $this->assertTrue($writer->has_any_data());
843         // Confirm the notifications.
844         $notifications = (array) $writer->get_data([get_string('notifications', 'core_message')]);
846         $this->assertCount(6, $notifications);
847     }
849     /**
850      * Test for provider::delete_data_for_all_users_in_context().
851      */
852     public function test_delete_data_for_all_users_in_context() {
853         global $DB;
855         $this->resetAfterTest();
857         // Create users to test with.
858         $user1 = $this->getDataGenerator()->create_user();
859         $user2 = $this->getDataGenerator()->create_user();
860         $user3 = $this->getDataGenerator()->create_user();
861         $user4 = $this->getDataGenerator()->create_user();
862         $user5 = $this->getDataGenerator()->create_user();
864         $now = time();
865         $timeread = $now - DAYSECS;
867         $user1context = context_user::instance($user1->id);
869         // Create contacts.
870         \core_message\api::add_contact($user1->id, $user2->id);
871         \core_message\api::add_contact($user2->id, $user3->id);
873         // Create contact requests.
874         \core_message\api::create_contact_request($user1->id, $user3->id);
875         \core_message\api::create_contact_request($user2->id, $user4->id);
877         // Block a user.
878         \core_message\api::block_user($user1->id, $user3->id);
879         \core_message\api::block_user($user3->id, $user4->id);
881         // Create messages.
882         $m1 = $this->create_message($user1->id, $user2->id, $now + (9 * DAYSECS), true);
883         $m2 = $this->create_message($user2->id, $user1->id, $now + (8 * DAYSECS));
884         $m3 = $this->create_message($user2->id, $user3->id, $now + (7 * DAYSECS));
886         // Create notifications.
887         $n1 = $this->create_notification($user1->id, $user2->id, $now + (9 * DAYSECS), $timeread);
888         $n2 = $this->create_notification($user2->id, $user1->id, $now + (8 * DAYSECS));
889         $n3 = $this->create_notification($user2->id, $user3->id, $now + (7 * DAYSECS));
891         // Delete one of the messages.
892         \core_message\api::delete_message($user1->id, $m2);
894         // There should be 2 contacts.
895         $this->assertEquals(2, $DB->count_records('message_contacts'));
897         // There should be 2 contact requests.
898         $this->assertEquals(2, $DB->count_records('message_contact_requests'));
900         // There should be 2 blocked users.
901         $this->assertEquals(2, $DB->count_records('message_users_blocked'));
903         // There should be 3 messages.
904         $this->assertEquals(3, $DB->count_records('messages'));
906         // There should be 2 user actions - one for reading the message, one for deleting.
907         $this->assertEquals(2, $DB->count_records('message_user_actions'));
909         // There should be 4 conversation members.
910         $this->assertEquals(4, $DB->count_records('message_conversation_members'));
912         // There should be 5 notifications (3 from create_notification and 2 from create_contact_request).
913         $this->assertEquals(5, $DB->count_records('notifications'));
915         provider::delete_data_for_all_users_in_context($user1context);
917         // Confirm there is only 1 contact left.
918         $this->assertEquals(1, $DB->count_records('message_contacts'));
919         // And it is not related to user1.
920         $this->assertEquals(0,
921                 $DB->count_records_select('message_contacts', 'userid = ? OR contactid = ?', [$user1->id, $user1->id]));
923         // Confirm there is only 1 contact request left.
924         $this->assertEquals(1, $DB->count_records('message_contact_requests'));
925         // And it is not related to user1.
926         $this->assertEquals(0,
927                 $DB->count_records_select('message_contact_requests', 'userid = ? OR requesteduserid = ?',
928                         [$user1->id, $user1->id]));
930         // Confirm there is only 1 blocked user left.
931         $this->assertEquals(1, $DB->count_records('message_users_blocked'));
932         // And it is not related to user1.
933         $this->assertEquals(0,
934                 $DB->count_records_select('message_users_blocked', 'userid = ? OR blockeduserid = ?', [$user1->id, $user1->id]));
936         // Confirm there are only 2 messages left.
937         $this->assertEquals(2, $DB->count_records('messages'));
938         // And none of them are from user1.
939         $this->assertEquals(0, $DB->count_records('messages', ['useridfrom' => $user1->id]));
941         // Confirm there is 0 user action left.
942         $this->assertEquals(0, $DB->count_records('message_user_actions'));
943         // And it is not for user1.
944         $this->assertEquals(0, $DB->count_records('message_user_actions', ['userid' => $user1->id]));
946         // Confirm there are only 3 conversation members left.
947         $this->assertEquals(3, $DB->count_records('message_conversation_members'));
948         // And user1 is not in any conversation.
949         $this->assertEquals(0, $DB->count_records('message_conversation_members', ['userid' => $user1->id]));
951         // Confirm there are only 2 notifications.
952         $this->assertEquals(2, $DB->count_records('notifications'));
953         // And it is not related to user1.
954         $this->assertEquals(0,
955                 $DB->count_records_select('notifications', 'useridfrom = ? OR useridto = ? ', [$user1->id, $user1->id]));
956     }
958     /**
959      * Test for provider::delete_data_for_user().
960      */
961     public function test_delete_data_for_user() {
962         global $DB;
964         $this->resetAfterTest();
966         // Create users to test with.
967         $user1 = $this->getDataGenerator()->create_user();
968         $user2 = $this->getDataGenerator()->create_user();
969         $user3 = $this->getDataGenerator()->create_user();
970         $user4 = $this->getDataGenerator()->create_user();
971         $user5 = $this->getDataGenerator()->create_user();
972         $user6 = $this->getDataGenerator()->create_user();
974         $now = time();
975         $timeread = $now - DAYSECS;
977         // Create contacts.
978         \core_message\api::add_contact($user1->id, $user2->id);
979         \core_message\api::add_contact($user2->id, $user3->id);
981         // Create contact requests.
982         \core_message\api::create_contact_request($user1->id, $user3->id);
983         \core_message\api::create_contact_request($user2->id, $user4->id);
985         // Block users.
986         \core_message\api::block_user($user1->id, $user5->id);
987         \core_message\api::block_user($user2->id, $user6->id);
989         // Create messages.
990         $m1 = $this->create_message($user1->id, $user2->id, $now + (9 * DAYSECS), $timeread);
991         $m2 = $this->create_message($user2->id, $user1->id, $now + (8 * DAYSECS));
993         // Create notifications.
994         $n1 = $this->create_notification($user1->id, $user2->id, $now + (9 * DAYSECS), $timeread);
995         $n2 = $this->create_notification($user2->id, $user1->id, $now + (8 * DAYSECS));
996         $n2 = $this->create_notification($user2->id, $user3->id, $now + (8 * DAYSECS));
998         // Delete one of the messages.
999         \core_message\api::delete_message($user1->id, $m2);
1001         // There should be 2 contacts.
1002         $this->assertEquals(2, $DB->count_records('message_contacts'));
1004         // There should be 1 contact request.
1005         $this->assertEquals(2, $DB->count_records('message_contact_requests'));
1007         // There should be 1 blocked user.
1008         $this->assertEquals(2, $DB->count_records('message_users_blocked'));
1010         // There should be two messages.
1011         $this->assertEquals(2, $DB->count_records('messages'));
1013         // There should be two user actions - one for reading the message, one for deleting.
1014         $this->assertEquals(2, $DB->count_records('message_user_actions'));
1016         // There should be two conversation members.
1017         $this->assertEquals(2, $DB->count_records('message_conversation_members'));
1019         // There should be 5 notifications (3 from create_notification and 2 from create_contact_request).
1020         $this->assertEquals(5, $DB->count_records('notifications'));
1022         $user1context = context_user::instance($user1->id);
1023         $contextlist = new \core_privacy\local\request\approved_contextlist($user1, 'core_message',
1024             [$user1context->id]);
1025         provider::delete_data_for_user($contextlist);
1027         // Confirm the user 2 data still exists.
1028         $contacts = $DB->get_records('message_contacts');
1029         $contactrequests = $DB->get_records('message_contact_requests');
1030         $blockedusers = $DB->get_records('message_users_blocked');
1031         $messages = $DB->get_records('messages');
1032         $muas = $DB->get_records('message_user_actions');
1033         $mcms = $DB->get_records('message_conversation_members');
1034         $notifications = $DB->get_records('notifications');
1036         $this->assertCount(1, $contacts);
1037         $contact = reset($contacts);
1038         $this->assertEquals($user2->id, $contact->userid);
1039         $this->assertEquals($user3->id, $contact->contactid);
1041         $this->assertCount(1, $contactrequests);
1042         $contactrequest = reset($contactrequests);
1043         $this->assertEquals($user2->id, $contactrequest->userid);
1044         $this->assertEquals($user4->id, $contactrequest->requesteduserid);
1046         $this->assertCount(1, $blockedusers);
1047         $blockeduser = reset($blockedusers);
1048         $this->assertEquals($user2->id, $blockeduser->userid);
1049         $this->assertEquals($user6->id, $blockeduser->blockeduserid);
1051         $this->assertCount(1, $messages);
1052         $message = reset($messages);
1053         $this->assertEquals($m2, $message->id);
1055         $this->assertCount(0, $muas);
1057         $this->assertCount(1, $mcms);
1058         $mcm = reset($mcms);
1059         $this->assertEquals($user2->id, $mcm->userid);
1061         $this->assertCount(2, $notifications);
1062         ksort($notifications);
1064         $notification = array_pop($notifications);
1065         $this->assertEquals($user2->id, $notification->useridfrom);
1066         $this->assertEquals($user3->id, $notification->useridto);
1067     }
1069     /**
1070      * Test for provider::get_users_in_context() when there is no message or notification.
1071      */
1072     public function test_get_users_in_context_no_data() {
1073         $this->resetAfterTest();
1075         $user = $this->getDataGenerator()->create_user();
1076         $usercontext = context_user::instance($user->id);
1078         $userlist = new \core_privacy\local\request\userlist($usercontext, 'core_message');
1079         \core_message\privacy\provider::get_users_in_context($userlist);
1081         $this->assertEmpty($userlist->get_userids());
1082     }
1084     /**
1085      * Test for provider::get_users_in_context() when there is a message between users.
1086      */
1087     public function test_get_users_in_context_with_message() {
1088         $this->resetAfterTest();
1090         $user1 = $this->getDataGenerator()->create_user();
1091         $user2 = $this->getDataGenerator()->create_user();
1093         $user1context = context_user::instance($user1->id);
1094         $user2context = context_user::instance($user2->id);
1096         // Test nothing is found before message is sent.
1097         $userlist = new \core_privacy\local\request\userlist($user1context, 'core_message');
1098         \core_message\privacy\provider::get_users_in_context($userlist);
1099         $this->assertCount(0, $userlist);
1100         $userlist = new \core_privacy\local\request\userlist($user2context, 'core_message');
1101         \core_message\privacy\provider::get_users_in_context($userlist);
1102         $this->assertCount(0, $userlist);
1104         $this->create_message($user1->id, $user2->id, time() - (9 * DAYSECS));
1106         // Test for the sender.
1107         $userlist = new \core_privacy\local\request\userlist($user1context, 'core_message');
1108         \core_message\privacy\provider::get_users_in_context($userlist);
1109         $this->assertCount(1, $userlist);
1110         $userincontext = $userlist->current();
1111         $this->assertEquals($user1->id, $userincontext->id);
1113         // Test for the receiver.
1114         $userlist = new \core_privacy\local\request\userlist($user2context, 'core_message');
1115         \core_message\privacy\provider::get_users_in_context($userlist);
1116         $this->assertCount(1, $userlist);
1117         $userincontext = $userlist->current();
1118         $this->assertEquals($user2->id, $userincontext->id);
1119     }
1121     /**
1122      * Test for provider::get_users_in_context() when there is a notification between users.
1123      */
1124     public function test_get_users_in_context_with_notification() {
1125         $this->resetAfterTest();
1127         $user1 = $this->getDataGenerator()->create_user();
1128         $user2 = $this->getDataGenerator()->create_user();
1130         $user1context = context_user::instance($user1->id);
1131         $user2context = context_user::instance($user2->id);
1133         // Test nothing is found before notification is created.
1134         $userlist = new \core_privacy\local\request\userlist($user1context, 'core_message');
1135         \core_message\privacy\provider::get_users_in_context($userlist);
1136         $this->assertCount(0, $userlist);
1137         $userlist = new \core_privacy\local\request\userlist($user2context, 'core_message');
1138         \core_message\privacy\provider::get_users_in_context($userlist);
1139         $this->assertCount(0, $userlist);
1141         $this->create_notification($user1->id, $user2->id, time() - (9 * DAYSECS));
1143         // Test for the sender.
1144         $userlist = new \core_privacy\local\request\userlist($user1context, 'core_message');
1145         \core_message\privacy\provider::get_users_in_context($userlist);
1146         $this->assertCount(1, $userlist);
1147         $userincontext = $userlist->current();
1148         $this->assertEquals($user1->id, $userincontext->id);
1150         // Test for the receiver.
1151         $userlist = new \core_privacy\local\request\userlist($user2context, 'core_message');
1152         \core_message\privacy\provider::get_users_in_context($userlist);
1153         $this->assertCount(1, $userlist);
1154         $userincontext = $userlist->current();
1155         $this->assertEquals($user2->id, $userincontext->id);
1156     }
1158     /**
1159      * Test for provider::get_users_in_context() when a users has a contact.
1160      */
1161     public function test_get_users_in_context_with_contact() {
1162         $this->resetAfterTest();
1164         $user1 = $this->getDataGenerator()->create_user();
1165         $user2 = $this->getDataGenerator()->create_user();
1167         $user1context = context_user::instance($user1->id);
1168         $user2context = context_user::instance($user2->id);
1170         // Test nothing is found before contact is created.
1171         $userlist = new \core_privacy\local\request\userlist($user1context, 'core_message');
1172         \core_message\privacy\provider::get_users_in_context($userlist);
1173         $this->assertCount(0, $userlist);
1174         $userlist = new \core_privacy\local\request\userlist($user2context, 'core_message');
1175         \core_message\privacy\provider::get_users_in_context($userlist);
1176         $this->assertCount(0, $userlist);
1178         \core_message\api::add_contact($user1->id, $user2->id);
1180         // Test for the user adding the contact.
1181         $userlist = new \core_privacy\local\request\userlist($user1context, 'core_message');
1182         \core_message\privacy\provider::get_users_in_context($userlist);
1183         $this->assertCount(1, $userlist);
1184         $userincontext = $userlist->current();
1185         $this->assertEquals($user1->id, $userincontext->id);
1187         // Test for the user who is the contact.
1188         $userlist = new \core_privacy\local\request\userlist($user2context, 'core_message');
1189         \core_message\privacy\provider::get_users_in_context($userlist);
1190         $this->assertCount(1, $userlist);
1191         $userincontext = $userlist->current();
1192         $this->assertEquals($user2->id, $userincontext->id);
1193     }
1195     /**
1196      * Test for provider::get_users_in_context() when a user makes a contact request.
1197      */
1198     public function test_get_users_in_context_with_contact_request() {
1199         $this->resetAfterTest();
1201         $user1 = $this->getDataGenerator()->create_user();
1202         $user2 = $this->getDataGenerator()->create_user();
1204         $user1context = context_user::instance($user1->id);
1205         $user2context = context_user::instance($user2->id);
1207         // Test nothing is found before request is created.
1208         $userlist = new \core_privacy\local\request\userlist($user1context, 'core_message');
1209         \core_message\privacy\provider::get_users_in_context($userlist);
1210         $this->assertCount(0, $userlist);
1211         $userlist = new \core_privacy\local\request\userlist($user2context, 'core_message');
1212         \core_message\privacy\provider::get_users_in_context($userlist);
1213         $this->assertCount(0, $userlist);
1215         \core_message\api::create_contact_request($user1->id, $user2->id);
1217         // Test for the user requesting the contact.
1218         $userlist = new \core_privacy\local\request\userlist($user1context, 'core_message');
1219         \core_message\privacy\provider::get_users_in_context($userlist);
1220         $this->assertCount(1, $userlist);
1221         $userincontext = $userlist->current();
1222         $this->assertEquals($user1->id, $userincontext->id);
1224         // Test for the user receiving the contact request.
1225         $userlist = new \core_privacy\local\request\userlist($user2context, 'core_message');
1226         \core_message\privacy\provider::get_users_in_context($userlist);
1227         $this->assertCount(1, $userlist);
1228         $userincontext = $userlist->current();
1229         $this->assertEquals($user2->id, $userincontext->id);
1230     }
1232     /**
1233      * Test for provider::get_users_in_context() when a user is blocked.
1234      */
1235     public function test_get_users_in_context_with_blocked_contact() {
1236         $this->resetAfterTest();
1238         $user1 = $this->getDataGenerator()->create_user();
1239         $user2 = $this->getDataGenerator()->create_user();
1241         $user1context = context_user::instance($user1->id);
1242         $user2context = context_user::instance($user2->id);
1244         // Test nothing is found before user is blocked.
1245         $userlist = new \core_privacy\local\request\userlist($user1context, 'core_message');
1246         \core_message\privacy\provider::get_users_in_context($userlist);
1247         $this->assertCount(0, $userlist);
1248         $userlist = new \core_privacy\local\request\userlist($user2context, 'core_message');
1249         \core_message\privacy\provider::get_users_in_context($userlist);
1250         $this->assertCount(0, $userlist);
1252         \core_message\api::block_user($user1->id, $user2->id);
1254         // Test for the blocking user.
1255         $userlist = new \core_privacy\local\request\userlist($user1context, 'core_message');
1256         \core_message\privacy\provider::get_users_in_context($userlist);
1257         $this->assertCount(1, $userlist);
1258         $userincontext = $userlist->current();
1259         $this->assertEquals($user1->id, $userincontext->id);
1261         // Test for the user who is blocked.
1262         $userlist = new \core_privacy\local\request\userlist($user2context, 'core_message');
1263         \core_message\privacy\provider::get_users_in_context($userlist);
1264         $this->assertCount(1, $userlist);
1265         $userincontext = $userlist->current();
1266         $this->assertEquals($user2->id, $userincontext->id);
1267     }
1269     /**
1270      * Test for provider::delete_data_for_users().
1271      */
1272     public function test_delete_data_for_users() {
1273         global $DB;
1275         $this->resetAfterTest();
1277         // Create users to test with.
1278         $user1 = $this->getDataGenerator()->create_user();
1279         $user2 = $this->getDataGenerator()->create_user();
1280         $user3 = $this->getDataGenerator()->create_user();
1281         $user4 = $this->getDataGenerator()->create_user();
1282         $user5 = $this->getDataGenerator()->create_user();
1283         $user6 = $this->getDataGenerator()->create_user();
1285         $now = time();
1286         $timeread = $now - DAYSECS;
1288         // Create contacts.
1289         \core_message\api::add_contact($user1->id, $user2->id);
1290         \core_message\api::add_contact($user2->id, $user3->id);
1292         // Create contact requests.
1293         \core_message\api::create_contact_request($user1->id, $user3->id);
1294         \core_message\api::create_contact_request($user2->id, $user4->id);
1296         // Block users.
1297         \core_message\api::block_user($user1->id, $user5->id);
1298         \core_message\api::block_user($user2->id, $user6->id);
1300         // Create messages.
1301         $m1 = $this->create_message($user1->id, $user2->id, $now + (9 * DAYSECS), $timeread);
1302         $m2 = $this->create_message($user2->id, $user1->id, $now + (8 * DAYSECS));
1304         // Create notifications.
1305         $n1 = $this->create_notification($user1->id, $user2->id, $now + (9 * DAYSECS), $timeread);
1306         $n2 = $this->create_notification($user2->id, $user1->id, $now + (8 * DAYSECS));
1307         $n2 = $this->create_notification($user2->id, $user3->id, $now + (8 * DAYSECS));
1309         // Delete one of the messages.
1310         \core_message\api::delete_message($user1->id, $m2);
1312         // There should be 2 contacts.
1313         $this->assertEquals(2, $DB->count_records('message_contacts'));
1315         // There should be 1 contact request.
1316         $this->assertEquals(2, $DB->count_records('message_contact_requests'));
1318         // There should be 1 blocked user.
1319         $this->assertEquals(2, $DB->count_records('message_users_blocked'));
1321         // There should be two messages.
1322         $this->assertEquals(2, $DB->count_records('messages'));
1324         // There should be two user actions - one for reading the message, one for deleting.
1325         $this->assertEquals(2, $DB->count_records('message_user_actions'));
1327         // There should be two conversation members.
1328         $this->assertEquals(2, $DB->count_records('message_conversation_members'));
1330         // There should be three notifications + two for the contact requests.
1331         $this->assertEquals(5, $DB->count_records('notifications'));
1333         $user1context = context_user::instance($user1->id);
1334         $approveduserlist = new \core_privacy\local\request\approved_userlist($user1context, 'core_message',
1335                 [$user1->id, $user2->id]);
1336         provider::delete_data_for_users($approveduserlist);
1338         // Only user1's data should be deleted. User2 should be skipped as user2 is an invalid user for user1context.
1340         // Confirm the user 2 data still exists.
1341         $contacts = $DB->get_records('message_contacts');
1342         $contactrequests = $DB->get_records('message_contact_requests');
1343         $blockedusers = $DB->get_records('message_users_blocked');
1344         $messages = $DB->get_records('messages');
1345         $muas = $DB->get_records('message_user_actions');
1346         $mcms = $DB->get_records('message_conversation_members');
1347         $notifications = $DB->get_records('notifications');
1349         $this->assertCount(1, $contacts);
1350         $contact = reset($contacts);
1351         $this->assertEquals($user2->id, $contact->userid);
1352         $this->assertEquals($user3->id, $contact->contactid);
1354         $this->assertCount(1, $contactrequests);
1355         $contactrequest = reset($contactrequests);
1356         $this->assertEquals($user2->id, $contactrequest->userid);
1357         $this->assertEquals($user4->id, $contactrequest->requesteduserid);
1359         $this->assertCount(1, $blockedusers);
1360         $blockeduser = reset($blockedusers);
1361         $this->assertEquals($user2->id, $blockeduser->userid);
1362         $this->assertEquals($user6->id, $blockeduser->blockeduserid);
1364         $this->assertCount(1, $messages);
1365         $message = reset($messages);
1366         $this->assertEquals($m2, $message->id);
1368         $this->assertCount(0, $muas);
1370         $this->assertCount(1, $mcms);
1371         $mcm = reset($mcms);
1372         $this->assertEquals($user2->id, $mcm->userid);
1374         $this->assertCount(2, $notifications);
1375         ksort($notifications);
1377         $notification = array_pop($notifications);
1378         $this->assertEquals($user2->id, $notification->useridfrom);
1379         $this->assertEquals($user3->id, $notification->useridto);
1380     }
1382     /**
1383      * Test for provider::add_contexts_for_conversations().
1384      */
1385     public function test_add_contexts_for_conversations() {
1386         $this->resetAfterTest();
1387         $this->setAdminUser();
1388         $component = 'core_group';
1389         $itemtype = 'groups';
1391         $user1 = $this->getDataGenerator()->create_user();
1392         $user2 = $this->getDataGenerator()->create_user();
1393         $user3 = $this->getDataGenerator()->create_user();
1394         $user4 = $this->getDataGenerator()->create_user();
1396         // Test nothing is found before group conversations is created or message is sent.
1397         $contextlist = new contextlist();
1398         provider::add_contexts_for_conversations($contextlist, $user1->id, $component, $itemtype);
1399         $this->assertCount(0, $contextlist);
1400         provider::add_contexts_for_conversations($contextlist, $user2->id, $component, $itemtype);
1401         $this->assertCount(0, $contextlist);
1403         // Create courses.
1404         $course1 = $this->getDataGenerator()->create_course();
1405         $course2 = $this->getDataGenerator()->create_course();
1406         $coursecontext1 = \context_course::instance($course1->id);
1407         $coursecontext2 = \context_course::instance($course2->id);
1409         // Enrol users to courses.
1410         $this->getDataGenerator()->enrol_user($user1->id, $course1->id);
1411         $this->getDataGenerator()->enrol_user($user2->id, $course1->id);
1412         $this->getDataGenerator()->enrol_user($user3->id, $course1->id);
1413         $this->getDataGenerator()->enrol_user($user1->id, $course2->id);
1414         $this->getDataGenerator()->enrol_user($user2->id, $course2->id);
1416         // Create course groups with messaging enabled.
1417         $group1a = $this->getDataGenerator()->create_group(array('courseid' => $course1->id, 'enablemessaging' => 1));
1418         $group2a = $this->getDataGenerator()->create_group(array('courseid' => $course2->id, 'enablemessaging' => 1));
1420         // Add users to groups.
1421         $this->getDataGenerator()->create_group_member(array('groupid' => $group1a->id, 'userid' => $user1->id));
1422         $this->getDataGenerator()->create_group_member(array('groupid' => $group1a->id, 'userid' => $user2->id));
1423         $this->getDataGenerator()->create_group_member(array('groupid' => $group1a->id, 'userid' => $user3->id));
1424         $this->getDataGenerator()->create_group_member(array('groupid' => $group2a->id, 'userid' => $user1->id));
1426         // Get conversation.
1427         $conversation1 = \core_message\api::get_conversation_by_area(
1428             $component,
1429             $itemtype,
1430             $group1a->id,
1431             $coursecontext1->id
1432         );
1434         // Send some messages to the group conversation.
1435         $now = time();
1436         $m1id = testhelper::send_fake_message_to_conversation($user1, $conversation1->id, 'Message 1', $now + 1);
1437         $m2id = testhelper::send_fake_message_to_conversation($user1, $conversation1->id, 'Message 2', $now + 2);
1438         $m3id = testhelper::send_fake_message_to_conversation($user2, $conversation1->id, 'Message 3', $now + 3);
1440         // Test for user1 (is member of the conversation and has sent a message).
1441         $contextlist = new contextlist();
1442         provider::add_contexts_for_conversations($contextlist, $user1->id, $component, $itemtype);
1443         $this->assertCount(2, $contextlist);
1444         $this->assertContains($coursecontext1->id, $contextlist->get_contextids());
1445         $this->assertContains($coursecontext2->id, $contextlist->get_contextids());
1447         // Test for user2 (is member of the conversation and has sent a message).
1448         $contextlist = new contextlist();
1449         provider::add_contexts_for_conversations($contextlist, $user2->id, $component, $itemtype);
1450         $this->assertCount(1, $contextlist);
1451         $this->assertEquals($coursecontext1, $contextlist->current());
1453         // Test for user3 (is member of the conversation).
1454         $contextlist = new contextlist();
1455         provider::add_contexts_for_conversations($contextlist, $user3->id, $component, $itemtype);
1456         $this->assertCount(1, $contextlist);
1457         $this->assertEquals($coursecontext1, $contextlist->current());
1459         // Test for user4 (doesn't belong to the conversation).
1460         $contextlist = new contextlist();
1461         provider::add_contexts_for_conversations($contextlist, $user4->id, $component, $itemtype);
1462         $this->assertCount(0, $contextlist);
1463     }
1465     /**
1466      * Test for provider::add_conversations_in_context().
1467      */
1468     public function test_add_conversations_in_context() {
1469         $this->resetAfterTest();
1470         $this->setAdminUser();
1471         $component = 'core_group';
1472         $itemtype = 'groups';
1474         $user1 = $this->getDataGenerator()->create_user();
1475         $user2 = $this->getDataGenerator()->create_user();
1476         $user3 = $this->getDataGenerator()->create_user();
1477         $user4 = $this->getDataGenerator()->create_user();
1479         // Create courses.
1480         $course1 = $this->getDataGenerator()->create_course();
1481         $course2 = $this->getDataGenerator()->create_course();
1482         $coursecontext1 = \context_course::instance($course1->id);
1483         $coursecontext2 = \context_course::instance($course2->id);
1485         // Test nothing is found before group conversations is created or message is sent.
1486         $userlist1 = new \core_privacy\local\request\userlist($coursecontext1, 'core_message');
1487         provider::add_conversations_in_context($userlist1, $component, $itemtype);
1488         $this->assertCount(0, $userlist1);
1490         // Enrol users to courses.
1491         $this->getDataGenerator()->enrol_user($user1->id, $course1->id);
1492         $this->getDataGenerator()->enrol_user($user2->id, $course1->id);
1493         $this->getDataGenerator()->enrol_user($user3->id, $course1->id);
1494         $this->getDataGenerator()->enrol_user($user4->id, $course1->id);
1495         $this->getDataGenerator()->enrol_user($user1->id, $course2->id);
1496         $this->getDataGenerator()->enrol_user($user2->id, $course2->id);
1498         // Create course groups with messaging enabled.
1499         $group1a = $this->getDataGenerator()->create_group(array('courseid' => $course1->id, 'enablemessaging' => 1));
1500         $group2a = $this->getDataGenerator()->create_group(array('courseid' => $course2->id, 'enablemessaging' => 1));
1502         // Add users to groups.
1503         $this->getDataGenerator()->create_group_member(array('groupid' => $group1a->id, 'userid' => $user1->id));
1504         $this->getDataGenerator()->create_group_member(array('groupid' => $group1a->id, 'userid' => $user2->id));
1505         $this->getDataGenerator()->create_group_member(array('groupid' => $group1a->id, 'userid' => $user3->id));
1506         $this->getDataGenerator()->create_group_member(array('groupid' => $group2a->id, 'userid' => $user1->id));
1508         // Get conversation.
1509         $conversation1 = \core_message\api::get_conversation_by_area(
1510             $component,
1511             $itemtype,
1512             $group1a->id,
1513             $coursecontext1->id
1514         );
1516         // Send some messages to the group conversation.
1517         $now = time();
1518         $m1id = testhelper::send_fake_message_to_conversation($user1, $conversation1->id, 'Message 1', $now + 1);
1519         $m2id = testhelper::send_fake_message_to_conversation($user1, $conversation1->id, 'Message 2', $now + 2);
1520         $m3id = testhelper::send_fake_message_to_conversation($user2, $conversation1->id, 'Message 3', $now + 3);
1522         // Test for users with any group conversation in course1.
1523         provider::add_conversations_in_context($userlist1, $component, $itemtype);
1524         $this->assertCount(3, $userlist1);
1525         $this->assertEquals([$user1->id, $user2->id, $user3->id], $userlist1->get_userids(), '', 0, 10, true);
1527         // Test for users with any group conversation in course2.
1528         $userlist2 = new \core_privacy\local\request\userlist($coursecontext2, 'core_message');
1529         provider::add_conversations_in_context($userlist2, $component, $itemtype);
1530         $this->assertCount(1, $userlist2);
1531         $this->assertEquals(
1532                 [$user1->id],
1533                 $userlist2->get_userids());
1534     }
1536     /**
1537      * Test for provider::export_conversations().
1538      */
1539     public function test_export_conversations() {
1540         global $DB;
1542         $this->resetAfterTest();
1543         $this->setAdminUser();
1544         $now = time();
1545         $systemcontext = \context_system::instance();
1547         // Create users to test with.
1548         $user1 = $this->getDataGenerator()->create_user();
1549         $user2 = $this->getDataGenerator()->create_user();
1550         $user3 = $this->getDataGenerator()->create_user();
1551         $user1context = \context_user::instance($user1->id);
1553         $course1 = $this->getDataGenerator()->create_course();
1554         $course2 = $this->getDataGenerator()->create_course();
1555         $coursecontext1 = \context_course::instance($course1->id);
1556         $coursecontext2 = \context_course::instance($course2->id);
1558         // Enrol users to courses.
1559         $this->getDataGenerator()->enrol_user($user1->id, $course1->id);
1560         $this->getDataGenerator()->enrol_user($user2->id, $course1->id);
1561         $this->getDataGenerator()->enrol_user($user3->id, $course1->id);
1562         $this->getDataGenerator()->enrol_user($user1->id, $course2->id);
1564         // Create course groups with group messaging enabled.
1565         $group1a = $this->getDataGenerator()->create_group(array('courseid' => $course1->id, 'enablemessaging' => 1));
1566         $group2a = $this->getDataGenerator()->create_group(array('courseid' => $course2->id, 'enablemessaging' => 1));
1568         // Add users to groups.
1569         $this->getDataGenerator()->create_group_member(array('groupid' => $group1a->id, 'userid' => $user1->id));
1570         $this->getDataGenerator()->create_group_member(array('groupid' => $group1a->id, 'userid' => $user2->id));
1571         $this->getDataGenerator()->create_group_member(array('groupid' => $group1a->id, 'userid' => $user3->id));
1572         $this->getDataGenerator()->create_group_member(array('groupid' => $group2a->id, 'userid' => $user1->id));
1574         // Send some private messages between user 1 and user 2.
1575         $pm1id = $this->create_message($user1->id, $user2->id, $now);
1577         // Get conversation.
1578         $iconversation1id = \core_message\api::get_conversation_between_users([$user1->id, $user2->id]);
1579         $component = 'core_group';
1580         $itemtype = 'groups';
1581         $conversation1 = \core_message\api::get_conversation_by_area(
1582             $component,
1583             $itemtype,
1584             $group1a->id,
1585             $coursecontext1->id
1586         );
1588         // Make favourite some conversations.
1589         \core_message\api::set_favourite_conversation($conversation1->id, $user1->id);
1590         \core_message\api::set_favourite_conversation($iconversation1id, $user2->id);
1592         // Mute some conversations.
1593         \core_message\api::mute_conversation($user1->id, $conversation1->id);
1594         \core_message\api::mute_conversation($user2->id, $iconversation1id);
1596         // Send some messages to the conversation.
1597         $m1 = testhelper::send_fake_message_to_conversation($user1, $conversation1->id, 'Message 1', $now + 1);
1598         $m2 = testhelper::send_fake_message_to_conversation($user1, $conversation1->id, 'Message 2', $now + 2);
1599         $m3 = testhelper::send_fake_message_to_conversation($user2, $conversation1->id, 'Message 3', $now + 3);
1601         $dbm1 = $DB->get_record('messages', ['id' => $m1]);
1602         $dbm2 = $DB->get_record('messages', ['id' => $m2]);
1603         $dbm3 = $DB->get_record('messages', ['id' => $m3]);
1605         // Mark as read and delete some messages.
1606         \core_message\api::mark_message_as_read($user1->id, $dbm3, $now + 5);
1607         \core_message\api::delete_message($user1->id, $m2);
1609         // Export all the conversations related to the groups in course1 for user1.
1610         provider::export_conversations($user1->id, 'core_group', 'groups', $coursecontext1);
1612         // Check that system context hasn't been exported.
1613         $writer = writer::with_context($systemcontext);
1614         $this->assertFalse($writer->has_any_data());
1616         // Check that course2 context hasn't been exported.
1617         $writer = writer::with_context($coursecontext2);
1618         $this->assertFalse($writer->has_any_data());
1620         // Check that course1 context has been exported for user1 and contains data.
1621         $writer = writer::with_context($coursecontext1);
1622         $this->assertTrue($writer->has_any_data());
1624         // Confirm the messages for conversation1 are correct.
1625         $messages = (array) $writer->get_data([
1626             get_string('messages', 'core_message'),
1627             get_string($conversation1->itemtype, $conversation1->component),
1628             get_string('privacy:export:conversationprefix', 'core_message') . $conversation1->name
1629         ]);
1630         $this->assertCount(3, $messages);
1632         usort($messages, ['static', 'sort_messages']);
1633         $m1 = array_shift($messages);
1634         $m2 = array_shift($messages);
1635         $m3 = array_shift($messages);
1637         // Check message 1 is correct.
1638         $this->assertEquals(get_string('yes'), $m1->issender);
1639         $this->assertEquals(message_format_message_text($dbm1), $m1->message);
1640         $this->assertEquals(transform::datetime($now + 1), $m1->timecreated);
1641         $this->assertEquals('-', $m1->timeread);
1642         $this->assertArrayNotHasKey('timedeleted', (array) $m1);
1644         // Check message 2 is correct.
1645         $this->assertEquals(get_string('yes'), $m2->issender);
1646         $this->assertEquals(message_format_message_text($dbm2), $m2->message);
1647         $this->assertEquals(transform::datetime($now + 2), $m2->timecreated);
1648         $this->assertEquals('-', $m2->timeread);
1649         $this->assertArrayHasKey('timedeleted', (array) $m2);
1651         // Check message 3 is correct.
1652         $this->assertEquals(get_string('no'), $m3->issender);
1653         $this->assertEquals(message_format_message_text($dbm3), $m3->message);
1654         $this->assertEquals(transform::datetime($now + 3), $m3->timecreated);
1655         $this->assertEquals(transform::datetime($now + 5), $m3->timeread);
1656         $this->assertArrayNotHasKey('timedeleted', (array) $m3);
1658         // Confirm the muted group conversation is correct.
1659         $mutedconversations = (array) $writer->get_related_data([
1660             get_string('messages', 'core_message'),
1661             get_string($conversation1->itemtype, $conversation1->component),
1662             get_string('privacy:export:conversationprefix', 'core_message') . $conversation1->name
1663         ], 'muted');
1664         $this->assertCount(2, $mutedconversations);
1665         $this->assertEquals(get_string('yes'), $mutedconversations['muted']);
1667         // Confirm the favourite group conversation is correct.
1668         $favourite = (array) $writer->get_related_data([
1669             get_string('messages', 'core_message'),
1670             get_string($conversation1->itemtype, $conversation1->component),
1671             get_string('privacy:export:conversationprefix', 'core_message') . $conversation1->name
1672         ], 'starred');
1673         $this->assertCount(4, $favourite);
1674         $this->assertEquals(get_string('yes'), $favourite['starred']);
1676         // Reset writer before exporting conversations for user2.
1677         writer::reset();
1679         // Export all the conversations related to the groups in course1 for user2.
1680         provider::export_conversations($user2->id, 'core_group', 'groups', $coursecontext1);
1682         // Check that system context hasn't been exported.
1683         $writer = writer::with_context($systemcontext);
1684         $this->assertFalse($writer->has_any_data());
1686         // Check that course2 context hasn't been exported.
1687         $writer = writer::with_context($coursecontext2);
1688         $this->assertFalse($writer->has_any_data());
1690         // Check that course1 context has been exported for user2 and contains data.
1691         $writer = writer::with_context($coursecontext1);
1692         $this->assertTrue($writer->has_any_data());
1694         // Confirm the messages for conversation1 are correct.
1695         $messages = (array) $writer->get_data([
1696             get_string('messages', 'core_message'),
1697             get_string($conversation1->itemtype, $conversation1->component),
1698             get_string('privacy:export:conversationprefix', 'core_message') . $conversation1->name
1699         ]);
1700         $this->assertCount(3, $messages);
1702         usort($messages, ['static', 'sort_messages']);
1703         $m1 = array_shift($messages);
1704         $m2 = array_shift($messages);
1705         $m3 = array_shift($messages);
1707         // Check message 1 is correct.
1708         $this->assertEquals(get_string('no'), $m1->issender);
1709         $this->assertEquals(message_format_message_text($dbm1), $m1->message);
1710         $this->assertEquals(transform::datetime($now + 1), $m1->timecreated);
1711         $this->assertEquals('-', $m1->timeread);
1712         $this->assertArrayNotHasKey('timedeleted', (array) $m1);
1714         // Check message 2 is correct.
1715         $this->assertEquals(get_string('no'), $m2->issender);
1716         $this->assertEquals(message_format_message_text($dbm2), $m2->message);
1717         $this->assertEquals(transform::datetime($now + 2), $m2->timecreated);
1718         $this->assertEquals('-', $m2->timeread);
1719         $this->assertArrayNotHasKey('timedeleted', (array) $m2);
1721         // Check message 3 is correct.
1722         $this->assertEquals(get_string('yes'), $m3->issender);
1723         $this->assertEquals(message_format_message_text($dbm3), $m3->message);
1724         $this->assertEquals(transform::datetime($now + 3), $m3->timecreated);
1725         $this->assertEquals('-', $m3->timeread);
1726         $this->assertArrayNotHasKey('timedeleted', (array) $m3);
1728         // Confirm the muted group conversation is correct.
1729         $mutedconversations = (array) $writer->get_related_data([
1730             get_string('messages', 'core_message'),
1731             get_string($conversation1->itemtype, $conversation1->component),
1732             $conversation1->name
1733         ], 'muted');
1734         $this->assertCount(0, $mutedconversations);
1736         // Confirm there are no favourite group conversation for user2.
1737         $favourite = (array) $writer->get_related_data([
1738             get_string('messages', 'core_message'),
1739             get_string($conversation1->itemtype, $conversation1->component),
1740             $conversation1->name
1741         ], 'starred');
1742         $this->assertCount(0, $favourite);
1743     }
1745     /**
1746      * Test for provider::delete_conversations_for_all_users().
1747      */
1748     public function test_delete_conversations_for_all_users() {
1749         global $DB;
1751         $this->resetAfterTest();
1752         $this->setAdminUser();
1753         $now = time();
1754         $timeread = $now - DAYSECS;
1755         $component = 'core_group';
1756         $itemtype = 'groups';
1758         // Create users to test with.
1759         $user1 = $this->getDataGenerator()->create_user();
1760         $user2 = $this->getDataGenerator()->create_user();
1761         $user3 = $this->getDataGenerator()->create_user();
1762         $user4 = $this->getDataGenerator()->create_user();
1763         $user5 = $this->getDataGenerator()->create_user();
1764         $user1context = \context_user::instance($user1->id);
1766         // Create contacts.
1767         \core_message\api::add_contact($user1->id, $user2->id);
1768         \core_message\api::add_contact($user2->id, $user3->id);
1770         // Create contact requests.
1771         \core_message\api::create_contact_request($user1->id, $user3->id);
1772         \core_message\api::create_contact_request($user2->id, $user4->id);
1774         // Block a user.
1775         \core_message\api::block_user($user1->id, $user3->id);
1776         \core_message\api::block_user($user3->id, $user4->id);
1778         // Create individual messages.
1779         $im1 = $this->create_message($user1->id, $user2->id, $now + (9 * DAYSECS), true);
1780         $im2 = $this->create_message($user2->id, $user1->id, $now + (8 * DAYSECS), true);
1781         $im3 = $this->create_message($user2->id, $user3->id, $now + (7 * DAYSECS));
1783         // Create notifications.
1784         $n1 = $this->create_notification($user1->id, $user2->id, $now + (9 * DAYSECS), $timeread);
1785         $n2 = $this->create_notification($user2->id, $user1->id, $now + (8 * DAYSECS));
1786         $n3 = $this->create_notification($user2->id, $user3->id, $now + (7 * DAYSECS));
1788         // Delete one of the messages.
1789         \core_message\api::delete_message($user1->id, $im2);
1791         // Create course2.
1792         $course1 = $this->getDataGenerator()->create_course();
1793         $course2 = $this->getDataGenerator()->create_course();
1794         $coursecontext1 = \context_course::instance($course1->id);
1795         $coursecontext2 = \context_course::instance($course2->id);
1797         // Enrol users to courses.
1798         $this->getDataGenerator()->enrol_user($user1->id, $course1->id);
1799         $this->getDataGenerator()->enrol_user($user2->id, $course1->id);
1800         $this->getDataGenerator()->enrol_user($user3->id, $course1->id);
1801         $this->getDataGenerator()->enrol_user($user1->id, $course2->id);
1802         $this->getDataGenerator()->enrol_user($user2->id, $course2->id);
1804         // Create course groups with group messaging enabled.
1805         $group1a = $this->getDataGenerator()->create_group(array('courseid' => $course1->id, 'enablemessaging' => 1));
1806         $group2a = $this->getDataGenerator()->create_group(array('courseid' => $course2->id, 'enablemessaging' => 1));
1808         // Add users to groups.
1809         $this->getDataGenerator()->create_group_member(array('groupid' => $group1a->id, 'userid' => $user1->id));
1810         $this->getDataGenerator()->create_group_member(array('groupid' => $group1a->id, 'userid' => $user2->id));
1811         $this->getDataGenerator()->create_group_member(array('groupid' => $group1a->id, 'userid' => $user3->id));
1812         $this->getDataGenerator()->create_group_member(array('groupid' => $group2a->id, 'userid' => $user1->id));
1813         $this->getDataGenerator()->create_group_member(array('groupid' => $group2a->id, 'userid' => $user2->id));
1815         // Get conversations.
1816         $iconversation1id = \core_message\api::get_conversation_between_users([$user1->id, $user2->id]);
1817         $conversation1 = \core_message\api::get_conversation_by_area(
1818             $component,
1819             $itemtype,
1820             $group1a->id,
1821             $coursecontext1->id
1822         );
1823         $conversation2 = \core_message\api::get_conversation_by_area(
1824             $component,
1825             $itemtype,
1826             $group2a->id,
1827             $coursecontext2->id
1828         );
1830         // Make favourite some conversations.
1831         \core_message\api::set_favourite_conversation($iconversation1id, $user1->id);
1832         \core_message\api::set_favourite_conversation($conversation1->id, $user1->id);
1833         \core_message\api::set_favourite_conversation($conversation1->id, $user2->id);
1835         // Send some messages to the conversation.
1836         $gm1 = testhelper::send_fake_message_to_conversation($user1, $conversation1->id, 'Message 1.1', $now + 1);
1837         $gm2 = testhelper::send_fake_message_to_conversation($user1, $conversation1->id, 'Message 1.2', $now + 2);
1838         $gm3 = testhelper::send_fake_message_to_conversation($user2, $conversation1->id, 'Message 1.3', $now + 3);
1839         $gm4 = testhelper::send_fake_message_to_conversation($user1, $conversation2->id, 'Message 2.1', $now + 4);
1840         $gm5 = testhelper::send_fake_message_to_conversation($user2, $conversation2->id, 'Message 2.2', $now + 5);
1842         $dbgm1 = $DB->get_record('messages', ['id' => $gm1]);
1843         $dbgm2 = $DB->get_record('messages', ['id' => $gm2]);
1844         $dbgm3 = $DB->get_record('messages', ['id' => $gm3]);
1845         $dbgm4 = $DB->get_record('messages', ['id' => $gm4]);
1846         $dbgm5 = $DB->get_record('messages', ['id' => $gm5]);
1848         // Mark as read one of the conversation messages.
1849         \core_message\api::mark_message_as_read($user1->id, $dbgm3, $now + 5);
1851         // Mark some conversations as muted by two users.
1852         \core_message\api::mute_conversation($user1->id, $iconversation1id);
1853         \core_message\api::mute_conversation($user1->id, $conversation1->id);
1854         \core_message\api::mute_conversation($user2->id, $conversation1->id);
1856         // There should be 2 contacts.
1857         $this->assertEquals(2, $DB->count_records('message_contacts'));
1859         // There should be 2 contact requests.
1860         $this->assertEquals(2, $DB->count_records('message_contact_requests'));
1862         // There should be 2 blocked users.
1863         $this->assertEquals(2, $DB->count_records('message_users_blocked'));
1865         // There should be 8 messages.
1866         $this->assertEquals(8, $DB->count_records('messages'));
1868         // There should be 4 user actions - 3 for reading the message, 1 for deleting.
1869         $this->assertEquals(4, $DB->count_records('message_user_actions'));
1871         // There should be 3 muted conversations.
1872         $this->assertEquals(3, $DB->count_records('message_conversation_actions'));
1874         // There should be 4 conversations - 2 individual + 2 group.
1875         $this->assertEquals(4, $DB->count_records('message_conversations'));
1877         // There should be 9 conversation members - (2 + 2) individual + (3 + 2) group.
1878         $this->assertEquals(9, $DB->count_records('message_conversation_members'));
1880         // There should be 5 notifications (3 from create_notification and 2 from create_contact_request).
1881         $this->assertEquals(5, $DB->count_records('notifications'));
1883         // There should be 3 favourite conversations.
1884         $this->assertEquals(3, $DB->count_records('favourite'));
1886         // Delete conversations for all users in course1.
1887         provider::delete_conversations_for_all_users($coursecontext1, $component, $itemtype);
1889         // There should be still 2 contacts.
1890         $this->assertEquals(2, $DB->count_records('message_contacts'));
1892         // There should be still 2 contact requests.
1893         $this->assertEquals(2, $DB->count_records('message_contact_requests'));
1895         // There should be still 2 blocked users.
1896         $this->assertEquals(2, $DB->count_records('message_users_blocked'));
1898         // There should be 1 muted conversation.
1899         $this->assertEquals(1, $DB->count_records('message_conversation_actions'));
1901         // There should be 3 notifications.
1902         $this->assertEquals(5, $DB->count_records('notifications'));
1904         // There should be 5 messages - 3 individual - 2 group (course2).
1905         $this->assertEquals(5, $DB->count_records('messages'));
1906         $messages = array_keys($DB->get_records('messages'));
1907         $this->assertContains($im1, $messages);
1908         $this->assertContains($im2, $messages);
1909         $this->assertContains($im3, $messages);
1910         $this->assertContains($gm4, $messages);
1911         $this->assertContains($gm5, $messages);
1913         // There should be 3 user actions - 2 for reading the message, 1 for deleting.
1914         $this->assertEquals(3, $DB->count_records('message_user_actions'));
1915         $useractions = $DB->get_records('message_user_actions');
1916         $useractions = array_map(function($action) {
1917                 return $action->messageid;
1918         }, $useractions);
1919         $this->assertNotContains($gm3, $useractions);
1921         // There should be 3 conversations - 2 individual + 1 group (course2).
1922         $this->assertEquals(3, $DB->count_records('message_conversations'));
1923         $conversations = $DB->get_records('message_conversations');
1924         $this->assertArrayNotHasKey($conversation1->id, $conversations);
1926         // There should be 6 conversation members - (2 + 2) individual + 2 group.
1927         $this->assertEquals(6, $DB->count_records('message_conversation_members'));
1929         // There should be 1 favourite conversation - the individual one.
1930         $this->assertEquals(1, $DB->count_records('favourite'));
1931     }
1933     /**
1934      * Test for provider::delete_conversations_for_all_users() in the system context.
1935      */
1936     public function test_delete_conversations_for_all_users_systemcontext() {
1937         global $DB;
1939         $this->resetAfterTest();
1940         $this->setAdminUser();
1941         $now = time();
1942         $timeread = $now - DAYSECS;
1943         $systemcontext = \context_system::instance();
1944         $component = 'core_group';
1945         $itemtype = 'groups';
1947         // Create users to test with.
1948         $user1 = $this->getDataGenerator()->create_user();
1949         $user2 = $this->getDataGenerator()->create_user();
1950         $user3 = $this->getDataGenerator()->create_user();
1951         $user4 = $this->getDataGenerator()->create_user();
1952         $user5 = $this->getDataGenerator()->create_user();
1954         // Create contacts.
1955         \core_message\api::add_contact($user1->id, $user2->id);
1956         \core_message\api::add_contact($user2->id, $user3->id);
1958         // Create contact requests.
1959         \core_message\api::create_contact_request($user1->id, $user3->id);
1960         \core_message\api::create_contact_request($user2->id, $user4->id);
1962         // Block a user.
1963         \core_message\api::block_user($user1->id, $user3->id);
1964         \core_message\api::block_user($user3->id, $user4->id);
1966         // Create individual messages.
1967         $im1 = $this->create_message($user1->id, $user2->id, $now + (9 * DAYSECS), true);
1968         $im2 = $this->create_message($user2->id, $user1->id, $now + (8 * DAYSECS), true);
1969         $im3 = $this->create_message($user2->id, $user3->id, $now + (7 * DAYSECS));
1971         // Create notifications.
1972         $n1 = $this->create_notification($user1->id, $user2->id, $now + (9 * DAYSECS), $timeread);
1973         $n2 = $this->create_notification($user2->id, $user1->id, $now + (8 * DAYSECS));
1974         $n3 = $this->create_notification($user2->id, $user3->id, $now + (7 * DAYSECS));
1976         // Delete one of the messages.
1977         \core_message\api::delete_message($user1->id, $im2);
1979         // Create course2.
1980         $course1 = $this->getDataGenerator()->create_course();
1981         $course2 = $this->getDataGenerator()->create_course();
1982         $coursecontext1 = \context_course::instance($course1->id);
1983         $coursecontext2 = \context_course::instance($course2->id);
1985         // Enrol users to courses.
1986         $this->getDataGenerator()->enrol_user($user1->id, $course1->id);
1987         $this->getDataGenerator()->enrol_user($user2->id, $course1->id);
1988         $this->getDataGenerator()->enrol_user($user3->id, $course1->id);
1989         $this->getDataGenerator()->enrol_user($user1->id, $course2->id);
1990         $this->getDataGenerator()->enrol_user($user2->id, $course2->id);
1992         // Create course groups with group messaging enabled.
1993         $group1a = $this->getDataGenerator()->create_group(array('courseid' => $course1->id, 'enablemessaging' => 1));
1994         $group2a = $this->getDataGenerator()->create_group(array('courseid' => $course2->id, 'enablemessaging' => 1));
1996         // Add users to groups.
1997         $this->getDataGenerator()->create_group_member(array('groupid' => $group1a->id, 'userid' => $user1->id));
1998         $this->getDataGenerator()->create_group_member(array('groupid' => $group1a->id, 'userid' => $user2->id));
1999         $this->getDataGenerator()->create_group_member(array('groupid' => $group1a->id, 'userid' => $user3->id));
2000         $this->getDataGenerator()->create_group_member(array('groupid' => $group2a->id, 'userid' => $user1->id));
2001         $this->getDataGenerator()->create_group_member(array('groupid' => $group2a->id, 'userid' => $user2->id));
2003         // Get conversations.
2004         $iconversation1id = \core_message\api::get_conversation_between_users([$user1->id, $user2->id]);
2005         $conversation1 = \core_message\api::get_conversation_by_area(
2006             $component,
2007             $itemtype,
2008             $group1a->id,
2009             $coursecontext1->id
2010         );
2011         $conversation2 = \core_message\api::get_conversation_by_area(
2012             $component,
2013             $itemtype,
2014             $group2a->id,
2015             $coursecontext2->id
2016         );
2018         // Make favourite some conversations.
2019         \core_message\api::set_favourite_conversation($iconversation1id, $user1->id);
2020         \core_message\api::set_favourite_conversation($conversation1->id, $user1->id);
2021         \core_message\api::set_favourite_conversation($conversation1->id, $user2->id);
2023         // Send some messages to the conversation.
2024         $gm1 = testhelper::send_fake_message_to_conversation($user1, $conversation1->id, 'Message 1.1', $now + 1);
2025         $gm2 = testhelper::send_fake_message_to_conversation($user1, $conversation1->id, 'Message 1.2', $now + 2);
2026         $gm3 = testhelper::send_fake_message_to_conversation($user2, $conversation1->id, 'Message 1.3', $now + 3);
2027         $gm4 = testhelper::send_fake_message_to_conversation($user1, $conversation2->id, 'Message 2.1', $now + 4);
2028         $gm5 = testhelper::send_fake_message_to_conversation($user2, $conversation2->id, 'Message 2.2', $now + 5);
2030         $dbgm3 = $DB->get_record('messages', ['id' => $gm3]);
2032         // Mark some conversations as muted by two users.
2033         \core_message\api::mute_conversation($user1->id, $iconversation1id);
2034         \core_message\api::mute_conversation($user1->id, $conversation1->id);
2035         \core_message\api::mute_conversation($user2->id, $conversation1->id);
2037         // Mark as read one of the conversation messages.
2038         \core_message\api::mark_message_as_read($user1->id, $dbgm3, $now + 5);
2040         // There should be 2 contacts.
2041         $this->assertEquals(2, $DB->count_records('message_contacts'));
2043         // There should be 2 contact requests.
2044         $this->assertEquals(2, $DB->count_records('message_contact_requests'));
2046         // There should be 2 blocked users.
2047         $this->assertEquals(2, $DB->count_records('message_users_blocked'));
2049         // There should be 8 messages.
2050         $this->assertEquals(8, $DB->count_records('messages'));
2052         // There should be 4 user actions - 3 for reading the message, 1 for deleting.
2053         $this->assertEquals(4, $DB->count_records('message_user_actions'));
2055         // There should be 3 muted conversations.
2056         $this->assertEquals(3, $DB->count_records('message_conversation_actions'));
2058         // There should be 4 conversations - 2 individual + 2 group.
2059         $this->assertEquals(4, $DB->count_records('message_conversations'));
2061         // There should be 9 conversation members - (2 + 2) individual + (3 + 2) group.
2062         $this->assertEquals(9, $DB->count_records('message_conversation_members'));
2064         // There should be 5 notifications (3 from create_notification and 2 from create_contact_request).
2065         $this->assertEquals(5, $DB->count_records('notifications'));
2067         // There should be 3 favourite conversations.
2068         $this->assertEquals(3, $DB->count_records('favourite'));
2070         // Delete group conversations for all users in system context.
2071         provider::delete_conversations_for_all_users($systemcontext, $component, $itemtype);
2073         // No conversations should be removed, because they are in the course context.
2074         $this->assertEquals(2, $DB->count_records('message_contacts'));
2075         $this->assertEquals(2, $DB->count_records('message_contact_requests'));
2076         $this->assertEquals(2, $DB->count_records('message_users_blocked'));
2077         $this->assertEquals(8, $DB->count_records('messages'));
2078         $this->assertEquals(4, $DB->count_records('message_user_actions'));
2079         $this->assertEquals(3, $DB->count_records('message_conversation_actions'));
2080         $this->assertEquals(4, $DB->count_records('message_conversations'));
2081         $this->assertEquals(9, $DB->count_records('message_conversation_members'));
2082         $this->assertEquals(5, $DB->count_records('notifications'));
2083         $this->assertEquals(3, $DB->count_records('favourite'));
2085         // Delete individual conversations for all users in system context.
2086         provider::delete_conversations_for_all_users($systemcontext, '', '');
2088         // No conversations should be removed, because they've been moved to user context.
2089         $this->assertEquals(2, $DB->count_records('message_contacts'));
2090         $this->assertEquals(2, $DB->count_records('message_contact_requests'));
2091         $this->assertEquals(2, $DB->count_records('message_users_blocked'));
2092         $this->assertEquals(8, $DB->count_records('messages'));
2093         $this->assertEquals(4, $DB->count_records('message_user_actions'));
2094         $this->assertEquals(3, $DB->count_records('message_conversation_actions'));
2095         $this->assertEquals(4, $DB->count_records('message_conversations'));
2096         $this->assertEquals(9, $DB->count_records('message_conversation_members'));
2097         $this->assertEquals(5, $DB->count_records('notifications'));
2098         $this->assertEquals(3, $DB->count_records('favourite'));
2099     }
2101     /**
2102      * Test for provider::delete_conversations_for_all_users() in the user context.
2103      */
2104     public function test_delete_conversations_for_all_users_usercontext() {
2105         global $DB;
2107         $this->resetAfterTest();
2108         $this->setAdminUser();
2109         $now = time();
2110         $timeread = $now - DAYSECS;
2111         $component = 'core_group';
2112         $itemtype = 'groups';
2114         // Create users to test with.
2115         $user1 = $this->getDataGenerator()->create_user();
2116         $user2 = $this->getDataGenerator()->create_user();
2117         $user3 = $this->getDataGenerator()->create_user();
2118         $user4 = $this->getDataGenerator()->create_user();
2119         $user5 = $this->getDataGenerator()->create_user();
2120         $user1context = \context_user::instance($user1->id);
2122         // Create contacts.
2123         \core_message\api::add_contact($user1->id, $user2->id);
2124         \core_message\api::add_contact($user2->id, $user3->id);
2126         // Create contact requests.
2127         \core_message\api::create_contact_request($user1->id, $user3->id);
2128         \core_message\api::create_contact_request($user2->id, $user4->id);
2130         // Block a user.
2131         \core_message\api::block_user($user1->id, $user3->id);
2132         \core_message\api::block_user($user3->id, $user4->id);
2134         // Create individual messages.
2135         $im1 = $this->create_message($user1->id, $user2->id, $now + (9 * DAYSECS), true);
2136         $im2 = $this->create_message($user2->id, $user1->id, $now + (8 * DAYSECS), true);
2137         $im3 = $this->create_message($user2->id, $user3->id, $now + (7 * DAYSECS));
2139         // Create notifications.
2140         $n1 = $this->create_notification($user1->id, $user2->id, $now + (9 * DAYSECS), $timeread);
2141         $n2 = $this->create_notification($user2->id, $user1->id, $now + (8 * DAYSECS));
2142         $n3 = $this->create_notification($user2->id, $user3->id, $now + (7 * DAYSECS));
2144         // Delete one of the messages.
2145         \core_message\api::delete_message($user1->id, $im2);
2147         // Create course2.
2148         $course1 = $this->getDataGenerator()->create_course();
2149         $course2 = $this->getDataGenerator()->create_course();
2150         $coursecontext1 = \context_course::instance($course1->id);
2151         $coursecontext2 = \context_course::instance($course2->id);
2153         // Enrol users to courses.
2154         $this->getDataGenerator()->enrol_user($user1->id, $course1->id);
2155         $this->getDataGenerator()->enrol_user($user2->id, $course1->id);
2156         $this->getDataGenerator()->enrol_user($user3->id, $course1->id);
2157         $this->getDataGenerator()->enrol_user($user1->id, $course2->id);
2158         $this->getDataGenerator()->enrol_user($user2->id, $course2->id);
2160         // Create course groups with group messaging enabled.
2161         $group1a = $this->getDataGenerator()->create_group(array('courseid' => $course1->id, 'enablemessaging' => 1));
2162         $group2a = $this->getDataGenerator()->create_group(array('courseid' => $course2->id, 'enablemessaging' => 1));
2164         // Add users to groups.
2165         $this->getDataGenerator()->create_group_member(array('groupid' => $group1a->id, 'userid' => $user1->id));
2166         $this->getDataGenerator()->create_group_member(array('groupid' => $group1a->id, 'userid' => $user2->id));
2167         $this->getDataGenerator()->create_group_member(array('groupid' => $group1a->id, 'userid' => $user3->id));
2168         $this->getDataGenerator()->create_group_member(array('groupid' => $group2a->id, 'userid' => $user1->id));
2169         $this->getDataGenerator()->create_group_member(array('groupid' => $group2a->id, 'userid' => $user2->id));
2171         // Get conversation.
2172         $iconversation1id = \core_message\api::get_conversation_between_users([$user1->id, $user2->id]);
2173         $iconversation2id = \core_message\api::get_conversation_between_users([$user2->id, $user3->id]);
2174         $conversation1 = \core_message\api::get_conversation_by_area(
2175             $component,
2176             $itemtype,
2177             $group1a->id,
2178             $coursecontext1->id
2179         );
2180         $conversation2 = \core_message\api::get_conversation_by_area(
2181             $component,
2182             $itemtype,
2183             $group2a->id,
2184             $coursecontext2->id
2185         );
2187         // Make favourite some conversations.
2188         \core_message\api::set_favourite_conversation($iconversation1id, $user1->id);
2189         \core_message\api::set_favourite_conversation($conversation1->id, $user1->id);
2190         \core_message\api::set_favourite_conversation($conversation1->id, $user2->id);
2192         // Send some messages to the conversation.
2193         $gm1 = testhelper::send_fake_message_to_conversation($user1, $conversation1->id, 'Message 1.1', $now + 1);
2194         $gm2 = testhelper::send_fake_message_to_conversation($user1, $conversation1->id, 'Message 1.2', $now + 2);
2195         $gm3 = testhelper::send_fake_message_to_conversation($user2, $conversation1->id, 'Message 1.3', $now + 3);
2196         $gm4 = testhelper::send_fake_message_to_conversation($user1, $conversation2->id, 'Message 2.1', $now + 4);
2197         $gm5 = testhelper::send_fake_message_to_conversation($user2, $conversation2->id, 'Message 2.2', $now + 5);
2199         $dbgm3 = $DB->get_record('messages', ['id' => $gm3]);
2201         // Mark as read one of the conversation messages.
2202         \core_message\api::mark_message_as_read($user1->id, $dbgm3, $now + 5);
2204         // Mark some of the conversations as muted by two users.
2205         \core_message\api::mute_conversation($user1->id, $iconversation1id);
2206         \core_message\api::mute_conversation($user1->id, $conversation1->id);
2207         \core_message\api::mute_conversation($user2->id, $conversation1->id);
2209         // There should be 2 contacts.
2210         $this->assertEquals(2, $DB->count_records('message_contacts'));
2212         // There should be 2 contact requests.
2213         $this->assertEquals(2, $DB->count_records('message_contact_requests'));
2215         // There should be 2 blocked users.
2216         $this->assertEquals(2, $DB->count_records('message_users_blocked'));
2218         // There should be 8 messages - 3 individual + 5 group.
2219         $this->assertEquals(8, $DB->count_records('messages'));
2221         // There should be 4 user actions - 3 for reading the message, 1 for deleting.
2222         $this->assertEquals(4, $DB->count_records('message_user_actions'));
2224         // There should be 3 muted conversations.
2225         $this->assertEquals(3, $DB->count_records('message_conversation_actions'));
2227         // There should be 4 conversations - 2 individual + 2 group.
2228         $this->assertEquals(4, $DB->count_records('message_conversations'));
2230         // There should be 9 conversation members - (2 + 2) individual + (3 + 2) group.
2231         $this->assertEquals(9, $DB->count_records('message_conversation_members'));
2233         // There should be 5 notifications (3 from create_notification and 2 from create_contact_request).
2234         $this->assertEquals(5, $DB->count_records('notifications'));
2236         // There should be 3 favourite conversations.
2237         $this->assertEquals(3, $DB->count_records('favourite'));
2239         // Delete group conversations for all users in user context.
2240         provider::delete_conversations_for_all_users($user1context, $component, $itemtype);
2242         // No conversations should be removed, because they are in the course context.
2243         $this->assertEquals(2, $DB->count_records('message_contacts'));
2244         $this->assertEquals(2, $DB->count_records('message_contact_requests'));
2245         $this->assertEquals(2, $DB->count_records('message_users_blocked'));
2246         $this->assertEquals(8, $DB->count_records('messages'));
2247         $this->assertEquals(4, $DB->count_records('message_user_actions'));
2248         $this->assertEquals(3, $DB->count_records('message_conversation_actions'));
2249         $this->assertEquals(4, $DB->count_records('message_conversations'));
2250         $this->assertEquals(9, $DB->count_records('message_conversation_members'));
2251         $this->assertEquals(5, $DB->count_records('notifications'));
2252         $this->assertEquals(3, $DB->count_records('favourite'));
2254         // Delete individual conversations for all users in user context.
2255         provider::delete_conversations_for_all_users($user1context, '', '');
2257         // No conversations should be removed, because they are in the course context.
2258         $this->assertEquals(2, $DB->count_records('message_contacts'));
2259         $this->assertEquals(2, $DB->count_records('message_contact_requests'));
2260         $this->assertEquals(2, $DB->count_records('message_users_blocked'));
2261         $this->assertEquals(8, $DB->count_records('messages'));
2262         $this->assertEquals(4, $DB->count_records('message_user_actions'));
2263         $this->assertEquals(3, $DB->count_records('message_conversation_actions'));
2264         $this->assertEquals(4, $DB->count_records('message_conversations'));
2265         $this->assertEquals(9, $DB->count_records('message_conversation_members'));
2266         $this->assertEquals(5, $DB->count_records('notifications'));
2267         $this->assertEquals(3, $DB->count_records('favourite'));
2268     }
2270     /**
2271      * Test for provider::delete_conversations_for_user().
2272      */
2273     public function test_delete_conversations_for_user() {
2274         global $DB;
2276         $this->resetAfterTest();
2277         $this->setAdminUser();
2278         $now = time();
2279         $timeread = $now - DAYSECS;
2280         $systemcontext = \context_system::instance();
2281         $component = 'core_group';
2282         $itemtype = 'groups';
2284         // Create users to test with.
2285         $user1 = $this->getDataGenerator()->create_user();
2286         $user2 = $this->getDataGenerator()->create_user();
2287         $user3 = $this->getDataGenerator()->create_user();
2288         $user4 = $this->getDataGenerator()->create_user();
2289         $user5 = $this->getDataGenerator()->create_user();
2290         $user1context = \context_user::instance($user1->id);
2292         // Create contacts.
2293         \core_message\api::add_contact($user1->id, $user2->id);
2294         \core_message\api::add_contact($user2->id, $user3->id);
2296         // Create contact requests.
2297         \core_message\api::create_contact_request($user1->id, $user3->id);
2298         \core_message\api::create_contact_request($user2->id, $user4->id);
2300         // Block a user.
2301         \core_message\api::block_user($user1->id, $user3->id);
2302         \core_message\api::block_user($user3->id, $user4->id);
2304         // Create private messages.
2305         $pm1 = $this->create_message($user1->id, $user2->id, $now + (9 * DAYSECS), true);
2306         $pm2 = $this->create_message($user2->id, $user1->id, $now + (8 * DAYSECS), true);
2307         $pm3 = $this->create_message($user2->id, $user3->id, $now + (7 * DAYSECS));
2309         // Create notifications.
2310         $n1 = $this->create_notification($user1->id, $user2->id, $now + (9 * DAYSECS), $timeread);
2311         $n2 = $this->create_notification($user2->id, $user1->id, $now + (8 * DAYSECS));
2312         $n3 = $this->create_notification($user2->id, $user3->id, $now + (7 * DAYSECS));
2314         // Delete one of the messages.
2315         \core_message\api::delete_message($user1->id, $pm2);
2317         // Create course.
2318         $course1 = $this->getDataGenerator()->create_course();
2319         $course2 = $this->getDataGenerator()->create_course();
2320         $coursecontext1 = \context_course::instance($course1->id);
2321         $coursecontext2 = \context_course::instance($course2->id);
2323         // Enrol users to courses.
2324         $this->getDataGenerator()->enrol_user($user1->id, $course1->id);
2325         $this->getDataGenerator()->enrol_user($user2->id, $course1->id);
2326         $this->getDataGenerator()->enrol_user($user3->id, $course1->id);
2327         $this->getDataGenerator()->enrol_user($user1->id, $course2->id);
2329         // Create course groups with group messaging enabled.
2330         $group1a = $this->getDataGenerator()->create_group(array('courseid' => $course1->id, 'enablemessaging' => 1));
2332         // Add users to groups.
2333         $this->getDataGenerator()->create_group_member(array('groupid' => $group1a->id, 'userid' => $user1->id));
2334         $this->getDataGenerator()->create_group_member(array('groupid' => $group1a->id, 'userid' => $user2->id));
2335         $this->getDataGenerator()->create_group_member(array('groupid' => $group1a->id, 'userid' => $user3->id));
2337         // Get conversation.
2338         $iconversation1id = \core_message\api::get_conversation_between_users([$user1->id, $user2->id]);
2339         $conversation1 = \core_message\api::get_conversation_by_area(
2340             $component,
2341             $itemtype,
2342             $group1a->id,
2343             $coursecontext1->id
2344         );
2346         // Make favourite some conversations.
2347         \core_message\api::set_favourite_conversation($iconversation1id, $user1->id);
2348         \core_message\api::set_favourite_conversation($conversation1->id, $user1->id);
2349         \core_message\api::set_favourite_conversation($conversation1->id, $user2->id);
2351         // Send some messages to the conversation.
2352         $gm1 = testhelper::send_fake_message_to_conversation($user1, $conversation1->id, 'Message 1', $now + 1);
2353         $gm2 = testhelper::send_fake_message_to_conversation($user1, $conversation1->id, 'Message 2', $now + 2);
2354         $gm3 = testhelper::send_fake_message_to_conversation($user2, $conversation1->id, 'Message 3', $now + 3);
2356         $dbm3 = $DB->get_record('messages', ['id' => $gm3]);
2358         // Mark as read one of the conversation messages.
2359         \core_message\api::mark_message_as_read($user1->id, $dbm3, $now + 5);
2361         // Mark some of the conversations as muted by two users.
2362         \core_message\api::mute_conversation($user1->id, $iconversation1id);
2363         \core_message\api::mute_conversation($user1->id, $conversation1->id);
2364         \core_message\api::mute_conversation($user2->id, $conversation1->id);
2366         // There should be 2 contacts.
2367         $this->assertEquals(2, $DB->count_records('message_contacts'));
2369         // There should be 2 contact requests.
2370         $this->assertEquals(2, $DB->count_records('message_contact_requests'));
2372         // There should be 2 blocked users.
2373         $this->assertEquals(2, $DB->count_records('message_users_blocked'));
2375         // There should be 5 notifications.
2376         $this->assertEquals(5, $DB->count_records('notifications'));
2378         // There should be 6 messages.
2379         $this->assertEquals(6, $DB->count_records('messages'));
2381         // There should be 4 user actions - 3 for reading the message, one for deleting.
2382         $this->assertEquals(4, $DB->count_records('message_user_actions'));
2384         // There should be 3 users muting a conversation.
2385         $this->assertEquals(3, $DB->count_records('message_conversation_actions'));
2387         // There should be 3 conversations - 2 private + 1 group.
2388         $this->assertEquals(3, $DB->count_records('message_conversations'));
2390         // There should be 7 conversation members - 2 + 2 private conversations + 3 group conversation.
2391         $this->assertEquals(7, $DB->count_records('message_conversation_members'));
2392         $members = $DB->get_records('message_conversation_members', ['conversationid' => $conversation1->id]);
2393         $members = array_map(function($member) {
2394                 return $member->userid;
2395         }, $members);
2396         $this->assertContains($user1->id, $members);
2398         // There should be three favourite conversations.
2399         $this->assertEquals(3, $DB->count_records('favourite'));
2401         // Delete group conversations for user1 in course1 and course2.
2402         $approvedcontextlist = new \core_privacy\tests\request\approved_contextlist($user1, 'core_message',
2403                 [$coursecontext1->id, $coursecontext2->id]);
2404         provider::delete_conversations_for_user($approvedcontextlist, $component, $itemtype);
2406         // There should be still 2 contacts.
2407         $this->assertEquals(2, $DB->count_records('message_contacts'));
2409         // There should be still 2 contact requests.
2410         $this->assertEquals(2, $DB->count_records('message_contact_requests'));
2412         // There should be still 2 blocked users.
2413         $this->assertEquals(2, $DB->count_records('message_users_blocked'));
2415         // There should be 2 muted conversation.
2416         $this->assertEquals(2, $DB->count_records('message_conversation_actions'));
2418         // There should be 3 notifications.
2419         $this->assertEquals(5, $DB->count_records('notifications'));
2421         // There should be 4 messages - 3 private + 1 group sent by user2.
2422         $this->assertEquals(4, $DB->count_records('messages'));
2423         $messages = array_keys($DB->get_records('messages'));
2424         $this->assertContains($pm1, $messages);
2425         $this->assertContains($pm2, $messages);
2426         $this->assertContains($pm3, $messages);
2427         $this->assertContains($gm3, $messages);
2429         // There should be 3 user actions - 2 for reading the message, one for deleting.
2430         $this->assertEquals(3, $DB->count_records('message_user_actions'));
2431         $useractions = $DB->get_records('message_user_actions');
2432         $useractions = array_map(function($action) {
2433                 return $action->messageid;
2434         }, $useractions);
2435         $this->assertNotContains($gm3, $useractions);
2437         // There should be still 3 conversations - 2 private + 1 group.
2438         $this->assertEquals(3, $DB->count_records('message_conversations'));
2440         // There should be 6 conversation members - 2 + 2 private conversations + 2 group conversation.
2441         $this->assertEquals(6, $DB->count_records('message_conversation_members'));
2442         $members = $DB->get_records('message_conversation_members', ['conversationid' => $conversation1->id]);
2443         $members = array_map(function($member) {
2444                 return $member->userid;
2445         }, $members);
2446         $this->assertNotContains($user1->id, $members);
2448         // There should be 2 favourite conversations - 2 group.
2449         $this->assertEquals(2, $DB->count_records('favourite'));
2450         $favourites = $DB->get_records('favourite');
2451         foreach ($favourites as $favourite) {
2452             if ($favourite->userid == $user1->id) {
2453                 $this->assertEquals($iconversation1id, $favourite->itemid);
2454             } else if ($favourite->userid == $user2->id) {
2455                 $this->assertEquals($conversation1->id, $favourite->itemid);
2456             }
2457         }
2458     }
2461     /**
2462      * Test for provider::delete_conversations_for_users().
2463      */
2464     public function test_delete_conversations_for_users() {
2465         global $DB;
2467         $this->resetAfterTest();
2468         $this->setAdminUser();
2469         $now = time();
2470         $timeread = $now - DAYSECS;
2471         $systemcontext = \context_system::instance();
2472         $component = 'core_group';
2473         $itemtype = 'groups';
2475         // Create users to test with.
2476         $user1 = $this->getDataGenerator()->create_user();
2477         $user2 = $this->getDataGenerator()->create_user();
2478         $user3 = $this->getDataGenerator()->create_user();
2479         $user4 = $this->getDataGenerator()->create_user();
2480         $user5 = $this->getDataGenerator()->create_user();
2481         $user1context = \context_user::instance($user1->id);
2483         // Create contacts.
2484         \core_message\api::add_contact($user1->id, $user2->id);
2485         \core_message\api::add_contact($user2->id, $user3->id);
2487         // Create contact requests.
2488         \core_message\api::create_contact_request($user1->id, $user3->id);
2489         \core_message\api::create_contact_request($user2->id, $user4->id);
2491         // Block a user.
2492         \core_message\api::block_user($user1->id, $user3->id);
2493         \core_message\api::block_user($user3->id, $user4->id);
2495         // Create private messages.
2496         $pm1 = $this->create_message($user1->id, $user2->id, $now + (9 * DAYSECS), true);
2497         $pm2 = $this->create_message($user2->id, $user1->id, $now + (8 * DAYSECS), true);
2498         $pm3 = $this->create_message($user2->id, $user3->id, $now + (7 * DAYSECS));
2500         // Create notifications.
2501         $n1 = $this->create_notification($user1->id, $user2->id, $now + (9 * DAYSECS), $timeread);
2502         $n2 = $this->create_notification($user2->id, $user1->id, $now + (8 * DAYSECS));
2503         $n3 = $this->create_notification($user2->id, $user3->id, $now + (7 * DAYSECS));
2505         // Delete one of the messages.
2506         \core_message\api::delete_message($user1->id, $pm2);
2508         // Create course.
2509         $course1 = $this->getDataGenerator()->create_course();
2510         $course2 = $this->getDataGenerator()->create_course();
2511         $coursecontext1 = \context_course::instance($course1->id);
2512         $coursecontext2 = \context_course::instance($course2->id);
2514         // Enrol users to courses.
2515         $this->getDataGenerator()->enrol_user($user1->id, $course1->id);
2516         $this->getDataGenerator()->enrol_user($user2->id, $course1->id);
2517         $this->getDataGenerator()->enrol_user($user3->id, $course1->id);
2518         $this->getDataGenerator()->enrol_user($user4->id, $course1->id);
2519         $this->getDataGenerator()->enrol_user($user1->id, $course2->id);
2521         // Create course groups with group messaging enabled.
2522         $group1a = $this->getDataGenerator()->create_group(array('courseid' => $course1->id, 'enablemessaging' => 1));
2523         $group2a = $this->getDataGenerator()->create_group(array('courseid' => $course2->id, 'enablemessaging' => 1));
2525         // Add users to groups.
2526         $this->getDataGenerator()->create_group_member(array('groupid' => $group1a->id, 'userid' => $user1->id));
2527         $this->getDataGenerator()->create_group_member(array('groupid' => $group1a->id, 'userid' => $user2->id));
2528         $this->getDataGenerator()->create_group_member(array('groupid' => $group1a->id, 'userid' => $user3->id));
2529         $this->getDataGenerator()->create_group_member(array('groupid' => $group1a->id, 'userid' => $user4->id));
2531         // Get conversation.
2532         $iconversation1id = \core_message\api::get_conversation_between_users([$user1->id, $user2->id]);
2533         $conversation1 = \core_message\api::get_conversation_by_area(
2534             $component,
2535             $itemtype,
2536             $group1a->id,
2537             $coursecontext1->id
2538         );
2540         // Make favourite some conversations.
2541         \core_message\api::set_favourite_conversation($iconversation1id, $user1->id);
2542         \core_message\api::set_favourite_conversation($conversation1->id, $user1->id);
2543         \core_message\api::set_favourite_conversation($conversation1->id, $user3->id);
2545         // Send some messages to the conversation.
2546         $gm1 = testhelper::send_fake_message_to_conversation($user1, $conversation1->id, 'Message 1', $now + 1);
2547         $gm2 = testhelper::send_fake_message_to_conversation($user2, $conversation1->id, 'Message 2', $now + 2);
2548         $gm3 = testhelper::send_fake_message_to_conversation($user3, $conversation1->id, 'Message 3', $now + 3);
2550         $dbm3 = $DB->get_record('messages', ['id' => $gm3]);
2552         // Mark as read one of the conversation messages.
2553         \core_message\api::mark_message_as_read($user1->id, $dbm3, $now + 5);
2555         // Mark some of the conversations as muted by two users.
2556         \core_message\api::mute_conversation($user1->id, $iconversation1id);
2557         \core_message\api::mute_conversation($user1->id, $conversation1->id);
2558         \core_message\api::mute_conversation($user2->id, $conversation1->id);
2560         // There should be 2 contacts.
2561         $this->assertEquals(2, $DB->count_records('message_contacts'));
2563         // There should be 2 contact requests.
2564         $this->assertEquals(2, $DB->count_records('message_contact_requests'));
2566         // There should be 2 blocked users.
2567         $this->assertEquals(2, $DB->count_records('message_users_blocked'));
2569         // There should be 5 notifications (3 from create_notification and 2 from create_contact_request).
2570         $this->assertEquals(5, $DB->count_records('notifications'));
2572         // There should be 6 messages.
2573         $this->assertEquals(6, $DB->count_records('messages'));
2575         // There should be 4 user actions - 3 for reading the message, one for deleting.
2576         $this->assertEquals(4, $DB->count_records('message_user_actions'));
2578         // There should be 3 muted conversation.
2579         $this->assertEquals(3, $DB->count_records('message_conversation_actions'));
2581         // There should be 3 conversations - 2 private + 2 group.
2582         $this->assertEquals(4, $DB->count_records('message_conversations'));
2584         // There should be 8 conversation members - (2 + 2) private + 4 group.
2585         $this->assertEquals(8, $DB->count_records('message_conversation_members'));
2586         $members = $DB->get_records('message_conversation_members', ['conversationid' => $conversation1->id]);
2587         $members = array_map(function($member) {
2588                 return $member->userid;
2589         }, $members);
2590         $this->assertContains($user1->id, $members);
2591         $this->assertContains($user4->id, $members);
2593         // There should be 3 favourite conversations.
2594         $this->assertEquals(3, $DB->count_records('favourite'));
2596         // Delete group conversations for user1 and user2 in course2 context.
2597         $approveduserlist = new \core_privacy\local\request\approved_userlist($coursecontext2, 'core_message',
2598                 [$user1->id, $user2->id]);
2599         provider::delete_conversations_for_users($approveduserlist, $component, $itemtype);
2601         // There should be exactly the same content, because $user1 and $user2 don't belong to any group in course2).
2602         $this->assertEquals(2, $DB->count_records('message_contacts'));
2603         $this->assertEquals(2, $DB->count_records('message_contact_requests'));
2604         $this->assertEquals(2, $DB->count_records('message_users_blocked'));
2605         $this->assertEquals(5, $DB->count_records('notifications'));
2606         $this->assertEquals(6, $DB->count_records('messages'));
2607         $this->assertEquals(4, $DB->count_records('message_user_actions'));
2608         $this->assertEquals(3, $DB->count_records('message_conversation_actions'));
2609         $this->assertEquals(4, $DB->count_records('message_conversations'));
2610         $this->assertEquals(8, $DB->count_records('message_conversation_members'));
2611         $this->assertEquals(3, $DB->count_records('favourite'));
2613         // Delete group conversations for user4 in course1 context.
2614         $approveduserlist = new \core_privacy\local\request\approved_userlist($coursecontext1, 'core_message',
2615                 [$user4->id]);
2616         provider::delete_conversations_for_users($approveduserlist, $component, $itemtype);
2618         // There should be the same content except for the members (to remove user4 from the group1 in course1).
2619         $this->assertEquals(2, $DB->count_records('message_contacts'));
2620         $this->assertEquals(2, $DB->count_records('message_contact_requests'));
2621         $this->assertEquals(2, $DB->count_records('message_users_blocked'));
2622         $this->assertEquals(5, $DB->count_records('notifications'));
2623         $this->assertEquals(6, $DB->count_records('messages'));
2624         $this->assertEquals(4, $DB->count_records('message_user_actions'));
2625         $this->assertEquals(3, $DB->count_records('message_conversation_actions'));
2626         $this->assertEquals(4, $DB->count_records('message_conversations'));
2627         $this->assertEquals(3, $DB->count_records('favourite'));
2628         // There should be 7 conversation members - (2 + 2) private + 3 group.
2629         $this->assertEquals(7, $DB->count_records('message_conversation_members'));
2631         // Delete group conversations for user1 and user2 in course1 context.
2632         $approveduserlist = new \core_privacy\local\request\approved_userlist($coursecontext1, 'core_message',
2633                 [$user1->id, $user2->id]);
2634         provider::delete_conversations_for_users($approveduserlist, $component, $itemtype);
2636         // There should be still 2 contacts.
2637         $this->assertEquals(2, $DB->count_records('message_contacts'));
2639         // There should be still 2 contact requests.
2640         $this->assertEquals(2, $DB->count_records('message_contact_requests'));
2642         // There should be still 2 blocked users.
2643         $this->assertEquals(2, $DB->count_records('message_users_blocked'));
2645         // There should be 5 notifications.
2646         $this->assertEquals(5, $DB->count_records('notifications'));
2648         // There should be 4 messages - 3 private + 1 group sent by user3.
2649         $this->assertEquals(4, $DB->count_records('messages'));
2650         $messages = array_keys($DB->get_records('messages'));
2651         $this->assertContains($pm1, $messages);
2652         $this->assertContains($pm2, $messages);
2653         $this->assertContains($pm3, $messages);
2654         $this->assertContains($gm3, $messages);
2656         // There should be 3 user actions - 2 for reading the message, one for deleting.
2657         $this->assertEquals(3, $DB->count_records('message_user_actions'));
2658         $useractions = $DB->get_records('message_user_actions');
2659         $useractions = array_map(function($action) {
2660                 return $action->messageid;
2661         }, $useractions);
2662         $this->assertNotContains($gm3, $useractions);
2664         // There should be 1 muted conversation.
2665         $this->assertEquals(1, $DB->count_records('message_conversation_actions'));
2667         // There should be still 4 conversations - 2 private + 2 group.
2668         $this->assertEquals(4, $DB->count_records('message_conversations'));
2670         // There should be 5 conversation members - (2 + 2) private + 1 group.
2671         $this->assertEquals(5, $DB->count_records('message_conversation_members'));
2672         $members = $DB->get_records('message_conversation_members', ['conversationid' => $conversation1->id]);
2673         $members = array_map(function($member) {
2674                 return $member->userid;
2675         }, $members);
2676         $this->assertNotContains($user1->id, $members);
2677         $this->assertNotContains($user2->id, $members);
2679         // There should be 2 favourite conversations - user1 individual + user3 group.
2680         $this->assertEquals(2, $DB->count_records('favourite'));
2681         $favourites = $DB->get_records('favourite');
2682         foreach ($favourites as $favourite) {
2683             if ($favourite->userid == $user1->id) {
2684                 $this->assertEquals($iconversation1id, $favourite->itemid);
2685             } else if ($favourite->userid == $user3->id) {
2686                 $this->assertEquals($conversation1->id, $favourite->itemid);
2687             }
2688         }
2689     }
2691     /**
2692      * Creates a message to be used for testing.
2693      *
2694      * @param int $useridfrom The user id from
2695      * @param int $useridto The user id to
2696      * @param int $timecreated
2697      * @param bool $read Do we want to mark the message as read?
2698      * @return int The id of the message
2699      * @throws dml_exception
2700      */
2701     private function create_message(int $useridfrom, int $useridto, int $timecreated = null, bool $read = false) {
2702         global $DB;
2704         static $i = 1;
2706         if (is_null($timecreated)) {
2707             $timecreated = time();
2708         }
2710         if (!$conversationid = \core_message\api::get_conversation_between_users([$useridfrom, $useridto])) {
2711             $conversation = \core_message\api::create_conversation(
2712                 \core_message\api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL,
2713                 [
2714                     $useridfrom,
2715                     $useridto
2716                 ]
2717             );
2718             $conversationid = $conversation->id;
2719         }
2721         // Ok, send the message.
2722         $record = new stdClass();
2723         $record->useridfrom = $useridfrom;
2724         $record->conversationid = $conversationid;
2725         $record->subject = 'No subject';
2726         $record->fullmessage = 'A rad message ' . $i;
2727         $record->smallmessage = 'A rad message ' . $i;
2728         $record->timecreated = $timecreated;
2729         $record->customdata = json_encode(['akey' => 'avalue']);
2731         $i++;
2733         $record->id = $DB->insert_record('messages', $record);
2735         if ($read) {
2736             \core_message\api::mark_message_as_read($useridto, $record);
2737         }
2739         return $record->id;
2740     }
2742     /**
2743      * Creates a notification to be used for testing.
2744      *
2745      * @param int $useridfrom The user id from
2746      * @param int $useridto The user id to
2747      * @param int|null $timecreated The time the notification was created
2748      * @param int|null $timeread The time the notification was read, null if it hasn't been.
2749      * @return int The id of the notification
2750      * @throws dml_exception
2751      */
2752     private function create_notification(int $useridfrom, int $useridto, int $timecreated = null, int $timeread = null) {
2753         global $DB;
2755         static $i = 1;
2757         if (is_null($timecreated)) {
2758             $timecreated = time();
2759         }
2761         $record = new stdClass();
2762         $record->useridfrom = $useridfrom;
2763         $record->useridto = $useridto;
2764         $record->subject = 'No subject';
2765         $record->fullmessage = 'Some rad notification ' . $i;
2766         $record->smallmessage = 'Yo homie, you got some stuff to do, yolo. ' . $i;
2767         $record->timeread = $timeread;
2768         $record->timecreated = $timecreated;
2769         $record->customdata = json_encode(['akey' => 'avalue']);
2771         $i++;
2773         return $DB->insert_record('notifications', $record);
2774     }
2776     /**
2777      * Comparison function for sorting messages.
2778      *
2779      * @param   \stdClass $a
2780      * @param   \stdClass $b
2781      * @return  bool
2782      */
2783     protected static function sort_messages($a, $b) {
2784         return $a->message > $b->message;
2785     }
2787     /**
2788      * Comparison function for sorting contacts.
2789      *
2790      * @param   \stdClass $a
2791      * @param   \stdClass $b
2792      * @return  bool
2793      */
2794     protected static function sort_contacts($a, $b) {
2795         return $a->contact > $b->contact;
2796     }