MDL-58424 core_calendar: removed unused get_action_events_* functions
[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 }
3607961d 609}