Differences Between: [Versions 310 and 311] [Versions 310 and 400] [Versions 310 and 401] [Versions 310 and 402] [Versions 310 and 403] [Versions 39 and 310]
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 * Unit tests for mod_folder lib 19 * 20 * @package mod_folder 21 * @category external 22 * @copyright 2015 Juan Leyva <juan@moodle.com> 23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 24 * @since Moodle 3.0 25 */ 26 27 defined('MOODLE_INTERNAL') || die(); 28 29 30 /** 31 * Unit tests for mod_folder lib 32 * 33 * @package mod_folder 34 * @category external 35 * @copyright 2015 Juan Leyva <juan@moodle.com> 36 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 37 * @since Moodle 3.0 38 */ 39 class mod_folder_lib_testcase extends advanced_testcase { 40 41 /** 42 * Setup. 43 */ 44 public function setUp(): void { 45 $this->resetAfterTest(); 46 $this->setAdminUser(); 47 } 48 49 /** 50 * Prepares things before this test case is initialised 51 * @return void 52 */ 53 public static function setUpBeforeClass(): void { 54 global $CFG; 55 require_once($CFG->dirroot . '/mod/folder/lib.php'); 56 } 57 58 /** 59 * Test folder_view 60 * @return void 61 */ 62 public function test_folder_view() { 63 global $CFG; 64 65 $CFG->enablecompletion = 1; 66 67 // Setup test data. 68 $course = $this->getDataGenerator()->create_course(array('enablecompletion' => 1)); 69 $folder = $this->getDataGenerator()->create_module('folder', array('course' => $course->id), 70 array('completion' => 2, 'completionview' => 1)); 71 $context = context_module::instance($folder->cmid); 72 $cm = get_coursemodule_from_instance('folder', $folder->id); 73 74 // Trigger and capture the event. 75 $sink = $this->redirectEvents(); 76 77 folder_view($folder, $course, $cm, $context); 78 79 $events = $sink->get_events(); 80 // 2 additional events thanks to completion. 81 $this->assertCount(3, $events); 82 $event = array_shift($events); 83 84 // Checking that the event contains the expected values. 85 $this->assertInstanceOf('\mod_folder\event\course_module_viewed', $event); 86 $this->assertEquals($context, $event->get_context()); 87 $moodleurl = new \moodle_url('/mod/folder/view.php', array('id' => $cm->id)); 88 $this->assertEquals($moodleurl, $event->get_url()); 89 $this->assertEventContextNotUsed($event); 90 $this->assertNotEmpty($event->get_name()); 91 92 // Check completion status. 93 $completion = new completion_info($course); 94 $completiondata = $completion->get_data($cm); 95 $this->assertEquals(1, $completiondata->completionstate); 96 } 97 98 public function test_folder_core_calendar_provide_event_action() { 99 // Create the activity. 100 $course = $this->getDataGenerator()->create_course(); 101 $folder = $this->getDataGenerator()->create_module('folder', array('course' => $course->id)); 102 103 // Create a calendar event. 104 $event = $this->create_action_event($course->id, $folder->id, 105 \core_completion\api::COMPLETION_EVENT_TYPE_DATE_COMPLETION_EXPECTED); 106 107 // Create an action factory. 108 $factory = new \core_calendar\action_factory(); 109 110 // Decorate action event. 111 $actionevent = mod_folder_core_calendar_provide_event_action($event, $factory); 112 113 // Confirm the event was decorated. 114 $this->assertInstanceOf('\core_calendar\local\event\value_objects\action', $actionevent); 115 $this->assertEquals(get_string('view'), $actionevent->get_name()); 116 $this->assertInstanceOf('moodle_url', $actionevent->get_url()); 117 $this->assertEquals(1, $actionevent->get_item_count()); 118 $this->assertTrue($actionevent->is_actionable()); 119 } 120 121 public function test_folder_core_calendar_provide_event_action_for_non_user() { 122 global $CFG; 123 124 // Create a course. 125 $course = $this->getDataGenerator()->create_course(); 126 127 // Create the activity. 128 $folder = $this->getDataGenerator()->create_module('folder', array('course' => $course->id)); 129 130 // Create a calendar event. 131 $event = $this->create_action_event($course->id, $folder->id, 132 \core_completion\api::COMPLETION_EVENT_TYPE_DATE_COMPLETION_EXPECTED); 133 134 // Now, log out. 135 $CFG->forcelogin = true; // We don't want to be logged in as guest, as guest users might still have some capabilities. 136 $this->setUser(); 137 138 // Create an action factory. 139 $factory = new \core_calendar\action_factory(); 140 141 // Decorate action event. 142 $actionevent = mod_folder_core_calendar_provide_event_action($event, $factory); 143 144 // Confirm the event is not shown at all. 145 $this->assertNull($actionevent); 146 } 147 148 public function test_folder_core_calendar_provide_event_action_in_hidden_section() { 149 // Create a course. 150 $course = $this->getDataGenerator()->create_course(); 151 152 // Create a student. 153 $student = $this->getDataGenerator()->create_and_enrol($course, 'student'); 154 155 // Create the activity. 156 $folder = $this->getDataGenerator()->create_module('folder', array('course' => $course->id)); 157 158 // Create a calendar event. 159 $event = $this->create_action_event($course->id, $folder->id, 160 \core_completion\api::COMPLETION_EVENT_TYPE_DATE_COMPLETION_EXPECTED); 161 162 // Set sections 0 as hidden. 163 set_section_visible($course->id, 0, 0); 164 165 // Create an action factory. 166 $factory = new \core_calendar\action_factory(); 167 168 // Decorate action event. 169 $actionevent = mod_folder_core_calendar_provide_event_action($event, $factory, $student->id); 170 171 // Confirm the event is not shown at all. 172 $this->assertNull($actionevent); 173 } 174 175 public function test_folder_core_calendar_provide_event_action_for_user() { 176 // Create a course. 177 $course = $this->getDataGenerator()->create_course(); 178 179 // Create a student. 180 $student = $this->getDataGenerator()->create_and_enrol($course, 'student'); 181 182 // Create the activity. 183 $folder = $this->getDataGenerator()->create_module('folder', array('course' => $course->id)); 184 185 // Create a calendar event. 186 $event = $this->create_action_event($course->id, $folder->id, 187 \core_completion\api::COMPLETION_EVENT_TYPE_DATE_COMPLETION_EXPECTED); 188 189 // Now, log out. 190 $this->setUser(); 191 192 // Create an action factory. 193 $factory = new \core_calendar\action_factory(); 194 195 // Decorate action event for the student. 196 $actionevent = mod_folder_core_calendar_provide_event_action($event, $factory, $student->id); 197 198 // Confirm the event was decorated. 199 $this->assertInstanceOf('\core_calendar\local\event\value_objects\action', $actionevent); 200 $this->assertEquals(get_string('view'), $actionevent->get_name()); 201 $this->assertInstanceOf('moodle_url', $actionevent->get_url()); 202 $this->assertEquals(1, $actionevent->get_item_count()); 203 $this->assertTrue($actionevent->is_actionable()); 204 } 205 206 public function test_folder_core_calendar_provide_event_action_already_completed() { 207 global $CFG; 208 209 $CFG->enablecompletion = 1; 210 211 // Create the activity. 212 $course = $this->getDataGenerator()->create_course(array('enablecompletion' => 1)); 213 $folder = $this->getDataGenerator()->create_module('folder', array('course' => $course->id), 214 array('completion' => 2, 'completionview' => 1, 'completionexpected' => time() + DAYSECS)); 215 216 // Get some additional data. 217 $cm = get_coursemodule_from_instance('folder', $folder->id); 218 219 // Create a calendar event. 220 $event = $this->create_action_event($course->id, $folder->id, 221 \core_completion\api::COMPLETION_EVENT_TYPE_DATE_COMPLETION_EXPECTED); 222 223 // Mark the activity as completed. 224 $completion = new completion_info($course); 225 $completion->set_module_viewed($cm); 226 227 // Create an action factory. 228 $factory = new \core_calendar\action_factory(); 229 230 // Decorate action event. 231 $actionevent = mod_folder_core_calendar_provide_event_action($event, $factory); 232 233 // Ensure result was null. 234 $this->assertNull($actionevent); 235 } 236 237 public function test_folder_core_calendar_provide_event_action_already_completed_for_user() { 238 global $CFG; 239 240 $CFG->enablecompletion = 1; 241 242 // Create a course. 243 $course = $this->getDataGenerator()->create_course(array('enablecompletion' => 1)); 244 245 // Create a student. 246 $student = $this->getDataGenerator()->create_and_enrol($course, 'student'); 247 248 // Create the activity. 249 $folder = $this->getDataGenerator()->create_module('folder', array('course' => $course->id), 250 array('completion' => 2, 'completionview' => 1, 'completionexpected' => time() + DAYSECS)); 251 252 // Get some additional data. 253 $cm = get_coursemodule_from_instance('folder', $folder->id); 254 255 // Create a calendar event. 256 $event = $this->create_action_event($course->id, $folder->id, 257 \core_completion\api::COMPLETION_EVENT_TYPE_DATE_COMPLETION_EXPECTED); 258 259 // Mark the activity as completed for the student. 260 $completion = new completion_info($course); 261 $completion->set_module_viewed($cm, $student->id); 262 263 // Now, log out. 264 $this->setUser(); 265 266 // Create an action factory. 267 $factory = new \core_calendar\action_factory(); 268 269 // Decorate action event for the student. 270 $actionevent = mod_folder_core_calendar_provide_event_action($event, $factory, $student->id); 271 272 // Ensure result was null. 273 $this->assertNull($actionevent); 274 } 275 276 /** 277 * Creates an action event. 278 * 279 * @param int $courseid The course id. 280 * @param int $instanceid The instance id. 281 * @param string $eventtype The event type. 282 * @return bool|calendar_event 283 */ 284 private function create_action_event($courseid, $instanceid, $eventtype) { 285 $event = new stdClass(); 286 $event->name = 'Calendar event'; 287 $event->modulename = 'folder'; 288 $event->courseid = $courseid; 289 $event->instance = $instanceid; 290 $event->type = CALENDAR_EVENT_TYPE_ACTION; 291 $event->eventtype = $eventtype; 292 $event->timestart = time(); 293 294 return calendar_event::create($event); 295 } 296 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body