on-demand release 3.5beta+
[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;
27use \core_privacy\local\request\writer;
28use \core_privacy\local\request\transform;
29
30defined('MOODLE_INTERNAL') || die();
31
32/**
33 * Privacy provider tests class.
34 *
35 * @package core_message
36 * @copyright 2018 Mark Nelson <markn@moodle.com>
37 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
38 */
39class core_message_privacy_provider_testcase extends \core_privacy\tests\provider_testcase {
40
41 /**
42 * Test for provider::get_metadata().
43 */
44 public function test_get_metadata() {
45 $collection = new collection('core_message');
46 $newcollection = provider::get_metadata($collection);
47 $itemcollection = $newcollection->get_collection();
48 $this->assertCount(6, $itemcollection);
49
50 $messagestable = array_shift($itemcollection);
51 $this->assertEquals('messages', $messagestable->get_name());
52
53 $messageuseractionstable = array_shift($itemcollection);
54 $this->assertEquals('message_user_actions', $messageuseractionstable->get_name());
55
56 $messageconversationmemberstable = array_shift($itemcollection);
57 $this->assertEquals('message_conversation_members', $messageconversationmemberstable->get_name());
58
59 $messagecontacts = array_shift($itemcollection);
60 $this->assertEquals('message_contacts', $messagecontacts->get_name());
61
62 $notificationstable = array_shift($itemcollection);
63 $this->assertEquals('notifications', $notificationstable->get_name());
64
65 $usersettings = array_shift($itemcollection);
66 $this->assertEquals('core_message_messageprovider_settings', $usersettings->get_name());
67
68 $privacyfields = $messagestable->get_privacy_fields();
69 $this->assertArrayHasKey('useridfrom', $privacyfields);
70 $this->assertArrayHasKey('conversationid', $privacyfields);
71 $this->assertArrayHasKey('subject', $privacyfields);
72 $this->assertArrayHasKey('fullmessage', $privacyfields);
73 $this->assertArrayHasKey('fullmessageformat', $privacyfields);
74 $this->assertArrayHasKey('fullmessagehtml', $privacyfields);
75 $this->assertArrayHasKey('smallmessage', $privacyfields);
76 $this->assertArrayHasKey('timecreated', $privacyfields);
77 $this->assertEquals('privacy:metadata:messages', $messagestable->get_summary());
78
79 $privacyfields = $messageuseractionstable->get_privacy_fields();
80 $this->assertArrayHasKey('userid', $privacyfields);
81 $this->assertArrayHasKey('messageid', $privacyfields);
82 $this->assertArrayHasKey('action', $privacyfields);
83 $this->assertArrayHasKey('timecreated', $privacyfields);
84 $this->assertEquals('privacy:metadata:message_user_actions', $messageuseractionstable->get_summary());
85
86 $privacyfields = $messageconversationmemberstable->get_privacy_fields();
87 $this->assertArrayHasKey('conversationid', $privacyfields);
88 $this->assertArrayHasKey('userid', $privacyfields);
89 $this->assertArrayHasKey('timecreated', $privacyfields);
90 $this->assertEquals('privacy:metadata:message_conversation_members', $messageconversationmemberstable->get_summary());
91
92 $privacyfields = $messagecontacts->get_privacy_fields();
93 $this->assertArrayHasKey('userid', $privacyfields);
94 $this->assertArrayHasKey('contactid', $privacyfields);
95 $this->assertArrayHasKey('blocked', $privacyfields);
96 $this->assertEquals('privacy:metadata:message_contacts', $messagecontacts->get_summary());
97
98 $privacyfields = $notificationstable->get_privacy_fields();
99 $this->assertArrayHasKey('useridfrom', $privacyfields);
100 $this->assertArrayHasKey('useridto', $privacyfields);
101 $this->assertArrayHasKey('subject', $privacyfields);
102 $this->assertArrayHasKey('fullmessage', $privacyfields);
103 $this->assertArrayHasKey('fullmessageformat', $privacyfields);
104 $this->assertArrayHasKey('fullmessagehtml', $privacyfields);
105 $this->assertArrayHasKey('smallmessage', $privacyfields);
106 $this->assertArrayHasKey('component', $privacyfields);
107 $this->assertArrayHasKey('eventtype', $privacyfields);
108 $this->assertArrayHasKey('contexturl', $privacyfields);
109 $this->assertArrayHasKey('contexturlname', $privacyfields);
110 $this->assertArrayHasKey('timeread', $privacyfields);
111 $this->assertArrayHasKey('timecreated', $privacyfields);
112 $this->assertEquals('privacy:metadata:notifications', $notificationstable->get_summary());
113 }
114
115 /**
116 * Test for provider::export_user_preferences().
117 */
118 public function test_export_user_preferences_no_pref() {
119 $this->resetAfterTest();
120
121 $user = $this->getDataGenerator()->create_user();
122 provider::export_user_preferences($user->id);
123
124 $writer = writer::with_context(\context_system::instance());
125
126 $this->assertFalse($writer->has_any_data());
127 }
128
129 /**
130 * Test for provider::export_user_preferences().
131 */
132 public function test_export_user_preferences() {
133 global $USER;
134
135 $this->resetAfterTest();
136
137 $this->setAdminUser();
138
139 // Create another user to set a preference for who we won't be exporting.
140 $user = $this->getDataGenerator()->create_user();
141
142 // Set some message user preferences.
143 set_user_preference('message_provider_moodle_instantmessage_loggedin', 'airnotifier', $USER->id);
144 set_user_preference('message_provider_moodle_instantmessage_loggedoff', 'popup', $USER->id);
145 set_user_preference('message_blocknoncontacts', 1, $USER->id);
146 set_user_preference('message_provider_moodle_instantmessage_loggedoff', 'inbound', $user->id);
147
148 // Set an unrelated preference.
149 set_user_preference('block_myoverview_last_tab', 'courses', $USER->id);
150
151 provider::export_user_preferences($USER->id);
152
153 $writer = writer::with_context(\context_system::instance());
154
155 $this->assertTrue($writer->has_any_data());
156
157 $prefs = (array) $writer->get_user_preferences('core_message');
158
159 // Check only 3 preferences exist.
160 $this->assertCount(3, $prefs);
161 $this->assertArrayHasKey('message_provider_moodle_instantmessage_loggedin', $prefs);
162 $this->assertArrayHasKey('message_provider_moodle_instantmessage_loggedoff', $prefs);
163 $this->assertArrayHasKey('message_blocknoncontacts', $prefs);
164
165 foreach ($prefs as $key => $pref) {
166 if ($key == 'message_provider_moodle_instantmessage_loggedin') {
167 $this->assertEquals('airnotifier', $pref->value);
168 } else if ($key == 'message_provider_moodle_instantmessage_loggedoff') {
169 $this->assertEquals('popup', $pref->value);
170 } else {
171 $this->assertEquals(1, $pref->value);
172 }
173 }
174 }
175
176 /**
177 * Test for provider::get_contexts_for_userid().
178 */
179 public function test_get_contexts_for_userid() {
180 $this->resetAfterTest();
181
182 $user = $this->getDataGenerator()->create_user();
183 $contextlist = provider::get_contexts_for_userid($user->id);
184 $this->assertCount(1, $contextlist);
185 $contextforuser = $contextlist->current();
186 $this->assertEquals(SYSCONTEXTID, $contextforuser->id);
187 }
188
189 /**
190 * Test for provider::export_user_data().
191 */
192 public function test_export_for_context_with_contacts() {
193 $this->resetAfterTest();
194
195 // Create users to test with.
196 $user1 = $this->getDataGenerator()->create_user();
197 $user2 = $this->getDataGenerator()->create_user();
198 $user3 = $this->getDataGenerator()->create_user();
199 $user4 = $this->getDataGenerator()->create_user();
200
201 // This user will not be added as a contact.
202 $this->getDataGenerator()->create_user();
203
204 message_add_contact($user2->id, 0, $user1->id);
205 message_add_contact($user3->id, 0, $user1->id);
206 message_add_contact($user4->id, 1, $user1->id);
207
208 $this->export_context_data_for_user($user1->id, \context_system::instance(), 'core_message');
209
210 $writer = writer::with_context(\context_system::instance());
211
212 $contacts = (array) $writer->get_data([get_string('contacts', 'core_message')]);
213
214 $this->assertCount(3, $contacts);
215
216 $contact1 = array_shift($contacts);
217 $this->assertEquals($user2->id, $contact1->contact);
218 $this->assertEquals(get_string('no'), $contact1->blocked);
219
220 $contact2 = array_shift($contacts);
221 $this->assertEquals($user3->id, $contact2->contact);
222 $this->assertEquals(get_string('no'), $contact2->blocked);
223
224 $contact3 = array_shift($contacts);
225 $this->assertEquals($user4->id, $contact3->contact);
226 $this->assertEquals(get_string('yes'), $contact3->blocked);
227 }
228
229 /**
230 * Test for provider::export_user_data().
231 */
232 public function test_export_for_context_with_messages() {
233 global $DB;
234
235 $this->resetAfterTest();
236
237 // Create users to test with.
238 $user1 = $this->getDataGenerator()->create_user();
239 $user2 = $this->getDataGenerator()->create_user();
240 $user3 = $this->getDataGenerator()->create_user();
241
242 $now = time();
243
244 // Send messages from user 1 to user 2.
245 $m1 = $this->create_message($user1->id, $user2->id, $now - (9 * DAYSECS), true);
246 $m2 = $this->create_message($user2->id, $user1->id, $now - (8 * DAYSECS));
247 $m3 = $this->create_message($user1->id, $user2->id, $now - (7 * DAYSECS));
248
249 // Send messages from user 3 to user 1.
250 $m4 = $this->create_message($user3->id, $user1->id, $now - (6 * DAYSECS), true);
251 $m5 = $this->create_message($user1->id, $user3->id, $now - (5 * DAYSECS));
252 $m6 = $this->create_message($user3->id, $user1->id, $now - (4 * DAYSECS));
253
254 // Send messages from user 3 to user 2 - these should not be included in the export.
255 $m7 = $this->create_message($user3->id, $user2->id, $now - (3 * DAYSECS), true);
256 $m8 = $this->create_message($user2->id, $user3->id, $now - (2 * DAYSECS));
257 $m9 = $this->create_message($user3->id, $user2->id, $now - (1 * DAYSECS));
258
259 // Mark message 2 and 5 as deleted.
260 \core_message\api::delete_message($user1->id, $m2);
261 \core_message\api::delete_message($user1->id, $m5);
262
263 $this->export_context_data_for_user($user1->id, \context_system::instance(), 'core_message');
264
265 $writer = writer::with_context(\context_system::instance());
266
267 $this->assertTrue($writer->has_any_data());
268
269 // Confirm the messages with user 2 are correct.
270 $messages = (array) $writer->get_data([get_string('messages', 'core_message'), fullname($user2)]);
271 $this->assertCount(3, $messages);
272
273 $dbm1 = $DB->get_record('messages', ['id' => $m1]);
274 $dbm2 = $DB->get_record('messages', ['id' => $m2]);
275 $dbm3 = $DB->get_record('messages', ['id' => $m3]);
276
277 $m1 = array_shift($messages);
278 $m2 = array_shift($messages);
279 $m3 = array_shift($messages);
280
281 $this->assertEquals(get_string('yes'), $m1->sender);
282 $this->assertEquals(message_format_message_text($dbm1), $m1->message);
283 $this->assertEquals(transform::datetime($now - (9 * DAYSECS)), $m1->timecreated);
284 $this->assertNotEquals('-', $m1->timeread);
285 $this->assertArrayNotHasKey('timedeleted', (array) $m1);
286
287 $this->assertEquals(get_string('no'), $m2->sender);
288 $this->assertEquals(message_format_message_text($dbm2), $m2->message);
289 $this->assertEquals(transform::datetime($now - (8 * DAYSECS)), $m2->timecreated);
290 $this->assertEquals('-', $m2->timeread);
291 $this->assertArrayHasKey('timedeleted', (array) $m2);
292
293 $this->assertEquals(get_string('yes'), $m3->sender);
294 $this->assertEquals(message_format_message_text($dbm3), $m3->message);
295 $this->assertEquals(transform::datetime($now - (7 * DAYSECS)), $m3->timecreated);
296 $this->assertEquals('-', $m3->timeread);
297
298 // Confirm the messages with user 3 are correct.
299 $messages = (array) $writer->get_data([get_string('messages', 'core_message'), fullname($user3)]);
300 $this->assertCount(3, $messages);
301
302 $dbm4 = $DB->get_record('messages', ['id' => $m4]);
303 $dbm5 = $DB->get_record('messages', ['id' => $m5]);
304 $dbm6 = $DB->get_record('messages', ['id' => $m6]);
305
306 $m4 = array_shift($messages);
307 $m5 = array_shift($messages);
308 $m6 = array_shift($messages);
309
310 $this->assertEquals(get_string('no'), $m4->sender);
311 $this->assertEquals(message_format_message_text($dbm4), $m4->message);
312 $this->assertEquals(transform::datetime($now - (6 * DAYSECS)), $m4->timecreated);
313 $this->assertNotEquals('-', $m4->timeread);
314 $this->assertArrayNotHasKey('timedeleted', (array) $m4);
315
316 $this->assertEquals(get_string('yes'), $m5->sender);
317 $this->assertEquals(message_format_message_text($dbm5), $m5->message);
318 $this->assertEquals(transform::datetime($now - (5 * DAYSECS)), $m5->timecreated);
319 $this->assertEquals('-', $m5->timeread);
320 $this->assertArrayHasKey('timedeleted', (array) $m5);
321
322 $this->assertEquals(get_string('no'), $m6->sender);
323 $this->assertEquals(message_format_message_text($dbm6), $m6->message);
324 $this->assertEquals(transform::datetime($now - (4 * DAYSECS)), $m6->timecreated);
325 $this->assertEquals('-', $m6->timeread);
326 }
327
328 /**
329 * Test for provider::export_user_data().
330 */
331 public function test_export_for_context_with_notifications() {
332 $this->resetAfterTest();
333
334 // Create users to test with.
335 $user1 = $this->getDataGenerator()->create_user();
336 $user2 = $this->getDataGenerator()->create_user();
337 $user3 = $this->getDataGenerator()->create_user();
338
339 $now = time();
340 $timeread = $now - DAYSECS;
341
342 // Send notifications from user 1 to user 2.
343 $this->create_notification($user1->id, $user2->id, $now + (9 * DAYSECS), $timeread);
344 $this->create_notification($user2->id, $user1->id, $now + (8 * DAYSECS));
345 $this->create_notification($user1->id, $user2->id, $now + (7 * DAYSECS));
346
347 // Send notifications from user 3 to user 1.
348 $this->create_notification($user3->id, $user1->id, $now + (6 * DAYSECS), $timeread);
349 $this->create_notification($user1->id, $user3->id, $now + (5 * DAYSECS));
350 $this->create_notification($user3->id, $user1->id, $now + (4 * DAYSECS));
351
352 // Send notifications from user 3 to user 2 - should not be part of the export.
353 $this->create_notification($user3->id, $user2->id, $now + (3 * DAYSECS), $timeread);
354 $this->create_notification($user2->id, $user3->id, $now + (2 * DAYSECS));
355 $this->create_notification($user3->id, $user2->id, $now + (1 * DAYSECS));
356
357 $this->export_context_data_for_user($user1->id, \context_system::instance(), 'core_message');
358
359 $writer = writer::with_context(\context_system::instance());
360
361 $this->assertTrue($writer->has_any_data());
362
363 // Confirm the notifications.
364 $notifications = (array) $writer->get_data([get_string('notifications', 'core_message')]);
365
366 $this->assertCount(6, $notifications);
367 }
368
369 /**
370 * Test for provider::delete_data_for_all_users_in_context().
371 */
372 public function test_delete_data_for_all_users_in_context() {
373 global $DB;
374
375 $this->resetAfterTest();
376
377 // Create users to test with.
378 $user1 = $this->getDataGenerator()->create_user();
379 $user2 = $this->getDataGenerator()->create_user();
380
381 $now = time();
382 $timeread = $now - DAYSECS;
383
384 $systemcontext = \context_system::instance();
385
386 // Create contacts.
387 message_add_contact($user1->id, 0, $user2->id);
388 message_add_contact($user2->id, 0, $user1->id);
389
390 // Create messages.
391 $m1 = $this->create_message($user1->id, $user2->id, $now + (9 * DAYSECS), true);
392 $m2 = $this->create_message($user2->id, $user1->id, $now + (8 * DAYSECS));
393
394 // Create notifications.
395 $n1 = $this->create_notification($user1->id, $user2->id, $now + (9 * DAYSECS), $timeread);
396 $n2 = $this->create_notification($user2->id, $user1->id, $now + (8 * DAYSECS));
397
398 // Delete one of the messages.
399 \core_message\api::delete_message($user1->id, $m2);
400
401 // There should be 2 contacts.
402 $this->assertEquals(2, $DB->count_records('message_contacts'));
403
404 // There should be two messages.
405 $this->assertEquals(2, $DB->count_records('messages'));
406
407 // There should be two user actions - one for reading the message, one for deleting.
408 $this->assertEquals(2, $DB->count_records('message_user_actions'));
409
410 // There should be two conversation members.
411 $this->assertEquals(2, $DB->count_records('message_conversation_members'));
412
413 // There should be two notifications.
414 $this->assertEquals(2, $DB->count_records('notifications'));
415
416 provider::delete_data_for_all_users_in_context($systemcontext);
417
418 // Confirm all has been deleted.
419 $this->assertEquals(0, $DB->count_records('message_contacts'));
420 $this->assertEquals(0, $DB->count_records('messages'));
421 $this->assertEquals(0, $DB->count_records('message_user_actions'));
422 $this->assertEquals(0, $DB->count_records('message_conversation_members'));
423 $this->assertEquals(0, $DB->count_records('notifications'));
424 }
425
426 /**
427 * Test for provider::delete_data_for_user().
428 */
429 public function test_delete_data_for_user() {
430 global $DB;
431
432 $this->resetAfterTest();
433
434 // Create users to test with.
435 $user1 = $this->getDataGenerator()->create_user();
436 $user2 = $this->getDataGenerator()->create_user();
437 $user3 = $this->getDataGenerator()->create_user();
438
439 $now = time();
440 $timeread = $now - DAYSECS;
441
442 // Create contacts.
443 message_add_contact($user1->id, 0, $user2->id);
444 message_add_contact($user2->id, 0, $user1->id);
445 message_add_contact($user2->id, 0, $user3->id);
446
447 // Create messages.
448 $m1 = $this->create_message($user1->id, $user2->id, $now + (9 * DAYSECS), $timeread);
449 $m2 = $this->create_message($user2->id, $user1->id, $now + (8 * DAYSECS));
450
451 // Create notifications.
452 $n1 = $this->create_notification($user1->id, $user2->id, $now + (9 * DAYSECS), $timeread);
453 $n2 = $this->create_notification($user2->id, $user1->id, $now + (8 * DAYSECS));
454 $n2 = $this->create_notification($user2->id, $user3->id, $now + (8 * DAYSECS));
455
456 // Delete one of the messages.
457 \core_message\api::delete_message($user1->id, $m2);
458
459 // There should be 3 contacts.
460 $this->assertEquals(3, $DB->count_records('message_contacts'));
461
462 // There should be two messages.
463 $this->assertEquals(2, $DB->count_records('messages'));
464
465 // There should be two user actions - one for reading the message, one for deleting.
466 $this->assertEquals(2, $DB->count_records('message_user_actions'));
467
468 // There should be two conversation members.
469 $this->assertEquals(2, $DB->count_records('message_conversation_members'));
470
471 // There should be three notifications.
472 $this->assertEquals(3, $DB->count_records('notifications'));
473
474 $systemcontext = \context_system::instance();
475 $contextlist = new \core_privacy\local\request\approved_contextlist($user1, 'core_message',
476 [$systemcontext->id]);
477 provider::delete_data_for_user($contextlist);
478
479 // Confirm the user 2 data still exists.
480 $contacts = $DB->get_records('message_contacts');
481 $messages = $DB->get_records('messages');
482 $muas = $DB->get_records('message_user_actions');
483 $mcms = $DB->get_records('message_conversation_members');
484 $notifications = $DB->get_records('notifications');
485
486 $this->assertCount(1, $contacts);
487 $contact = reset($contacts);
488 $this->assertEquals($user3->id, $contact->userid);
489 $this->assertEquals($user2->id, $contact->contactid);
490
491 $this->assertCount(1, $messages);
492 $message = reset($messages);
493 $this->assertEquals($m2, $message->id);
494
495 $this->assertCount(1, $muas);
496 $mua = reset($muas);
497 $this->assertEquals($user2->id, $mua->userid);
498 $this->assertEquals($m1, $mua->messageid);
499 $this->assertEquals(\core_message\api::MESSAGE_ACTION_READ, $mua->action);
500
501 $this->assertCount(1, $mcms);
502 $mcm = reset($mcms);
503 $this->assertEquals($user2->id, $mcm->userid);
504
505 $this->assertCount(1, $notifications);
506 $notification = reset($notifications);
507 $this->assertEquals($user2->id, $notification->useridfrom);
508 $this->assertEquals($user3->id, $notification->useridto);
509 }
510
511 /**
512 * Creates a message to be used for testing.
513 *
514 * @param int $useridfrom The user id from
515 * @param int $useridto The user id to
516 * @param int $timecreated
517 * @param bool $read Do we want to mark the message as read?
518 * @return int The id of the message
519 * @throws dml_exception
520 */
521 private function create_message(int $useridfrom, int $useridto, int $timecreated = null, bool $read = false) {
522 global $DB;
523
524 static $i = 1;
525
526 if (is_null($timecreated)) {
527 $timecreated = time();
528 }
529
530 if (!$conversationid = \core_message\api::get_conversation_between_users([$useridfrom, $useridto])) {
531 $conversationid = \core_message\api::create_conversation_between_users([$useridfrom,
532 $useridto]);
533 }
534
535 // Ok, send the message.
536 $record = new stdClass();
537 $record->useridfrom = $useridfrom;
538 $record->conversationid = $conversationid;
539 $record->subject = 'No subject';
540 $record->fullmessage = 'A rad message ' . $i;
541 $record->smallmessage = 'A rad message ' . $i;
542 $record->timecreated = $timecreated;
543
544 $i++;
545
546 $record->id = $DB->insert_record('messages', $record);
547
548 if ($read) {
549 \core_message\api::mark_message_as_read($useridto, $record);
550 }
551
552 return $record->id;
553 }
554
555 /**
556 * Creates a notification to be used for testing.
557 *
558 * @param int $useridfrom The user id from
559 * @param int $useridto The user id to
560 * @param int|null $timecreated The time the notification was created
561 * @param int|null $timeread The time the notification was read, null if it hasn't been.
562 * @return int The id of the notification
563 * @throws dml_exception
564 */
565 private function create_notification(int $useridfrom, int $useridto, int $timecreated = null, int $timeread = null) {
566 global $DB;
567
568 static $i = 1;
569
570 if (is_null($timecreated)) {
571 $timecreated = time();
572 }
573
574 $record = new stdClass();
575 $record->useridfrom = $useridfrom;
576 $record->useridto = $useridto;
577 $record->subject = 'No subject';
578 $record->fullmessage = 'Some rad notification ' . $i;
579 $record->smallmessage = 'Yo homie, you got some stuff to do, yolo. ' . $i;
580 $record->timeread = $timeread;
581 $record->timecreated = $timecreated;
582
583 $i++;
584
585 return $DB->insert_record('notifications', $record);
586 }
587}