MDL-58424 core_calendar: moved logic from api.php to lib.php
[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
1575194a
RW
47 /**
48 * Requesting calendar events from a given time should return all events with a sort
49 * time at or after the requested time. All events prior to that time should not
50 * be return.
51 *
52 * If there are no events on or after the given time then an empty result set should
53 * be returned.
54 */
39e7bbae
RW
55 public function test_get_calendar_action_events_by_timesort_after_time() {
56 $user = $this->getDataGenerator()->create_user();
57 $course = $this->getDataGenerator()->create_course();
58 $generator = $this->getDataGenerator()->get_plugin_generator('mod_assign');
59 $moduleinstance = $generator->create_instance(['course' => $course->id]);
60
61 $this->getDataGenerator()->enrol_user($user->id, $course->id);
62 $this->resetAfterTest(true);
c91b4c02 63 $this->setAdminUser();
1575194a
RW
64
65 $params = [
66 'type' => CALENDAR_EVENT_TYPE_ACTION,
7aedfe32
CB
67 'courseid' => $course->id,
68 'modulename' => 'assign',
69 'instance' => $moduleinstance->id,
bd01fd15 70 'userid' => $user->id,
1575194a
RW
71 'eventtype' => 'user',
72 'repeats' => 0,
73 'timestart' => 1,
74 ];
75
76 $event1 = create_event(array_merge($params, ['name' => 'Event 1', 'timesort' => 1]));
77 $event2 = create_event(array_merge($params, ['name' => 'Event 2', 'timesort' => 2]));
78 $event3 = create_event(array_merge($params, ['name' => 'Event 3', 'timesort' => 3]));
79 $event4 = create_event(array_merge($params, ['name' => 'Event 4', 'timesort' => 4]));
80 $event5 = create_event(array_merge($params, ['name' => 'Event 5', 'timesort' => 5]));
81 $event6 = create_event(array_merge($params, ['name' => 'Event 6', 'timesort' => 6]));
82 $event7 = create_event(array_merge($params, ['name' => 'Event 7', 'timesort' => 7]));
83 $event8 = create_event(array_merge($params, ['name' => 'Event 8', 'timesort' => 8]));
84
39e7bbae 85 $this->setUser($user);
1575194a
RW
86 $result = api::get_action_events_by_timesort(5);
87
88 $this->assertCount(4, $result);
89 $this->assertEquals('Event 5', $result[0]->name);
90 $this->assertEquals('Event 6', $result[1]->name);
91 $this->assertEquals('Event 7', $result[2]->name);
92 $this->assertEquals('Event 8', $result[3]->name);
93
94 $result = api::get_action_events_by_timesort(9);
95
96 $this->assertEmpty($result);
97 }
98
99 /**
100 * Requesting calendar events before a given time should return all events with a sort
101 * time at or before the requested time (inclusive). All events after that time
102 * should not be returned.
103 *
104 * If there are no events before the given time then an empty result set should be
105 * returned.
106 */
39e7bbae
RW
107 public function test_get_calendar_action_events_by_timesort_before_time() {
108 $user = $this->getDataGenerator()->create_user();
109 $course = $this->getDataGenerator()->create_course();
110 $generator = $this->getDataGenerator()->get_plugin_generator('mod_assign');
111 $moduleinstance = $generator->create_instance(['course' => $course->id]);
112
113 $this->getDataGenerator()->enrol_user($user->id, $course->id);
114 $this->resetAfterTest(true);
c91b4c02 115 $this->setAdminUser();
1575194a
RW
116
117 $params = [
118 'type' => CALENDAR_EVENT_TYPE_ACTION,
7aedfe32
CB
119 'courseid' => $course->id,
120 'modulename' => 'assign',
121 'instance' => $moduleinstance->id,
c91b4c02 122 'userid' => 1,
1575194a
RW
123 'eventtype' => 'user',
124 'repeats' => 0,
125 'timestart' => 1,
126 ];
127
128 $event1 = create_event(array_merge($params, ['name' => 'Event 1', 'timesort' => 2]));
129 $event2 = create_event(array_merge($params, ['name' => 'Event 2', 'timesort' => 3]));
130 $event3 = create_event(array_merge($params, ['name' => 'Event 3', 'timesort' => 4]));
131 $event4 = create_event(array_merge($params, ['name' => 'Event 4', 'timesort' => 5]));
132 $event5 = create_event(array_merge($params, ['name' => 'Event 5', 'timesort' => 6]));
133 $event6 = create_event(array_merge($params, ['name' => 'Event 6', 'timesort' => 7]));
134 $event7 = create_event(array_merge($params, ['name' => 'Event 7', 'timesort' => 8]));
135 $event8 = create_event(array_merge($params, ['name' => 'Event 8', 'timesort' => 9]));
136
39e7bbae 137 $this->setUser($user);
1575194a
RW
138 $result = api::get_action_events_by_timesort(null, 5);
139
140 $this->assertCount(4, $result);
141 $this->assertEquals('Event 1', $result[0]->name);
142 $this->assertEquals('Event 2', $result[1]->name);
143 $this->assertEquals('Event 3', $result[2]->name);
144 $this->assertEquals('Event 4', $result[3]->name);
145
146 $result = api::get_action_events_by_timesort(null, 1);
147
148 $this->assertEmpty($result);
149 }
150
151 /**
152 * Requesting calendar events within a given time range should return all events with
153 * a sort time between the lower and upper time bound (inclusive).
154 *
155 * If there are no events in the given time range then an empty result set should be
156 * returned.
157 */
39e7bbae
RW
158 public function test_get_calendar_action_events_by_timesort_time_range() {
159 $user = $this->getDataGenerator()->create_user();
160 $course = $this->getDataGenerator()->create_course();
161 $generator = $this->getDataGenerator()->get_plugin_generator('mod_assign');
162 $moduleinstance = $generator->create_instance(['course' => $course->id]);
163
164 $this->getDataGenerator()->enrol_user($user->id, $course->id);
165 $this->resetAfterTest(true);
c91b4c02 166 $this->setAdminUser();
1575194a
RW
167
168 $params = [
169 'type' => CALENDAR_EVENT_TYPE_ACTION,
7aedfe32
CB
170 'courseid' => $course->id,
171 'modulename' => 'assign',
172 'instance' => $moduleinstance->id,
c91b4c02 173 'userid' => 1,
1575194a
RW
174 'eventtype' => 'user',
175 'repeats' => 0,
176 'timestart' => 1,
177 ];
178
179 $event1 = create_event(array_merge($params, ['name' => 'Event 1', 'timesort' => 1]));
180 $event2 = create_event(array_merge($params, ['name' => 'Event 2', 'timesort' => 2]));
181 $event3 = create_event(array_merge($params, ['name' => 'Event 3', 'timesort' => 3]));
182 $event4 = create_event(array_merge($params, ['name' => 'Event 4', 'timesort' => 4]));
183 $event5 = create_event(array_merge($params, ['name' => 'Event 5', 'timesort' => 5]));
184 $event6 = create_event(array_merge($params, ['name' => 'Event 6', 'timesort' => 6]));
185 $event7 = create_event(array_merge($params, ['name' => 'Event 7', 'timesort' => 7]));
186 $event8 = create_event(array_merge($params, ['name' => 'Event 8', 'timesort' => 8]));
187
39e7bbae 188 $this->setUser($user);
1575194a
RW
189 $result = api::get_action_events_by_timesort(3, 6);
190
191 $this->assertCount(4, $result);
192 $this->assertEquals('Event 3', $result[0]->name);
193 $this->assertEquals('Event 4', $result[1]->name);
194 $this->assertEquals('Event 5', $result[2]->name);
195 $this->assertEquals('Event 6', $result[3]->name);
196
197 $result = api::get_action_events_by_timesort(10, 15);
198
199 $this->assertEmpty($result);
200 }
201
202 /**
203 * Requesting calendar events within a given time range and a limit and offset should return
204 * the number of events up to the given limit value that have a sort time between the lower
205 * and uppper time bound (inclusive) where the result set is shifted by the offset value.
206 *
207 * If there are no events in the given time range then an empty result set should be
208 * returned.
209 */
39e7bbae
RW
210 public function test_get_calendar_action_events_by_timesort_time_limit_offset() {
211 $user = $this->getDataGenerator()->create_user();
212 $course = $this->getDataGenerator()->create_course();
213 $generator = $this->getDataGenerator()->get_plugin_generator('mod_assign');
214 $moduleinstance = $generator->create_instance(['course' => $course->id]);
215
216 $this->getDataGenerator()->enrol_user($user->id, $course->id);
217 $this->resetAfterTest(true);
c91b4c02 218 $this->setAdminUser();
1575194a
RW
219
220 $params = [
221 'type' => CALENDAR_EVENT_TYPE_ACTION,
7aedfe32
CB
222 'courseid' => $course->id,
223 'modulename' => 'assign',
224 'instance' => $moduleinstance->id,
c91b4c02 225 'userid' => 1,
1575194a
RW
226 'eventtype' => 'user',
227 'repeats' => 0,
228 'timestart' => 1,
229 ];
230
231 $event1 = create_event(array_merge($params, ['name' => 'Event 1', 'timesort' => 1]));
232 $event2 = create_event(array_merge($params, ['name' => 'Event 2', 'timesort' => 2]));
233 $event3 = create_event(array_merge($params, ['name' => 'Event 3', 'timesort' => 3]));
234 $event4 = create_event(array_merge($params, ['name' => 'Event 4', 'timesort' => 4]));
235 $event5 = create_event(array_merge($params, ['name' => 'Event 5', 'timesort' => 5]));
236 $event6 = create_event(array_merge($params, ['name' => 'Event 6', 'timesort' => 6]));
237 $event7 = create_event(array_merge($params, ['name' => 'Event 7', 'timesort' => 7]));
238 $event8 = create_event(array_merge($params, ['name' => 'Event 8', 'timesort' => 8]));
239
39e7bbae 240 $this->setUser($user);
1575194a
RW
241 $result = api::get_action_events_by_timesort(2, 7, $event3->id, 2);
242
243 $this->assertCount(2, $result);
244 $this->assertEquals('Event 4', $result[0]->name);
245 $this->assertEquals('Event 5', $result[1]->name);
246
247 $result = api::get_action_events_by_timesort(2, 7, $event5->id, 2);
248
249 $this->assertCount(2, $result);
250 $this->assertEquals('Event 6', $result[0]->name);
251 $this->assertEquals('Event 7', $result[1]->name);
252
253 $result = api::get_action_events_by_timesort(2, 7, $event7->id, 2);
254
255 $this->assertEmpty($result);
256 }
39e7bbae
RW
257
258 /**
259 * Requesting calendar events from a given course and time should return all
260 * events with a sort time at or after the requested time. All events prior
261 * to that time should not be return.
262 *
263 * If there are no events on or after the given time then an empty result set should
264 * be returned.
265 */
266 public function test_get_calendar_action_events_by_course_after_time() {
267 $user = $this->getDataGenerator()->create_user();
268 $course1 = $this->getDataGenerator()->create_course();
269 $course2 = $this->getDataGenerator()->create_course();
270 $generator = $this->getDataGenerator()->get_plugin_generator('mod_assign');
271 $moduleinstance1 = $generator->create_instance(['course' => $course1->id]);
272 $moduleinstance2 = $generator->create_instance(['course' => $course2->id]);
273
274 $this->getDataGenerator()->enrol_user($user->id, $course1->id);
275 $this->getDataGenerator()->enrol_user($user->id, $course2->id);
276 $this->resetAfterTest(true);
277 $this->setUser($user);
278
279 $params = [
280 'type' => CALENDAR_EVENT_TYPE_ACTION,
281 'modulename' => 'assign',
282 'instance' => $moduleinstance1->id,
283 'userid' => $user->id,
284 'courseid' => $course1->id,
285 'eventtype' => 'user',
286 'repeats' => 0,
287 'timestart' => 1,
288 ];
289
290 $event1 = create_event(array_merge($params, ['name' => 'Event 1', 'timesort' => 1]));
291 $event2 = create_event(array_merge($params, ['name' => 'Event 2', 'timesort' => 2]));
292 $event3 = create_event(array_merge($params, ['name' => 'Event 3', 'timesort' => 3]));
293 $event4 = create_event(array_merge($params, ['name' => 'Event 4', 'timesort' => 4]));
294 $event5 = create_event(array_merge($params, ['name' => 'Event 5', 'timesort' => 5]));
295 $event6 = create_event(array_merge($params, ['name' => 'Event 6', 'timesort' => 6]));
296 $event7 = create_event(array_merge($params, ['name' => 'Event 7', 'timesort' => 7]));
297 $event8 = create_event(array_merge($params, ['name' => 'Event 8', 'timesort' => 8]));
298
299 $params['courseid'] = $course2->id;
300 $params['instance'] = $moduleinstance2->id;
301 $event9 = create_event(array_merge($params, ['name' => 'Event 9', 'timesort' => 1]));
302 $event10 = create_event(array_merge($params, ['name' => 'Event 10', 'timesort' => 2]));
303 $event11 = create_event(array_merge($params, ['name' => 'Event 11', 'timesort' => 3]));
304 $event12 = create_event(array_merge($params, ['name' => 'Event 12', 'timesort' => 4]));
305 $event13 = create_event(array_merge($params, ['name' => 'Event 13', 'timesort' => 5]));
306 $event14 = create_event(array_merge($params, ['name' => 'Event 14', 'timesort' => 6]));
307 $event15 = create_event(array_merge($params, ['name' => 'Event 15', 'timesort' => 7]));
308 $event16 = create_event(array_merge($params, ['name' => 'Event 16', 'timesort' => 8]));
309
310 $result = api::get_action_events_by_course($course1, 5);
311
312 $this->assertCount(4, $result);
313 $this->assertEquals('Event 5', $result[0]->name);
314 $this->assertEquals('Event 6', $result[1]->name);
315 $this->assertEquals('Event 7', $result[2]->name);
316 $this->assertEquals('Event 8', $result[3]->name);
317
318 $result = api::get_action_events_by_course($course1, 9);
319
320 $this->assertEmpty($result);
321 }
322
323 /**
324 * Requesting calendar events for a course and before a given time should return
325 * all events with a sort time at or before the requested time (inclusive). All
326 * events after that time should not be returned.
327 *
328 * If there are no events before the given time then an empty result set should be
329 * returned.
330 */
331 public function test_get_calendar_action_events_by_course_before_time() {
332 $user = $this->getDataGenerator()->create_user();
333 $course1 = $this->getDataGenerator()->create_course();
334 $course2 = $this->getDataGenerator()->create_course();
335 $generator = $this->getDataGenerator()->get_plugin_generator('mod_assign');
336 $moduleinstance1 = $generator->create_instance(['course' => $course1->id]);
337 $moduleinstance2 = $generator->create_instance(['course' => $course2->id]);
338
339 $this->getDataGenerator()->enrol_user($user->id, $course1->id);
340 $this->getDataGenerator()->enrol_user($user->id, $course2->id);
341 $this->resetAfterTest(true);
342 $this->setUser($user);
343
344 $params = [
345 'type' => CALENDAR_EVENT_TYPE_ACTION,
346 'modulename' => 'assign',
347 'instance' => $moduleinstance1->id,
348 'userid' => $user->id,
349 'courseid' => $course1->id,
350 'eventtype' => 'user',
351 'repeats' => 0,
352 'timestart' => 1,
353 ];
354
355 $event1 = create_event(array_merge($params, ['name' => 'Event 1', 'timesort' => 2]));
356 $event2 = create_event(array_merge($params, ['name' => 'Event 2', 'timesort' => 3]));
357 $event3 = create_event(array_merge($params, ['name' => 'Event 3', 'timesort' => 4]));
358 $event4 = create_event(array_merge($params, ['name' => 'Event 4', 'timesort' => 5]));
359 $event5 = create_event(array_merge($params, ['name' => 'Event 5', 'timesort' => 6]));
360 $event6 = create_event(array_merge($params, ['name' => 'Event 6', 'timesort' => 7]));
361 $event7 = create_event(array_merge($params, ['name' => 'Event 7', 'timesort' => 8]));
362 $event8 = create_event(array_merge($params, ['name' => 'Event 8', 'timesort' => 9]));
363
364 $params['courseid'] = $course2->id;
365 $params['instance'] = $moduleinstance2->id;
366 $event9 = create_event(array_merge($params, ['name' => 'Event 9', 'timesort' => 2]));
367 $event10 = create_event(array_merge($params, ['name' => 'Event 10', 'timesort' => 3]));
368 $event11 = create_event(array_merge($params, ['name' => 'Event 11', 'timesort' => 4]));
369 $event12 = create_event(array_merge($params, ['name' => 'Event 12', 'timesort' => 5]));
370 $event13 = create_event(array_merge($params, ['name' => 'Event 13', 'timesort' => 6]));
371 $event14 = create_event(array_merge($params, ['name' => 'Event 14', 'timesort' => 7]));
372 $event15 = create_event(array_merge($params, ['name' => 'Event 15', 'timesort' => 8]));
373 $event16 = create_event(array_merge($params, ['name' => 'Event 16', 'timesort' => 9]));
374
375 $result = api::get_action_events_by_course($course1, null, 5);
376
377 $this->assertCount(4, $result);
378 $this->assertEquals('Event 1', $result[0]->name);
379 $this->assertEquals('Event 2', $result[1]->name);
380 $this->assertEquals('Event 3', $result[2]->name);
381 $this->assertEquals('Event 4', $result[3]->name);
382
383 $result = api::get_action_events_by_course($course1, null, 1);
384
385 $this->assertEmpty($result);
386 }
387
388 /**
389 * Requesting calendar events for a course and within a given time range should
390 * return all events with a sort time between the lower and upper time bound
391 * (inclusive).
392 *
393 * If there are no events in the given time range then an empty result set should be
394 * returned.
395 */
396 public function test_get_calendar_action_events_by_course_time_range() {
397 $user = $this->getDataGenerator()->create_user();
398 $course1 = $this->getDataGenerator()->create_course();
399 $course2 = $this->getDataGenerator()->create_course();
400 $generator = $this->getDataGenerator()->get_plugin_generator('mod_assign');
401 $moduleinstance1 = $generator->create_instance(['course' => $course1->id]);
402 $moduleinstance2 = $generator->create_instance(['course' => $course2->id]);
403
404 $this->getDataGenerator()->enrol_user($user->id, $course1->id);
405 $this->getDataGenerator()->enrol_user($user->id, $course2->id);
406 $this->resetAfterTest(true);
407 $this->setUser($user);
408
409 $params = [
410 'type' => CALENDAR_EVENT_TYPE_ACTION,
411 'modulename' => 'assign',
412 'instance' => $moduleinstance1->id,
413 'userid' => $user->id,
414 'courseid' => $course1->id,
415 'eventtype' => 'user',
416 'repeats' => 0,
417 'timestart' => 1,
418 ];
419
420 $event1 = create_event(array_merge($params, ['name' => 'Event 1', 'timesort' => 1]));
421 $event2 = create_event(array_merge($params, ['name' => 'Event 2', 'timesort' => 2]));
422 $event3 = create_event(array_merge($params, ['name' => 'Event 3', 'timesort' => 3]));
423 $event4 = create_event(array_merge($params, ['name' => 'Event 4', 'timesort' => 4]));
424 $event5 = create_event(array_merge($params, ['name' => 'Event 5', 'timesort' => 5]));
425 $event6 = create_event(array_merge($params, ['name' => 'Event 6', 'timesort' => 6]));
426 $event7 = create_event(array_merge($params, ['name' => 'Event 7', 'timesort' => 7]));
427 $event8 = create_event(array_merge($params, ['name' => 'Event 8', 'timesort' => 8]));
428
429 $params['courseid'] = $course2->id;
430 $params['instance'] = $moduleinstance2->id;
431 $event9 = create_event(array_merge($params, ['name' => 'Event 9', 'timesort' => 1]));
432 $event10 = create_event(array_merge($params, ['name' => 'Event 10', 'timesort' => 2]));
433 $event11 = create_event(array_merge($params, ['name' => 'Event 11', 'timesort' => 3]));
434 $event12 = create_event(array_merge($params, ['name' => 'Event 12', 'timesort' => 4]));
435 $event13 = create_event(array_merge($params, ['name' => 'Event 13', 'timesort' => 5]));
436 $event14 = create_event(array_merge($params, ['name' => 'Event 14', 'timesort' => 6]));
437 $event15 = create_event(array_merge($params, ['name' => 'Event 15', 'timesort' => 7]));
438 $event16 = create_event(array_merge($params, ['name' => 'Event 16', 'timesort' => 8]));
439
440 $result = api::get_action_events_by_course($course1, 3, 6);
441
442 $this->assertCount(4, $result);
443 $this->assertEquals('Event 3', $result[0]->name);
444 $this->assertEquals('Event 4', $result[1]->name);
445 $this->assertEquals('Event 5', $result[2]->name);
446 $this->assertEquals('Event 6', $result[3]->name);
447
448 $result = api::get_action_events_by_course($course1, 10, 15);
449
450 $this->assertEmpty($result);
451 }
452
453 /**
454 * Requesting calendar events for a course and within a given time range and a limit
455 * and offset should return the number of events up to the given limit value that have
456 * a sort time between the lower and uppper time bound (inclusive) where the result
457 * set is shifted by the offset value.
458 *
459 * If there are no events in the given time range then an empty result set should be
460 * returned.
461 */
462 public function test_get_calendar_action_events_by_course_time_limit_offset() {
463 $user = $this->getDataGenerator()->create_user();
464 $course1 = $this->getDataGenerator()->create_course();
465 $course2 = $this->getDataGenerator()->create_course();
466 $generator = $this->getDataGenerator()->get_plugin_generator('mod_assign');
467 $moduleinstance1 = $generator->create_instance(['course' => $course1->id]);
468 $moduleinstance2 = $generator->create_instance(['course' => $course2->id]);
469
470 $this->getDataGenerator()->enrol_user($user->id, $course1->id);
471 $this->getDataGenerator()->enrol_user($user->id, $course2->id);
472 $this->resetAfterTest(true);
473 $this->setUser($user);
474
475 $params = [
476 'type' => CALENDAR_EVENT_TYPE_ACTION,
477 'modulename' => 'assign',
478 'instance' => $moduleinstance1->id,
479 'userid' => $user->id,
480 'courseid' => $course1->id,
481 'eventtype' => 'user',
482 'repeats' => 0,
483 'timestart' => 1,
484 ];
485
486 $event1 = create_event(array_merge($params, ['name' => 'Event 1', 'timesort' => 1]));
487 $event2 = create_event(array_merge($params, ['name' => 'Event 2', 'timesort' => 2]));
488 $event3 = create_event(array_merge($params, ['name' => 'Event 3', 'timesort' => 3]));
489 $event4 = create_event(array_merge($params, ['name' => 'Event 4', 'timesort' => 4]));
490 $event5 = create_event(array_merge($params, ['name' => 'Event 5', 'timesort' => 5]));
491 $event6 = create_event(array_merge($params, ['name' => 'Event 6', 'timesort' => 6]));
492 $event7 = create_event(array_merge($params, ['name' => 'Event 7', 'timesort' => 7]));
493 $event8 = create_event(array_merge($params, ['name' => 'Event 8', 'timesort' => 8]));
494
495 $params['courseid'] = $course2->id;
496 $params['instance'] = $moduleinstance2->id;
497 $event9 = create_event(array_merge($params, ['name' => 'Event 9', 'timesort' => 1]));
498 $event10 = create_event(array_merge($params, ['name' => 'Event 10', 'timesort' => 2]));
499 $event11 = create_event(array_merge($params, ['name' => 'Event 11', 'timesort' => 3]));
500 $event12 = create_event(array_merge($params, ['name' => 'Event 12', 'timesort' => 4]));
501 $event13 = create_event(array_merge($params, ['name' => 'Event 13', 'timesort' => 5]));
502 $event14 = create_event(array_merge($params, ['name' => 'Event 14', 'timesort' => 6]));
503 $event15 = create_event(array_merge($params, ['name' => 'Event 15', 'timesort' => 7]));
504 $event16 = create_event(array_merge($params, ['name' => 'Event 16', 'timesort' => 8]));
505
42ac7ac6 506 $result = api::get_action_events_by_course($course1, 2, 7, $event3->id, 2);
39e7bbae
RW
507
508 $this->assertCount(2, $result);
509 $this->assertEquals('Event 4', $result[0]->name);
510 $this->assertEquals('Event 5', $result[1]->name);
511
42ac7ac6 512 $result = api::get_action_events_by_course($course1, 2, 7, $event5->id, 2);
39e7bbae
RW
513
514 $this->assertCount(2, $result);
515 $this->assertEquals('Event 6', $result[0]->name);
516 $this->assertEquals('Event 7', $result[1]->name);
517
42ac7ac6 518 $result = api::get_action_events_by_course($course1, 2, 7, $event7->id, 2);
39e7bbae
RW
519
520 $this->assertEmpty($result);
521 }
42ac7ac6
RW
522
523 /**
524 * Test that get_action_events_by_courses will return a list of events for each
525 * course you provided as long as the user is enrolled in the course.
526 */
527 public function test_get_action_events_by_courses() {
528 $user = $this->getDataGenerator()->create_user();
529 $course1 = $this->getDataGenerator()->create_course();
530 $course2 = $this->getDataGenerator()->create_course();
531 $course3 = $this->getDataGenerator()->create_course();
532 $generator = $this->getDataGenerator()->get_plugin_generator('mod_assign');
533 $moduleinstance1 = $generator->create_instance(['course' => $course1->id]);
534 $moduleinstance2 = $generator->create_instance(['course' => $course2->id]);
535 $moduleinstance3 = $generator->create_instance(['course' => $course3->id]);
536
537 $this->getDataGenerator()->enrol_user($user->id, $course1->id);
538 $this->getDataGenerator()->enrol_user($user->id, $course2->id);
539 $this->getDataGenerator()->enrol_user($user->id, $course3->id);
540 $this->resetAfterTest(true);
541 $this->setUser($user);
542
543 $params = [
544 'type' => CALENDAR_EVENT_TYPE_ACTION,
545 'modulename' => 'assign',
546 'instance' => $moduleinstance1->id,
547 'userid' => $user->id,
548 'courseid' => $course1->id,
549 'eventtype' => 'user',
550 'repeats' => 0,
551 'timestart' => 1,
552 ];
553
554 $event1 = create_event(array_merge($params, ['name' => 'Event 1', 'timesort' => 1]));
555 $event2 = create_event(array_merge($params, ['name' => 'Event 2', 'timesort' => 2]));
556
557 $params['courseid'] = $course2->id;
558 $params['instance'] = $moduleinstance2->id;
559 $event3 = create_event(array_merge($params, ['name' => 'Event 3', 'timesort' => 3]));
560 $event4 = create_event(array_merge($params, ['name' => 'Event 4', 'timesort' => 4]));
561 $event5 = create_event(array_merge($params, ['name' => 'Event 5', 'timesort' => 5]));
562
563 $params['courseid'] = $course3->id;
564 $params['instance'] = $moduleinstance3->id;
565 $event6 = create_event(array_merge($params, ['name' => 'Event 6', 'timesort' => 6]));
566 $event7 = create_event(array_merge($params, ['name' => 'Event 7', 'timesort' => 7]));
567 $event8 = create_event(array_merge($params, ['name' => 'Event 8', 'timesort' => 8]));
568 $event9 = create_event(array_merge($params, ['name' => 'Event 9', 'timesort' => 9]));
569
570 $result = api::get_action_events_by_courses([], 1);
571
572 $this->assertEmpty($result);
573
574 $result = api::get_action_events_by_courses([$course1], 3);
575
576 $this->assertEmpty($result[$course1->id]);
577
578 $result = api::get_action_events_by_courses([$course1], 1);
579
580 $this->assertCount(2, $result[$course1->id]);
581 $this->assertEquals('Event 1', $result[$course1->id][0]->name);
582 $this->assertEquals('Event 2', $result[$course1->id][1]->name);
583
584 $result = api::get_action_events_by_courses([$course1, $course2], 1);
585
586 $this->assertCount(2, $result[$course1->id]);
587 $this->assertEquals('Event 1', $result[$course1->id][0]->name);
588 $this->assertEquals('Event 2', $result[$course1->id][1]->name);
589 $this->assertCount(3, $result[$course2->id]);
590 $this->assertEquals('Event 3', $result[$course2->id][0]->name);
591 $this->assertEquals('Event 4', $result[$course2->id][1]->name);
592 $this->assertEquals('Event 5', $result[$course2->id][2]->name);
593
594 $result = api::get_action_events_by_courses([$course1, $course2], 2, 4);
595
596 $this->assertCount(1, $result[$course1->id]);
597 $this->assertEquals('Event 2', $result[$course1->id][0]->name);
598 $this->assertCount(2, $result[$course2->id]);
599 $this->assertEquals('Event 3', $result[$course2->id][0]->name);
600 $this->assertEquals('Event 4', $result[$course2->id][1]->name);
601
602 $result = api::get_action_events_by_courses([$course1, $course2, $course3], 1, null, 1);
603
604 $this->assertCount(1, $result[$course1->id]);
605 $this->assertEquals('Event 1', $result[$course1->id][0]->name);
606 $this->assertCount(1, $result[$course2->id]);
607 $this->assertEquals('Event 3', $result[$course2->id][0]->name);
608 $this->assertCount(1, $result[$course3->id]);
609 $this->assertEquals('Event 6', $result[$course3->id][0]->name);
610 }
3607961d 611}