MDL-60062 quiz: add support for drag drop of calendar events
[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
55f5fcb9
RW
29use \core_calendar\local\event\container;
30
3607961d 31/**
f8443a26 32 * Class contaning unit tests for the calendar local 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 */
f8443a26 38class core_calendar_local_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);
f8443a26 86 $result = \core_calendar\local\api::get_action_events_by_timesort(5);
1575194a
RW
87
88 $this->assertCount(4, $result);
f8443a26
MN
89 $this->assertEquals('Event 5', $result[0]->get_name());
90 $this->assertEquals('Event 6', $result[1]->get_name());
91 $this->assertEquals('Event 7', $result[2]->get_name());
92 $this->assertEquals('Event 8', $result[3]->get_name());
1575194a 93
f8443a26 94 $result = \core_calendar\local\api::get_action_events_by_timesort(9);
1575194a
RW
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);
f8443a26 138 $result = \core_calendar\local\api::get_action_events_by_timesort(null, 5);
1575194a
RW
139
140 $this->assertCount(4, $result);
f8443a26
MN
141 $this->assertEquals('Event 1', $result[0]->get_name());
142 $this->assertEquals('Event 2', $result[1]->get_name());
143 $this->assertEquals('Event 3', $result[2]->get_name());
144 $this->assertEquals('Event 4', $result[3]->get_name());
1575194a 145
f8443a26 146 $result = \core_calendar\local\api::get_action_events_by_timesort(null, 1);
1575194a
RW
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);
f8443a26 189 $result = \core_calendar\local\api::get_action_events_by_timesort(3, 6);
1575194a
RW
190
191 $this->assertCount(4, $result);
f8443a26
MN
192 $this->assertEquals('Event 3', $result[0]->get_name());
193 $this->assertEquals('Event 4', $result[1]->get_name());
194 $this->assertEquals('Event 5', $result[2]->get_name());
195 $this->assertEquals('Event 6', $result[3]->get_name());
1575194a 196
f8443a26 197 $result = \core_calendar\local\api::get_action_events_by_timesort(10, 15);
1575194a
RW
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);
f8443a26 241 $result = \core_calendar\local\api::get_action_events_by_timesort(2, 7, $event3->id, 2);
1575194a
RW
242
243 $this->assertCount(2, $result);
f8443a26
MN
244 $this->assertEquals('Event 4', $result[0]->get_name());
245 $this->assertEquals('Event 5', $result[1]->get_name());
1575194a 246
f8443a26 247 $result = \core_calendar\local\api::get_action_events_by_timesort(2, 7, $event5->id, 2);
1575194a
RW
248
249 $this->assertCount(2, $result);
f8443a26
MN
250 $this->assertEquals('Event 6', $result[0]->get_name());
251 $this->assertEquals('Event 7', $result[1]->get_name());
1575194a 252
f8443a26 253 $result = \core_calendar\local\api::get_action_events_by_timesort(2, 7, $event7->id, 2);
1575194a
RW
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
f8443a26 310 $result = \core_calendar\local\api::get_action_events_by_course($course1, 5);
39e7bbae
RW
311
312 $this->assertCount(4, $result);
f8443a26
MN
313 $this->assertEquals('Event 5', $result[0]->get_name());
314 $this->assertEquals('Event 6', $result[1]->get_name());
315 $this->assertEquals('Event 7', $result[2]->get_name());
316 $this->assertEquals('Event 8', $result[3]->get_name());
39e7bbae 317
f8443a26 318 $result = \core_calendar\local\api::get_action_events_by_course($course1, 9);
39e7bbae
RW
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
f8443a26 375 $result = \core_calendar\local\api::get_action_events_by_course($course1, null, 5);
39e7bbae
RW
376
377 $this->assertCount(4, $result);
f8443a26
MN
378 $this->assertEquals('Event 1', $result[0]->get_name());
379 $this->assertEquals('Event 2', $result[1]->get_name());
380 $this->assertEquals('Event 3', $result[2]->get_name());
381 $this->assertEquals('Event 4', $result[3]->get_name());
39e7bbae 382
f8443a26 383 $result = \core_calendar\local\api::get_action_events_by_course($course1, null, 1);
39e7bbae
RW
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
f8443a26 440 $result = \core_calendar\local\api::get_action_events_by_course($course1, 3, 6);
39e7bbae
RW
441
442 $this->assertCount(4, $result);
f8443a26
MN
443 $this->assertEquals('Event 3', $result[0]->get_name());
444 $this->assertEquals('Event 4', $result[1]->get_name());
445 $this->assertEquals('Event 5', $result[2]->get_name());
446 $this->assertEquals('Event 6', $result[3]->get_name());
39e7bbae 447
f8443a26 448 $result = \core_calendar\local\api::get_action_events_by_course($course1, 10, 15);
39e7bbae
RW
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
f8443a26 506 $result = \core_calendar\local\api::get_action_events_by_course($course1, 2, 7, $event3->id, 2);
39e7bbae
RW
507
508 $this->assertCount(2, $result);
f8443a26
MN
509 $this->assertEquals('Event 4', $result[0]->get_name());
510 $this->assertEquals('Event 5', $result[1]->get_name());
39e7bbae 511
f8443a26 512 $result = \core_calendar\local\api::get_action_events_by_course($course1, 2, 7, $event5->id, 2);
39e7bbae
RW
513
514 $this->assertCount(2, $result);
f8443a26
MN
515 $this->assertEquals('Event 6', $result[0]->get_name());
516 $this->assertEquals('Event 7', $result[1]->get_name());
39e7bbae 517
f8443a26 518 $result = \core_calendar\local\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
f8443a26 570 $result = \core_calendar\local\api::get_action_events_by_courses([], 1);
42ac7ac6
RW
571
572 $this->assertEmpty($result);
573
f8443a26 574 $result = \core_calendar\local\api::get_action_events_by_courses([$course1], 3);
42ac7ac6
RW
575
576 $this->assertEmpty($result[$course1->id]);
577
f8443a26 578 $result = \core_calendar\local\api::get_action_events_by_courses([$course1], 1);
42ac7ac6
RW
579
580 $this->assertCount(2, $result[$course1->id]);
f8443a26
MN
581 $this->assertEquals('Event 1', $result[$course1->id][0]->get_name());
582 $this->assertEquals('Event 2', $result[$course1->id][1]->get_name());
42ac7ac6 583
f8443a26 584 $result = \core_calendar\local\api::get_action_events_by_courses([$course1, $course2], 1);
42ac7ac6
RW
585
586 $this->assertCount(2, $result[$course1->id]);
f8443a26
MN
587 $this->assertEquals('Event 1', $result[$course1->id][0]->get_name());
588 $this->assertEquals('Event 2', $result[$course1->id][1]->get_name());
42ac7ac6 589 $this->assertCount(3, $result[$course2->id]);
f8443a26
MN
590 $this->assertEquals('Event 3', $result[$course2->id][0]->get_name());
591 $this->assertEquals('Event 4', $result[$course2->id][1]->get_name());
592 $this->assertEquals('Event 5', $result[$course2->id][2]->get_name());
42ac7ac6 593
f8443a26 594 $result = \core_calendar\local\api::get_action_events_by_courses([$course1, $course2], 2, 4);
42ac7ac6
RW
595
596 $this->assertCount(1, $result[$course1->id]);
f8443a26 597 $this->assertEquals('Event 2', $result[$course1->id][0]->get_name());
42ac7ac6 598 $this->assertCount(2, $result[$course2->id]);
f8443a26
MN
599 $this->assertEquals('Event 3', $result[$course2->id][0]->get_name());
600 $this->assertEquals('Event 4', $result[$course2->id][1]->get_name());
42ac7ac6 601
f8443a26 602 $result = \core_calendar\local\api::get_action_events_by_courses([$course1, $course2, $course3], 1, null, 1);
42ac7ac6
RW
603
604 $this->assertCount(1, $result[$course1->id]);
f8443a26 605 $this->assertEquals('Event 1', $result[$course1->id][0]->get_name());
42ac7ac6 606 $this->assertCount(1, $result[$course2->id]);
f8443a26 607 $this->assertEquals('Event 3', $result[$course2->id][0]->get_name());
42ac7ac6 608 $this->assertCount(1, $result[$course3->id]);
f8443a26 609 $this->assertEquals('Event 6', $result[$course3->id][0]->get_name());
42ac7ac6 610 }
2229368a
MN
611
612 /**
613 * Test that the get_legacy_events() function only returns activity events that are enabled.
614 */
615 public function test_get_legacy_events_with_disabled_module() {
616 global $DB;
617
618 $this->setAdminUser();
619
620 $course = $this->getDataGenerator()->create_course();
621
622 $assigngenerator = $this->getDataGenerator()->get_plugin_generator('mod_assign');
623 $assigninstance = $assigngenerator->create_instance(['course' => $course->id]);
624
625 $lessongenerator = $this->getDataGenerator()->get_plugin_generator('mod_lesson');
626 $lessoninstance = $lessongenerator->create_instance(['course' => $course->id]);
627 $student = $this->getDataGenerator()->create_user();
628 $this->getDataGenerator()->enrol_user($student->id, $course->id, 'student');
629 $this->setUser($student);
630 $events = [
631 [
632 'name' => 'Start of assignment',
633 'description' => '',
634 'format' => 1,
635 'courseid' => $course->id,
636 'groupid' => 0,
637 'userid' => 2,
638 'modulename' => 'assign',
639 'instance' => $assigninstance->id,
640 'eventtype' => 'due',
641 'timestart' => time(),
642 'timeduration' => 86400,
643 'visible' => 1
644 ], [
645 'name' => 'Start of lesson',
646 'description' => '',
647 'format' => 1,
648 'courseid' => $course->id,
649 'groupid' => 0,
650 'userid' => 2,
651 'modulename' => 'lesson',
652 'instance' => $lessoninstance->id,
653 'eventtype' => 'end',
654 'timestart' => time(),
655 'timeduration' => 86400,
656 'visible' => 1
657 ]
658 ];
659 foreach ($events as $event) {
660 calendar_event::create($event, false);
661 }
662 $timestart = time() - 60;
663 $timeend = time() + 60;
664
665 // Get all events.
22753c8c 666 $events = calendar_get_legacy_events($timestart, $timeend, true, 0, true);
2229368a
MN
667 $this->assertCount(2, $events);
668
669 // Disable the lesson module.
670 $modulerecord = $DB->get_record('modules', ['name' => 'lesson']);
671 $modulerecord->visible = 0;
672 $DB->update_record('modules', $modulerecord);
673
674 // Check that we only return the assign event.
22753c8c 675 $events = calendar_get_legacy_events($timestart, $timeend, true, 0, true);
2229368a
MN
676 $this->assertCount(1, $events);
677 $event = reset($events);
678 $this->assertEquals('assign', $event->modulename);
679 }
a776415d
CB
680
681 /**
682 * Test for \core_calendar\local\api::get_legacy_events() when there are user and group overrides.
683 */
684 public function test_get_legacy_events_with_overrides() {
685 $generator = $this->getDataGenerator();
686
687 $course = $generator->create_course();
688
689 $plugingenerator = $this->getDataGenerator()->get_plugin_generator('mod_assign');
690 if (!isset($params['course'])) {
691 $params['course'] = $course->id;
692 }
693
694 $instance = $plugingenerator->create_instance($params);
695
696 // Create users.
697 $useroverridestudent = $generator->create_user();
698 $group1student = $generator->create_user();
699 $group2student = $generator->create_user();
700 $group12student = $generator->create_user();
701 $nogroupstudent = $generator->create_user();
702
703 // Enrol users.
704 $generator->enrol_user($useroverridestudent->id, $course->id, 'student');
705 $generator->enrol_user($group1student->id, $course->id, 'student');
706 $generator->enrol_user($group2student->id, $course->id, 'student');
707 $generator->enrol_user($group12student->id, $course->id, 'student');
708 $generator->enrol_user($nogroupstudent->id, $course->id, 'student');
709
710 // Create groups.
711 $group1 = $generator->create_group(['courseid' => $course->id]);
712 $group2 = $generator->create_group(['courseid' => $course->id]);
713
714 // Add members to groups.
715 $generator->create_group_member(['groupid' => $group1->id, 'userid' => $group1student->id]);
716 $generator->create_group_member(['groupid' => $group2->id, 'userid' => $group2student->id]);
717 $generator->create_group_member(['groupid' => $group1->id, 'userid' => $group12student->id]);
718 $generator->create_group_member(['groupid' => $group2->id, 'userid' => $group12student->id]);
719 $now = time();
720
721 // Events with the same module name, instance and event type.
722 $events = [
723 [
724 'name' => 'Assignment 1 due date',
725 'description' => '',
726 'format' => 0,
727 'courseid' => $course->id,
728 'groupid' => 0,
729 'userid' => 2,
730 'modulename' => 'assign',
731 'instance' => $instance->id,
732 'eventtype' => 'due',
733 'timestart' => $now,
734 'timeduration' => 0,
735 'visible' => 1
736 ], [
737 'name' => 'Assignment 1 due date - User override',
738 'description' => '',
739 'format' => 1,
740 'courseid' => 0,
741 'groupid' => 0,
742 'userid' => $useroverridestudent->id,
743 'modulename' => 'assign',
744 'instance' => $instance->id,
745 'eventtype' => 'due',
746 'timestart' => $now + 86400,
747 'timeduration' => 0,
748 'visible' => 1,
749 'priority' => CALENDAR_EVENT_USER_OVERRIDE_PRIORITY
750 ], [
751 'name' => 'Assignment 1 due date - Group A override',
752 'description' => '',
753 'format' => 1,
754 'courseid' => $course->id,
755 'groupid' => $group1->id,
756 'userid' => 2,
757 'modulename' => 'assign',
758 'instance' => $instance->id,
759 'eventtype' => 'due',
760 'timestart' => $now + (2 * 86400),
761 'timeduration' => 0,
762 'visible' => 1,
763 'priority' => 1,
764 ], [
765 'name' => 'Assignment 1 due date - Group B override',
766 'description' => '',
767 'format' => 1,
768 'courseid' => $course->id,
769 'groupid' => $group2->id,
770 'userid' => 2,
771 'modulename' => 'assign',
772 'instance' => $instance->id,
773 'eventtype' => 'due',
774 'timestart' => $now + (3 * 86400),
775 'timeduration' => 0,
776 'visible' => 1,
777 'priority' => 2,
778 ],
779 ];
780
781 foreach ($events as $event) {
782 calendar_event::create($event, false);
783 }
784
785 $timestart = $now - 100;
786 $timeend = $now + (3 * 86400);
787 $groups = [$group1->id, $group2->id];
788
789 // Get user override events.
790 $this->setUser($useroverridestudent);
791 $events = calendar_get_legacy_events($timestart, $timeend, $useroverridestudent->id, $groups, $course->id);
792 $this->assertCount(1, $events);
793 $event = reset($events);
794 $this->assertEquals('Assignment 1 due date - User override', $event->name);
795
796 // Get event for user with override but with the timestart and timeend parameters only covering the original event.
797 $events = calendar_get_legacy_events($timestart, $now, $useroverridestudent->id, $groups, $course->id);
798 $this->assertCount(0, $events);
799
800 // Get events for user that does not belong to any group and has no user override events.
801 $this->setUser($nogroupstudent);
802 $events = calendar_get_legacy_events($timestart, $timeend, $nogroupstudent->id, $groups, $course->id);
803 $this->assertCount(1, $events);
804 $event = reset($events);
805 $this->assertEquals('Assignment 1 due date', $event->name);
806
807 // Get events for user that belongs to groups A and B and has no user override events.
808 $this->setUser($group12student);
809 $events = calendar_get_legacy_events($timestart, $timeend, $group12student->id, $groups, $course->id);
810 $this->assertCount(1, $events);
811 $event = reset($events);
812 $this->assertEquals('Assignment 1 due date - Group A override', $event->name);
813
814 // Get events for user that belongs to group A and has no user override events.
815 $this->setUser($group1student);
816 $events = calendar_get_legacy_events($timestart, $timeend, $group1student->id, $groups, $course->id);
817 $this->assertCount(1, $events);
818 $event = reset($events);
819 $this->assertEquals('Assignment 1 due date - Group A override', $event->name);
820
821 // Add repeating events.
822 $repeatingevents = [
823 [
824 'name' => 'Repeating site event',
825 'description' => '',
826 'format' => 1,
827 'courseid' => SITEID,
828 'groupid' => 0,
829 'userid' => 2,
830 'repeatid' => $event->id,
831 'modulename' => '0',
832 'instance' => 0,
833 'eventtype' => 'site',
834 'timestart' => $now + 86400,
835 'timeduration' => 0,
836 'visible' => 1,
837 ],
838 [
839 'name' => 'Repeating site event',
840 'description' => '',
841 'format' => 1,
842 'courseid' => SITEID,
843 'groupid' => 0,
844 'userid' => 2,
845 'repeatid' => $event->id,
846 'modulename' => '0',
847 'instance' => 0,
848 'eventtype' => 'site',
849 'timestart' => $now + (2 * 86400),
850 'timeduration' => 0,
851 'visible' => 1,
852 ],
853 ];
854
855 foreach ($repeatingevents as $event) {
856 calendar_event::create($event, false);
857 }
858
859 // Make sure repeating events are not filtered out.
860 $events = calendar_get_legacy_events($timestart, $timeend, true, true, true);
861 $this->assertCount(3, $events);
862 }
55f5fcb9
RW
863
864 /**
865 * Setting the start date on the calendar event should update the date
866 * of the event but should leave the time of day unchanged.
867 */
868 public function test_update_event_start_day_updates_date() {
869 $generator = $this->getDataGenerator();
870 $user = $generator->create_user();
871 $roleid = $generator->create_role();
872 $context = \context_system::instance();
96283892
RW
873 $originalstarttime = new DateTimeImmutable('2017-01-1T15:00:00+08:00');
874 $newstartdate = new DateTimeImmutable('2018-02-2T10:00:00+08:00');
55f5fcb9
RW
875 $expected = new DateTimeImmutable('2018-02-2T15:00:00+08:00');
876 $mapper = container::get_event_mapper();
877
878 $generator->role_assign($roleid, $user->id, $context->id);
879 assign_capability('moodle/calendar:manageownentries', CAP_ALLOW, $roleid, $context, true);
880
881 $this->setUser($user);
882 $this->resetAfterTest(true);
883
884 $event = create_event([
885 'name' => 'Test event',
886 'userid' => $user->id,
887 'eventtype' => 'user',
888 'repeats' => 0,
96283892 889 'timestart' => $originalstarttime->getTimestamp(),
55f5fcb9
RW
890 ]);
891 $event = $mapper->from_legacy_event_to_event($event);
892
96283892 893 $newEvent = \core_calendar\local\api::update_event_start_day($event, $newstartdate);
55f5fcb9
RW
894 $actual = $newEvent->get_times()->get_start_time();
895
896 $this->assertEquals($expected->getTimestamp(), $actual->getTimestamp());
897 }
898
899 /**
900 * A user should not be able to update the start date of the event
901 * that they don't have the capabilities to modify.
902 */
903 public function test_update_event_start_day_no_permission() {
904 $generator = $this->getDataGenerator();
905 $user = $generator->create_user();
906 $roleid = $generator->create_role();
907 $context = \context_system::instance();
96283892
RW
908 $originalstarttime = new DateTimeImmutable('2017-01-1T15:00:00+08:00');
909 $newstartdate = new DateTimeImmutable('2018-02-2T10:00:00+08:00');
55f5fcb9
RW
910 $expected = new DateTimeImmutable('2018-02-2T15:00:00+08:00');
911 $mapper = container::get_event_mapper();
912
913 $generator->role_assign($roleid, $user->id, $context->id);
914
915 $this->setUser($user);
916 $this->resetAfterTest(true);
917
918 $event = create_event([
919 'name' => 'Test event',
920 'userid' => $user->id,
921 'eventtype' => 'user',
922 'repeats' => 0,
96283892 923 'timestart' => $originalstarttime->getTimestamp(),
55f5fcb9
RW
924 ]);
925 $event = $mapper->from_legacy_event_to_event($event);
926
927 assign_capability('moodle/calendar:manageownentries', CAP_PROHIBIT, $roleid, $context, true);
928 $this->expectException('moodle_exception');
96283892 929 $newEvent = \core_calendar\local\api::update_event_start_day($event, $newstartdate);
55f5fcb9 930 }
3607961d 931}