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