MDL-48285 calendar: Adapting unit tests to the new behaviour
[moodle.git] / calendar / tests / externallib_test.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_externallib_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 (!isset($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         $where = $DB->sql_compare_text('name') ." = ?";
117         $count = count($DB->get_records_select("event", $where, array('test')));
118         $this->assertEquals(1, $count);
119         $aftercount = count($DB->get_records("event"));
120         $this->assertEquals($prevcount + 1, $aftercount);
122         $this->create_calendar_event('user', $USER->id, 'user', 3);
123         $where = $DB->sql_compare_text('name') ." = ?";
124         $count = count($DB->get_records_select("event", $where, array('user')));
126         $this->assertEquals(3, $count);
127         $aftercount = count($DB->get_records("event"));
128         $this->assertEquals($prevcount + 4, $aftercount);
130     }
132     /**
133      * Test delete_calendar_events
134      */
135     public function test_delete_calendar_events() {
136         global $DB, $USER;
138         $this->resetAfterTest(true);
139         $this->setAdminUser();
141         // Create a few stuff to test with.
142         $user = $this->getDataGenerator()->create_user();
143         $course = $this->getDataGenerator()->create_course();
144         $record = new stdClass();
145         $record->courseid = $course->id;
146         $group = $this->getDataGenerator()->create_group($record);
148         $notdeletedcount = $DB->count_records('event');
150         // Let's create a few events.
151         $siteevent = $this->create_calendar_event('site', $USER->id, 'site');
152         $record = new stdClass();
153         $record->courseid = $course->id;
154         $courseevent = $this->create_calendar_event('course', $USER->id, 'course', 2, time(), $record);
155         $userevent = $this->create_calendar_event('user', $USER->id);
156         $record = new stdClass();
157         $record->courseid = $course->id;
158         $record->groupid = $group->id;
159         $groupevent = $this->create_calendar_event('group', $USER->id, 'group', 0, time(), $record);
161         // Now lets try to delete stuff with proper rights.
162         $events = array(
163                 array('eventid' => $siteevent->id, 'repeat' => 0),
164                 array('eventid' => $courseevent->id, 'repeat' => 1),
165                 array('eventid' => $userevent->id, 'repeat' => 0),
166                 array('eventid' => $groupevent->id, 'repeat' => 0)
167                 );
168         core_calendar_external::delete_calendar_events($events);
170         // Check to see if things were deleted properly.
171         $deletedcount = $DB->count_records('event');
172         $this->assertEquals($notdeletedcount, $deletedcount);
174         // Let's create a few events.
175         $siteevent = $this->create_calendar_event('site', $USER->id, 'site');
176         $record = new stdClass();
177         $record->courseid = $course->id;
178         $courseevent = $this->create_calendar_event('course', $USER->id, 'course', 3, time(), $record);
179         $userevent = $this->create_calendar_event('user', $USER->id);
180         $record = new stdClass();
181         $record->courseid = $course->id;
182         $record->groupid = $group->id;
183         $groupevent = $this->create_calendar_event('group', $USER->id, 'group', 0, time(), $record);
185         $this->setuser($user);
186         $sitecontext = context_system::instance();
187         $coursecontext = context_course::instance($course->id);
188         $usercontext = context_user::instance($user->id);
189         $role = $DB->get_record('role', array('shortname' => 'student'));
190         $this->getDataGenerator()->enrol_user($user->id, $course->id, $role->id);
192         // Remove all caps.
193         $this->unassignUserCapability('moodle/calendar:manageentries', $sitecontext->id, $role->id);
194         $this->unassignUserCapability('moodle/calendar:manageentries', $coursecontext->id, $role->id);
195         $this->unassignUserCapability('moodle/calendar:managegroupentries', $coursecontext->id, $role->id);
196         $this->unassignUserCapability('moodle/calendar:manageownentries', $usercontext->id, $role->id);
198         // Assign proper caps and attempt delete.
199          $this->assignUserCapability('moodle/calendar:manageentries', $sitecontext->id, $role->id);
200          $events = array(
201                 array('eventid' => $siteevent->id, 'repeat' => 0),
202                 );
203         core_calendar_external::delete_calendar_events($events);
204         $deletedcount = $DB->count_records('event');
205         $count = $notdeletedcount+5;
206         $this->assertEquals($count, $deletedcount);
208          $this->assignUserCapability('moodle/calendar:manageentries', $sitecontext->id, $role->id);
209          $events = array(
210                 array('eventid' => $courseevent->id, 'repeat' => 0),
211                 );
212         core_calendar_external::delete_calendar_events($events);
213         $deletedcount = $DB->count_records('event');
214         $count = $notdeletedcount+4;
215         $this->assertEquals($count, $deletedcount);
217          $this->assignUserCapability('moodle/calendar:manageownentries', $usercontext->id, $role->id);
218          $events = array(
219                 array('eventid' => $userevent->id, 'repeat' => 0),
220                 );
221         core_calendar_external::delete_calendar_events($events);
222         $deletedcount = $DB->count_records('event');
223         $count = $notdeletedcount+3;
224         $this->assertEquals($count, $deletedcount);
226          $this->assignUserCapability('moodle/calendar:managegroupentries', $coursecontext->id, $role->id);
227          $events = array(
228                 array('eventid' => $groupevent->id, 'repeat' => 0),
229                 );
230         core_calendar_external::delete_calendar_events($events);
231         $deletedcount = $DB->count_records('event');
232         $count = $notdeletedcount+2;
233         $this->assertEquals($count, $deletedcount);
235         $notdeletedcount = $deletedcount;
237         // Let us try deleting without caps.
239         $siteevent = $this->create_calendar_event('site', $USER->id, 'site');
240         $record = new stdClass();
241         $record->courseid = $course->id;
242         $courseevent = $this->create_calendar_event('course', $USER->id, 'course', 3, time(), $record);
243         $userevent = $this->create_calendar_event('user', $USER->id);
244         $record = new stdClass();
245         $record->courseid = $course->id;
246         $record->groupid = $group->id;
247         $groupevent = $this->create_calendar_event('group', $USER->id, 'group', 0, time(), $record);
249         $this->setGuestUser();
250         $this->setExpectedException('moodle_exception');
251         $events = array(
252             array('eventid' => $siteevent->id, 'repeat' => 0),
253             array('eventid' => $courseevent->id, 'repeat' => 0),
254             array('eventid' => $userevent->id, 'repeat' => 0),
255             array('eventid' => $groupevent->id, 'repeat' => 0)
256         );
257         core_calendar_external::delete_calendar_events($events);
258     }
260     /**
261      * Test get_calendar_events
262      */
263     public function test_get_calendar_events() {
264         global $DB, $USER;
266         $this->resetAfterTest(true);
267         $this->setAdminUser();
269         // Create a few stuff to test with.
270         $user = $this->getDataGenerator()->create_user();
271         $course = $this->getDataGenerator()->create_course();
272         $record = new stdClass();
273         $record->courseid = $course->id;
274         $group = $this->getDataGenerator()->create_group($record);
276         $beforecount = $DB->count_records('event');
278         // Let's create a few events.
279         $siteevent = $this->create_calendar_event('site', $USER->id, 'site');
280         $record = new stdClass();
281         $record->courseid = $course->id;
282         $courseevent = $this->create_calendar_event('course', $USER->id, 'course', 2, time(), $record);
283         $userevent = $this->create_calendar_event('user', $USER->id);
284         $record = new stdClass();
285         $record->courseid = $course->id;
286         $record->groupid = $group->id;
287         $groupevent = $this->create_calendar_event('group', $USER->id, 'group', 0, time(), $record);
289         $paramevents = array ('eventids' => array($siteevent->id), 'courseids' => array($course->id), 'groupids' => array($group->id));
290         $options = array ('siteevents' => true, 'userevents' => true);
291         $events = core_calendar_external::get_calendar_events($paramevents, $options);
292         $events = external_api::clean_returnvalue(core_calendar_external::get_calendar_events_returns(), $events);
294         // Check to see if we got all events.
295         $this->assertEquals(5, count($events['events']));
296         $this->assertEquals(0, count($events['warnings']));
297         $options = array ('siteevents' => true, 'userevents' => true, 'timeend' => time() + 7*WEEKSECS);
298         $events = core_calendar_external::get_calendar_events($paramevents, $options);
299         $events = external_api::clean_returnvalue(core_calendar_external::get_calendar_events_returns(), $events);
300         $this->assertEquals(5, count($events['events']));
301         $this->assertEquals(0, count($events['warnings']));
303         // Let's play around with caps.
304         $this->setUser($user);
305         $events = core_calendar_external::get_calendar_events($paramevents, $options);
306         $events = external_api::clean_returnvalue(core_calendar_external::get_calendar_events_returns(), $events);
307         $this->assertEquals(2, count($events['events'])); // site, user.
308         $this->assertEquals(2, count($events['warnings'])); // course, group.
310         $role = $DB->get_record('role', array('shortname' => 'student'));
311         $this->getDataGenerator()->enrol_user($user->id, $course->id, $role->id);
312         $events = core_calendar_external::get_calendar_events($paramevents, $options);
313         $events = external_api::clean_returnvalue(core_calendar_external::get_calendar_events_returns(), $events);
314         $this->assertEquals(4, count($events['events'])); // site, user, both course events.
315         $this->assertEquals(1, count($events['warnings'])); // group.
317         $options = array ('siteevents' => true, 'userevents' => true, 'timeend' => time() + HOURSECS);
318         $events = core_calendar_external::get_calendar_events($paramevents, $options);
319         $events = external_api::clean_returnvalue(core_calendar_external::get_calendar_events_returns(), $events);
320         $this->assertEquals(3, count($events['events'])); // site, user, one course event.
321         $this->assertEquals(1, count($events['warnings'])); // group.
323         groups_add_member($group, $user);
324         $events = core_calendar_external::get_calendar_events($paramevents, $options);
325         $events = external_api::clean_returnvalue(core_calendar_external::get_calendar_events_returns(), $events);
326         $this->assertEquals(4, count($events['events'])); // site, user, group, one course event.
327         $this->assertEquals(0, count($events['warnings']));
329         $paramevents = array ('courseids' => array($course->id), 'groupids' => array($group->id));
330         $events = core_calendar_external::get_calendar_events($paramevents, $options);
331         $this->assertEquals(4, count($events['events'])); // site, user, group, one course event.
332         $this->assertEquals(0, count($events['warnings']));
334         $paramevents = array ('groupids' => array($group->id, 23));
335         $events = core_calendar_external::get_calendar_events($paramevents, $options);
336         $events = external_api::clean_returnvalue(core_calendar_external::get_calendar_events_returns(), $events);
337         $this->assertEquals(3, count($events['events'])); // site, user, group.
338         $this->assertEquals(1, count($events['warnings']));
340         $paramevents = array ('courseids' => array(23));
341         $events = core_calendar_external::get_calendar_events($paramevents, $options);
342         $events = external_api::clean_returnvalue(core_calendar_external::get_calendar_events_returns(), $events);
343         $this->assertEquals(2, count($events['events'])); // site, user.
344         $this->assertEquals(1, count($events['warnings']));
346         $paramevents = array ();
347         $options = array ('siteevents' => false, 'userevents' => false, 'timeend' => time() + 7*WEEKSECS);
348         $events = core_calendar_external::get_calendar_events($paramevents, $options);
349         $events = external_api::clean_returnvalue(core_calendar_external::get_calendar_events_returns(), $events);
350         $this->assertEquals(0, count($events['events'])); // nothing returned.
351         $this->assertEquals(0, count($events['warnings']));
353         $paramevents = array ('eventids' => array($siteevent->id, $groupevent->id));
354         $options = array ('siteevents' => false, 'userevents' => false, 'timeend' => time() + 7*WEEKSECS);
355         $events = core_calendar_external::get_calendar_events($paramevents, $options);
356         $events = external_api::clean_returnvalue(core_calendar_external::get_calendar_events_returns(), $events);
357         $this->assertEquals(2, count($events['events'])); // site, group.
358         $this->assertEquals(0, count($events['warnings']));
360         $paramevents = array ('eventids' => array($siteevent->id));
361         $events = core_calendar_external::get_calendar_events($paramevents, $options);
362         $events = external_api::clean_returnvalue(core_calendar_external::get_calendar_events_returns(), $events);
363         $this->assertEquals(1, count($events['events'])); // site.
364         $this->assertEquals(0, count($events['warnings']));
365     }
367     /**
368      * Test core_calendar_external::create_calendar_events
369      */
370     public function test_core_create_calendar_events() {
371         global $DB, $USER, $SITE;
373         $this->resetAfterTest(true);
374         $this->setAdminUser();
376         // Create a few stuff to test with.
377         $user = $this->getDataGenerator()->create_user();
378         $course = $this->getDataGenerator()->create_course();
379         $record = new stdClass();
380         $record->courseid = $course->id;
381         $group = $this->getDataGenerator()->create_group($record);
383         $prevcount = $DB->count_records('event');
385         // Let's create a few events.
386         $events = array (
387                 array('name' => 'site', 'courseid' => $SITE->id, 'eventtype' => 'site'),
388                 array('name' => 'course', 'courseid' => $course->id, 'eventtype' => 'course', 'repeats' => 2),
389                 array('name' => 'group', 'courseid' => $course->id, 'groupid' => $group->id, 'eventtype' => 'group'),
390                 array('name' => 'user')
391                 );
392         $eventsret = core_calendar_external::create_calendar_events($events);
394         // Check to see if things were created properly.
395         $aftercount = $DB->count_records('event');
396         $this->assertEquals($prevcount + 5, $aftercount);
397         $this->assertEquals(5, count($eventsret['events']));
398         $this->assertEquals(0, count($eventsret['warnings']));
400         $sitecontext = context_system::instance();
401         $coursecontext = context_course::instance($course->id);
403         $this->setUser($user);
404         $prevcount = $aftercount;
405         $events = array (
406                 array('name' => 'course', 'courseid' => $course->id, 'eventtype' => 'course', 'repeats' => 2),
407                 array('name' => 'group', 'courseid' => $course->id, 'groupid' => $group->id, 'eventtype' => 'group'),
408                 array('name' => 'user')
409         );
410         $role = $DB->get_record('role', array('shortname' => 'student'));
411         $this->getDataGenerator()->enrol_user($user->id, $course->id, $role->id);
412         groups_add_member($group, $user);
413         $this->assignUserCapability('moodle/calendar:manageentries', $coursecontext->id, $role->id);
414         $this->assignUserCapability('moodle/calendar:managegroupentries', $coursecontext->id, $role->id);
415         $eventsret = core_calendar_external::create_calendar_events($events);
416         $eventsret = external_api::clean_returnvalue(core_calendar_external::create_calendar_events_returns(), $eventsret);
417         // Check to see if things were created properly.
418         $aftercount = $DB->count_records('event');
419         $this->assertEquals($prevcount + 4, $aftercount);
420         $this->assertEquals(4, count($eventsret['events']));
421         $this->assertEquals(0, count($eventsret['warnings']));
423         // Check to see nothing was created without proper permission.
424         $this->setGuestUser();
425         $prevcount = $DB->count_records('event');
426         $eventsret = core_calendar_external::create_calendar_events($events);
427         $eventsret = external_api::clean_returnvalue(core_calendar_external::create_calendar_events_returns(), $eventsret);
428         $aftercount = $DB->count_records('event');
429         $this->assertEquals($prevcount, $aftercount);
430         $this->assertEquals(0, count($eventsret['events']));
431         $this->assertEquals(3, count($eventsret['warnings']));
433         $this->setUser($user);
434         $this->unassignUserCapability('moodle/calendar:manageentries', $coursecontext->id, $role->id);
435         $this->unassignUserCapability('moodle/calendar:managegroupentries', $coursecontext->id, $role->id);
436         $prevcount = $DB->count_records('event');
437         $eventsret = core_calendar_external::create_calendar_events($events);
438         $eventsret = external_api::clean_returnvalue(core_calendar_external::create_calendar_events_returns(), $eventsret);
439         $aftercount = $DB->count_records('event');
440         $this->assertEquals($prevcount + 1, $aftercount); // User event.
441         $this->assertEquals(1, count($eventsret['events']));
442         $this->assertEquals(2, count($eventsret['warnings']));
443     }