MDL-37100 webservices: Adding warnings and cleaning up get_calendar_events() api
[moodle.git] / calendar / tests / externallib_tests.php
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/>.
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  */
26 defined('MOODLE_INTERNAL') || die();
28 global $CFG;
30 require_once($CFG->dirroot . '/webservice/tests/helpers.php');
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  */
41 class core_calendar_external_testcase extends externallib_advanced_testcase {
43     /**
44      * Tests set up
45      */
46     protected function setUp() {
47         global $CFG;
48         require_once($CFG->dirroot . '/calendar/externallib.php');
49     }
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      */
64     public static function create_calendar_event($name, $userid = 0, $type = 'user', $repeats = 0, $timestart  = null, $prop = null) {
65         global $CFG, $DB, $USER, $SITE;
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     }
107     public function test_create_calendar_events () {
108         global $DB, $USER;
110         $this->setAdminUser();
111         $this->resetAfterTest();
112         $prevcount = count($DB->get_records("event"));
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);
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);
127     }
129     /**
130      * Test delete_calendar_events
131      */
132     public function test_delete_calendar_events() {
133         global $DB, $USER;
135         $this->resetAfterTest(true);
136         $this->setAdminUser();
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);
145         $notdeletedcount = $DB->count_records('event');
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);
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);
167         // Check to see if things were deleted properly.
168         $deletedcount = $DB->count_records('event');
169         $this->assertEquals($notdeletedcount, $deletedcount);
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);
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);
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);
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);
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);
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);
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);
232         $notdeletedcount = $deletedcount;
234         // Let us try deleting without caps.
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);
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     }
257     /**
258      * Test get_calendar_events
259      */
260     public function test_get_calendar_events() {
261         global $DB, $USER;
263         $this->resetAfterTest(true);
264         $this->setAdminUser();
266         // Create a few stuff to test with.
267         $user = $this->getDataGenerator()->create_user();
268         $course = $this->getDataGenerator()->create_course();
269         $record = new stdClass();
270         $record->courseid = $course->id;
271         $group = $this->getDataGenerator()->create_group($record);
273         $beforecount = $DB->count_records('event');
275         // Let's create a few events.
276         $siteevent = $this->create_calendar_event('site', $USER->id, 'site');
277         $record = new stdClass();
278         $record->courseid = $course->id;
279         $courseevent = $this->create_calendar_event('course', $USER->id, 'course', 2, time(), $record);
280         $userevent = $this->create_calendar_event('user', $USER->id);
281         $record = new stdClass();
282         $record->courseid = $course->id;
283         $record->groupid = $group->id;
284         $groupevent = $this->create_calendar_event('group', $USER->id, 'group', 0, time(), $record);
286         $paramevents = array ('eventids' => array($siteevent->id), 'courseids' => array($course->id), 'groupids' => array($group->id));
287         $options = array ('siteevents' => true, 'userevents' => true);
288         $events = core_calendar_external::get_calendar_events($paramevents, $options);
289         $events = external_api::clean_returnvalue(core_calendar_external::get_calendar_events_returns(), $events);
291         // Check to see if we got all events.
292         $this->assertEquals(4, count($events['events']));
293         $this->assertEquals(0, count($events['warnings']));
294         $options = array ('siteevents' => true, 'userevents' => true, 'timeend' => time() + 7*WEEKSECS);
295         $events = core_calendar_external::get_calendar_events($paramevents, $options);
296         $events = external_api::clean_returnvalue(core_calendar_external::get_calendar_events_returns(), $events);
297         $this->assertEquals(5, count($events['events']));
298         $this->assertEquals(0, count($events['warnings']));
300         // Let's play around with caps.
301         $this->setUser($user);
302         $events = core_calendar_external::get_calendar_events($paramevents, $options);
303         $events = external_api::clean_returnvalue(core_calendar_external::get_calendar_events_returns(), $events);
304         $this->assertEquals(2, count($events['events'])); // site, user.
305         $this->assertEquals(2, count($events['warnings'])); // course, group.
307         $role = $DB->get_record('role', array('shortname' => 'student'));
308         $this->getDataGenerator()->enrol_user($user->id, $course->id, $role->id);
309         $events = core_calendar_external::get_calendar_events($paramevents, $options);
310         $events = external_api::clean_returnvalue(core_calendar_external::get_calendar_events_returns(), $events);
311         $this->assertEquals(4, count($events['events'])); // site, user, both course events.
312         $this->assertEquals(1, count($events['warnings'])); // group.
314         $options = array ('siteevents' => true, 'userevents' => true);
315         $events = core_calendar_external::get_calendar_events($paramevents, $options);
316         $events = external_api::clean_returnvalue(core_calendar_external::get_calendar_events_returns(), $events);
317         $this->assertEquals(3, count($events['events'])); // site, user, one course event.
318         $this->assertEquals(1, count($events['warnings'])); // group.
320         groups_add_member($group, $user);
321         $events = core_calendar_external::get_calendar_events($paramevents, $options);
322         $events = external_api::clean_returnvalue(core_calendar_external::get_calendar_events_returns(), $events);
323         $this->assertEquals(4, count($events['events'])); // site, user, group, one course event.
324         $this->assertEquals(0, count($events['warnings']));
326         $paramevents = array ('courseids' => array($course->id), 'groupids' => array($group->id));
327         $events = core_calendar_external::get_calendar_events($paramevents, $options);
328         $this->assertEquals(4, count($events['events'])); // site, user, group, one course event.
329         $this->assertEquals(0, count($events['warnings']));
331         $paramevents = array ('groupids' => array($group->id, 23));
332         $events = core_calendar_external::get_calendar_events($paramevents, $options);
333         $events = external_api::clean_returnvalue(core_calendar_external::get_calendar_events_returns(), $events);
334         $this->assertEquals(3, count($events['events'])); // site, user, group.
335         $this->assertEquals(1, count($events['warnings']));
337         $paramevents = array ('courseids' => array(23));
338         $events = core_calendar_external::get_calendar_events($paramevents, $options);
339         $events = external_api::clean_returnvalue(core_calendar_external::get_calendar_events_returns(), $events);
340         $this->assertEquals(2, count($events['events'])); // site, user.
341         $this->assertEquals(1, count($events['warnings']));
343         $paramevents = array ();
344         $options = array ('siteevents' => false, 'userevents' => false, 'timeend' => time() + 7*WEEKSECS);
345         $events = core_calendar_external::get_calendar_events($paramevents, $options);
346         $events = external_api::clean_returnvalue(core_calendar_external::get_calendar_events_returns(), $events);
347         $this->assertEquals(0, count($events['events'])); // nothing returned.
348         $this->assertEquals(0, count($events['warnings']));
350         $paramevents = array ('eventids' => array($siteevent->id, $groupevent->id));
351         $options = array ('siteevents' => false, 'userevents' => false, 'timeend' => time() + 7*WEEKSECS);
352         $events = core_calendar_external::get_calendar_events($paramevents, $options);
353         $events = external_api::clean_returnvalue(core_calendar_external::get_calendar_events_returns(), $events);
354         $this->assertEquals(2, count($events['events'])); // site, group.
355         $this->assertEquals(0, count($events['warnings']));
357         $paramevents = array ('eventids' => array($siteevent->id));
358         $events = core_calendar_external::get_calendar_events($paramevents, $options);
359         $events = external_api::clean_returnvalue(core_calendar_external::get_calendar_events_returns(), $events);
360         $this->assertEquals(1, count($events['events'])); // site.
361         $this->assertEquals(0, count($events['warnings']));
362     }