MDL-55956 mod: Set event priority for activity overrides
[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
122 $course = $this->getDataGenerator()->create_course();
123 $events = [[
124 'name' => 'Start of assignment',
125 'description' => '',
126 'format' => 1,
127 'courseid' => $course->id,
128 'groupid' => 0,
129 'userid' => 2,
130 'modulename' => 'assign',
131 'instance' => 1,
132 'eventtype' => 'due',
133 'timestart' => time(),
134 'timeduration' => 86400,
135 'visible' => 1
136 ], [
137 'name' => 'Start of lesson',
138 'description' => '',
139 'format' => 1,
140 'courseid' => $course->id,
141 'groupid' => 0,
142 'userid' => 2,
143 'modulename' => 'lesson',
144 'instance' => 1,
145 'eventtype' => 'end',
146 'timestart' => time(),
147 'timeduration' => 86400,
148 'visible' => 1
149 ]
150 ];
151
152 foreach ($events as $event) {
153 calendar_event::create($event, false);
154 }
155
156 $timestart = time() - 60;
157 $timeend = time() + 60;
158
159 // Get all events.
160 $events = calendar_get_events($timestart, $timeend, true, 0, true);
161 $this->assertCount(2, $events);
162
163 // Disable the lesson module.
164 $modulerecord = $DB->get_record('modules', ['name' => 'lesson']);
165 $modulerecord->visible = 0;
166 $DB->update_record('modules', $modulerecord);
167
168 // Check that we only return the assign event.
169 $events = calendar_get_events($timestart, $timeend, true, 0, true);
170 $this->assertCount(1, $events);
171 $event = reset($events);
172 $this->assertEquals('assign', $event->modulename);
173 }
3607961d 174}