Differences Between: [Versions 310 and 311] [Versions 39 and 311]
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 namespace core_calendar; 18 19 use core_calendar\local\event\strategies\raw_event_retrieval_strategy; 20 21 defined('MOODLE_INTERNAL') || die(); 22 23 global $CFG; 24 require_once($CFG->dirroot . '/calendar/tests/helpers.php'); 25 26 /** 27 * Raw event retrieval strategy tests. 28 * 29 * @package core_calendar 30 * @copyright 2017 Cameron Ball <cameron@cameron1729.xyz> 31 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 32 */ 33 class raw_event_retrieval_strategy_test extends \advanced_testcase { 34 /** 35 * Test retrieval strategy when module is disabled. 36 */ 37 public function test_get_raw_events_with_disabled_module() { 38 global $DB; 39 40 $this->resetAfterTest(); 41 $retrievalstrategy = new raw_event_retrieval_strategy(); 42 $generator = $this->getDataGenerator(); 43 $course = $generator->create_course(); 44 $student = $generator->create_user(); 45 $generator->enrol_user($student->id, $course->id, 'student'); 46 $this->setUser($student); 47 $events = [ 48 [ 49 'name' => 'Start of assignment', 50 'description' => '', 51 'location' => 'Test', 52 'format' => 1, 53 'courseid' => $course->id, 54 'groupid' => 0, 55 'userid' => 2, 56 'modulename' => 'assign', 57 'instance' => 1, 58 'eventtype' => 'due', 59 'timestart' => time(), 60 'timeduration' => 86400, 61 'visible' => 1 62 ], [ 63 'name' => 'Start of lesson', 64 'description' => '', 65 'location' => 'Test', 66 'format' => 1, 67 'courseid' => $course->id, 68 'groupid' => 0, 69 'userid' => 2, 70 'modulename' => 'lesson', 71 'instance' => 1, 72 'eventtype' => 'end', 73 'timestart' => time(), 74 'timeduration' => 86400, 75 'visible' => 1 76 ] 77 ]; 78 79 foreach ($events as $event) { 80 \calendar_event::create($event, false); 81 } 82 83 // Get all events. 84 $events = $retrievalstrategy->get_raw_events(null, [0], null); 85 $this->assertCount(2, $events); 86 87 // Disable the lesson module. 88 $DB->set_field('modules', 'visible', 0, ['name' => 'lesson']); 89 90 // Check that we only return the assign event. 91 $events = $retrievalstrategy->get_raw_events(null, [0], null); 92 $this->assertCount(1, $events); 93 $event = reset($events); 94 $this->assertEquals('assign', $event->modulename); 95 96 // Now, log out and repeat the above test in the reverse order. 97 $this->setUser(); 98 99 // Check that we only return the assign event (given that the lesson module is still disabled). 100 $events = $retrievalstrategy->get_raw_events([$student->id], [0], null); 101 $this->assertCount(1, $events); 102 $event = reset($events); 103 $this->assertEquals('assign', $event->modulename); 104 105 // Enable the lesson module. 106 $DB->set_field('modules', 'visible', 1, ['name' => 'lesson']); 107 108 // Get all events. 109 $events = $retrievalstrategy->get_raw_events(null, [0], null); 110 $this->assertCount(2, $events); 111 } 112 113 /** 114 * Test retrieval strategy when there are overrides. 115 */ 116 public function test_get_raw_event_strategy_with_overrides() { 117 $this->resetAfterTest(); 118 119 $retrievalstrategy = new raw_event_retrieval_strategy(); 120 $generator = $this->getDataGenerator(); 121 $course = $generator->create_course(); 122 $plugingenerator = $this->getDataGenerator()->get_plugin_generator('mod_assign'); 123 124 $instance = $plugingenerator->create_instance(['course' => $course->id]); 125 126 // Create users. 127 $useroverridestudent = $generator->create_user(); 128 $group1student = $generator->create_user(); 129 $group2student = $generator->create_user(); 130 $group12student = $generator->create_user(); 131 $nogroupstudent = $generator->create_user(); 132 133 // Enrol users. 134 $generator->enrol_user($useroverridestudent->id, $course->id, 'student'); 135 $generator->enrol_user($group1student->id, $course->id, 'student'); 136 $generator->enrol_user($group2student->id, $course->id, 'student'); 137 $generator->enrol_user($group12student->id, $course->id, 'student'); 138 139 $generator->enrol_user($nogroupstudent->id, $course->id, 'student'); 140 141 // Create groups. 142 $group1 = $generator->create_group(['courseid' => $course->id, 'name' => 'Group 1']); 143 $group2 = $generator->create_group(['courseid' => $course->id, 'name' => 'Group 2']); 144 145 // Add members to groups. 146 $generator->create_group_member(['groupid' => $group1->id, 'userid' => $group1student->id]); 147 $generator->create_group_member(['groupid' => $group2->id, 'userid' => $group2student->id]); 148 $generator->create_group_member(['groupid' => $group1->id, 'userid' => $group12student->id]); 149 $generator->create_group_member(['groupid' => $group2->id, 'userid' => $group12student->id]); 150 151 $now = time(); 152 153 // Events with the same module name, instance and event type. 154 $events = [ 155 [ 156 'name' => 'Assignment 1 due date', 157 'description' => '', 158 'location' => 'Test', 159 'format' => 0, 160 'courseid' => $course->id, 161 'groupid' => 0, 162 'userid' => 2, 163 'modulename' => 'assign', 164 'instance' => $instance->id, 165 'eventtype' => 'due', 166 'timestart' => $now, 167 'timeduration' => 0, 168 'visible' => 1 169 ], [ 170 'name' => 'Assignment 1 due date - User override', 171 'description' => '', 172 'location' => 'Test', 173 'format' => 1, 174 'courseid' => 0, 175 'groupid' => 0, 176 'userid' => $useroverridestudent->id, 177 'modulename' => 'assign', 178 'instance' => $instance->id, 179 'eventtype' => 'due', 180 'timestart' => $now + 86400, 181 'timeduration' => 0, 182 'visible' => 1, 183 'priority' => CALENDAR_EVENT_USER_OVERRIDE_PRIORITY 184 ], [ 185 'name' => 'Assignment 1 due date - Group A override', 186 'description' => '', 187 'location' => 'Test', 188 'format' => 1, 189 'courseid' => $course->id, 190 'groupid' => $group1->id, 191 'userid' => 2, 192 'modulename' => 'assign', 193 'instance' => $instance->id, 194 'eventtype' => 'due', 195 'timestart' => $now + (2 * 86400), 196 'timeduration' => 0, 197 'visible' => 1, 198 'priority' => 1, 199 ], [ 200 'name' => 'Assignment 1 due date - Group B override', 201 'description' => '', 202 'location' => 'Test', 203 'format' => 1, 204 'courseid' => $course->id, 205 'groupid' => $group2->id, 206 'userid' => 2, 207 'modulename' => 'assign', 208 'instance' => $instance->id, 209 'eventtype' => 'due', 210 'timestart' => $now + (3 * 86400), 211 'timeduration' => 0, 212 'visible' => 1, 213 'priority' => 2, 214 ], 215 ]; 216 217 foreach ($events as $event) { 218 \calendar_event::create($event, false); 219 } 220 221 $groups = [$group1->id, $group2->id]; 222 223 // Do the following tests multiple times when logged in with different users. Also run the whole set when logged out. 224 // In any cases, the tests should not depend on the logged-in user. 225 foreach ([$useroverridestudent, $nogroupstudent, $group12student, $group1student, null] as $login) { 226 $this->setUser($login); 227 228 // Get user override events. 229 $events = $retrievalstrategy->get_raw_events([$useroverridestudent->id], $groups, [$course->id]); 230 $this->assertCount(1, $events); 231 $event = reset($events); 232 $this->assertEquals('Assignment 1 due date - User override', $event->name); 233 234 // Get events for user that does not belong to any group and has no user override events. 235 $events = $retrievalstrategy->get_raw_events([$nogroupstudent->id], $groups, [$course->id]); 236 $this->assertCount(1, $events); 237 $event = reset($events); 238 $this->assertEquals('Assignment 1 due date', $event->name); 239 240 // Get events for user that belongs to groups A and B and has no user override events. 241 $events = $retrievalstrategy->get_raw_events([$group12student->id], $groups, [$course->id]); 242 $this->assertCount(1, $events); 243 $event = reset($events); 244 $this->assertEquals('Assignment 1 due date - Group A override', $event->name); 245 246 // Get events for user that belongs to group A and has no user override events. 247 $events = $retrievalstrategy->get_raw_events([$group1student->id], $groups, [$course->id]); 248 $this->assertCount(1, $events); 249 $event = reset($events); 250 $this->assertEquals('Assignment 1 due date - Group A override', $event->name); 251 } 252 253 // Add repeating events. 254 $repeatingevents = [ 255 [ 256 'name' => 'Repeating site event', 257 'description' => '', 258 'location' => 'Test', 259 'format' => 1, 260 'courseid' => SITEID, 261 'groupid' => 0, 262 'userid' => 2, 263 'repeatid' => 1, 264 'modulename' => '0', 265 'instance' => 0, 266 'eventtype' => 'site', 267 'timestart' => $now + 86400, 268 'timeduration' => 0, 269 'visible' => 1, 270 ], 271 [ 272 'name' => 'Repeating site event', 273 'description' => '', 274 'location' => 'Test', 275 'format' => 1, 276 'courseid' => SITEID, 277 'groupid' => 0, 278 'userid' => 2, 279 'repeatid' => 1, 280 'modulename' => '0', 281 'instance' => 0, 282 'eventtype' => 'site', 283 'timestart' => $now + (2 * 86400), 284 'timeduration' => 0, 285 'visible' => 1, 286 ], 287 ]; 288 289 foreach ($repeatingevents as $event) { 290 \calendar_event::create($event, false); 291 } 292 293 // Make sure repeating events are not filtered out. 294 $events = $retrievalstrategy->get_raw_events(); 295 $this->assertCount(3, $events); 296 } 297 298 /** 299 * Test retrieval strategy with category specifications. 300 */ 301 public function test_get_raw_events_category() { 302 $this->resetAfterTest(); 303 $retrievalstrategy = new raw_event_retrieval_strategy(); 304 $generator = $this->getDataGenerator(); 305 $category1 = $generator->create_category(); 306 $category2 = $generator->create_category(); 307 $events = [ 308 [ 309 'name' => 'E1', 310 'eventtype' => 'category', 311 'description' => '', 312 'location' => 'Test', 313 'format' => 1, 314 'categoryid' => $category1->id, 315 'userid' => 2, 316 'timestart' => time(), 317 ], 318 [ 319 'name' => 'E2', 320 'eventtype' => 'category', 321 'description' => '', 322 'location' => 'Test', 323 'format' => 1, 324 'categoryid' => $category2->id, 325 'userid' => 2, 326 'timestart' => time() + 1, 327 ], 328 ]; 329 330 foreach ($events as $event) { 331 \calendar_event::create($event, false); 332 } 333 334 // Get all events. 335 $events = $retrievalstrategy->get_raw_events(null, null, null, null); 336 $this->assertCount(2, $events); 337 338 $event = array_shift($events); 339 $this->assertEquals('E1', $event->name); 340 $event = array_shift($events); 341 $this->assertEquals('E2', $event->name); 342 343 // Get events for C1 events. 344 $events = $retrievalstrategy->get_raw_events(null, null, null, [$category1->id]); 345 $this->assertCount(1, $events); 346 347 $event = array_shift($events); 348 $this->assertEquals('E1', $event->name); 349 350 // Get events for C2 events. 351 $events = $retrievalstrategy->get_raw_events(null, null, null, [$category2->id]); 352 $this->assertCount(1, $events); 353 354 $event = array_shift($events); 355 $this->assertEquals('E2', $event->name); 356 357 // Get events for several categories. 358 $events = $retrievalstrategy->get_raw_events(null, null, null, [$category1->id, $category2->id]); 359 $this->assertCount(2, $events); 360 } 361 362 public function test_get_raw_events_for_multiple_users() { 363 $this->resetAfterTest(); 364 365 $generator = $this->getDataGenerator(); 366 367 // Create users. 368 $user1 = $generator->create_user(); 369 $user2 = $generator->create_user(); 370 $user3 = $generator->create_user(); 371 372 // Create user events. 373 $events = [ 374 [ 375 'name' => 'User1 Event', 376 'eventtype' => 'user', 377 'userid' => $user1->id, 378 'timestart' => time(), 379 ], [ 380 'name' => 'User2 Event', 381 'eventtype' => 'user', 382 'userid' => $user2->id, 383 'timestart' => time(), 384 ], [ 385 'name' => 'User3 Event', 386 'eventtype' => 'user', 387 'userid' => $user3->id, 388 'timestart' => time(), 389 ] 390 ]; 391 foreach ($events as $event) { 392 \calendar_event::create($event, false); 393 } 394 395 $retrievalstrategy = new raw_event_retrieval_strategy(); 396 397 // Get all events. 398 $events = $retrievalstrategy->get_raw_events([$user1->id, $user2->id]); 399 $this->assertCount(2, $events); 400 $this->assertEqualsCanonicalizing( 401 ['User1 Event', 'User2 Event'], 402 array_column($events, 'name')); 403 } 404 405 public function test_get_raw_events_for_groups_with_no_members() { 406 $this->resetAfterTest(); 407 408 $generator = $this->getDataGenerator(); 409 410 $course = $generator->create_course(); 411 412 // Create groups. 413 $group1 = $generator->create_group(['courseid' => $course->id, 'name' => 'Group 1']); 414 $group2 = $generator->create_group(['courseid' => $course->id, 'name' => 'Group 2']); 415 416 // Create group events. 417 $events = [ 418 [ 419 'name' => 'Group 1 Event', 420 'eventtype' => 'group', 421 'groupid' => $group1->id, 422 'timestart' => time(), 423 ], [ 424 'name' => 'Group 2 Event', 425 'eventtype' => 'group', 426 'groupid' => $group2->id, 427 'timestart' => time(), 428 ] 429 ]; 430 foreach ($events as $event) { 431 \calendar_event::create($event, false); 432 } 433 434 $retrievalstrategy = new raw_event_retrieval_strategy; 435 436 // Get group eventsl. 437 $events = $retrievalstrategy->get_raw_events(null, [$group1->id, $group2->id]); 438 $this->assertCount(2, $events); 439 $this->assertEqualsCanonicalizing( 440 ['Group 1 Event', 'Group 2 Event'], 441 array_column($events, 'name')); 442 } 443 444 /** 445 * Test retrieval strategy with empty filters. 446 * This covers a edge case not covered elsewhere to ensure its SQL is cross 447 * db compatible. The test is ensuring we don't get a DML Exception with 448 * the filters setup this way. 449 */ 450 public function test_get_raw_events_with_empty_user_and_category_lists() { 451 $retrievalstrategy = new raw_event_retrieval_strategy; 452 $retrievalstrategy->get_raw_events([], null, null, []); 453 } 454 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body