MDL-57750 calendar: add api function to get action events by timesort
[moodle.git] / calendar / tests / api_test.php
CommitLineData
3607961d
DP
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/**
12cbce0a 18 * Contains the class containing unit tests for the calendar API.
3607961d
DP
19 *
20 * @package core_calendar
12cbce0a 21 * @copyright 2017 Mark Nelson <markn@moodle.com>
3607961d
DP
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
24
25defined('MOODLE_INTERNAL') || die();
3607961d
DP
26
27/**
12cbce0a 28 * Class contaning unit tests for the calendar API.
3607961d
DP
29 *
30 * @package core_calendar
12cbce0a 31 * @copyright 2017 Mark Nelson <markn@moodle.com>
3607961d
DP
32 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
33 */
12cbce0a 34class core_calendar_api_testcase extends advanced_testcase {
3607961d 35
12cbce0a
MN
36 /**
37 * Tests set up
38 */
b8ad1728 39 protected function setUp() {
12cbce0a 40 $this->resetAfterTest();
b8ad1728 41 }
3607961d 42
12cbce0a 43 public function test_get_course_cached() {
3607961d
DP
44 // Setup some test courses.
45 $course1 = $this->getDataGenerator()->create_course();
46 $course2 = $this->getDataGenerator()->create_course();
47 $course3 = $this->getDataGenerator()->create_course();
48
49 // Load courses into cache.
50 $coursecache = null;
12cbce0a
MN
51 \core_calendar\api::get_course_cached($coursecache, $course1->id);
52 \core_calendar\api::get_course_cached($coursecache, $course2->id);
53 \core_calendar\api::get_course_cached($coursecache, $course3->id);
3607961d
DP
54
55 // Verify the cache.
56 $this->assertArrayHasKey($course1->id, $coursecache);
57 $cachedcourse1 = $coursecache[$course1->id];
58 $this->assertEquals($course1->id, $cachedcourse1->id);
59 $this->assertEquals($course1->shortname, $cachedcourse1->shortname);
60 $this->assertEquals($course1->fullname, $cachedcourse1->fullname);
61
62 $this->assertArrayHasKey($course2->id, $coursecache);
63 $cachedcourse2 = $coursecache[$course2->id];
64 $this->assertEquals($course2->id, $cachedcourse2->id);
65 $this->assertEquals($course2->shortname, $cachedcourse2->shortname);
66 $this->assertEquals($course2->fullname, $cachedcourse2->fullname);
67
68 $this->assertArrayHasKey($course3->id, $coursecache);
69 $cachedcourse3 = $coursecache[$course3->id];
70 $this->assertEquals($course3->id, $cachedcourse3->id);
71 $this->assertEquals($course3->shortname, $cachedcourse3->shortname);
72 $this->assertEquals($course3->fullname, $cachedcourse3->fullname);
73 }
b8ad1728
JPG
74
75 /**
12cbce0a 76 * Test that the get_events() function only returns activity events that are enabled.
b8ad1728 77 */
12cbce0a 78 public function test_get_events_with_disabled_module() {
cbbca0d0
AG
79 global $DB;
80
f52459bb
JP
81 $generator = $this->getDataGenerator();
82 $course = $generator->create_course();
83 $student = $generator->create_user();
84 $generator->enrol_user($student->id, $course->id, 'student');
85 $this->setUser($student);
f52459bb
JP
86 $events = [
87 [
88 'name' => 'Start of assignment',
89 'description' => '',
90 'format' => 1,
91 'courseid' => $course->id,
92 'groupid' => 0,
93 'userid' => 2,
94 'modulename' => 'assign',
95 'instance' => 1,
96 'eventtype' => 'due',
97 'timestart' => time(),
98 'timeduration' => 86400,
99 'visible' => 1
100 ], [
f52459bb
JP
101 'name' => 'Start of lesson',
102 'description' => '',
103 'format' => 1,
104 'courseid' => $course->id,
105 'groupid' => 0,
106 'userid' => 2,
107 'modulename' => 'lesson',
108 'instance' => 1,
109 'eventtype' => 'end',
110 'timestart' => time(),
111 'timeduration' => 86400,
112 'visible' => 1
113 ]
114 ];
cbbca0d0 115 foreach ($events as $event) {
e057f279 116 \core_calendar\event::create($event, false);
cbbca0d0 117 }
cbbca0d0
AG
118 $timestart = time() - 60;
119 $timeend = time() + 60;
cbbca0d0 120 // Get all events.
12cbce0a 121 $events = \core_calendar\api::get_events($timestart, $timeend, true, 0, true);
cbbca0d0 122 $this->assertCount(2, $events);
cbbca0d0
AG
123 // Disable the lesson module.
124 $modulerecord = $DB->get_record('modules', ['name' => 'lesson']);
125 $modulerecord->visible = 0;
126 $DB->update_record('modules', $modulerecord);
cbbca0d0 127 // Check that we only return the assign event.
12cbce0a 128 $events = \core_calendar\api::get_events($timestart, $timeend, true, 0, true);
cbbca0d0
AG
129 $this->assertCount(1, $events);
130 $event = reset($events);
131 $this->assertEquals('assign', $event->modulename);
132 }
f52459bb
JP
133
134 /**
135 * Test for calendar_get_events() when there are user and group overrides.
136 */
137 public function test_calendar_get_events_with_overrides() {
138 global $DB;
f52459bb
JP
139 $generator = $this->getDataGenerator();
140 $course = $generator->create_course();
141 $plugingenerator = $this->getDataGenerator()->get_plugin_generator('mod_assign');
142 if (!isset($params['course'])) {
143 $params['course'] = $course->id;
144 }
145 $instance = $plugingenerator->create_instance($params);
f52459bb
JP
146 // Create users.
147 $useroverridestudent = $generator->create_user();
148 $group1student = $generator->create_user();
149 $group2student = $generator->create_user();
150 $group12student = $generator->create_user();
151 $nogroupstudent = $generator->create_user();
f52459bb
JP
152 // Enrol users.
153 $generator->enrol_user($useroverridestudent->id, $course->id, 'student');
154 $generator->enrol_user($group1student->id, $course->id, 'student');
155 $generator->enrol_user($group2student->id, $course->id, 'student');
156 $generator->enrol_user($group12student->id, $course->id, 'student');
157 $generator->enrol_user($nogroupstudent->id, $course->id, 'student');
f52459bb
JP
158 // Create groups.
159 $group1 = $generator->create_group(['courseid' => $course->id]);
160 $group2 = $generator->create_group(['courseid' => $course->id]);
f52459bb
JP
161 // Add members to groups.
162 $generator->create_group_member(['groupid' => $group1->id, 'userid' => $group1student->id]);
163 $generator->create_group_member(['groupid' => $group2->id, 'userid' => $group2student->id]);
164 $generator->create_group_member(['groupid' => $group1->id, 'userid' => $group12student->id]);
165 $generator->create_group_member(['groupid' => $group2->id, 'userid' => $group12student->id]);
f52459bb
JP
166 $now = time();
167 // Events with the same module name, instance and event type.
168 $events = [
169 [
170 'name' => 'Assignment 1 due date',
171 'description' => '',
172 'format' => 0,
173 'courseid' => $course->id,
174 'groupid' => 0,
175 'userid' => 2,
176 'modulename' => 'assign',
177 'instance' => $instance->id,
178 'eventtype' => 'due',
179 'timestart' => $now,
180 'timeduration' => 0,
181 'visible' => 1
182 ], [
183 'name' => 'Assignment 1 due date - User override',
184 'description' => '',
185 'format' => 1,
186 'courseid' => 0,
187 'groupid' => 0,
188 'userid' => $useroverridestudent->id,
189 'modulename' => 'assign',
190 'instance' => $instance->id,
191 'eventtype' => 'due',
192 'timestart' => $now + 86400,
193 'timeduration' => 0,
194 'visible' => 1,
195 'priority' => CALENDAR_EVENT_USER_OVERRIDE_PRIORITY
196 ], [
197 'name' => 'Assignment 1 due date - Group A override',
198 'description' => '',
199 'format' => 1,
200 'courseid' => $course->id,
201 'groupid' => $group1->id,
202 'userid' => 2,
203 'modulename' => 'assign',
204 'instance' => $instance->id,
205 'eventtype' => 'due',
206 'timestart' => $now + (2 * 86400),
207 'timeduration' => 0,
208 'visible' => 1,
209 'priority' => 1,
210 ], [
211 'name' => 'Assignment 1 due date - Group B override',
212 'description' => '',
213 'format' => 1,
214 'courseid' => $course->id,
215 'groupid' => $group2->id,
216 'userid' => 2,
217 'modulename' => 'assign',
218 'instance' => $instance->id,
219 'eventtype' => 'due',
220 'timestart' => $now + (3 * 86400),
221 'timeduration' => 0,
222 'visible' => 1,
223 'priority' => 2,
224 ],
225 ];
f52459bb 226 foreach ($events as $event) {
12cbce0a 227 \core_calendar\event::create($event, false);
f52459bb 228 }
f52459bb
JP
229 $timestart = $now - 100;
230 $timeend = $now + (3 * 86400);
f52459bb 231 $groups = [$group1->id, $group2->id];
f52459bb
JP
232 // Get user override events.
233 $this->setUser($useroverridestudent);
12cbce0a 234 $events = \core_calendar\api::get_events($timestart, $timeend, $useroverridestudent->id, $groups, $course->id);
f52459bb
JP
235 $this->assertCount(1, $events);
236 $event = reset($events);
237 $this->assertEquals('Assignment 1 due date - User override', $event->name);
f52459bb 238 // Get event for user with override but with the timestart and timeend parameters only covering the original event.
12cbce0a 239 $events = \core_calendar\api::get_events($timestart, $now, $useroverridestudent->id, $groups, $course->id);
f52459bb 240 $this->assertCount(0, $events);
f52459bb
JP
241 // Get events for user that does not belong to any group and has no user override events.
242 $this->setUser($nogroupstudent);
12cbce0a 243 $events = \core_calendar\api::get_events($timestart, $timeend, $nogroupstudent->id, $groups, $course->id);
f52459bb
JP
244 $this->assertCount(1, $events);
245 $event = reset($events);
246 $this->assertEquals('Assignment 1 due date', $event->name);
f52459bb
JP
247 // Get events for user that belongs to groups A and B and has no user override events.
248 $this->setUser($group12student);
12cbce0a 249 $events = \core_calendar\api::get_events($timestart, $timeend, $group12student->id, $groups, $course->id);
f52459bb
JP
250 $this->assertCount(1, $events);
251 $event = reset($events);
252 $this->assertEquals('Assignment 1 due date - Group B override', $event->name);
f52459bb
JP
253 // Get events for user that belongs to group A and has no user override events.
254 $this->setUser($group1student);
12cbce0a 255 $events = \core_calendar\api::get_events($timestart, $timeend, $group1student->id, $groups, $course->id);
f52459bb
JP
256 $this->assertCount(1, $events);
257 $event = reset($events);
258 $this->assertEquals('Assignment 1 due date - Group A override', $event->name);
f52459bb
JP
259 // Add repeating events.
260 $repeatingevents = [
261 [
262 'name' => 'Repeating site event',
263 'description' => '',
264 'format' => 1,
265 'courseid' => SITEID,
266 'groupid' => 0,
267 'userid' => 2,
268 'repeatid' => 1,
269 'modulename' => '0',
270 'instance' => 0,
271 'eventtype' => 'site',
272 'timestart' => $now + 86400,
273 'timeduration' => 0,
274 'visible' => 1,
275 ],
276 [
277 'name' => 'Repeating site event',
278 'description' => '',
279 'format' => 1,
280 'courseid' => SITEID,
281 'groupid' => 0,
282 'userid' => 2,
283 'repeatid' => 1,
284 'modulename' => '0',
285 'instance' => 0,
286 'eventtype' => 'site',
287 'timestart' => $now + (2 * 86400),
288 'timeduration' => 0,
289 'visible' => 1,
290 ],
291 ];
292 foreach ($repeatingevents as $event) {
12cbce0a 293 \core_calendar\event::create($event, false);
f52459bb 294 }
f52459bb 295 // Make sure repeating events are not filtered out.
12cbce0a 296 $events = \core_calendar\api::get_events($timestart, $timeend, true, true, true);
f52459bb
JP
297 $this->assertCount(3, $events);
298 }
12cbce0a
MN
299
300 /**
301 * Test the update_subscription() function.
302 */
303 public function test_update_subscription() {
304 $this->resetAfterTest(true);
305
306 $subscription = new stdClass();
307 $subscription->eventtype = 'site';
308 $subscription->name = 'test';
309 $id = \core_calendar\api::add_subscription($subscription);
310
311 $subscription = \core_calendar\api::get_subscription($id);
312 $subscription->name = 'awesome';
313 \core_calendar\api::update_subscription($subscription);
314 $sub = \core_calendar\api::get_subscription($id);
315 $this->assertEquals($subscription->name, $sub->name);
316
317 $subscription = \core_calendar\api::get_subscription($id);
318 $subscription->name = 'awesome2';
319 $subscription->pollinterval = 604800;
320 \core_calendar\api::update_subscription($subscription);
321 $sub = \core_calendar\api::get_subscription($id);
322 $this->assertEquals($subscription->name, $sub->name);
323 $this->assertEquals($subscription->pollinterval, $sub->pollinterval);
324
325 $subscription = new stdClass();
326 $subscription->name = 'awesome4';
327 $this->expectException('coding_exception');
328 \core_calendar\api::update_subscription($subscription);
329 }
330
331 public function test_add_subscription() {
332 global $DB, $CFG;
333
334 require_once($CFG->dirroot . '/lib/bennu/bennu.inc.php');
335
336 $this->resetAfterTest(true);
337
338 // Test for Microsoft Outlook 2010.
339 $subscription = new stdClass();
340 $subscription->name = 'Microsoft Outlook 2010';
341 $subscription->importfrom = CALENDAR_IMPORT_FROM_FILE;
342 $subscription->eventtype = 'site';
343 $id = \core_calendar\api::add_subscription($subscription);
344
345 $calendar = file_get_contents($CFG->dirroot . '/lib/tests/fixtures/ms_outlook_2010.ics');
346 $ical = new iCalendar();
347 $ical->unserialize($calendar);
348 $this->assertEquals($ical->parser_errors, array());
349
350 $sub = \core_calendar\api::get_subscription($id);
351 \core_calendar\api::import_icalendar_events($ical, $sub->courseid, $sub->id);
352 $count = $DB->count_records('event', array('subscriptionid' => $sub->id));
353 $this->assertEquals($count, 1);
354
355 // Test for OSX Yosemite.
356 $subscription = new stdClass();
357 $subscription->name = 'OSX Yosemite';
358 $subscription->importfrom = CALENDAR_IMPORT_FROM_FILE;
359 $subscription->eventtype = 'site';
360 $id = \core_calendar\api::add_subscription($subscription);
361
362 $calendar = file_get_contents($CFG->dirroot . '/lib/tests/fixtures/osx_yosemite.ics');
363 $ical = new iCalendar();
364 $ical->unserialize($calendar);
365 $this->assertEquals($ical->parser_errors, array());
366
367 $sub = \core_calendar\api::get_subscription($id);
368 \core_calendar\api::import_icalendar_events($ical, $sub->courseid, $sub->id);
369 $count = $DB->count_records('event', array('subscriptionid' => $sub->id));
370 $this->assertEquals($count, 1);
371
372 // Test for Google Gmail.
373 $subscription = new stdClass();
374 $subscription->name = 'Google Gmail';
375 $subscription->importfrom = CALENDAR_IMPORT_FROM_FILE;
376 $subscription->eventtype = 'site';
377 $id = \core_calendar\api::add_subscription($subscription);
378
379 $calendar = file_get_contents($CFG->dirroot . '/lib/tests/fixtures/google_gmail.ics');
380 $ical = new iCalendar();
381 $ical->unserialize($calendar);
382 $this->assertEquals($ical->parser_errors, array());
383
384 $sub = \core_calendar\api::get_subscription($id);
385 \core_calendar\api::import_icalendar_events($ical, $sub->courseid, $sub->id);
386 $count = $DB->count_records('event', array('subscriptionid' => $sub->id));
387 $this->assertEquals($count, 1);
388 }
3607961d 389}