Differences Between: [Versions 39 and 310]
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 * Privacy test for the authentication mnet 18 * 19 * @package auth_mnet 20 * @category test 21 * @copyright 2018 Victor Deniz <victor@moodle.com> 22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 23 */ 24 25 defined('MOODLE_INTERNAL') || die(); 26 27 use \auth_mnet\privacy\provider; 28 use \core_privacy\local\request\approved_contextlist; 29 use \core_privacy\local\request\writer; 30 use \core_privacy\tests\provider_testcase; 31 use core_privacy\local\request\transform; 32 use core_privacy\local\request\approved_userlist; 33 34 /** 35 * Privacy test for the authentication mnet 36 * 37 * @package auth_mnet 38 * @category test 39 * @copyright 2018 Victor Deniz <victor@moodle.com> 40 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 41 */ 42 class auth_mnet_privacy_testcase extends provider_testcase { 43 /** 44 * Set up method. 45 */ 46 public function setUp(): void { 47 $this->resetAfterTest(); 48 $this->setAdminUser(); 49 } 50 51 /** 52 * Check that a user context is returned if there is any user data for this user. 53 */ 54 public function test_get_contexts_for_userid() { 55 global $DB; 56 57 $user = $this->getDataGenerator()->create_user(['auth' => 'mnet']); 58 $this->assertEmpty(provider::get_contexts_for_userid($user->id)); 59 60 // Insert mnet_log record. 61 $logrecord = new stdClass(); 62 $logrecord->hostid = ''; 63 $logrecord->remoteid = 65; 64 $logrecord->time = time(); 65 $logrecord->userid = $user->id; 66 67 $DB->insert_record('mnet_log', $logrecord); 68 69 $contextlist = provider::get_contexts_for_userid($user->id); 70 71 // Check that we only get back one context. 72 $this->assertCount(1, $contextlist); 73 74 // Check that a context is returned is the expected. 75 $usercontext = \context_user::instance($user->id); 76 $this->assertEquals($usercontext->id, $contextlist->get_contextids()[0]); 77 } 78 79 /** 80 * Test that user data is exported correctly. 81 */ 82 public function test_export_user_data() { 83 global $DB; 84 85 $user = $this->getDataGenerator()->create_user(['auth' => 'mnet']); 86 87 // Insert mnet_host record. 88 $hostrecord = new stdClass(); 89 $hostrecord->wwwroot = 'https://external.moodle.com'; 90 $hostrecord->name = 'External Moodle'; 91 $hostrecord->public_key = '-----BEGIN CERTIFICATE-----'; 92 93 $hostid = $DB->insert_record('mnet_host', $hostrecord); 94 95 // Insert mnet_log record. 96 $logrecord = new stdClass(); 97 $logrecord->hostid = $hostid; 98 $logrecord->remoteid = 65; 99 $logrecord->time = time(); 100 $logrecord->userid = $user->id; 101 $logrecord->course = 3; 102 $logrecord->coursename = 'test course'; 103 104 $DB->insert_record('mnet_log', $logrecord); 105 106 $usercontext = \context_user::instance($user->id); 107 108 $writer = writer::with_context($usercontext); 109 $this->assertFalse($writer->has_any_data()); 110 $approvedlist = new approved_contextlist($user, 'auth_mnet', [$usercontext->id]); 111 provider::export_user_data($approvedlist); 112 113 $data = $writer->get_data([get_string('pluginname', 'auth_mnet'), $hostrecord->name, $logrecord->coursename]); 114 115 $this->assertEquals($logrecord->remoteid, reset($data)->remoteid); 116 $this->assertEquals(transform::datetime($logrecord->time), reset($data)->time); 117 } 118 119 /** 120 * Test deleting all user data for a specific context. 121 */ 122 public function test_delete_data_for_all_users_in_context() { 123 global $DB; 124 125 $user1 = $this->getDataGenerator()->create_user(['auth' => 'mnet']); 126 127 // Insert mnet_log record. 128 $logrecord1 = new stdClass(); 129 $logrecord1->hostid = ''; 130 $logrecord1->remoteid = 65; 131 $logrecord1->time = time(); 132 $logrecord1->userid = $user1->id; 133 134 $DB->insert_record('mnet_log', $logrecord1); 135 136 $user1context = \context_user::instance($user1->id); 137 138 $user2 = $this->getDataGenerator()->create_user(['auth' => 'mnet']); 139 140 // Insert mnet_log record. 141 $logrecord2 = new stdClass(); 142 $logrecord2->hostid = ''; 143 $logrecord2->remoteid = 65; 144 $logrecord2->time = time(); 145 $logrecord2->userid = $user2->id; 146 147 $DB->insert_record('mnet_log', $logrecord2); 148 149 // Get all mnet log records. 150 $mnetlogrecords = $DB->get_records('mnet_log', array()); 151 // There should be two. 152 $this->assertCount(2, $mnetlogrecords); 153 154 // Delete everything for the first user context. 155 provider::delete_data_for_all_users_in_context($user1context); 156 157 // Get all user1 mnet log records. 158 $mnetlogrecords = $DB->get_records('mnet_log', ['userid' => $user1->id]); 159 $this->assertCount(0, $mnetlogrecords); 160 161 // Get all mnet log records. 162 $mnetlogrecords = $DB->get_records('mnet_log', array()); 163 // There should be one (user2). 164 $this->assertCount(1, $mnetlogrecords); 165 } 166 167 /** 168 * This should work identical to the above test. 169 */ 170 public function test_delete_data_for_user() { 171 global $DB; 172 173 $user1 = $this->getDataGenerator()->create_user(['auth' => 'mnet']); 174 175 // Insert mnet_log record. 176 $logrecord1 = new stdClass(); 177 $logrecord1->hostid = ''; 178 $logrecord1->remoteid = 65; 179 $logrecord1->time = time(); 180 $logrecord1->userid = $user1->id; 181 182 $DB->insert_record('mnet_log', $logrecord1); 183 184 $user1context = \context_user::instance($user1->id); 185 186 $user2 = $this->getDataGenerator()->create_user(['auth' => 'mnet']); 187 188 // Insert mnet_log record. 189 $logrecord2 = new stdClass(); 190 $logrecord2->hostid = ''; 191 $logrecord2->remoteid = 65; 192 $logrecord2->time = time(); 193 $logrecord2->userid = $user2->id; 194 195 $DB->insert_record('mnet_log', $logrecord2); 196 197 // Get all mnet log records. 198 $mnetlogrecords = $DB->get_records('mnet_log', array()); 199 // There should be two. 200 $this->assertCount(2, $mnetlogrecords); 201 202 // Delete everything for the first user. 203 $approvedlist = new approved_contextlist($user1, 'auth_mnet', [$user1context->id]); 204 provider::delete_data_for_user($approvedlist); 205 206 // Get all user1 mnet log records. 207 $mnetlogrecords = $DB->get_records('mnet_log', ['userid' => $user1->id]); 208 $this->assertCount(0, $mnetlogrecords); 209 210 // Get all mnet log records. 211 $mnetlogrecords = $DB->get_records('mnet_log', array()); 212 // There should be one (user2). 213 $this->assertCount(1, $mnetlogrecords); 214 } 215 216 /** 217 * Test that only users with a user context are fetched. 218 */ 219 public function test_get_users_in_context() { 220 global $DB; 221 222 $this->resetAfterTest(); 223 224 $component = 'auth_mnet'; 225 // Create a user. 226 $user = $this->getDataGenerator()->create_user(['auth' => 'mnet']); 227 $usercontext = context_user::instance($user->id); 228 229 // The list of users should not return anything yet (related data still haven't been created). 230 $userlist = new \core_privacy\local\request\userlist($usercontext, $component); 231 provider::get_users_in_context($userlist); 232 $this->assertCount(0, $userlist); 233 234 // Insert mnet_log record. 235 $logrecord = new stdClass(); 236 $logrecord->hostid = ''; 237 $logrecord->remoteid = 65; 238 $logrecord->time = time(); 239 $logrecord->userid = $user->id; 240 $DB->insert_record('mnet_log', $logrecord); 241 242 // The list of users for user context should return the user. 243 provider::get_users_in_context($userlist); 244 $this->assertCount(1, $userlist); 245 $expected = [$user->id]; 246 $actual = $userlist->get_userids(); 247 $this->assertEquals($expected, $actual); 248 249 // The list of users for system context should not return any users. 250 $systemcontext = context_system::instance(); 251 $userlist = new \core_privacy\local\request\userlist($systemcontext, $component); 252 provider::get_users_in_context($userlist); 253 $this->assertCount(0, $userlist); 254 } 255 256 /** 257 * Test that data for users in approved userlist is deleted. 258 */ 259 public function test_delete_data_for_users() { 260 global $DB; 261 262 $this->resetAfterTest(); 263 264 $component = 'auth_mnet'; 265 // Create user1. 266 $user1 = $this->getDataGenerator()->create_user(['auth' => 'mnet']); 267 $usercontext1 = context_user::instance($user1->id); 268 // Create user2. 269 $user2 = $this->getDataGenerator()->create_user(['auth' => 'mnet']); 270 $usercontext2 = context_user::instance($user2->id); 271 272 // Insert mnet_log record. 273 $logrecord1 = new stdClass(); 274 $logrecord1->hostid = ''; 275 $logrecord1->remoteid = 65; 276 $logrecord1->time = time(); 277 $logrecord1->userid = $user1->id; 278 $DB->insert_record('mnet_log', $logrecord1); 279 280 // Insert mnet_log record. 281 $logrecord2 = new stdClass(); 282 $logrecord2->hostid = ''; 283 $logrecord2->remoteid = 65; 284 $logrecord2->time = time(); 285 $logrecord2->userid = $user2->id; 286 $DB->insert_record('mnet_log', $logrecord2); 287 288 // The list of users for usercontext1 should return user1. 289 $userlist1 = new \core_privacy\local\request\userlist($usercontext1, $component); 290 provider::get_users_in_context($userlist1); 291 $this->assertCount(1, $userlist1); 292 $expected = [$user1->id]; 293 $actual = $userlist1->get_userids(); 294 $this->assertEquals($expected, $actual); 295 296 // The list of users for usercontext2 should return user2. 297 $userlist2 = new \core_privacy\local\request\userlist($usercontext2, $component); 298 provider::get_users_in_context($userlist2); 299 $this->assertCount(1, $userlist2); 300 $expected = [$user2->id]; 301 $actual = $userlist2->get_userids(); 302 $this->assertEquals($expected, $actual); 303 304 // Add userlist1 to the approved user list. 305 $approvedlist = new approved_userlist($usercontext1, $component, $userlist1->get_userids()); 306 307 // Delete user data using delete_data_for_user for usercontext1. 308 provider::delete_data_for_users($approvedlist); 309 310 // Re-fetch users in usercontext1 - The user list should now be empty. 311 $userlist1 = new \core_privacy\local\request\userlist($usercontext1, $component); 312 provider::get_users_in_context($userlist1); 313 $this->assertCount(0, $userlist1); 314 // Re-fetch users in usercontext2 - The user list should not be empty (user2). 315 $userlist2 = new \core_privacy\local\request\userlist($usercontext2, $component); 316 provider::get_users_in_context($userlist2); 317 $this->assertCount(1, $userlist2); 318 319 // User data should be only removed in the user context. 320 $systemcontext = context_system::instance(); 321 // Add userlist2 to the approved user list in the system context. 322 $approvedlist = new approved_userlist($systemcontext, $component, $userlist2->get_userids()); 323 // Delete user1 data using delete_data_for_user. 324 provider::delete_data_for_users($approvedlist); 325 // Re-fetch users in usercontext2 - The user list should not be empty (user2). 326 $userlist2 = new \core_privacy\local\request\userlist($usercontext2, $component); 327 provider::get_users_in_context($userlist2); 328 $this->assertCount(1, $userlist2); 329 } 330 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body