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