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