MDL-57503 calendar: external function for action events by courses
[moodle.git] / calendar / tests / externallib_test.php
CommitLineData
644a7ef7
AA
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/**
18 * External calendar functions unit tests
19 *
20 * @package core_calendar
21 * @category external
22 * @copyright 2012 Ankit Agarwal
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 */
25
26defined('MOODLE_INTERNAL') || die();
27
28global $CFG;
29
30require_once($CFG->dirroot . '/webservice/tests/helpers.php');
31
32/**
33 * External course functions unit tests
34 *
35 * @package core_calendar
36 * @category external
37 * @copyright 2012 Ankit Agarwal
38 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
39 * @since Moodle 2.5
40 */
8252b7c2 41class core_calendar_externallib_testcase extends externallib_advanced_testcase {
644a7ef7
AA
42
43 /**
44 * Tests set up
45 */
46 protected function setUp() {
47 global $CFG;
48 require_once($CFG->dirroot . '/calendar/externallib.php');
49 }
50
51 /** Create calendar events or update them
52 * Set $prop->id, if you want to do an update instead of creating an new event
53 *
54 * @param string $name Event title
55 * @param int $userid User id
56 * @param string $type Event type
57 * @param int $repeats Number of repeated events to create
58 * @param int $timestart Time stamp of the event start
59 * @param mixed $prop List of event properties as array or object
60 * @return mixed Event object or false;
61 * @since Moodle 2.5
62 */
63
64 public static function create_calendar_event($name, $userid = 0, $type = 'user', $repeats = 0, $timestart = null, $prop = null) {
f52459bb 65 global $CFG, $DB, $SITE;
644a7ef7
AA
66
67 require_once("$CFG->dirroot/calendar/lib.php");
68 if (!empty($prop)) {
261a41f1
AA
69 if (is_array($prop)) {
70 $prop = (object)$prop;
71 }
644a7ef7
AA
72 } else {
73 $prop = new stdClass();
74 }
75 $prop->name = $name;
76 if (empty($prop->eventtype)) {
77 $prop->eventtype = $type;
78 }
79 if (empty($prop->repeats)) {
80 $prop->repeats = $repeats;
81 }
82 if (empty($prop->timestart)) {
83 $prop->timestart = time();
84 }
85 if (empty($prop->timeduration)) {
86 $prop->timeduration = 0;
87 }
4139f131
RW
88 if (empty($prop->timesort)) {
89 $prop->timesort = 0;
90 }
91 if (empty($prop->type)) {
92 $prop->type = CALENDAR_EVENT_TYPE_STANDARD;
93 }
644a7ef7
AA
94 if (empty($prop->repeats)) {
95 $prop->repeat = 0;
96 } else {
97 $prop->repeat = 1;
98 }
99 if (empty($prop->userid)) {
261a41f1 100 if (!empty($userid)) {
644a7ef7 101 $prop->userid = $userid;
261a41f1
AA
102 } else {
103 return false;
104 }
644a7ef7 105 }
5ac3b20d 106 if (!isset($prop->courseid)) {
644a7ef7
AA
107 $prop->courseid = $SITE->id;
108 }
f52459bb
JP
109
110 // Determine event priority.
111 if ($prop->courseid == 0 && isset($prop->groupid) && $prop->groupid == 0 && !empty($prop->userid)) {
112 // User override event.
113 $prop->priority = CALENDAR_EVENT_USER_OVERRIDE_PRIORITY;
114 } else if ($prop->courseid != $SITE->id && !empty($prop->groupid)) {
115 // Group override event.
116 $priorityparams = ['courseid' => $prop->courseid, 'groupid' => $prop->groupid];
117 // Group override event with the highest priority.
118 $groupevents = $DB->get_records('event', $priorityparams, 'priority DESC', 'id, priority', 0, 1);
119 $priority = 1;
120 if (!empty($groupevents)) {
121 $event = reset($groupevents);
122 if (!empty($event->priority)) {
123 $priority = $event->priority + 1;
124 }
125 }
126 $prop->priority = $priority;
127 }
128
e057f279 129 $event = new \core_calendar\event($prop);
644a7ef7
AA
130 return $event->create($prop);
131 }
132
133 public function test_create_calendar_events () {
134 global $DB, $USER;
135
136 $this->setAdminUser();
137 $this->resetAfterTest();
138 $prevcount = count($DB->get_records("event"));
139
140 // Create a few events and do asserts.
141 $this->create_calendar_event('test', $USER->id);
9dadb590
AA
142 $where = $DB->sql_compare_text('name') ." = ?";
143 $count = count($DB->get_records_select("event", $where, array('test')));
644a7ef7
AA
144 $this->assertEquals(1, $count);
145 $aftercount = count($DB->get_records("event"));
146 $this->assertEquals($prevcount + 1, $aftercount);
147
148 $this->create_calendar_event('user', $USER->id, 'user', 3);
9dadb590
AA
149 $where = $DB->sql_compare_text('name') ." = ?";
150 $count = count($DB->get_records_select("event", $where, array('user')));
151
644a7ef7
AA
152 $this->assertEquals(3, $count);
153 $aftercount = count($DB->get_records("event"));
154 $this->assertEquals($prevcount + 4, $aftercount);
155
156 }
157
158 /**
261a41f1 159 * Test delete_calendar_events
52f3e060
RT
160 *
161 * @expectedException moodle_exception
644a7ef7
AA
162 */
163 public function test_delete_calendar_events() {
164 global $DB, $USER;
165
166 $this->resetAfterTest(true);
167 $this->setAdminUser();
168
169 // Create a few stuff to test with.
170 $user = $this->getDataGenerator()->create_user();
171 $course = $this->getDataGenerator()->create_course();
172 $record = new stdClass();
173 $record->courseid = $course->id;
174 $group = $this->getDataGenerator()->create_group($record);
175
176 $notdeletedcount = $DB->count_records('event');
177
178 // Let's create a few events.
179 $siteevent = $this->create_calendar_event('site', $USER->id, 'site');
180 $record = new stdClass();
181 $record->courseid = $course->id;
182 $courseevent = $this->create_calendar_event('course', $USER->id, 'course', 2, time(), $record);
183 $userevent = $this->create_calendar_event('user', $USER->id);
184 $record = new stdClass();
185 $record->courseid = $course->id;
186 $record->groupid = $group->id;
187 $groupevent = $this->create_calendar_event('group', $USER->id, 'group', 0, time(), $record);
188
189 // Now lets try to delete stuff with proper rights.
190 $events = array(
191 array('eventid' => $siteevent->id, 'repeat' => 0),
192 array('eventid' => $courseevent->id, 'repeat' => 1),
193 array('eventid' => $userevent->id, 'repeat' => 0),
194 array('eventid' => $groupevent->id, 'repeat' => 0)
195 );
196 core_calendar_external::delete_calendar_events($events);
197
198 // Check to see if things were deleted properly.
199 $deletedcount = $DB->count_records('event');
200 $this->assertEquals($notdeletedcount, $deletedcount);
201
202 // Let's create a few events.
203 $siteevent = $this->create_calendar_event('site', $USER->id, 'site');
204 $record = new stdClass();
205 $record->courseid = $course->id;
206 $courseevent = $this->create_calendar_event('course', $USER->id, 'course', 3, time(), $record);
207 $userevent = $this->create_calendar_event('user', $USER->id);
208 $record = new stdClass();
209 $record->courseid = $course->id;
210 $record->groupid = $group->id;
211 $groupevent = $this->create_calendar_event('group', $USER->id, 'group', 0, time(), $record);
212
213 $this->setuser($user);
214 $sitecontext = context_system::instance();
215 $coursecontext = context_course::instance($course->id);
216 $usercontext = context_user::instance($user->id);
217 $role = $DB->get_record('role', array('shortname' => 'student'));
218 $this->getDataGenerator()->enrol_user($user->id, $course->id, $role->id);
219
220 // Remove all caps.
221 $this->unassignUserCapability('moodle/calendar:manageentries', $sitecontext->id, $role->id);
222 $this->unassignUserCapability('moodle/calendar:manageentries', $coursecontext->id, $role->id);
223 $this->unassignUserCapability('moodle/calendar:managegroupentries', $coursecontext->id, $role->id);
224 $this->unassignUserCapability('moodle/calendar:manageownentries', $usercontext->id, $role->id);
225
226 // Assign proper caps and attempt delete.
227 $this->assignUserCapability('moodle/calendar:manageentries', $sitecontext->id, $role->id);
228 $events = array(
229 array('eventid' => $siteevent->id, 'repeat' => 0),
230 );
231 core_calendar_external::delete_calendar_events($events);
232 $deletedcount = $DB->count_records('event');
233 $count = $notdeletedcount+5;
234 $this->assertEquals($count, $deletedcount);
235
236 $this->assignUserCapability('moodle/calendar:manageentries', $sitecontext->id, $role->id);
237 $events = array(
238 array('eventid' => $courseevent->id, 'repeat' => 0),
239 );
240 core_calendar_external::delete_calendar_events($events);
241 $deletedcount = $DB->count_records('event');
242 $count = $notdeletedcount+4;
243 $this->assertEquals($count, $deletedcount);
244
245 $this->assignUserCapability('moodle/calendar:manageownentries', $usercontext->id, $role->id);
246 $events = array(
247 array('eventid' => $userevent->id, 'repeat' => 0),
248 );
249 core_calendar_external::delete_calendar_events($events);
250 $deletedcount = $DB->count_records('event');
251 $count = $notdeletedcount+3;
252 $this->assertEquals($count, $deletedcount);
253
254 $this->assignUserCapability('moodle/calendar:managegroupentries', $coursecontext->id, $role->id);
255 $events = array(
256 array('eventid' => $groupevent->id, 'repeat' => 0),
257 );
258 core_calendar_external::delete_calendar_events($events);
259 $deletedcount = $DB->count_records('event');
260 $count = $notdeletedcount+2;
261 $this->assertEquals($count, $deletedcount);
262
263 $notdeletedcount = $deletedcount;
264
265 // Let us try deleting without caps.
266
267 $siteevent = $this->create_calendar_event('site', $USER->id, 'site');
268 $record = new stdClass();
269 $record->courseid = $course->id;
270 $courseevent = $this->create_calendar_event('course', $USER->id, 'course', 3, time(), $record);
271 $userevent = $this->create_calendar_event('user', $USER->id);
272 $record = new stdClass();
273 $record->courseid = $course->id;
274 $record->groupid = $group->id;
275 $groupevent = $this->create_calendar_event('group', $USER->id, 'group', 0, time(), $record);
276
277 $this->setGuestUser();
52f3e060 278
644a7ef7
AA
279 $events = array(
280 array('eventid' => $siteevent->id, 'repeat' => 0),
281 array('eventid' => $courseevent->id, 'repeat' => 0),
282 array('eventid' => $userevent->id, 'repeat' => 0),
283 array('eventid' => $groupevent->id, 'repeat' => 0)
284 );
285 core_calendar_external::delete_calendar_events($events);
286 }
287
261a41f1
AA
288 /**
289 * Test get_calendar_events
7c30224a
AA
290 */
291 public function test_get_calendar_events() {
292 global $DB, $USER;
293
294 $this->resetAfterTest(true);
f52459bb 295 set_config('calendar_adminseesall', 1);
7c30224a
AA
296 $this->setAdminUser();
297
298 // Create a few stuff to test with.
299 $user = $this->getDataGenerator()->create_user();
300 $course = $this->getDataGenerator()->create_course();
301 $record = new stdClass();
302 $record->courseid = $course->id;
303 $group = $this->getDataGenerator()->create_group($record);
304
305 $beforecount = $DB->count_records('event');
306
307 // Let's create a few events.
261a41f1 308 $siteevent = $this->create_calendar_event('site', $USER->id, 'site');
871988b0
JL
309
310 // This event will have description with an inline fake image.
311 $draftidfile = file_get_unused_draft_itemid();
312 $usercontext = context_course::instance($course->id);
313 $filerecord = array(
314 'contextid' => $usercontext->id,
315 'component' => 'user',
316 'filearea' => 'draft',
317 'itemid' => $draftidfile,
318 'filepath' => '/',
319 'filename' => 'fakeimage.png',
320 );
321 $fs = get_file_storage();
322 $fs->create_file_from_string($filerecord, 'img contents');
323
7c30224a
AA
324 $record = new stdClass();
325 $record->courseid = $course->id;
f52459bb 326 $record->groupid = 0;
871988b0
JL
327 $record->description = array(
328 'format' => FORMAT_HTML,
329 'text' => 'Text with img <img src="@@PLUGINFILE@@/fakeimage.png">',
330 'itemid' => $draftidfile
331 );
261a41f1 332 $courseevent = $this->create_calendar_event('course', $USER->id, 'course', 2, time(), $record);
f52459bb
JP
333
334 $record = new stdClass();
335 $record->courseid = 0;
336 $record->groupid = 0;
337 $userevent = $this->create_calendar_event('user', $USER->id, 'user', 0, time(), $record);
338
7c30224a
AA
339 $record = new stdClass();
340 $record->courseid = $course->id;
341 $record->groupid = $group->id;
261a41f1 342 $groupevent = $this->create_calendar_event('group', $USER->id, 'group', 0, time(), $record);
7c30224a
AA
343
344 $paramevents = array ('eventids' => array($siteevent->id), 'courseids' => array($course->id), 'groupids' => array($group->id));
345 $options = array ('siteevents' => true, 'userevents' => true);
346 $events = core_calendar_external::get_calendar_events($paramevents, $options);
261a41f1 347 $events = external_api::clean_returnvalue(core_calendar_external::get_calendar_events_returns(), $events);
7c30224a
AA
348
349 // Check to see if we got all events.
ef5b65da 350 $this->assertEquals(5, count($events['events']));
261a41f1 351 $this->assertEquals(0, count($events['warnings']));
7c30224a
AA
352 $options = array ('siteevents' => true, 'userevents' => true, 'timeend' => time() + 7*WEEKSECS);
353 $events = core_calendar_external::get_calendar_events($paramevents, $options);
261a41f1
AA
354 $events = external_api::clean_returnvalue(core_calendar_external::get_calendar_events_returns(), $events);
355 $this->assertEquals(5, count($events['events']));
356 $this->assertEquals(0, count($events['warnings']));
7c30224a 357
871988b0
JL
358 // Expect the same URL in the description of two different events (because they are repeated).
359 $coursecontext = context_course::instance($course->id);
360 $expectedurl = "webservice/pluginfile.php/$coursecontext->id/calendar/event_description/$courseevent->id/fakeimage.png";
361 $withdescription = 0;
362 foreach ($events['events'] as $event) {
363 if (!empty($event['description'])) {
364 $withdescription++;
365 $this->assertContains($expectedurl, $event['description']);
366 }
367 }
368 $this->assertEquals(2, $withdescription);
369
7c30224a 370 // Let's play around with caps.
f52459bb
JP
371
372 // Create user event for the user $user.
373 $record = new stdClass();
374 $record->courseid = 0;
375 $record->groupid = 0;
376 $this->create_calendar_event('user', $user->id, 'user', 0, time(), $record);
377
7c30224a
AA
378 $this->setUser($user);
379 $events = core_calendar_external::get_calendar_events($paramevents, $options);
261a41f1
AA
380 $events = external_api::clean_returnvalue(core_calendar_external::get_calendar_events_returns(), $events);
381 $this->assertEquals(2, count($events['events'])); // site, user.
382 $this->assertEquals(2, count($events['warnings'])); // course, group.
7c30224a
AA
383
384 $role = $DB->get_record('role', array('shortname' => 'student'));
385 $this->getDataGenerator()->enrol_user($user->id, $course->id, $role->id);
386 $events = core_calendar_external::get_calendar_events($paramevents, $options);
261a41f1
AA
387 $events = external_api::clean_returnvalue(core_calendar_external::get_calendar_events_returns(), $events);
388 $this->assertEquals(4, count($events['events'])); // site, user, both course events.
389 $this->assertEquals(1, count($events['warnings'])); // group.
7c30224a 390
ef5b65da 391 $options = array ('siteevents' => true, 'userevents' => true, 'timeend' => time() + HOURSECS);
7c30224a 392 $events = core_calendar_external::get_calendar_events($paramevents, $options);
261a41f1
AA
393 $events = external_api::clean_returnvalue(core_calendar_external::get_calendar_events_returns(), $events);
394 $this->assertEquals(3, count($events['events'])); // site, user, one course event.
395 $this->assertEquals(1, count($events['warnings'])); // group.
7c30224a
AA
396
397 groups_add_member($group, $user);
398 $events = core_calendar_external::get_calendar_events($paramevents, $options);
261a41f1
AA
399 $events = external_api::clean_returnvalue(core_calendar_external::get_calendar_events_returns(), $events);
400 $this->assertEquals(4, count($events['events'])); // site, user, group, one course event.
401 $this->assertEquals(0, count($events['warnings']));
7c30224a
AA
402
403 $paramevents = array ('courseids' => array($course->id), 'groupids' => array($group->id));
404 $events = core_calendar_external::get_calendar_events($paramevents, $options);
e2c85157 405 $events = external_api::clean_returnvalue(core_calendar_external::get_calendar_events_returns(), $events);
261a41f1
AA
406 $this->assertEquals(4, count($events['events'])); // site, user, group, one course event.
407 $this->assertEquals(0, count($events['warnings']));
7c30224a 408
261a41f1 409 $paramevents = array ('groupids' => array($group->id, 23));
7c30224a 410 $events = core_calendar_external::get_calendar_events($paramevents, $options);
261a41f1
AA
411 $events = external_api::clean_returnvalue(core_calendar_external::get_calendar_events_returns(), $events);
412 $this->assertEquals(3, count($events['events'])); // site, user, group.
413 $this->assertEquals(1, count($events['warnings']));
7c30224a 414
261a41f1 415 $paramevents = array ('courseids' => array(23));
7c30224a 416 $events = core_calendar_external::get_calendar_events($paramevents, $options);
261a41f1
AA
417 $events = external_api::clean_returnvalue(core_calendar_external::get_calendar_events_returns(), $events);
418 $this->assertEquals(2, count($events['events'])); // site, user.
419 $this->assertEquals(1, count($events['warnings']));
7c30224a
AA
420
421 $paramevents = array ();
422 $options = array ('siteevents' => false, 'userevents' => false, 'timeend' => time() + 7*WEEKSECS);
423 $events = core_calendar_external::get_calendar_events($paramevents, $options);
261a41f1
AA
424 $events = external_api::clean_returnvalue(core_calendar_external::get_calendar_events_returns(), $events);
425 $this->assertEquals(0, count($events['events'])); // nothing returned.
426 $this->assertEquals(0, count($events['warnings']));
7c30224a
AA
427
428 $paramevents = array ('eventids' => array($siteevent->id, $groupevent->id));
429 $options = array ('siteevents' => false, 'userevents' => false, 'timeend' => time() + 7*WEEKSECS);
430 $events = core_calendar_external::get_calendar_events($paramevents, $options);
261a41f1
AA
431 $events = external_api::clean_returnvalue(core_calendar_external::get_calendar_events_returns(), $events);
432 $this->assertEquals(2, count($events['events'])); // site, group.
433 $this->assertEquals(0, count($events['warnings']));
7c30224a
AA
434
435 $paramevents = array ('eventids' => array($siteevent->id));
436 $events = core_calendar_external::get_calendar_events($paramevents, $options);
261a41f1
AA
437 $events = external_api::clean_returnvalue(core_calendar_external::get_calendar_events_returns(), $events);
438 $this->assertEquals(1, count($events['events'])); // site.
439 $this->assertEquals(0, count($events['warnings']));
11dc7c1c
NM
440
441 // Try getting a course event by its id.
442 $paramevents = array ('eventids' => array($courseevent->id));
443 $events = core_calendar_external::get_calendar_events($paramevents, $options);
444 $events = external_api::clean_returnvalue(core_calendar_external::get_calendar_events_returns(), $events);
445 $this->assertEquals(1, count($events['events']));
446 $this->assertEquals(0, count($events['warnings']));
39b85137
JL
447
448 // Now, create an activity event.
449 $this->setAdminUser();
450 $nexttime = time() + DAYSECS;
451 $assign = $this->getDataGenerator()->create_module('assign', array('course' => $course->id, 'duedate' => $nexttime));
452
453 $this->setUser($user);
454 $paramevents = array ('courseids' => array($course->id));
455 $options = array ('siteevents' => true, 'userevents' => true, 'timeend' => time() + WEEKSECS);
456 $events = core_calendar_external::get_calendar_events($paramevents, $options);
457 $events = external_api::clean_returnvalue(core_calendar_external::get_calendar_events_returns(), $events);
458
459 $this->assertCount(5, $events['events']);
460
461 // Hide the assignment.
462 set_coursemodule_visible($assign->cmid, 0);
463 // Empty all the caches that may be affected by this change.
464 accesslib_clear_all_caches_for_unit_testing();
465 course_modinfo::clear_instance_cache();
466
467 $events = core_calendar_external::get_calendar_events($paramevents, $options);
468 $events = external_api::clean_returnvalue(core_calendar_external::get_calendar_events_returns(), $events);
469 // Expect one less.
470 $this->assertCount(4, $events['events']);
7c30224a 471 }
15c0b1cc
AA
472
473 /**
474 * Test core_calendar_external::create_calendar_events
475 */
476 public function test_core_create_calendar_events() {
477 global $DB, $USER, $SITE;
478
479 $this->resetAfterTest(true);
480 $this->setAdminUser();
481
482 // Create a few stuff to test with.
483 $user = $this->getDataGenerator()->create_user();
484 $course = $this->getDataGenerator()->create_course();
485 $record = new stdClass();
486 $record->courseid = $course->id;
487 $group = $this->getDataGenerator()->create_group($record);
488
489 $prevcount = $DB->count_records('event');
490
491 // Let's create a few events.
492 $events = array (
493 array('name' => 'site', 'courseid' => $SITE->id, 'eventtype' => 'site'),
494 array('name' => 'course', 'courseid' => $course->id, 'eventtype' => 'course', 'repeats' => 2),
495 array('name' => 'group', 'courseid' => $course->id, 'groupid' => $group->id, 'eventtype' => 'group'),
496 array('name' => 'user')
497 );
498 $eventsret = core_calendar_external::create_calendar_events($events);
e2c85157 499 $eventsret = external_api::clean_returnvalue(core_calendar_external::create_calendar_events_returns(), $eventsret);
15c0b1cc
AA
500
501 // Check to see if things were created properly.
502 $aftercount = $DB->count_records('event');
503 $this->assertEquals($prevcount + 5, $aftercount);
504 $this->assertEquals(5, count($eventsret['events']));
505 $this->assertEquals(0, count($eventsret['warnings']));
506
507 $sitecontext = context_system::instance();
508 $coursecontext = context_course::instance($course->id);
509
510 $this->setUser($user);
511 $prevcount = $aftercount;
512 $events = array (
513 array('name' => 'course', 'courseid' => $course->id, 'eventtype' => 'course', 'repeats' => 2),
514 array('name' => 'group', 'courseid' => $course->id, 'groupid' => $group->id, 'eventtype' => 'group'),
515 array('name' => 'user')
516 );
517 $role = $DB->get_record('role', array('shortname' => 'student'));
518 $this->getDataGenerator()->enrol_user($user->id, $course->id, $role->id);
519 groups_add_member($group, $user);
520 $this->assignUserCapability('moodle/calendar:manageentries', $coursecontext->id, $role->id);
521 $this->assignUserCapability('moodle/calendar:managegroupentries', $coursecontext->id, $role->id);
522 $eventsret = core_calendar_external::create_calendar_events($events);
523 $eventsret = external_api::clean_returnvalue(core_calendar_external::create_calendar_events_returns(), $eventsret);
524 // Check to see if things were created properly.
525 $aftercount = $DB->count_records('event');
526 $this->assertEquals($prevcount + 4, $aftercount);
527 $this->assertEquals(4, count($eventsret['events']));
528 $this->assertEquals(0, count($eventsret['warnings']));
529
530 // Check to see nothing was created without proper permission.
531 $this->setGuestUser();
532 $prevcount = $DB->count_records('event');
533 $eventsret = core_calendar_external::create_calendar_events($events);
534 $eventsret = external_api::clean_returnvalue(core_calendar_external::create_calendar_events_returns(), $eventsret);
535 $aftercount = $DB->count_records('event');
536 $this->assertEquals($prevcount, $aftercount);
537 $this->assertEquals(0, count($eventsret['events']));
538 $this->assertEquals(3, count($eventsret['warnings']));
539
540 $this->setUser($user);
541 $this->unassignUserCapability('moodle/calendar:manageentries', $coursecontext->id, $role->id);
542 $this->unassignUserCapability('moodle/calendar:managegroupentries', $coursecontext->id, $role->id);
543 $prevcount = $DB->count_records('event');
544 $eventsret = core_calendar_external::create_calendar_events($events);
545 $eventsret = external_api::clean_returnvalue(core_calendar_external::create_calendar_events_returns(), $eventsret);
546 $aftercount = $DB->count_records('event');
547 $this->assertEquals($prevcount + 1, $aftercount); // User event.
548 $this->assertEquals(1, count($eventsret['events']));
549 $this->assertEquals(2, count($eventsret['warnings']));
550 }
4139f131
RW
551
552 /**
553 * Requesting calendar events from a given time should return all events with a sort
554 * time at or after the requested time. All events prior to that time should not
555 * be return.
556 *
557 * If there are no events on or after the given time then an empty result set should
558 * be returned.
559 */
560 function test_get_calendar_action_events_by_timesort_after_time() {
561 $user = $this->getDataGenerator()->create_user();
562 $course = $this->getDataGenerator()->create_course();
563 $generator = $this->getDataGenerator()->get_plugin_generator('mod_assign');
564 $moduleinstance = $generator->create_instance(['course' => $course->id]);
565
566 $this->getDataGenerator()->enrol_user($user->id, $course->id);
567 $this->resetAfterTest(true);
568 $this->setUser($user);
569
570 $params = [
571 'type' => CALENDAR_EVENT_TYPE_ACTION,
572 'modulename' => 'assign',
573 'instance' => $moduleinstance->id,
574 'courseid' => $course->id,
575 ];
576
577 $event1 = $this->create_calendar_event('Event 1', $user->id, 'user', 0, 1, array_merge($params, ['timesort' => 1]));
578 $event2 = $this->create_calendar_event('Event 2', $user->id, 'user', 0, 1, array_merge($params, ['timesort' => 2]));
579 $event3 = $this->create_calendar_event('Event 3', $user->id, 'user', 0, 1, array_merge($params, ['timesort' => 3]));
580 $event4 = $this->create_calendar_event('Event 4', $user->id, 'user', 0, 1, array_merge($params, ['timesort' => 4]));
581 $event5 = $this->create_calendar_event('Event 5', $user->id, 'user', 0, 1, array_merge($params, ['timesort' => 5]));
582 $event6 = $this->create_calendar_event('Event 6', $user->id, 'user', 0, 1, array_merge($params, ['timesort' => 6]));
583 $event7 = $this->create_calendar_event('Event 7', $user->id, 'user', 0, 1, array_merge($params, ['timesort' => 7]));
584 $event8 = $this->create_calendar_event('Event 8', $user->id, 'user', 0, 1, array_merge($params, ['timesort' => 8]));
585
586 $result = core_calendar_external::get_calendar_action_events_by_timesort(5);
587 $result = external_api::clean_returnvalue(
588 core_calendar_external::get_calendar_action_events_by_timesort_returns(),
589 $result
590 );
591 $events = $result['events'];
592
593 $this->assertCount(4, $events);
594 $this->assertEquals('Event 5', $events[0]['name']);
595 $this->assertEquals('Event 6', $events[1]['name']);
596 $this->assertEquals('Event 7', $events[2]['name']);
597 $this->assertEquals('Event 8', $events[3]['name']);
598 $this->assertEquals($event5->id, $result['firstid']);
599 $this->assertEquals($event8->id, $result['lastid']);
600
601 $result = core_calendar_external::get_calendar_action_events_by_timesort(9);
602 $result = external_api::clean_returnvalue(
603 core_calendar_external::get_calendar_action_events_by_timesort_returns(),
604 $result
605 );
606
607 $this->assertEmpty($result['events']);
608 $this->assertNull($result['firstid']);
609 $this->assertNull($result['lastid']);
610 }
611
612 /**
613 * Requesting calendar events before a given time should return all events with a sort
614 * time at or before the requested time (inclusive). All events after that time
615 * should not be returned.
616 *
617 * If there are no events before the given time then an empty result set should be
618 * returned.
619 */
620 function test_get_calendar_action_events_by_timesort_before_time() {
621 $user = $this->getDataGenerator()->create_user();
622 $course = $this->getDataGenerator()->create_course();
623 $generator = $this->getDataGenerator()->get_plugin_generator('mod_assign');
624 $moduleinstance = $generator->create_instance(['course' => $course->id]);
625
626 $this->getDataGenerator()->enrol_user($user->id, $course->id);
627 $this->resetAfterTest(true);
628 $this->setUser($user);
629
630 $params = [
631 'type' => CALENDAR_EVENT_TYPE_ACTION,
632 'modulename' => 'assign',
633 'instance' => $moduleinstance->id,
634 'courseid' => $course->id,
635 ];
636
637 $event1 = $this->create_calendar_event('Event 1', $user->id, 'user', 0, 1, array_merge($params, ['timesort' => 2]));
638 $event2 = $this->create_calendar_event('Event 2', $user->id, 'user', 0, 1, array_merge($params, ['timesort' => 3]));
639 $event3 = $this->create_calendar_event('Event 3', $user->id, 'user', 0, 1, array_merge($params, ['timesort' => 4]));
640 $event4 = $this->create_calendar_event('Event 4', $user->id, 'user', 0, 1, array_merge($params, ['timesort' => 5]));
641 $event5 = $this->create_calendar_event('Event 5', $user->id, 'user', 0, 1, array_merge($params, ['timesort' => 6]));
642 $event6 = $this->create_calendar_event('Event 6', $user->id, 'user', 0, 1, array_merge($params, ['timesort' => 7]));
643 $event7 = $this->create_calendar_event('Event 7', $user->id, 'user', 0, 1, array_merge($params, ['timesort' => 8]));
644 $event8 = $this->create_calendar_event('Event 8', $user->id, 'user', 0, 1, array_merge($params, ['timesort' => 9]));
645
646 $result = core_calendar_external::get_calendar_action_events_by_timesort(null, 5);
647 $result = external_api::clean_returnvalue(
648 core_calendar_external::get_calendar_action_events_by_timesort_returns(),
649 $result
650 );
651 $events = $result['events'];
652
653 $this->assertCount(4, $events);
654 $this->assertEquals('Event 1', $events[0]['name']);
655 $this->assertEquals('Event 2', $events[1]['name']);
656 $this->assertEquals('Event 3', $events[2]['name']);
657 $this->assertEquals('Event 4', $events[3]['name']);
658 $this->assertEquals($event1->id, $result['firstid']);
659 $this->assertEquals($event4->id, $result['lastid']);
660
661 $result = core_calendar_external::get_calendar_action_events_by_timesort(null, 1);
662 $result = external_api::clean_returnvalue(
663 core_calendar_external::get_calendar_action_events_by_timesort_returns(),
664 $result
665 );
666
667 $this->assertEmpty($result['events']);
668 $this->assertNull($result['firstid']);
669 $this->assertNull($result['lastid']);
670 }
671
672 /**
673 * Requesting calendar events within a given time range should return all events with
674 * a sort time between the lower and upper time bound (inclusive).
675 *
676 * If there are no events in the given time range then an empty result set should be
677 * returned.
678 */
679 function test_get_calendar_action_events_by_timesort_time_range() {
680 $user = $this->getDataGenerator()->create_user();
681 $course = $this->getDataGenerator()->create_course();
682 $generator = $this->getDataGenerator()->get_plugin_generator('mod_assign');
683 $moduleinstance = $generator->create_instance(['course' => $course->id]);
684
685 $this->getDataGenerator()->enrol_user($user->id, $course->id);
686 $this->resetAfterTest(true);
687 $this->setUser($user);
688
689 $params = [
690 'type' => CALENDAR_EVENT_TYPE_ACTION,
691 'modulename' => 'assign',
692 'instance' => $moduleinstance->id,
693 'courseid' => $course->id,
694 ];
695
696 $event1 = $this->create_calendar_event('Event 1', $user->id, 'user', 0, 1, array_merge($params, ['timesort' => 1]));
697 $event2 = $this->create_calendar_event('Event 2', $user->id, 'user', 0, 1, array_merge($params, ['timesort' => 2]));
698 $event3 = $this->create_calendar_event('Event 3', $user->id, 'user', 0, 1, array_merge($params, ['timesort' => 3]));
699 $event4 = $this->create_calendar_event('Event 4', $user->id, 'user', 0, 1, array_merge($params, ['timesort' => 4]));
700 $event5 = $this->create_calendar_event('Event 5', $user->id, 'user', 0, 1, array_merge($params, ['timesort' => 5]));
701 $event6 = $this->create_calendar_event('Event 6', $user->id, 'user', 0, 1, array_merge($params, ['timesort' => 6]));
702 $event7 = $this->create_calendar_event('Event 7', $user->id, 'user', 0, 1, array_merge($params, ['timesort' => 7]));
703 $event8 = $this->create_calendar_event('Event 8', $user->id, 'user', 0, 1, array_merge($params, ['timesort' => 8]));
704
705 $result = core_calendar_external::get_calendar_action_events_by_timesort(3, 6);
706 $result = external_api::clean_returnvalue(
707 core_calendar_external::get_calendar_action_events_by_timesort_returns(),
708 $result
709 );
710 $events = $result['events'];
711
712 $this->assertCount(4, $events);
713 $this->assertEquals('Event 3', $events[0]['name']);
714 $this->assertEquals('Event 4', $events[1]['name']);
715 $this->assertEquals('Event 5', $events[2]['name']);
716 $this->assertEquals('Event 6', $events[3]['name']);
717 $this->assertEquals($event3->id, $result['firstid']);
718 $this->assertEquals($event6->id, $result['lastid']);
719
720 $result = core_calendar_external::get_calendar_action_events_by_timesort(10, 15);
721 $result = external_api::clean_returnvalue(
722 core_calendar_external::get_calendar_action_events_by_timesort_returns(),
723 $result
724 );
725
726 $this->assertEmpty($result['events']);
727 $this->assertNull($result['firstid']);
728 $this->assertNull($result['lastid']);
729 }
730
731 /**
732 * Requesting calendar events within a given time range and a limit and offset should return
733 * the number of events up to the given limit value that have a sort time between the lower
734 * and uppper time bound (inclusive) where the result set is shifted by the offset value.
735 *
736 * If there are no events in the given time range then an empty result set should be
737 * returned.
738 */
739 function test_get_calendar_action_events_by_timesort_time_limit_offset() {
740 $user = $this->getDataGenerator()->create_user();
741 $course = $this->getDataGenerator()->create_course();
742 $generator = $this->getDataGenerator()->get_plugin_generator('mod_assign');
743 $moduleinstance = $generator->create_instance(['course' => $course->id]);
744
745 $this->getDataGenerator()->enrol_user($user->id, $course->id);
746 $this->resetAfterTest(true);
747 $this->setUser($user);
748
749 $params = [
750 'type' => CALENDAR_EVENT_TYPE_ACTION,
751 'modulename' => 'assign',
752 'instance' => $moduleinstance->id,
753 'courseid' => $course->id,
754 ];
755
756 $event1 = $this->create_calendar_event('Event 1', $user->id, 'user', 0, 1, array_merge($params, ['timesort' => 1]));
757 $event2 = $this->create_calendar_event('Event 2', $user->id, 'user', 0, 1, array_merge($params, ['timesort' => 2]));
758 $event3 = $this->create_calendar_event('Event 3', $user->id, 'user', 0, 1, array_merge($params, ['timesort' => 3]));
759 $event4 = $this->create_calendar_event('Event 4', $user->id, 'user', 0, 1, array_merge($params, ['timesort' => 4]));
760 $event5 = $this->create_calendar_event('Event 5', $user->id, 'user', 0, 1, array_merge($params, ['timesort' => 5]));
761 $event6 = $this->create_calendar_event('Event 6', $user->id, 'user', 0, 1, array_merge($params, ['timesort' => 6]));
762 $event7 = $this->create_calendar_event('Event 7', $user->id, 'user', 0, 1, array_merge($params, ['timesort' => 7]));
763 $event8 = $this->create_calendar_event('Event 8', $user->id, 'user', 0, 1, array_merge($params, ['timesort' => 8]));
764
765 $result = core_calendar_external::get_calendar_action_events_by_timesort(2, 7, $event3->id, 2);
766 $result = external_api::clean_returnvalue(
767 core_calendar_external::get_calendar_action_events_by_timesort_returns(),
768 $result
769 );
770 $events = $result['events'];
771
772 $this->assertCount(2, $events);
773 $this->assertEquals('Event 4', $events[0]['name']);
774 $this->assertEquals('Event 5', $events[1]['name']);
775 $this->assertEquals($event4->id, $result['firstid']);
776 $this->assertEquals($event5->id, $result['lastid']);
777
778 $result = core_calendar_external::get_calendar_action_events_by_timesort(2, 7, $event5->id, 2);
779 $result = external_api::clean_returnvalue(
780 core_calendar_external::get_calendar_action_events_by_timesort_returns(),
781 $result
782 );
783 $events = $result['events'];
784
785 $this->assertCount(2, $events);
786 $this->assertEquals('Event 6', $events[0]['name']);
787 $this->assertEquals('Event 7', $events[1]['name']);
788 $this->assertEquals($event6->id, $result['firstid']);
789 $this->assertEquals($event7->id, $result['lastid']);
790
791 $result = core_calendar_external::get_calendar_action_events_by_timesort(2, 7, $event7->id, 2);
792 $result = external_api::clean_returnvalue(
793 core_calendar_external::get_calendar_action_events_by_timesort_returns(),
794 $result
795 );
796
797 $this->assertEmpty($result['events']);
798 $this->assertNull($result['firstid']);
799 $this->assertNull($result['lastid']);
800 }
5e026bb0
RW
801
802 /**
803 * Requesting calendar events from a given course and time should return all
804 * events with a sort time at or after the requested time. All events prior
805 * to that time should not be return.
806 *
807 * If there are no events on or after the given time then an empty result set should
808 * be returned.
809 */
810 function test_get_calendar_action_events_by_course_after_time() {
811 $user = $this->getDataGenerator()->create_user();
812 $course1 = $this->getDataGenerator()->create_course();
813 $course2 = $this->getDataGenerator()->create_course();
814 $generator = $this->getDataGenerator()->get_plugin_generator('mod_assign');
815 $instance1 = $generator->create_instance(['course' => $course1->id]);
816 $instance2 = $generator->create_instance(['course' => $course2->id]);
817 $records = [];
818
819 $this->getDataGenerator()->enrol_user($user->id, $course1->id);
820 $this->getDataGenerator()->enrol_user($user->id, $course2->id);
821 $this->resetAfterTest(true);
822 $this->setUser($user);
823
824 for ($i = 1; $i < 19; $i++) {
825 $courseid = ($i < 9) ? $course1->id : $course2->id;
826 $instance = ($i < 9) ? $instance1->id : $instance2->id;
827 $records[] = $this->create_calendar_event(
828 sprintf('Event %d', $i),
829 $user->id,
830 'user',
831 0,
832 1,
833 [
834 'type' => CALENDAR_EVENT_TYPE_ACTION,
835 'courseid' => $courseid,
836 'timesort' => $i,
837 'modulename' => 'assign',
838 'instance' => $instance,
839 ]
840 );
841 }
842
843 $result = core_calendar_external::get_calendar_action_events_by_course($course1->id, 5);
844 $result = external_api::clean_returnvalue(
845 core_calendar_external::get_calendar_action_events_by_course_returns(),
846 $result
847 );
848 $result = $result['events'];
849
850 $this->assertCount(4, $result);
851 $this->assertEquals('Event 5', $result[0]['name']);
852 $this->assertEquals('Event 6', $result[1]['name']);
853 $this->assertEquals('Event 7', $result[2]['name']);
854 $this->assertEquals('Event 8', $result[3]['name']);
855
856 $result = core_calendar_external::get_calendar_action_events_by_course($course1->id, 9);
857 $result = external_api::clean_returnvalue(
858 core_calendar_external::get_calendar_action_events_by_course_returns(),
859 $result
860 );
861 $result = $result['events'];
862
863 $this->assertEmpty($result);
864 }
865
866 /**
867 * Requesting calendar events for a course and before a given time should return
868 * all events with a sort time at or before the requested time (inclusive). All
869 * events after that time should not be returned.
870 *
871 * If there are no events before the given time then an empty result set should be
872 * returned.
873 */
874 function test_get_calendar_action_events_by_course_before_time() {
875 $user = $this->getDataGenerator()->create_user();
876 $course1 = $this->getDataGenerator()->create_course();
877 $course2 = $this->getDataGenerator()->create_course();
878 $generator = $this->getDataGenerator()->get_plugin_generator('mod_assign');
879 $instance1 = $generator->create_instance(['course' => $course1->id]);
880 $instance2 = $generator->create_instance(['course' => $course2->id]);
881 $records = [];
882
883 $this->getDataGenerator()->enrol_user($user->id, $course1->id);
884 $this->getDataGenerator()->enrol_user($user->id, $course2->id);
885 $this->resetAfterTest(true);
886 $this->setUser($user);
887
888 for ($i = 1; $i < 19; $i++) {
889 $courseid = ($i < 9) ? $course1->id : $course2->id;
890 $instance = ($i < 9) ? $instance1->id : $instance2->id;
891 $records[] = $this->create_calendar_event(
892 sprintf('Event %d', $i),
893 $user->id,
894 'user',
895 0,
896 1,
897 [
898 'type' => CALENDAR_EVENT_TYPE_ACTION,
899 'courseid' => $courseid,
900 'timesort' => $i + 1,
901 'modulename' => 'assign',
902 'instance' => $instance,
903 ]
904 );
905 }
906
907 $result = core_calendar_external::get_calendar_action_events_by_course($course1->id, null, 5);
908 $result = external_api::clean_returnvalue(
909 core_calendar_external::get_calendar_action_events_by_course_returns(),
910 $result
911 );
912 $result = $result['events'];
913
914 $this->assertCount(4, $result);
915 $this->assertEquals('Event 1', $result[0]['name']);
916 $this->assertEquals('Event 2', $result[1]['name']);
917 $this->assertEquals('Event 3', $result[2]['name']);
918 $this->assertEquals('Event 4', $result[3]['name']);
919
920 $result = core_calendar_external::get_calendar_action_events_by_course($course1->id, null, 1);
921 $result = external_api::clean_returnvalue(
922 core_calendar_external::get_calendar_action_events_by_course_returns(),
923 $result
924 );
925 $result = $result['events'];
926
927 $this->assertEmpty($result);
928 }
929
930 /**
931 * Requesting calendar events for a course and within a given time range should
932 * return all events with a sort time between the lower and upper time bound
933 * (inclusive).
934 *
935 * If there are no events in the given time range then an empty result set should be
936 * returned.
937 */
938 function test_get_calendar_action_events_by_course_time_range() {
939 $user = $this->getDataGenerator()->create_user();
940 $course1 = $this->getDataGenerator()->create_course();
941 $course2 = $this->getDataGenerator()->create_course();
942 $generator = $this->getDataGenerator()->get_plugin_generator('mod_assign');
943 $instance1 = $generator->create_instance(['course' => $course1->id]);
944 $instance2 = $generator->create_instance(['course' => $course2->id]);
945 $records = [];
946
947 $this->getDataGenerator()->enrol_user($user->id, $course1->id);
948 $this->getDataGenerator()->enrol_user($user->id, $course2->id);
949 $this->resetAfterTest(true);
950 $this->setUser($user);
951
952 for ($i = 1; $i < 19; $i++) {
953 $courseid = ($i < 9) ? $course1->id : $course2->id;
954 $instance = ($i < 9) ? $instance1->id : $instance2->id;
955 $records[] = $this->create_calendar_event(
956 sprintf('Event %d', $i),
957 $user->id,
958 'user',
959 0,
960 1,
961 [
962 'type' => CALENDAR_EVENT_TYPE_ACTION,
963 'courseid' => $courseid,
964 'timesort' => $i,
965 'modulename' => 'assign',
966 'instance' => $instance,
967 ]
968 );
969 }
970
971 $result = core_calendar_external::get_calendar_action_events_by_course($course1->id, 3, 6);
972 $result = external_api::clean_returnvalue(
973 core_calendar_external::get_calendar_action_events_by_course_returns(),
974 $result
975 );
976 $result = $result['events'];
977
978 $this->assertCount(4, $result);
979 $this->assertEquals('Event 3', $result[0]['name']);
980 $this->assertEquals('Event 4', $result[1]['name']);
981 $this->assertEquals('Event 5', $result[2]['name']);
982 $this->assertEquals('Event 6', $result[3]['name']);
983
984 $result = core_calendar_external::get_calendar_action_events_by_course($course1->id, 10, 15);
985 $result = external_api::clean_returnvalue(
986 core_calendar_external::get_calendar_action_events_by_course_returns(),
987 $result
988 );
989 $result = $result['events'];
990
991 $this->assertEmpty($result);
992 }
993
994 /**
995 * Requesting calendar events for a course and within a given time range and a limit
996 * and offset should return the number of events up to the given limit value that have
997 * a sort time between the lower and uppper time bound (inclusive) where the result
998 * set is shifted by the offset value.
999 *
1000 * If there are no events in the given time range then an empty result set should be
1001 * returned.
1002 */
1003 function test_get_calendar_action_events_by_course_time_limit_offset() {
1004 $user = $this->getDataGenerator()->create_user();
1005 $course1 = $this->getDataGenerator()->create_course();
1006 $course2 = $this->getDataGenerator()->create_course();
1007 $generator = $this->getDataGenerator()->get_plugin_generator('mod_assign');
1008 $instance1 = $generator->create_instance(['course' => $course1->id]);
1009 $instance2 = $generator->create_instance(['course' => $course2->id]);
1010 $records = [];
1011
1012 $this->getDataGenerator()->enrol_user($user->id, $course1->id);
1013 $this->getDataGenerator()->enrol_user($user->id, $course2->id);
1014 $this->resetAfterTest(true);
1015 $this->setUser($user);
1016
1017 for ($i = 1; $i < 19; $i++) {
1018 $courseid = ($i < 9) ? $course1->id : $course2->id;
1019 $instance = ($i < 9) ? $instance1->id : $instance2->id;
1020 $records[] = $this->create_calendar_event(
1021 sprintf('Event %d', $i),
1022 $user->id,
1023 'user',
1024 0,
1025 1,
1026 [
1027 'type' => CALENDAR_EVENT_TYPE_ACTION,
1028 'courseid' => $courseid,
1029 'timesort' => $i,
1030 'modulename' => 'assign',
1031 'instance' => $instance,
1032 ]
1033 );
1034 }
1035
1036 $result = core_calendar_external::get_calendar_action_events_by_course(
1037 $course1->id, 2, 7, $records[2]->id, 2);
1038 $result = external_api::clean_returnvalue(
1039 core_calendar_external::get_calendar_action_events_by_course_returns(),
1040 $result
1041 );
1042 $result = $result['events'];
1043
1044 $this->assertCount(2, $result);
1045 $this->assertEquals('Event 4', $result[0]['name']);
1046 $this->assertEquals('Event 5', $result[1]['name']);
1047
1048 $result = core_calendar_external::get_calendar_action_events_by_course(
1049 $course1->id, 2, 7, $records[4]->id, 2);
1050 $result = external_api::clean_returnvalue(
1051 core_calendar_external::get_calendar_action_events_by_course_returns(),
1052 $result
1053 );
1054 $result = $result['events'];
1055
1056 $this->assertCount(2, $result);
1057 $this->assertEquals('Event 6', $result[0]['name']);
1058 $this->assertEquals('Event 7', $result[1]['name']);
1059
1060 $result = core_calendar_external::get_calendar_action_events_by_course(
1061 $course1->id, 2, 7, $records[6]->id, 2);
1062 $result = external_api::clean_returnvalue(
1063 core_calendar_external::get_calendar_action_events_by_course_returns(),
1064 $result
1065 );
1066 $result = $result['events'];
1067
1068 $this->assertEmpty($result);
1069 }
7c30224a 1070}