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