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 core_repository implementation of the privacy API. 18 * 19 * @package core_repository 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 25 defined('MOODLE_INTERNAL') || die(); 26 27 use \core_privacy\local\metadata\collection; 28 use \core_privacy\local\request\writer; 29 use \core_privacy\local\request\approved_contextlist; 30 use \core_repository\privacy\provider; 31 use \core_privacy\local\request\approved_userlist; 32 33 /** 34 * Unit tests for the core_repository implementation of the privacy API. 35 * 36 * @copyright 2018 Zig Tan <zig@moodle.com> 37 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 38 */ 39 class core_repository_privacy_testcase extends \core_privacy\tests\provider_testcase { 40 41 /** 42 * Overriding setUp() function to always reset after tests. 43 */ 44 public function setUp(): void { 45 $this->resetAfterTest(true); 46 } 47 48 /** 49 * Test for provider::get_contexts_for_userid(). 50 */ 51 public function test_get_contexts_for_userid() { 52 // Test setup. 53 $user = $this->getDataGenerator()->create_user(); 54 $this->setUser($user); 55 56 // Test the User's retrieved contextlist is empty because no repository_instances have added for the User yet. 57 $contextlist = provider::get_contexts_for_userid($user->id); 58 $contexts = $contextlist->get_contexts(); 59 $this->assertCount(0, $contexts); 60 61 // Create 3 repository_instances records for the User. 62 $this->setup_test_scenario_data($user->id, 3); 63 64 // Test the User's retrieved contextlist contains only one context. 65 $contextlist = provider::get_contexts_for_userid($user->id); 66 $contexts = $contextlist->get_contexts(); 67 $this->assertCount(1, $contexts); 68 69 // Test the User's contexts equal the User's own context. 70 $context = reset($contexts); 71 $this->assertEquals(CONTEXT_USER, $context->contextlevel); 72 $this->assertEquals($user->id, $context->instanceid); 73 } 74 75 /** 76 * Test for provider::export_user_data(). 77 */ 78 public function test_export_user_data() { 79 // Test setup. 80 $user = $this->getDataGenerator()->create_user(); 81 $this->setUser($user); 82 83 // Create 3 repository_instances records for the User. 84 $this->setup_test_scenario_data($user->id, 3); 85 86 // Test the User's retrieved contextlist contains only one context. 87 $contextlist = provider::get_contexts_for_userid($user->id); 88 $contexts = $contextlist->get_contexts(); 89 $this->assertCount(1, $contexts); 90 91 // Test the User's contexts equal the User's own context. 92 $context = reset($contexts); 93 $this->assertEquals(CONTEXT_USER, $context->contextlevel); 94 $this->assertEquals($user->id, $context->instanceid); 95 96 // Retrieve repository_instances data only for this user. 97 $approvedcontextlist = new approved_contextlist($user, 'core_repository', $contextlist->get_contextids()); 98 provider::export_user_data($approvedcontextlist); 99 100 // Test the repository_instances data is exported at the User context level. 101 $user = $approvedcontextlist->get_user(); 102 $contextuser = context_user::instance($user->id); 103 $writer = writer::with_context($contextuser); 104 $this->assertTrue($writer->has_any_data()); 105 } 106 107 /** 108 * Test for provider::delete_data_for_all_users_in_context(). 109 */ 110 public function test_delete_data_for_all_users_in_context() { 111 global $DB; 112 113 // Test setup. 114 $user = $this->getDataGenerator()->create_user(); 115 $this->setUser($user); 116 117 // Create 3 repository_instances records for the User. 118 $this->setup_test_scenario_data($user->id, 3); 119 120 // Test the User's retrieved contextlist contains only one context. 121 $contextlist = provider::get_contexts_for_userid($user->id); 122 $contexts = $contextlist->get_contexts(); 123 $this->assertCount(1, $contexts); 124 125 // Test the User's contexts equal the User's own context. 126 $context = reset($contexts); 127 $this->assertEquals(CONTEXT_USER, $context->contextlevel); 128 129 // Delete all the User's records in mdl_repository_instances table by the specified User context. 130 provider::delete_data_for_all_users_in_context($context); 131 132 // Test the cohort roles records in mdl_repository_instances table is equals zero. 133 $repositoryinstances = $DB->get_records('repository_instances', ['userid' => $user->id]); 134 $this->assertCount(0, $repositoryinstances); 135 } 136 137 /** 138 * Test for provider::delete_data_for_user(). 139 */ 140 public function test_delete_data_for_user() { 141 global $DB; 142 143 // Test setup. 144 $user = $this->getDataGenerator()->create_user(); 145 $this->setUser($user); 146 147 // Create 3 repository_instances records for the User. 148 $this->setup_test_scenario_data($user->id, 3); 149 150 // Test the User's retrieved contextlist contains only one context. 151 $contextlist = provider::get_contexts_for_userid($user->id); 152 $contexts = $contextlist->get_contexts(); 153 $this->assertCount(1, $contexts); 154 155 // Test the User's contexts equal the User's own context. 156 $context = reset($contexts); 157 $this->assertEquals(CONTEXT_USER, $context->contextlevel); 158 159 // Delete all the User's records in mdl_repository_instances table by the specified User approved context list. 160 $approvedcontextlist = new approved_contextlist($user, 'repository_instances', $contextlist->get_contextids()); 161 provider::delete_data_for_user($approvedcontextlist); 162 163 // Test the cohort roles records in mdl_repository_instances table is equals zero. 164 $repositoryinstances = $DB->get_records('repository_instances', ['userid' => $user->id]); 165 $this->assertCount(0, $repositoryinstances); 166 } 167 168 /** 169 * Test that only users with a user context are fetched. 170 */ 171 public function test_get_users_in_context() { 172 $this->resetAfterTest(); 173 174 $component = 'core_repository'; 175 // Create a user. 176 $user = $this->getDataGenerator()->create_user(); 177 $usercontext = \context_user::instance($user->id); 178 $userlist = new \core_privacy\local\request\userlist($usercontext, $component); 179 // The list of users should not return anything yet (related data still haven't been created). 180 provider::get_users_in_context($userlist); 181 $this->assertCount(0, $userlist); 182 183 // Create 3 repository_instances records for user. 184 $this->setup_test_scenario_data($user->id, 3); 185 186 // The list of users for user context should return the user. 187 provider::get_users_in_context($userlist); 188 $this->assertCount(1, $userlist); 189 $expected = [$user->id]; 190 $actual = $userlist->get_userids(); 191 $this->assertEquals($expected, $actual); 192 193 // The list of users for system context should not return any users. 194 $systemcontext = context_system::instance(); 195 $userlist = new \core_privacy\local\request\userlist($systemcontext, $component); 196 provider::get_users_in_context($userlist); 197 $this->assertCount(0, $userlist); 198 } 199 200 /** 201 * Test that data for users in approved userlist is deleted. 202 */ 203 public function test_delete_data_for_users() { 204 $this->resetAfterTest(); 205 206 $component = 'core_repository'; 207 // Create user1. 208 $user1 = $this->getDataGenerator()->create_user(); 209 $this->setUser($user1); 210 $usercontext1 = \context_user::instance($user1->id); 211 // Create list of users with a related user data in usercontext1. 212 $userlist1 = new \core_privacy\local\request\userlist($usercontext1, $component); 213 214 // Create a user2. 215 $user2 = $this->getDataGenerator()->create_user(); 216 $this->setUser($user2); 217 $usercontext2 = \context_user::instance($user2->id); 218 // Create list of users with a related user data in usercontext2. 219 $userlist2 = new \core_privacy\local\request\userlist($usercontext2, $component); 220 221 // Create repository_instances record for user1. 222 $this->setup_test_scenario_data($user1->id, 1); 223 // Create repository_instances record for user2. 224 $this->setup_test_scenario_data($user2->id, 1); 225 226 // Ensure the user list for usercontext1 contains user1. 227 provider::get_users_in_context($userlist1); 228 $this->assertCount(1, $userlist1); 229 // Ensure the user list for usercontext2 contains user2. 230 provider::get_users_in_context($userlist2); 231 $this->assertCount(1, $userlist2); 232 233 // Convert $userlist1 into an approved_contextlist. 234 $approvedlist = new approved_userlist($usercontext1, $component, $userlist1->get_userids()); 235 236 // Delete using delete_data_for_user. 237 provider::delete_data_for_users($approvedlist); 238 239 // Re-fetch users in the usercontext1 - The user list should now be empty. 240 $userlist1 = new \core_privacy\local\request\userlist($usercontext1, $component); 241 provider::get_users_in_context($userlist1); 242 $this->assertCount(0, $userlist1); 243 // Re-fetch users in the usercontext2 - The user list should not be empty. 244 $userlist2 = new \core_privacy\local\request\userlist($usercontext2, $component); 245 provider::get_users_in_context($userlist2); 246 $this->assertCount(1, $userlist2); 247 248 // User data should be only removed in the user context. 249 $systemcontext = context_system::instance(); 250 // Add userlist2 to the approved user list in the system context. 251 $approvedlist = new approved_userlist($systemcontext, $component, $userlist2->get_userids()); 252 // Delete user1 data using delete_data_for_user. 253 provider::delete_data_for_users($approvedlist); 254 // Re-fetch users in usercontext2 - The user list should not be empty (user2). 255 $userlist2 = new \core_privacy\local\request\userlist($usercontext2, $component); 256 provider::get_users_in_context($userlist2); 257 $this->assertCount(1, $userlist2); 258 } 259 260 /** 261 * Helper function to setup repository_instances records for testing a specific user. 262 * 263 * @param int $userid The Id of the User used for testing. 264 * @param int $noscenarios The number of repository_instance records to create for the User. 265 * @throws dml_exception 266 */ 267 private function setup_test_scenario_data($userid, $noscenarios) { 268 global $DB; 269 270 for ($i = 0; $i < $noscenarios; $i++) { 271 $repositoryinstance = (object)[ 272 'typeid' => ($i + 1), 273 'name' => 'My Test Repo', 274 'userid' => $userid, 275 'contextid' => 1, 276 'username' => 'some username', 277 'password' => 'some password', 278 'timecreated' => date('u'), 279 'timemodified' => date('u'), 280 'readonly' => 0 281 ]; 282 $DB->insert_record('repository_instances', $repositoryinstance); 283 } 284 } 285 286 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body