MDL-57503 calendar: add api function for action events by courses
[moodle.git] / calendar / tests / api_test.php
CommitLineData
3607961d
DP
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/**
12cbce0a 18 * Contains the class containing unit tests for the calendar API.
3607961d
DP
19 *
20 * @package core_calendar
12cbce0a 21 * @copyright 2017 Mark Nelson <markn@moodle.com>
3607961d
DP
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
24
25defined('MOODLE_INTERNAL') || die();
3607961d 26
1575194a
RW
27require_once(__DIR__ . '/helpers.php');
28
29use \core_calendar\api;
30
3607961d 31/**
12cbce0a 32 * Class contaning unit tests for the calendar API.
3607961d
DP
33 *
34 * @package core_calendar
12cbce0a 35 * @copyright 2017 Mark Nelson <markn@moodle.com>
3607961d
DP
36 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
37 */
12cbce0a 38class core_calendar_api_testcase extends advanced_testcase {
3607961d 39
12cbce0a
MN
40 /**
41 * Tests set up
42 */
b8ad1728 43 protected function setUp() {
12cbce0a 44 $this->resetAfterTest();
b8ad1728 45 }
3607961d 46
12cbce0a 47 public function test_get_course_cached() {
3607961d
DP
48 // Setup some test courses.
49 $course1 = $this->getDataGenerator()->create_course();
50 $course2 = $this->getDataGenerator()->create_course();
51 $course3 = $this->getDataGenerator()->create_course();
52
53 // Load courses into cache.
54 $coursecache = null;
12cbce0a
MN
55 \core_calendar\api::get_course_cached($coursecache, $course1->id);
56 \core_calendar\api::get_course_cached($coursecache, $course2->id);
57 \core_calendar\api::get_course_cached($coursecache, $course3->id);
3607961d
DP
58
59 // Verify the cache.
60 $this->assertArrayHasKey($course1->id, $coursecache);
61 $cachedcourse1 = $coursecache[$course1->id];
62 $this->assertEquals($course1->id, $cachedcourse1->id);
63 $this->assertEquals($course1->shortname, $cachedcourse1->shortname);
64 $this->assertEquals($course1->fullname, $cachedcourse1->fullname);
65
66 $this->assertArrayHasKey($course2->id, $coursecache);
67 $cachedcourse2 = $coursecache[$course2->id];
68 $this->assertEquals($course2->id, $cachedcourse2->id);
69 $this->assertEquals($course2->shortname, $cachedcourse2->shortname);
70 $this->assertEquals($course2->fullname, $cachedcourse2->fullname);
71
72 $this->assertArrayHasKey($course3->id, $coursecache);
73 $cachedcourse3 = $coursecache[$course3->id];
74 $this->assertEquals($course3->id, $cachedcourse3->id);
75 $this->assertEquals($course3->shortname, $cachedcourse3->shortname);
76 $this->assertEquals($course3->fullname, $cachedcourse3->fullname);
77 }
b8ad1728
JPG
78
79 /**
12cbce0a 80 * Test that the get_events() function only returns activity events that are enabled.
b8ad1728 81 */
12cbce0a 82 public function test_get_events_with_disabled_module() {
cbbca0d0
AG
83 global $DB;
84
f52459bb
JP
85 $generator = $this->getDataGenerator();
86 $course = $generator->create_course();
87 $student = $generator->create_user();
88 $generator->enrol_user($student->id, $course->id, 'student');
89 $this->setUser($student);
f52459bb
JP
90 $events = [
91 [
92 'name' => 'Start of assignment',
93 'description' => '',
94 'format' => 1,
95 'courseid' => $course->id,
96 'groupid' => 0,
97 'userid' => 2,
98 'modulename' => 'assign',
99 'instance' => 1,
100 'eventtype' => 'due',
101 'timestart' => time(),
102 'timeduration' => 86400,
103 'visible' => 1
104 ], [
f52459bb
JP
105 'name' => 'Start of lesson',
106 'description' => '',
107 'format' => 1,
108 'courseid' => $course->id,
109 'groupid' => 0,
110 'userid' => 2,
111 'modulename' => 'lesson',
112 'instance' => 1,
113 'eventtype' => 'end',
114 'timestart' => time(),
115 'timeduration' => 86400,
116 'visible' => 1
117 ]
118 ];
cbbca0d0 119 foreach ($events as $event) {
e057f279 120 \core_calendar\event::create($event, false);
cbbca0d0 121 }
cbbca0d0
AG
122 $timestart = time() - 60;
123 $timeend = time() + 60;
cbbca0d0 124 // Get all events.
12cbce0a 125 $events = \core_calendar\api::get_events($timestart, $timeend, true, 0, true);
cbbca0d0 126 $this->assertCount(2, $events);
cbbca0d0
AG
127 // Disable the lesson module.
128 $modulerecord = $DB->get_record('modules', ['name' => 'lesson']);
129 $modulerecord->visible = 0;
130 $DB->update_record('modules', $modulerecord);
cbbca0d0 131 // Check that we only return the assign event.
12cbce0a 132 $events = \core_calendar\api::get_events($timestart, $timeend, true, 0, true);
cbbca0d0
AG
133 $this->assertCount(1, $events);
134 $event = reset($events);
135 $this->assertEquals('assign', $event->modulename);
136 }
f52459bb
JP
137
138 /**
139 * Test for calendar_get_events() when there are user and group overrides.
140 */
141 public function test_calendar_get_events_with_overrides() {
142 global $DB;
f52459bb
JP
143 $generator = $this->getDataGenerator();
144 $course = $generator->create_course();
145 $plugingenerator = $this->getDataGenerator()->get_plugin_generator('mod_assign');
146 if (!isset($params['course'])) {
147 $params['course'] = $course->id;
148 }
149 $instance = $plugingenerator->create_instance($params);
f52459bb
JP
150 // Create users.
151 $useroverridestudent = $generator->create_user();
152 $group1student = $generator->create_user();
153 $group2student = $generator->create_user();
154 $group12student = $generator->create_user();
155 $nogroupstudent = $generator->create_user();
f52459bb
JP
156 // Enrol users.
157 $generator->enrol_user($useroverridestudent->id, $course->id, 'student');
158 $generator->enrol_user($group1student->id, $course->id, 'student');
159 $generator->enrol_user($group2student->id, $course->id, 'student');
160 $generator->enrol_user($group12student->id, $course->id, 'student');
161 $generator->enrol_user($nogroupstudent->id, $course->id, 'student');
f52459bb
JP
162 // Create groups.
163 $group1 = $generator->create_group(['courseid' => $course->id]);
164 $group2 = $generator->create_group(['courseid' => $course->id]);
f52459bb
JP
165 // Add members to groups.
166 $generator->create_group_member(['groupid' => $group1->id, 'userid' => $group1student->id]);
167 $generator->create_group_member(['groupid' => $group2->id, 'userid' => $group2student->id]);
168 $generator->create_group_member(['groupid' => $group1->id, 'userid' => $group12student->id]);
169 $generator->create_group_member(['groupid' => $group2->id, 'userid' => $group12student->id]);
f52459bb
JP
170 $now = time();
171 // Events with the same module name, instance and event type.
172 $events = [
173 [
174 'name' => 'Assignment 1 due date',
175 'description' => '',
176 'format' => 0,
177 'courseid' => $course->id,
178 'groupid' => 0,
179 'userid' => 2,
180 'modulename' => 'assign',
181 'instance' => $instance->id,
182 'eventtype' => 'due',
183 'timestart' => $now,
184 'timeduration' => 0,
185 'visible' => 1
186 ], [
187 'name' => 'Assignment 1 due date - User override',
188 'description' => '',
189 'format' => 1,
190 'courseid' => 0,
191 'groupid' => 0,
192 'userid' => $useroverridestudent->id,
193 'modulename' => 'assign',
194 'instance' => $instance->id,
195 'eventtype' => 'due',
196 'timestart' => $now + 86400,
197 'timeduration' => 0,
198 'visible' => 1,
199 'priority' => CALENDAR_EVENT_USER_OVERRIDE_PRIORITY
200 ], [
201 'name' => 'Assignment 1 due date - Group A override',
202 'description' => '',
203 'format' => 1,
204 'courseid' => $course->id,
205 'groupid' => $group1->id,
206 'userid' => 2,
207 'modulename' => 'assign',
208 'instance' => $instance->id,
209 'eventtype' => 'due',
210 'timestart' => $now + (2 * 86400),
211 'timeduration' => 0,
212 'visible' => 1,
213 'priority' => 1,
214 ], [
215 'name' => 'Assignment 1 due date - Group B override',
216 'description' => '',
217 'format' => 1,
218 'courseid' => $course->id,
219 'groupid' => $group2->id,
220 'userid' => 2,
221 'modulename' => 'assign',
222 'instance' => $instance->id,
223 'eventtype' => 'due',
224 'timestart' => $now + (3 * 86400),
225 'timeduration' => 0,
226 'visible' => 1,
227 'priority' => 2,
228 ],
229 ];
f52459bb 230 foreach ($events as $event) {
12cbce0a 231 \core_calendar\event::create($event, false);
f52459bb 232 }
f52459bb
JP
233 $timestart = $now - 100;
234 $timeend = $now + (3 * 86400);
f52459bb 235 $groups = [$group1->id, $group2->id];
f52459bb
JP
236 // Get user override events.
237 $this->setUser($useroverridestudent);
12cbce0a 238 $events = \core_calendar\api::get_events($timestart, $timeend, $useroverridestudent->id, $groups, $course->id);
f52459bb
JP
239 $this->assertCount(1, $events);
240 $event = reset($events);
241 $this->assertEquals('Assignment 1 due date - User override', $event->name);
f52459bb 242 // Get event for user with override but with the timestart and timeend parameters only covering the original event.
12cbce0a 243 $events = \core_calendar\api::get_events($timestart, $now, $useroverridestudent->id, $groups, $course->id);
f52459bb 244 $this->assertCount(0, $events);
f52459bb
JP
245 // Get events for user that does not belong to any group and has no user override events.
246 $this->setUser($nogroupstudent);
12cbce0a 247 $events = \core_calendar\api::get_events($timestart, $timeend, $nogroupstudent->id, $groups, $course->id);
f52459bb
JP
248 $this->assertCount(1, $events);
249 $event = reset($events);
250 $this->assertEquals('Assignment 1 due date', $event->name);
f52459bb
JP
251 // Get events for user that belongs to groups A and B and has no user override events.
252 $this->setUser($group12student);
12cbce0a 253 $events = \core_calendar\api::get_events($timestart, $timeend, $group12student->id, $groups, $course->id);
f52459bb
JP
254 $this->assertCount(1, $events);
255 $event = reset($events);
256 $this->assertEquals('Assignment 1 due date - Group B override', $event->name);
f52459bb
JP
257 // Get events for user that belongs to group A and has no user override events.
258 $this->setUser($group1student);
12cbce0a 259 $events = \core_calendar\api::get_events($timestart, $timeend, $group1student->id, $groups, $course->id);
f52459bb
JP
260 $this->assertCount(1, $events);
261 $event = reset($events);
262 $this->assertEquals('Assignment 1 due date - Group A override', $event->name);
f52459bb
JP
263 // Add repeating events.
264 $repeatingevents = [
265 [
266 'name' => 'Repeating site event',
267 'description' => '',
268 'format' => 1,
269 'courseid' => SITEID,
270 'groupid' => 0,
271 'userid' => 2,
272 'repeatid' => 1,
273 'modulename' => '0',
274 'instance' => 0,
275 'eventtype' => 'site',
276 'timestart' => $now + 86400,
277 'timeduration' => 0,
278 'visible' => 1,
279 ],
280 [
281 'name' => 'Repeating site event',
282 'description' => '',
283 'format' => 1,
284 'courseid' => SITEID,
285 'groupid' => 0,
286 'userid' => 2,
287 'repeatid' => 1,
288 'modulename' => '0',
289 'instance' => 0,
290 'eventtype' => 'site',
291 'timestart' => $now + (2 * 86400),
292 'timeduration' => 0,
293 'visible' => 1,
294 ],
295 ];
296 foreach ($repeatingevents as $event) {
12cbce0a 297 \core_calendar\event::create($event, false);
f52459bb 298 }
f52459bb 299 // Make sure repeating events are not filtered out.
12cbce0a 300 $events = \core_calendar\api::get_events($timestart, $timeend, true, true, true);
f52459bb
JP
301 $this->assertCount(3, $events);
302 }
12cbce0a
MN
303
304 /**
305 * Test the update_subscription() function.
306 */
307 public function test_update_subscription() {
308 $this->resetAfterTest(true);
309
310 $subscription = new stdClass();
311 $subscription->eventtype = 'site';
312 $subscription->name = 'test';
313 $id = \core_calendar\api::add_subscription($subscription);
314
315 $subscription = \core_calendar\api::get_subscription($id);
316 $subscription->name = 'awesome';
317 \core_calendar\api::update_subscription($subscription);
318 $sub = \core_calendar\api::get_subscription($id);
319 $this->assertEquals($subscription->name, $sub->name);
320
321 $subscription = \core_calendar\api::get_subscription($id);
322 $subscription->name = 'awesome2';
323 $subscription->pollinterval = 604800;
324 \core_calendar\api::update_subscription($subscription);
325 $sub = \core_calendar\api::get_subscription($id);
326 $this->assertEquals($subscription->name, $sub->name);
327 $this->assertEquals($subscription->pollinterval, $sub->pollinterval);
328
329 $subscription = new stdClass();
330 $subscription->name = 'awesome4';
331 $this->expectException('coding_exception');
332 \core_calendar\api::update_subscription($subscription);
333 }
334
335 public function test_add_subscription() {
336 global $DB, $CFG;
337
338 require_once($CFG->dirroot . '/lib/bennu/bennu.inc.php');
339
340 $this->resetAfterTest(true);
341
342 // Test for Microsoft Outlook 2010.
343 $subscription = new stdClass();
344 $subscription->name = 'Microsoft Outlook 2010';
345 $subscription->importfrom = CALENDAR_IMPORT_FROM_FILE;
346 $subscription->eventtype = 'site';
347 $id = \core_calendar\api::add_subscription($subscription);
348
349 $calendar = file_get_contents($CFG->dirroot . '/lib/tests/fixtures/ms_outlook_2010.ics');
350 $ical = new iCalendar();
351 $ical->unserialize($calendar);
352 $this->assertEquals($ical->parser_errors, array());
353
354 $sub = \core_calendar\api::get_subscription($id);
355 \core_calendar\api::import_icalendar_events($ical, $sub->courseid, $sub->id);
356 $count = $DB->count_records('event', array('subscriptionid' => $sub->id));
357 $this->assertEquals($count, 1);
358
359 // Test for OSX Yosemite.
360 $subscription = new stdClass();
361 $subscription->name = 'OSX Yosemite';
362 $subscription->importfrom = CALENDAR_IMPORT_FROM_FILE;
363 $subscription->eventtype = 'site';
364 $id = \core_calendar\api::add_subscription($subscription);
365
366 $calendar = file_get_contents($CFG->dirroot . '/lib/tests/fixtures/osx_yosemite.ics');
367 $ical = new iCalendar();
368 $ical->unserialize($calendar);
369 $this->assertEquals($ical->parser_errors, array());
370
371 $sub = \core_calendar\api::get_subscription($id);
372 \core_calendar\api::import_icalendar_events($ical, $sub->courseid, $sub->id);
373 $count = $DB->count_records('event', array('subscriptionid' => $sub->id));
374 $this->assertEquals($count, 1);
375
376 // Test for Google Gmail.
377 $subscription = new stdClass();
378 $subscription->name = 'Google Gmail';
379 $subscription->importfrom = CALENDAR_IMPORT_FROM_FILE;
380 $subscription->eventtype = 'site';
381 $id = \core_calendar\api::add_subscription($subscription);
382
383 $calendar = file_get_contents($CFG->dirroot . '/lib/tests/fixtures/google_gmail.ics');
384 $ical = new iCalendar();
385 $ical->unserialize($calendar);
386 $this->assertEquals($ical->parser_errors, array());
387
388 $sub = \core_calendar\api::get_subscription($id);
389 \core_calendar\api::import_icalendar_events($ical, $sub->courseid, $sub->id);
390 $count = $DB->count_records('event', array('subscriptionid' => $sub->id));
391 $this->assertEquals($count, 1);
392 }
1575194a
RW
393
394 /**
395 * Requesting calendar events from a given time should return all events with a sort
396 * time at or after the requested time. All events prior to that time should not
397 * be return.
398 *
399 * If there are no events on or after the given time then an empty result set should
400 * be returned.
401 */
39e7bbae
RW
402 public function test_get_calendar_action_events_by_timesort_after_time() {
403 $user = $this->getDataGenerator()->create_user();
404 $course = $this->getDataGenerator()->create_course();
405 $generator = $this->getDataGenerator()->get_plugin_generator('mod_assign');
406 $moduleinstance = $generator->create_instance(['course' => $course->id]);
407
408 $this->getDataGenerator()->enrol_user($user->id, $course->id);
409 $this->resetAfterTest(true);
c91b4c02 410 $this->setAdminUser();
1575194a
RW
411
412 $params = [
413 'type' => CALENDAR_EVENT_TYPE_ACTION,
c91b4c02
MN
414 'courseid' => 1,
415 'modulename' => 'xyz',
416 'instance' => 1,
417 'userid' => 1,
1575194a
RW
418 'eventtype' => 'user',
419 'repeats' => 0,
420 'timestart' => 1,
421 ];
422
423 $event1 = create_event(array_merge($params, ['name' => 'Event 1', 'timesort' => 1]));
424 $event2 = create_event(array_merge($params, ['name' => 'Event 2', 'timesort' => 2]));
425 $event3 = create_event(array_merge($params, ['name' => 'Event 3', 'timesort' => 3]));
426 $event4 = create_event(array_merge($params, ['name' => 'Event 4', 'timesort' => 4]));
427 $event5 = create_event(array_merge($params, ['name' => 'Event 5', 'timesort' => 5]));
428 $event6 = create_event(array_merge($params, ['name' => 'Event 6', 'timesort' => 6]));
429 $event7 = create_event(array_merge($params, ['name' => 'Event 7', 'timesort' => 7]));
430 $event8 = create_event(array_merge($params, ['name' => 'Event 8', 'timesort' => 8]));
431
39e7bbae 432 $this->setUser($user);
1575194a
RW
433 $result = api::get_action_events_by_timesort(5);
434
435 $this->assertCount(4, $result);
436 $this->assertEquals('Event 5', $result[0]->name);
437 $this->assertEquals('Event 6', $result[1]->name);
438 $this->assertEquals('Event 7', $result[2]->name);
439 $this->assertEquals('Event 8', $result[3]->name);
440
441 $result = api::get_action_events_by_timesort(9);
442
443 $this->assertEmpty($result);
444 }
445
446 /**
447 * Requesting calendar events before a given time should return all events with a sort
448 * time at or before the requested time (inclusive). All events after that time
449 * should not be returned.
450 *
451 * If there are no events before the given time then an empty result set should be
452 * returned.
453 */
39e7bbae
RW
454 public function test_get_calendar_action_events_by_timesort_before_time() {
455 $user = $this->getDataGenerator()->create_user();
456 $course = $this->getDataGenerator()->create_course();
457 $generator = $this->getDataGenerator()->get_plugin_generator('mod_assign');
458 $moduleinstance = $generator->create_instance(['course' => $course->id]);
459
460 $this->getDataGenerator()->enrol_user($user->id, $course->id);
461 $this->resetAfterTest(true);
c91b4c02 462 $this->setAdminUser();
1575194a
RW
463
464 $params = [
465 'type' => CALENDAR_EVENT_TYPE_ACTION,
c91b4c02
MN
466 'courseid' => 1,
467 'modulename' => 'xyz',
468 'instance' => 1,
469 'userid' => 1,
1575194a
RW
470 'eventtype' => 'user',
471 'repeats' => 0,
472 'timestart' => 1,
473 ];
474
475 $event1 = create_event(array_merge($params, ['name' => 'Event 1', 'timesort' => 2]));
476 $event2 = create_event(array_merge($params, ['name' => 'Event 2', 'timesort' => 3]));
477 $event3 = create_event(array_merge($params, ['name' => 'Event 3', 'timesort' => 4]));
478 $event4 = create_event(array_merge($params, ['name' => 'Event 4', 'timesort' => 5]));
479 $event5 = create_event(array_merge($params, ['name' => 'Event 5', 'timesort' => 6]));
480 $event6 = create_event(array_merge($params, ['name' => 'Event 6', 'timesort' => 7]));
481 $event7 = create_event(array_merge($params, ['name' => 'Event 7', 'timesort' => 8]));
482 $event8 = create_event(array_merge($params, ['name' => 'Event 8', 'timesort' => 9]));
483
39e7bbae 484 $this->setUser($user);
1575194a
RW
485 $result = api::get_action_events_by_timesort(null, 5);
486
487 $this->assertCount(4, $result);
488 $this->assertEquals('Event 1', $result[0]->name);
489 $this->assertEquals('Event 2', $result[1]->name);
490 $this->assertEquals('Event 3', $result[2]->name);
491 $this->assertEquals('Event 4', $result[3]->name);
492
493 $result = api::get_action_events_by_timesort(null, 1);
494
495 $this->assertEmpty($result);
496 }
497
498 /**
499 * Requesting calendar events within a given time range should return all events with
500 * a sort time between the lower and upper time bound (inclusive).
501 *
502 * If there are no events in the given time range then an empty result set should be
503 * returned.
504 */
39e7bbae
RW
505 public function test_get_calendar_action_events_by_timesort_time_range() {
506 $user = $this->getDataGenerator()->create_user();
507 $course = $this->getDataGenerator()->create_course();
508 $generator = $this->getDataGenerator()->get_plugin_generator('mod_assign');
509 $moduleinstance = $generator->create_instance(['course' => $course->id]);
510
511 $this->getDataGenerator()->enrol_user($user->id, $course->id);
512 $this->resetAfterTest(true);
c91b4c02 513 $this->setAdminUser();
1575194a
RW
514
515 $params = [
516 'type' => CALENDAR_EVENT_TYPE_ACTION,
c91b4c02
MN
517 'courseid' => 1,
518 'modulename' => 'xyz',
519 'instance' => 1,
520 'userid' => 1,
1575194a
RW
521 'eventtype' => 'user',
522 'repeats' => 0,
523 'timestart' => 1,
524 ];
525
526 $event1 = create_event(array_merge($params, ['name' => 'Event 1', 'timesort' => 1]));
527 $event2 = create_event(array_merge($params, ['name' => 'Event 2', 'timesort' => 2]));
528 $event3 = create_event(array_merge($params, ['name' => 'Event 3', 'timesort' => 3]));
529 $event4 = create_event(array_merge($params, ['name' => 'Event 4', 'timesort' => 4]));
530 $event5 = create_event(array_merge($params, ['name' => 'Event 5', 'timesort' => 5]));
531 $event6 = create_event(array_merge($params, ['name' => 'Event 6', 'timesort' => 6]));
532 $event7 = create_event(array_merge($params, ['name' => 'Event 7', 'timesort' => 7]));
533 $event8 = create_event(array_merge($params, ['name' => 'Event 8', 'timesort' => 8]));
534
39e7bbae 535 $this->setUser($user);
1575194a
RW
536 $result = api::get_action_events_by_timesort(3, 6);
537
538 $this->assertCount(4, $result);
539 $this->assertEquals('Event 3', $result[0]->name);
540 $this->assertEquals('Event 4', $result[1]->name);
541 $this->assertEquals('Event 5', $result[2]->name);
542 $this->assertEquals('Event 6', $result[3]->name);
543
544 $result = api::get_action_events_by_timesort(10, 15);
545
546 $this->assertEmpty($result);
547 }
548
549 /**
550 * Requesting calendar events within a given time range and a limit and offset should return
551 * the number of events up to the given limit value that have a sort time between the lower
552 * and uppper time bound (inclusive) where the result set is shifted by the offset value.
553 *
554 * If there are no events in the given time range then an empty result set should be
555 * returned.
556 */
39e7bbae
RW
557 public function test_get_calendar_action_events_by_timesort_time_limit_offset() {
558 $user = $this->getDataGenerator()->create_user();
559 $course = $this->getDataGenerator()->create_course();
560 $generator = $this->getDataGenerator()->get_plugin_generator('mod_assign');
561 $moduleinstance = $generator->create_instance(['course' => $course->id]);
562
563 $this->getDataGenerator()->enrol_user($user->id, $course->id);
564 $this->resetAfterTest(true);
c91b4c02 565 $this->setAdminUser();
1575194a
RW
566
567 $params = [
568 'type' => CALENDAR_EVENT_TYPE_ACTION,
c91b4c02
MN
569 'courseid' => 1,
570 'modulename' => 'xyz',
571 'instance' => 1,
572 'userid' => 1,
1575194a
RW
573 'eventtype' => 'user',
574 'repeats' => 0,
575 'timestart' => 1,
576 ];
577
578 $event1 = create_event(array_merge($params, ['name' => 'Event 1', 'timesort' => 1]));
579 $event2 = create_event(array_merge($params, ['name' => 'Event 2', 'timesort' => 2]));
580 $event3 = create_event(array_merge($params, ['name' => 'Event 3', 'timesort' => 3]));
581 $event4 = create_event(array_merge($params, ['name' => 'Event 4', 'timesort' => 4]));
582 $event5 = create_event(array_merge($params, ['name' => 'Event 5', 'timesort' => 5]));
583 $event6 = create_event(array_merge($params, ['name' => 'Event 6', 'timesort' => 6]));
584 $event7 = create_event(array_merge($params, ['name' => 'Event 7', 'timesort' => 7]));
585 $event8 = create_event(array_merge($params, ['name' => 'Event 8', 'timesort' => 8]));
586
39e7bbae 587 $this->setUser($user);
1575194a
RW
588 $result = api::get_action_events_by_timesort(2, 7, $event3->id, 2);
589
590 $this->assertCount(2, $result);
591 $this->assertEquals('Event 4', $result[0]->name);
592 $this->assertEquals('Event 5', $result[1]->name);
593
594 $result = api::get_action_events_by_timesort(2, 7, $event5->id, 2);
595
596 $this->assertCount(2, $result);
597 $this->assertEquals('Event 6', $result[0]->name);
598 $this->assertEquals('Event 7', $result[1]->name);
599
600 $result = api::get_action_events_by_timesort(2, 7, $event7->id, 2);
601
602 $this->assertEmpty($result);
603 }
39e7bbae
RW
604
605 /**
606 * Requesting calendar events from a given course and time should return all
607 * events with a sort time at or after the requested time. All events prior
608 * to that time should not be return.
609 *
610 * If there are no events on or after the given time then an empty result set should
611 * be returned.
612 */
613 public function test_get_calendar_action_events_by_course_after_time() {
614 $user = $this->getDataGenerator()->create_user();
615 $course1 = $this->getDataGenerator()->create_course();
616 $course2 = $this->getDataGenerator()->create_course();
617 $generator = $this->getDataGenerator()->get_plugin_generator('mod_assign');
618 $moduleinstance1 = $generator->create_instance(['course' => $course1->id]);
619 $moduleinstance2 = $generator->create_instance(['course' => $course2->id]);
620
621 $this->getDataGenerator()->enrol_user($user->id, $course1->id);
622 $this->getDataGenerator()->enrol_user($user->id, $course2->id);
623 $this->resetAfterTest(true);
624 $this->setUser($user);
625
626 $params = [
627 'type' => CALENDAR_EVENT_TYPE_ACTION,
628 'modulename' => 'assign',
629 'instance' => $moduleinstance1->id,
630 'userid' => $user->id,
631 'courseid' => $course1->id,
632 'eventtype' => 'user',
633 'repeats' => 0,
634 'timestart' => 1,
635 ];
636
637 $event1 = create_event(array_merge($params, ['name' => 'Event 1', 'timesort' => 1]));
638 $event2 = create_event(array_merge($params, ['name' => 'Event 2', 'timesort' => 2]));
639 $event3 = create_event(array_merge($params, ['name' => 'Event 3', 'timesort' => 3]));
640 $event4 = create_event(array_merge($params, ['name' => 'Event 4', 'timesort' => 4]));
641 $event5 = create_event(array_merge($params, ['name' => 'Event 5', 'timesort' => 5]));
642 $event6 = create_event(array_merge($params, ['name' => 'Event 6', 'timesort' => 6]));
643 $event7 = create_event(array_merge($params, ['name' => 'Event 7', 'timesort' => 7]));
644 $event8 = create_event(array_merge($params, ['name' => 'Event 8', 'timesort' => 8]));
645
646 $params['courseid'] = $course2->id;
647 $params['instance'] = $moduleinstance2->id;
648 $event9 = create_event(array_merge($params, ['name' => 'Event 9', 'timesort' => 1]));
649 $event10 = create_event(array_merge($params, ['name' => 'Event 10', 'timesort' => 2]));
650 $event11 = create_event(array_merge($params, ['name' => 'Event 11', 'timesort' => 3]));
651 $event12 = create_event(array_merge($params, ['name' => 'Event 12', 'timesort' => 4]));
652 $event13 = create_event(array_merge($params, ['name' => 'Event 13', 'timesort' => 5]));
653 $event14 = create_event(array_merge($params, ['name' => 'Event 14', 'timesort' => 6]));
654 $event15 = create_event(array_merge($params, ['name' => 'Event 15', 'timesort' => 7]));
655 $event16 = create_event(array_merge($params, ['name' => 'Event 16', 'timesort' => 8]));
656
657 $result = api::get_action_events_by_course($course1, 5);
658
659 $this->assertCount(4, $result);
660 $this->assertEquals('Event 5', $result[0]->name);
661 $this->assertEquals('Event 6', $result[1]->name);
662 $this->assertEquals('Event 7', $result[2]->name);
663 $this->assertEquals('Event 8', $result[3]->name);
664
665 $result = api::get_action_events_by_course($course1, 9);
666
667 $this->assertEmpty($result);
668 }
669
670 /**
671 * Requesting calendar events for a course and before a given time should return
672 * all events with a sort time at or before the requested time (inclusive). All
673 * events after that time should not be returned.
674 *
675 * If there are no events before the given time then an empty result set should be
676 * returned.
677 */
678 public function test_get_calendar_action_events_by_course_before_time() {
679 $user = $this->getDataGenerator()->create_user();
680 $course1 = $this->getDataGenerator()->create_course();
681 $course2 = $this->getDataGenerator()->create_course();
682 $generator = $this->getDataGenerator()->get_plugin_generator('mod_assign');
683 $moduleinstance1 = $generator->create_instance(['course' => $course1->id]);
684 $moduleinstance2 = $generator->create_instance(['course' => $course2->id]);
685
686 $this->getDataGenerator()->enrol_user($user->id, $course1->id);
687 $this->getDataGenerator()->enrol_user($user->id, $course2->id);
688 $this->resetAfterTest(true);
689 $this->setUser($user);
690
691 $params = [
692 'type' => CALENDAR_EVENT_TYPE_ACTION,
693 'modulename' => 'assign',
694 'instance' => $moduleinstance1->id,
695 'userid' => $user->id,
696 'courseid' => $course1->id,
697 'eventtype' => 'user',
698 'repeats' => 0,
699 'timestart' => 1,
700 ];
701
702 $event1 = create_event(array_merge($params, ['name' => 'Event 1', 'timesort' => 2]));
703 $event2 = create_event(array_merge($params, ['name' => 'Event 2', 'timesort' => 3]));
704 $event3 = create_event(array_merge($params, ['name' => 'Event 3', 'timesort' => 4]));
705 $event4 = create_event(array_merge($params, ['name' => 'Event 4', 'timesort' => 5]));
706 $event5 = create_event(array_merge($params, ['name' => 'Event 5', 'timesort' => 6]));
707 $event6 = create_event(array_merge($params, ['name' => 'Event 6', 'timesort' => 7]));
708 $event7 = create_event(array_merge($params, ['name' => 'Event 7', 'timesort' => 8]));
709 $event8 = create_event(array_merge($params, ['name' => 'Event 8', 'timesort' => 9]));
710
711 $params['courseid'] = $course2->id;
712 $params['instance'] = $moduleinstance2->id;
713 $event9 = create_event(array_merge($params, ['name' => 'Event 9', 'timesort' => 2]));
714 $event10 = create_event(array_merge($params, ['name' => 'Event 10', 'timesort' => 3]));
715 $event11 = create_event(array_merge($params, ['name' => 'Event 11', 'timesort' => 4]));
716 $event12 = create_event(array_merge($params, ['name' => 'Event 12', 'timesort' => 5]));
717 $event13 = create_event(array_merge($params, ['name' => 'Event 13', 'timesort' => 6]));
718 $event14 = create_event(array_merge($params, ['name' => 'Event 14', 'timesort' => 7]));
719 $event15 = create_event(array_merge($params, ['name' => 'Event 15', 'timesort' => 8]));
720 $event16 = create_event(array_merge($params, ['name' => 'Event 16', 'timesort' => 9]));
721
722 $result = api::get_action_events_by_course($course1, null, 5);
723
724 $this->assertCount(4, $result);
725 $this->assertEquals('Event 1', $result[0]->name);
726 $this->assertEquals('Event 2', $result[1]->name);
727 $this->assertEquals('Event 3', $result[2]->name);
728 $this->assertEquals('Event 4', $result[3]->name);
729
730 $result = api::get_action_events_by_course($course1, null, 1);
731
732 $this->assertEmpty($result);
733 }
734
735 /**
736 * Requesting calendar events for a course and within a given time range should
737 * return all events with a sort time between the lower and upper time bound
738 * (inclusive).
739 *
740 * If there are no events in the given time range then an empty result set should be
741 * returned.
742 */
743 public function test_get_calendar_action_events_by_course_time_range() {
744 $user = $this->getDataGenerator()->create_user();
745 $course1 = $this->getDataGenerator()->create_course();
746 $course2 = $this->getDataGenerator()->create_course();
747 $generator = $this->getDataGenerator()->get_plugin_generator('mod_assign');
748 $moduleinstance1 = $generator->create_instance(['course' => $course1->id]);
749 $moduleinstance2 = $generator->create_instance(['course' => $course2->id]);
750
751 $this->getDataGenerator()->enrol_user($user->id, $course1->id);
752 $this->getDataGenerator()->enrol_user($user->id, $course2->id);
753 $this->resetAfterTest(true);
754 $this->setUser($user);
755
756 $params = [
757 'type' => CALENDAR_EVENT_TYPE_ACTION,
758 'modulename' => 'assign',
759 'instance' => $moduleinstance1->id,
760 'userid' => $user->id,
761 'courseid' => $course1->id,
762 'eventtype' => 'user',
763 'repeats' => 0,
764 'timestart' => 1,
765 ];
766
767 $event1 = create_event(array_merge($params, ['name' => 'Event 1', 'timesort' => 1]));
768 $event2 = create_event(array_merge($params, ['name' => 'Event 2', 'timesort' => 2]));
769 $event3 = create_event(array_merge($params, ['name' => 'Event 3', 'timesort' => 3]));
770 $event4 = create_event(array_merge($params, ['name' => 'Event 4', 'timesort' => 4]));
771 $event5 = create_event(array_merge($params, ['name' => 'Event 5', 'timesort' => 5]));
772 $event6 = create_event(array_merge($params, ['name' => 'Event 6', 'timesort' => 6]));
773 $event7 = create_event(array_merge($params, ['name' => 'Event 7', 'timesort' => 7]));
774 $event8 = create_event(array_merge($params, ['name' => 'Event 8', 'timesort' => 8]));
775
776 $params['courseid'] = $course2->id;
777 $params['instance'] = $moduleinstance2->id;
778 $event9 = create_event(array_merge($params, ['name' => 'Event 9', 'timesort' => 1]));
779 $event10 = create_event(array_merge($params, ['name' => 'Event 10', 'timesort' => 2]));
780 $event11 = create_event(array_merge($params, ['name' => 'Event 11', 'timesort' => 3]));
781 $event12 = create_event(array_merge($params, ['name' => 'Event 12', 'timesort' => 4]));
782 $event13 = create_event(array_merge($params, ['name' => 'Event 13', 'timesort' => 5]));
783 $event14 = create_event(array_merge($params, ['name' => 'Event 14', 'timesort' => 6]));
784 $event15 = create_event(array_merge($params, ['name' => 'Event 15', 'timesort' => 7]));
785 $event16 = create_event(array_merge($params, ['name' => 'Event 16', 'timesort' => 8]));
786
787 $result = api::get_action_events_by_course($course1, 3, 6);
788
789 $this->assertCount(4, $result);
790 $this->assertEquals('Event 3', $result[0]->name);
791 $this->assertEquals('Event 4', $result[1]->name);
792 $this->assertEquals('Event 5', $result[2]->name);
793 $this->assertEquals('Event 6', $result[3]->name);
794
795 $result = api::get_action_events_by_course($course1, 10, 15);
796
797 $this->assertEmpty($result);
798 }
799
800 /**
801 * Requesting calendar events for a course and within a given time range and a limit
802 * and offset should return the number of events up to the given limit value that have
803 * a sort time between the lower and uppper time bound (inclusive) where the result
804 * set is shifted by the offset value.
805 *
806 * If there are no events in the given time range then an empty result set should be
807 * returned.
808 */
809 public function test_get_calendar_action_events_by_course_time_limit_offset() {
810 $user = $this->getDataGenerator()->create_user();
811 $course1 = $this->getDataGenerator()->create_course();
812 $course2 = $this->getDataGenerator()->create_course();
813 $generator = $this->getDataGenerator()->get_plugin_generator('mod_assign');
814 $moduleinstance1 = $generator->create_instance(['course' => $course1->id]);
815 $moduleinstance2 = $generator->create_instance(['course' => $course2->id]);
816
817 $this->getDataGenerator()->enrol_user($user->id, $course1->id);
818 $this->getDataGenerator()->enrol_user($user->id, $course2->id);
819 $this->resetAfterTest(true);
820 $this->setUser($user);
821
822 $params = [
823 'type' => CALENDAR_EVENT_TYPE_ACTION,
824 'modulename' => 'assign',
825 'instance' => $moduleinstance1->id,
826 'userid' => $user->id,
827 'courseid' => $course1->id,
828 'eventtype' => 'user',
829 'repeats' => 0,
830 'timestart' => 1,
831 ];
832
833 $event1 = create_event(array_merge($params, ['name' => 'Event 1', 'timesort' => 1]));
834 $event2 = create_event(array_merge($params, ['name' => 'Event 2', 'timesort' => 2]));
835 $event3 = create_event(array_merge($params, ['name' => 'Event 3', 'timesort' => 3]));
836 $event4 = create_event(array_merge($params, ['name' => 'Event 4', 'timesort' => 4]));
837 $event5 = create_event(array_merge($params, ['name' => 'Event 5', 'timesort' => 5]));
838 $event6 = create_event(array_merge($params, ['name' => 'Event 6', 'timesort' => 6]));
839 $event7 = create_event(array_merge($params, ['name' => 'Event 7', 'timesort' => 7]));
840 $event8 = create_event(array_merge($params, ['name' => 'Event 8', 'timesort' => 8]));
841
842 $params['courseid'] = $course2->id;
843 $params['instance'] = $moduleinstance2->id;
844 $event9 = create_event(array_merge($params, ['name' => 'Event 9', 'timesort' => 1]));
845 $event10 = create_event(array_merge($params, ['name' => 'Event 10', 'timesort' => 2]));
846 $event11 = create_event(array_merge($params, ['name' => 'Event 11', 'timesort' => 3]));
847 $event12 = create_event(array_merge($params, ['name' => 'Event 12', 'timesort' => 4]));
848 $event13 = create_event(array_merge($params, ['name' => 'Event 13', 'timesort' => 5]));
849 $event14 = create_event(array_merge($params, ['name' => 'Event 14', 'timesort' => 6]));
850 $event15 = create_event(array_merge($params, ['name' => 'Event 15', 'timesort' => 7]));
851 $event16 = create_event(array_merge($params, ['name' => 'Event 16', 'timesort' => 8]));
852
853 $result = api::get_action_events_by_course_and_timesort($course1, 2, 7, $event3->id, 2);
854
855 $this->assertCount(2, $result);
856 $this->assertEquals('Event 4', $result[0]->name);
857 $this->assertEquals('Event 5', $result[1]->name);
858
859 $result = api::get_action_events_by_course_and_timesort($course1, 2, 7, $event5->id, 2);
860
861 $this->assertCount(2, $result);
862 $this->assertEquals('Event 6', $result[0]->name);
863 $this->assertEquals('Event 7', $result[1]->name);
864
865 $result = api::get_action_events_by_course_and_timesort($course1, 2, 7, $event7->id, 2);
866
867 $this->assertEmpty($result);
868 }
3607961d 869}