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 tests for core_calendar. 19 * 20 * @package core_calendar 21 * @category test 22 * @copyright 2018 Zig Tan <zig@moodle.com> 23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 24 */ 25 26 defined('MOODLE_INTERNAL') || die(); 27 global $CFG; 28 29 require_once($CFG->dirroot . '/calendar/lib.php'); 30 require_once($CFG->dirroot . '/calendar/tests/externallib_test.php'); 31 32 use \core_calendar\privacy\provider; 33 use \core_privacy\local\metadata\collection; 34 use \core_privacy\local\request\approved_contextlist; 35 use \core_privacy\local\request\writer; 36 use \core_privacy\tests\provider_testcase; 37 use \core_privacy\local\request\approved_userlist; 38 39 /** 40 * Unit tests for calendar/classes/privacy/provider 41 * 42 * @copyright 2018 Zig Tan <zig@moodle.com> 43 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 44 */ 45 class core_calendar_privacy_testcase extends provider_testcase { 46 47 /** 48 * Overriding setUp() function to always reset after tests. 49 */ 50 public function setUp(): void { 51 $this->resetAfterTest(true); 52 } 53 54 /** 55 * Test for provider::get_contexts_for_userid(). 56 * 57 * @throws coding_exception 58 */ 59 public function test_get_contexts_for_userid() { 60 // Create test user to create Calendar Events and Subscriptions. 61 $user = $this->getDataGenerator()->create_user(); 62 $this->setUser($user); 63 64 // Create a Category and Courses to assign Calendar Events and Subscriptions. 65 $category = $this->getDataGenerator()->create_category(); 66 $course1 = $this->getDataGenerator()->create_course(); 67 $course2 = $this->getDataGenerator()->create_course(); 68 $course3 = $this->getDataGenerator()->create_course(); 69 $grouprecord = (object)[ 70 'courseid' => $course3->id, 71 'name' => 'test_group' 72 ]; 73 $course3group = $this->getDataGenerator()->create_group($grouprecord); 74 75 // Get contexts. 76 $usercontext = context_user::instance($user->id); 77 $categorycontext = context_coursecat::instance($category->id); 78 $course1context = context_course::instance($course1->id); 79 $course2context = context_course::instance($course2->id); 80 $course3context = context_course::instance($course3->id); 81 82 // Add Category Calendar Events for Category. 83 $this->create_test_standard_calendar_event('category', $user->id, time(), '', $category->id); 84 $this->create_test_standard_calendar_event('category', $user->id, time(), '', $category->id); 85 86 // Add User Calendar Events for User. 87 $this->create_test_standard_calendar_event('user', $user->id, time(), ''); 88 $this->create_test_standard_calendar_event('user', $user->id, time(), '', 0, $course1->id); 89 $this->create_test_standard_calendar_event('user', $user->id, time(), '', 0, $course2->id); 90 91 // Add a Course Calendar Event for Course 1. 92 $this->create_test_standard_calendar_event('course', $user->id, time(), '', 0, $course1->id); 93 94 // Add a Course Assignment Action Calendar Event for Course 2. 95 $generator = $this->getDataGenerator()->get_plugin_generator('mod_assign'); 96 $params['course'] = $course2->id; 97 $params['assignsubmission_onlinetext_enabled'] = 1; 98 $instance = $generator->create_instance($params); 99 $cm = get_coursemodule_from_instance('assign', $instance->id); 100 $modulecontext = context_module::instance($cm->id); 101 $assign = new assign($modulecontext, $cm, $course2); 102 $this->create_test_action_calendar_event('duedate', $course2->id, $instance->id, 'assign', $user->id, time()); 103 $this->create_test_action_calendar_event('gradingduedate', $course2->id, $instance->id, 'assign', $user->id, time()); 104 105 // Add a Calendar Subscription and Group Calendar Event to Course 3. 106 $this->create_test_calendar_subscription('course', 'https://calendar.google.com/', $user->id, 0, $course3->id); 107 $this->create_test_standard_calendar_event('group', $user->id, time(), '', 0, $course3->id, $course3group->id); 108 109 // The user will be in these contexts. 110 $usercontextids = [ 111 $usercontext->id, 112 $categorycontext->id, 113 $course1context->id, 114 $modulecontext->id, 115 $course3context->id 116 ]; 117 // Retrieve the user's context ids. 118 $contextids = provider::get_contexts_for_userid($user->id); 119 120 // Check the user context list and retrieved user context lists contains the same number of records. 121 $this->assertEquals(count($usercontextids), count($contextids->get_contextids())); 122 // There should be no difference between the contexts. 123 $this->assertEmpty(array_diff($usercontextids, $contextids->get_contextids())); 124 } 125 126 /** 127 * Test for provider::export_user_data(). 128 * 129 * @throws coding_exception 130 */ 131 public function test_export_user_data() { 132 global $DB; 133 134 // Create test user to create Calendar Events and Subscriptions with. 135 $user = $this->getDataGenerator()->create_user(); 136 $this->setUser($user); 137 138 // Create a Category to test creating a Category Calendar Event. 139 $category = $this->getDataGenerator()->create_category(); 140 $course1 = $this->getDataGenerator()->create_course(); 141 $course2 = $this->getDataGenerator()->create_course(); 142 $course3 = $this->getDataGenerator()->create_course(); 143 $grouprecord = (object)[ 144 'courseid' => $course3->id, 145 'name' => 'test_group' 146 ]; 147 $course3group = $this->getDataGenerator()->create_group($grouprecord); 148 149 // Add User Calendar Events for User. 150 $event1 = $this->create_test_standard_calendar_event('user', $user->id, time(), ''); 151 152 // Add Category Calendar Events for Category. 153 $event2 = $this->create_test_standard_calendar_event('category', $user->id, time(), '', $category->id); 154 155 // Add two Course Calendar Event for Course 1 and set the same time (1 day a head). 156 $time = strtotime('+1 day', time()); 157 $event3 = $this->create_test_standard_calendar_event('course', $user->id, $time, 'ABC', 0, $course1->id); 158 $event4 = $this->create_test_standard_calendar_event('course', $user->id, $time, 'DEF', 0, $course1->id); 159 160 // Add a Course Assignment Action Calendar Event for Course 2. 161 $generator = $this->getDataGenerator()->get_plugin_generator('mod_assign'); 162 $params['course'] = $course2->id; 163 $params['assignsubmission_onlinetext_enabled'] = 1; 164 $instance = $generator->create_instance($params); 165 $cm = get_coursemodule_from_instance('assign', $instance->id); 166 $modulecontext = context_module::instance($cm->id); 167 $assign = new assign($modulecontext, $cm, $course2); 168 $event5 = $this->create_test_action_calendar_event('duedate', $course2->id, $instance->id, 'assign', $user->id, time()); 169 170 // Add a Calendar Subscription and Group Calendar Event to Course 3. 171 $subscription1 = $this->create_test_calendar_subscription('course', 'https://calendar.google.com/', $user->id, 0, $course3->id); 172 $event6 = $this->create_test_standard_calendar_event('group', $user->id, time(), '', 0, $course3->id, $course3group->id); 173 174 // Retrieve the user's context ids. 175 $contextlist = provider::get_contexts_for_userid($user->id); 176 $approvedcontextlist = new approved_contextlist($user, 'core_calendar', $contextlist->get_contextids()); 177 178 // Retrieve Calendar Event and Subscriptions data only for this user. 179 provider::export_user_data($approvedcontextlist); 180 181 foreach ($contextlist as $context) { 182 $writer = writer::with_context($context); 183 $this->assertTrue($writer->has_any_data()); 184 185 // Test event 1 that was created for the test User. 186 if ($context->instanceid == $user->id && $context->contextlevel == CONTEXT_USER) { 187 // Test the content contains Calendar Event user data. 188 $subcontexts = [ 189 get_string('calendar', 'calendar'), 190 get_string('events', 'calendar'), 191 date('c', $event1->timestart) 192 ]; 193 $name = "user-event"; 194 $data = $writer->get_related_data($subcontexts, $name); 195 $this->assertEquals('Standard Calendar Event user', $data->name); 196 } 197 198 // Test event 2 that was created for the test Category. 199 if ($context->instanceid == $category->id && $context->contextlevel == CONTEXT_COURSECAT) { 200 // Test the content contains Calendar Event category data. 201 $subcontexts = [ 202 get_string('calendar', 'calendar'), 203 get_string('events', 'calendar'), 204 date('c', $event2->timestart) 205 ]; 206 $name = "category-event"; 207 $data = $writer->get_related_data($subcontexts, $name); 208 $this->assertEquals('Standard Calendar Event category', $data->name); 209 } 210 211 // Test events 3, 4, and 5 that were created for the test Course 1. 212 if ($context->instanceid == $course1->id && $context->contextlevel == CONTEXT_COURSE) { 213 // Test the content contains Calendar Event course data set with the same time, and the exported files are uniquely identified. 214 $subcontext1 = [ 215 get_string('calendar', 'calendar'), 216 get_string('events', 'calendar'), 217 date('c', $event3->timestart) 218 ]; 219 $name1 = "course-event-1"; 220 $data1 = $writer->get_related_data($subcontext1, $name1); 221 $this->assertEquals('Standard Calendar Event course -- ABC', $data1->name); 222 223 $subcontext2 = [ 224 get_string('calendar', 'calendar'), 225 get_string('events', 'calendar'), 226 date('c', $event4->timestart) 227 ]; 228 $name2 = "course-event-2"; 229 $data2 = $writer->get_related_data($subcontext2, $name2); 230 $this->assertEquals('Standard Calendar Event course -- DEF', $data2->name); 231 } 232 233 // Test action event that were created for the test Course 2. 234 if ($context->instanceid == $cm->id && $context->contextlevel == CONTEXT_MODULE) { 235 // Test the content contains Calendar Action Event course data. 236 $subcontexts = [ 237 get_string('calendar', 'calendar'), 238 get_string('events', 'calendar'), 239 date('c', $event5->timestart) 240 ]; 241 $name = "duedate-event"; 242 $data = $writer->get_related_data($subcontexts, $name); 243 $this->assertEquals('Action Calendar Event duedate -- assign', $data->name); 244 } 245 246 // Test Calendar Subscription and Event that were created for the test Course 3. 247 if ($context->instanceid == $course3->id && $context->contextlevel == CONTEXT_COURSE) { 248 // Test the content contains Calendar Subscription data also created for the test Course 3. 249 $subcontexts = [ 250 get_string('calendar', 'calendar'), 251 get_string('subscriptions', 'calendar') 252 ]; 253 $name = "course-subscription"; 254 $data = $writer->get_related_data($subcontexts, $name); 255 $this->assertEquals('Calendar Subscription course', $data->name); 256 257 // Test the content contains Calendar Event group data also created for the test Course 3. 258 $subcontexts = [ 259 get_string('calendar', 'calendar'), 260 get_string('events', 'calendar'), 261 date('c', $event6->timestart) 262 ]; 263 $name = "group-event"; 264 $data = $writer->get_related_data($subcontexts, $name); 265 $this->assertEquals('Standard Calendar Event group', $data->name); 266 } 267 } 268 269 } 270 271 /** 272 * Test for provider::test_export_user_preferences(). 273 */ 274 public function test_export_user_preferences() { 275 global $DB; 276 277 // Test setup. 278 $user = $this->getDataGenerator()->create_user(); 279 $this->setUser($user); 280 281 // Add a user home page preference for the User. 282 set_user_preference('calendar_savedflt', 'true', $user); 283 284 // Test the user preference exists. 285 $params = [ 286 'userid' => $user->id, 287 'name' => 'calendar_savedflt' 288 ]; 289 290 // Test the user preferences export contains 1 user preference record for the User. 291 provider::export_user_preferences($user->id); 292 $contextuser = context_user::instance($user->id); 293 $writer = writer::with_context($contextuser); 294 $this->assertTrue($writer->has_any_data()); 295 296 $exportedpreferences = $writer->get_user_preferences('core_calendar'); 297 $this->assertCount(1, (array) $exportedpreferences); 298 $this->assertEquals('true', $exportedpreferences->calendarsavedflt->value); 299 } 300 301 /** 302 * Test for provider::delete_data_for_all_users_in_context(). 303 * 304 * @throws dml_exception 305 */ 306 public function test_delete_data_for_all_users_in_context() { 307 global $DB; 308 309 // Create test user to create Calendar Events and Subscriptions with. 310 $user1 = $this->getDataGenerator()->create_user(); 311 $user2 = $this->getDataGenerator()->create_user(); 312 313 // Create a Course to test creating a Category Calendar Event. 314 $course1 = $this->getDataGenerator()->create_course(); 315 $course2 = $this->getDataGenerator()->create_course(); 316 317 // Get contexts. 318 $course1context = context_course::instance($course1->id); 319 $course2context = context_course::instance($course2->id); 320 321 // Add a Course Calendar Event by User 1 for Course 1 and Course 2. 322 $this->setUser($user1); 323 $this->create_test_standard_calendar_event('course', $user1->id, time(), '', 0, $course1->id); 324 $this->create_test_standard_calendar_event('course', $user1->id, time(), '', 0, $course2->id); 325 326 // Add a Calendar Subscription by User 1 for Course 1. 327 $this->create_test_calendar_subscription('course', 'https://calendar.google.com/', $user1->id, 0, $course1->id); 328 329 // Add a Course Calendar Event by User 2 for Course 1 and Course 2. 330 $this->setUser($user2); 331 $this->create_test_standard_calendar_event('course', $user2->id, time(), '', 0, $course1->id); 332 $this->create_test_standard_calendar_event('course', $user2->id, time(), '', 0, $course2->id); 333 334 // Add a Calendar Subscription by User 2 for Course 2. 335 $this->create_test_calendar_subscription('course', 'https://calendar.google.com/', $user2->id, 0, $course2->id); 336 337 // Add a Course Assignment Action Calendar Event by User 2 for Course 2. 338 $generator = $this->getDataGenerator()->get_plugin_generator('mod_assign'); 339 $params['course'] = $course2->id; 340 $params['assignsubmission_onlinetext_enabled'] = 1; 341 $instance = $generator->create_instance($params); 342 $cm = get_coursemodule_from_instance('assign', $instance->id); 343 $modulecontext = context_module::instance($cm->id); 344 $assign = new assign($modulecontext, $cm, $course2); 345 $this->create_test_action_calendar_event('duedate', $course2->id, $instance->id, 'assign', $user2->id, time()); 346 $this->create_test_action_calendar_event('gradingduedate', $course2->id, $instance->id, 'assign', $user2->id, time()); 347 348 // Delete all Calendar Events for all Users by Context for Course 1. 349 provider::delete_data_for_all_users_in_context($course1context); 350 351 // Verify all Calendar Events for Course 1 were deleted. 352 $events = $DB->get_records('event', array('courseid' => $course1->id)); 353 $this->assertCount(0, $events); 354 // Verify all Calendar Subscriptions for Course 1 were deleted. 355 $subscriptions = $DB->get_records('event_subscriptions', array('courseid' => $course1->id)); 356 $this->assertCount(0, $subscriptions); 357 358 // Verify all Calendar Events for Course 2 exists still. 359 $events = $DB->get_records('event', array('courseid' => $course2->id)); 360 $this->assertCount(4, $events); 361 // Verify all Calendar Subscriptions for Course 2 exists still. 362 $subscriptions = $DB->get_records('event_subscriptions', array('courseid' => $course2->id)); 363 $this->assertCount(1, $subscriptions); 364 365 // Delete all Calendar Events for all Users by Context for Course 2. 366 provider::delete_data_for_all_users_in_context($course2context); 367 368 // Verify all Calendar Events for Course 2 context were deleted. 369 $events = $DB->get_records('event', array('courseid' => $course2->id, 'modulename' => '0')); 370 $this->assertCount(0, $events); 371 // Verify all Calendar Subscriptions for Course 2 were deleted. 372 $subscriptions = $DB->get_records('event_subscriptions', array('courseid' => $course2->id)); 373 $this->assertCount(0, $subscriptions); 374 375 // Verify all Calendar Events for the assignment exists still. 376 $events = $DB->get_records('event', array('modulename' => 'assign')); 377 $this->assertCount(2, $events); 378 379 // Delete all Calendar Events for all Users by Context for the assignment. 380 provider::delete_data_for_all_users_in_context($modulecontext); 381 382 // Verify all Calendar Events for the assignment context were deleted. 383 $events = $DB->get_records('event', array('modulename' => 'assign')); 384 $this->assertCount(0, $events); 385 } 386 387 /** 388 * Test for provider::delete_data_for_user(). 389 * 390 * @throws dml_exception 391 */ 392 public function test_delete_data_for_user() { 393 global $DB; 394 395 // Create test user to create Calendar Events and Subscriptions with. 396 $user1 = $this->getDataGenerator()->create_user(); 397 $user2 = $this->getDataGenerator()->create_user(); 398 399 // Create a Category and Courses to test creating a Category Calendar Event. 400 $category = $this->getDataGenerator()->create_category(); 401 $course1 = $this->getDataGenerator()->create_course(); 402 $course2 = $this->getDataGenerator()->create_course(); 403 404 // Add 5 Calendar Events for User 1 for various contexts. 405 $this->setUser($user1); 406 $this->create_test_standard_calendar_event('user', $user1->id, time(), ''); 407 $this->create_test_standard_calendar_event('site', $user1->id, time(), '', 0, 1); 408 $this->create_test_standard_calendar_event('category', $user1->id, time(), '', $category->id); 409 $this->create_test_standard_calendar_event('course', $user1->id, time(), '', 0, $course1->id); 410 $this->create_test_standard_calendar_event('course', $user1->id, time(), '', 0, $course2->id); 411 412 // Add 1 Calendar Subscription for User 1 at course context. 413 $this->create_test_calendar_subscription('course', 'https://calendar.google.com/', $user1->id, 0, $course2->id); 414 415 // Add 3 Calendar Events for User 2 for various contexts. 416 $this->setUser($user2); 417 $this->create_test_standard_calendar_event('user', $user2->id, time(), ''); 418 $this->create_test_standard_calendar_event('category', $user2->id, time(), '', $category->id); 419 $this->create_test_standard_calendar_event('course', $user2->id, time(), '', 0, $course1->id); 420 421 // Add 1 Calendar Subscription for User 2 at course context. 422 $this->create_test_calendar_subscription('course', 'https://calendar.google.com/', $user2->id, 0, $course2->id); 423 424 // Retrieve the user's context ids. 425 $contextlist = provider::get_contexts_for_userid($user1->id); 426 $approvedcontextlist = new approved_contextlist($user1, 'core_calendar', $contextlist->get_contextids()); 427 428 // Delete all Calendar data for User 1. 429 provider::delete_data_for_user($approvedcontextlist); 430 431 // Test all Calendar Events and Subscriptions for User 1 equals zero. 432 $events = $DB->get_records('event', ['userid' => $user1->id]); 433 $this->assertCount(0, $events); 434 $eventsubscriptions = $DB->get_records('event_subscriptions', ['userid' => $user1->id]); 435 $this->assertCount(0, $eventsubscriptions); 436 437 // Test all Calendar Events and Subscriptions for User 2 still exists and matches the same number created. 438 $events = $DB->get_records('event', ['userid' => $user2->id]); 439 $this->assertCount(3, $events); 440 $eventsubscriptions = $DB->get_records('event_subscriptions', ['userid' => $user2->id]); 441 $this->assertCount(1, $eventsubscriptions); 442 } 443 444 /** 445 * Test that only users with a user context are fetched. 446 */ 447 public function test_get_users_in_context() { 448 $component = 'core_calendar'; 449 450 // Create user1 to create Calendar Events and Subscriptions. 451 $user1 = $this->getDataGenerator()->create_user(); 452 $usercontext1 = context_user::instance($user1->id); 453 // Create user2 to create Calendar Events and Subscriptions. 454 $user2 = $this->getDataGenerator()->create_user(); 455 $usercontext2 = context_user::instance($user2->id); 456 // Create user3 to create Calendar Events and Subscriptions. 457 $user3 = $this->getDataGenerator()->create_user(); 458 $usercontext3 = context_user::instance($user3->id); 459 460 // Create a Category and Courses to assign Calendar Events and Subscriptions. 461 $category = $this->getDataGenerator()->create_category(); 462 $categorycontext = context_coursecat::instance($category->id); 463 $course1 = $this->getDataGenerator()->create_course(); 464 $course1context = context_course::instance($course1->id); 465 $course2 = $this->getDataGenerator()->create_course(); 466 $course2context = context_course::instance($course2->id); 467 $course3 = $this->getDataGenerator()->create_course(); 468 $course3context = context_course::instance($course3->id); 469 $grouprecord = (object)[ 470 'courseid' => $course3->id, 471 'name' => 'test_group' 472 ]; 473 $course3group = $this->getDataGenerator()->create_group($grouprecord); 474 475 // Add Category Calendar Events for Category. 476 $this->setUser($user1); 477 $this->create_test_standard_calendar_event('category', $user1->id, time(), '', 478 $category->id); 479 $this->setUser($user2); 480 $this->create_test_standard_calendar_event('category', $user2->id, time(), '', 481 $category->id); 482 483 // Add User Calendar Events for user1 and user2. 484 $this->setUser($user1); 485 $this->create_test_standard_calendar_event('user', $user1->id, time(), ''); 486 $this->create_test_standard_calendar_event('user', $user1->id, time(), '', 487 0, $course1->id); 488 $this->create_test_standard_calendar_event('user', $user1->id, time(), '', 489 0, $course2->id); 490 $this->setUser($user2); 491 $this->create_test_standard_calendar_event('user', $user2->id, time(), '', 492 0, $course1->id); 493 494 // Add a Course Calendar Events for Course 1. 495 $this->setUser($user1); 496 $this->create_test_standard_calendar_event('course', $user1->id, time(), '', 497 0, $course1->id); 498 $this->setUser($user2); 499 $this->create_test_standard_calendar_event('course', $user2->id, time(), '', 500 0, $course1->id); 501 502 // Add a Course Assignment Action Calendar Event for Course 2. 503 $generator = $this->getDataGenerator()->get_plugin_generator('mod_assign'); 504 $params['course'] = $course2->id; 505 $params['assignsubmission_onlinetext_enabled'] = 1; 506 $instance = $generator->create_instance($params); 507 $cm = get_coursemodule_from_instance('assign', $instance->id); 508 $modulecontext = context_module::instance($cm->id); 509 $assign = new assign($modulecontext, $cm, $course2); 510 $this->setUser($user2); 511 $this->create_test_action_calendar_event('duedate', $course2->id, $instance->id, 512 'assign', $user2->id, time()); 513 $this->create_test_action_calendar_event('gradingduedate', $course2->id, $instance->id, 514 'assign', $user2->id, time()); 515 516 // Add a Calendar Subscription and Group Calendar Event to Course 3. 517 $this->create_test_standard_calendar_event('group', $user2->id, time(), '', 0, 518 $course3->id, $course3group->id); 519 $this->setUser($user3); 520 $this->create_test_calendar_subscription('course', 'https://calendar.google.com/', $user3->id, 521 0, $course3->id); 522 523 // The user list for usercontext1 should return user1. 524 $userlist1 = new \core_privacy\local\request\userlist($usercontext1, $component); 525 provider::get_users_in_context($userlist1); 526 $this->assertCount(1, $userlist1); 527 $this->assertTrue(in_array($user1->id, $userlist1->get_userids())); 528 // The user list for usercontext2 should return user2. 529 $userlist2 = new \core_privacy\local\request\userlist($usercontext2, $component); 530 provider::get_users_in_context($userlist2); 531 $this->assertCount(1, $userlist2); 532 $this->assertTrue(in_array($user2->id, $userlist2->get_userids())); 533 // The user list for course1context should return user1 and user2. 534 $userlist3 = new \core_privacy\local\request\userlist($course1context, $component); 535 provider::get_users_in_context($userlist3); 536 $this->assertCount(2, $userlist3); 537 $this->assertTrue(in_array($user1->id, $userlist3->get_userids())); 538 $this->assertTrue(in_array($user2->id, $userlist3->get_userids())); 539 // The user list for course2context should not return any users. 540 $userlist4 = new \core_privacy\local\request\userlist($course2context, $component); 541 provider::get_users_in_context($userlist4); 542 $this->assertCount(0, $userlist4); 543 // The user list for course3context should return user2 and user3. 544 $userlist5 = new \core_privacy\local\request\userlist($course3context, $component); 545 provider::get_users_in_context($userlist5); 546 $this->assertCount(2, $userlist5); 547 $this->assertTrue(in_array($user2->id, $userlist5->get_userids())); 548 $this->assertTrue(in_array($user3->id, $userlist5->get_userids())); 549 // The user list for categorycontext should return user1 and user2. 550 $userlist6 = new \core_privacy\local\request\userlist($categorycontext, $component); 551 provider::get_users_in_context($userlist6); 552 $this->assertCount(2, $userlist6); 553 $this->assertTrue(in_array($user1->id, $userlist6->get_userids())); 554 $this->assertTrue(in_array($user2->id, $userlist6->get_userids())); 555 // The user list for modulecontext should return user2. 556 $userlist7 = new \core_privacy\local\request\userlist($modulecontext, $component); 557 provider::get_users_in_context($userlist7); 558 $this->assertCount(1, $userlist7); 559 $this->assertTrue(in_array($user2->id, $userlist7->get_userids())); 560 // The user list for usercontext3 should not return any users. 561 $userlist8 = new \core_privacy\local\request\userlist($usercontext3, $component); 562 provider::get_users_in_context($userlist8); 563 $this->assertCount(0, $userlist8); 564 } 565 566 /** 567 * Test that data for users in approved userlist is deleted. 568 */ 569 public function test_delete_data_for_users() { 570 $component = 'core_calendar'; 571 572 // Create user1 to create Calendar Events and Subscriptions. 573 $user1 = $this->getDataGenerator()->create_user(); 574 $usercontext1 = context_user::instance($user1->id); 575 // Create user2 to create Calendar Events and Subscriptions. 576 $user2 = $this->getDataGenerator()->create_user(); 577 $usercontext2 = context_user::instance($user2->id); 578 // Create user3 to create Calendar Events and Subscriptions. 579 $user3 = $this->getDataGenerator()->create_user(); 580 $usercontext3 = context_user::instance($user3->id); 581 582 // Create a Category and Courses to assign Calendar Events and Subscriptions. 583 $category = $this->getDataGenerator()->create_category(); 584 $categorycontext = context_coursecat::instance($category->id); 585 $course1 = $this->getDataGenerator()->create_course(); 586 $course1context = context_course::instance($course1->id); 587 $course2 = $this->getDataGenerator()->create_course(); 588 $course2context = context_course::instance($course2->id); 589 $course3 = $this->getDataGenerator()->create_course(); 590 $course3context = context_course::instance($course3->id); 591 $grouprecord = (object)[ 592 'courseid' => $course3->id, 593 'name' => 'test_group' 594 ]; 595 $course3group = $this->getDataGenerator()->create_group($grouprecord); 596 597 // Add Category Calendar Events for Category. 598 $this->setUser($user1); 599 $this->create_test_standard_calendar_event('category', $user1->id, time(), '', 600 $category->id); 601 $this->setUser($user2); 602 $this->create_test_standard_calendar_event('category', $user2->id, time(), '', 603 $category->id); 604 605 // Add User Calendar Events for user1 and user2. 606 $this->setUser($user1); 607 $this->create_test_standard_calendar_event('user', $user1->id, time(), ''); 608 $this->create_test_standard_calendar_event('user', $user1->id, time(), '', 609 0, $course1->id); 610 $this->create_test_standard_calendar_event('user', $user1->id, time(), '', 611 0, $course2->id); 612 $this->setUser($user2); 613 $this->create_test_standard_calendar_event('user', $user2->id, time(), '', 614 0, $course1->id); 615 616 // Add a Course Calendar Events for Course 1. 617 $this->setUser($user1); 618 $this->create_test_standard_calendar_event('course', $user1->id, time(), '', 619 0, $course1->id); 620 $this->setUser($user2); 621 $this->create_test_standard_calendar_event('course', $user2->id, time(), '', 622 0, $course1->id); 623 624 // Add a Course Assignment Action Calendar Event for Course 2. 625 $generator = $this->getDataGenerator()->get_plugin_generator('mod_assign'); 626 $params['course'] = $course2->id; 627 $params['assignsubmission_onlinetext_enabled'] = 1; 628 $instance = $generator->create_instance($params); 629 $cm = get_coursemodule_from_instance('assign', $instance->id); 630 $modulecontext = context_module::instance($cm->id); 631 $assign = new assign($modulecontext, $cm, $course2); 632 $this->setUser($user2); 633 $this->create_test_action_calendar_event('duedate', $course2->id, $instance->id, 634 'assign', $user2->id, time()); 635 $this->create_test_action_calendar_event('gradingduedate', $course2->id, $instance->id, 636 'assign', $user2->id, time()); 637 638 // Add a Calendar Subscription and Group Calendar Event to Course 3. 639 $this->create_test_standard_calendar_event('group', $user2->id, time(), '', 0, 640 $course3->id, $course3group->id); 641 $this->setUser($user3); 642 $this->create_test_calendar_subscription('course', 'https://calendar.google.com/', $user3->id, 643 0, $course3->id); 644 645 // The user list for usercontext1 should return user1. 646 $userlist1 = new \core_privacy\local\request\userlist($usercontext1, $component); 647 provider::get_users_in_context($userlist1); 648 $this->assertCount(1, $userlist1); 649 // The user list for usercontext2 should return user2. 650 $userlist2 = new \core_privacy\local\request\userlist($usercontext2, $component); 651 provider::get_users_in_context($userlist2); 652 $this->assertCount(1, $userlist2); 653 // The user list for course1context should return user1 and user2. 654 $userlist3 = new \core_privacy\local\request\userlist($course1context, $component); 655 provider::get_users_in_context($userlist3); 656 $this->assertCount(2, $userlist3); 657 // The user list for course2context should not return any users. 658 $userlist4 = new \core_privacy\local\request\userlist($course2context, $component); 659 provider::get_users_in_context($userlist4); 660 $this->assertCount(0, $userlist4); 661 // The user list for course3context should return user2 and user3. 662 $userlist5 = new \core_privacy\local\request\userlist($course3context, $component); 663 provider::get_users_in_context($userlist5); 664 $this->assertCount(2, $userlist5); 665 // The user list for categorycontext should return user1 and user2. 666 $userlist6 = new \core_privacy\local\request\userlist($categorycontext, $component); 667 provider::get_users_in_context($userlist6); 668 $this->assertCount(2, $userlist6); 669 // The user list for modulecontext should return user2. 670 $userlist7 = new \core_privacy\local\request\userlist($modulecontext, $component); 671 provider::get_users_in_context($userlist7); 672 $this->assertCount(1, $userlist7); 673 // The user list for usercontext3 should not return any users. 674 $userlist8 = new \core_privacy\local\request\userlist($usercontext3, $component); 675 provider::get_users_in_context($userlist8); 676 $this->assertCount(0, $userlist8); 677 678 // Convert $userlist1 into an approved_contextlist. 679 $approvedlist1 = new approved_userlist($usercontext1, $component, $userlist1->get_userids()); 680 // Delete using delete_data_for_user. 681 provider::delete_data_for_users($approvedlist1); 682 // The user list for usercontext1 should not return any users. 683 $userlist1 = new \core_privacy\local\request\userlist($usercontext1, $component); 684 provider::get_users_in_context($userlist1); 685 $this->assertCount(0, $userlist1); 686 // The user list for usercontext2 should still return users2. 687 $userlist2 = new \core_privacy\local\request\userlist($usercontext2, $component); 688 provider::get_users_in_context($userlist2); 689 $this->assertCount(1, $userlist2); 690 691 // Convert $userlist3 into an approved_contextlist. 692 // Pass an empty array as a value for the approved user list. 693 $approvedlist2 = new approved_userlist($course1context, $component, []); 694 // Delete using delete_data_for_user. 695 provider::delete_data_for_users($approvedlist2); 696 // The user list for course1context should return user1 and user2. 697 $userlist3 = new \core_privacy\local\request\userlist($course1context, $component); 698 provider::get_users_in_context($userlist3); 699 $this->assertCount(2, $userlist3); 700 $this->assertTrue(in_array($user1->id, $userlist3->get_userids())); 701 $this->assertTrue(in_array($user2->id, $userlist3->get_userids())); 702 703 // Convert $userlist3 into an approved_contextlist. 704 // Pass the ID of user1 as a value for the approved user list. 705 $approvedlist2 = new approved_userlist($course1context, $component, [$user1->id]); 706 // Delete using delete_data_for_user. 707 provider::delete_data_for_users($approvedlist2); 708 // The user list for course1context should return user2. 709 $userlist3 = new \core_privacy\local\request\userlist($course1context, $component); 710 provider::get_users_in_context($userlist3); 711 $this->assertCount(1, $userlist3); 712 $this->assertTrue(in_array($user2->id, $userlist3->get_userids())); 713 714 // The user list for course3context should still return user2 and user3. 715 $userlist5 = new \core_privacy\local\request\userlist($course3context, $component); 716 provider::get_users_in_context($userlist5); 717 $this->assertCount(2, $userlist5); 718 719 // Convert $userlist6 into an approved_contextlist. 720 $approvedlist3 = new approved_userlist($categorycontext, $component, $userlist6->get_userids()); 721 // Delete using delete_data_for_user. 722 provider::delete_data_for_users($approvedlist3); 723 // The user list for categorycontext should not return any users. 724 $userlist6 = new \core_privacy\local\request\userlist($categorycontext, $component); 725 provider::get_users_in_context($userlist6); 726 $this->assertCount(0, $userlist6); 727 728 // Convert $userlist7 into an approved_contextlist. 729 $approvedlist4 = new approved_userlist($modulecontext, $component, $userlist7->get_userids()); 730 // Delete using delete_data_for_user. 731 provider::delete_data_for_users($approvedlist4); 732 // The user list for modulecontext should not return any users. 733 $userlist7 = new \core_privacy\local\request\userlist($modulecontext, $component); 734 provider::get_users_in_context($userlist7); 735 $this->assertCount(0, $userlist7); 736 } 737 738 // Start of helper functions. 739 740 /** 741 * Helper function to create a Standard Calendar Event. 742 * 743 * @param string $eventtype Calendar event type 744 * @param int $userid User Id 745 * @param int $time Timestamp value 746 * @param string $customname Custom name 747 * @param int $categoryid Course Category Id 748 * @param int $courseid Course Id 749 * @param int $groupid Group Id 750 * @return bool|calendar_event Standard Calendar Event created. 751 * @throws coding_exception 752 */ 753 protected function create_test_standard_calendar_event($eventtype, $userid, $time, $customname = '', $categoryid = 0, $courseid = 0, $groupid = 0) { 754 // Create a standard calendar event. 755 $name = "Standard Calendar Event $eventtype"; 756 if ($customname != '') { 757 $name .= " -- $customname"; 758 } 759 760 $event = (object)[ 761 'name' => $name, 762 'categoryid' => $categoryid, 763 'courseid' => $courseid, 764 'groupid' => $groupid, 765 'userid' => $userid, 766 'modulename' => 0, 767 'instance' => 0, 768 'eventtype' => $eventtype, 769 'type' => CALENDAR_EVENT_TYPE_STANDARD, 770 'timestart' => $time, 771 'visible' => 1 772 ]; 773 return calendar_event::create($event, false); 774 } 775 776 /** 777 * Helper function to create an Action Calendar Event. 778 * 779 * @param string $eventtype Calendar event type 780 * @param int $courseid Course Id 781 * @param int $instanceid Activity Module instance id 782 * @param string $modulename Activity Module name 783 * @param int $userid User Id 784 * @param int $time Timestamp value 785 * @return bool|calendar_event Action Calendar Event created. 786 * @throws coding_exception 787 */ 788 protected function create_test_action_calendar_event($eventtype, $courseid, $instanceid, $modulename, $userid, $time) { 789 // Create an action calendar event. 790 $event = (object)[ 791 'name' => "Action Calendar Event $eventtype -- $modulename", 792 'categoryid' => 0, 793 'courseid' => $courseid, 794 'groupid' => 0, 795 'userid' => $userid, 796 'modulename' => $modulename, 797 'instance' => $instanceid, 798 'eventtype' => $eventtype, 799 'type' => CALENDAR_EVENT_TYPE_ACTION, 800 'timestart' => $time, 801 'visible' => 1 802 ]; 803 return calendar_event::create($event, false); 804 } 805 806 /** 807 * Helper function to create a Calendar Subscription. 808 * 809 * @param string $eventtype Calendar Subscription event type 810 * @param string $url Calendar Subscription URL 811 * @param int $userid User Id 812 * @param int $categoryid Category Id 813 * @param int $courseid Course Id 814 * @param int $groupid Group Id 815 * @return int Calendar Subscription Id 816 */ 817 protected function create_test_calendar_subscription($eventtype, $url, $userid, $categoryid = 0, $courseid = 0, $groupid = 0) { 818 // Create a subscription calendar event. 819 $subscription = (object)[ 820 'name' => "Calendar Subscription " . $eventtype, 821 'url' => $url, 822 'categoryid' => $categoryid, 823 'courseid' => $courseid, 824 'groupid' => $groupid, 825 'userid' => $userid, 826 'eventtype' => $eventtype 827 ]; 828 829 return calendar_add_subscription($subscription); 830 } 831 832 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body