See Release Notes
Long Term Support Release
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 /** 18 * Base class for unit tests for core_cohort. 19 * 20 * @package core_cohort 21 * @category test 22 * @copyright 2018 Sara Arjona <sara@moodle.com> 23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 24 */ 25 namespace core_cohort\privacy; 26 27 defined('MOODLE_INTERNAL') || die(); 28 29 use core_cohort\privacy\provider; 30 use core_privacy\local\request\approved_contextlist; 31 use core_privacy\local\request\writer; 32 use core_privacy\tests\provider_testcase; 33 use core_privacy\local\request\approved_userlist; 34 35 /** 36 * Unit tests for cohort\classes\privacy\provider.php 37 * 38 * @copyright 2018 Sara Arjona <sara@moodle.com> 39 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 40 */ 41 class provider_test extends provider_testcase { 42 43 /** 44 * Basic setup for these tests. 45 */ 46 public function setUp(): void { 47 $this->resetAfterTest(true); 48 } 49 50 /** 51 * Test getting the context for the user ID related to this plugin. 52 */ 53 public function test_get_contexts_for_userid() { 54 // Create system cohort and category cohort. 55 $coursecategory = $this->getDataGenerator()->create_category(); 56 $coursecategoryctx = \context_coursecat::instance($coursecategory->id); 57 $systemctx = \context_system::instance(); 58 $categorycohort = $this->getDataGenerator()->create_cohort([ 59 'contextid' => $coursecategoryctx->id, 60 'name' => 'Category cohort 1', 61 ]); 62 $systemcohort = $this->getDataGenerator()->create_cohort([ 63 'contextid' => $systemctx->id, 64 'name' => 'System cohort 1' 65 ]); 66 67 // Create user and add to the system and category cohorts. 68 $user = $this->getDataGenerator()->create_user(); 69 cohort_add_member($categorycohort->id, $user->id); 70 cohort_add_member($systemcohort->id, $user->id); 71 72 // User is member of 2 cohorts. 73 $contextlist = provider::get_contexts_for_userid($user->id); 74 $this->assertCount(2, (array) $contextlist->get_contextids()); 75 $this->assertContainsEquals($coursecategoryctx->id, $contextlist->get_contextids()); 76 $this->assertContainsEquals($systemctx->id, $contextlist->get_contextids()); 77 } 78 79 /** 80 * Test that data is exported correctly for this plugin. 81 */ 82 public function test_export_user_data() { 83 // Create system cohort and category cohort. 84 $coursecategory = $this->getDataGenerator()->create_category(); 85 $coursecategoryctx = \context_coursecat::instance($coursecategory->id); 86 $systemctx = \context_system::instance(); 87 $categorycohort = $this->getDataGenerator()->create_cohort([ 88 'contextid' => $coursecategoryctx->id, 89 'name' => 'Category cohort 1', 90 ]); 91 $systemcohort1 = $this->getDataGenerator()->create_cohort([ 92 'contextid' => $systemctx->id, 93 'name' => 'System cohort 1' 94 ]); 95 $systemcohort2 = $this->getDataGenerator()->create_cohort([ 96 'contextid' => $systemctx->id, 97 'name' => 'System cohort 2' 98 ]); 99 100 // Create user and add to the system and category cohorts. 101 $user = $this->getDataGenerator()->create_user(); 102 cohort_add_member($categorycohort->id, $user->id); 103 cohort_add_member($systemcohort1->id, $user->id); 104 cohort_add_member($systemcohort2->id, $user->id); 105 106 // Validate system cohort exported data. 107 $writer = writer::with_context($systemctx); 108 $this->assertFalse($writer->has_any_data()); 109 $this->export_context_data_for_user($user->id, $systemctx, 'core_cohort'); 110 $data = $writer->get_related_data([], 'cohort'); 111 $this->assertCount(2, $data); 112 113 // Validate category cohort exported data. 114 $writer = writer::with_context($coursecategoryctx); 115 $this->assertFalse($writer->has_any_data()); 116 $this->export_context_data_for_user($user->id, $coursecategoryctx, 'core_cohort'); 117 $data = $writer->get_related_data([], 'cohort'); 118 $this->assertCount(1, $data); 119 $this->assertEquals($categorycohort->name, reset($data)->name); 120 } 121 122 /** 123 * Test for provider::delete_data_for_all_users_in_context(). 124 */ 125 public function test_delete_data_for_all_users_in_context() { 126 global $DB; 127 128 // Create system cohort and category cohort. 129 $coursecategory = $this->getDataGenerator()->create_category(); 130 $coursecategoryctx = \context_coursecat::instance($coursecategory->id); 131 $systemctx = \context_system::instance(); 132 $categorycohort = $this->getDataGenerator()->create_cohort([ 133 'contextid' => $coursecategoryctx->id, 134 'name' => 'Category cohort 1', 135 'idnumber' => '', 136 'description' => '' 137 ]); 138 $systemcohort = $this->getDataGenerator()->create_cohort([ 139 'contextid' => $systemctx->id, 140 'name' => 'System cohort 1' 141 ]); 142 143 // Create user and add to the system and category cohorts. 144 $user = $this->getDataGenerator()->create_user(); 145 cohort_add_member($categorycohort->id, $user->id); 146 cohort_add_member($systemcohort->id, $user->id); 147 148 // Before deletion, we should have 2 entries in the cohort_members table. 149 $count = $DB->count_records('cohort_members'); 150 $this->assertEquals(2, $count); 151 152 // Delete data based on system context. 153 provider::delete_data_for_all_users_in_context($systemctx); 154 155 // After deletion, the cohort_members entries should have been deleted. 156 $count = $DB->count_records('cohort_members'); 157 $this->assertEquals(1, $count); 158 159 // Delete data based on category context. 160 provider::delete_data_for_all_users_in_context($coursecategoryctx); 161 162 // After deletion, the cohort_members entries should have been deleted. 163 $count = $DB->count_records('cohort_members'); 164 $this->assertEquals(0, $count); 165 } 166 167 /** 168 * Test for provider::delete_data_for_user(). 169 */ 170 public function test_delete_data_for_user() { 171 global $DB; 172 173 // Create system cohort and category cohort. 174 $coursecategory = $this->getDataGenerator()->create_category(); 175 $coursecategoryctx = \context_coursecat::instance($coursecategory->id); 176 $systemctx = \context_system::instance(); 177 $categorycohort = $this->getDataGenerator()->create_cohort([ 178 'contextid' => $coursecategoryctx->id, 179 'name' => 'Category cohort 1', 180 'idnumber' => '', 181 'description' => '' 182 ]); 183 $systemcohort = $this->getDataGenerator()->create_cohort([ 184 'contextid' => $systemctx->id, 185 'name' => 'System cohort 1' 186 ]); 187 188 // Create user and add to the system and category cohorts. 189 $user1 = $this->getDataGenerator()->create_user(); 190 cohort_add_member($categorycohort->id, $user1->id); 191 cohort_add_member($systemcohort->id, $user1->id); 192 193 // Create another user and add to the system and category cohorts. 194 $user2 = $this->getDataGenerator()->create_user(); 195 cohort_add_member($categorycohort->id, $user2->id); 196 cohort_add_member($systemcohort->id, $user2->id); 197 198 // Create another user and add to the system cohort. 199 $user3 = $this->getDataGenerator()->create_user(); 200 cohort_add_member($systemcohort->id, $user3->id); 201 202 // Before deletion, we should have 5 entries in the cohort_members table. 203 $count = $DB->count_records('cohort_members'); 204 $this->assertEquals(5, $count); 205 206 $contextlist = provider::get_contexts_for_userid($user1->id); 207 $contexts = []; 208 $contexts[] = \context_user::instance($user1->id)->id; 209 $contexts = array_merge($contexts, $contextlist->get_contextids()); 210 $approvedcontextlist = new approved_contextlist($user1, 'cohort', $contexts); 211 provider::delete_data_for_user($approvedcontextlist); 212 213 // After deletion, the cohort_members entries for the first student should have been deleted. 214 $count = $DB->count_records('cohort_members', ['userid' => $user1->id]); 215 $this->assertEquals(0, $count); 216 $count = $DB->count_records('cohort_members'); 217 $this->assertEquals(3, $count); 218 219 // Confirm that the cohorts hasn't been removed. 220 $cohortscount = $DB->get_records('cohort'); 221 $this->assertCount(2, (array) $cohortscount); 222 } 223 224 /** 225 * Test that only users within a course context are fetched. 226 */ 227 public function test_get_users_in_context() { 228 $component = 'core_cohort'; 229 230 // Create system cohort and category cohort. 231 $coursecategory = $this->getDataGenerator()->create_category(); 232 $coursecategoryctx = \context_coursecat::instance($coursecategory->id); 233 $systemctx = \context_system::instance(); 234 $categorycohort = $this->getDataGenerator()->create_cohort([ 235 'contextid' => $coursecategoryctx->id, 236 'name' => 'Category cohort 1', 237 ]); 238 // Create user. 239 $user = $this->getDataGenerator()->create_user(); 240 $userctx = \context_user::instance($user->id); 241 242 $userlist1 = new \core_privacy\local\request\userlist($coursecategoryctx, $component); 243 provider::get_users_in_context($userlist1); 244 $this->assertCount(0, $userlist1); 245 246 $userlist2 = new \core_privacy\local\request\userlist($systemctx, $component); 247 provider::get_users_in_context($userlist2); 248 $this->assertCount(0, $userlist2); 249 250 $systemcohort = $this->getDataGenerator()->create_cohort([ 251 'contextid' => $systemctx->id, 252 'name' => 'System cohort 1' 253 ]); 254 // Create user and add to the system and category cohorts. 255 cohort_add_member($categorycohort->id, $user->id); 256 cohort_add_member($systemcohort->id, $user->id); 257 258 // The list of users within the coursecat context should contain user. 259 $userlist1 = new \core_privacy\local\request\userlist($coursecategoryctx, $component); 260 provider::get_users_in_context($userlist1); 261 $this->assertCount(1, $userlist1); 262 $expected = [$user->id]; 263 $actual = $userlist1->get_userids(); 264 $this->assertEquals($expected, $actual); 265 266 // The list of users within the system context should contain user. 267 $userlist2 = new \core_privacy\local\request\userlist($systemctx, $component); 268 provider::get_users_in_context($userlist2); 269 $this->assertCount(1, $userlist2); 270 $expected = [$user->id]; 271 $actual = $userlist2->get_userids(); 272 $this->assertEquals($expected, $actual); 273 274 // The list of users within the user context should be empty. 275 $userlist3 = new \core_privacy\local\request\userlist($userctx, $component); 276 provider::get_users_in_context($userlist3); 277 $this->assertCount(0, $userlist3); 278 } 279 280 /** 281 * Test that data for users in approved userlist is deleted. 282 */ 283 public function test_delete_data_for_users() { 284 $component = 'core_cohort'; 285 286 // Create system cohort and category cohort. 287 $coursecategory = $this->getDataGenerator()->create_category(); 288 $coursecategoryctx = \context_coursecat::instance($coursecategory->id); 289 $systemctx = \context_system::instance(); 290 $categorycohort = $this->getDataGenerator()->create_cohort([ 291 'contextid' => $coursecategoryctx->id, 292 'name' => 'Category cohort 1', 293 ]); 294 // Create user1. 295 $user1 = $this->getDataGenerator()->create_user(); 296 $userctx1 = \context_user::instance($user1->id); 297 // Create user2. 298 $user2 = $this->getDataGenerator()->create_user(); 299 300 $systemcohort = $this->getDataGenerator()->create_cohort([ 301 'contextid' => $systemctx->id, 302 'name' => 'System cohort 1' 303 ]); 304 // Create user and add to the system and category cohorts. 305 cohort_add_member($categorycohort->id, $user1->id); 306 cohort_add_member($systemcohort->id, $user1->id); 307 cohort_add_member($categorycohort->id, $user2->id); 308 309 $userlist1 = new \core_privacy\local\request\userlist($coursecategoryctx, $component); 310 provider::get_users_in_context($userlist1); 311 $this->assertCount(2, $userlist1); 312 313 $userlist2 = new \core_privacy\local\request\userlist($systemctx, $component); 314 provider::get_users_in_context($userlist2); 315 $this->assertCount(1, $userlist2); 316 317 // Convert $userlist1 into an approved_contextlist. 318 $approvedlist1 = new approved_userlist($coursecategoryctx, $component, $userlist1->get_userids()); 319 // Delete using delete_data_for_user. 320 provider::delete_data_for_users($approvedlist1); 321 322 // Re-fetch users in coursecategoryctx. 323 $userlist1 = new \core_privacy\local\request\userlist($coursecategoryctx, $component); 324 provider::get_users_in_context($userlist1); 325 // The user data in coursecategoryctx should be deleted. 326 $this->assertCount(0, $userlist1); 327 // Re-fetch users in coursecategoryctx. 328 $userlist2 = new \core_privacy\local\request\userlist($systemctx, $component); 329 provider::get_users_in_context($userlist2); 330 // The user data in coursecontext2 should be still present. 331 $this->assertCount(1, $userlist2); 332 333 // Convert $userlist2 into an approved_contextlist in the user context. 334 $approvedlist3 = new approved_userlist($userctx1, $component, $userlist2->get_userids()); 335 // Delete using delete_data_for_user. 336 provider::delete_data_for_users($approvedlist3); 337 // Re-fetch users in coursecontext1. 338 $userlist3 = new \core_privacy\local\request\userlist($systemctx, $component); 339 provider::get_users_in_context($userlist3); 340 // The user data in systemcontext should not be deleted. 341 $this->assertCount(1, $userlist3); 342 } 343 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body