Merge branch 'MDL-63768-master' of https://github.com/lucaboesch/moodle
[moodle.git] / calendar / tests / calendar_event_exporter_test.php
blob86c815d7f38f63032698d26c9ca62a6b518cf62a
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.
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 * Calendar event exporter tests tests.
20 * @package core_calendar
21 * @copyright 2017 Ryan Wyllie <ryan@moodle.com>
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 defined('MOODLE_INTERNAL') || die();
27 use core_calendar\external\calendar_event_exporter;
28 use core_calendar\local\event\container;
29 use core_calendar\type_factory;
31 require_once(__DIR__ . '/helpers.php');
33 /**
34 * Calendar event exporter testcase.
36 * @copyright 2017 Ryan Wyllie <ryan@moodle.com>
37 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
39 class core_calendar_event_exporter_testcase extends advanced_testcase {
40 /**
41 * Data provider for the timestamp min limit test case to confirm
42 * that the minimum time limit is set correctly on the boundary cases.
44 public function get_timestamp_min_limit_test_cases() {
45 $now = time();
46 $todaymidnight = usergetmidnight($now);
47 $tomorrowmidnight = $todaymidnight + DAYSECS;
48 $eightam = $todaymidnight + (60 * 60 * 8);
49 $starttime = (new DateTime())->setTimestamp($eightam);
51 return [
52 'before min' => [
53 $starttime,
55 ($starttime->getTimestamp() + 1),
56 'some error'
58 $tomorrowmidnight
60 'equal min' => [
61 $starttime,
63 $starttime->getTimestamp(),
64 'some error'
66 $todaymidnight
68 'after min' => [
69 $starttime,
71 ($starttime->getTimestamp() - 1),
72 'some error'
74 $todaymidnight
79 /**
80 * @dataProvider get_timestamp_min_limit_test_cases()
82 public function test_get_timestamp_min_limit($starttime, $min, $expected) {
83 $class = \core_calendar\external\calendar_event_exporter::class;
84 $mock = $this->getMockBuilder($class)
85 ->disableOriginalConstructor()
86 ->setMethods(null)
87 ->getMock();
88 $reflector = new ReflectionClass($class);
89 $method = $reflector->getMethod('get_timestamp_min_limit');
90 $method->setAccessible(true);
92 $result = $method->invoke($mock, $starttime, $min);
93 $this->assertEquals($expected, $result['mindaytimestamp']);
94 $this->assertEquals($min[1], $result['mindayerror']);
97 /**
98 * Data provider for the timestamp max limit test case to confirm
99 * that the maximum time limit is set correctly on the boundary cases.
101 public function get_timestamp_max_limit_test_cases() {
102 $now = time();
103 $todaymidnight = usergetmidnight($now);
104 $yesterdaymidnight = $todaymidnight - DAYSECS;
105 $eightam = $todaymidnight + (60 * 60 * 8);
106 $starttime = (new DateTime())->setTimestamp($eightam);
108 return [
109 'before max' => [
110 $starttime,
112 ($starttime->getTimestamp() + 1),
113 'some error'
115 $todaymidnight
117 'equal max' => [
118 $starttime,
120 $starttime->getTimestamp(),
121 'some error'
123 $todaymidnight
125 'after max' => [
126 $starttime,
128 ($starttime->getTimestamp() - 1),
129 'some error'
131 $yesterdaymidnight
137 * @dataProvider get_timestamp_max_limit_test_cases()
139 public function test_get_timestamp_max_limit($starttime, $max, $expected) {
140 $class = \core_calendar\external\calendar_event_exporter::class;
141 $mock = $this->getMockBuilder($class)
142 ->disableOriginalConstructor()
143 ->setMethods(null)
144 ->getMock();
145 $reflector = new ReflectionClass($class);
146 $method = $reflector->getMethod('get_timestamp_max_limit');
147 $method->setAccessible(true);
149 $result = $method->invoke($mock, $starttime, $max);
150 $this->assertEquals($expected, $result['maxdaytimestamp']);
151 $this->assertEquals($max[1], $result['maxdayerror']);
155 * Exporting a course event should generate the course URL.
157 public function test_calendar_event_exporter_course_url_course_event() {
158 global $CFG, $PAGE;
159 require_once($CFG->dirroot . '/course/lib.php');
161 $this->resetAfterTest(true);
162 $this->setAdminUser();
163 $generator = $this->getDataGenerator();
164 $user = $generator->create_user();
165 $course = $generator->create_course();
166 $context = context_course::instance($course->id);
167 $now = time();
168 $mapper = container::get_event_mapper();
169 $legacyevent = create_event([
170 'courseid' => $course->id,
171 'userid' => 1,
172 'eventtype' => 'course',
173 'timestart' => $now
175 $event = $mapper->from_legacy_event_to_event($legacyevent);
176 $exporter = new calendar_event_exporter($event, [
177 'context' => $context,
178 'course' => $course,
179 'moduleinstance' => null,
180 'daylink' => new moodle_url(''),
181 'type' => type_factory::get_calendar_instance(),
182 'today' => $now
185 $courseurl = course_get_url($course->id);
186 $expected = $courseurl->out(false);
187 $renderer = $PAGE->get_renderer('core_calendar');
188 $exportedevent = $exporter->export($renderer);
190 // The exported URL should be for the course.
191 $this->assertEquals($expected, $exportedevent->url);
195 * Exporting a user event should generate the site course URL.
197 public function test_calendar_event_exporter_course_url_user_event() {
198 global $CFG, $PAGE;
199 require_once($CFG->dirroot . '/course/lib.php');
201 $this->resetAfterTest(true);
202 $this->setAdminUser();
203 $generator = $this->getDataGenerator();
204 $user = $generator->create_user();
205 $context = context_user::instance($user->id);
206 $now = time();
207 $mapper = container::get_event_mapper();
208 $legacyevent = create_event([
209 'courseid' => 0,
210 'userid' => $user->id,
211 'eventtype' => 'user',
212 'timestart' => $now
214 $event = $mapper->from_legacy_event_to_event($legacyevent);
215 $exporter = new calendar_event_exporter($event, [
216 'context' => $context,
217 'course' => null,
218 'moduleinstance' => null,
219 'daylink' => new moodle_url(''),
220 'type' => type_factory::get_calendar_instance(),
221 'today' => $now
224 $courseurl = course_get_url(SITEID);
225 $expected = $courseurl->out(false);
226 $renderer = $PAGE->get_renderer('core_calendar');
227 $exportedevent = $exporter->export($renderer);
229 // The exported URL should be for the site course.
230 $this->assertEquals($expected, $exportedevent->url);
234 * Popup name respects filters for course shortname.
236 public function test_calendar_event_exporter_popupname_course_shortname_strips_links() {
237 global $CFG, $PAGE;
239 $this->resetAfterTest(true);
240 $this->setAdminUser();
241 $generator = $this->getDataGenerator();
242 $user = $generator->create_user();
243 $rawshortname = 'Shortname <a href="#">link</a>';
244 $nolinkshortname = strip_links($rawshortname);
245 $course = $generator->create_course(['shortname' => $rawshortname]);
246 $coursecontext = context_course::instance($course->id);
247 $now = time();
248 $mapper = container::get_event_mapper();
249 $renderer = $PAGE->get_renderer('core_calendar');
250 $legacyevent = create_event([
251 'courseid' => $course->id,
252 'userid' => 1,
253 'eventtype' => 'course',
254 'timestart' => $now
256 $event = $mapper->from_legacy_event_to_event($legacyevent);
257 $exporter = new calendar_event_exporter($event, [
258 'context' => $coursecontext,
259 'course' => $course,
260 'moduleinstance' => null,
261 'daylink' => new moodle_url(''),
262 'type' => type_factory::get_calendar_instance(),
263 'today' => $now
266 $exportedevent = $exporter->export($renderer);
267 // Links should always be stripped from the course short name.
268 $this->assertRegExp("/$nolinkshortname/", $exportedevent->popupname);
272 * Exported event contains the exported course.
274 public function test_calendar_event_exporter_exports_course() {
275 global $CFG, $PAGE;
277 $this->resetAfterTest(true);
278 $this->setAdminUser();
279 $generator = $this->getDataGenerator();
280 $user = $generator->create_user();
281 $rawshortname = 'Shortname <a href="#">link</a>';
282 $nolinkshortname = strip_links($rawshortname);
283 $course = $generator->create_course(['shortname' => $rawshortname]);
284 $coursecontext = context_course::instance($course->id);
285 $now = time();
286 $mapper = container::get_event_mapper();
287 $renderer = $PAGE->get_renderer('core_calendar');
288 $legacyevent = create_event([
289 'courseid' => $course->id,
290 'userid' => 1,
291 'eventtype' => 'course',
292 'timestart' => $now
294 $event = $mapper->from_legacy_event_to_event($legacyevent);
295 $exporter = new calendar_event_exporter($event, [
296 'context' => $coursecontext,
297 'course' => $course,
298 'moduleinstance' => null,
299 'daylink' => new moodle_url(''),
300 'type' => type_factory::get_calendar_instance(),
301 'today' => $now
304 $exportedevent = $exporter->export($renderer);
305 $courseexporter = new \core_course\external\course_summary_exporter($course, [
306 'context' => $coursecontext
308 $exportedcourse = $courseexporter->export($renderer);
309 $this->assertEquals($exportedevent->course, $exportedcourse);