See Release Notes
Long Term Support Release
Differences Between: [Versions 401 and 402] [Versions 401 and 403]
1 <?php 2 // This file is part of Moodle - http://moodle.org/ 3 // 4 // Moodle is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // Moodle is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU General Public License for more details. 13 // 14 // You should have received a copy of the GNU General Public License 15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>. 16 17 /** 18 * Base class for unit tests for enrol_cohort. 19 * 20 * @package enrol_cohort 21 * @category test 22 * @copyright 2018 Carlos Escobedo <carlos@moodle.com> 23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 24 */ 25 namespace enrol_cohort\privacy; 26 27 defined('MOODLE_INTERNAL') || die(); 28 29 use core_privacy\local\request\writer; 30 use core_privacy\local\request\approved_contextlist; 31 use enrol_cohort\privacy\provider; 32 33 /** 34 * Unit tests for the enrol_cohort implementation of the privacy API. 35 * 36 * @copyright 2018 Carlos Escobedo <carlos@moodle.com> 37 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 38 */ 39 class provider_test extends \core_privacy\tests\provider_testcase { 40 41 /** 42 * Test getting the context for the user ID related to this plugin. 43 */ 44 public function test_get_contexts_for_userid() { 45 global $DB; 46 47 $this->resetAfterTest(); 48 $trace = new \null_progress_trace(); 49 50 $cohortplugin = enrol_get_plugin('cohort'); 51 $user1 = $this->getDataGenerator()->create_user(); 52 $cat1 = $this->getDataGenerator()->create_category(); 53 $course1 = $this->getDataGenerator()->create_course(array('category' => $cat1->id)); 54 $group1 = $this->getDataGenerator()->create_group(array('courseid' => $course1->id)); 55 $studentrole = $DB->get_record('role', array('shortname' => 'student')); 56 $cohort1 = $this->getDataGenerator()->create_cohort( 57 array('contextid' => \context_coursecat::instance($cat1->id)->id)); 58 $cohortplugin->add_instance($course1, array( 59 'customint1' => $cohort1->id, 60 'roleid' => $studentrole->id, 61 'customint2' => $group1->id) 62 ); 63 64 cohort_add_member($cohort1->id, $user1->id); 65 enrol_cohort_sync($trace, $course1->id); 66 // Check if user1 is enrolled into course1 in group 1. 67 $this->assertEquals(1, $DB->count_records('role_assignments', array())); 68 $this->assertTrue($DB->record_exists('groups_members', array( 69 'groupid' => $group1->id, 70 'userid' => $user1->id, 71 'component' => 'enrol_cohort') 72 )); 73 // Check context course fro provider to user1. 74 $context = \context_course::instance($course1->id); 75 $contextlist = provider::get_contexts_for_userid($user1->id); 76 $this->assertEquals($context->id, $contextlist->current()->id); 77 } 78 79 /** 80 * Test that user data is exported correctly. 81 */ 82 public function test_export_user_data() { 83 global $DB; 84 85 $this->resetAfterTest(); 86 $trace = new \null_progress_trace(); 87 88 $cohortplugin = enrol_get_plugin('cohort'); 89 $user1 = $this->getDataGenerator()->create_user(); 90 $cat1 = $this->getDataGenerator()->create_category(); 91 $course1 = $this->getDataGenerator()->create_course(array('category' => $cat1->id)); 92 $group1 = $this->getDataGenerator()->create_group(array('courseid' => $course1->id)); 93 $studentrole = $DB->get_record('role', array('shortname' => 'student')); 94 $cohort1 = $this->getDataGenerator()->create_cohort( 95 array('contextid' => \context_coursecat::instance($cat1->id)->id)); 96 $cohortplugin->add_instance($course1, array( 97 'customint1' => $cohort1->id, 98 'roleid' => $studentrole->id, 99 'customint2' => $group1->id) 100 ); 101 102 cohort_add_member($cohort1->id, $user1->id); 103 enrol_cohort_sync($trace, $course1->id); 104 // Check if user1 is enrolled into course1 in group 1. 105 $this->assertEquals(1, $DB->count_records('role_assignments', array())); 106 $this->assertTrue($DB->record_exists('groups_members', array( 107 'groupid' => $group1->id, 108 'userid' => $user1->id, 109 'component' => 'enrol_cohort') 110 )); 111 112 $this->setUser($user1); 113 $contextlist = provider::get_contexts_for_userid($user1->id); 114 $approvedcontextlist = new approved_contextlist($user1, 'enrol_cohort', $contextlist->get_contextids()); 115 provider::export_user_data($approvedcontextlist); 116 foreach ($contextlist as $context) { 117 $writer = writer::with_context($context); 118 $data = $writer->get_data([ 119 get_string('pluginname', 'enrol_cohort'), 120 get_string('groups', 'core_group') 121 ]); 122 $this->assertTrue($writer->has_any_data()); 123 if ($context->contextlevel == CONTEXT_COURSE) { 124 $exportedgroups = $data->groups; 125 // User1 only belongs to group1 via enrol_cohort. 126 $this->assertCount(1, $exportedgroups); 127 $exportedgroup = reset($exportedgroups); 128 $this->assertEquals($group1->name, $exportedgroup->name); 129 } 130 } 131 } 132 133 /** 134 * Test for provider::delete_data_for_all_users_in_context(). 135 */ 136 public function test_delete_data_for_all_users_in_context() { 137 global $DB; 138 139 $this->resetAfterTest(); 140 $trace = new \null_progress_trace(); 141 142 $cohortplugin = enrol_get_plugin('cohort'); 143 $user1 = $this->getDataGenerator()->create_user(); 144 $user2 = $this->getDataGenerator()->create_user(); 145 $user3 = $this->getDataGenerator()->create_user(); 146 $cat1 = $this->getDataGenerator()->create_category(); 147 $course1 = $this->getDataGenerator()->create_course(array('category' => $cat1->id)); 148 $group1 = $this->getDataGenerator()->create_group(array('courseid' => $course1->id)); 149 $studentrole = $DB->get_record('role', array('shortname' => 'student')); 150 $cohort1 = $this->getDataGenerator()->create_cohort( 151 array('contextid' => \context_coursecat::instance($cat1->id)->id)); 152 $cohortplugin->add_instance($course1, array( 153 'customint1' => $cohort1->id, 154 'roleid' => $studentrole->id, 155 'customint2' => $group1->id) 156 ); 157 158 cohort_add_member($cohort1->id, $user1->id); 159 cohort_add_member($cohort1->id, $user2->id); 160 cohort_add_member($cohort1->id, $user3->id); 161 enrol_cohort_sync($trace, $course1->id); 162 $this->assertEquals( 163 3, 164 $DB->count_records_sql("SELECT COUNT(gm.id) 165 FROM {groups_members} gm 166 JOIN {groups} g ON gm.groupid = g.id 167 WHERE g.courseid = ?", [$course1->id]) 168 ); 169 170 $coursecontext1 = \context_course::instance($course1->id); 171 provider::delete_data_for_all_users_in_context($coursecontext1); 172 $this->assertEquals( 173 0, 174 $DB->count_records_sql("SELECT COUNT(gm.id) 175 FROM {groups_members} gm 176 JOIN {groups} g ON gm.groupid = g.id 177 WHERE g.courseid = ?", [$course1->id]) 178 ); 179 } 180 181 /** 182 * Test for provider::delete_data_for_user(). 183 */ 184 public function test_delete_data_for_user() { 185 global $DB; 186 187 $this->resetAfterTest(); 188 $trace = new \null_progress_trace(); 189 190 $cohortplugin = enrol_get_plugin('cohort'); 191 $user1 = $this->getDataGenerator()->create_user(); 192 $user2 = $this->getDataGenerator()->create_user(); 193 $user3 = $this->getDataGenerator()->create_user(); 194 $cat1 = $this->getDataGenerator()->create_category(); 195 $course1 = $this->getDataGenerator()->create_course(array('category' => $cat1->id)); 196 $course2 = $this->getDataGenerator()->create_course(array('category' => $cat1->id)); 197 $group1 = $this->getDataGenerator()->create_group(array('courseid' => $course1->id)); 198 $group2 = $this->getDataGenerator()->create_group(array('courseid' => $course2->id)); 199 $studentrole = $DB->get_record('role', array('shortname' => 'student')); 200 $cohort1 = $this->getDataGenerator()->create_cohort( 201 array('contextid' => \context_coursecat::instance($cat1->id)->id)); 202 $cohortplugin->add_instance($course1, array( 203 'customint1' => $cohort1->id, 204 'roleid' => $studentrole->id, 205 'customint2' => $group1->id) 206 ); 207 $cohortplugin->add_instance($course2, array( 208 'customint1' => $cohort1->id, 209 'roleid' => $studentrole->id, 210 'customint2' => $group2->id) 211 ); 212 213 $this->getDataGenerator()->enrol_user($user2->id, $course1->id); 214 $this->getDataGenerator()->enrol_user($user3->id, $course1->id); 215 $this->getDataGenerator()->create_group_member(array('groupid' => $group1->id, 'userid' => $user2->id)); 216 $this->getDataGenerator()->create_group_member(array('groupid' => $group1->id, 'userid' => $user3->id)); 217 218 cohort_add_member($cohort1->id, $user1->id); 219 enrol_cohort_sync($trace, $course1->id); 220 221 $this->assertEquals( 222 3, 223 $DB->count_records_sql("SELECT COUNT(gm.id) 224 FROM {groups_members} gm 225 JOIN {groups} g ON gm.groupid = g.id 226 WHERE g.courseid = ?", [$course1->id]) 227 ); 228 229 $this->assertEquals( 230 1, 231 $DB->count_records_sql("SELECT COUNT(gm.id) 232 FROM {groups_members} gm 233 JOIN {groups} g ON gm.groupid = g.id 234 WHERE g.courseid = ?", [$course2->id]) 235 ); 236 237 $this->setUser($user1); 238 $coursecontext1 = \context_course::instance($course1->id); 239 $coursecontext2 = \context_course::instance($course2->id); 240 $approvedcontextlist = new \core_privacy\tests\request\approved_contextlist($user1, 'enrol_cohort', 241 [$coursecontext1->id, $coursecontext2->id]); 242 provider::delete_data_for_user($approvedcontextlist); 243 // Check we have 2 users in groups because we are deleted user1. 244 $this->assertEquals( 245 2, 246 $DB->count_records_sql("SELECT COUNT(gm.id) 247 FROM {groups_members} gm 248 JOIN {groups} g ON gm.groupid = g.id 249 WHERE g.courseid = ?", [$course1->id]) 250 ); 251 // Check we have not users in groups. 252 $this->assertEquals( 253 0, 254 $DB->count_records_sql("SELECT COUNT(gm.id) 255 FROM {groups_members} gm 256 JOIN {groups} g ON gm.groupid = g.id 257 WHERE g.courseid = ?", [$course2->id]) 258 ); 259 } 260 261 /** 262 * Test for provider::delete_data_for_users(). 263 */ 264 public function test_delete_data_for_users() { 265 global $DB; 266 267 $this->resetAfterTest(); 268 269 $trace = new \null_progress_trace(); 270 271 $cohortplugin = enrol_get_plugin('cohort'); 272 273 $user1 = $this->getDataGenerator()->create_user(); 274 $user2 = $this->getDataGenerator()->create_user(); 275 $user3 = $this->getDataGenerator()->create_user(); 276 277 $cat1 = $this->getDataGenerator()->create_category(); 278 279 $course1 = $this->getDataGenerator()->create_course(array('category' => $cat1->id)); 280 $course2 = $this->getDataGenerator()->create_course(array('category' => $cat1->id)); 281 282 $group1 = $this->getDataGenerator()->create_group(array('courseid' => $course1->id)); 283 $group2 = $this->getDataGenerator()->create_group(array('courseid' => $course2->id)); 284 285 $studentrole = $DB->get_record('role', array('shortname' => 'student')); 286 287 $cohort1 = $this->getDataGenerator()->create_cohort( 288 array('contextid' => \context_coursecat::instance($cat1->id)->id)); 289 $cohortplugin->add_instance($course1, array( 290 'customint1' => $cohort1->id, 291 'roleid' => $studentrole->id, 292 'customint2' => $group1->id) 293 ); 294 $cohortplugin->add_instance($course2, array( 295 'customint1' => $cohort1->id, 296 'roleid' => $studentrole->id, 297 'customint2' => $group2->id) 298 ); 299 300 $this->getDataGenerator()->enrol_user($user2->id, $course1->id); 301 $this->getDataGenerator()->enrol_user($user3->id, $course1->id); 302 $this->getDataGenerator()->create_group_member(array('groupid' => $group1->id, 'userid' => $user2->id)); 303 $this->getDataGenerator()->create_group_member(array('groupid' => $group1->id, 'userid' => $user3->id)); 304 305 cohort_add_member($cohort1->id, $user1->id); 306 enrol_cohort_sync($trace, $course1->id); 307 308 $this->assertEquals( 309 3, 310 $DB->count_records_sql("SELECT COUNT(gm.id) 311 FROM {groups_members} gm 312 JOIN {groups} g ON gm.groupid = g.id 313 WHERE g.courseid = ?", [$course1->id]) 314 ); 315 316 $this->assertEquals( 317 1, 318 $DB->count_records_sql("SELECT COUNT(gm.id) 319 FROM {groups_members} gm 320 JOIN {groups} g ON gm.groupid = g.id 321 WHERE g.courseid = ?", [$course2->id]) 322 ); 323 324 $coursecontext1 = \context_course::instance($course1->id); 325 326 $approveduserlist = new \core_privacy\local\request\approved_userlist($coursecontext1, 'enrol_cohort', 327 [$user1->id, $user2->id]); 328 provider::delete_data_for_users($approveduserlist); 329 330 // Check we have 2 users in groups because we have deleted user1. 331 // User2's membership is manual and is not as the result of a cohort enrolment. 332 $this->assertEquals( 333 2, 334 $DB->count_records_sql("SELECT COUNT(gm.id) 335 FROM {groups_members} gm 336 JOIN {groups} g ON gm.groupid = g.id 337 WHERE g.courseid = ?", [$course1->id]) 338 ); 339 340 // Check that course2 is not touched. 341 $this->assertEquals( 342 1, 343 $DB->count_records_sql("SELECT COUNT(gm.id) 344 FROM {groups_members} gm 345 JOIN {groups} g ON gm.groupid = g.id 346 WHERE g.courseid = ?", [$course2->id]) 347 ); 348 } 349 350 /** 351 * Test for provider::get_users_in_context(). 352 */ 353 public function test_get_users_in_context() { 354 global $DB; 355 356 $this->resetAfterTest(); 357 358 $trace = new \null_progress_trace(); 359 360 $cohortplugin = enrol_get_plugin('cohort'); 361 362 $cat1 = $this->getDataGenerator()->create_category(); 363 $course1 = $this->getDataGenerator()->create_course(array('category' => $cat1->id)); 364 $group1 = $this->getDataGenerator()->create_group(array('courseid' => $course1->id)); 365 $studentrole = $DB->get_record('role', array('shortname' => 'student')); 366 $cohort1 = $this->getDataGenerator()->create_cohort( 367 array('contextid' => \context_coursecat::instance($cat1->id)->id)); 368 $cohortplugin->add_instance($course1, array( 369 'customint1' => $cohort1->id, 370 'roleid' => $studentrole->id, 371 'customint2' => $group1->id) 372 ); 373 374 $user1 = $this->getDataGenerator()->create_user(); 375 376 cohort_add_member($cohort1->id, $user1->id); 377 enrol_cohort_sync($trace, $course1->id); 378 379 // Check if user1 is enrolled into course1 in group 1. 380 $this->assertEquals(1, $DB->count_records('role_assignments', array())); 381 $this->assertTrue($DB->record_exists('groups_members', array( 382 'groupid' => $group1->id, 383 'userid' => $user1->id, 384 'component' => 'enrol_cohort') 385 )); 386 387 $context = \context_course::instance($course1->id); 388 389 $userlist = new \core_privacy\local\request\userlist($context, 'enrol_cohort'); 390 \enrol_cohort\privacy\provider::get_users_in_context($userlist); 391 392 $this->assertEquals([$user1->id], $userlist->get_userids()); 393 } 394 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body