2 // This file is part of Moodle - http://moodle.org/
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.
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 namespace core_calendar
;
19 use core_calendar\external\calendar_event_exporter
;
20 use core_calendar\local\event\container
;
22 defined('MOODLE_INTERNAL') ||
die();
23 require_once(__DIR__
. '/helpers.php');
26 * Calendar event exporter testcase.
28 * @package core_calendar
29 * @copyright 2017 Ryan Wyllie <ryan@moodle.com>
30 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
32 class calendar_event_exporter_test
extends \advanced_testcase
{
34 * Data provider for the timestamp min limit test case to confirm
35 * that the minimum time limit is set correctly on the boundary cases.
37 public function get_timestamp_min_limit_test_cases() {
39 $todaymidnight = usergetmidnight($now);
40 $tomorrowmidnight = $todaymidnight + DAYSECS
;
41 $eightam = $todaymidnight +
(60 * 60 * 8);
42 $starttime = (new \
DateTime())->setTimestamp($eightam);
48 ($starttime->getTimestamp() +
1),
56 $starttime->getTimestamp(),
64 ($starttime->getTimestamp() - 1),
73 * @dataProvider get_timestamp_min_limit_test_cases()
75 public function test_get_timestamp_min_limit($starttime, $min, $expected) {
76 $class = calendar_event_exporter
::class;
77 $mock = $this->getMockBuilder($class)
78 ->disableOriginalConstructor()
81 $reflector = new \
ReflectionClass($class);
82 $method = $reflector->getMethod('get_timestamp_min_limit');
83 $method->setAccessible(true);
85 $result = $method->invoke($mock, $starttime, $min);
86 $this->assertEquals($expected, $result['mindaytimestamp']);
87 $this->assertEquals($min[1], $result['mindayerror']);
91 * Data provider for the timestamp max limit test case to confirm
92 * that the maximum time limit is set correctly on the boundary cases.
94 public function get_timestamp_max_limit_test_cases() {
96 $todaymidnight = usergetmidnight($now);
97 $yesterdaymidnight = $todaymidnight - DAYSECS
;
98 $eightam = $todaymidnight +
(60 * 60 * 8);
99 $starttime = (new \
DateTime())->setTimestamp($eightam);
105 ($starttime->getTimestamp() +
1),
113 $starttime->getTimestamp(),
121 ($starttime->getTimestamp() - 1),
130 * @dataProvider get_timestamp_max_limit_test_cases()
132 public function test_get_timestamp_max_limit($starttime, $max, $expected) {
133 $class = calendar_event_exporter
::class;
134 $mock = $this->getMockBuilder($class)
135 ->disableOriginalConstructor()
138 $reflector = new \
ReflectionClass($class);
139 $method = $reflector->getMethod('get_timestamp_max_limit');
140 $method->setAccessible(true);
142 $result = $method->invoke($mock, $starttime, $max);
143 $this->assertEquals($expected, $result['maxdaytimestamp']);
144 $this->assertEquals($max[1], $result['maxdayerror']);
148 * Exporting a course event should generate the course URL.
150 public function test_calendar_event_exporter_course_url_course_event() {
152 require_once($CFG->dirroot
. '/course/lib.php');
154 $this->resetAfterTest(true);
155 $this->setAdminUser();
156 $generator = $this->getDataGenerator();
157 $user = $generator->create_user();
158 $course = $generator->create_course();
159 $context = \context_course
::instance($course->id
);
161 $mapper = container
::get_event_mapper();
162 $legacyevent = create_event([
163 'courseid' => $course->id
,
165 'eventtype' => 'course',
168 $event = $mapper->from_legacy_event_to_event($legacyevent);
169 $exporter = new calendar_event_exporter($event, [
170 'context' => $context,
172 'moduleinstance' => null,
173 'daylink' => new \
moodle_url(''),
174 'type' => type_factory
::get_calendar_instance(),
178 $courseurl = course_get_url($course->id
);
179 $expected = $courseurl->out(false);
180 $renderer = $PAGE->get_renderer('core_calendar');
181 $exportedevent = $exporter->export($renderer);
183 // The exported URL should be for the course.
184 $this->assertEquals($expected, $exportedevent->url
);
188 * Exporting a user event should generate the site course URL.
190 public function test_calendar_event_exporter_course_url_user_event() {
192 require_once($CFG->dirroot
. '/course/lib.php');
194 $this->resetAfterTest(true);
195 $this->setAdminUser();
196 $generator = $this->getDataGenerator();
197 $user = $generator->create_user();
198 $context = \context_user
::instance($user->id
);
200 $mapper = container
::get_event_mapper();
201 $legacyevent = create_event([
203 'userid' => $user->id
,
204 'eventtype' => 'user',
207 $event = $mapper->from_legacy_event_to_event($legacyevent);
208 $exporter = new calendar_event_exporter($event, [
209 'context' => $context,
211 'moduleinstance' => null,
212 'daylink' => new \
moodle_url(''),
213 'type' => type_factory
::get_calendar_instance(),
217 $courseurl = course_get_url(SITEID
);
218 $expected = $courseurl->out(false);
219 $renderer = $PAGE->get_renderer('core_calendar');
220 $exportedevent = $exporter->export($renderer);
222 // The exported URL should be for the site course.
223 $this->assertEquals($expected, $exportedevent->url
);
227 * Popup name respects filters for course shortname.
229 public function test_calendar_event_exporter_popupname_course_shortname_strips_links() {
232 $this->resetAfterTest(true);
233 $this->setAdminUser();
234 $generator = $this->getDataGenerator();
235 $user = $generator->create_user();
236 $rawshortname = 'Shortname <a href="#">link</a>';
237 $nolinkshortname = strip_links($rawshortname);
238 $course = $generator->create_course(['shortname' => $rawshortname]);
239 $coursecontext = \context_course
::instance($course->id
);
241 $mapper = container
::get_event_mapper();
242 $renderer = $PAGE->get_renderer('core_calendar');
243 $legacyevent = create_event([
244 'courseid' => $course->id
,
246 'eventtype' => 'course',
249 $event = $mapper->from_legacy_event_to_event($legacyevent);
250 $exporter = new calendar_event_exporter($event, [
251 'context' => $coursecontext,
253 'moduleinstance' => null,
254 'daylink' => new \
moodle_url(''),
255 'type' => type_factory
::get_calendar_instance(),
259 $exportedevent = $exporter->export($renderer);
260 // Links should always be stripped from the course short name.
261 $this->assertMatchesRegularExpression("/$nolinkshortname/", $exportedevent->popupname
);
265 * Exported event contains the exported course.
267 public function test_calendar_event_exporter_exports_course() {
270 $this->resetAfterTest(true);
271 $this->setAdminUser();
272 $generator = $this->getDataGenerator();
273 $user = $generator->create_user();
274 $rawshortname = 'Shortname <a href="#">link</a>';
275 $nolinkshortname = strip_links($rawshortname);
276 $course = $generator->create_course(['shortname' => $rawshortname]);
277 $coursecontext = \context_course
::instance($course->id
);
279 $mapper = container
::get_event_mapper();
280 $renderer = $PAGE->get_renderer('core_calendar');
281 $legacyevent = create_event([
282 'courseid' => $course->id
,
284 'eventtype' => 'course',
287 $event = $mapper->from_legacy_event_to_event($legacyevent);
288 $exporter = new calendar_event_exporter($event, [
289 'context' => $coursecontext,
291 'moduleinstance' => null,
292 'daylink' => new \
moodle_url(''),
293 'type' => type_factory
::get_calendar_instance(),
297 $exportedevent = $exporter->export($renderer);
298 $courseexporter = new \core_course\external\
course_summary_exporter($course, [
299 'context' => $coursecontext
301 $exportedcourse = $courseexporter->export($renderer);
302 $this->assertEquals($exportedevent->course
, $exportedcourse);