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