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 /** 18 * Privacy provider tests. 19 * 20 * @package core_group 21 * @category test 22 * @copyright 2018 Shamim Rezaie <shamim@moodle.com> 23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 24 */ 25 26 defined('MOODLE_INTERNAL') || die(); 27 28 use core_privacy\tests\provider_testcase; 29 use core_privacy\local\metadata\collection; 30 use core_group\privacy\provider; 31 use core_privacy\local\request\writer; 32 33 /** 34 * Class core_group_privacy_provider_testcase. 35 * 36 * @copyright 2018 Shamim Rezaie <shamim@moodle.com> 37 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 38 */ 39 class core_group_privacy_provider_testcase extends provider_testcase { 40 41 /** 42 * Test for provider::get_metadata(). 43 */ 44 public function test_get_metadata() { 45 $collection = new collection('core_group'); 46 $newcollection = provider::get_metadata($collection); 47 $itemcollection = $newcollection->get_collection(); 48 $this->assertCount(2, $itemcollection); 49 50 $table = array_shift($itemcollection); 51 $this->assertEquals('groups_members', $table->get_name()); 52 $this->assertEquals('privacy:metadata:groups', $table->get_summary()); 53 54 $privacyfields = $table->get_privacy_fields(); 55 $this->assertArrayHasKey('groupid', $privacyfields); 56 $this->assertArrayHasKey('userid', $privacyfields); 57 $this->assertArrayHasKey('timeadded', $privacyfields); 58 59 $table = array_shift($itemcollection); 60 $this->assertEquals('core_message', $table->get_name()); 61 $this->assertEquals('privacy:metadata:core_message', $table->get_summary()); 62 } 63 64 /** 65 * Test for provider::export_groups() to export manual group memberships. 66 */ 67 public function test_export_groups() { 68 $this->resetAfterTest(); 69 70 $course = $this->getDataGenerator()->create_course(); 71 $user1 = $this->getDataGenerator()->create_user(); 72 $user2 = $this->getDataGenerator()->create_user(); 73 $group1 = $this->getDataGenerator()->create_group(array('courseid' => $course->id)); 74 $group2 = $this->getDataGenerator()->create_group(array('courseid' => $course->id)); 75 $group3 = $this->getDataGenerator()->create_group(array('courseid' => $course->id)); 76 $group4 = $this->getDataGenerator()->create_group(array('courseid' => $course->id)); 77 $this->getDataGenerator()->enrol_user($user1->id, $course->id); 78 $this->getDataGenerator()->enrol_user($user2->id, $course->id); 79 80 // Add user1 to group1 and group2. 81 $this->getDataGenerator()->create_group_member(array('groupid' => $group1->id, 'userid' => $user1->id)); 82 $this->getDataGenerator()->create_group_member(array('groupid' => $group2->id, 'userid' => $user1->id)); 83 84 // Add user2 to group2 and group3. 85 $this->getDataGenerator()->create_group_member(array('groupid' => $group2->id, 'userid' => $user2->id)); 86 $this->getDataGenerator()->create_group_member(array('groupid' => $group3->id, 'userid' => $user2->id)); 87 88 $context = context_course::instance($course->id); 89 90 // Retrieve groups for user1. 91 $this->setUser($user1); 92 $writer = writer::with_context($context); 93 provider::export_groups($context, ''); 94 95 $data = $writer->get_data([get_string('groups', 'core_group')]); 96 $exportedgroups = $data->groups; 97 98 // User1 belongs to group1 and group2. 99 $this->assertEqualsCanonicalizing( 100 [$group1->name, $group2->name], 101 array_column($exportedgroups, 'name')); 102 } 103 104 /** 105 * Test for provider::export_groups() to export group memberships of a component. 106 */ 107 public function test_export_groups_for_component() { 108 $this->resetAfterTest(); 109 110 $course = $this->getDataGenerator()->create_course(); 111 $user1 = $this->getDataGenerator()->create_user(); 112 $user2 = $this->getDataGenerator()->create_user(); 113 $group1 = $this->getDataGenerator()->create_group(array('courseid' => $course->id)); 114 $group2 = $this->getDataGenerator()->create_group(array('courseid' => $course->id)); 115 $group3 = $this->getDataGenerator()->create_group(array('courseid' => $course->id)); 116 $group4 = $this->getDataGenerator()->create_group(array('courseid' => $course->id)); 117 $group5 = $this->getDataGenerator()->create_group(array('courseid' => $course->id)); 118 $this->getDataGenerator()->enrol_user($user1->id, $course->id, null, 'self'); 119 $this->getDataGenerator()->enrol_user($user2->id, $course->id, null, 'self'); 120 121 // Add user1 to group1 (via enrol_self) and group2 and group3. 122 $this->getDataGenerator()->create_group_member( 123 array('groupid' => $group1->id, 'userid' => $user1->id, 'component' => 'enrol_self')); 124 $this->getDataGenerator()->create_group_member(array('groupid' => $group2->id, 'userid' => $user1->id)); 125 $this->getDataGenerator()->create_group_member(array('groupid' => $group3->id, 'userid' => $user1->id)); 126 127 // Add user2 to group3 (via enrol_self) and group4. 128 $this->getDataGenerator()->create_group_member( 129 array('groupid' => $group3->id, 'userid' => $user2->id, 'component' => 'enrol_self')); 130 $this->getDataGenerator()->create_group_member(array('groupid' => $group4->id, 'userid' => $user2->id)); 131 132 $context = context_course::instance($course->id); 133 134 // Retrieve groups for user1. 135 $this->setUser($user1); 136 $writer = writer::with_context($context); 137 provider::export_groups($context, 'enrol_self'); 138 139 $data = $writer->get_data([get_string('groups', 'core_group')]); 140 $exportedgroups = $data->groups; 141 142 // User1 only belongs to group1 via enrol_self. 143 $this->assertCount(1, $exportedgroups); 144 $exportedgroup = reset($exportedgroups); 145 $this->assertEquals($group1->name, $exportedgroup->name); 146 } 147 148 /** 149 * Test for provider::delete_groups_for_all_users() to delete manual group memberships. 150 */ 151 public function test_delete_groups_for_all_users() { 152 global $DB; 153 154 $this->resetAfterTest(); 155 156 $course1 = $this->getDataGenerator()->create_course(); 157 $course2 = $this->getDataGenerator()->create_course(); 158 159 $group1a = $this->getDataGenerator()->create_group(array('courseid' => $course1->id)); 160 $group1b = $this->getDataGenerator()->create_group(array('courseid' => $course1->id)); 161 $group2a = $this->getDataGenerator()->create_group(array('courseid' => $course2->id)); 162 $group2b = $this->getDataGenerator()->create_group(array('courseid' => $course2->id)); 163 164 $user1 = $this->getDataGenerator()->create_user(); 165 $user2 = $this->getDataGenerator()->create_user(); 166 167 $this->getDataGenerator()->enrol_user($user1->id, $course1->id); 168 $this->getDataGenerator()->enrol_user($user1->id, $course2->id); 169 $this->getDataGenerator()->enrol_user($user2->id, $course1->id); 170 $this->getDataGenerator()->enrol_user($user2->id, $course2->id); 171 172 $this->getDataGenerator()->create_group_member(array('groupid' => $group1a->id, 'userid' => $user1->id)); 173 $this->getDataGenerator()->create_group_member(array('groupid' => $group1b->id, 'userid' => $user2->id)); 174 $this->getDataGenerator()->create_group_member(array('groupid' => $group2a->id, 'userid' => $user1->id)); 175 $this->getDataGenerator()->create_group_member(array('groupid' => $group2b->id, 'userid' => $user2->id)); 176 177 $this->assertEquals( 178 2, 179 $DB->count_records_sql("SELECT COUNT(gm.id) 180 FROM {groups_members} gm 181 JOIN {groups} g ON gm.groupid = g.id 182 WHERE g.courseid = ?", [$course1->id]) 183 ); 184 $this->assertEquals( 185 2, 186 $DB->count_records_sql("SELECT COUNT(gm.id) 187 FROM {groups_members} gm 188 JOIN {groups} g ON gm.groupid = g.id 189 WHERE g.courseid = ?", [$course2->id]) 190 ); 191 192 $coursecontext1 = context_course::instance($course1->id); 193 provider::delete_groups_for_all_users($coursecontext1, ''); 194 195 $this->assertEquals( 196 0, 197 $DB->count_records_sql("SELECT COUNT(gm.id) 198 FROM {groups_members} gm 199 JOIN {groups} g ON gm.groupid = g.id 200 WHERE g.courseid = ?", [$course1->id]) 201 ); 202 $this->assertEquals( 203 2, 204 $DB->count_records_sql("SELECT COUNT(gm.id) 205 FROM {groups_members} gm 206 JOIN {groups} g ON gm.groupid = g.id 207 WHERE g.courseid = ?", [$course2->id]) 208 ); 209 } 210 211 /** 212 * Test for provider::delete_groups_for_all_users() to delete group memberships of a component. 213 */ 214 public function test_delete_groups_for_all_users_for_component() { 215 global $DB; 216 217 $this->resetAfterTest(); 218 219 $course1 = $this->getDataGenerator()->create_course(); 220 $course2 = $this->getDataGenerator()->create_course(); 221 222 $group1a = $this->getDataGenerator()->create_group(array('courseid' => $course1->id)); 223 $group1b = $this->getDataGenerator()->create_group(array('courseid' => $course1->id)); 224 $group2a = $this->getDataGenerator()->create_group(array('courseid' => $course2->id)); 225 $group2b = $this->getDataGenerator()->create_group(array('courseid' => $course2->id)); 226 227 $user1 = $this->getDataGenerator()->create_user(); 228 $user2 = $this->getDataGenerator()->create_user(); 229 230 $this->getDataGenerator()->enrol_user($user1->id, $course1->id, null, 'self'); 231 $this->getDataGenerator()->enrol_user($user1->id, $course2->id, null, 'self'); 232 $this->getDataGenerator()->enrol_user($user2->id, $course1->id, null, 'self'); 233 $this->getDataGenerator()->enrol_user($user2->id, $course2->id, null, 'self'); 234 235 $this->getDataGenerator()->create_group_member( 236 array('groupid' => $group1a->id, 'userid' => $user1->id, 'component' => 'enrol_self')); 237 $this->getDataGenerator()->create_group_member(array('groupid' => $group1b->id, 'userid' => $user2->id)); 238 $this->getDataGenerator()->create_group_member( 239 array('groupid' => $group2a->id, 'userid' => $user1->id, 'component' => 'enrol_self')); 240 $this->getDataGenerator()->create_group_member(array('groupid' => $group2b->id, 'userid' => $user2->id)); 241 242 $this->assertEquals( 243 2, 244 $DB->count_records_sql("SELECT COUNT(gm.id) 245 FROM {groups_members} gm 246 JOIN {groups} g ON gm.groupid = g.id 247 WHERE g.courseid = ?", [$course1->id]) 248 ); 249 $this->assertEquals( 250 2, 251 $DB->count_records_sql("SELECT COUNT(gm.id) 252 FROM {groups_members} gm 253 JOIN {groups} g ON gm.groupid = g.id 254 WHERE g.courseid = ?", [$course2->id]) 255 ); 256 257 $coursecontext1 = context_course::instance($course1->id); 258 provider::delete_groups_for_all_users($coursecontext1, 'enrol_self'); 259 260 $this->assertEquals( 261 1, 262 $DB->count_records_sql("SELECT COUNT(gm.id) 263 FROM {groups_members} gm 264 JOIN {groups} g ON gm.groupid = g.id 265 WHERE g.courseid = ?", [$course1->id]) 266 ); 267 $this->assertEquals( 268 2, 269 $DB->count_records_sql("SELECT COUNT(gm.id) 270 FROM {groups_members} gm 271 JOIN {groups} g ON gm.groupid = g.id 272 WHERE g.courseid = ?", [$course2->id]) 273 ); 274 } 275 276 /** 277 * Test for provider::delete_groups_for_all_users() to check deleting from cache. 278 */ 279 public function test_delete_groups_for_all_users_deletes_cache() { 280 $this->resetAfterTest(); 281 282 $course = $this->getDataGenerator()->create_course(); 283 284 $group1 = $this->getDataGenerator()->create_group(array('courseid' => $course->id)); 285 $group2 = $this->getDataGenerator()->create_group(array('courseid' => $course->id)); 286 287 $user1 = $this->getDataGenerator()->create_user(); 288 $user2 = $this->getDataGenerator()->create_user(); 289 290 $this->getDataGenerator()->enrol_user($user1->id, $course->id); 291 $this->getDataGenerator()->enrol_user($user2->id, $course->id); 292 293 $this->getDataGenerator()->create_group_member(array('userid' => $user1->id, 'groupid' => $group1->id)); 294 $this->getDataGenerator()->create_group_member(array('userid' => $user1->id, 'groupid' => $group2->id)); 295 $this->getDataGenerator()->create_group_member(array('userid' => $user2->id, 'groupid' => $group1->id)); 296 297 $this->assertEqualsCanonicalizing([[$group1->id, $group2->id]], groups_get_user_groups($course->id, $user1->id)); 298 $this->assertEquals([[$group1->id]], groups_get_user_groups($course->id, $user2->id)); 299 300 $coursecontext = context_course::instance($course->id); 301 provider::delete_groups_for_all_users($coursecontext, ''); 302 303 $this->assertEquals([[]], groups_get_user_groups($course->id, $user1->id)); 304 $this->assertEquals([[]], groups_get_user_groups($course->id, $user2->id)); 305 } 306 307 /** 308 * Test for provider::delete_groups_for_user() to delete manual group memberships. 309 */ 310 public function test_delete_groups_for_user() { 311 global $DB; 312 313 $this->resetAfterTest(); 314 315 $course1 = $this->getDataGenerator()->create_course(); 316 $course2 = $this->getDataGenerator()->create_course(); 317 $course3 = $this->getDataGenerator()->create_course(); 318 319 $group1a = $this->getDataGenerator()->create_group(array('courseid' => $course1->id)); 320 $group1b = $this->getDataGenerator()->create_group(array('courseid' => $course1->id)); 321 $group2a = $this->getDataGenerator()->create_group(array('courseid' => $course2->id)); 322 $group2b = $this->getDataGenerator()->create_group(array('courseid' => $course2->id)); 323 $group3a = $this->getDataGenerator()->create_group(array('courseid' => $course3->id)); 324 $group3b = $this->getDataGenerator()->create_group(array('courseid' => $course3->id)); 325 326 $user1 = $this->getDataGenerator()->create_user(); 327 $user2 = $this->getDataGenerator()->create_user(); 328 329 $this->getDataGenerator()->enrol_user($user1->id, $course1->id); 330 $this->getDataGenerator()->enrol_user($user1->id, $course2->id); 331 $this->getDataGenerator()->enrol_user($user1->id, $course3->id); 332 $this->getDataGenerator()->enrol_user($user2->id, $course1->id); 333 $this->getDataGenerator()->enrol_user($user2->id, $course2->id); 334 $this->getDataGenerator()->enrol_user($user2->id, $course3->id); 335 336 $this->getDataGenerator()->create_group_member(array('groupid' => $group1a->id, 'userid' => $user1->id)); 337 $this->getDataGenerator()->create_group_member(array('groupid' => $group1b->id, 'userid' => $user2->id)); 338 $this->getDataGenerator()->create_group_member(array('groupid' => $group2a->id, 'userid' => $user1->id)); 339 $this->getDataGenerator()->create_group_member(array('groupid' => $group2b->id, 'userid' => $user2->id)); 340 $this->getDataGenerator()->create_group_member(array('groupid' => $group3a->id, 'userid' => $user1->id)); 341 $this->getDataGenerator()->create_group_member(array('groupid' => $group3b->id, 'userid' => $user2->id)); 342 343 $this->assertEquals( 344 2, 345 $DB->count_records_sql("SELECT COUNT(gm.id) 346 FROM {groups_members} gm 347 JOIN {groups} g ON gm.groupid = g.id 348 WHERE g.courseid = ?", [$course1->id]) 349 ); 350 $this->assertEquals( 351 2, 352 $DB->count_records_sql("SELECT COUNT(gm.id) 353 FROM {groups_members} gm 354 JOIN {groups} g ON gm.groupid = g.id 355 WHERE g.courseid = ?", [$course2->id]) 356 ); 357 $this->assertEquals( 358 2, 359 $DB->count_records_sql("SELECT COUNT(gm.id) 360 FROM {groups_members} gm 361 JOIN {groups} g ON gm.groupid = g.id 362 WHERE g.courseid = ?", [$course2->id]) 363 ); 364 $this->assertEquals( 365 3, 366 $DB->count_records_sql("SELECT COUNT(gm.id) 367 FROM {groups_members} gm 368 JOIN {groups} g ON gm.groupid = g.id 369 WHERE gm.userid = ?", [$user1->id]) 370 ); 371 372 $this->setUser($user1); 373 $coursecontext1 = context_course::instance($course1->id); 374 $coursecontext2 = context_course::instance($course2->id); 375 $approvedcontextlist = new \core_privacy\tests\request\approved_contextlist($user1, 'core_group', 376 [$coursecontext1->id, $coursecontext2->id]); 377 provider::delete_groups_for_user($approvedcontextlist, ''); 378 379 $this->assertEquals( 380 1, 381 $DB->count_records_sql("SELECT COUNT(gm.id) 382 FROM {groups_members} gm 383 JOIN {groups} g ON gm.groupid = g.id 384 WHERE g.courseid = ?", [$course1->id]) 385 ); 386 $this->assertEquals( 387 1, 388 $DB->count_records_sql("SELECT COUNT(gm.id) 389 FROM {groups_members} gm 390 JOIN {groups} g ON gm.groupid = g.id 391 WHERE g.courseid = ?", [$course2->id]) 392 ); 393 $this->assertEquals( 394 2, 395 $DB->count_records_sql("SELECT COUNT(gm.id) 396 FROM {groups_members} gm 397 JOIN {groups} g ON gm.groupid = g.id 398 WHERE g.courseid = ?", [$course3->id]) 399 ); 400 $this->assertEquals( 401 1, 402 $DB->count_records_sql("SELECT COUNT(gm.id) 403 FROM {groups_members} gm 404 JOIN {groups} g ON gm.groupid = g.id 405 WHERE gm.userid = ?", [$user1->id]) 406 ); 407 } 408 409 /** 410 * Test for provider::delete_groups_for_user() to delete group memberships of a component. 411 */ 412 public function test_delete_groups_for_user_for_component() { 413 global $DB; 414 415 $this->resetAfterTest(); 416 417 $course1 = $this->getDataGenerator()->create_course(); 418 $course2 = $this->getDataGenerator()->create_course(); 419 $course3 = $this->getDataGenerator()->create_course(); 420 421 $group1a = $this->getDataGenerator()->create_group(array('courseid' => $course1->id)); 422 $group1b = $this->getDataGenerator()->create_group(array('courseid' => $course1->id)); 423 $group2a = $this->getDataGenerator()->create_group(array('courseid' => $course2->id)); 424 $group2b = $this->getDataGenerator()->create_group(array('courseid' => $course2->id)); 425 $group3a = $this->getDataGenerator()->create_group(array('courseid' => $course3->id)); 426 $group3b = $this->getDataGenerator()->create_group(array('courseid' => $course3->id)); 427 428 $user1 = $this->getDataGenerator()->create_user(); 429 $user2 = $this->getDataGenerator()->create_user(); 430 431 $this->getDataGenerator()->enrol_user($user1->id, $course1->id, null, 'self'); 432 $this->getDataGenerator()->enrol_user($user1->id, $course2->id, null, 'self'); 433 $this->getDataGenerator()->enrol_user($user1->id, $course3->id, null, 'self'); 434 $this->getDataGenerator()->enrol_user($user2->id, $course1->id, null, 'self'); 435 $this->getDataGenerator()->enrol_user($user2->id, $course2->id, null, 'self'); 436 $this->getDataGenerator()->enrol_user($user2->id, $course3->id, null, 'self'); 437 438 $this->getDataGenerator()->create_group_member( 439 array('groupid' => $group1a->id, 'userid' => $user1->id, 'component' => 'enrol_self')); 440 $this->getDataGenerator()->create_group_member( 441 array('groupid' => $group1b->id, 'userid' => $user2->id, 'component' => 'enrol_self')); 442 $this->getDataGenerator()->create_group_member(array('groupid' => $group2a->id, 'userid' => $user1->id)); 443 $this->getDataGenerator()->create_group_member(array('groupid' => $group2b->id, 'userid' => $user2->id)); 444 $this->getDataGenerator()->create_group_member(array('groupid' => $group3a->id, 'userid' => $user1->id)); 445 $this->getDataGenerator()->create_group_member(array('groupid' => $group3b->id, 'userid' => $user2->id)); 446 447 $this->assertEquals( 448 2, 449 $DB->count_records_sql("SELECT COUNT(gm.id) 450 FROM {groups_members} gm 451 JOIN {groups} g ON gm.groupid = g.id 452 WHERE g.courseid = ?", [$course1->id]) 453 ); 454 $this->assertEquals( 455 2, 456 $DB->count_records_sql("SELECT COUNT(gm.id) 457 FROM {groups_members} gm 458 JOIN {groups} g ON gm.groupid = g.id 459 WHERE g.courseid = ?", [$course2->id]) 460 ); 461 $this->assertEquals( 462 2, 463 $DB->count_records_sql("SELECT COUNT(gm.id) 464 FROM {groups_members} gm 465 JOIN {groups} g ON gm.groupid = g.id 466 WHERE g.courseid = ?", [$course2->id]) 467 ); 468 $this->assertEquals( 469 3, 470 $DB->count_records_sql("SELECT COUNT(gm.id) 471 FROM {groups_members} gm 472 JOIN {groups} g ON gm.groupid = g.id 473 WHERE gm.userid = ?", [$user1->id]) 474 ); 475 476 $this->setUser($user1); 477 $coursecontext1 = context_course::instance($course1->id); 478 $coursecontext2 = context_course::instance($course2->id); 479 $approvedcontextlist = new \core_privacy\tests\request\approved_contextlist($user1, 'core_group', 480 [$coursecontext1->id, $coursecontext2->id]); 481 provider::delete_groups_for_user($approvedcontextlist, 'enrol_self'); 482 483 $this->assertEquals( 484 1, 485 $DB->count_records_sql("SELECT COUNT(gm.id) 486 FROM {groups_members} gm 487 JOIN {groups} g ON gm.groupid = g.id 488 WHERE g.courseid = ?", [$course1->id]) 489 ); 490 $this->assertEquals( 491 2, 492 $DB->count_records_sql("SELECT COUNT(gm.id) 493 FROM {groups_members} gm 494 JOIN {groups} g ON gm.groupid = g.id 495 WHERE g.courseid = ?", [$course2->id]) 496 ); 497 $this->assertEquals( 498 2, 499 $DB->count_records_sql("SELECT COUNT(gm.id) 500 FROM {groups_members} gm 501 JOIN {groups} g ON gm.groupid = g.id 502 WHERE g.courseid = ?", [$course3->id]) 503 ); 504 $this->assertEquals( 505 2, 506 $DB->count_records_sql("SELECT COUNT(gm.id) 507 FROM {groups_members} gm 508 JOIN {groups} g ON gm.groupid = g.id 509 WHERE gm.userid = ?", [$user1->id]) 510 ); 511 } 512 513 /** 514 * Test for provider::delete_groups_for_users() to delete group memberships of a component. 515 */ 516 public function test_delete_groups_for_users_for_component() { 517 global $DB; 518 519 $this->resetAfterTest(); 520 521 $course1 = $this->getDataGenerator()->create_course(); 522 $course2 = $this->getDataGenerator()->create_course(); 523 $course3 = $this->getDataGenerator()->create_course(); 524 525 $group1a = $this->getDataGenerator()->create_group(array('courseid' => $course1->id)); 526 $group1b = $this->getDataGenerator()->create_group(array('courseid' => $course1->id)); 527 $group2a = $this->getDataGenerator()->create_group(array('courseid' => $course2->id)); 528 $group2b = $this->getDataGenerator()->create_group(array('courseid' => $course2->id)); 529 $group3a = $this->getDataGenerator()->create_group(array('courseid' => $course3->id)); 530 $group3b = $this->getDataGenerator()->create_group(array('courseid' => $course3->id)); 531 532 $user1 = $this->getDataGenerator()->create_user(); 533 $user2 = $this->getDataGenerator()->create_user(); 534 535 $this->getDataGenerator()->enrol_user($user1->id, $course1->id, null, 'self'); 536 $this->getDataGenerator()->enrol_user($user1->id, $course2->id, null, 'self'); 537 $this->getDataGenerator()->enrol_user($user1->id, $course3->id, null, 'self'); 538 $this->getDataGenerator()->enrol_user($user2->id, $course1->id, null, 'self'); 539 $this->getDataGenerator()->enrol_user($user2->id, $course2->id, null, 'self'); 540 $this->getDataGenerator()->enrol_user($user2->id, $course3->id, null, 'self'); 541 542 $this->getDataGenerator()->create_group_member( 543 array('groupid' => $group1a->id, 'userid' => $user1->id, 'component' => 'enrol_self')); 544 $this->getDataGenerator()->create_group_member( 545 array('groupid' => $group1b->id, 'userid' => $user2->id, 'component' => 'enrol_self')); 546 $this->getDataGenerator()->create_group_member( 547 array('groupid' => $group2a->id, 'userid' => $user1->id, 'component' => 'enrol_self')); 548 $this->getDataGenerator()->create_group_member( 549 array('groupid' => $group2b->id, 'userid' => $user2->id, 'component' => 'enrol_self')); 550 $this->getDataGenerator()->create_group_member(array('groupid' => $group3a->id, 'userid' => $user1->id)); 551 $this->getDataGenerator()->create_group_member(array('groupid' => $group3b->id, 'userid' => $user2->id)); 552 553 $this->assertEquals( 554 2, 555 $DB->count_records_sql("SELECT COUNT(gm.id) 556 FROM {groups_members} gm 557 JOIN {groups} g ON gm.groupid = g.id 558 WHERE g.courseid = ?", [$course1->id]) 559 ); 560 $this->assertEquals( 561 2, 562 $DB->count_records_sql("SELECT COUNT(gm.id) 563 FROM {groups_members} gm 564 JOIN {groups} g ON gm.groupid = g.id 565 WHERE g.courseid = ?", [$course2->id]) 566 ); 567 $this->assertEquals( 568 2, 569 $DB->count_records_sql("SELECT COUNT(gm.id) 570 FROM {groups_members} gm 571 JOIN {groups} g ON gm.groupid = g.id 572 WHERE g.courseid = ?", [$course2->id]) 573 ); 574 $this->assertEquals( 575 3, 576 $DB->count_records_sql("SELECT COUNT(gm.id) 577 FROM {groups_members} gm 578 JOIN {groups} g ON gm.groupid = g.id 579 WHERE gm.userid = ?", [$user1->id]) 580 ); 581 582 // Delete user1 and user2 from groups in course1. 583 $coursecontext1 = context_course::instance($course1->id); 584 $approveduserlist = new \core_privacy\local\request\approved_userlist($coursecontext1, 'core_group', 585 [$user1->id, $user2->id]); 586 provider::delete_groups_for_users($approveduserlist, 'enrol_self'); 587 588 $this->assertEquals( 589 0, 590 $DB->count_records_sql("SELECT COUNT(gm.id) 591 FROM {groups_members} gm 592 JOIN {groups} g ON gm.groupid = g.id 593 WHERE g.courseid = ?", [$course1->id]) 594 ); 595 $this->assertEquals( 596 2, 597 $DB->count_records_sql("SELECT COUNT(gm.id) 598 FROM {groups_members} gm 599 JOIN {groups} g ON gm.groupid = g.id 600 WHERE g.courseid = ?", [$course2->id]) 601 ); 602 $this->assertEquals( 603 2, 604 $DB->count_records_sql("SELECT COUNT(gm.id) 605 FROM {groups_members} gm 606 JOIN {groups} g ON gm.groupid = g.id 607 WHERE g.courseid = ?", [$course3->id]) 608 ); 609 $this->assertEquals( 610 2, 611 $DB->count_records_sql("SELECT COUNT(gm.id) 612 FROM {groups_members} gm 613 JOIN {groups} g ON gm.groupid = g.id 614 WHERE gm.userid = ?", [$user1->id]) 615 ); 616 617 // Delete user1 and user2 from course3. 618 $coursecontext3 = context_course::instance($course3->id); 619 $approveduserlist = new \core_privacy\local\request\approved_userlist($coursecontext3, 'core_group', 620 [$user1->id, $user2->id]); 621 provider::delete_groups_for_users($approveduserlist, 'enrol_self'); 622 $this->assertEquals( 623 0, 624 $DB->count_records_sql("SELECT COUNT(gm.id) 625 FROM {groups_members} gm 626 JOIN {groups} g ON gm.groupid = g.id 627 WHERE g.courseid = ?", [$course1->id]) 628 ); 629 $this->assertEquals( 630 2, 631 $DB->count_records_sql("SELECT COUNT(gm.id) 632 FROM {groups_members} gm 633 JOIN {groups} g ON gm.groupid = g.id 634 WHERE g.courseid = ?", [$course2->id]) 635 ); 636 $this->assertEquals( 637 2, 638 $DB->count_records_sql("SELECT COUNT(gm.id) 639 FROM {groups_members} gm 640 JOIN {groups} g ON gm.groupid = g.id 641 WHERE g.courseid = ?", [$course3->id]) 642 ); 643 $this->assertEquals( 644 2, 645 $DB->count_records_sql("SELECT COUNT(gm.id) 646 FROM {groups_members} gm 647 JOIN {groups} g ON gm.groupid = g.id 648 WHERE gm.userid = ?", [$user1->id]) 649 ); 650 } 651 652 /** 653 * Test for provider::delete_groups_for_user() to check deleting from cache. 654 */ 655 public function test_delete_groups_for_user_deletes_cache() { 656 $this->resetAfterTest(); 657 658 $course = $this->getDataGenerator()->create_course(); 659 660 $group1 = $this->getDataGenerator()->create_group(array('courseid' => $course->id)); 661 $group2 = $this->getDataGenerator()->create_group(array('courseid' => $course->id)); 662 663 $user = $this->getDataGenerator()->create_user(); 664 665 $this->getDataGenerator()->enrol_user($user->id, $course->id); 666 667 $this->getDataGenerator()->create_group_member(array('userid' => $user->id, 'groupid' => $group1->id)); 668 $this->getDataGenerator()->create_group_member(array('userid' => $user->id, 'groupid' => $group2->id)); 669 670 $this->assertEqualsCanonicalizing([[$group1->id, $group2->id]], groups_get_user_groups($course->id, $user->id)); 671 672 $this->setUser($user); 673 $coursecontext = context_course::instance($course->id); 674 $approvedcontextlist = new \core_privacy\tests\request\approved_contextlist($user, 'core_group', [$coursecontext->id]); 675 provider::delete_groups_for_user($approvedcontextlist, ''); 676 677 $this->assertEquals([[]], groups_get_user_groups($course->id, $user->id)); 678 } 679 680 /** 681 * Test for provider::get_contexts_for_userid(). 682 */ 683 public function test_get_contexts_for_userid() { 684 $this->resetAfterTest(); 685 686 $course1 = $this->getDataGenerator()->create_course(); 687 $course2 = $this->getDataGenerator()->create_course(); 688 $course3 = $this->getDataGenerator()->create_course(); 689 690 $group1a = $this->getDataGenerator()->create_group(array('courseid' => $course1->id)); 691 $group1b = $this->getDataGenerator()->create_group(array('courseid' => $course1->id)); 692 $group2a = $this->getDataGenerator()->create_group(array('courseid' => $course2->id)); 693 $group2b = $this->getDataGenerator()->create_group(array('courseid' => $course2->id)); 694 $group3a = $this->getDataGenerator()->create_group(array('courseid' => $course3->id)); 695 $group3b = $this->getDataGenerator()->create_group(array('courseid' => $course3->id)); 696 697 $user1 = $this->getDataGenerator()->create_user(); 698 $user2 = $this->getDataGenerator()->create_user(); 699 700 $this->getDataGenerator()->enrol_user($user1->id, $course1->id); 701 $this->getDataGenerator()->enrol_user($user1->id, $course2->id); 702 $this->getDataGenerator()->enrol_user($user1->id, $course3->id); 703 $this->getDataGenerator()->enrol_user($user2->id, $course1->id); 704 $this->getDataGenerator()->enrol_user($user2->id, $course2->id); 705 $this->getDataGenerator()->enrol_user($user2->id, $course3->id); 706 707 $this->getDataGenerator()->create_group_member(array('userid' => $user1->id, 'groupid' => $group1a->id)); 708 $this->getDataGenerator()->create_group_member(array('userid' => $user1->id, 'groupid' => $group2a->id)); 709 $this->getDataGenerator()->create_group_member(array('userid' => $user2->id, 'groupid' => $group1b->id)); 710 $this->getDataGenerator()->create_group_member(array('userid' => $user2->id, 'groupid' => $group2b->id)); 711 $this->getDataGenerator()->create_group_member(array('userid' => $user2->id, 'groupid' => $group3b->id)); 712 713 $coursecontext1 = context_course::instance($course1->id); 714 $coursecontext2 = context_course::instance($course2->id); 715 716 // User1 is member of some groups in course1 and course2 + self-conversation. 717 $contextlist = provider::get_contexts_for_userid($user1->id); 718 $contextids = array_values($contextlist->get_contextids()); 719 720 $this->assertCount(3, $contextlist); 721 // One of the user context is the one related to self-conversation. Let's test group contexts. 722 $this->assertContains($coursecontext1->id, $contextids); 723 $this->assertContains($coursecontext2->id, $contextids); 724 } 725 726 /** 727 * Test for provider::get_contexts_for_userid() when there are group memberships from other components. 728 */ 729 public function test_get_contexts_for_userid_component() { 730 $this->resetAfterTest(); 731 732 $course1 = $this->getDataGenerator()->create_course(); 733 $course2 = $this->getDataGenerator()->create_course(); 734 735 $group1 = $this->getDataGenerator()->create_group(array('courseid' => $course1->id)); 736 $group2 = $this->getDataGenerator()->create_group(array('courseid' => $course2->id)); 737 738 $user = $this->getDataGenerator()->create_user(); 739 740 $this->getDataGenerator()->enrol_user($user->id, $course1->id); 741 $this->getDataGenerator()->enrol_user($user->id, $course2->id); 742 743 $this->getDataGenerator()->create_group_member( 744 array( 745 'userid' => $user->id, 746 'groupid' => $group1->id 747 )); 748 $this->getDataGenerator()->create_group_member( 749 array( 750 'userid' => $user->id, 751 'groupid' => $group2->id, 752 'component' => 'enrol_meta' 753 )); 754 755 $coursecontext1 = context_course::instance($course1->id); 756 757 // User is member of some groups in course1 and course2, 758 // but only the membership in course1 is directly managed by core_group. 759 $contextlist = provider::get_contexts_for_userid($user->id); 760 $this->assertEquals($coursecontext1->id, $contextlist->get_contextids()[0]); 761 } 762 763 /** 764 * Test for provider::export_user_data(). 765 */ 766 public function test_export_user_data() { 767 $this->resetAfterTest(); 768 769 $course = $this->getDataGenerator()->create_course(); 770 $user1 = $this->getDataGenerator()->create_user(); 771 $user2 = $this->getDataGenerator()->create_user(); 772 $group1 = $this->getDataGenerator()->create_group(array('courseid' => $course->id)); 773 $group2 = $this->getDataGenerator()->create_group(array('courseid' => $course->id)); 774 $group3 = $this->getDataGenerator()->create_group(array('courseid' => $course->id)); 775 $group4 = $this->getDataGenerator()->create_group(array('courseid' => $course->id)); 776 $this->getDataGenerator()->enrol_user($user1->id, $course->id); 777 $this->getDataGenerator()->enrol_user($user2->id, $course->id); 778 779 // Add user1 to group1 and group2. 780 $this->getDataGenerator()->create_group_member(array('groupid' => $group1->id, 'userid' => $user1->id)); 781 $this->getDataGenerator()->create_group_member(array('groupid' => $group2->id, 'userid' => $user1->id)); 782 783 // Add user2 to group2 and group3. 784 $this->getDataGenerator()->create_group_member(array('groupid' => $group2->id, 'userid' => $user2->id)); 785 $this->getDataGenerator()->create_group_member(array('groupid' => $group3->id, 'userid' => $user2->id)); 786 787 $context = context_course::instance($course->id); 788 789 $this->setUser($user1); 790 791 // Export all of the data for the context. 792 $this->export_context_data_for_user($user1->id, $context, 'core_group'); 793 794 $writer = writer::with_context($context); 795 $this->assertTrue($writer->has_any_data()); 796 797 $data = $writer->get_data([get_string('groups', 'core_group')]); 798 $exportedgroups = $data->groups; 799 800 // User1 belongs to group1 and group2. 801 $this->assertEqualsCanonicalizing( 802 [$group1->name, $group2->name], 803 array_column($exportedgroups, 'name')); 804 } 805 806 /** 807 * Test for provider::delete_data_for_all_users_in_context(). 808 */ 809 public function test_delete_data_for_all_users_in_context() { 810 global $DB; 811 812 $this->resetAfterTest(); 813 814 $course1 = $this->getDataGenerator()->create_course(); 815 $course2 = $this->getDataGenerator()->create_course(); 816 817 $group1a = $this->getDataGenerator()->create_group(array('courseid' => $course1->id)); 818 $group1b = $this->getDataGenerator()->create_group(array('courseid' => $course1->id)); 819 $group2a = $this->getDataGenerator()->create_group(array('courseid' => $course2->id)); 820 $group2b = $this->getDataGenerator()->create_group(array('courseid' => $course2->id)); 821 822 $user1 = $this->getDataGenerator()->create_user(); 823 $user2 = $this->getDataGenerator()->create_user(); 824 825 $this->getDataGenerator()->enrol_user($user1->id, $course1->id); 826 $this->getDataGenerator()->enrol_user($user1->id, $course2->id); 827 $this->getDataGenerator()->enrol_user($user2->id, $course1->id); 828 $this->getDataGenerator()->enrol_user($user2->id, $course2->id); 829 830 $this->getDataGenerator()->create_group_member(array('groupid' => $group1a->id, 'userid' => $user1->id)); 831 $this->getDataGenerator()->create_group_member(array('groupid' => $group1b->id, 'userid' => $user2->id)); 832 $this->getDataGenerator()->create_group_member(array('groupid' => $group2a->id, 'userid' => $user1->id)); 833 $this->getDataGenerator()->create_group_member(array('groupid' => $group2b->id, 'userid' => $user2->id)); 834 835 $this->assertEquals( 836 2, 837 $DB->count_records_sql("SELECT COUNT(gm.id) 838 FROM {groups_members} gm 839 JOIN {groups} g ON gm.groupid = g.id 840 WHERE g.courseid = ?", [$course1->id]) 841 ); 842 $this->assertEquals( 843 2, 844 $DB->count_records_sql("SELECT COUNT(gm.id) 845 FROM {groups_members} gm 846 JOIN {groups} g ON gm.groupid = g.id 847 WHERE g.courseid = ?", [$course2->id]) 848 ); 849 850 $coursecontext1 = context_course::instance($course1->id); 851 provider::delete_data_for_all_users_in_context($coursecontext1); 852 853 $this->assertEquals( 854 0, 855 $DB->count_records_sql("SELECT COUNT(gm.id) 856 FROM {groups_members} gm 857 JOIN {groups} g ON gm.groupid = g.id 858 WHERE g.courseid = ?", [$course1->id]) 859 ); 860 $this->assertEquals( 861 2, 862 $DB->count_records_sql("SELECT COUNT(gm.id) 863 FROM {groups_members} gm 864 JOIN {groups} g ON gm.groupid = g.id 865 WHERE g.courseid = ?", [$course2->id]) 866 ); 867 } 868 869 /** 870 * Test for provider::delete_data_for_user(). 871 */ 872 public function test_delete_data_for_user() { 873 global $DB; 874 875 $this->resetAfterTest(); 876 877 $course1 = $this->getDataGenerator()->create_course(); 878 $course2 = $this->getDataGenerator()->create_course(); 879 $course3 = $this->getDataGenerator()->create_course(); 880 881 $group1a = $this->getDataGenerator()->create_group(array('courseid' => $course1->id)); 882 $group1b = $this->getDataGenerator()->create_group(array('courseid' => $course1->id)); 883 $group2a = $this->getDataGenerator()->create_group(array('courseid' => $course2->id)); 884 $group2b = $this->getDataGenerator()->create_group(array('courseid' => $course2->id)); 885 $group3a = $this->getDataGenerator()->create_group(array('courseid' => $course3->id)); 886 $group3b = $this->getDataGenerator()->create_group(array('courseid' => $course3->id)); 887 888 $user1 = $this->getDataGenerator()->create_user(); 889 $user2 = $this->getDataGenerator()->create_user(); 890 891 $this->getDataGenerator()->enrol_user($user1->id, $course1->id); 892 $this->getDataGenerator()->enrol_user($user1->id, $course2->id); 893 $this->getDataGenerator()->enrol_user($user1->id, $course3->id); 894 $this->getDataGenerator()->enrol_user($user2->id, $course1->id); 895 $this->getDataGenerator()->enrol_user($user2->id, $course2->id); 896 $this->getDataGenerator()->enrol_user($user2->id, $course3->id); 897 898 $this->getDataGenerator()->create_group_member(array('groupid' => $group1a->id, 'userid' => $user1->id)); 899 $this->getDataGenerator()->create_group_member(array('groupid' => $group1b->id, 'userid' => $user2->id)); 900 $this->getDataGenerator()->create_group_member(array('groupid' => $group2a->id, 'userid' => $user1->id)); 901 $this->getDataGenerator()->create_group_member(array('groupid' => $group2b->id, 'userid' => $user2->id)); 902 $this->getDataGenerator()->create_group_member(array('groupid' => $group3a->id, 'userid' => $user1->id)); 903 $this->getDataGenerator()->create_group_member(array('groupid' => $group3b->id, 'userid' => $user2->id)); 904 905 $this->assertEquals( 906 2, 907 $DB->count_records_sql("SELECT COUNT(gm.id) 908 FROM {groups_members} gm 909 JOIN {groups} g ON gm.groupid = g.id 910 WHERE g.courseid = ?", [$course1->id]) 911 ); 912 $this->assertEquals( 913 2, 914 $DB->count_records_sql("SELECT COUNT(gm.id) 915 FROM {groups_members} gm 916 JOIN {groups} g ON gm.groupid = g.id 917 WHERE g.courseid = ?", [$course2->id]) 918 ); 919 $this->assertEquals( 920 2, 921 $DB->count_records_sql("SELECT COUNT(gm.id) 922 FROM {groups_members} gm 923 JOIN {groups} g ON gm.groupid = g.id 924 WHERE g.courseid = ?", [$course2->id]) 925 ); 926 $this->assertEquals( 927 3, 928 $DB->count_records_sql("SELECT COUNT(gm.id) 929 FROM {groups_members} gm 930 JOIN {groups} g ON gm.groupid = g.id 931 WHERE gm.userid = ?", [$user1->id]) 932 ); 933 934 $this->setUser($user1); 935 $coursecontext1 = context_course::instance($course1->id); 936 $coursecontext2 = context_course::instance($course2->id); 937 $approvedcontextlist = new \core_privacy\tests\request\approved_contextlist($user1, 'core_group', 938 [$coursecontext1->id, $coursecontext2->id]); 939 provider::delete_data_for_user($approvedcontextlist); 940 941 $this->assertEquals( 942 1, 943 $DB->count_records_sql("SELECT COUNT(gm.id) 944 FROM {groups_members} gm 945 JOIN {groups} g ON gm.groupid = g.id 946 WHERE g.courseid = ?", [$course1->id]) 947 ); 948 $this->assertEquals( 949 1, 950 $DB->count_records_sql("SELECT COUNT(gm.id) 951 FROM {groups_members} gm 952 JOIN {groups} g ON gm.groupid = g.id 953 WHERE g.courseid = ?", [$course2->id]) 954 ); 955 $this->assertEquals( 956 2, 957 $DB->count_records_sql("SELECT COUNT(gm.id) 958 FROM {groups_members} gm 959 JOIN {groups} g ON gm.groupid = g.id 960 WHERE g.courseid = ?", [$course3->id]) 961 ); 962 $this->assertEquals( 963 1, 964 $DB->count_records_sql("SELECT COUNT(gm.id) 965 FROM {groups_members} gm 966 JOIN {groups} g ON gm.groupid = g.id 967 WHERE gm.userid = ?", [$user1->id]) 968 ); 969 } 970 971 /** 972 * Test for provider::delete_data_for_users(). 973 */ 974 public function test_delete_data_for_users() { 975 global $DB; 976 977 $this->resetAfterTest(); 978 979 $course1 = $this->getDataGenerator()->create_course(); 980 $course2 = $this->getDataGenerator()->create_course(); 981 982 $group1a = $this->getDataGenerator()->create_group(array('courseid' => $course1->id)); 983 $group1b = $this->getDataGenerator()->create_group(array('courseid' => $course1->id)); 984 $group1c = $this->getDataGenerator()->create_group(array('courseid' => $course1->id)); 985 $group2a = $this->getDataGenerator()->create_group(array('courseid' => $course2->id)); 986 $group2b = $this->getDataGenerator()->create_group(array('courseid' => $course2->id)); 987 $group2c = $this->getDataGenerator()->create_group(array('courseid' => $course2->id)); 988 989 $user1 = $this->getDataGenerator()->create_user(); 990 $user2 = $this->getDataGenerator()->create_user(); 991 $user3 = $this->getDataGenerator()->create_user(); 992 993 $this->getDataGenerator()->enrol_user($user1->id, $course1->id); 994 $this->getDataGenerator()->enrol_user($user1->id, $course2->id); 995 $this->getDataGenerator()->enrol_user($user2->id, $course1->id); 996 $this->getDataGenerator()->enrol_user($user2->id, $course2->id); 997 $this->getDataGenerator()->enrol_user($user3->id, $course1->id); 998 $this->getDataGenerator()->enrol_user($user3->id, $course2->id); 999 1000 $this->getDataGenerator()->create_group_member(array('groupid' => $group1a->id, 'userid' => $user1->id)); 1001 $this->getDataGenerator()->create_group_member(array('groupid' => $group1b->id, 'userid' => $user2->id)); 1002 $this->getDataGenerator()->create_group_member(array('groupid' => $group1c->id, 'userid' => $user3->id)); 1003 $this->getDataGenerator()->create_group_member(array('groupid' => $group2a->id, 'userid' => $user1->id)); 1004 $this->getDataGenerator()->create_group_member(array('groupid' => $group2b->id, 'userid' => $user2->id)); 1005 $this->getDataGenerator()->create_group_member(array('groupid' => $group2c->id, 'userid' => $user3->id)); 1006 1007 $this->assertEquals( 1008 3, 1009 $DB->count_records_sql("SELECT COUNT(gm.id) 1010 FROM {groups_members} gm 1011 JOIN {groups} g ON gm.groupid = g.id 1012 WHERE g.courseid = ?", [$course1->id]) 1013 ); 1014 $this->assertEquals( 1015 3, 1016 $DB->count_records_sql("SELECT COUNT(gm.id) 1017 FROM {groups_members} gm 1018 JOIN {groups} g ON gm.groupid = g.id 1019 WHERE g.courseid = ?", [$course2->id]) 1020 ); 1021 1022 $coursecontext1 = context_course::instance($course1->id); 1023 $approveduserlist = new \core_privacy\local\request\approved_userlist($coursecontext1, 'core_group', 1024 [$user1->id, $user2->id]); 1025 provider::delete_data_for_users($approveduserlist); 1026 1027 $this->assertEquals( 1028 [$user3->id], 1029 $DB->get_fieldset_sql("SELECT gm.userid 1030 FROM {groups_members} gm 1031 JOIN {groups} g ON gm.groupid = g.id 1032 WHERE g.courseid = ?", [$course1->id]) 1033 ); 1034 $this->assertEquals( 1035 3, 1036 $DB->count_records_sql("SELECT COUNT(gm.id) 1037 FROM {groups_members} gm 1038 JOIN {groups} g ON gm.groupid = g.id 1039 WHERE g.courseid = ?", [$course2->id]) 1040 ); 1041 } 1042 1043 /** 1044 * Test for provider::get_users_in_context(). 1045 */ 1046 public function test_get_users_in_context() { 1047 $this->resetAfterTest(); 1048 1049 $course1 = $this->getDataGenerator()->create_course(); 1050 $course2 = $this->getDataGenerator()->create_course(); 1051 1052 $group1a = $this->getDataGenerator()->create_group(array('courseid' => $course1->id)); 1053 $group1b = $this->getDataGenerator()->create_group(array('courseid' => $course1->id)); 1054 $group2a = $this->getDataGenerator()->create_group(array('courseid' => $course2->id)); 1055 $group2b = $this->getDataGenerator()->create_group(array('courseid' => $course2->id)); 1056 1057 $user1 = $this->getDataGenerator()->create_user(); 1058 $user2 = $this->getDataGenerator()->create_user(); 1059 $user3 = $this->getDataGenerator()->create_user(); 1060 1061 $this->getDataGenerator()->enrol_user($user1->id, $course1->id); 1062 $this->getDataGenerator()->enrol_user($user1->id, $course2->id); 1063 $this->getDataGenerator()->enrol_user($user2->id, $course1->id); 1064 $this->getDataGenerator()->enrol_user($user2->id, $course2->id); 1065 $this->getDataGenerator()->enrol_user($user3->id, $course1->id); 1066 $this->getDataGenerator()->enrol_user($user3->id, $course2->id); 1067 1068 $this->getDataGenerator()->create_group_member(array('userid' => $user1->id, 'groupid' => $group1a->id)); 1069 $this->getDataGenerator()->create_group_member(array('userid' => $user1->id, 'groupid' => $group2a->id)); 1070 $this->getDataGenerator()->create_group_member(array('userid' => $user2->id, 'groupid' => $group1b->id)); 1071 $this->getDataGenerator()->create_group_member(array('userid' => $user2->id, 'groupid' => $group2b->id)); 1072 $this->getDataGenerator()->create_group_member(array('userid' => $user3->id, 'groupid' => $group2a->id)); 1073 1074 $coursecontext1 = context_course::instance($course1->id); 1075 1076 $userlist = new \core_privacy\local\request\userlist($coursecontext1, 'core_group'); 1077 \core_group\privacy\provider::get_users_in_context($userlist); 1078 1079 // Only user1 and user2. User3 is not member of any group in course1. 1080 $this->assertCount(2, $userlist); 1081 $this->assertEqualsCanonicalizing( 1082 [$user1->id, $user2->id], 1083 $userlist->get_userids()); 1084 } 1085 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body