6fc24366528708a217c825c781c44bdee35e338a
[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->assertEquals('privacy:metadata:messages', $messagestable->get_summary());
94         $privacyfields = $messageuseractionstable->get_privacy_fields();
95         $this->assertArrayHasKey('userid', $privacyfields);
96         $this->assertArrayHasKey('messageid', $privacyfields);
97         $this->assertArrayHasKey('action', $privacyfields);
98         $this->assertArrayHasKey('timecreated', $privacyfields);
99         $this->assertEquals('privacy:metadata:message_user_actions', $messageuseractionstable->get_summary());
101         $privacyfields = $messageconversationmemberstable->get_privacy_fields();
102         $this->assertArrayHasKey('conversationid', $privacyfields);
103         $this->assertArrayHasKey('userid', $privacyfields);
104         $this->assertArrayHasKey('timecreated', $privacyfields);
105         $this->assertEquals('privacy:metadata:message_conversation_members', $messageconversationmemberstable->get_summary());
107         $privacyfields = $messagecontacts->get_privacy_fields();
108         $this->assertArrayHasKey('userid', $privacyfields);
109         $this->assertArrayHasKey('contactid', $privacyfields);
110         $this->assertArrayHasKey('timecreated', $privacyfields);
111         $this->assertEquals('privacy:metadata:message_contacts', $messagecontacts->get_summary());
113         $privacyfields = $messagecontactrequests->get_privacy_fields();
114         $this->assertArrayHasKey('userid', $privacyfields);
115         $this->assertArrayHasKey('requesteduserid', $privacyfields);
116         $this->assertArrayHasKey('timecreated', $privacyfields);
117         $this->assertEquals('privacy:metadata:message_contact_requests', $messagecontactrequests->get_summary());
119         $privacyfields = $messageusersblocked->get_privacy_fields();
120         $this->assertArrayHasKey('userid', $privacyfields);
121         $this->assertArrayHasKey('blockeduserid', $privacyfields);
122         $this->assertArrayHasKey('timecreated', $privacyfields);
123         $this->assertEquals('privacy:metadata:message_users_blocked', $messageusersblocked->get_summary());
125         $privacyfields = $notificationstable->get_privacy_fields();
126         $this->assertArrayHasKey('useridfrom', $privacyfields);
127         $this->assertArrayHasKey('useridto', $privacyfields);
128         $this->assertArrayHasKey('subject', $privacyfields);
129         $this->assertArrayHasKey('fullmessage', $privacyfields);
130         $this->assertArrayHasKey('fullmessageformat', $privacyfields);
131         $this->assertArrayHasKey('fullmessagehtml', $privacyfields);
132         $this->assertArrayHasKey('smallmessage', $privacyfields);
133         $this->assertArrayHasKey('component', $privacyfields);
134         $this->assertArrayHasKey('eventtype', $privacyfields);
135         $this->assertArrayHasKey('contexturl', $privacyfields);
136         $this->assertArrayHasKey('contexturlname', $privacyfields);
137         $this->assertArrayHasKey('timeread', $privacyfields);
138         $this->assertArrayHasKey('timecreated', $privacyfields);
139         $this->assertEquals('privacy:metadata:notifications', $notificationstable->get_summary());
140     }
142     /**
143      * Test for provider::export_user_preferences().
144      */
145     public function test_export_user_preferences_no_pref() {
146         $this->resetAfterTest();
148         $user = $this->getDataGenerator()->create_user();
149         provider::export_user_preferences($user->id);
151         $writer = writer::with_context(\context_system::instance());
153         $this->assertFalse($writer->has_any_data());
154     }
156     /**
157      * Test for provider::export_user_preferences().
158      */
159     public function test_export_user_preferences() {
160         global $USER;
162         $this->resetAfterTest();
164         $this->setAdminUser();
166         // Create another user to set a preference for who we won't be exporting.
167         $user = $this->getDataGenerator()->create_user();
169         // Set some message user preferences.
170         set_user_preference('message_provider_moodle_instantmessage_loggedin', 'airnotifier', $USER->id);
171         set_user_preference('message_provider_moodle_instantmessage_loggedoff', 'popup', $USER->id);
172         set_user_preference('message_blocknoncontacts', \core_message\api::MESSAGE_PRIVACY_ONLYCONTACTS, $USER->id);
173         set_user_preference('message_entertosend', true, $USER->id);
174         set_user_preference('message_provider_moodle_instantmessage_loggedoff', 'inbound', $user->id);
176         // Set an unrelated preference.
177         set_user_preference('some_unrelated_preference', 'courses', $USER->id);
179         provider::export_user_preferences($USER->id);
181         $writer = writer::with_context(\context_system::instance());
183         $this->assertTrue($writer->has_any_data());
185         $prefs = (array) $writer->get_user_preferences('core_message');
187         // Check only 3 preferences exist.
188         $this->assertCount(4, $prefs);
189         $this->assertArrayHasKey('message_provider_moodle_instantmessage_loggedin', $prefs);
190         $this->assertArrayHasKey('message_provider_moodle_instantmessage_loggedoff', $prefs);
191         $this->assertArrayHasKey('message_blocknoncontacts', $prefs);
192         $this->assertArrayHasKey('message_entertosend', $prefs);
194         foreach ($prefs as $key => $pref) {
195             if ($key == 'message_provider_moodle_instantmessage_loggedin') {
196                 $this->assertEquals('airnotifier', $pref->value);
197             } else if ($key == 'message_provider_moodle_instantmessage_loggedoff') {
198                 $this->assertEquals('popup', $pref->value);
199             } else {
200                 $this->assertEquals(1, $pref->value);
201             }
202         }
203     }
205     /**
206      * Test for provider::get_contexts_for_userid() when there is no message or notification.
207      */
208     public function test_get_contexts_for_userid_no_data() {
209         $this->resetAfterTest();
211         $user = $this->getDataGenerator()->create_user();
212         $contextlist = provider::get_contexts_for_userid($user->id);
213         $this->assertEmpty($contextlist);
214     }
216     /**
217      * Test for provider::get_contexts_for_userid() when there is a private message between users.
218      */
219     public function test_get_contexts_for_userid_with_private_messages() {
220         $this->resetAfterTest();
222         $user1 = $this->getDataGenerator()->create_user();
223         $user2 = $this->getDataGenerator()->create_user();
224         $user3 = $this->getDataGenerator()->create_user();
226         // Test nothing is found before group conversations is created or message is sent.
227         $contextlist = provider::get_contexts_for_userid($user1->id);
228         $this->assertCount(0, $contextlist);
229         $contextlist = provider::get_contexts_for_userid($user2->id);
230         $this->assertCount(0, $contextlist);
232         // Send some private messages.
233         $pm1id = $this->create_message($user1->id, $user2->id, time() - (9 * DAYSECS));
235         // Test for the sender (user1).
236         $contextlist = provider::get_contexts_for_userid($user1->id);
237         $this->assertCount(1, $contextlist);
238         $contextforuser = $contextlist->current();
239         $this->assertEquals(
240                 \context_user::instance($user1->id)->id,
241                 $contextforuser->id);
243         // Test for the receiver (user2).
244         $contextlist = provider::get_contexts_for_userid($user2->id);
245         $this->assertCount(1, $contextlist);
246         $contextforuser = $contextlist->current();
247         $this->assertEquals(
248                 \context_user::instance($user2->id)->id,
249                 $contextforuser->id);
251         // Test for user3 (no private messages).
252         $contextlist = provider::get_contexts_for_userid($user3->id);
253         $this->assertCount(0, $contextlist);
254     }
256     /**
257      * Test for provider::get_contexts_for_userid() when there is several messages (private and group).
258      */
259     public function test_get_contexts_for_userid_with_messages() {
260         $this->resetAfterTest();
261         $this->setAdminUser();
263         $user1 = $this->getDataGenerator()->create_user();
264         $user2 = $this->getDataGenerator()->create_user();
265         $user3 = $this->getDataGenerator()->create_user();
266         $user4 = $this->getDataGenerator()->create_user();
268         // Test nothing is found before group conversations is created or message is sent.
269         $contextlist = provider::get_contexts_for_userid($user1->id);
270         $this->assertCount(0, $contextlist);
271         $contextlist = provider::get_contexts_for_userid($user2->id);
272         $this->assertCount(0, $contextlist);
274         // Create course.
275         $course1 = $this->getDataGenerator()->create_course();
276         $coursecontext1 = context_course::instance($course1->id);
278         // Enrol users to courses.
279         $this->getDataGenerator()->enrol_user($user1->id, $course1->id);
280         $this->getDataGenerator()->enrol_user($user2->id, $course1->id);
281         $this->getDataGenerator()->enrol_user($user3->id, $course1->id);
283         // Create groups (only one with enablemessaging = 1).
284         $group1a = $this->getDataGenerator()->create_group(array('courseid' => $course1->id, 'enablemessaging' => 1));
286         // Add users to groups.
287         $this->getDataGenerator()->create_group_member(array('groupid' => $group1a->id, 'userid' => $user1->id));
288         $this->getDataGenerator()->create_group_member(array('groupid' => $group1a->id, 'userid' => $user2->id));
289         $this->getDataGenerator()->create_group_member(array('groupid' => $group1a->id, 'userid' => $user3->id));
291         // Get conversation.
292         $component = 'core_group';
293         $itemtype = 'groups';
294         $conversation1 = \core_message\api::get_conversation_by_area(
295             $component,
296             $itemtype,
297             $group1a->id,
298             $coursecontext1->id
299         );
301         // Send some messages to the group conversation.
302         $now = time();
303         $m1id = testhelper::send_fake_message_to_conversation($user1, $conversation1->id, 'Message 1', $now + 1);
304         $m2id = testhelper::send_fake_message_to_conversation($user1, $conversation1->id, 'Message 2', $now + 2);
305         $m3id = testhelper::send_fake_message_to_conversation($user2, $conversation1->id, 'Message 3', $now + 3);
307         // Test for user1 (although is member of the conversation, hasn't any private message).
308         $contextlist = provider::get_contexts_for_userid($user1->id);
309         $this->assertCount(0, $contextlist);
311         // Test for user2 (although is member of the conversation, hasn't any private message).
312         $contextlist = provider::get_contexts_for_userid($user2->id);
313         $this->assertCount(0, $contextlist);
315         // Test for user3 (although is member of the conversation, hasn't any private message).
316         $contextlist = provider::get_contexts_for_userid($user3->id);
317         $this->assertCount(0, $contextlist);
319         // Test for user4 (doesn't belong to the conversation).
320         $contextlist = provider::get_contexts_for_userid($user4->id);
321         $this->assertCount(0, $contextlist);
323         // Send some private messages.
324         $pm1id = $this->create_message($user1->id, $user2->id, time() - (9 * DAYSECS));
326         // Test user1 now has the user context because of the private message.
327         $contextlist = provider::get_contexts_for_userid($user1->id);
328         $this->assertCount(1, $contextlist);
329         $contextforuser = $contextlist->current();
330         $this->assertEquals(
331                 \context_user::instance($user1->id)->id,
332                 $contextforuser->id);
334         // Test user2 now has the user context because of the private message.
335         $contextlist = provider::get_contexts_for_userid($user2->id);
336         $this->assertCount(1, $contextlist);
337         $contextforuser = $contextlist->current();
338         $this->assertEquals(
339                 \context_user::instance($user2->id)->id,
340                 $contextforuser->id);
342         // Test for user3 (although is member of the conversation, hasn't still any private message).
343         $contextlist = provider::get_contexts_for_userid($user3->id);
344         $this->assertCount(0, $contextlist);
346         // Test for user4 (doesn't belong to the conversation and hasn't any private message).
347         $contextlist = provider::get_contexts_for_userid($user4->id);
348         $this->assertCount(0, $contextlist);
349     }
351     /**
352      * Test for provider::get_contexts_for_userid() when there is a notification between users.
353      */
354     public function test_get_contexts_for_userid_with_notification() {
355         $this->resetAfterTest();
357         $user1 = $this->getDataGenerator()->create_user();
358         $user2 = $this->getDataGenerator()->create_user();
360         // Test nothing is found before notification is created.
361         $contextlist = provider::get_contexts_for_userid($user1->id);
362         $this->assertCount(0, $contextlist);
363         $contextlist = provider::get_contexts_for_userid($user2->id);
364         $this->assertCount(0, $contextlist);
366         $this->create_notification($user1->id, $user2->id, time() - (9 * DAYSECS));
368         // Test for the sender.
369         $contextlist = provider::get_contexts_for_userid($user1->id);
370         $this->assertCount(1, $contextlist);
371         $contextforuser = $contextlist->current();
372         $this->assertEquals(
373                 context_user::instance($user1->id)->id,
374                 $contextforuser->id);
376         // Test for the receiver.
377         $contextlist = provider::get_contexts_for_userid($user2->id);
378         $this->assertCount(1, $contextlist);
379         $contextforuser = $contextlist->current();
380         $this->assertEquals(
381                 context_user::instance($user2->id)->id,
382                 $contextforuser->id);
383     }
385     /**
386      * Test for provider::get_contexts_for_userid() when a users has a contact.
387      */
388     public function test_get_contexts_for_userid_with_contact() {
389         $this->resetAfterTest();
391         $user1 = $this->getDataGenerator()->create_user();
392         $user2 = $this->getDataGenerator()->create_user();
394         // Test nothing is found before contact is created.
395         $contextlist = provider::get_contexts_for_userid($user1->id);
396         $this->assertCount(0, $contextlist);
397         $contextlist = provider::get_contexts_for_userid($user2->id);
398         $this->assertCount(0, $contextlist);
400         \core_message\api::add_contact($user1->id, $user2->id);
402         // Test for the user adding the contact.
403         $contextlist = provider::get_contexts_for_userid($user1->id);
404         $this->assertCount(1, $contextlist);
405         $contextforuser = $contextlist->current();
406         $this->assertEquals(
407                 context_user::instance($user1->id)->id,
408                 $contextforuser->id);
410         // Test for the user who is the contact.
411         $contextlist = provider::get_contexts_for_userid($user2->id);
412         $this->assertCount(1, $contextlist);
413         $contextforuser = $contextlist->current();
414         $this->assertEquals(
415                 context_user::instance($user2->id)->id,
416                 $contextforuser->id);
417     }
419     /**
420      * Test for provider::get_contexts_for_userid() when a user makes a contact request.
421      */
422     public function test_get_contexts_for_userid_with_contact_request() {
423         $this->resetAfterTest();
425         $user1 = $this->getDataGenerator()->create_user();
426         $user2 = $this->getDataGenerator()->create_user();
428         // Test nothing is found before request is created.
429         $contextlist = provider::get_contexts_for_userid($user1->id);
430         $this->assertCount(0, $contextlist);
431         $contextlist = provider::get_contexts_for_userid($user2->id);
432         $this->assertCount(0, $contextlist);
434         \core_message\api::create_contact_request($user1->id, $user2->id);
436         // Test for the user requesting the contact.
437         $contextlist = provider::get_contexts_for_userid($user1->id);
438         $this->assertCount(1, $contextlist);
439         $contextforuser = $contextlist->current();
440         $this->assertEquals(
441                 context_user::instance($user1->id)->id,
442                 $contextforuser->id);
444         // Test for the user receiving the contact request.
445         $contextlist = provider::get_contexts_for_userid($user2->id);
446         $this->assertCount(1, $contextlist);
447         $contextforuser = $contextlist->current();
448         $this->assertEquals(
449                 context_user::instance($user2->id)->id,
450                 $contextforuser->id);
451     }
453     /**
454      * Test for provider::get_contexts_for_userid() when a user is blocked.
455      */
456     public function test_get_contexts_for_userid_with_blocked_contact() {
457         $this->resetAfterTest();
459         $user1 = $this->getDataGenerator()->create_user();
460         $user2 = $this->getDataGenerator()->create_user();
462         // Test nothing is found before user is blocked.
463         $contextlist = provider::get_contexts_for_userid($user1->id);
464         $this->assertCount(0, $contextlist);
465         $contextlist = provider::get_contexts_for_userid($user2->id);
466         $this->assertCount(0, $contextlist);
468         \core_message\api::block_user($user1->id, $user2->id);
470         // Test for the blocking user.
471         $contextlist = provider::get_contexts_for_userid($user1->id);
472         $this->assertCount(1, $contextlist);
473         $contextforuser = $contextlist->current();
474         $this->assertEquals(
475                 context_user::instance($user1->id)->id,
476                 $contextforuser->id);
478         // Test for the user who is blocked.
479         $contextlist = provider::get_contexts_for_userid($user2->id);
480         $this->assertCount(1, $contextlist);
481         $contextforuser = $contextlist->current();
482         $this->assertEquals(
483                 context_user::instance($user2->id)->id,
484                 $contextforuser->id);
485     }
487     /**
488      * Test for provider::export_user_data().
489      */
490     public function test_export_for_context_with_contacts() {
491         $this->resetAfterTest();
493         // Create users to test with.
494         $user1 = $this->getDataGenerator()->create_user();
495         $user2 = $this->getDataGenerator()->create_user();
496         $user3 = $this->getDataGenerator()->create_user();
497         $user4 = $this->getDataGenerator()->create_user();
499         \core_message\api::add_contact($user1->id, $user2->id);
500         \core_message\api::add_contact($user1->id, $user3->id);
501         \core_message\api::add_contact($user1->id, $user4->id);
503         $user1context = context_user::instance($user1->id);
505         $this->export_context_data_for_user($user1->id, $user1context, 'core_message');
507         $writer = writer::with_context($user1context);
509         $contacts = (array) $writer->get_data([get_string('contacts', 'core_message')]);
510         usort($contacts, ['static', 'sort_contacts']);
512         $this->assertCount(3, $contacts);
514         $contact1 = array_shift($contacts);
515         $this->assertEquals($user2->id, $contact1->contact);
517         $contact2 = array_shift($contacts);
518         $this->assertEquals($user3->id, $contact2->contact);
520         $contact3 = array_shift($contacts);
521         $this->assertEquals($user4->id, $contact3->contact);
522     }
524     /**
525      * Test for provider::export_user_data().
526      */
527     public function test_export_for_context_with_contact_requests() {
528         $this->resetAfterTest();
530         // Create users to test with.
531         $user1 = $this->getDataGenerator()->create_user();
532         $user2 = $this->getDataGenerator()->create_user();
533         $user3 = $this->getDataGenerator()->create_user();
534         $user4 = $this->getDataGenerator()->create_user();
536         \core_message\api::create_contact_request($user1->id, $user2->id);
537         \core_message\api::create_contact_request($user3->id, $user1->id);
538         \core_message\api::create_contact_request($user1->id, $user4->id);
540         $user1context = context_user::instance($user1->id);
542         $this->export_context_data_for_user($user1->id, $user1context, 'core_message');
544         $writer = writer::with_context($user1context);
546         $contactrequests = (array) $writer->get_data([get_string('contactrequests', 'core_message')]);
548         $this->assertCount(3, $contactrequests);
550         $contactrequest1 = array_shift($contactrequests);
551         $this->assertEquals($user2->id, $contactrequest1->contactrequest);
552         $this->assertEquals(get_string('yes'), $contactrequest1->maderequest);
554         $contactrequest2 = array_shift($contactrequests);
555         $this->assertEquals($user3->id, $contactrequest2->contactrequest);
556         $this->assertEquals(get_string('no'), $contactrequest2->maderequest);
558         $contactrequest3 = array_shift($contactrequests);
559         $this->assertEquals($user4->id, $contactrequest3->contactrequest);
560         $this->assertEquals(get_string('yes'), $contactrequest3->maderequest);
561     }
563     /**
564      * Test for provider::export_user_data().
565      */
566     public function test_export_for_context_with_blocked_users() {
567         $this->resetAfterTest();
569         // Create users to test with.
570         $user1 = $this->getDataGenerator()->create_user();
571         $user2 = $this->getDataGenerator()->create_user();
572         $user3 = $this->getDataGenerator()->create_user();
573         $user4 = $this->getDataGenerator()->create_user();
575         \core_message\api::block_user($user1->id, $user2->id);
576         \core_message\api::block_user($user1->id, $user3->id);
577         \core_message\api::block_user($user1->id, $user4->id);
579         $user1context = context_user::instance($user1->id);
581         $this->export_context_data_for_user($user1->id, $user1context, 'core_message');
583         $writer = writer::with_context($user1context);
585         $blockedusers = (array) $writer->get_data([get_string('blockedusers', 'core_message')]);
587         $this->assertCount(3, $blockedusers);
589         $blockeduser1 = array_shift($blockedusers);
590         $this->assertEquals($user2->id, $blockeduser1->blockeduser);
592         $blockeduser2 = array_shift($blockedusers);
593         $this->assertEquals($user3->id, $blockeduser2->blockeduser);
595         $blockeduser3 = array_shift($blockedusers);
596         $this->assertEquals($user4->id, $blockeduser3->blockeduser);
597     }
599     /**
600      * Test for provider::export_user_data().
601      */
602     public function test_export_for_context_with_private_messages() {
603         global $DB;
605         $this->resetAfterTest();
607         // Create users to test with.
608         $user1 = $this->getDataGenerator()->create_user();
609         $user2 = $this->getDataGenerator()->create_user();
610         $user3 = $this->getDataGenerator()->create_user();
612         $now = time();
614         // Send messages from user 1 to user 2.
615         $m1 = $this->create_message($user1->id, $user2->id, $now - (9 * DAYSECS), true);
616         $m2 = $this->create_message($user2->id, $user1->id, $now - (8 * DAYSECS));
617         $m3 = $this->create_message($user1->id, $user2->id, $now - (7 * DAYSECS));
619         // Send messages from user 3 to user 1.
620         $m4 = $this->create_message($user3->id, $user1->id, $now - (6 * DAYSECS), true);
621         $m5 = $this->create_message($user1->id, $user3->id, $now - (5 * DAYSECS));
622         $m6 = $this->create_message($user3->id, $user1->id, $now - (4 * DAYSECS));
624         // Send messages from user 3 to user 2 - these should not be included in the export.
625         $m7 = $this->create_message($user3->id, $user2->id, $now - (3 * DAYSECS), true);
626         $m8 = $this->create_message($user2->id, $user3->id, $now - (2 * DAYSECS));
627         $m9 = $this->create_message($user3->id, $user2->id, $now - (1 * DAYSECS));
629         // Mark message 2 and 5 as deleted.
630         \core_message\api::delete_message($user1->id, $m2);
631         \core_message\api::delete_message($user1->id, $m5);
633         $user1context = context_user::instance($user1->id);
635         $this->export_context_data_for_user($user1->id, $user1context, 'core_message');
637         $writer = writer::with_context($user1context);
639         $this->assertTrue($writer->has_any_data());
641         // Confirm the messages with user 2 are correct.
642         $messages = (array) $writer->get_data([get_string('messages', 'core_message'), $user2->id]);
643         $this->assertCount(3, $messages);
645         $dbm1 = $DB->get_record('messages', ['id' => $m1]);
646         $dbm2 = $DB->get_record('messages', ['id' => $m2]);
647         $dbm3 = $DB->get_record('messages', ['id' => $m3]);
649         usort($messages, ['static', 'sort_messages']);
650         $m1 = array_shift($messages);
651         $m2 = array_shift($messages);
652         $m3 = array_shift($messages);
654         $this->assertEquals(get_string('yes'), $m1->issender);
655         $this->assertEquals(message_format_message_text($dbm1), $m1->message);
656         $this->assertEquals(transform::datetime($now - (9 * DAYSECS)), $m1->timecreated);
657         $this->assertEquals('-', $m1->timeread);
658         $this->assertArrayNotHasKey('timedeleted', (array) $m1);
660         $this->assertEquals(get_string('no'), $m2->issender);
661         $this->assertEquals(message_format_message_text($dbm2), $m2->message);
662         $this->assertEquals(transform::datetime($now - (8 * DAYSECS)), $m2->timecreated);
663         $this->assertEquals('-', $m2->timeread);
664         $this->assertArrayHasKey('timedeleted', (array) $m2);
666         $this->assertEquals(get_string('yes'), $m3->issender);
667         $this->assertEquals(message_format_message_text($dbm3), $m3->message);
668         $this->assertEquals(transform::datetime($now - (7 * DAYSECS)), $m3->timecreated);
669         $this->assertEquals('-', $m3->timeread);
671         // Confirm the messages with user 3 are correct.
672         $messages = (array) $writer->get_data([get_string('messages', 'core_message'), $user3->id]);
673         $this->assertCount(3, $messages);
675         $dbm4 = $DB->get_record('messages', ['id' => $m4]);
676         $dbm5 = $DB->get_record('messages', ['id' => $m5]);
677         $dbm6 = $DB->get_record('messages', ['id' => $m6]);
679         usort($messages, ['static', 'sort_messages']);
680         $m4 = array_shift($messages);
681         $m5 = array_shift($messages);
682         $m6 = array_shift($messages);
684         $this->assertEquals(get_string('no'), $m4->issender);
685         $this->assertEquals(message_format_message_text($dbm4), $m4->message);
686         $this->assertEquals(transform::datetime($now - (6 * DAYSECS)), $m4->timecreated);
687         $this->assertNotEquals('-', $m4->timeread);
688         $this->assertArrayNotHasKey('timedeleted', (array) $m4);
690         $this->assertEquals(get_string('yes'), $m5->issender);
691         $this->assertEquals(message_format_message_text($dbm5), $m5->message);
692         $this->assertEquals(transform::datetime($now - (5 * DAYSECS)), $m5->timecreated);
693         $this->assertEquals('-', $m5->timeread);
694         $this->assertArrayHasKey('timedeleted', (array) $m5);
696         $this->assertEquals(get_string('no'), $m6->issender);
697         $this->assertEquals(message_format_message_text($dbm6), $m6->message);
698         $this->assertEquals(transform::datetime($now - (4 * DAYSECS)), $m6->timecreated);
699         $this->assertEquals('-', $m6->timeread);
700     }
702     /**
703      * Test for provider::export_user_data().
704      */
705     public function test_export_for_context_with_messages() {
706         global $DB;
708         $this->resetAfterTest();
709         $this->setAdminUser();
710         $now = time();
711         $systemcontext = \context_system::instance();
713         // Create users to test with.
714         $user1 = $this->getDataGenerator()->create_user();
715         $user2 = $this->getDataGenerator()->create_user();
716         $user3 = $this->getDataGenerator()->create_user();
717         $user1context = \context_user::instance($user1->id);
719         $course1 = $this->getDataGenerator()->create_course();
720         $course2 = $this->getDataGenerator()->create_course();
721         $coursecontext1 = \context_course::instance($course1->id);
722         $coursecontext2 = \context_course::instance($course2->id);
724         // Enrol users to courses.
725         $this->getDataGenerator()->enrol_user($user1->id, $course1->id);
726         $this->getDataGenerator()->enrol_user($user2->id, $course1->id);
727         $this->getDataGenerator()->enrol_user($user3->id, $course1->id);
728         $this->getDataGenerator()->enrol_user($user1->id, $course2->id);
730         // Create course groups with group messaging enabled.
731         $group1a = $this->getDataGenerator()->create_group(array('courseid' => $course1->id, 'enablemessaging' => 1));
732         $group2a = $this->getDataGenerator()->create_group(array('courseid' => $course2->id, 'enablemessaging' => 1));
734         // Add users to groups.
735         $this->getDataGenerator()->create_group_member(array('groupid' => $group1a->id, 'userid' => $user1->id));
736         $this->getDataGenerator()->create_group_member(array('groupid' => $group1a->id, 'userid' => $user2->id));
737         $this->getDataGenerator()->create_group_member(array('groupid' => $group1a->id, 'userid' => $user3->id));
738         $this->getDataGenerator()->create_group_member(array('groupid' => $group2a->id, 'userid' => $user1->id));
740         // Get conversation.
741         $component = 'core_group';
742         $itemtype = 'groups';
743         $conversation = \core_message\api::get_conversation_by_area(
744             $component,
745             $itemtype,
746             $group1a->id,
747             $coursecontext1->id
748         );
750         // Send some private messages between user 1 and user 2.
751         $pm1id = $this->create_message($user1->id, $user2->id, $now);
753         $dbpm1 = $DB->get_record('messages', ['id' => $pm1id]);
755         // Send some messages to the conversation.
756         $m1 = testhelper::send_fake_message_to_conversation($user1, $conversation->id, 'Message 1', $now + 1);
757         $m2 = testhelper::send_fake_message_to_conversation($user1, $conversation->id, 'Message 2', $now + 2);
758         $m3 = testhelper::send_fake_message_to_conversation($user2, $conversation->id, 'Message 3', $now + 3);
760         $dbm1 = $DB->get_record('messages', ['id' => $m1]);
761         $dbm2 = $DB->get_record('messages', ['id' => $m2]);
762         $dbm3 = $DB->get_record('messages', ['id' => $m3]);
764         // Mark as read and delete some messages.
765         \core_message\api::mark_message_as_read($user2->id, $dbm1);
766         \core_message\api::delete_message($user1->id, $m2);
768         // Confirm the user1 has no data in any course context because private messages are related to user context.
769         $this->export_context_data_for_user($user1->id, $coursecontext2, 'core_message');
771         // Check that system context hasn't been exported.
772         $writer = writer::with_context($systemcontext);
773         $this->assertFalse($writer->has_any_data());
775         // Check that course1 context hasn't been exported.
776         $writer = writer::with_context($coursecontext1);
777         $this->assertFalse($writer->has_any_data());
779         // Check that course2 context has been exported and contains data.
780         $writer = writer::with_context($coursecontext2);
781         $this->assertFalse($writer->has_any_data());
783         // Confirm the user1 has only private messages in the user context.
784         $this->export_context_data_for_user($user1->id, $user1context, 'core_message');
785         $writer = writer::with_context($user1context);
786         $this->assertTrue($writer->has_any_data());
788         // Confirm the messages with user 2 are correct.
789         $messages = (array) $writer->get_data([get_string('messages', 'core_message'), $user2->id]);
790         $this->assertCount(1, $messages);
791         $m1 = reset($messages);
793         $this->assertEquals(get_string('yes'), $m1->issender);
794         $this->assertEquals(message_format_message_text($dbpm1), $m1->message);
795         $this->assertEquals(transform::datetime($now), $m1->timecreated);
796         $this->assertEquals('-', $m1->timeread);
797         $this->assertArrayNotHasKey('timedeleted', (array) $m1);
799         // Confirm the messages with user 3 are correct.
800         $messages = (array) $writer->get_data([get_string('messages', 'core_message'), fullname($user3)]);
801         $this->assertCount(0, $messages);
802     }
804     /**
805      * Test for provider::export_user_data().
806      */
807     public function test_export_for_context_with_notifications() {
808         $this->resetAfterTest();
810         // Create users to test with.
811         $user1 = $this->getDataGenerator()->create_user();
812         $user2 = $this->getDataGenerator()->create_user();
813         $user3 = $this->getDataGenerator()->create_user();
815         $now = time();
816         $timeread = $now - DAYSECS;
818         // Send notifications from user 1 to user 2.
819         $this->create_notification($user1->id, $user2->id, $now + (9 * DAYSECS), $timeread);
820         $this->create_notification($user2->id, $user1->id, $now + (8 * DAYSECS));
821         $this->create_notification($user1->id, $user2->id, $now + (7 * DAYSECS));
823         // Send notifications from user 3 to user 1.
824         $this->create_notification($user3->id, $user1->id, $now + (6 * DAYSECS), $timeread);
825         $this->create_notification($user1->id, $user3->id, $now + (5 * DAYSECS));
826         $this->create_notification($user3->id, $user1->id, $now + (4 * DAYSECS));
828         // Send notifications from user 3 to user 2 - should not be part of the export.
829         $this->create_notification($user3->id, $user2->id, $now + (3 * DAYSECS), $timeread);
830         $this->create_notification($user2->id, $user3->id, $now + (2 * DAYSECS));
831         $this->create_notification($user3->id, $user2->id, $now + (1 * DAYSECS));
833         $user1context = context_user::instance($user1->id);
835         $this->export_context_data_for_user($user1->id, $user1context, 'core_message');
837         $writer = writer::with_context($user1context);
839         $this->assertTrue($writer->has_any_data());
841         // Confirm the notifications.
842         $notifications = (array) $writer->get_data([get_string('notifications', 'core_message')]);
844         $this->assertCount(6, $notifications);
845     }
847     /**
848      * Test for provider::delete_data_for_all_users_in_context().
849      */
850     public function test_delete_data_for_all_users_in_context() {
851         global $DB;
853         $this->resetAfterTest();
855         // Create users to test with.
856         $user1 = $this->getDataGenerator()->create_user();
857         $user2 = $this->getDataGenerator()->create_user();
858         $user3 = $this->getDataGenerator()->create_user();
859         $user4 = $this->getDataGenerator()->create_user();
860         $user5 = $this->getDataGenerator()->create_user();
862         $now = time();
863         $timeread = $now - DAYSECS;
865         $user1context = context_user::instance($user1->id);
867         // Create contacts.
868         \core_message\api::add_contact($user1->id, $user2->id);
869         \core_message\api::add_contact($user2->id, $user3->id);
871         // Create contact requests.
872         \core_message\api::create_contact_request($user1->id, $user3->id);
873         \core_message\api::create_contact_request($user2->id, $user4->id);
875         // Block a user.
876         \core_message\api::block_user($user1->id, $user3->id);
877         \core_message\api::block_user($user3->id, $user4->id);
879         // Create messages.
880         $m1 = $this->create_message($user1->id, $user2->id, $now + (9 * DAYSECS), true);
881         $m2 = $this->create_message($user2->id, $user1->id, $now + (8 * DAYSECS));
882         $m3 = $this->create_message($user2->id, $user3->id, $now + (7 * DAYSECS));
884         // Create notifications.
885         $n1 = $this->create_notification($user1->id, $user2->id, $now + (9 * DAYSECS), $timeread);
886         $n2 = $this->create_notification($user2->id, $user1->id, $now + (8 * DAYSECS));
887         $n3 = $this->create_notification($user2->id, $user3->id, $now + (7 * DAYSECS));
889         // Delete one of the messages.
890         \core_message\api::delete_message($user1->id, $m2);
892         // There should be 2 contacts.
893         $this->assertEquals(2, $DB->count_records('message_contacts'));
895         // There should be 2 contact requests.
896         $this->assertEquals(2, $DB->count_records('message_contact_requests'));
898         // There should be 2 blocked users.
899         $this->assertEquals(2, $DB->count_records('message_users_blocked'));
901         // There should be 3 messages.
902         $this->assertEquals(3, $DB->count_records('messages'));
904         // There should be 2 user actions - one for reading the message, one for deleting.
905         $this->assertEquals(2, $DB->count_records('message_user_actions'));
907         // There should be 4 conversation members.
908         $this->assertEquals(4, $DB->count_records('message_conversation_members'));
910         // There should be 5 notifications (3 from create_notification and 2 from create_contact_request).
911         $this->assertEquals(5, $DB->count_records('notifications'));
913         provider::delete_data_for_all_users_in_context($user1context);
915         // Confirm there is only 1 contact left.
916         $this->assertEquals(1, $DB->count_records('message_contacts'));
917         // And it is not related to user1.
918         $this->assertEquals(0,
919                 $DB->count_records_select('message_contacts', 'userid = ? OR contactid = ?', [$user1->id, $user1->id]));
921         // Confirm there is only 1 contact request left.
922         $this->assertEquals(1, $DB->count_records('message_contact_requests'));
923         // And it is not related to user1.
924         $this->assertEquals(0,
925                 $DB->count_records_select('message_contact_requests', 'userid = ? OR requesteduserid = ?',
926                         [$user1->id, $user1->id]));
928         // Confirm there is only 1 blocked user left.
929         $this->assertEquals(1, $DB->count_records('message_users_blocked'));
930         // And it is not related to user1.
931         $this->assertEquals(0,
932                 $DB->count_records_select('message_users_blocked', 'userid = ? OR blockeduserid = ?', [$user1->id, $user1->id]));
934         // Confirm there are only 2 messages left.
935         $this->assertEquals(2, $DB->count_records('messages'));
936         // And none of them are from user1.
937         $this->assertEquals(0, $DB->count_records('messages', ['useridfrom' => $user1->id]));
939         // Confirm there is 0 user action left.
940         $this->assertEquals(0, $DB->count_records('message_user_actions'));
941         // And it is not for user1.
942         $this->assertEquals(0, $DB->count_records('message_user_actions', ['userid' => $user1->id]));
944         // Confirm there are only 3 conversation members left.
945         $this->assertEquals(3, $DB->count_records('message_conversation_members'));
946         // And user1 is not in any conversation.
947         $this->assertEquals(0, $DB->count_records('message_conversation_members', ['userid' => $user1->id]));
949         // Confirm there are only 2 notifications.
950         $this->assertEquals(2, $DB->count_records('notifications'));
951         // And it is not related to user1.
952         $this->assertEquals(0,
953                 $DB->count_records_select('notifications', 'useridfrom = ? OR useridto = ? ', [$user1->id, $user1->id]));
954     }
956     /**
957      * Test for provider::delete_data_for_user().
958      */
959     public function test_delete_data_for_user() {
960         global $DB;
962         $this->resetAfterTest();
964         // Create users to test with.
965         $user1 = $this->getDataGenerator()->create_user();
966         $user2 = $this->getDataGenerator()->create_user();
967         $user3 = $this->getDataGenerator()->create_user();
968         $user4 = $this->getDataGenerator()->create_user();
969         $user5 = $this->getDataGenerator()->create_user();
970         $user6 = $this->getDataGenerator()->create_user();
972         $now = time();
973         $timeread = $now - DAYSECS;
975         // Create contacts.
976         \core_message\api::add_contact($user1->id, $user2->id);
977         \core_message\api::add_contact($user2->id, $user3->id);
979         // Create contact requests.
980         \core_message\api::create_contact_request($user1->id, $user3->id);
981         \core_message\api::create_contact_request($user2->id, $user4->id);
983         // Block users.
984         \core_message\api::block_user($user1->id, $user5->id);
985         \core_message\api::block_user($user2->id, $user6->id);
987         // Create messages.
988         $m1 = $this->create_message($user1->id, $user2->id, $now + (9 * DAYSECS), $timeread);
989         $m2 = $this->create_message($user2->id, $user1->id, $now + (8 * DAYSECS));
991         // Create notifications.
992         $n1 = $this->create_notification($user1->id, $user2->id, $now + (9 * DAYSECS), $timeread);
993         $n2 = $this->create_notification($user2->id, $user1->id, $now + (8 * DAYSECS));
994         $n2 = $this->create_notification($user2->id, $user3->id, $now + (8 * DAYSECS));
996         // Delete one of the messages.
997         \core_message\api::delete_message($user1->id, $m2);
999         // There should be 2 contacts.
1000         $this->assertEquals(2, $DB->count_records('message_contacts'));
1002         // There should be 1 contact request.
1003         $this->assertEquals(2, $DB->count_records('message_contact_requests'));
1005         // There should be 1 blocked user.
1006         $this->assertEquals(2, $DB->count_records('message_users_blocked'));
1008         // There should be two messages.
1009         $this->assertEquals(2, $DB->count_records('messages'));
1011         // There should be two user actions - one for reading the message, one for deleting.
1012         $this->assertEquals(2, $DB->count_records('message_user_actions'));
1014         // There should be two conversation members.
1015         $this->assertEquals(2, $DB->count_records('message_conversation_members'));
1017         // There should be 5 notifications (3 from create_notification and 2 from create_contact_request).
1018         $this->assertEquals(5, $DB->count_records('notifications'));
1020         $user1context = context_user::instance($user1->id);
1021         $contextlist = new \core_privacy\local\request\approved_contextlist($user1, 'core_message',
1022             [$user1context->id]);
1023         provider::delete_data_for_user($contextlist);
1025         // Confirm the user 2 data still exists.
1026         $contacts = $DB->get_records('message_contacts');
1027         $contactrequests = $DB->get_records('message_contact_requests');
1028         $blockedusers = $DB->get_records('message_users_blocked');
1029         $messages = $DB->get_records('messages');
1030         $muas = $DB->get_records('message_user_actions');
1031         $mcms = $DB->get_records('message_conversation_members');
1032         $notifications = $DB->get_records('notifications');
1034         $this->assertCount(1, $contacts);
1035         $contact = reset($contacts);
1036         $this->assertEquals($user2->id, $contact->userid);
1037         $this->assertEquals($user3->id, $contact->contactid);
1039         $this->assertCount(1, $contactrequests);
1040         $contactrequest = reset($contactrequests);
1041         $this->assertEquals($user2->id, $contactrequest->userid);
1042         $this->assertEquals($user4->id, $contactrequest->requesteduserid);
1044         $this->assertCount(1, $blockedusers);
1045         $blockeduser = reset($blockedusers);
1046         $this->assertEquals($user2->id, $blockeduser->userid);
1047         $this->assertEquals($user6->id, $blockeduser->blockeduserid);
1049         $this->assertCount(1, $messages);
1050         $message = reset($messages);
1051         $this->assertEquals($m2, $message->id);
1053         $this->assertCount(0, $muas);
1055         $this->assertCount(1, $mcms);
1056         $mcm = reset($mcms);
1057         $this->assertEquals($user2->id, $mcm->userid);
1059         $this->assertCount(2, $notifications);
1060         ksort($notifications);
1062         $notification = array_pop($notifications);
1063         $this->assertEquals($user2->id, $notification->useridfrom);
1064         $this->assertEquals($user3->id, $notification->useridto);
1065     }
1067     /**
1068      * Test for provider::get_users_in_context() when there is no message or notification.
1069      */
1070     public function test_get_users_in_context_no_data() {
1071         $this->resetAfterTest();
1073         $user = $this->getDataGenerator()->create_user();
1074         $usercontext = context_user::instance($user->id);
1076         $userlist = new \core_privacy\local\request\userlist($usercontext, 'core_message');
1077         \core_message\privacy\provider::get_users_in_context($userlist);
1079         $this->assertEmpty($userlist->get_userids());
1080     }
1082     /**
1083      * Test for provider::get_users_in_context() when there is a message between users.
1084      */
1085     public function test_get_users_in_context_with_message() {
1086         $this->resetAfterTest();
1088         $user1 = $this->getDataGenerator()->create_user();
1089         $user2 = $this->getDataGenerator()->create_user();
1091         $user1context = context_user::instance($user1->id);
1092         $user2context = context_user::instance($user2->id);
1094         // Test nothing is found before message is sent.
1095         $userlist = new \core_privacy\local\request\userlist($user1context, 'core_message');
1096         \core_message\privacy\provider::get_users_in_context($userlist);
1097         $this->assertCount(0, $userlist);
1098         $userlist = new \core_privacy\local\request\userlist($user2context, 'core_message');
1099         \core_message\privacy\provider::get_users_in_context($userlist);
1100         $this->assertCount(0, $userlist);
1102         $this->create_message($user1->id, $user2->id, time() - (9 * DAYSECS));
1104         // Test for the sender.
1105         $userlist = new \core_privacy\local\request\userlist($user1context, 'core_message');
1106         \core_message\privacy\provider::get_users_in_context($userlist);
1107         $this->assertCount(1, $userlist);
1108         $userincontext = $userlist->current();
1109         $this->assertEquals($user1->id, $userincontext->id);
1111         // Test for the receiver.
1112         $userlist = new \core_privacy\local\request\userlist($user2context, 'core_message');
1113         \core_message\privacy\provider::get_users_in_context($userlist);
1114         $this->assertCount(1, $userlist);
1115         $userincontext = $userlist->current();
1116         $this->assertEquals($user2->id, $userincontext->id);
1117     }
1119     /**
1120      * Test for provider::get_users_in_context() when there is a notification between users.
1121      */
1122     public function test_get_users_in_context_with_notification() {
1123         $this->resetAfterTest();
1125         $user1 = $this->getDataGenerator()->create_user();
1126         $user2 = $this->getDataGenerator()->create_user();
1128         $user1context = context_user::instance($user1->id);
1129         $user2context = context_user::instance($user2->id);
1131         // Test nothing is found before notification is created.
1132         $userlist = new \core_privacy\local\request\userlist($user1context, 'core_message');
1133         \core_message\privacy\provider::get_users_in_context($userlist);
1134         $this->assertCount(0, $userlist);
1135         $userlist = new \core_privacy\local\request\userlist($user2context, 'core_message');
1136         \core_message\privacy\provider::get_users_in_context($userlist);
1137         $this->assertCount(0, $userlist);
1139         $this->create_notification($user1->id, $user2->id, time() - (9 * DAYSECS));
1141         // Test for the sender.
1142         $userlist = new \core_privacy\local\request\userlist($user1context, 'core_message');
1143         \core_message\privacy\provider::get_users_in_context($userlist);
1144         $this->assertCount(1, $userlist);
1145         $userincontext = $userlist->current();
1146         $this->assertEquals($user1->id, $userincontext->id);
1148         // Test for the receiver.
1149         $userlist = new \core_privacy\local\request\userlist($user2context, 'core_message');
1150         \core_message\privacy\provider::get_users_in_context($userlist);
1151         $this->assertCount(1, $userlist);
1152         $userincontext = $userlist->current();
1153         $this->assertEquals($user2->id, $userincontext->id);
1154     }
1156     /**
1157      * Test for provider::get_users_in_context() when a users has a contact.
1158      */
1159     public function test_get_users_in_context_with_contact() {
1160         $this->resetAfterTest();
1162         $user1 = $this->getDataGenerator()->create_user();
1163         $user2 = $this->getDataGenerator()->create_user();
1165         $user1context = context_user::instance($user1->id);
1166         $user2context = context_user::instance($user2->id);
1168         // Test nothing is found before contact is created.
1169         $userlist = new \core_privacy\local\request\userlist($user1context, 'core_message');
1170         \core_message\privacy\provider::get_users_in_context($userlist);
1171         $this->assertCount(0, $userlist);
1172         $userlist = new \core_privacy\local\request\userlist($user2context, 'core_message');
1173         \core_message\privacy\provider::get_users_in_context($userlist);
1174         $this->assertCount(0, $userlist);
1176         \core_message\api::add_contact($user1->id, $user2->id);
1178         // Test for the user adding the contact.
1179         $userlist = new \core_privacy\local\request\userlist($user1context, 'core_message');
1180         \core_message\privacy\provider::get_users_in_context($userlist);
1181         $this->assertCount(1, $userlist);
1182         $userincontext = $userlist->current();
1183         $this->assertEquals($user1->id, $userincontext->id);
1185         // Test for the user who is the contact.
1186         $userlist = new \core_privacy\local\request\userlist($user2context, 'core_message');
1187         \core_message\privacy\provider::get_users_in_context($userlist);
1188         $this->assertCount(1, $userlist);
1189         $userincontext = $userlist->current();
1190         $this->assertEquals($user2->id, $userincontext->id);
1191     }
1193     /**
1194      * Test for provider::get_users_in_context() when a user makes a contact request.
1195      */
1196     public function test_get_users_in_context_with_contact_request() {
1197         $this->resetAfterTest();
1199         $user1 = $this->getDataGenerator()->create_user();
1200         $user2 = $this->getDataGenerator()->create_user();
1202         $user1context = context_user::instance($user1->id);
1203         $user2context = context_user::instance($user2->id);
1205         // Test nothing is found before request is created.
1206         $userlist = new \core_privacy\local\request\userlist($user1context, 'core_message');
1207         \core_message\privacy\provider::get_users_in_context($userlist);
1208         $this->assertCount(0, $userlist);
1209         $userlist = new \core_privacy\local\request\userlist($user2context, 'core_message');
1210         \core_message\privacy\provider::get_users_in_context($userlist);
1211         $this->assertCount(0, $userlist);
1213         \core_message\api::create_contact_request($user1->id, $user2->id);
1215         // Test for the user requesting the contact.
1216         $userlist = new \core_privacy\local\request\userlist($user1context, 'core_message');
1217         \core_message\privacy\provider::get_users_in_context($userlist);
1218         $this->assertCount(1, $userlist);
1219         $userincontext = $userlist->current();
1220         $this->assertEquals($user1->id, $userincontext->id);
1222         // Test for the user receiving the contact request.
1223         $userlist = new \core_privacy\local\request\userlist($user2context, 'core_message');
1224         \core_message\privacy\provider::get_users_in_context($userlist);
1225         $this->assertCount(1, $userlist);
1226         $userincontext = $userlist->current();
1227         $this->assertEquals($user2->id, $userincontext->id);
1228     }
1230     /**
1231      * Test for provider::get_users_in_context() when a user is blocked.
1232      */
1233     public function test_get_users_in_context_with_blocked_contact() {
1234         $this->resetAfterTest();
1236         $user1 = $this->getDataGenerator()->create_user();
1237         $user2 = $this->getDataGenerator()->create_user();
1239         $user1context = context_user::instance($user1->id);
1240         $user2context = context_user::instance($user2->id);
1242         // Test nothing is found before user is blocked.
1243         $userlist = new \core_privacy\local\request\userlist($user1context, 'core_message');
1244         \core_message\privacy\provider::get_users_in_context($userlist);
1245         $this->assertCount(0, $userlist);
1246         $userlist = new \core_privacy\local\request\userlist($user2context, 'core_message');
1247         \core_message\privacy\provider::get_users_in_context($userlist);
1248         $this->assertCount(0, $userlist);
1250         \core_message\api::block_user($user1->id, $user2->id);
1252         // Test for the blocking user.
1253         $userlist = new \core_privacy\local\request\userlist($user1context, 'core_message');
1254         \core_message\privacy\provider::get_users_in_context($userlist);
1255         $this->assertCount(1, $userlist);
1256         $userincontext = $userlist->current();
1257         $this->assertEquals($user1->id, $userincontext->id);
1259         // Test for the user who is blocked.
1260         $userlist = new \core_privacy\local\request\userlist($user2context, 'core_message');
1261         \core_message\privacy\provider::get_users_in_context($userlist);
1262         $this->assertCount(1, $userlist);
1263         $userincontext = $userlist->current();
1264         $this->assertEquals($user2->id, $userincontext->id);
1265     }
1267     /**
1268      * Test for provider::delete_data_for_users().
1269      */
1270     public function test_delete_data_for_users() {
1271         global $DB;
1273         $this->resetAfterTest();
1275         // Create users to test with.
1276         $user1 = $this->getDataGenerator()->create_user();
1277         $user2 = $this->getDataGenerator()->create_user();
1278         $user3 = $this->getDataGenerator()->create_user();
1279         $user4 = $this->getDataGenerator()->create_user();
1280         $user5 = $this->getDataGenerator()->create_user();
1281         $user6 = $this->getDataGenerator()->create_user();
1283         $now = time();
1284         $timeread = $now - DAYSECS;
1286         // Create contacts.
1287         \core_message\api::add_contact($user1->id, $user2->id);
1288         \core_message\api::add_contact($user2->id, $user3->id);
1290         // Create contact requests.
1291         \core_message\api::create_contact_request($user1->id, $user3->id);
1292         \core_message\api::create_contact_request($user2->id, $user4->id);
1294         // Block users.
1295         \core_message\api::block_user($user1->id, $user5->id);
1296         \core_message\api::block_user($user2->id, $user6->id);
1298         // Create messages.
1299         $m1 = $this->create_message($user1->id, $user2->id, $now + (9 * DAYSECS), $timeread);
1300         $m2 = $this->create_message($user2->id, $user1->id, $now + (8 * DAYSECS));
1302         // Create notifications.
1303         $n1 = $this->create_notification($user1->id, $user2->id, $now + (9 * DAYSECS), $timeread);
1304         $n2 = $this->create_notification($user2->id, $user1->id, $now + (8 * DAYSECS));
1305         $n2 = $this->create_notification($user2->id, $user3->id, $now + (8 * DAYSECS));
1307         // Delete one of the messages.
1308         \core_message\api::delete_message($user1->id, $m2);
1310         // There should be 2 contacts.
1311         $this->assertEquals(2, $DB->count_records('message_contacts'));
1313         // There should be 1 contact request.
1314         $this->assertEquals(2, $DB->count_records('message_contact_requests'));
1316         // There should be 1 blocked user.
1317         $this->assertEquals(2, $DB->count_records('message_users_blocked'));
1319         // There should be two messages.
1320         $this->assertEquals(2, $DB->count_records('messages'));
1322         // There should be two user actions - one for reading the message, one for deleting.
1323         $this->assertEquals(2, $DB->count_records('message_user_actions'));
1325         // There should be two conversation members.
1326         $this->assertEquals(2, $DB->count_records('message_conversation_members'));
1328         // There should be three notifications + two for the contact requests.
1329         $this->assertEquals(5, $DB->count_records('notifications'));
1331         $user1context = context_user::instance($user1->id);
1332         $approveduserlist = new \core_privacy\local\request\approved_userlist($user1context, 'core_message',
1333                 [$user1->id, $user2->id]);
1334         provider::delete_data_for_users($approveduserlist);
1336         // Only user1's data should be deleted. User2 should be skipped as user2 is an invalid user for user1context.
1338         // Confirm the user 2 data still exists.
1339         $contacts = $DB->get_records('message_contacts');
1340         $contactrequests = $DB->get_records('message_contact_requests');
1341         $blockedusers = $DB->get_records('message_users_blocked');
1342         $messages = $DB->get_records('messages');
1343         $muas = $DB->get_records('message_user_actions');
1344         $mcms = $DB->get_records('message_conversation_members');
1345         $notifications = $DB->get_records('notifications');
1347         $this->assertCount(1, $contacts);
1348         $contact = reset($contacts);
1349         $this->assertEquals($user2->id, $contact->userid);
1350         $this->assertEquals($user3->id, $contact->contactid);
1352         $this->assertCount(1, $contactrequests);
1353         $contactrequest = reset($contactrequests);
1354         $this->assertEquals($user2->id, $contactrequest->userid);
1355         $this->assertEquals($user4->id, $contactrequest->requesteduserid);
1357         $this->assertCount(1, $blockedusers);
1358         $blockeduser = reset($blockedusers);
1359         $this->assertEquals($user2->id, $blockeduser->userid);
1360         $this->assertEquals($user6->id, $blockeduser->blockeduserid);
1362         $this->assertCount(1, $messages);
1363         $message = reset($messages);
1364         $this->assertEquals($m2, $message->id);
1366         $this->assertCount(0, $muas);
1368         $this->assertCount(1, $mcms);
1369         $mcm = reset($mcms);
1370         $this->assertEquals($user2->id, $mcm->userid);
1372         $this->assertCount(2, $notifications);
1373         ksort($notifications);
1375         $notification = array_pop($notifications);
1376         $this->assertEquals($user2->id, $notification->useridfrom);
1377         $this->assertEquals($user3->id, $notification->useridto);
1378     }
1380     /**
1381      * Test for provider::add_contexts_for_conversations().
1382      */
1383     public function test_add_contexts_for_conversations() {
1384         $this->resetAfterTest();
1385         $this->setAdminUser();
1386         $component = 'core_group';
1387         $itemtype = 'groups';
1389         $user1 = $this->getDataGenerator()->create_user();
1390         $user2 = $this->getDataGenerator()->create_user();
1391         $user3 = $this->getDataGenerator()->create_user();
1392         $user4 = $this->getDataGenerator()->create_user();
1394         // Test nothing is found before group conversations is created or message is sent.
1395         $contextlist = new contextlist();
1396         provider::add_contexts_for_conversations($contextlist, $user1->id, $component, $itemtype);
1397         $this->assertCount(0, $contextlist);
1398         provider::add_contexts_for_conversations($contextlist, $user2->id, $component, $itemtype);
1399         $this->assertCount(0, $contextlist);
1401         // Create courses.
1402         $course1 = $this->getDataGenerator()->create_course();
1403         $course2 = $this->getDataGenerator()->create_course();
1404         $coursecontext1 = \context_course::instance($course1->id);
1405         $coursecontext2 = \context_course::instance($course2->id);
1407         // Enrol users to courses.
1408         $this->getDataGenerator()->enrol_user($user1->id, $course1->id);
1409         $this->getDataGenerator()->enrol_user($user2->id, $course1->id);
1410         $this->getDataGenerator()->enrol_user($user3->id, $course1->id);
1411         $this->getDataGenerator()->enrol_user($user1->id, $course2->id);
1412         $this->getDataGenerator()->enrol_user($user2->id, $course2->id);
1414         // Create course groups with messaging enabled.
1415         $group1a = $this->getDataGenerator()->create_group(array('courseid' => $course1->id, 'enablemessaging' => 1));
1416         $group2a = $this->getDataGenerator()->create_group(array('courseid' => $course2->id, 'enablemessaging' => 1));
1418         // Add users to groups.
1419         $this->getDataGenerator()->create_group_member(array('groupid' => $group1a->id, 'userid' => $user1->id));
1420         $this->getDataGenerator()->create_group_member(array('groupid' => $group1a->id, 'userid' => $user2->id));
1421         $this->getDataGenerator()->create_group_member(array('groupid' => $group1a->id, 'userid' => $user3->id));
1422         $this->getDataGenerator()->create_group_member(array('groupid' => $group2a->id, 'userid' => $user1->id));
1424         // Get conversation.
1425         $conversation1 = \core_message\api::get_conversation_by_area(
1426             $component,
1427             $itemtype,
1428             $group1a->id,
1429             $coursecontext1->id
1430         );
1432         // Send some messages to the group conversation.
1433         $now = time();
1434         $m1id = testhelper::send_fake_message_to_conversation($user1, $conversation1->id, 'Message 1', $now + 1);
1435         $m2id = testhelper::send_fake_message_to_conversation($user1, $conversation1->id, 'Message 2', $now + 2);
1436         $m3id = testhelper::send_fake_message_to_conversation($user2, $conversation1->id, 'Message 3', $now + 3);
1438         // Test for user1 (is member of the conversation and has sent a message).
1439         $contextlist = new contextlist();
1440         provider::add_contexts_for_conversations($contextlist, $user1->id, $component, $itemtype);
1441         $this->assertCount(2, $contextlist);
1442         $this->assertContains($coursecontext1->id, $contextlist->get_contextids());
1443         $this->assertContains($coursecontext2->id, $contextlist->get_contextids());
1445         // Test for user2 (is member of the conversation and has sent a message).
1446         $contextlist = new contextlist();
1447         provider::add_contexts_for_conversations($contextlist, $user2->id, $component, $itemtype);
1448         $this->assertCount(1, $contextlist);
1449         $this->assertEquals($coursecontext1, $contextlist->current());
1451         // Test for user3 (is member of the conversation).
1452         $contextlist = new contextlist();
1453         provider::add_contexts_for_conversations($contextlist, $user3->id, $component, $itemtype);
1454         $this->assertCount(1, $contextlist);
1455         $this->assertEquals($coursecontext1, $contextlist->current());
1457         // Test for user4 (doesn't belong to the conversation).
1458         $contextlist = new contextlist();
1459         provider::add_contexts_for_conversations($contextlist, $user4->id, $component, $itemtype);
1460         $this->assertCount(0, $contextlist);
1461     }
1463     /**
1464      * Test for provider::add_conversations_in_context().
1465      */
1466     public function test_add_conversations_in_context() {
1467         $this->resetAfterTest();
1468         $this->setAdminUser();
1469         $component = 'core_group';
1470         $itemtype = 'groups';
1472         $user1 = $this->getDataGenerator()->create_user();
1473         $user2 = $this->getDataGenerator()->create_user();
1474         $user3 = $this->getDataGenerator()->create_user();
1475         $user4 = $this->getDataGenerator()->create_user();
1477         // Create courses.
1478         $course1 = $this->getDataGenerator()->create_course();
1479         $course2 = $this->getDataGenerator()->create_course();
1480         $coursecontext1 = \context_course::instance($course1->id);
1481         $coursecontext2 = \context_course::instance($course2->id);
1483         // Test nothing is found before group conversations is created or message is sent.
1484         $userlist1 = new \core_privacy\local\request\userlist($coursecontext1, 'core_message');
1485         provider::add_conversations_in_context($userlist1, $component, $itemtype);
1486         $this->assertCount(0, $userlist1);
1488         // Enrol users to courses.
1489         $this->getDataGenerator()->enrol_user($user1->id, $course1->id);
1490         $this->getDataGenerator()->enrol_user($user2->id, $course1->id);
1491         $this->getDataGenerator()->enrol_user($user3->id, $course1->id);
1492         $this->getDataGenerator()->enrol_user($user4->id, $course1->id);
1493         $this->getDataGenerator()->enrol_user($user1->id, $course2->id);
1494         $this->getDataGenerator()->enrol_user($user2->id, $course2->id);
1496         // Create course groups with messaging enabled.
1497         $group1a = $this->getDataGenerator()->create_group(array('courseid' => $course1->id, 'enablemessaging' => 1));
1498         $group2a = $this->getDataGenerator()->create_group(array('courseid' => $course2->id, 'enablemessaging' => 1));
1500         // Add users to groups.
1501         $this->getDataGenerator()->create_group_member(array('groupid' => $group1a->id, 'userid' => $user1->id));
1502         $this->getDataGenerator()->create_group_member(array('groupid' => $group1a->id, 'userid' => $user2->id));
1503         $this->getDataGenerator()->create_group_member(array('groupid' => $group1a->id, 'userid' => $user3->id));
1504         $this->getDataGenerator()->create_group_member(array('groupid' => $group2a->id, 'userid' => $user1->id));
1506         // Get conversation.
1507         $conversation1 = \core_message\api::get_conversation_by_area(
1508             $component,
1509             $itemtype,
1510             $group1a->id,
1511             $coursecontext1->id
1512         );
1514         // Send some messages to the group conversation.
1515         $now = time();
1516         $m1id = testhelper::send_fake_message_to_conversation($user1, $conversation1->id, 'Message 1', $now + 1);
1517         $m2id = testhelper::send_fake_message_to_conversation($user1, $conversation1->id, 'Message 2', $now + 2);
1518         $m3id = testhelper::send_fake_message_to_conversation($user2, $conversation1->id, 'Message 3', $now + 3);
1520         // Test for users with any group conversation in course1.
1521         provider::add_conversations_in_context($userlist1, $component, $itemtype);
1522         $this->assertCount(3, $userlist1);
1523         $this->assertEquals([$user1->id, $user2->id, $user3->id], $userlist1->get_userids(), '', 0, 10, true);
1525         // Test for users with any group conversation in course2.
1526         $userlist2 = new \core_privacy\local\request\userlist($coursecontext2, 'core_message');
1527         provider::add_conversations_in_context($userlist2, $component, $itemtype);
1528         $this->assertCount(1, $userlist2);
1529         $this->assertEquals(
1530                 [$user1->id],
1531                 $userlist2->get_userids());
1532     }
1534     /**
1535      * Test for provider::export_conversations().
1536      */
1537     public function test_export_conversations() {
1538         global $DB;
1540         $this->resetAfterTest();
1541         $this->setAdminUser();
1542         $now = time();
1543         $systemcontext = \context_system::instance();
1545         // Create users to test with.
1546         $user1 = $this->getDataGenerator()->create_user();
1547         $user2 = $this->getDataGenerator()->create_user();
1548         $user3 = $this->getDataGenerator()->create_user();
1549         $user1context = \context_user::instance($user1->id);
1551         $course1 = $this->getDataGenerator()->create_course();
1552         $course2 = $this->getDataGenerator()->create_course();
1553         $coursecontext1 = \context_course::instance($course1->id);
1554         $coursecontext2 = \context_course::instance($course2->id);
1556         // Enrol users to courses.
1557         $this->getDataGenerator()->enrol_user($user1->id, $course1->id);
1558         $this->getDataGenerator()->enrol_user($user2->id, $course1->id);
1559         $this->getDataGenerator()->enrol_user($user3->id, $course1->id);
1560         $this->getDataGenerator()->enrol_user($user1->id, $course2->id);
1562         // Create course groups with group messaging enabled.
1563         $group1a = $this->getDataGenerator()->create_group(array('courseid' => $course1->id, 'enablemessaging' => 1));
1564         $group2a = $this->getDataGenerator()->create_group(array('courseid' => $course2->id, 'enablemessaging' => 1));
1566         // Add users to groups.
1567         $this->getDataGenerator()->create_group_member(array('groupid' => $group1a->id, 'userid' => $user1->id));
1568         $this->getDataGenerator()->create_group_member(array('groupid' => $group1a->id, 'userid' => $user2->id));
1569         $this->getDataGenerator()->create_group_member(array('groupid' => $group1a->id, 'userid' => $user3->id));
1570         $this->getDataGenerator()->create_group_member(array('groupid' => $group2a->id, 'userid' => $user1->id));
1572         // Send some private messages between user 1 and user 2.
1573         $pm1id = $this->create_message($user1->id, $user2->id, $now);
1575         // Get conversation.
1576         $iconversation1id = \core_message\api::get_conversation_between_users([$user1->id, $user2->id]);
1577         $component = 'core_group';
1578         $itemtype = 'groups';
1579         $conversation1 = \core_message\api::get_conversation_by_area(
1580             $component,
1581             $itemtype,
1582             $group1a->id,
1583             $coursecontext1->id
1584         );
1586         // Make favourite some conversations.
1587         \core_message\api::set_favourite_conversation($conversation1->id, $user1->id);
1588         \core_message\api::set_favourite_conversation($iconversation1id, $user2->id);
1590         // Mute some conversations.
1591         \core_message\api::mute_conversation($user1->id, $conversation1->id);
1592         \core_message\api::mute_conversation($user2->id, $iconversation1id);
1594         // Send some messages to the conversation.
1595         $m1 = testhelper::send_fake_message_to_conversation($user1, $conversation1->id, 'Message 1', $now + 1);
1596         $m2 = testhelper::send_fake_message_to_conversation($user1, $conversation1->id, 'Message 2', $now + 2);
1597         $m3 = testhelper::send_fake_message_to_conversation($user2, $conversation1->id, 'Message 3', $now + 3);
1599         $dbm1 = $DB->get_record('messages', ['id' => $m1]);
1600         $dbm2 = $DB->get_record('messages', ['id' => $m2]);
1601         $dbm3 = $DB->get_record('messages', ['id' => $m3]);
1603         // Mark as read and delete some messages.
1604         \core_message\api::mark_message_as_read($user1->id, $dbm3, $now + 5);
1605         \core_message\api::delete_message($user1->id, $m2);
1607         // Export all the conversations related to the groups in course1 for user1.
1608         provider::export_conversations($user1->id, 'core_group', 'groups', $coursecontext1);
1610         // Check that system context hasn't been exported.
1611         $writer = writer::with_context($systemcontext);
1612         $this->assertFalse($writer->has_any_data());
1614         // Check that course2 context hasn't been exported.
1615         $writer = writer::with_context($coursecontext2);
1616         $this->assertFalse($writer->has_any_data());
1618         // Check that course1 context has been exported for user1 and contains data.
1619         $writer = writer::with_context($coursecontext1);
1620         $this->assertTrue($writer->has_any_data());
1622         // Confirm the messages for conversation1 are correct.
1623         $messages = (array) $writer->get_data([
1624             get_string('messages', 'core_message'),
1625             get_string($conversation1->itemtype, $conversation1->component),
1626             get_string('privacy:export:conversationprefix', 'core_message') . $conversation1->name
1627         ]);
1628         $this->assertCount(3, $messages);
1630         usort($messages, ['static', 'sort_messages']);
1631         $m1 = array_shift($messages);
1632         $m2 = array_shift($messages);
1633         $m3 = array_shift($messages);
1635         // Check message 1 is correct.
1636         $this->assertEquals(get_string('yes'), $m1->issender);
1637         $this->assertEquals(message_format_message_text($dbm1), $m1->message);
1638         $this->assertEquals(transform::datetime($now + 1), $m1->timecreated);
1639         $this->assertEquals('-', $m1->timeread);
1640         $this->assertArrayNotHasKey('timedeleted', (array) $m1);
1642         // Check message 2 is correct.
1643         $this->assertEquals(get_string('yes'), $m2->issender);
1644         $this->assertEquals(message_format_message_text($dbm2), $m2->message);
1645         $this->assertEquals(transform::datetime($now + 2), $m2->timecreated);
1646         $this->assertEquals('-', $m2->timeread);
1647         $this->assertArrayHasKey('timedeleted', (array) $m2);
1649         // Check message 3 is correct.
1650         $this->assertEquals(get_string('no'), $m3->issender);
1651         $this->assertEquals(message_format_message_text($dbm3), $m3->message);
1652         $this->assertEquals(transform::datetime($now + 3), $m3->timecreated);
1653         $this->assertEquals(transform::datetime($now + 5), $m3->timeread);
1654         $this->assertArrayNotHasKey('timedeleted', (array) $m3);
1656         // Confirm the muted group conversation is correct.
1657         $mutedconversations = (array) $writer->get_related_data([
1658             get_string('messages', 'core_message'),
1659             get_string($conversation1->itemtype, $conversation1->component),
1660             get_string('privacy:export:conversationprefix', 'core_message') . $conversation1->name
1661         ], 'muted');
1662         $this->assertCount(2, $mutedconversations);
1663         $this->assertEquals(get_string('yes'), $mutedconversations['muted']);
1665         // Confirm the favourite group conversation is correct.
1666         $favourite = (array) $writer->get_related_data([
1667             get_string('messages', 'core_message'),
1668             get_string($conversation1->itemtype, $conversation1->component),
1669             get_string('privacy:export:conversationprefix', 'core_message') . $conversation1->name
1670         ], 'starred');
1671         $this->assertCount(4, $favourite);
1672         $this->assertEquals(get_string('yes'), $favourite['starred']);
1674         // Reset writer before exporting conversations for user2.
1675         writer::reset();
1677         // Export all the conversations related to the groups in course1 for user2.
1678         provider::export_conversations($user2->id, 'core_group', 'groups', $coursecontext1);
1680         // Check that system context hasn't been exported.
1681         $writer = writer::with_context($systemcontext);
1682         $this->assertFalse($writer->has_any_data());
1684         // Check that course2 context hasn't been exported.
1685         $writer = writer::with_context($coursecontext2);
1686         $this->assertFalse($writer->has_any_data());
1688         // Check that course1 context has been exported for user2 and contains data.
1689         $writer = writer::with_context($coursecontext1);
1690         $this->assertTrue($writer->has_any_data());
1692         // Confirm the messages for conversation1 are correct.
1693         $messages = (array) $writer->get_data([
1694             get_string('messages', 'core_message'),
1695             get_string($conversation1->itemtype, $conversation1->component),
1696             get_string('privacy:export:conversationprefix', 'core_message') . $conversation1->name
1697         ]);
1698         $this->assertCount(3, $messages);
1700         usort($messages, ['static', 'sort_messages']);
1701         $m1 = array_shift($messages);
1702         $m2 = array_shift($messages);
1703         $m3 = array_shift($messages);
1705         // Check message 1 is correct.
1706         $this->assertEquals(get_string('no'), $m1->issender);
1707         $this->assertEquals(message_format_message_text($dbm1), $m1->message);
1708         $this->assertEquals(transform::datetime($now + 1), $m1->timecreated);
1709         $this->assertEquals('-', $m1->timeread);
1710         $this->assertArrayNotHasKey('timedeleted', (array) $m1);
1712         // Check message 2 is correct.
1713         $this->assertEquals(get_string('no'), $m2->issender);
1714         $this->assertEquals(message_format_message_text($dbm2), $m2->message);
1715         $this->assertEquals(transform::datetime($now + 2), $m2->timecreated);
1716         $this->assertEquals('-', $m2->timeread);
1717         $this->assertArrayNotHasKey('timedeleted', (array) $m2);
1719         // Check message 3 is correct.
1720         $this->assertEquals(get_string('yes'), $m3->issender);
1721         $this->assertEquals(message_format_message_text($dbm3), $m3->message);
1722         $this->assertEquals(transform::datetime($now + 3), $m3->timecreated);
1723         $this->assertEquals('-', $m3->timeread);
1724         $this->assertArrayNotHasKey('timedeleted', (array) $m3);
1726         // Confirm the muted group conversation is correct.
1727         $mutedconversations = (array) $writer->get_related_data([
1728             get_string('messages', 'core_message'),
1729             get_string($conversation1->itemtype, $conversation1->component),
1730             $conversation1->name
1731         ], 'muted');
1732         $this->assertCount(0, $mutedconversations);
1734         // Confirm there are no favourite group conversation for user2.
1735         $favourite = (array) $writer->get_related_data([
1736             get_string('messages', 'core_message'),
1737             get_string($conversation1->itemtype, $conversation1->component),
1738             $conversation1->name
1739         ], 'starred');
1740         $this->assertCount(0, $favourite);
1741     }
1743     /**
1744      * Test for provider::delete_conversations_for_all_users().
1745      */
1746     public function test_delete_conversations_for_all_users() {
1747         global $DB;
1749         $this->resetAfterTest();
1750         $this->setAdminUser();
1751         $now = time();
1752         $timeread = $now - DAYSECS;
1753         $component = 'core_group';
1754         $itemtype = 'groups';
1756         // Create users to test with.
1757         $user1 = $this->getDataGenerator()->create_user();
1758         $user2 = $this->getDataGenerator()->create_user();
1759         $user3 = $this->getDataGenerator()->create_user();
1760         $user4 = $this->getDataGenerator()->create_user();
1761         $user5 = $this->getDataGenerator()->create_user();
1762         $user1context = \context_user::instance($user1->id);
1764         // Create contacts.
1765         \core_message\api::add_contact($user1->id, $user2->id);
1766         \core_message\api::add_contact($user2->id, $user3->id);
1768         // Create contact requests.
1769         \core_message\api::create_contact_request($user1->id, $user3->id);
1770         \core_message\api::create_contact_request($user2->id, $user4->id);
1772         // Block a user.
1773         \core_message\api::block_user($user1->id, $user3->id);
1774         \core_message\api::block_user($user3->id, $user4->id);
1776         // Create individual messages.
1777         $im1 = $this->create_message($user1->id, $user2->id, $now + (9 * DAYSECS), true);
1778         $im2 = $this->create_message($user2->id, $user1->id, $now + (8 * DAYSECS), true);
1779         $im3 = $this->create_message($user2->id, $user3->id, $now + (7 * DAYSECS));
1781         // Create notifications.
1782         $n1 = $this->create_notification($user1->id, $user2->id, $now + (9 * DAYSECS), $timeread);
1783         $n2 = $this->create_notification($user2->id, $user1->id, $now + (8 * DAYSECS));
1784         $n3 = $this->create_notification($user2->id, $user3->id, $now + (7 * DAYSECS));
1786         // Delete one of the messages.
1787         \core_message\api::delete_message($user1->id, $im2);
1789         // Create course2.
1790         $course1 = $this->getDataGenerator()->create_course();
1791         $course2 = $this->getDataGenerator()->create_course();
1792         $coursecontext1 = \context_course::instance($course1->id);
1793         $coursecontext2 = \context_course::instance($course2->id);
1795         // Enrol users to courses.
1796         $this->getDataGenerator()->enrol_user($user1->id, $course1->id);
1797         $this->getDataGenerator()->enrol_user($user2->id, $course1->id);
1798         $this->getDataGenerator()->enrol_user($user3->id, $course1->id);
1799         $this->getDataGenerator()->enrol_user($user1->id, $course2->id);
1800         $this->getDataGenerator()->enrol_user($user2->id, $course2->id);
1802         // Create course groups with group messaging enabled.
1803         $group1a = $this->getDataGenerator()->create_group(array('courseid' => $course1->id, 'enablemessaging' => 1));
1804         $group2a = $this->getDataGenerator()->create_group(array('courseid' => $course2->id, 'enablemessaging' => 1));
1806         // Add users to groups.
1807         $this->getDataGenerator()->create_group_member(array('groupid' => $group1a->id, 'userid' => $user1->id));
1808         $this->getDataGenerator()->create_group_member(array('groupid' => $group1a->id, 'userid' => $user2->id));
1809         $this->getDataGenerator()->create_group_member(array('groupid' => $group1a->id, 'userid' => $user3->id));
1810         $this->getDataGenerator()->create_group_member(array('groupid' => $group2a->id, 'userid' => $user1->id));
1811         $this->getDataGenerator()->create_group_member(array('groupid' => $group2a->id, 'userid' => $user2->id));
1813         // Get conversations.
1814         $iconversation1id = \core_message\api::get_conversation_between_users([$user1->id, $user2->id]);
1815         $conversation1 = \core_message\api::get_conversation_by_area(
1816             $component,
1817             $itemtype,
1818             $group1a->id,
1819             $coursecontext1->id
1820         );
1821         $conversation2 = \core_message\api::get_conversation_by_area(
1822             $component,
1823             $itemtype,
1824             $group2a->id,
1825             $coursecontext2->id
1826         );
1828         // Make favourite some conversations.
1829         \core_message\api::set_favourite_conversation($iconversation1id, $user1->id);
1830         \core_message\api::set_favourite_conversation($conversation1->id, $user1->id);
1831         \core_message\api::set_favourite_conversation($conversation1->id, $user2->id);
1833         // Send some messages to the conversation.
1834         $gm1 = testhelper::send_fake_message_to_conversation($user1, $conversation1->id, 'Message 1.1', $now + 1);
1835         $gm2 = testhelper::send_fake_message_to_conversation($user1, $conversation1->id, 'Message 1.2', $now + 2);
1836         $gm3 = testhelper::send_fake_message_to_conversation($user2, $conversation1->id, 'Message 1.3', $now + 3);
1837         $gm4 = testhelper::send_fake_message_to_conversation($user1, $conversation2->id, 'Message 2.1', $now + 4);
1838         $gm5 = testhelper::send_fake_message_to_conversation($user2, $conversation2->id, 'Message 2.2', $now + 5);
1840         $dbgm1 = $DB->get_record('messages', ['id' => $gm1]);
1841         $dbgm2 = $DB->get_record('messages', ['id' => $gm2]);
1842         $dbgm3 = $DB->get_record('messages', ['id' => $gm3]);
1843         $dbgm4 = $DB->get_record('messages', ['id' => $gm4]);
1844         $dbgm5 = $DB->get_record('messages', ['id' => $gm5]);
1846         // Mark as read one of the conversation messages.
1847         \core_message\api::mark_message_as_read($user1->id, $dbgm3, $now + 5);
1849         // Mark some conversations as muted by two users.
1850         \core_message\api::mute_conversation($user1->id, $iconversation1id);
1851         \core_message\api::mute_conversation($user1->id, $conversation1->id);
1852         \core_message\api::mute_conversation($user2->id, $conversation1->id);
1854         // There should be 2 contacts.
1855         $this->assertEquals(2, $DB->count_records('message_contacts'));
1857         // There should be 2 contact requests.
1858         $this->assertEquals(2, $DB->count_records('message_contact_requests'));
1860         // There should be 2 blocked users.
1861         $this->assertEquals(2, $DB->count_records('message_users_blocked'));
1863         // There should be 8 messages.
1864         $this->assertEquals(8, $DB->count_records('messages'));
1866         // There should be 4 user actions - 3 for reading the message, 1 for deleting.
1867         $this->assertEquals(4, $DB->count_records('message_user_actions'));
1869         // There should be 3 muted conversations.
1870         $this->assertEquals(3, $DB->count_records('message_conversation_actions'));
1872         // There should be 4 conversations - 2 individual + 2 group.
1873         $this->assertEquals(4, $DB->count_records('message_conversations'));
1875         // There should be 9 conversation members - (2 + 2) individual + (3 + 2) group.
1876         $this->assertEquals(9, $DB->count_records('message_conversation_members'));
1878         // There should be 5 notifications (3 from create_notification and 2 from create_contact_request).
1879         $this->assertEquals(5, $DB->count_records('notifications'));
1881         // There should be 3 favourite conversations.
1882         $this->assertEquals(3, $DB->count_records('favourite'));
1884         // Delete conversations for all users in course1.
1885         provider::delete_conversations_for_all_users($coursecontext1, $component, $itemtype);
1887         // There should be still 2 contacts.
1888         $this->assertEquals(2, $DB->count_records('message_contacts'));
1890         // There should be still 2 contact requests.
1891         $this->assertEquals(2, $DB->count_records('message_contact_requests'));
1893         // There should be still 2 blocked users.
1894         $this->assertEquals(2, $DB->count_records('message_users_blocked'));
1896         // There should be 1 muted conversation.
1897         $this->assertEquals(1, $DB->count_records('message_conversation_actions'));
1899         // There should be 3 notifications.
1900         $this->assertEquals(5, $DB->count_records('notifications'));
1902         // There should be 5 messages - 3 individual - 2 group (course2).
1903         $this->assertEquals(5, $DB->count_records('messages'));
1904         $messages = array_keys($DB->get_records('messages'));
1905         $this->assertContains($im1, $messages);
1906         $this->assertContains($im2, $messages);
1907         $this->assertContains($im3, $messages);
1908         $this->assertContains($gm4, $messages);
1909         $this->assertContains($gm5, $messages);
1911         // There should be 3 user actions - 2 for reading the message, 1 for deleting.
1912         $this->assertEquals(3, $DB->count_records('message_user_actions'));
1913         $useractions = $DB->get_records('message_user_actions');
1914         $useractions = array_map(function($action) {
1915                 return $action->messageid;
1916         }, $useractions);
1917         $this->assertNotContains($gm3, $useractions);
1919         // There should be 3 conversations - 2 individual + 1 group (course2).
1920         $this->assertEquals(3, $DB->count_records('message_conversations'));
1921         $conversations = $DB->get_records('message_conversations');
1922         $this->assertArrayNotHasKey($conversation1->id, $conversations);
1924         // There should be 6 conversation members - (2 + 2) individual + 2 group.
1925         $this->assertEquals(6, $DB->count_records('message_conversation_members'));
1927         // There should be 1 favourite conversation - the individual one.
1928         $this->assertEquals(1, $DB->count_records('favourite'));
1929     }
1931     /**
1932      * Test for provider::delete_conversations_for_all_users() in the system context.
1933      */
1934     public function test_delete_conversations_for_all_users_systemcontext() {
1935         global $DB;
1937         $this->resetAfterTest();
1938         $this->setAdminUser();
1939         $now = time();
1940         $timeread = $now - DAYSECS;
1941         $systemcontext = \context_system::instance();
1942         $component = 'core_group';
1943         $itemtype = 'groups';
1945         // Create users to test with.
1946         $user1 = $this->getDataGenerator()->create_user();
1947         $user2 = $this->getDataGenerator()->create_user();
1948         $user3 = $this->getDataGenerator()->create_user();
1949         $user4 = $this->getDataGenerator()->create_user();
1950         $user5 = $this->getDataGenerator()->create_user();
1952         // Create contacts.
1953         \core_message\api::add_contact($user1->id, $user2->id);
1954         \core_message\api::add_contact($user2->id, $user3->id);
1956         // Create contact requests.
1957         \core_message\api::create_contact_request($user1->id, $user3->id);
1958         \core_message\api::create_contact_request($user2->id, $user4->id);
1960         // Block a user.
1961         \core_message\api::block_user($user1->id, $user3->id);
1962         \core_message\api::block_user($user3->id, $user4->id);
1964         // Create individual messages.
1965         $im1 = $this->create_message($user1->id, $user2->id, $now + (9 * DAYSECS), true);
1966         $im2 = $this->create_message($user2->id, $user1->id, $now + (8 * DAYSECS), true);
1967         $im3 = $this->create_message($user2->id, $user3->id, $now + (7 * DAYSECS));
1969         // Create notifications.
1970         $n1 = $this->create_notification($user1->id, $user2->id, $now + (9 * DAYSECS), $timeread);
1971         $n2 = $this->create_notification($user2->id, $user1->id, $now + (8 * DAYSECS));
1972         $n3 = $this->create_notification($user2->id, $user3->id, $now + (7 * DAYSECS));
1974         // Delete one of the messages.
1975         \core_message\api::delete_message($user1->id, $im2);
1977         // Create course2.
1978         $course1 = $this->getDataGenerator()->create_course();
1979         $course2 = $this->getDataGenerator()->create_course();
1980         $coursecontext1 = \context_course::instance($course1->id);
1981         $coursecontext2 = \context_course::instance($course2->id);
1983         // Enrol users to courses.
1984         $this->getDataGenerator()->enrol_user($user1->id, $course1->id);
1985         $this->getDataGenerator()->enrol_user($user2->id, $course1->id);
1986         $this->getDataGenerator()->enrol_user($user3->id, $course1->id);
1987         $this->getDataGenerator()->enrol_user($user1->id, $course2->id);
1988         $this->getDataGenerator()->enrol_user($user2->id, $course2->id);
1990         // Create course groups with group messaging enabled.
1991         $group1a = $this->getDataGenerator()->create_group(array('courseid' => $course1->id, 'enablemessaging' => 1));
1992         $group2a = $this->getDataGenerator()->create_group(array('courseid' => $course2->id, 'enablemessaging' => 1));
1994         // Add users to groups.
1995         $this->getDataGenerator()->create_group_member(array('groupid' => $group1a->id, 'userid' => $user1->id));
1996         $this->getDataGenerator()->create_group_member(array('groupid' => $group1a->id, 'userid' => $user2->id));
1997         $this->getDataGenerator()->create_group_member(array('groupid' => $group1a->id, 'userid' => $user3->id));
1998         $this->getDataGenerator()->create_group_member(array('groupid' => $group2a->id, 'userid' => $user1->id));
1999         $this->getDataGenerator()->create_group_member(array('groupid' => $group2a->id, 'userid' => $user2->id));
2001         // Get conversations.
2002         $iconversation1id = \core_message\api::get_conversation_between_users([$user1->id, $user2->id]);
2003         $conversation1 = \core_message\api::get_conversation_by_area(
2004             $component,
2005             $itemtype,
2006             $group1a->id,
2007             $coursecontext1->id
2008         );
2009         $conversation2 = \core_message\api::get_conversation_by_area(
2010             $component,
2011             $itemtype,
2012             $group2a->id,
2013             $coursecontext2->id
2014         );
2016         // Make favourite some conversations.
2017         \core_message\api::set_favourite_conversation($iconversation1id, $user1->id);
2018         \core_message\api::set_favourite_conversation($conversation1->id, $user1->id);
2019         \core_message\api::set_favourite_conversation($conversation1->id, $user2->id);
2021         // Send some messages to the conversation.
2022         $gm1 = testhelper::send_fake_message_to_conversation($user1, $conversation1->id, 'Message 1.1', $now + 1);
2023         $gm2 = testhelper::send_fake_message_to_conversation($user1, $conversation1->id, 'Message 1.2', $now + 2);
2024         $gm3 = testhelper::send_fake_message_to_conversation($user2, $conversation1->id, 'Message 1.3', $now + 3);
2025         $gm4 = testhelper::send_fake_message_to_conversation($user1, $conversation2->id, 'Message 2.1', $now + 4);
2026         $gm5 = testhelper::send_fake_message_to_conversation($user2, $conversation2->id, 'Message 2.2', $now + 5);
2028         $dbgm3 = $DB->get_record('messages', ['id' => $gm3]);
2030         // Mark some conversations as muted by two users.
2031         \core_message\api::mute_conversation($user1->id, $iconversation1id);
2032         \core_message\api::mute_conversation($user1->id, $conversation1->id);
2033         \core_message\api::mute_conversation($user2->id, $conversation1->id);
2035         // Mark as read one of the conversation messages.
2036         \core_message\api::mark_message_as_read($user1->id, $dbgm3, $now + 5);
2038         // There should be 2 contacts.
2039         $this->assertEquals(2, $DB->count_records('message_contacts'));
2041         // There should be 2 contact requests.
2042         $this->assertEquals(2, $DB->count_records('message_contact_requests'));
2044         // There should be 2 blocked users.
2045         $this->assertEquals(2, $DB->count_records('message_users_blocked'));
2047         // There should be 8 messages.
2048         $this->assertEquals(8, $DB->count_records('messages'));
2050         // There should be 4 user actions - 3 for reading the message, 1 for deleting.
2051         $this->assertEquals(4, $DB->count_records('message_user_actions'));
2053         // There should be 3 muted conversations.
2054         $this->assertEquals(3, $DB->count_records('message_conversation_actions'));
2056         // There should be 4 conversations - 2 individual + 2 group.
2057         $this->assertEquals(4, $DB->count_records('message_conversations'));
2059         // There should be 9 conversation members - (2 + 2) individual + (3 + 2) group.
2060         $this->assertEquals(9, $DB->count_records('message_conversation_members'));
2062         // There should be 5 notifications (3 from create_notification and 2 from create_contact_request).
2063         $this->assertEquals(5, $DB->count_records('notifications'));
2065         // There should be 3 favourite conversations.
2066         $this->assertEquals(3, $DB->count_records('favourite'));
2068         // Delete group conversations for all users in system context.
2069         provider::delete_conversations_for_all_users($systemcontext, $component, $itemtype);
2071         // No conversations should be removed, because they are in the course context.
2072         $this->assertEquals(2, $DB->count_records('message_contacts'));
2073         $this->assertEquals(2, $DB->count_records('message_contact_requests'));
2074         $this->assertEquals(2, $DB->count_records('message_users_blocked'));
2075         $this->assertEquals(8, $DB->count_records('messages'));
2076         $this->assertEquals(4, $DB->count_records('message_user_actions'));
2077         $this->assertEquals(3, $DB->count_records('message_conversation_actions'));
2078         $this->assertEquals(4, $DB->count_records('message_conversations'));
2079         $this->assertEquals(9, $DB->count_records('message_conversation_members'));
2080         $this->assertEquals(5, $DB->count_records('notifications'));
2081         $this->assertEquals(3, $DB->count_records('favourite'));
2083         // Delete individual conversations for all users in system context.
2084         provider::delete_conversations_for_all_users($systemcontext, '', '');
2086         // No conversations should be removed, because they've been moved to user context.
2087         $this->assertEquals(2, $DB->count_records('message_contacts'));
2088         $this->assertEquals(2, $DB->count_records('message_contact_requests'));
2089         $this->assertEquals(2, $DB->count_records('message_users_blocked'));
2090         $this->assertEquals(8, $DB->count_records('messages'));
2091         $this->assertEquals(4, $DB->count_records('message_user_actions'));
2092         $this->assertEquals(3, $DB->count_records('message_conversation_actions'));
2093         $this->assertEquals(4, $DB->count_records('message_conversations'));
2094         $this->assertEquals(9, $DB->count_records('message_conversation_members'));
2095         $this->assertEquals(5, $DB->count_records('notifications'));
2096         $this->assertEquals(3, $DB->count_records('favourite'));
2097     }
2099     /**
2100      * Test for provider::delete_conversations_for_all_users() in the user context.
2101      */
2102     public function test_delete_conversations_for_all_users_usercontext() {
2103         global $DB;
2105         $this->resetAfterTest();
2106         $this->setAdminUser();
2107         $now = time();
2108         $timeread = $now - DAYSECS;
2109         $component = 'core_group';
2110         $itemtype = 'groups';
2112         // Create users to test with.
2113         $user1 = $this->getDataGenerator()->create_user();
2114         $user2 = $this->getDataGenerator()->create_user();
2115         $user3 = $this->getDataGenerator()->create_user();
2116         $user4 = $this->getDataGenerator()->create_user();
2117         $user5 = $this->getDataGenerator()->create_user();
2118         $user1context = \context_user::instance($user1->id);
2120         // Create contacts.
2121         \core_message\api::add_contact($user1->id, $user2->id);
2122         \core_message\api::add_contact($user2->id, $user3->id);
2124         // Create contact requests.
2125         \core_message\api::create_contact_request($user1->id, $user3->id);
2126         \core_message\api::create_contact_request($user2->id, $user4->id);
2128         // Block a user.
2129         \core_message\api::block_user($user1->id, $user3->id);
2130         \core_message\api::block_user($user3->id, $user4->id);
2132         // Create individual messages.
2133         $im1 = $this->create_message($user1->id, $user2->id, $now + (9 * DAYSECS), true);
2134         $im2 = $this->create_message($user2->id, $user1->id, $now + (8 * DAYSECS), true);
2135         $im3 = $this->create_message($user2->id, $user3->id, $now + (7 * DAYSECS));
2137         // Create notifications.
2138         $n1 = $this->create_notification($user1->id, $user2->id, $now + (9 * DAYSECS), $timeread);
2139         $n2 = $this->create_notification($user2->id, $user1->id, $now + (8 * DAYSECS));
2140         $n3 = $this->create_notification($user2->id, $user3->id, $now + (7 * DAYSECS));
2142         // Delete one of the messages.
2143         \core_message\api::delete_message($user1->id, $im2);
2145         // Create course2.
2146         $course1 = $this->getDataGenerator()->create_course();
2147         $course2 = $this->getDataGenerator()->create_course();
2148         $coursecontext1 = \context_course::instance($course1->id);
2149         $coursecontext2 = \context_course::instance($course2->id);
2151         // Enrol users to courses.
2152         $this->getDataGenerator()->enrol_user($user1->id, $course1->id);
2153         $this->getDataGenerator()->enrol_user($user2->id, $course1->id);
2154         $this->getDataGenerator()->enrol_user($user3->id, $course1->id);
2155         $this->getDataGenerator()->enrol_user($user1->id, $course2->id);
2156         $this->getDataGenerator()->enrol_user($user2->id, $course2->id);
2158         // Create course groups with group messaging enabled.
2159         $group1a = $this->getDataGenerator()->create_group(array('courseid' => $course1->id, 'enablemessaging' => 1));
2160         $group2a = $this->getDataGenerator()->create_group(array('courseid' => $course2->id, 'enablemessaging' => 1));
2162         // Add users to groups.
2163         $this->getDataGenerator()->create_group_member(array('groupid' => $group1a->id, 'userid' => $user1->id));
2164         $this->getDataGenerator()->create_group_member(array('groupid' => $group1a->id, 'userid' => $user2->id));
2165         $this->getDataGenerator()->create_group_member(array('groupid' => $group1a->id, 'userid' => $user3->id));
2166         $this->getDataGenerator()->create_group_member(array('groupid' => $group2a->id, 'userid' => $user1->id));
2167         $this->getDataGenerator()->create_group_member(array('groupid' => $group2a->id, 'userid' => $user2->id));
2169         // Get conversation.
2170         $iconversation1id = \core_message\api::get_conversation_between_users([$user1->id, $user2->id]);
2171         $iconversation2id = \core_message\api::get_conversation_between_users([$user2->id, $user3->id]);
2172         $conversation1 = \core_message\api::get_conversation_by_area(
2173             $component,
2174             $itemtype,
2175             $group1a->id,
2176             $coursecontext1->id
2177         );
2178         $conversation2 = \core_message\api::get_conversation_by_area(
2179             $component,
2180             $itemtype,
2181             $group2a->id,
2182             $coursecontext2->id
2183         );
2185         // Make favourite some conversations.
2186         \core_message\api::set_favourite_conversation($iconversation1id, $user1->id);
2187         \core_message\api::set_favourite_conversation($conversation1->id, $user1->id);
2188         \core_message\api::set_favourite_conversation($conversation1->id, $user2->id);
2190         // Send some messages to the conversation.
2191         $gm1 = testhelper::send_fake_message_to_conversation($user1, $conversation1->id, 'Message 1.1', $now + 1);
2192         $gm2 = testhelper::send_fake_message_to_conversation($user1, $conversation1->id, 'Message 1.2', $now + 2);
2193         $gm3 = testhelper::send_fake_message_to_conversation($user2, $conversation1->id, 'Message 1.3', $now + 3);
2194         $gm4 = testhelper::send_fake_message_to_conversation($user1, $conversation2->id, 'Message 2.1', $now + 4);
2195         $gm5 = testhelper::send_fake_message_to_conversation($user2, $conversation2->id, 'Message 2.2', $now + 5);
2197         $dbgm3 = $DB->get_record('messages', ['id' => $gm3]);
2199         // Mark as read one of the conversation messages.
2200         \core_message\api::mark_message_as_read($user1->id, $dbgm3, $now + 5);
2202         // Mark some of the conversations as muted by two users.
2203         \core_message\api::mute_conversation($user1->id, $iconversation1id);
2204         \core_message\api::mute_conversation($user1->id, $conversation1->id);
2205         \core_message\api::mute_conversation($user2->id, $conversation1->id);
2207         // There should be 2 contacts.
2208         $this->assertEquals(2, $DB->count_records('message_contacts'));
2210         // There should be 2 contact requests.
2211         $this->assertEquals(2, $DB->count_records('message_contact_requests'));
2213         // There should be 2 blocked users.
2214         $this->assertEquals(2, $DB->count_records('message_users_blocked'));
2216         // There should be 8 messages - 3 individual + 5 group.
2217         $this->assertEquals(8, $DB->count_records('messages'));
2219         // There should be 4 user actions - 3 for reading the message, 1 for deleting.
2220         $this->assertEquals(4, $DB->count_records('message_user_actions'));
2222         // There should be 3 muted conversations.
2223         $this->assertEquals(3, $DB->count_records('message_conversation_actions'));
2225         // There should be 4 conversations - 2 individual + 2 group.
2226         $this->assertEquals(4, $DB->count_records('message_conversations'));
2228         // There should be 9 conversation members - (2 + 2) individual + (3 + 2) group.
2229         $this->assertEquals(9, $DB->count_records('message_conversation_members'));
2231         // There should be 5 notifications (3 from create_notification and 2 from create_contact_request).
2232         $this->assertEquals(5, $DB->count_records('notifications'));
2234         // There should be 3 favourite conversations.
2235         $this->assertEquals(3, $DB->count_records('favourite'));
2237         // Delete group conversations for all users in user context.
2238         provider::delete_conversations_for_all_users($user1context, $component, $itemtype);
2240         // No conversations should be removed, because they are in the course context.
2241         $this->assertEquals(2, $DB->count_records('message_contacts'));
2242         $this->assertEquals(2, $DB->count_records('message_contact_requests'));
2243         $this->assertEquals(2, $DB->count_records('message_users_blocked'));
2244         $this->assertEquals(8, $DB->count_records('messages'));
2245         $this->assertEquals(4, $DB->count_records('message_user_actions'));
2246         $this->assertEquals(3, $DB->count_records('message_conversation_actions'));
2247         $this->assertEquals(4, $DB->count_records('message_conversations'));
2248         $this->assertEquals(9, $DB->count_records('message_conversation_members'));
2249         $this->assertEquals(5, $DB->count_records('notifications'));
2250         $this->assertEquals(3, $DB->count_records('favourite'));
2252         // Delete individual conversations for all users in user context.
2253         provider::delete_conversations_for_all_users($user1context, '', '');
2255         // No conversations should be removed, because they are in the course context.
2256         $this->assertEquals(2, $DB->count_records('message_contacts'));
2257         $this->assertEquals(2, $DB->count_records('message_contact_requests'));
2258         $this->assertEquals(2, $DB->count_records('message_users_blocked'));
2259         $this->assertEquals(8, $DB->count_records('messages'));
2260         $this->assertEquals(4, $DB->count_records('message_user_actions'));
2261         $this->assertEquals(3, $DB->count_records('message_conversation_actions'));
2262         $this->assertEquals(4, $DB->count_records('message_conversations'));
2263         $this->assertEquals(9, $DB->count_records('message_conversation_members'));
2264         $this->assertEquals(5, $DB->count_records('notifications'));
2265         $this->assertEquals(3, $DB->count_records('favourite'));
2266     }
2268     /**
2269      * Test for provider::delete_conversations_for_user().
2270      */
2271     public function test_delete_conversations_for_user() {
2272         global $DB;
2274         $this->resetAfterTest();
2275         $this->setAdminUser();
2276         $now = time();
2277         $timeread = $now - DAYSECS;
2278         $systemcontext = \context_system::instance();
2279         $component = 'core_group';
2280         $itemtype = 'groups';
2282         // Create users to test with.
2283         $user1 = $this->getDataGenerator()->create_user();
2284         $user2 = $this->getDataGenerator()->create_user();
2285         $user3 = $this->getDataGenerator()->create_user();
2286         $user4 = $this->getDataGenerator()->create_user();
2287         $user5 = $this->getDataGenerator()->create_user();
2288         $user1context = \context_user::instance($user1->id);
2290         // Create contacts.
2291         \core_message\api::add_contact($user1->id, $user2->id);
2292         \core_message\api::add_contact($user2->id, $user3->id);
2294         // Create contact requests.
2295         \core_message\api::create_contact_request($user1->id, $user3->id);
2296         \core_message\api::create_contact_request($user2->id, $user4->id);
2298         // Block a user.
2299         \core_message\api::block_user($user1->id, $user3->id);
2300         \core_message\api::block_user($user3->id, $user4->id);
2302         // Create private messages.
2303         $pm1 = $this->create_message($user1->id, $user2->id, $now + (9 * DAYSECS), true);
2304         $pm2 = $this->create_message($user2->id, $user1->id, $now + (8 * DAYSECS), true);
2305         $pm3 = $this->create_message($user2->id, $user3->id, $now + (7 * DAYSECS));
2307         // Create notifications.
2308         $n1 = $this->create_notification($user1->id, $user2->id, $now + (9 * DAYSECS), $timeread);
2309         $n2 = $this->create_notification($user2->id, $user1->id, $now + (8 * DAYSECS));
2310         $n3 = $this->create_notification($user2->id, $user3->id, $now + (7 * DAYSECS));
2312         // Delete one of the messages.
2313         \core_message\api::delete_message($user1->id, $pm2);
2315         // Create course.
2316         $course1 = $this->getDataGenerator()->create_course();
2317         $course2 = $this->getDataGenerator()->create_course();
2318         $coursecontext1 = \context_course::instance($course1->id);
2319         $coursecontext2 = \context_course::instance($course2->id);
2321         // Enrol users to courses.
2322         $this->getDataGenerator()->enrol_user($user1->id, $course1->id);
2323         $this->getDataGenerator()->enrol_user($user2->id, $course1->id);
2324         $this->getDataGenerator()->enrol_user($user3->id, $course1->id);
2325         $this->getDataGenerator()->enrol_user($user1->id, $course2->id);
2327         // Create course groups with group messaging enabled.
2328         $group1a = $this->getDataGenerator()->create_group(array('courseid' => $course1->id, 'enablemessaging' => 1));
2330         // Add users to groups.
2331         $this->getDataGenerator()->create_group_member(array('groupid' => $group1a->id, 'userid' => $user1->id));
2332         $this->getDataGenerator()->create_group_member(array('groupid' => $group1a->id, 'userid' => $user2->id));
2333         $this->getDataGenerator()->create_group_member(array('groupid' => $group1a->id, 'userid' => $user3->id));
2335         // Get conversation.
2336         $iconversation1id = \core_message\api::get_conversation_between_users([$user1->id, $user2->id]);
2337         $conversation1 = \core_message\api::get_conversation_by_area(
2338             $component,
2339             $itemtype,
2340             $group1a->id,
2341             $coursecontext1->id
2342         );
2344         // Make favourite some conversations.
2345         \core_message\api::set_favourite_conversation($iconversation1id, $user1->id);
2346         \core_message\api::set_favourite_conversation($conversation1->id, $user1->id);
2347         \core_message\api::set_favourite_conversation($conversation1->id, $user2->id);
2349         // Send some messages to the conversation.
2350         $gm1 = testhelper::send_fake_message_to_conversation($user1, $conversation1->id, 'Message 1', $now + 1);
2351         $gm2 = testhelper::send_fake_message_to_conversation($user1, $conversation1->id, 'Message 2', $now + 2);
2352         $gm3 = testhelper::send_fake_message_to_conversation($user2, $conversation1->id, 'Message 3', $now + 3);
2354         $dbm3 = $DB->get_record('messages', ['id' => $gm3]);
2356         // Mark as read one of the conversation messages.
2357         \core_message\api::mark_message_as_read($user1->id, $dbm3, $now + 5);
2359         // Mark some of the conversations as muted by two users.
2360         \core_message\api::mute_conversation($user1->id, $iconversation1id);
2361         \core_message\api::mute_conversation($user1->id, $conversation1->id);
2362         \core_message\api::mute_conversation($user2->id, $conversation1->id);
2364         // There should be 2 contacts.
2365         $this->assertEquals(2, $DB->count_records('message_contacts'));
2367         // There should be 2 contact requests.
2368         $this->assertEquals(2, $DB->count_records('message_contact_requests'));
2370         // There should be 2 blocked users.
2371         $this->assertEquals(2, $DB->count_records('message_users_blocked'));
2373         // There should be 5 notifications.
2374         $this->assertEquals(5, $DB->count_records('notifications'));
2376         // There should be 6 messages.
2377         $this->assertEquals(6, $DB->count_records('messages'));
2379         // There should be 4 user actions - 3 for reading the message, one for deleting.
2380         $this->assertEquals(4, $DB->count_records('message_user_actions'));
2382         // There should be 3 users muting a conversation.
2383         $this->assertEquals(3, $DB->count_records('message_conversation_actions'));
2385         // There should be 3 conversations - 2 private + 1 group.
2386         $this->assertEquals(3, $DB->count_records('message_conversations'));
2388         // There should be 7 conversation members - 2 + 2 private conversations + 3 group conversation.
2389         $this->assertEquals(7, $DB->count_records('message_conversation_members'));
2390         $members = $DB->get_records('message_conversation_members', ['conversationid' => $conversation1->id]);
2391         $members = array_map(function($member) {
2392                 return $member->userid;
2393         }, $members);
2394         $this->assertContains($user1->id, $members);
2396         // There should be three favourite conversations.
2397         $this->assertEquals(3, $DB->count_records('favourite'));
2399         // Delete group conversations for user1 in course1 and course2.
2400         $approvedcontextlist = new \core_privacy\tests\request\approved_contextlist($user1, 'core_message',
2401                 [$coursecontext1->id, $coursecontext2->id]);
2402         provider::delete_conversations_for_user($approvedcontextlist, $component, $itemtype);
2404         // There should be still 2 contacts.
2405         $this->assertEquals(2, $DB->count_records('message_contacts'));
2407         // There should be still 2 contact requests.
2408         $this->assertEquals(2, $DB->count_records('message_contact_requests'));
2410         // There should be still 2 blocked users.
2411         $this->assertEquals(2, $DB->count_records('message_users_blocked'));
2413         // There should be 2 muted conversation.
2414         $this->assertEquals(2, $DB->count_records('message_conversation_actions'));
2416         // There should be 3 notifications.
2417         $this->assertEquals(5, $DB->count_records('notifications'));
2419         // There should be 4 messages - 3 private + 1 group sent by user2.
2420         $this->assertEquals(4, $DB->count_records('messages'));
2421         $messages = array_keys($DB->get_records('messages'));
2422         $this->assertContains($pm1, $messages);
2423         $this->assertContains($pm2, $messages);
2424         $this->assertContains($pm3, $messages);
2425         $this->assertContains($gm3, $messages);
2427         // There should be 3 user actions - 2 for reading the message, one for deleting.
2428         $this->assertEquals(3, $DB->count_records('message_user_actions'));
2429         $useractions = $DB->get_records('message_user_actions');
2430         $useractions = array_map(function($action) {
2431                 return $action->messageid;
2432         }, $useractions);
2433         $this->assertNotContains($gm3, $useractions);
2435         // There should be still 3 conversations - 2 private + 1 group.
2436         $this->assertEquals(3, $DB->count_records('message_conversations'));
2438         // There should be 6 conversation members - 2 + 2 private conversations + 2 group conversation.
2439         $this->assertEquals(6, $DB->count_records('message_conversation_members'));
2440         $members = $DB->get_records('message_conversation_members', ['conversationid' => $conversation1->id]);
2441         $members = array_map(function($member) {
2442                 return $member->userid;
2443         }, $members);
2444         $this->assertNotContains($user1->id, $members);
2446         // There should be 2 favourite conversations - 2 group.
2447         $this->assertEquals(2, $DB->count_records('favourite'));
2448         $favourites = $DB->get_records('favourite');
2449         foreach ($favourites as $favourite) {
2450             if ($favourite->userid == $user1->id) {
2451                 $this->assertEquals($iconversation1id, $favourite->itemid);
2452             } else if ($favourite->userid == $user2->id) {
2453                 $this->assertEquals($conversation1->id, $favourite->itemid);
2454             }
2455         }
2456     }
2459     /**
2460      * Test for provider::delete_conversations_for_users().
2461      */
2462     public function test_delete_conversations_for_users() {
2463         global $DB;
2465         $this->resetAfterTest();
2466         $this->setAdminUser();
2467         $now = time();
2468         $timeread = $now - DAYSECS;
2469         $systemcontext = \context_system::instance();
2470         $component = 'core_group';
2471         $itemtype = 'groups';
2473         // Create users to test with.
2474         $user1 = $this->getDataGenerator()->create_user();
2475         $user2 = $this->getDataGenerator()->create_user();
2476         $user3 = $this->getDataGenerator()->create_user();
2477         $user4 = $this->getDataGenerator()->create_user();
2478         $user5 = $this->getDataGenerator()->create_user();
2479         $user1context = \context_user::instance($user1->id);
2481         // Create contacts.
2482         \core_message\api::add_contact($user1->id, $user2->id);
2483         \core_message\api::add_contact($user2->id, $user3->id);
2485         // Create contact requests.
2486         \core_message\api::create_contact_request($user1->id, $user3->id);
2487         \core_message\api::create_contact_request($user2->id, $user4->id);
2489         // Block a user.
2490         \core_message\api::block_user($user1->id, $user3->id);
2491         \core_message\api::block_user($user3->id, $user4->id);
2493         // Create private messages.
2494         $pm1 = $this->create_message($user1->id, $user2->id, $now + (9 * DAYSECS), true);
2495         $pm2 = $this->create_message($user2->id, $user1->id, $now + (8 * DAYSECS), true);
2496         $pm3 = $this->create_message($user2->id, $user3->id, $now + (7 * DAYSECS));
2498         // Create notifications.
2499         $n1 = $this->create_notification($user1->id, $user2->id, $now + (9 * DAYSECS), $timeread);
2500         $n2 = $this->create_notification($user2->id, $user1->id, $now + (8 * DAYSECS));
2501         $n3 = $this->create_notification($user2->id, $user3->id, $now + (7 * DAYSECS));
2503         // Delete one of the messages.
2504         \core_message\api::delete_message($user1->id, $pm2);
2506         // Create course.
2507         $course1 = $this->getDataGenerator()->create_course();
2508         $course2 = $this->getDataGenerator()->create_course();
2509         $coursecontext1 = \context_course::instance($course1->id);
2510         $coursecontext2 = \context_course::instance($course2->id);
2512         // Enrol users to courses.
2513         $this->getDataGenerator()->enrol_user($user1->id, $course1->id);
2514         $this->getDataGenerator()->enrol_user($user2->id, $course1->id);
2515         $this->getDataGenerator()->enrol_user($user3->id, $course1->id);
2516         $this->getDataGenerator()->enrol_user($user4->id, $course1->id);
2517         $this->getDataGenerator()->enrol_user($user1->id, $course2->id);
2519         // Create course groups with group messaging enabled.
2520         $group1a = $this->getDataGenerator()->create_group(array('courseid' => $course1->id, 'enablemessaging' => 1));
2521         $group2a = $this->getDataGenerator()->create_group(array('courseid' => $course2->id, 'enablemessaging' => 1));
2523         // Add users to groups.
2524         $this->getDataGenerator()->create_group_member(array('groupid' => $group1a->id, 'userid' => $user1->id));
2525         $this->getDataGenerator()->create_group_member(array('groupid' => $group1a->id, 'userid' => $user2->id));
2526         $this->getDataGenerator()->create_group_member(array('groupid' => $group1a->id, 'userid' => $user3->id));
2527         $this->getDataGenerator()->create_group_member(array('groupid' => $group1a->id, 'userid' => $user4->id));
2529         // Get conversation.
2530         $iconversation1id = \core_message\api::get_conversation_between_users([$user1->id, $user2->id]);
2531         $conversation1 = \core_message\api::get_conversation_by_area(
2532             $component,
2533             $itemtype,
2534             $group1a->id,
2535             $coursecontext1->id
2536         );
2538         // Make favourite some conversations.
2539         \core_message\api::set_favourite_conversation($iconversation1id, $user1->id);
2540         \core_message\api::set_favourite_conversation($conversation1->id, $user1->id);
2541         \core_message\api::set_favourite_conversation($conversation1->id, $user3->id);
2543         // Send some messages to the conversation.
2544         $gm1 = testhelper::send_fake_message_to_conversation($user1, $conversation1->id, 'Message 1', $now + 1);
2545         $gm2 = testhelper::send_fake_message_to_conversation($user2, $conversation1->id, 'Message 2', $now + 2);
2546         $gm3 = testhelper::send_fake_message_to_conversation($user3, $conversation1->id, 'Message 3', $now + 3);
2548         $dbm3 = $DB->get_record('messages', ['id' => $gm3]);
2550         // Mark as read one of the conversation messages.
2551         \core_message\api::mark_message_as_read($user1->id, $dbm3, $now + 5);
2553         // Mark some of the conversations as muted by two users.
2554         \core_message\api::mute_conversation($user1->id, $iconversation1id);
2555         \core_message\api::mute_conversation($user1->id, $conversation1->id);
2556         \core_message\api::mute_conversation($user2->id, $conversation1->id);
2558         // There should be 2 contacts.
2559         $this->assertEquals(2, $DB->count_records('message_contacts'));
2561         // There should be 2 contact requests.
2562         $this->assertEquals(2, $DB->count_records('message_contact_requests'));
2564         // There should be 2 blocked users.
2565         $this->assertEquals(2, $DB->count_records('message_users_blocked'));
2567         // There should be 5 notifications (3 from create_notification and 2 from create_contact_request).
2568         $this->assertEquals(5, $DB->count_records('notifications'));
2570         // There should be 6 messages.
2571         $this->assertEquals(6, $DB->count_records('messages'));
2573         // There should be 4 user actions - 3 for reading the message, one for deleting.
2574         $this->assertEquals(4, $DB->count_records('message_user_actions'));
2576         // There should be 3 muted conversation.
2577         $this->assertEquals(3, $DB->count_records('message_conversation_actions'));
2579         // There should be 3 conversations - 2 private + 2 group.
2580         $this->assertEquals(4, $DB->count_records('message_conversations'));
2582         // There should be 8 conversation members - (2 + 2) private + 4 group.
2583         $this->assertEquals(8, $DB->count_records('message_conversation_members'));
2584         $members = $DB->get_records('message_conversation_members', ['conversationid' => $conversation1->id]);
2585         $members = array_map(function($member) {
2586                 return $member->userid;
2587         }, $members);
2588         $this->assertContains($user1->id, $members);
2589         $this->assertContains($user4->id, $members);
2591         // There should be 3 favourite conversations.
2592         $this->assertEquals(3, $DB->count_records('favourite'));
2594         // Delete group conversations for user1 and user2 in course2 context.
2595         $approveduserlist = new \core_privacy\local\request\approved_userlist($coursecontext2, 'core_message',
2596                 [$user1->id, $user2->id]);
2597         provider::delete_conversations_for_users($approveduserlist, $component, $itemtype);
2599         // There should be exactly the same content, because $user1 and $user2 don't belong to any group in course2).
2600         $this->assertEquals(2, $DB->count_records('message_contacts'));
2601         $this->assertEquals(2, $DB->count_records('message_contact_requests'));
2602         $this->assertEquals(2, $DB->count_records('message_users_blocked'));
2603         $this->assertEquals(5, $DB->count_records('notifications'));
2604         $this->assertEquals(6, $DB->count_records('messages'));
2605         $this->assertEquals(4, $DB->count_records('message_user_actions'));
2606         $this->assertEquals(3, $DB->count_records('message_conversation_actions'));
2607         $this->assertEquals(4, $DB->count_records('message_conversations'));
2608         $this->assertEquals(8, $DB->count_records('message_conversation_members'));
2609         $this->assertEquals(3, $DB->count_records('favourite'));
2611         // Delete group conversations for user4 in course1 context.
2612         $approveduserlist = new \core_privacy\local\request\approved_userlist($coursecontext1, 'core_message',
2613                 [$user4->id]);
2614         provider::delete_conversations_for_users($approveduserlist, $component, $itemtype);
2616         // There should be the same content except for the members (to remove user4 from the group1 in course1).
2617         $this->assertEquals(2, $DB->count_records('message_contacts'));
2618         $this->assertEquals(2, $DB->count_records('message_contact_requests'));
2619         $this->assertEquals(2, $DB->count_records('message_users_blocked'));
2620         $this->assertEquals(5, $DB->count_records('notifications'));
2621         $this->assertEquals(6, $DB->count_records('messages'));
2622         $this->assertEquals(4, $DB->count_records('message_user_actions'));
2623         $this->assertEquals(3, $DB->count_records('message_conversation_actions'));
2624         $this->assertEquals(4, $DB->count_records('message_conversations'));
2625         $this->assertEquals(3, $DB->count_records('favourite'));
2626         // There should be 7 conversation members - (2 + 2) private + 3 group.
2627         $this->assertEquals(7, $DB->count_records('message_conversation_members'));
2629         // Delete group conversations for user1 and user2 in course1 context.
2630         $approveduserlist = new \core_privacy\local\request\approved_userlist($coursecontext1, 'core_message',
2631                 [$user1->id, $user2->id]);
2632         provider::delete_conversations_for_users($approveduserlist, $component, $itemtype);
2634         // There should be still 2 contacts.
2635         $this->assertEquals(2, $DB->count_records('message_contacts'));
2637         // There should be still 2 contact requests.
2638         $this->assertEquals(2, $DB->count_records('message_contact_requests'));
2640         // There should be still 2 blocked users.
2641         $this->assertEquals(2, $DB->count_records('message_users_blocked'));
2643         // There should be 5 notifications.
2644         $this->assertEquals(5, $DB->count_records('notifications'));
2646         // There should be 4 messages - 3 private + 1 group sent by user3.
2647         $this->assertEquals(4, $DB->count_records('messages'));
2648         $messages = array_keys($DB->get_records('messages'));
2649         $this->assertContains($pm1, $messages);
2650         $this->assertContains($pm2, $messages);
2651         $this->assertContains($pm3, $messages);
2652         $this->assertContains($gm3, $messages);
2654         // There should be 3 user actions - 2 for reading the message, one for deleting.
2655         $this->assertEquals(3, $DB->count_records('message_user_actions'));
2656         $useractions = $DB->get_records('message_user_actions');
2657         $useractions = array_map(function($action) {
2658                 return $action->messageid;
2659         }, $useractions);
2660         $this->assertNotContains($gm3, $useractions);
2662         // There should be 1 muted conversation.
2663         $this->assertEquals(1, $DB->count_records('message_conversation_actions'));
2665         // There should be still 4 conversations - 2 private + 2 group.
2666         $this->assertEquals(4, $DB->count_records('message_conversations'));
2668         // There should be 5 conversation members - (2 + 2) private + 1 group.
2669         $this->assertEquals(5, $DB->count_records('message_conversation_members'));
2670         $members = $DB->get_records('message_conversation_members', ['conversationid' => $conversation1->id]);
2671         $members = array_map(function($member) {
2672                 return $member->userid;
2673         }, $members);
2674         $this->assertNotContains($user1->id, $members);
2675         $this->assertNotContains($user2->id, $members);
2677         // There should be 2 favourite conversations - user1 individual + user3 group.
2678         $this->assertEquals(2, $DB->count_records('favourite'));
2679         $favourites = $DB->get_records('favourite');
2680         foreach ($favourites as $favourite) {
2681             if ($favourite->userid == $user1->id) {
2682                 $this->assertEquals($iconversation1id, $favourite->itemid);
2683             } else if ($favourite->userid == $user3->id) {
2684                 $this->assertEquals($conversation1->id, $favourite->itemid);
2685             }
2686         }
2687     }
2689     /**
2690      * Creates a message to be used for testing.
2691      *
2692      * @param int $useridfrom The user id from
2693      * @param int $useridto The user id to
2694      * @param int $timecreated
2695      * @param bool $read Do we want to mark the message as read?
2696      * @return int The id of the message
2697      * @throws dml_exception
2698      */
2699     private function create_message(int $useridfrom, int $useridto, int $timecreated = null, bool $read = false) {
2700         global $DB;
2702         static $i = 1;
2704         if (is_null($timecreated)) {
2705             $timecreated = time();
2706         }
2708         if (!$conversationid = \core_message\api::get_conversation_between_users([$useridfrom, $useridto])) {
2709             $conversation = \core_message\api::create_conversation(
2710                 \core_message\api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL,
2711                 [
2712                     $useridfrom,
2713                     $useridto
2714                 ]
2715             );
2716             $conversationid = $conversation->id;
2717         }
2719         // Ok, send the message.
2720         $record = new stdClass();
2721         $record->useridfrom = $useridfrom;
2722         $record->conversationid = $conversationid;
2723         $record->subject = 'No subject';
2724         $record->fullmessage = 'A rad message ' . $i;
2725         $record->smallmessage = 'A rad message ' . $i;
2726         $record->timecreated = $timecreated;
2728         $i++;
2730         $record->id = $DB->insert_record('messages', $record);
2732         if ($read) {
2733             \core_message\api::mark_message_as_read($useridto, $record);
2734         }
2736         return $record->id;
2737     }
2739     /**
2740      * Creates a notification to be used for testing.
2741      *
2742      * @param int $useridfrom The user id from
2743      * @param int $useridto The user id to
2744      * @param int|null $timecreated The time the notification was created
2745      * @param int|null $timeread The time the notification was read, null if it hasn't been.
2746      * @return int The id of the notification
2747      * @throws dml_exception
2748      */
2749     private function create_notification(int $useridfrom, int $useridto, int $timecreated = null, int $timeread = null) {
2750         global $DB;
2752         static $i = 1;
2754         if (is_null($timecreated)) {
2755             $timecreated = time();
2756         }
2758         $record = new stdClass();
2759         $record->useridfrom = $useridfrom;
2760         $record->useridto = $useridto;
2761         $record->subject = 'No subject';
2762         $record->fullmessage = 'Some rad notification ' . $i;
2763         $record->smallmessage = 'Yo homie, you got some stuff to do, yolo. ' . $i;
2764         $record->timeread = $timeread;
2765         $record->timecreated = $timecreated;
2767         $i++;
2769         return $DB->insert_record('notifications', $record);
2770     }
2772     /**
2773      * Comparison function for sorting messages.
2774      *
2775      * @param   \stdClass $a
2776      * @param   \stdClass $b
2777      * @return  bool
2778      */
2779     protected static function sort_messages($a, $b) {
2780         return $a->message > $b->message;
2781     }
2783     /**
2784      * Comparison function for sorting contacts.
2785      *
2786      * @param   \stdClass $a
2787      * @param   \stdClass $b
2788      * @return  bool
2789      */
2790     protected static function sort_contacts($a, $b) {
2791         return $a->contact > $b->contact;
2792     }