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