See Release Notes
Long Term Support Release
Differences Between: [Versions 310 and 401] [Versions 39 and 401]
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_page lib 19 * 20 * @package mod_page 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 namespace mod_page; 27 28 defined('MOODLE_INTERNAL') || die(); 29 30 31 /** 32 * Unit tests for mod_page lib 33 * 34 * @package mod_page 35 * @category external 36 * @copyright 2015 Juan Leyva <juan@moodle.com> 37 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 38 * @since Moodle 3.0 39 */ 40 class lib_test extends \advanced_testcase { 41 42 /** 43 * Prepares things before this test case is initialised 44 * @return void 45 */ 46 public static function setUpBeforeClass(): void { 47 global $CFG; 48 require_once($CFG->dirroot . '/mod/page/lib.php'); 49 } 50 51 /** 52 * Test page_view 53 * @return void 54 */ 55 public function test_page_view() { 56 global $CFG; 57 58 $CFG->enablecompletion = 1; 59 $this->resetAfterTest(); 60 61 // Setup test data. 62 $course = $this->getDataGenerator()->create_course(array('enablecompletion' => 1)); 63 $page = $this->getDataGenerator()->create_module('page', array('course' => $course->id), 64 array('completion' => 2, 'completionview' => 1)); 65 $context = \context_module::instance($page->cmid); 66 $cm = get_coursemodule_from_instance('page', $page->id); 67 68 // Trigger and capture the event. 69 $sink = $this->redirectEvents(); 70 71 $this->setAdminUser(); 72 page_view($page, $course, $cm, $context); 73 74 $events = $sink->get_events(); 75 // 2 additional events thanks to completion. 76 $this->assertCount(3, $events); 77 $event = array_shift($events); 78 79 // Checking that the event contains the expected values. 80 $this->assertInstanceOf('\mod_page\event\course_module_viewed', $event); 81 $this->assertEquals($context, $event->get_context()); 82 $moodleurl = new \moodle_url('/mod/page/view.php', array('id' => $cm->id)); 83 $this->assertEquals($moodleurl, $event->get_url()); 84 $this->assertEventContextNotUsed($event); 85 $this->assertNotEmpty($event->get_name()); 86 87 // Check completion status. 88 $completion = new \completion_info($course); 89 $completiondata = $completion->get_data($cm); 90 $this->assertEquals(1, $completiondata->completionstate); 91 } 92 93 public function test_page_core_calendar_provide_event_action() { 94 $this->resetAfterTest(); 95 $this->setAdminUser(); 96 97 // Create the activity. 98 $course = $this->getDataGenerator()->create_course(); 99 $page = $this->getDataGenerator()->create_module('page', array('course' => $course->id)); 100 101 // Create a calendar event. 102 $event = $this->create_action_event($course->id, $page->id, 103 \core_completion\api::COMPLETION_EVENT_TYPE_DATE_COMPLETION_EXPECTED); 104 105 // Create an action factory. 106 $factory = new \core_calendar\action_factory(); 107 108 // Decorate action event. 109 $actionevent = mod_page_core_calendar_provide_event_action($event, $factory); 110 111 // Confirm the event was decorated. 112 $this->assertInstanceOf('\core_calendar\local\event\value_objects\action', $actionevent); 113 $this->assertEquals(get_string('view'), $actionevent->get_name()); 114 $this->assertInstanceOf('moodle_url', $actionevent->get_url()); 115 $this->assertEquals(1, $actionevent->get_item_count()); 116 $this->assertTrue($actionevent->is_actionable()); 117 } 118 119 public function test_page_core_calendar_provide_event_action_already_completed() { 120 global $CFG; 121 122 $this->resetAfterTest(); 123 $this->setAdminUser(); 124 125 $CFG->enablecompletion = 1; 126 127 // Create the activity. 128 $course = $this->getDataGenerator()->create_course(array('enablecompletion' => 1)); 129 $page = $this->getDataGenerator()->create_module('page', array('course' => $course->id), 130 array('completion' => 2, 'completionview' => 1, 'completionexpected' => time() + DAYSECS)); 131 132 // Get some additional data. 133 $cm = get_coursemodule_from_instance('page', $page->id); 134 135 // Create a calendar event. 136 $event = $this->create_action_event($course->id, $page->id, 137 \core_completion\api::COMPLETION_EVENT_TYPE_DATE_COMPLETION_EXPECTED); 138 139 // Mark the activity as completed. 140 $completion = new \completion_info($course); 141 $completion->set_module_viewed($cm); 142 143 // Create an action factory. 144 $factory = new \core_calendar\action_factory(); 145 146 // Decorate action event. 147 $actionevent = mod_page_core_calendar_provide_event_action($event, $factory); 148 149 // Ensure result was null. 150 $this->assertNull($actionevent); 151 } 152 153 /** 154 * Test mod_page_core_calendar_provide_event_action with user override 155 */ 156 public function test_page_core_calendar_provide_event_action_user_override() { 157 global $CFG, $USER; 158 159 $this->resetAfterTest(); 160 $this->setAdminUser(); 161 $user = $this->getDataGenerator()->create_user(); 162 $CFG->enablecompletion = 1; 163 164 // Create the activity. 165 $course = $this->getDataGenerator()->create_course(array('enablecompletion' => 1)); 166 $page = $this->getDataGenerator()->create_module('page', array('course' => $course->id), 167 array('completion' => 2, 'completionview' => 1, 'completionexpected' => time() + DAYSECS)); 168 169 // Get some additional data. 170 $cm = get_coursemodule_from_instance('page', $page->id); 171 172 // Create a calendar event. 173 $event = $this->create_action_event($course->id, $page->id, 174 \core_completion\api::COMPLETION_EVENT_TYPE_DATE_COMPLETION_EXPECTED); 175 176 // Mark the activity as completed. 177 $completion = new \completion_info($course); 178 $completion->set_module_viewed($cm); 179 180 // Create an action factory. 181 $factory = new \core_calendar\action_factory(); 182 183 // Decorate action event. 184 $actionevent = mod_page_core_calendar_provide_event_action($event, $factory, $USER->id); 185 186 // Decorate action with a userid override. 187 $actionevent2 = mod_page_core_calendar_provide_event_action($event, $factory, $user->id); 188 189 // Ensure result was null because it has been marked as completed for the associated user. 190 // Logic was brought across from the "_already_completed" function. 191 $this->assertNull($actionevent); 192 193 // Confirm the event was decorated. 194 $this->assertNotNull($actionevent2); 195 $this->assertInstanceOf('\core_calendar\local\event\value_objects\action', $actionevent2); 196 $this->assertEquals(get_string('view'), $actionevent2->get_name()); 197 $this->assertInstanceOf('moodle_url', $actionevent2->get_url()); 198 $this->assertEquals(1, $actionevent2->get_item_count()); 199 $this->assertTrue($actionevent2->is_actionable()); 200 } 201 202 /** 203 * Creates an action event. 204 * 205 * @param int $courseid The course id. 206 * @param int $instanceid The instance id. 207 * @param string $eventtype The event type. 208 * @return bool|calendar_event 209 */ 210 private function create_action_event($courseid, $instanceid, $eventtype) { 211 $event = new \stdClass(); 212 $event->name = 'Calendar event'; 213 $event->modulename = 'page'; 214 $event->courseid = $courseid; 215 $event->instance = $instanceid; 216 $event->type = CALENDAR_EVENT_TYPE_ACTION; 217 $event->eventtype = $eventtype; 218 $event->timestart = time(); 219 220 return \calendar_event::create($event); 221 } 222 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body