MDL-37100 webservices: Define core_calendar_get_calendar_events() api
[moodle.git] / calendar / tests / externallib_tests.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 */
41class core_calendar_external_testcase extends externallib_advanced_testcase {
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)) {
69 if (is_array($prop)) {
70 $prop = (object)$prop;
71 }
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)) {
94 if (!empty($userid)) {
95 $prop->userid = $userid;
96 } else {
97 return false;
98 }
99 }
100 if (empty($prop->courseid)) {
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);
116 $count = count($DB->get_records("event", array('name' => 'test')));
117 $this->assertEquals(1, $count);
118 $aftercount = count($DB->get_records("event"));
119 $this->assertEquals($prevcount + 1, $aftercount);
120
121 $this->create_calendar_event('user', $USER->id, 'user', 3);
122 $count = count($DB->get_records("event", array('name' => 'user')));
123 $this->assertEquals(3, $count);
124 $aftercount = count($DB->get_records("event"));
125 $this->assertEquals($prevcount + 4, $aftercount);
126
127 }
128
129 /**
130 * Test delete_courses
131 */
132 public function test_delete_calendar_events() {
133 global $DB, $USER;
134
135 $this->resetAfterTest(true);
136 $this->setAdminUser();
137
138 // Create a few stuff to test with.
139 $user = $this->getDataGenerator()->create_user();
140 $course = $this->getDataGenerator()->create_course();
141 $record = new stdClass();
142 $record->courseid = $course->id;
143 $group = $this->getDataGenerator()->create_group($record);
144
145 $notdeletedcount = $DB->count_records('event');
146
147 // Let's create a few events.
148 $siteevent = $this->create_calendar_event('site', $USER->id, 'site');
149 $record = new stdClass();
150 $record->courseid = $course->id;
151 $courseevent = $this->create_calendar_event('course', $USER->id, 'course', 2, time(), $record);
152 $userevent = $this->create_calendar_event('user', $USER->id);
153 $record = new stdClass();
154 $record->courseid = $course->id;
155 $record->groupid = $group->id;
156 $groupevent = $this->create_calendar_event('group', $USER->id, 'group', 0, time(), $record);
157
158 // Now lets try to delete stuff with proper rights.
159 $events = array(
160 array('eventid' => $siteevent->id, 'repeat' => 0),
161 array('eventid' => $courseevent->id, 'repeat' => 1),
162 array('eventid' => $userevent->id, 'repeat' => 0),
163 array('eventid' => $groupevent->id, 'repeat' => 0)
164 );
165 core_calendar_external::delete_calendar_events($events);
166
167 // Check to see if things were deleted properly.
168 $deletedcount = $DB->count_records('event');
169 $this->assertEquals($notdeletedcount, $deletedcount);
170
171 // Let's create a few events.
172 $siteevent = $this->create_calendar_event('site', $USER->id, 'site');
173 $record = new stdClass();
174 $record->courseid = $course->id;
175 $courseevent = $this->create_calendar_event('course', $USER->id, 'course', 3, time(), $record);
176 $userevent = $this->create_calendar_event('user', $USER->id);
177 $record = new stdClass();
178 $record->courseid = $course->id;
179 $record->groupid = $group->id;
180 $groupevent = $this->create_calendar_event('group', $USER->id, 'group', 0, time(), $record);
181
182 $this->setuser($user);
183 $sitecontext = context_system::instance();
184 $coursecontext = context_course::instance($course->id);
185 $usercontext = context_user::instance($user->id);
186 $role = $DB->get_record('role', array('shortname' => 'student'));
187 $this->getDataGenerator()->enrol_user($user->id, $course->id, $role->id);
188
189 // Remove all caps.
190 $this->unassignUserCapability('moodle/calendar:manageentries', $sitecontext->id, $role->id);
191 $this->unassignUserCapability('moodle/calendar:manageentries', $coursecontext->id, $role->id);
192 $this->unassignUserCapability('moodle/calendar:managegroupentries', $coursecontext->id, $role->id);
193 $this->unassignUserCapability('moodle/calendar:manageownentries', $usercontext->id, $role->id);
194
195 // Assign proper caps and attempt delete.
196 $this->assignUserCapability('moodle/calendar:manageentries', $sitecontext->id, $role->id);
197 $events = array(
198 array('eventid' => $siteevent->id, 'repeat' => 0),
199 );
200 core_calendar_external::delete_calendar_events($events);
201 $deletedcount = $DB->count_records('event');
202 $count = $notdeletedcount+5;
203 $this->assertEquals($count, $deletedcount);
204
205 $this->assignUserCapability('moodle/calendar:manageentries', $sitecontext->id, $role->id);
206 $events = array(
207 array('eventid' => $courseevent->id, 'repeat' => 0),
208 );
209 core_calendar_external::delete_calendar_events($events);
210 $deletedcount = $DB->count_records('event');
211 $count = $notdeletedcount+4;
212 $this->assertEquals($count, $deletedcount);
213
214 $this->assignUserCapability('moodle/calendar:manageownentries', $usercontext->id, $role->id);
215 $events = array(
216 array('eventid' => $userevent->id, 'repeat' => 0),
217 );
218 core_calendar_external::delete_calendar_events($events);
219 $deletedcount = $DB->count_records('event');
220 $count = $notdeletedcount+3;
221 $this->assertEquals($count, $deletedcount);
222
223 $this->assignUserCapability('moodle/calendar:managegroupentries', $coursecontext->id, $role->id);
224 $events = array(
225 array('eventid' => $groupevent->id, 'repeat' => 0),
226 );
227 core_calendar_external::delete_calendar_events($events);
228 $deletedcount = $DB->count_records('event');
229 $count = $notdeletedcount+2;
230 $this->assertEquals($count, $deletedcount);
231
232 $notdeletedcount = $deletedcount;
233
234 // Let us try deleting without caps.
235
236 $siteevent = $this->create_calendar_event('site', $USER->id, 'site');
237 $record = new stdClass();
238 $record->courseid = $course->id;
239 $courseevent = $this->create_calendar_event('course', $USER->id, 'course', 3, time(), $record);
240 $userevent = $this->create_calendar_event('user', $USER->id);
241 $record = new stdClass();
242 $record->courseid = $course->id;
243 $record->groupid = $group->id;
244 $groupevent = $this->create_calendar_event('group', $USER->id, 'group', 0, time(), $record);
245
246 $this->setGuestUser();
247 $this->setExpectedException('moodle_exception');
248 $events = array(
249 array('eventid' => $siteevent->id, 'repeat' => 0),
250 array('eventid' => $courseevent->id, 'repeat' => 0),
251 array('eventid' => $userevent->id, 'repeat' => 0),
252 array('eventid' => $groupevent->id, 'repeat' => 0)
253 );
254 core_calendar_external::delete_calendar_events($events);
255 }
7c30224a 256}
644a7ef7 257
7c30224a
AA
258 /**
259 * Test delete_courses
260 */
261 public function test_get_calendar_events() {
262 global $DB, $USER;
263
264 $this->resetAfterTest(true);
265 $this->setAdminUser();
266
267 // Create a few stuff to test with.
268 $user = $this->getDataGenerator()->create_user();
269 $course = $this->getDataGenerator()->create_course();
270 $record = new stdClass();
271 $record->courseid = $course->id;
272 $group = $this->getDataGenerator()->create_group($record);
273
274 $beforecount = $DB->count_records('event');
275
276 // Let's create a few events.
277 $siteevent = $this->getDataGenerator()->create_calendar_event('site', $USER->id, 'site');
278 $record = new stdClass();
279 $record->courseid = $course->id;
280 $courseevent = $this->getDataGenerator()->create_calendar_event('course', $USER->id, 'course', 2, time(), $record);
281 $userevent = $this->getDataGenerator()->create_calendar_event('user', $USER->id);
282 $record = new stdClass();
283 $record->courseid = $course->id;
284 $record->groupid = $group->id;
285 $groupevent = $this->getDataGenerator()->create_calendar_event('group', $USER->id, 'group', 0, time(), $record);
286
287 $paramevents = array ('eventids' => array($siteevent->id), 'courseids' => array($course->id), 'groupids' => array($group->id));
288 $options = array ('siteevents' => true, 'userevents' => true);
289 $events = core_calendar_external::get_calendar_events($paramevents, $options);
290
291 // Check to see if we got all events.
292 $this->assertEquals(4, count($events));
293 $options = array ('siteevents' => true, 'userevents' => true, 'timeend' => time() + 7*WEEKSECS);
294 $events = core_calendar_external::get_calendar_events($paramevents, $options);
295 $this->assertEquals(5, count($events));
296
297 // Let's play around with caps.
298 $this->setUser($user);
299 $events = core_calendar_external::get_calendar_events($paramevents, $options);
300 $this->assertEquals(2, count($events)); // site, user.
301
302 $role = $DB->get_record('role', array('shortname' => 'student'));
303 $this->getDataGenerator()->enrol_user($user->id, $course->id, $role->id);
304 $events = core_calendar_external::get_calendar_events($paramevents, $options);
305 $this->assertEquals(4, count($events)); // site, user, both course events.
306
307 $options = array ('siteevents' => true, 'userevents' => true);
308 $events = core_calendar_external::get_calendar_events($paramevents, $options);
309 $this->assertEquals(3, count($events)); // site, user, one course event.
310
311 groups_add_member($group, $user);
312 $events = core_calendar_external::get_calendar_events($paramevents, $options);
313 $this->assertEquals(4, count($events)); // site, user, group, one course event.
314
315 $paramevents = array ('courseids' => array($course->id), 'groupids' => array($group->id));
316 $events = core_calendar_external::get_calendar_events($paramevents, $options);
317 $this->assertEquals(4, count($events)); // site, user, group, one course event.
318
319 $paramevents = array ('groupids' => array($group->id));
320 $events = core_calendar_external::get_calendar_events($paramevents, $options);
321 $this->assertEquals(3, count($events)); // site, user, group.
322
323 $paramevents = array ();
324 $events = core_calendar_external::get_calendar_events($paramevents, $options);
325 $this->assertEquals(2, count($events)); // site, user.
326
327 $paramevents = array ();
328 $options = array ('siteevents' => false, 'userevents' => false, 'timeend' => time() + 7*WEEKSECS);
329 $events = core_calendar_external::get_calendar_events($paramevents, $options);
330 $this->assertEquals(0, count($events)); // nothing returned.
331
332 $paramevents = array ('eventids' => array($siteevent->id, $groupevent->id));
333 $options = array ('siteevents' => false, 'userevents' => false, 'timeend' => time() + 7*WEEKSECS);
334 $events = core_calendar_external::get_calendar_events($paramevents, $options);
335 $this->assertEquals(2, count($events)); // site, group.
336
337 $paramevents = array ('eventids' => array($siteevent->id));
338 $events = core_calendar_external::get_calendar_events($paramevents, $options);
339 $this->assertEquals(1, count($events)); // site.
340
341 }
342}