Differences Between: [Versions 310 and 402] [Versions 311 and 402] [Versions 39 and 402] [Versions 400 and 402] [Versions 402 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 namespace mod_label; 18 19 /** 20 * Unit tests for the activity label's lib. 21 * 22 * @package mod_label 23 * @category test 24 * @copyright 2017 Mark Nelson <markn@moodle.com> 25 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 26 */ 27 class lib_test extends \advanced_testcase { 28 29 /** 30 * Set up. 31 */ 32 public function setUp(): void { 33 $this->resetAfterTest(); 34 $this->setAdminUser(); 35 } 36 37 public function test_label_core_calendar_provide_event_action() { 38 // Create the activity. 39 $course = $this->getDataGenerator()->create_course(); 40 $label = $this->getDataGenerator()->create_module('label', array('course' => $course->id)); 41 42 // Create a calendar event. 43 $event = $this->create_action_event($course->id, $label->id, 44 \core_completion\api::COMPLETION_EVENT_TYPE_DATE_COMPLETION_EXPECTED); 45 46 // Create an action factory. 47 $factory = new \core_calendar\action_factory(); 48 49 // Decorate action event. 50 $actionevent = mod_label_core_calendar_provide_event_action($event, $factory); 51 52 // Confirm the event was decorated. 53 $this->assertInstanceOf('\core_calendar\local\event\value_objects\action', $actionevent); 54 $this->assertEquals(get_string('view'), $actionevent->get_name()); 55 $this->assertInstanceOf('moodle_url', $actionevent->get_url()); 56 $this->assertEquals(1, $actionevent->get_item_count()); 57 $this->assertTrue($actionevent->is_actionable()); 58 } 59 60 public function test_label_core_calendar_provide_event_action_as_non_user() { 61 global $CFG; 62 63 // Create the activity. 64 $course = $this->getDataGenerator()->create_course(); 65 $label = $this->getDataGenerator()->create_module('label', array('course' => $course->id)); 66 67 // Create a calendar event. 68 $event = $this->create_action_event($course->id, $label->id, 69 \core_completion\api::COMPLETION_EVENT_TYPE_DATE_COMPLETION_EXPECTED); 70 71 // Now log out. 72 $CFG->forcelogin = true; // We don't want to be logged in as guest, as guest users might still have some capabilities. 73 $this->setUser(); 74 75 // Create an action factory. 76 $factory = new \core_calendar\action_factory(); 77 78 // Decorate action event. 79 $actionevent = mod_label_core_calendar_provide_event_action($event, $factory); 80 81 // Confirm the event is not shown at all. 82 $this->assertNull($actionevent); 83 } 84 85 public function test_label_core_calendar_provide_event_action_in_hidden_section() { 86 // Create the activity. 87 $course = $this->getDataGenerator()->create_course(); 88 $label = $this->getDataGenerator()->create_module('label', array('course' => $course->id)); 89 90 // Create a student. 91 $student = $this->getDataGenerator()->create_and_enrol($course, 'student'); 92 93 // Create a calendar event. 94 $event = $this->create_action_event($course->id, $label->id, 95 \core_completion\api::COMPLETION_EVENT_TYPE_DATE_COMPLETION_EXPECTED); 96 97 // Set sections 0 as hidden. 98 set_section_visible($course->id, 0, 0); 99 100 // Create an action factory. 101 $factory = new \core_calendar\action_factory(); 102 103 // Decorate action event for the student. 104 $actionevent = mod_label_core_calendar_provide_event_action($event, $factory, $student->id); 105 106 // Confirm the event is not shown at all. 107 $this->assertNull($actionevent); 108 } 109 110 public function test_label_core_calendar_provide_event_action_for_user() { 111 global $CFG; 112 113 // Create the activity. 114 $course = $this->getDataGenerator()->create_course(); 115 $label = $this->getDataGenerator()->create_module('label', array('course' => $course->id)); 116 117 // Enrol a student in the course. 118 $student = $this->getDataGenerator()->create_and_enrol($course, 'student'); 119 120 // Create a calendar event. 121 $event = $this->create_action_event($course->id, $label->id, 122 \core_completion\api::COMPLETION_EVENT_TYPE_DATE_COMPLETION_EXPECTED); 123 124 // Now, log out. 125 $CFG->forcelogin = true; // We don't want to be logged in as guest, as guest users might still have some capabilities. 126 $this->setUser(); 127 128 // Create an action factory. 129 $factory = new \core_calendar\action_factory(); 130 131 // Decorate action event for the student. 132 $actionevent = mod_label_core_calendar_provide_event_action($event, $factory, $student->id); 133 134 // Confirm the event was decorated. 135 $this->assertInstanceOf('\core_calendar\local\event\value_objects\action', $actionevent); 136 $this->assertEquals(get_string('view'), $actionevent->get_name()); 137 $this->assertInstanceOf('moodle_url', $actionevent->get_url()); 138 $this->assertEquals(1, $actionevent->get_item_count()); 139 $this->assertTrue($actionevent->is_actionable()); 140 } 141 142 public function test_label_core_calendar_provide_event_action_already_completed() { 143 global $CFG; 144 145 $CFG->enablecompletion = 1; 146 147 // Create the activity. 148 $course = $this->getDataGenerator()->create_course(array('enablecompletion' => 1)); 149 $label = $this->getDataGenerator()->create_module('label', array('course' => $course->id), 150 array('completion' => 2, 'completionview' => 1, 'completionexpected' => time() + DAYSECS)); 151 152 // Get some additional data. 153 $cm = get_coursemodule_from_instance('label', $label->id); 154 155 // Create a calendar event. 156 $event = $this->create_action_event($course->id, $label->id, 157 \core_completion\api::COMPLETION_EVENT_TYPE_DATE_COMPLETION_EXPECTED); 158 159 // Mark the activity as completed. 160 $completion = new \completion_info($course); 161 $completion->set_module_viewed($cm); 162 163 // Create an action factory. 164 $factory = new \core_calendar\action_factory(); 165 166 // Decorate action event. 167 $actionevent = mod_label_core_calendar_provide_event_action($event, $factory); 168 169 // Ensure result was null. 170 $this->assertNull($actionevent); 171 } 172 173 public function test_label_core_calendar_provide_event_action_already_completed_for_user() { 174 global $CFG; 175 176 $CFG->enablecompletion = 1; 177 178 // Create the activity. 179 $course = $this->getDataGenerator()->create_course(array('enablecompletion' => 1)); 180 $label = $this->getDataGenerator()->create_module('label', array('course' => $course->id), 181 array('completion' => 2, 'completionview' => 1, 'completionexpected' => time() + DAYSECS)); 182 183 // Enrol a student in the course. 184 $student = $this->getDataGenerator()->create_and_enrol($course, 'student'); 185 186 // Get some additional data. 187 $cm = get_coursemodule_from_instance('label', $label->id); 188 189 // Create a calendar event. 190 $event = $this->create_action_event($course->id, $label->id, 191 \core_completion\api::COMPLETION_EVENT_TYPE_DATE_COMPLETION_EXPECTED); 192 193 // Mark the activity as completed for the student. 194 $completion = new \completion_info($course); 195 $completion->set_module_viewed($cm, $student->id); 196 197 // Create an action factory. 198 $factory = new \core_calendar\action_factory(); 199 200 // Decorate action event for the student. 201 $actionevent = mod_label_core_calendar_provide_event_action($event, $factory, $student->id); 202 203 // Ensure result was null. 204 $this->assertNull($actionevent); 205 } 206 207 /** 208 * Check label name with different content inserted in the label intro. 209 * 210 * @param string $labelcontent 211 * @param string $labelformat 212 * @param string $expectedlabelname 213 * @return void 214 * @covers \get_label_name 215 * @dataProvider label_get_name_data_provider 216 */ 217 public function test_label_get_label_name(string $labelcontent, string $labelformat, string $expectedlabelname): void { 218 $course = $this->getDataGenerator()->create_course(); 219 // When creating the module, get_label_name is called and fills label->name. 220 $label = $this->getDataGenerator()->create_module('label', [ 221 'course' => $course->id, 222 'intro' => $labelcontent, 223 'introformat' => $labelformat 224 ] 225 ); 226 $this->assertEquals($expectedlabelname, $label->name); 227 } 228 229 /** 230 * Dataprovider for test_label_get_label_name 231 * 232 * @return array 233 */ 234 public function label_get_name_data_provider(): array { 235 return [ 236 'simple' => [ 237 'content' => '<p>Simple textual content<p>', 238 'format' => FORMAT_HTML, 239 'expected' => 'Simple textual content' 240 ], 241 'empty' => [ 242 'content' => '', 243 'format' => FORMAT_HTML, 244 'expected' => 'Test label 1' 245 ], 246 'withaudiocontent' => [ 247 'content' => '<p>Test with audio</p> 248 <p> <audio controls="controls"> 249 <source src="@@PLUGINFILE@@/moodle-hit-song.mp3"> 250 @@PLUGINFILE@@/moodle-hit-song.mp3 251 </audio> </p>', 252 'format' => FORMAT_HTML, 253 'expected' => 'Test with audio' 254 ], 255 'withvideo' => [ 256 'content' => '<p>Test video</p> 257 <p> <video controls="controls"> 258 <source src="https://www.youtube.com/watch?v=xxxyy"> 259 https://www.youtube.com/watch?v=xxxyy 260 </video> </p>', 261 'format' => FORMAT_HTML, 262 'expected' => 'Test video https://www.youtube.com/watch?v=xxxyy' 263 ], 264 'with video trimming' => [ 265 'content' => '<p>Test with video to be trimmed</p> 266 <p> <video controls="controls"> 267 <source src="https://www.youtube.com/watch?v=xxxyy"> 268 https://www.youtube.com/watch?v=xxxyy 269 </video> </p>', 270 'format' => FORMAT_HTML, 271 'expected' => 'Test with video to be trimmed https://www.youtube....' 272 ], 273 'with plain text' => [ 274 'content' => 'Content with @@PLUGINFILE@@/moodle-hit-song.mp3 nothing', 275 'format' => FORMAT_HTML, 276 'expected' => 'Content with nothing' 277 ], 278 'with several spaces' => [ 279 'content' => "Content with @@PLUGINFILE@@/moodle-hit-song.mp3 \r several spaces", 280 'format' => FORMAT_HTML, 281 'expected' => 'Content with several spaces' 282 ], 283 'empty spaces' => [ 284 'content' => ' ', 285 'format' => FORMAT_HTML, 286 'expected' => 'Text and media area' 287 ], 288 'only html' => [ 289 'content' => '<audio controls="controls"><source src=""></audio>', 290 'format' => FORMAT_HTML, 291 'expected' => 'Text and media area' 292 ], 293 'markdown' => [ 294 'content' => "##Simple Title\n simple markdown format", 295 'format' => FORMAT_MARKDOWN, 296 'expected' => 'Simple Title simple markdown format' 297 ], 298 'markdown with pluginfile' => [ 299 'content' => "##Simple Title\n simple markdown format @@PLUGINFILE@@/moodle-hit-song.mp3", 300 'format' => FORMAT_MARKDOWN, 301 'expected' => 'Simple Title simple markdown format' 302 ], 303 'plain text' => [ 304 'content' => "Simple plain text @@PLUGINFILE@@/moodle-hit-song.mp3", 305 'format' => FORMAT_PLAIN, 306 'expected' => 'Simple plain text' 307 ], 308 'moodle format text' => [ 309 'content' => "Simple plain text @@PLUGINFILE@@/moodle-hit-song.mp3", 310 'format' => FORMAT_MOODLE, 311 'expected' => 'Simple plain text' 312 ], 313 'html format text' => [ 314 'content' => "<h1>Simple plain title</h1><p> with plain text</p> @@PLUGINFILE@@/moodle-hit-song.mp3", 315 'format' => FORMAT_HTML, 316 'expected' => 'Simple plain title with plain text' 317 ], 318 ]; 319 } 320 321 /** 322 * Creates an action event. 323 * 324 * @param int $courseid The course id. 325 * @param int $instanceid The instance id. 326 * @param string $eventtype The event type. 327 * @return bool|calendar_event 328 */ 329 private function create_action_event($courseid, $instanceid, $eventtype) { 330 $event = new \stdClass(); 331 $event->name = 'Calendar event'; 332 $event->modulename = 'label'; 333 $event->courseid = $courseid; 334 $event->instance = $instanceid; 335 $event->type = CALENDAR_EVENT_TYPE_ACTION; 336 $event->eventtype = $eventtype; 337 $event->timestart = time(); 338 339 return \calendar_event::create($event); 340 } 341 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body