Differences Between: [Versions 310 and 311] [Versions 310 and 400] [Versions 310 and 401] [Versions 310 and 402] [Versions 310 and 403]
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 event exporter tests tests. 19 * 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 23 */ 24 25 defined('MOODLE_INTERNAL') || die(); 26 27 use core_calendar\external\calendar_event_exporter; 28 use core_calendar\local\event\container; 29 use core_calendar\type_factory; 30 31 require_once (__DIR__ . '/helpers.php'); 32 33 /** 34 * Calendar event exporter testcase. 35 * 36 * @copyright 2017 Ryan Wyllie <ryan@moodle.com> 37 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 38 */ 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. 43 */ 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); 50 51 return [ 52 'before min' => [ 53 $starttime, 54 [ 55 ($starttime->getTimestamp() + 1), 56 'some error' 57 ], 58 $tomorrowmidnight 59 ], 60 'equal min' => [ 61 $starttime, 62 [ 63 $starttime->getTimestamp(), 64 'some error' 65 ], 66 $todaymidnight 67 ], 68 'after min' => [ 69 $starttime, 70 [ 71 ($starttime->getTimestamp() - 1), 72 'some error' 73 ], 74 $todaymidnight 75 ] 76 ]; 77 } 78 79 /** 80 * @dataProvider get_timestamp_min_limit_test_cases() 81 */ 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); 91 92 $result = $method->invoke($mock, $starttime, $min); 93 $this->assertEquals($expected, $result['mindaytimestamp']); 94 $this->assertEquals($min[1], $result['mindayerror']); 95 } 96 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. 100 */ 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); 107 108 return [ 109 'before max' => [ 110 $starttime, 111 [ 112 ($starttime->getTimestamp() + 1), 113 'some error' 114 ], 115 $todaymidnight 116 ], 117 'equal max' => [ 118 $starttime, 119 [ 120 $starttime->getTimestamp(), 121 'some error' 122 ], 123 $todaymidnight 124 ], 125 'after max' => [ 126 $starttime, 127 [ 128 ($starttime->getTimestamp() - 1), 129 'some error' 130 ], 131 $yesterdaymidnight 132 ] 133 ]; 134 } 135 136 /** 137 * @dataProvider get_timestamp_max_limit_test_cases() 138 */ 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); 148 149 $result = $method->invoke($mock, $starttime, $max); 150 $this->assertEquals($expected, $result['maxdaytimestamp']); 151 $this->assertEquals($max[1], $result['maxdayerror']); 152 } 153 154 /** 155 * Exporting a course event should generate the course URL. 156 */ 157 public function test_calendar_event_exporter_course_url_course_event() { 158 global $CFG, $PAGE; 159 require_once($CFG->dirroot . '/course/lib.php'); 160 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 174 ]); 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 183 ]); 184 185 $courseurl = course_get_url($course->id); 186 $expected = $courseurl->out(false); 187 $renderer = $PAGE->get_renderer('core_calendar'); 188 $exportedevent = $exporter->export($renderer); 189 190 // The exported URL should be for the course. 191 $this->assertEquals($expected, $exportedevent->url); 192 } 193 194 /** 195 * Exporting a user event should generate the site course URL. 196 */ 197 public function test_calendar_event_exporter_course_url_user_event() { 198 global $CFG, $PAGE; 199 require_once($CFG->dirroot . '/course/lib.php'); 200 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 213 ]); 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 222 ]); 223 224 $courseurl = course_get_url(SITEID); 225 $expected = $courseurl->out(false); 226 $renderer = $PAGE->get_renderer('core_calendar'); 227 $exportedevent = $exporter->export($renderer); 228 229 // The exported URL should be for the site course. 230 $this->assertEquals($expected, $exportedevent->url); 231 } 232 233 /** 234 * Popup name respects filters for course shortname. 235 */ 236 public function test_calendar_event_exporter_popupname_course_shortname_strips_links() { 237 global $CFG, $PAGE; 238 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 255 ]); 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 264 ]); 265 266 $exportedevent = $exporter->export($renderer); 267 // Links should always be stripped from the course short name. 268 $this->assertRegExp("/$nolinkshortname/", $exportedevent->popupname); 269 } 270 271 /** 272 * Exported event contains the exported course. 273 */ 274 public function test_calendar_event_exporter_exports_course() { 275 global $CFG, $PAGE; 276 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 293 ]); 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 302 ]); 303 304 $exportedevent = $exporter->export($renderer); 305 $courseexporter = new \core_course\external\course_summary_exporter($course, [ 306 'context' => $coursecontext 307 ]); 308 $exportedcourse = $courseexporter->export($renderer); 309 $this->assertEquals($exportedevent->course, $exportedcourse); 310 } 311 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body