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