MDL-62781 question/privacy: fix tests with CodeRunner is installed
[moodle.git] / message / tests / privacy_provider_test.php
blob39e549fdf9ac4871d57c7d2bfbc11ddccce24b2d
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.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17 /**
18 * Privacy provider tests.
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
25 use core_privacy\local\metadata\collection;
26 use core_message\privacy\provider;
27 use \core_privacy\local\request\writer;
28 use \core_privacy\local\request\transform;
30 defined('MOODLE_INTERNAL') || die();
32 /**
33 * Privacy provider tests class.
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
39 class core_message_privacy_provider_testcase extends \core_privacy\tests\provider_testcase {
41 /**
42 * Test for provider::get_metadata().
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);
50 $messagestable = array_shift($itemcollection);
51 $this->assertEquals('messages', $messagestable->get_name());
53 $messageuseractionstable = array_shift($itemcollection);
54 $this->assertEquals('message_user_actions', $messageuseractionstable->get_name());
56 $messageconversationmemberstable = array_shift($itemcollection);
57 $this->assertEquals('message_conversation_members', $messageconversationmemberstable->get_name());
59 $messagecontacts = array_shift($itemcollection);
60 $this->assertEquals('message_contacts', $messagecontacts->get_name());
62 $notificationstable = array_shift($itemcollection);
63 $this->assertEquals('notifications', $notificationstable->get_name());
65 $usersettings = array_shift($itemcollection);
66 $this->assertEquals('core_message_messageprovider_settings', $usersettings->get_name());
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());
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());
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());
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());
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());
116 * Test for provider::export_user_preferences().
118 public function test_export_user_preferences_no_pref() {
119 $this->resetAfterTest();
121 $user = $this->getDataGenerator()->create_user();
122 provider::export_user_preferences($user->id);
124 $writer = writer::with_context(\context_system::instance());
126 $this->assertFalse($writer->has_any_data());
130 * Test for provider::export_user_preferences().
132 public function test_export_user_preferences() {
133 global $USER;
135 $this->resetAfterTest();
137 $this->setAdminUser();
139 // Create another user to set a preference for who we won't be exporting.
140 $user = $this->getDataGenerator()->create_user();
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);
148 // Set an unrelated preference.
149 set_user_preference('block_myoverview_last_tab', 'courses', $USER->id);
151 provider::export_user_preferences($USER->id);
153 $writer = writer::with_context(\context_system::instance());
155 $this->assertTrue($writer->has_any_data());
157 $prefs = (array) $writer->get_user_preferences('core_message');
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);
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);
177 * Test for provider::get_contexts_for_userid().
179 public function test_get_contexts_for_userid() {
180 $this->resetAfterTest();
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);
190 * Test for provider::export_user_data().
192 public function test_export_for_context_with_contacts() {
193 $this->resetAfterTest();
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();
201 // This user will not be added as a contact.
202 $this->getDataGenerator()->create_user();
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);
208 $this->export_context_data_for_user($user1->id, \context_system::instance(), 'core_message');
210 $writer = writer::with_context(\context_system::instance());
212 $contacts = (array) $writer->get_data([get_string('contacts', 'core_message')]);
213 usort($contacts, ['static', 'sort_contacts']);
215 $this->assertCount(3, $contacts);
217 $contact1 = array_shift($contacts);
218 $this->assertEquals($user2->id, $contact1->contact);
219 $this->assertEquals(get_string('no'), $contact1->blocked);
221 $contact2 = array_shift($contacts);
222 $this->assertEquals($user3->id, $contact2->contact);
223 $this->assertEquals(get_string('no'), $contact2->blocked);
225 $contact3 = array_shift($contacts);
226 $this->assertEquals($user4->id, $contact3->contact);
227 $this->assertEquals(get_string('yes'), $contact3->blocked);
231 * Test for provider::export_user_data().
233 public function test_export_for_context_with_messages() {
234 global $DB;
236 $this->resetAfterTest();
238 // Create users to test with.
239 $user1 = $this->getDataGenerator()->create_user();
240 $user2 = $this->getDataGenerator()->create_user();
241 $user3 = $this->getDataGenerator()->create_user();
243 $now = time();
245 // Send messages from user 1 to user 2.
246 $m1 = $this->create_message($user1->id, $user2->id, $now - (9 * DAYSECS), true);
247 $m2 = $this->create_message($user2->id, $user1->id, $now - (8 * DAYSECS));
248 $m3 = $this->create_message($user1->id, $user2->id, $now - (7 * DAYSECS));
250 // Send messages from user 3 to user 1.
251 $m4 = $this->create_message($user3->id, $user1->id, $now - (6 * DAYSECS), true);
252 $m5 = $this->create_message($user1->id, $user3->id, $now - (5 * DAYSECS));
253 $m6 = $this->create_message($user3->id, $user1->id, $now - (4 * DAYSECS));
255 // Send messages from user 3 to user 2 - these should not be included in the export.
256 $m7 = $this->create_message($user3->id, $user2->id, $now - (3 * DAYSECS), true);
257 $m8 = $this->create_message($user2->id, $user3->id, $now - (2 * DAYSECS));
258 $m9 = $this->create_message($user3->id, $user2->id, $now - (1 * DAYSECS));
260 // Mark message 2 and 5 as deleted.
261 \core_message\api::delete_message($user1->id, $m2);
262 \core_message\api::delete_message($user1->id, $m5);
264 $this->export_context_data_for_user($user1->id, \context_system::instance(), 'core_message');
266 $writer = writer::with_context(\context_system::instance());
268 $this->assertTrue($writer->has_any_data());
270 // Confirm the messages with user 2 are correct.
271 $messages = (array) $writer->get_data([get_string('messages', 'core_message'), fullname($user2)]);
272 $this->assertCount(3, $messages);
274 $dbm1 = $DB->get_record('messages', ['id' => $m1]);
275 $dbm2 = $DB->get_record('messages', ['id' => $m2]);
276 $dbm3 = $DB->get_record('messages', ['id' => $m3]);
278 usort($messages, ['static', 'sort_messages']);
279 $m1 = array_shift($messages);
280 $m2 = array_shift($messages);
281 $m3 = array_shift($messages);
283 $this->assertEquals(get_string('yes'), $m1->sender);
284 $this->assertEquals(message_format_message_text($dbm1), $m1->message);
285 $this->assertEquals(transform::datetime($now - (9 * DAYSECS)), $m1->timecreated);
286 $this->assertNotEquals('-', $m1->timeread);
287 $this->assertArrayNotHasKey('timedeleted', (array) $m1);
289 $this->assertEquals(get_string('no'), $m2->sender);
290 $this->assertEquals(message_format_message_text($dbm2), $m2->message);
291 $this->assertEquals(transform::datetime($now - (8 * DAYSECS)), $m2->timecreated);
292 $this->assertEquals('-', $m2->timeread);
293 $this->assertArrayHasKey('timedeleted', (array) $m2);
295 $this->assertEquals(get_string('yes'), $m3->sender);
296 $this->assertEquals(message_format_message_text($dbm3), $m3->message);
297 $this->assertEquals(transform::datetime($now - (7 * DAYSECS)), $m3->timecreated);
298 $this->assertEquals('-', $m3->timeread);
300 // Confirm the messages with user 3 are correct.
301 $messages = (array) $writer->get_data([get_string('messages', 'core_message'), fullname($user3)]);
302 $this->assertCount(3, $messages);
304 $dbm4 = $DB->get_record('messages', ['id' => $m4]);
305 $dbm5 = $DB->get_record('messages', ['id' => $m5]);
306 $dbm6 = $DB->get_record('messages', ['id' => $m6]);
308 usort($messages, ['static', 'sort_messages']);
309 $m4 = array_shift($messages);
310 $m5 = array_shift($messages);
311 $m6 = array_shift($messages);
313 $this->assertEquals(get_string('no'), $m4->sender);
314 $this->assertEquals(message_format_message_text($dbm4), $m4->message);
315 $this->assertEquals(transform::datetime($now - (6 * DAYSECS)), $m4->timecreated);
316 $this->assertNotEquals('-', $m4->timeread);
317 $this->assertArrayNotHasKey('timedeleted', (array) $m4);
319 $this->assertEquals(get_string('yes'), $m5->sender);
320 $this->assertEquals(message_format_message_text($dbm5), $m5->message);
321 $this->assertEquals(transform::datetime($now - (5 * DAYSECS)), $m5->timecreated);
322 $this->assertEquals('-', $m5->timeread);
323 $this->assertArrayHasKey('timedeleted', (array) $m5);
325 $this->assertEquals(get_string('no'), $m6->sender);
326 $this->assertEquals(message_format_message_text($dbm6), $m6->message);
327 $this->assertEquals(transform::datetime($now - (4 * DAYSECS)), $m6->timecreated);
328 $this->assertEquals('-', $m6->timeread);
332 * Test for provider::export_user_data().
334 public function test_export_for_context_with_notifications() {
335 $this->resetAfterTest();
337 // Create users to test with.
338 $user1 = $this->getDataGenerator()->create_user();
339 $user2 = $this->getDataGenerator()->create_user();
340 $user3 = $this->getDataGenerator()->create_user();
342 $now = time();
343 $timeread = $now - DAYSECS;
345 // Send notifications from user 1 to user 2.
346 $this->create_notification($user1->id, $user2->id, $now + (9 * DAYSECS), $timeread);
347 $this->create_notification($user2->id, $user1->id, $now + (8 * DAYSECS));
348 $this->create_notification($user1->id, $user2->id, $now + (7 * DAYSECS));
350 // Send notifications from user 3 to user 1.
351 $this->create_notification($user3->id, $user1->id, $now + (6 * DAYSECS), $timeread);
352 $this->create_notification($user1->id, $user3->id, $now + (5 * DAYSECS));
353 $this->create_notification($user3->id, $user1->id, $now + (4 * DAYSECS));
355 // Send notifications from user 3 to user 2 - should not be part of the export.
356 $this->create_notification($user3->id, $user2->id, $now + (3 * DAYSECS), $timeread);
357 $this->create_notification($user2->id, $user3->id, $now + (2 * DAYSECS));
358 $this->create_notification($user3->id, $user2->id, $now + (1 * DAYSECS));
360 $this->export_context_data_for_user($user1->id, \context_system::instance(), 'core_message');
362 $writer = writer::with_context(\context_system::instance());
364 $this->assertTrue($writer->has_any_data());
366 // Confirm the notifications.
367 $notifications = (array) $writer->get_data([get_string('notifications', 'core_message')]);
369 $this->assertCount(6, $notifications);
373 * Test for provider::delete_data_for_all_users_in_context().
375 public function test_delete_data_for_all_users_in_context() {
376 global $DB;
378 $this->resetAfterTest();
380 // Create users to test with.
381 $user1 = $this->getDataGenerator()->create_user();
382 $user2 = $this->getDataGenerator()->create_user();
384 $now = time();
385 $timeread = $now - DAYSECS;
387 $systemcontext = \context_system::instance();
389 // Create contacts.
390 message_add_contact($user1->id, 0, $user2->id);
391 message_add_contact($user2->id, 0, $user1->id);
393 // Create messages.
394 $m1 = $this->create_message($user1->id, $user2->id, $now + (9 * DAYSECS), true);
395 $m2 = $this->create_message($user2->id, $user1->id, $now + (8 * DAYSECS));
397 // Create notifications.
398 $n1 = $this->create_notification($user1->id, $user2->id, $now + (9 * DAYSECS), $timeread);
399 $n2 = $this->create_notification($user2->id, $user1->id, $now + (8 * DAYSECS));
401 // Delete one of the messages.
402 \core_message\api::delete_message($user1->id, $m2);
404 // There should be 2 contacts.
405 $this->assertEquals(2, $DB->count_records('message_contacts'));
407 // There should be two messages.
408 $this->assertEquals(2, $DB->count_records('messages'));
410 // There should be two user actions - one for reading the message, one for deleting.
411 $this->assertEquals(2, $DB->count_records('message_user_actions'));
413 // There should be two conversation members.
414 $this->assertEquals(2, $DB->count_records('message_conversation_members'));
416 // There should be two notifications.
417 $this->assertEquals(2, $DB->count_records('notifications'));
419 provider::delete_data_for_all_users_in_context($systemcontext);
421 // Confirm all has been deleted.
422 $this->assertEquals(0, $DB->count_records('message_contacts'));
423 $this->assertEquals(0, $DB->count_records('messages'));
424 $this->assertEquals(0, $DB->count_records('message_user_actions'));
425 $this->assertEquals(0, $DB->count_records('message_conversation_members'));
426 $this->assertEquals(0, $DB->count_records('notifications'));
430 * Test for provider::delete_data_for_user().
432 public function test_delete_data_for_user() {
433 global $DB;
435 $this->resetAfterTest();
437 // Create users to test with.
438 $user1 = $this->getDataGenerator()->create_user();
439 $user2 = $this->getDataGenerator()->create_user();
440 $user3 = $this->getDataGenerator()->create_user();
442 $now = time();
443 $timeread = $now - DAYSECS;
445 // Create contacts.
446 message_add_contact($user1->id, 0, $user2->id);
447 message_add_contact($user2->id, 0, $user1->id);
448 message_add_contact($user2->id, 0, $user3->id);
450 // Create messages.
451 $m1 = $this->create_message($user1->id, $user2->id, $now + (9 * DAYSECS), $timeread);
452 $m2 = $this->create_message($user2->id, $user1->id, $now + (8 * DAYSECS));
454 // Create notifications.
455 $n1 = $this->create_notification($user1->id, $user2->id, $now + (9 * DAYSECS), $timeread);
456 $n2 = $this->create_notification($user2->id, $user1->id, $now + (8 * DAYSECS));
457 $n2 = $this->create_notification($user2->id, $user3->id, $now + (8 * DAYSECS));
459 // Delete one of the messages.
460 \core_message\api::delete_message($user1->id, $m2);
462 // There should be 3 contacts.
463 $this->assertEquals(3, $DB->count_records('message_contacts'));
465 // There should be two messages.
466 $this->assertEquals(2, $DB->count_records('messages'));
468 // There should be two user actions - one for reading the message, one for deleting.
469 $this->assertEquals(2, $DB->count_records('message_user_actions'));
471 // There should be two conversation members.
472 $this->assertEquals(2, $DB->count_records('message_conversation_members'));
474 // There should be three notifications.
475 $this->assertEquals(3, $DB->count_records('notifications'));
477 $systemcontext = \context_system::instance();
478 $contextlist = new \core_privacy\local\request\approved_contextlist($user1, 'core_message',
479 [$systemcontext->id]);
480 provider::delete_data_for_user($contextlist);
482 // Confirm the user 2 data still exists.
483 $contacts = $DB->get_records('message_contacts');
484 $messages = $DB->get_records('messages');
485 $muas = $DB->get_records('message_user_actions');
486 $mcms = $DB->get_records('message_conversation_members');
487 $notifications = $DB->get_records('notifications');
489 $this->assertCount(1, $contacts);
490 $contact = reset($contacts);
491 $this->assertEquals($user3->id, $contact->userid);
492 $this->assertEquals($user2->id, $contact->contactid);
494 $this->assertCount(1, $messages);
495 $message = reset($messages);
496 $this->assertEquals($m2, $message->id);
498 $this->assertCount(1, $muas);
499 $mua = reset($muas);
500 $this->assertEquals($user2->id, $mua->userid);
501 $this->assertEquals($m1, $mua->messageid);
502 $this->assertEquals(\core_message\api::MESSAGE_ACTION_READ, $mua->action);
504 $this->assertCount(1, $mcms);
505 $mcm = reset($mcms);
506 $this->assertEquals($user2->id, $mcm->userid);
508 $this->assertCount(1, $notifications);
509 $notification = reset($notifications);
510 $this->assertEquals($user2->id, $notification->useridfrom);
511 $this->assertEquals($user3->id, $notification->useridto);
515 * Creates a message to be used for testing.
517 * @param int $useridfrom The user id from
518 * @param int $useridto The user id to
519 * @param int $timecreated
520 * @param bool $read Do we want to mark the message as read?
521 * @return int The id of the message
522 * @throws dml_exception
524 private function create_message(int $useridfrom, int $useridto, int $timecreated = null, bool $read = false) {
525 global $DB;
527 static $i = 1;
529 if (is_null($timecreated)) {
530 $timecreated = time();
533 if (!$conversationid = \core_message\api::get_conversation_between_users([$useridfrom, $useridto])) {
534 $conversationid = \core_message\api::create_conversation_between_users([$useridfrom,
535 $useridto]);
538 // Ok, send the message.
539 $record = new stdClass();
540 $record->useridfrom = $useridfrom;
541 $record->conversationid = $conversationid;
542 $record->subject = 'No subject';
543 $record->fullmessage = 'A rad message ' . $i;
544 $record->smallmessage = 'A rad message ' . $i;
545 $record->timecreated = $timecreated;
547 $i++;
549 $record->id = $DB->insert_record('messages', $record);
551 if ($read) {
552 \core_message\api::mark_message_as_read($useridto, $record);
555 return $record->id;
559 * Creates a notification to be used for testing.
561 * @param int $useridfrom The user id from
562 * @param int $useridto The user id to
563 * @param int|null $timecreated The time the notification was created
564 * @param int|null $timeread The time the notification was read, null if it hasn't been.
565 * @return int The id of the notification
566 * @throws dml_exception
568 private function create_notification(int $useridfrom, int $useridto, int $timecreated = null, int $timeread = null) {
569 global $DB;
571 static $i = 1;
573 if (is_null($timecreated)) {
574 $timecreated = time();
577 $record = new stdClass();
578 $record->useridfrom = $useridfrom;
579 $record->useridto = $useridto;
580 $record->subject = 'No subject';
581 $record->fullmessage = 'Some rad notification ' . $i;
582 $record->smallmessage = 'Yo homie, you got some stuff to do, yolo. ' . $i;
583 $record->timeread = $timeread;
584 $record->timecreated = $timecreated;
586 $i++;
588 return $DB->insert_record('notifications', $record);
592 * Comparison function for sorting messages.
594 * @param \stdClass $a
595 * @param \stdClass $b
596 * @return bool
598 protected static function sort_messages($a, $b) {
599 return $a->message > $b->message;
603 * Comparison function for sorting contacts.
605 * @param \stdClass $a
606 * @param \stdClass $b
607 * @return bool
609 protected static function sort_contacts($a, $b) {
610 return $a->contact > $b->contact;