MDL-57502 core_calendar: add get calendar action events web service
[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 }
88 if (empty($prop->repeats)) {
89 $prop->repeat = 0;
90 } else {
91 $prop->repeat = 1;
92 }
93 if (empty($prop->userid)) {
261a41f1 94 if (!empty($userid)) {
644a7ef7 95 $prop->userid = $userid;
261a41f1
AA
96 } else {
97 return false;
98 }
644a7ef7 99 }
5ac3b20d 100 if (!isset($prop->courseid)) {
644a7ef7
AA
101 $prop->courseid = $SITE->id;
102 }
f52459bb
JP
103
104 // Determine event priority.
105 if ($prop->courseid == 0 && isset($prop->groupid) && $prop->groupid == 0 && !empty($prop->userid)) {
106 // User override event.
107 $prop->priority = CALENDAR_EVENT_USER_OVERRIDE_PRIORITY;
108 } else if ($prop->courseid != $SITE->id && !empty($prop->groupid)) {
109 // Group override event.
110 $priorityparams = ['courseid' => $prop->courseid, 'groupid' => $prop->groupid];
111 // Group override event with the highest priority.
112 $groupevents = $DB->get_records('event', $priorityparams, 'priority DESC', 'id, priority', 0, 1);
113 $priority = 1;
114 if (!empty($groupevents)) {
115 $event = reset($groupevents);
116 if (!empty($event->priority)) {
117 $priority = $event->priority + 1;
118 }
119 }
120 $prop->priority = $priority;
121 }
122
e057f279 123 $event = new \core_calendar\event($prop);
644a7ef7
AA
124 return $event->create($prop);
125 }
126
127 public function test_create_calendar_events () {
128 global $DB, $USER;
129
130 $this->setAdminUser();
131 $this->resetAfterTest();
132 $prevcount = count($DB->get_records("event"));
133
134 // Create a few events and do asserts.
135 $this->create_calendar_event('test', $USER->id);
9dadb590
AA
136 $where = $DB->sql_compare_text('name') ." = ?";
137 $count = count($DB->get_records_select("event", $where, array('test')));
644a7ef7
AA
138 $this->assertEquals(1, $count);
139 $aftercount = count($DB->get_records("event"));
140 $this->assertEquals($prevcount + 1, $aftercount);
141
142 $this->create_calendar_event('user', $USER->id, 'user', 3);
9dadb590
AA
143 $where = $DB->sql_compare_text('name') ." = ?";
144 $count = count($DB->get_records_select("event", $where, array('user')));
145
644a7ef7
AA
146 $this->assertEquals(3, $count);
147 $aftercount = count($DB->get_records("event"));
148 $this->assertEquals($prevcount + 4, $aftercount);
149
150 }
151
152 /**
261a41f1 153 * Test delete_calendar_events
52f3e060
RT
154 *
155 * @expectedException moodle_exception
644a7ef7
AA
156 */
157 public function test_delete_calendar_events() {
158 global $DB, $USER;
159
160 $this->resetAfterTest(true);
161 $this->setAdminUser();
162
163 // Create a few stuff to test with.
164 $user = $this->getDataGenerator()->create_user();
165 $course = $this->getDataGenerator()->create_course();
166 $record = new stdClass();
167 $record->courseid = $course->id;
168 $group = $this->getDataGenerator()->create_group($record);
169
170 $notdeletedcount = $DB->count_records('event');
171
172 // Let's create a few events.
173 $siteevent = $this->create_calendar_event('site', $USER->id, 'site');
174 $record = new stdClass();
175 $record->courseid = $course->id;
176 $courseevent = $this->create_calendar_event('course', $USER->id, 'course', 2, time(), $record);
177 $userevent = $this->create_calendar_event('user', $USER->id);
178 $record = new stdClass();
179 $record->courseid = $course->id;
180 $record->groupid = $group->id;
181 $groupevent = $this->create_calendar_event('group', $USER->id, 'group', 0, time(), $record);
182
183 // Now lets try to delete stuff with proper rights.
184 $events = array(
185 array('eventid' => $siteevent->id, 'repeat' => 0),
186 array('eventid' => $courseevent->id, 'repeat' => 1),
187 array('eventid' => $userevent->id, 'repeat' => 0),
188 array('eventid' => $groupevent->id, 'repeat' => 0)
189 );
190 core_calendar_external::delete_calendar_events($events);
191
192 // Check to see if things were deleted properly.
193 $deletedcount = $DB->count_records('event');
194 $this->assertEquals($notdeletedcount, $deletedcount);
195
196 // Let's create a few events.
197 $siteevent = $this->create_calendar_event('site', $USER->id, 'site');
198 $record = new stdClass();
199 $record->courseid = $course->id;
200 $courseevent = $this->create_calendar_event('course', $USER->id, 'course', 3, time(), $record);
201 $userevent = $this->create_calendar_event('user', $USER->id);
202 $record = new stdClass();
203 $record->courseid = $course->id;
204 $record->groupid = $group->id;
205 $groupevent = $this->create_calendar_event('group', $USER->id, 'group', 0, time(), $record);
206
207 $this->setuser($user);
208 $sitecontext = context_system::instance();
209 $coursecontext = context_course::instance($course->id);
210 $usercontext = context_user::instance($user->id);
211 $role = $DB->get_record('role', array('shortname' => 'student'));
212 $this->getDataGenerator()->enrol_user($user->id, $course->id, $role->id);
213
214 // Remove all caps.
215 $this->unassignUserCapability('moodle/calendar:manageentries', $sitecontext->id, $role->id);
216 $this->unassignUserCapability('moodle/calendar:manageentries', $coursecontext->id, $role->id);
217 $this->unassignUserCapability('moodle/calendar:managegroupentries', $coursecontext->id, $role->id);
218 $this->unassignUserCapability('moodle/calendar:manageownentries', $usercontext->id, $role->id);
219
220 // Assign proper caps and attempt delete.
221 $this->assignUserCapability('moodle/calendar:manageentries', $sitecontext->id, $role->id);
222 $events = array(
223 array('eventid' => $siteevent->id, 'repeat' => 0),
224 );
225 core_calendar_external::delete_calendar_events($events);
226 $deletedcount = $DB->count_records('event');
227 $count = $notdeletedcount+5;
228 $this->assertEquals($count, $deletedcount);
229
230 $this->assignUserCapability('moodle/calendar:manageentries', $sitecontext->id, $role->id);
231 $events = array(
232 array('eventid' => $courseevent->id, 'repeat' => 0),
233 );
234 core_calendar_external::delete_calendar_events($events);
235 $deletedcount = $DB->count_records('event');
236 $count = $notdeletedcount+4;
237 $this->assertEquals($count, $deletedcount);
238
239 $this->assignUserCapability('moodle/calendar:manageownentries', $usercontext->id, $role->id);
240 $events = array(
241 array('eventid' => $userevent->id, 'repeat' => 0),
242 );
243 core_calendar_external::delete_calendar_events($events);
244 $deletedcount = $DB->count_records('event');
245 $count = $notdeletedcount+3;
246 $this->assertEquals($count, $deletedcount);
247
248 $this->assignUserCapability('moodle/calendar:managegroupentries', $coursecontext->id, $role->id);
249 $events = array(
250 array('eventid' => $groupevent->id, 'repeat' => 0),
251 );
252 core_calendar_external::delete_calendar_events($events);
253 $deletedcount = $DB->count_records('event');
254 $count = $notdeletedcount+2;
255 $this->assertEquals($count, $deletedcount);
256
257 $notdeletedcount = $deletedcount;
258
259 // Let us try deleting without caps.
260
261 $siteevent = $this->create_calendar_event('site', $USER->id, 'site');
262 $record = new stdClass();
263 $record->courseid = $course->id;
264 $courseevent = $this->create_calendar_event('course', $USER->id, 'course', 3, time(), $record);
265 $userevent = $this->create_calendar_event('user', $USER->id);
266 $record = new stdClass();
267 $record->courseid = $course->id;
268 $record->groupid = $group->id;
269 $groupevent = $this->create_calendar_event('group', $USER->id, 'group', 0, time(), $record);
270
271 $this->setGuestUser();
52f3e060 272
644a7ef7
AA
273 $events = array(
274 array('eventid' => $siteevent->id, 'repeat' => 0),
275 array('eventid' => $courseevent->id, 'repeat' => 0),
276 array('eventid' => $userevent->id, 'repeat' => 0),
277 array('eventid' => $groupevent->id, 'repeat' => 0)
278 );
279 core_calendar_external::delete_calendar_events($events);
280 }
281
261a41f1
AA
282 /**
283 * Test get_calendar_events
7c30224a
AA
284 */
285 public function test_get_calendar_events() {
286 global $DB, $USER;
287
288 $this->resetAfterTest(true);
f52459bb 289 set_config('calendar_adminseesall', 1);
7c30224a
AA
290 $this->setAdminUser();
291
292 // Create a few stuff to test with.
293 $user = $this->getDataGenerator()->create_user();
294 $course = $this->getDataGenerator()->create_course();
295 $record = new stdClass();
296 $record->courseid = $course->id;
297 $group = $this->getDataGenerator()->create_group($record);
298
299 $beforecount = $DB->count_records('event');
300
301 // Let's create a few events.
261a41f1 302 $siteevent = $this->create_calendar_event('site', $USER->id, 'site');
871988b0
JL
303
304 // This event will have description with an inline fake image.
305 $draftidfile = file_get_unused_draft_itemid();
306 $usercontext = context_course::instance($course->id);
307 $filerecord = array(
308 'contextid' => $usercontext->id,
309 'component' => 'user',
310 'filearea' => 'draft',
311 'itemid' => $draftidfile,
312 'filepath' => '/',
313 'filename' => 'fakeimage.png',
314 );
315 $fs = get_file_storage();
316 $fs->create_file_from_string($filerecord, 'img contents');
317
7c30224a
AA
318 $record = new stdClass();
319 $record->courseid = $course->id;
f52459bb 320 $record->groupid = 0;
871988b0
JL
321 $record->description = array(
322 'format' => FORMAT_HTML,
323 'text' => 'Text with img <img src="@@PLUGINFILE@@/fakeimage.png">',
324 'itemid' => $draftidfile
325 );
261a41f1 326 $courseevent = $this->create_calendar_event('course', $USER->id, 'course', 2, time(), $record);
f52459bb
JP
327
328 $record = new stdClass();
329 $record->courseid = 0;
330 $record->groupid = 0;
331 $userevent = $this->create_calendar_event('user', $USER->id, 'user', 0, time(), $record);
332
7c30224a
AA
333 $record = new stdClass();
334 $record->courseid = $course->id;
335 $record->groupid = $group->id;
261a41f1 336 $groupevent = $this->create_calendar_event('group', $USER->id, 'group', 0, time(), $record);
7c30224a
AA
337
338 $paramevents = array ('eventids' => array($siteevent->id), 'courseids' => array($course->id), 'groupids' => array($group->id));
339 $options = array ('siteevents' => true, 'userevents' => true);
340 $events = core_calendar_external::get_calendar_events($paramevents, $options);
261a41f1 341 $events = external_api::clean_returnvalue(core_calendar_external::get_calendar_events_returns(), $events);
7c30224a
AA
342
343 // Check to see if we got all events.
ef5b65da 344 $this->assertEquals(5, count($events['events']));
261a41f1 345 $this->assertEquals(0, count($events['warnings']));
7c30224a
AA
346 $options = array ('siteevents' => true, 'userevents' => true, 'timeend' => time() + 7*WEEKSECS);
347 $events = core_calendar_external::get_calendar_events($paramevents, $options);
261a41f1
AA
348 $events = external_api::clean_returnvalue(core_calendar_external::get_calendar_events_returns(), $events);
349 $this->assertEquals(5, count($events['events']));
350 $this->assertEquals(0, count($events['warnings']));
7c30224a 351
871988b0
JL
352 // Expect the same URL in the description of two different events (because they are repeated).
353 $coursecontext = context_course::instance($course->id);
354 $expectedurl = "webservice/pluginfile.php/$coursecontext->id/calendar/event_description/$courseevent->id/fakeimage.png";
355 $withdescription = 0;
356 foreach ($events['events'] as $event) {
357 if (!empty($event['description'])) {
358 $withdescription++;
359 $this->assertContains($expectedurl, $event['description']);
360 }
361 }
362 $this->assertEquals(2, $withdescription);
363
7c30224a 364 // Let's play around with caps.
f52459bb
JP
365
366 // Create user event for the user $user.
367 $record = new stdClass();
368 $record->courseid = 0;
369 $record->groupid = 0;
370 $this->create_calendar_event('user', $user->id, 'user', 0, time(), $record);
371
7c30224a
AA
372 $this->setUser($user);
373 $events = core_calendar_external::get_calendar_events($paramevents, $options);
261a41f1
AA
374 $events = external_api::clean_returnvalue(core_calendar_external::get_calendar_events_returns(), $events);
375 $this->assertEquals(2, count($events['events'])); // site, user.
376 $this->assertEquals(2, count($events['warnings'])); // course, group.
7c30224a
AA
377
378 $role = $DB->get_record('role', array('shortname' => 'student'));
379 $this->getDataGenerator()->enrol_user($user->id, $course->id, $role->id);
380 $events = core_calendar_external::get_calendar_events($paramevents, $options);
261a41f1
AA
381 $events = external_api::clean_returnvalue(core_calendar_external::get_calendar_events_returns(), $events);
382 $this->assertEquals(4, count($events['events'])); // site, user, both course events.
383 $this->assertEquals(1, count($events['warnings'])); // group.
7c30224a 384
ef5b65da 385 $options = array ('siteevents' => true, 'userevents' => true, 'timeend' => time() + HOURSECS);
7c30224a 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(3, count($events['events'])); // site, user, one course event.
389 $this->assertEquals(1, count($events['warnings'])); // group.
7c30224a
AA
390
391 groups_add_member($group, $user);
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(4, count($events['events'])); // site, user, group, one course event.
395 $this->assertEquals(0, count($events['warnings']));
7c30224a
AA
396
397 $paramevents = array ('courseids' => array($course->id), 'groupids' => array($group->id));
398 $events = core_calendar_external::get_calendar_events($paramevents, $options);
e2c85157 399 $events = external_api::clean_returnvalue(core_calendar_external::get_calendar_events_returns(), $events);
261a41f1
AA
400 $this->assertEquals(4, count($events['events'])); // site, user, group, one course event.
401 $this->assertEquals(0, count($events['warnings']));
7c30224a 402
261a41f1 403 $paramevents = array ('groupids' => array($group->id, 23));
7c30224a 404 $events = core_calendar_external::get_calendar_events($paramevents, $options);
261a41f1
AA
405 $events = external_api::clean_returnvalue(core_calendar_external::get_calendar_events_returns(), $events);
406 $this->assertEquals(3, count($events['events'])); // site, user, group.
407 $this->assertEquals(1, count($events['warnings']));
7c30224a 408
261a41f1 409 $paramevents = array ('courseids' => array(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(2, count($events['events'])); // site, user.
413 $this->assertEquals(1, count($events['warnings']));
7c30224a
AA
414
415 $paramevents = array ();
416 $options = array ('siteevents' => false, 'userevents' => false, 'timeend' => time() + 7*WEEKSECS);
417 $events = core_calendar_external::get_calendar_events($paramevents, $options);
261a41f1
AA
418 $events = external_api::clean_returnvalue(core_calendar_external::get_calendar_events_returns(), $events);
419 $this->assertEquals(0, count($events['events'])); // nothing returned.
420 $this->assertEquals(0, count($events['warnings']));
7c30224a
AA
421
422 $paramevents = array ('eventids' => array($siteevent->id, $groupevent->id));
423 $options = array ('siteevents' => false, 'userevents' => false, 'timeend' => time() + 7*WEEKSECS);
424 $events = core_calendar_external::get_calendar_events($paramevents, $options);
261a41f1
AA
425 $events = external_api::clean_returnvalue(core_calendar_external::get_calendar_events_returns(), $events);
426 $this->assertEquals(2, count($events['events'])); // site, group.
427 $this->assertEquals(0, count($events['warnings']));
7c30224a
AA
428
429 $paramevents = array ('eventids' => array($siteevent->id));
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(1, count($events['events'])); // site.
433 $this->assertEquals(0, count($events['warnings']));
11dc7c1c
NM
434
435 // Try getting a course event by its id.
436 $paramevents = array ('eventids' => array($courseevent->id));
437 $events = core_calendar_external::get_calendar_events($paramevents, $options);
438 $events = external_api::clean_returnvalue(core_calendar_external::get_calendar_events_returns(), $events);
439 $this->assertEquals(1, count($events['events']));
440 $this->assertEquals(0, count($events['warnings']));
39b85137
JL
441
442 // Now, create an activity event.
443 $this->setAdminUser();
444 $nexttime = time() + DAYSECS;
445 $assign = $this->getDataGenerator()->create_module('assign', array('course' => $course->id, 'duedate' => $nexttime));
446
447 $this->setUser($user);
448 $paramevents = array ('courseids' => array($course->id));
449 $options = array ('siteevents' => true, 'userevents' => true, 'timeend' => time() + WEEKSECS);
450 $events = core_calendar_external::get_calendar_events($paramevents, $options);
451 $events = external_api::clean_returnvalue(core_calendar_external::get_calendar_events_returns(), $events);
452
453 $this->assertCount(5, $events['events']);
454
455 // Hide the assignment.
456 set_coursemodule_visible($assign->cmid, 0);
457 // Empty all the caches that may be affected by this change.
458 accesslib_clear_all_caches_for_unit_testing();
459 course_modinfo::clear_instance_cache();
460
461 $events = core_calendar_external::get_calendar_events($paramevents, $options);
462 $events = external_api::clean_returnvalue(core_calendar_external::get_calendar_events_returns(), $events);
463 // Expect one less.
464 $this->assertCount(4, $events['events']);
7c30224a 465 }
15c0b1cc
AA
466
467 /**
468 * Test core_calendar_external::create_calendar_events
469 */
470 public function test_core_create_calendar_events() {
471 global $DB, $USER, $SITE;
472
473 $this->resetAfterTest(true);
474 $this->setAdminUser();
475
476 // Create a few stuff to test with.
477 $user = $this->getDataGenerator()->create_user();
478 $course = $this->getDataGenerator()->create_course();
479 $record = new stdClass();
480 $record->courseid = $course->id;
481 $group = $this->getDataGenerator()->create_group($record);
482
483 $prevcount = $DB->count_records('event');
484
485 // Let's create a few events.
486 $events = array (
487 array('name' => 'site', 'courseid' => $SITE->id, 'eventtype' => 'site'),
488 array('name' => 'course', 'courseid' => $course->id, 'eventtype' => 'course', 'repeats' => 2),
489 array('name' => 'group', 'courseid' => $course->id, 'groupid' => $group->id, 'eventtype' => 'group'),
490 array('name' => 'user')
491 );
492 $eventsret = core_calendar_external::create_calendar_events($events);
e2c85157 493 $eventsret = external_api::clean_returnvalue(core_calendar_external::create_calendar_events_returns(), $eventsret);
15c0b1cc
AA
494
495 // Check to see if things were created properly.
496 $aftercount = $DB->count_records('event');
497 $this->assertEquals($prevcount + 5, $aftercount);
498 $this->assertEquals(5, count($eventsret['events']));
499 $this->assertEquals(0, count($eventsret['warnings']));
500
501 $sitecontext = context_system::instance();
502 $coursecontext = context_course::instance($course->id);
503
504 $this->setUser($user);
505 $prevcount = $aftercount;
506 $events = array (
507 array('name' => 'course', 'courseid' => $course->id, 'eventtype' => 'course', 'repeats' => 2),
508 array('name' => 'group', 'courseid' => $course->id, 'groupid' => $group->id, 'eventtype' => 'group'),
509 array('name' => 'user')
510 );
511 $role = $DB->get_record('role', array('shortname' => 'student'));
512 $this->getDataGenerator()->enrol_user($user->id, $course->id, $role->id);
513 groups_add_member($group, $user);
514 $this->assignUserCapability('moodle/calendar:manageentries', $coursecontext->id, $role->id);
515 $this->assignUserCapability('moodle/calendar:managegroupentries', $coursecontext->id, $role->id);
516 $eventsret = core_calendar_external::create_calendar_events($events);
517 $eventsret = external_api::clean_returnvalue(core_calendar_external::create_calendar_events_returns(), $eventsret);
518 // Check to see if things were created properly.
519 $aftercount = $DB->count_records('event');
520 $this->assertEquals($prevcount + 4, $aftercount);
521 $this->assertEquals(4, count($eventsret['events']));
522 $this->assertEquals(0, count($eventsret['warnings']));
523
524 // Check to see nothing was created without proper permission.
525 $this->setGuestUser();
526 $prevcount = $DB->count_records('event');
527 $eventsret = core_calendar_external::create_calendar_events($events);
528 $eventsret = external_api::clean_returnvalue(core_calendar_external::create_calendar_events_returns(), $eventsret);
529 $aftercount = $DB->count_records('event');
530 $this->assertEquals($prevcount, $aftercount);
531 $this->assertEquals(0, count($eventsret['events']));
532 $this->assertEquals(3, count($eventsret['warnings']));
533
534 $this->setUser($user);
535 $this->unassignUserCapability('moodle/calendar:manageentries', $coursecontext->id, $role->id);
536 $this->unassignUserCapability('moodle/calendar:managegroupentries', $coursecontext->id, $role->id);
537 $prevcount = $DB->count_records('event');
538 $eventsret = core_calendar_external::create_calendar_events($events);
539 $eventsret = external_api::clean_returnvalue(core_calendar_external::create_calendar_events_returns(), $eventsret);
540 $aftercount = $DB->count_records('event');
541 $this->assertEquals($prevcount + 1, $aftercount); // User event.
542 $this->assertEquals(1, count($eventsret['events']));
543 $this->assertEquals(2, count($eventsret['warnings']));
544 }
7c30224a 545}