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