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 * Unit tests for the repository_onedrive implementation of the privacy API. 18 * 19 * @package repository_onedrive 20 * @category test 21 * @copyright 2018 Zig Tan <zig@moodle.com> 22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 23 */ 24 defined('MOODLE_INTERNAL') || die(); 25 use \core_privacy\local\metadata\collection; 26 use \core_privacy\local\request\writer; 27 use \core_privacy\local\request\approved_contextlist; 28 use \core_privacy\local\request\approved_userlist; 29 use \repository_onedrive\privacy\provider; 30 /** 31 * Unit tests for the repository_onedrive implementation of the privacy API. 32 * 33 * @copyright 2018 Zig Tan <zig@moodle.com> 34 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 35 */ 36 class repository_onedrive_privacy_testcase extends \core_privacy\tests\provider_testcase { 37 38 /** 39 * Overriding setUp() function to always reset after tests. 40 */ 41 public function setUp(): void { 42 $this->resetAfterTest(true); 43 } 44 45 /** 46 * Test for provider::get_contexts_for_userid(). 47 */ 48 public function test_get_contexts_for_userid() { 49 global $DB; 50 51 // Test setup. 52 $user = $this->getDataGenerator()->create_user(); 53 $this->setUser($user); 54 55 // Add two repository_onedrive_access records for the User. 56 $access = (object)[ 57 'usermodified' => $user->id, 58 'itemid' => 'Some onedrive access item data', 59 'permissionid' => 'Some onedrive access permission data', 60 'timecreated' => date('u'), 61 'timemodified' => date('u'), 62 ]; 63 $DB->insert_record('repository_onedrive_access', $access); 64 $access = (object)[ 65 'usermodified' => $user->id, 66 'itemid' => 'Another onedrive access item data', 67 'permissionid' => 'Another onedrive access permission data', 68 'timecreated' => date('u'), 69 'timemodified' => date('u'), 70 ]; 71 $DB->insert_record('repository_onedrive_access', $access); 72 73 // Test there are two repository_onedrive_access records for the User. 74 $access = $DB->get_records('repository_onedrive_access', ['usermodified' => $user->id]); 75 $this->assertCount(2, $access); 76 77 // Test the User's retrieved contextlist contains only one context. 78 $contextlist = provider::get_contexts_for_userid($user->id); 79 $contexts = $contextlist->get_contexts(); 80 $this->assertCount(1, $contexts); 81 82 // Test the User's contexts equal the User's own context. 83 $context = reset($contexts); 84 $this->assertEquals(CONTEXT_USER, $context->contextlevel); 85 $this->assertEquals($user->id, $context->instanceid); 86 } 87 88 /** 89 * Test for provider::test_get_users_in_context(). 90 */ 91 public function test_get_users_in_context() { 92 global $DB; 93 $component = 'repository_onedrive'; 94 95 // Test setup. 96 $user1 = $this->getDataGenerator()->create_user(); 97 $user2 = $this->getDataGenerator()->create_user(); 98 $u1id = $user1->id; 99 $u2id = $user2->id; 100 101 // Add a repository_onedrive_access records for each user. 102 $this->setUser($user1); 103 $access = (object)[ 104 'usermodified' => $u1id, 105 'itemid' => 'Some onedrive access item data', 106 'permissionid' => 'Some onedrive access permission data', 107 'timecreated' => date('u'), 108 'timemodified' => date('u'), 109 ]; 110 $DB->insert_record('repository_onedrive_access', $access); 111 112 $this->setUser($user2); 113 $access = (object)[ 114 'usermodified' => $u2id, 115 'itemid' => 'Another onedrive access item data', 116 'permissionid' => 'Another onedrive access permission data', 117 'timecreated' => date('u'), 118 'timemodified' => date('u'), 119 ]; 120 $DB->insert_record('repository_onedrive_access', $access); 121 122 // Fetch the context of each user's access record. 123 $contextlist = provider::get_contexts_for_userid($u1id); 124 $u1contexts = $contextlist->get_contexts(); 125 $this->assertCount(1, $u1contexts); 126 127 $contextlist = provider::get_contexts_for_userid($u2id); 128 $u2contexts = $contextlist->get_contexts(); 129 $this->assertCount(1, $u2contexts); 130 131 $contexts = [ 132 $u1id => $u1contexts[0], 133 $u2id => $u2contexts[0], 134 ]; 135 136 // Test context 1 only contains user 1. 137 $userlist = new \core_privacy\local\request\userlist($contexts[$u1id], $component); 138 provider::get_users_in_context($userlist); 139 140 $this->assertCount(1, $userlist); 141 $actual = $userlist->get_userids(); 142 $this->assertEquals([$u1id], $actual); 143 144 // Test context 2 only contains user 2. 145 $userlist = new \core_privacy\local\request\userlist($contexts[$u2id], $component); 146 provider::get_users_in_context($userlist); 147 148 $this->assertCount(1, $userlist); 149 $actual = $userlist->get_userids(); 150 $this->assertEquals([$u2id], $actual); 151 152 // Test the contexts match the users' contexts. 153 $this->assertEquals($u1id, $contexts[$u1id]->instanceid); 154 $this->assertEquals($u2id, $contexts[$u2id]->instanceid); 155 } 156 157 /** 158 * Test for provider::export_user_data(). 159 */ 160 public function test_export_user_data() { 161 global $DB; 162 163 // Test setup. 164 $user = $this->getDataGenerator()->create_user(); 165 $this->setUser($user); 166 167 // Add two repository_onedrive_access records for the User. 168 $access = (object)[ 169 'usermodified' => $user->id, 170 'itemid' => 'Some onedrive access item data', 171 'permissionid' => 'Some onedrive access permission data', 172 'timecreated' => date('u'), 173 'timemodified' => date('u'), 174 ]; 175 $DB->insert_record('repository_onedrive_access', $access); 176 $access = (object)[ 177 'usermodified' => $user->id, 178 'itemid' => 'Another onedrive access item data', 179 'permissionid' => 'Another onedrive access permission data', 180 'timecreated' => date('u'), 181 'timemodified' => date('u'), 182 ]; 183 $DB->insert_record('repository_onedrive_access', $access); 184 185 // Test there are two repository_onedrive_access records for the User. 186 $access = $DB->get_records('repository_onedrive_access', ['usermodified' => $user->id]); 187 $this->assertCount(2, $access); 188 189 // Test the User's retrieved contextlist contains only one context. 190 $contextlist = provider::get_contexts_for_userid($user->id); 191 $contexts = $contextlist->get_contexts(); 192 $this->assertCount(1, $contexts); 193 194 // Test the User's contexts equal the User's own context. 195 $context = reset($contexts); 196 $this->assertEquals(CONTEXT_USER, $context->contextlevel); 197 $this->assertEquals($user->id, $context->instanceid); 198 $approvedcontextlist = new approved_contextlist($user, 'repository_onedrive', $contextlist->get_contextids()); 199 200 // Retrieve repository_onedrive_access data only for this user. 201 provider::export_user_data($approvedcontextlist); 202 203 // Test the repository_onedrive_access data is exported at the User context level. 204 $user = $approvedcontextlist->get_user(); 205 $contextuser = context_user::instance($user->id); 206 $writer = writer::with_context($contextuser); 207 $this->assertTrue($writer->has_any_data()); 208 } 209 210 /** 211 * Test for provider::delete_data_for_all_users_in_context(). 212 */ 213 public function test_delete_data_for_all_users_in_context() { 214 global $DB; 215 216 // Test setup. 217 $user = $this->getDataGenerator()->create_user(); 218 $this->setUser($user); 219 220 // Add two repository_onedrive_access records for the User. 221 $access = (object)[ 222 'usermodified' => $user->id, 223 'itemid' => 'Some onedrive access item data', 224 'permissionid' => 'Some onedrive access permission data', 225 'timecreated' => date('u'), 226 'timemodified' => date('u'), 227 ]; 228 $DB->insert_record('repository_onedrive_access', $access); 229 $access = (object)[ 230 'usermodified' => $user->id, 231 'itemid' => 'Another onedrive access item data', 232 'permissionid' => 'Another onedrive access permission data', 233 'timecreated' => date('u'), 234 'timemodified' => date('u'), 235 ]; 236 $DB->insert_record('repository_onedrive_access', $access); 237 238 // Test there are two repository_onedrive_access records for the User. 239 $access = $DB->get_records('repository_onedrive_access', ['usermodified' => $user->id]); 240 $this->assertCount(2, $access); 241 242 // Test the User's retrieved contextlist contains only one context. 243 $contextlist = provider::get_contexts_for_userid($user->id); 244 $contexts = $contextlist->get_contexts(); 245 $this->assertCount(1, $contexts); 246 247 // Test the User's contexts equal the User's own context. 248 $context = reset($contexts); 249 $this->assertEquals(CONTEXT_USER, $context->contextlevel); 250 $this->assertEquals($user->id, $context->instanceid); 251 252 // Test delete all users content by context. 253 provider::delete_data_for_all_users_in_context($context); 254 $access = $DB->get_records('repository_onedrive_access', ['usermodified' => $user->id]); 255 $this->assertCount(0, $access); 256 } 257 258 /** 259 * Test for provider::delete_data_for_user(). 260 */ 261 public function test_delete_data_for_user() { 262 global $DB; 263 264 // Test setup. 265 $user1 = $this->getDataGenerator()->create_user(); 266 $user2 = $this->getDataGenerator()->create_user(); 267 $this->setUser($user1); 268 269 // Add 3 repository_onedrive_accesss records for User 1. 270 $noaccess = 3; 271 for ($a = 0; $a < $noaccess; $a++) { 272 $access = (object)[ 273 'usermodified' => $user1->id, 274 'itemid' => 'Some onedrive access item data - ' . $a, 275 'permissionid' => 'Some onedrive access permission data - ' . $a, 276 'timecreated' => date('u'), 277 'timemodified' => date('u'), 278 ]; 279 $DB->insert_record('repository_onedrive_access', $access); 280 } 281 // Add 1 repository_onedrive_accesss record for User 2. 282 $access = (object)[ 283 'usermodified' => $user2->id, 284 'itemid' => 'Some onedrive access item data', 285 'permissionid' => 'Some onedrive access permission data', 286 'timecreated' => date('u'), 287 'timemodified' => date('u'), 288 ]; 289 $DB->insert_record('repository_onedrive_access', $access); 290 291 // Test the created repository_onedrive records for User 1 equals test number of access specified. 292 $communities = $DB->get_records('repository_onedrive_access', ['usermodified' => $user1->id]); 293 $this->assertCount($noaccess, $communities); 294 295 // Test the created repository_onedrive records for User 2 equals 1. 296 $communities = $DB->get_records('repository_onedrive_access', ['usermodified' => $user2->id]); 297 $this->assertCount(1, $communities); 298 299 // Test the deletion of repository_onedrive_access records for User 1 results in zero records. 300 $contextlist = provider::get_contexts_for_userid($user1->id); 301 $contexts = $contextlist->get_contexts(); 302 $this->assertCount(1, $contexts); 303 304 // Test the User's contexts equal the User's own context. 305 $context = reset($contexts); 306 $this->assertEquals(CONTEXT_USER, $context->contextlevel); 307 $this->assertEquals($user1->id, $context->instanceid); 308 309 $approvedcontextlist = new approved_contextlist($user1, 'repository_onedrive', $contextlist->get_contextids()); 310 provider::delete_data_for_user($approvedcontextlist); 311 $access = $DB->get_records('repository_onedrive_access', ['usermodified' => $user1->id]); 312 $this->assertCount(0, $access); 313 314 // Test that User 2's single repository_onedrive_access record still exists. 315 $contextlist = provider::get_contexts_for_userid($user2->id); 316 $contexts = $contextlist->get_contexts(); 317 $this->assertCount(1, $contexts); 318 319 // Test the User's contexts equal the User's own context. 320 $context = reset($contexts); 321 $this->assertEquals(CONTEXT_USER, $context->contextlevel); 322 $this->assertEquals($user2->id, $context->instanceid); 323 $access = $DB->get_records('repository_onedrive_access', ['usermodified' => $user2->id]); 324 $this->assertCount(1, $access); 325 } 326 327 /** 328 * Test for provider::delete_data_for_users(). 329 */ 330 public function test_delete_data_for_users() { 331 global $DB; 332 $component = 'repository_onedrive'; 333 334 // Test setup. 335 $user1 = $this->getDataGenerator()->create_user(); 336 $user2 = $this->getDataGenerator()->create_user(); 337 $this->setUser($user1); 338 339 // Add 3 repository_onedrive_accesss records for User 1. 340 $noaccess = 3; 341 for ($a = 0; $a < $noaccess; $a++) { 342 $access = (object)[ 343 'usermodified' => $user1->id, 344 'itemid' => 'Some onedrive access item data for user 1 - ' . $a, 345 'permissionid' => 'Some onedrive access permission data - ' . $a, 346 'timecreated' => date('u'), 347 'timemodified' => date('u'), 348 ]; 349 $DB->insert_record('repository_onedrive_access', $access); 350 } 351 // Add 1 repository_onedrive_accesss record for User 2. 352 $access = (object)[ 353 'usermodified' => $user2->id, 354 'itemid' => 'Some onedrive access item data for user 2', 355 'permissionid' => 'Some onedrive access permission data', 356 'timecreated' => date('u'), 357 'timemodified' => date('u'), 358 ]; 359 $DB->insert_record('repository_onedrive_access', $access); 360 361 // Test the created repository_onedrive records for User 1 equals test number of access specified. 362 $communities = $DB->get_records('repository_onedrive_access', ['usermodified' => $user1->id]); 363 $this->assertCount($noaccess, $communities); 364 365 // Test the created repository_onedrive records for User 2 equals 1. 366 $communities = $DB->get_records('repository_onedrive_access', ['usermodified' => $user2->id]); 367 $this->assertCount(1, $communities); 368 369 // Fetch the context of each user's access record. 370 $contextlist = provider::get_contexts_for_userid($user1->id); 371 $u1contexts = $contextlist->get_contexts(); 372 373 // Test the deletion of context 1 results in deletion of user 1's records only. 374 $approveduserids = [$user1->id, $user2->id]; 375 $approvedlist = new approved_userlist($u1contexts[0], $component, $approveduserids); 376 provider::delete_data_for_users($approvedlist); 377 378 $access = $DB->get_records('repository_onedrive_access', ['usermodified' => $user1->id]); 379 $this->assertCount(0, $access); 380 381 // Ensure user 2's record still exists. 382 $access = $DB->get_records('repository_onedrive_access', ['usermodified' => $user2->id]); 383 $this->assertCount(1, $access); 384 } 385 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body