Differences Between: [Versions 310 and 311] [Versions 311 and 400] [Versions 311 and 401] [Versions 311 and 402] [Versions 311 and 403] [Versions 39 and 311]
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 * Creates an action event. 209 * 210 * @param int $courseid The course id. 211 * @param int $instanceid The instance id. 212 * @param string $eventtype The event type. 213 * @return bool|calendar_event 214 */ 215 private function create_action_event($courseid, $instanceid, $eventtype) { 216 $event = new \stdClass(); 217 $event->name = 'Calendar event'; 218 $event->modulename = 'label'; 219 $event->courseid = $courseid; 220 $event->instance = $instanceid; 221 $event->type = CALENDAR_EVENT_TYPE_ACTION; 222 $event->eventtype = $eventtype; 223 $event->timestart = time(); 224 225 return \calendar_event::create($event); 226 } 227 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body