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