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