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 /** 18 * Tests for the enrol_lti_plugin class. 19 * 20 * @package enrol_lti 21 * @copyright 2016 Jun Pataleta <jun@moodle.com> 22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 23 */ 24 namespace enrol_lti; 25 26 use course_enrolment_manager; 27 use enrol_lti_plugin; 28 use enrol_lti\data_connector; 29 use enrol_lti\tool_provider; 30 use IMSGlobal\LTI\ToolProvider\ResourceLink; 31 use IMSGlobal\LTI\ToolProvider\ToolConsumer; 32 use IMSGlobal\LTI\ToolProvider\ToolProvider; 33 use IMSGlobal\LTI\ToolProvider\User; 34 35 defined('MOODLE_INTERNAL') || die(); 36 37 /** 38 * Tests for the enrol_lti_plugin class. 39 * 40 * @package enrol_lti 41 * @copyright 2016 Jun Pataleta <jun@moodle.com> 42 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 43 */ 44 class lib_test extends \advanced_testcase { 45 46 /** 47 * Test set up. 48 * 49 * This is executed before running any tests in this file. 50 */ 51 public function setUp(): void { 52 $this->resetAfterTest(); 53 $this->setAdminUser(); 54 } 55 56 /** 57 * Test for enrol_lti_plugin::delete_instance(). 58 */ 59 public function test_delete_instance() { 60 global $DB; 61 62 // Create tool enrolment instance. 63 $data = new \stdClass(); 64 $data->enrolstartdate = time(); 65 $data->secret = 'secret'; 66 $tool = $this->getDataGenerator()->create_lti_tool($data); 67 68 // Create consumer and related data. 69 $dataconnector = new data_connector(); 70 $consumer = new ToolConsumer('testkey', $dataconnector); 71 $consumer->secret = $tool->secret; 72 $consumer->ltiVersion = ToolProvider::LTI_VERSION1; 73 $consumer->name = 'TEST CONSUMER NAME'; 74 $consumer->consumerName = 'TEST CONSUMER INSTANCE NAME'; 75 $consumer->consumerGuid = 'TEST CONSUMER INSTANCE GUID'; 76 $consumer->consumerVersion = 'TEST CONSUMER INFO VERSION'; 77 $consumer->enabled = true; 78 $consumer->protected = true; 79 $consumer->save(); 80 81 $resourcelink = ResourceLink::fromConsumer($consumer, 'testresourcelinkid'); 82 $resourcelink->save(); 83 84 $ltiuser = User::fromResourceLink($resourcelink, ''); 85 $ltiuser->ltiResultSourcedId = 'testLtiResultSourcedId'; 86 $ltiuser->ltiUserId = 'testuserid'; 87 $ltiuser->email = 'user1@example.com'; 88 $ltiuser->save(); 89 90 $tp = new tool_provider($tool->id); 91 $tp->user = $ltiuser; 92 $tp->resourceLink = $resourcelink; 93 $tp->consumer = $consumer; 94 $tp->map_tool_to_consumer(); 95 96 $mappingparams = [ 97 'toolid' => $tool->id, 98 'consumerid' => $tp->consumer->getRecordId() 99 ]; 100 101 // Check first that the related records exist. 102 $this->assertTrue($DB->record_exists('enrol_lti_tool_consumer_map', $mappingparams)); 103 $this->assertTrue($DB->record_exists('enrol_lti_lti2_consumer', [ 'id' => $consumer->getRecordId() ])); 104 $this->assertTrue($DB->record_exists('enrol_lti_lti2_resource_link', [ 'id' => $resourcelink->getRecordId() ])); 105 $this->assertTrue($DB->record_exists('enrol_lti_lti2_user_result', [ 'id' => $ltiuser->getRecordId() ])); 106 107 // Perform deletion. 108 $enrollti = new enrol_lti_plugin(); 109 $instance = $DB->get_record('enrol', ['id' => $tool->enrolid]); 110 $enrollti->delete_instance($instance); 111 112 // Check that the related records have been deleted. 113 $this->assertFalse($DB->record_exists('enrol_lti_tool_consumer_map', $mappingparams)); 114 $this->assertFalse($DB->record_exists('enrol_lti_lti2_consumer', [ 'id' => $consumer->getRecordId() ])); 115 $this->assertFalse($DB->record_exists('enrol_lti_lti2_resource_link', [ 'id' => $resourcelink->getRecordId() ])); 116 $this->assertFalse($DB->record_exists('enrol_lti_lti2_user_result', [ 'id' => $ltiuser->getRecordId() ])); 117 118 // Check that the enrolled users and the tool instance has been deleted. 119 $this->assertFalse($DB->record_exists('enrol_lti_users', [ 'toolid' => $tool->id ])); 120 $this->assertFalse($DB->record_exists('enrol_lti_tools', [ 'id' => $tool->id ])); 121 $this->assertFalse($DB->record_exists('enrol', [ 'id' => $instance->id ])); 122 } 123 124 /** 125 * Test for getting user enrolment actions. 126 */ 127 public function test_get_user_enrolment_actions() { 128 global $CFG, $DB, $PAGE; 129 $this->resetAfterTest(); 130 131 // Set page URL to prevent debugging messages. 132 $PAGE->set_url('/enrol/editinstance.php'); 133 134 $pluginname = 'lti'; 135 136 // Only enable the lti enrol plugin. 137 $CFG->enrol_plugins_enabled = $pluginname; 138 139 $generator = $this->getDataGenerator(); 140 141 // Get the enrol plugin. 142 $plugin = enrol_get_plugin($pluginname); 143 144 // Create a course. 145 $course = $generator->create_course(); 146 $context = \context_course::instance($course->id); 147 $teacherroleid = $DB->get_field('role', 'id', ['shortname' => 'editingteacher'], MUST_EXIST); 148 $studentroleid = $DB->get_field('role', 'id', ['shortname' => 'student'], MUST_EXIST); 149 150 // Enable this enrol plugin for the course. 151 $fields = ['contextid' => $context->id, 'roleinstructor' => $teacherroleid, 'rolelearner' => $studentroleid]; 152 $plugin->add_instance($course, $fields); 153 154 // Create a student. 155 $student = $generator->create_user(); 156 // Enrol the student to the course. 157 $generator->enrol_user($student->id, $course->id, 'student', $pluginname); 158 159 // Teachers don't have enrol/lti:unenrol capability by default. Login as admin for simplicity. 160 $this->setAdminUser(); 161 162 require_once($CFG->dirroot . '/enrol/locallib.php'); 163 $manager = new course_enrolment_manager($PAGE, $course); 164 $userenrolments = $manager->get_user_enrolments($student->id); 165 $this->assertCount(1, $userenrolments); 166 167 $ue = reset($userenrolments); 168 $actions = $plugin->get_user_enrolment_actions($manager, $ue); 169 // LTI enrolment has 1 enrol actions for active users -- unenrol. 170 $this->assertCount(1, $actions); 171 } 172 173 /** 174 * Test the behaviour of an enrolment method when the activity to which it provides access is deleted. 175 * 176 * @covers \enrol_lti_pre_course_module_delete 177 */ 178 public function test_course_module_deletion() { 179 // Create two modules and publish them. 180 $course = $this->getDataGenerator()->create_course(); 181 $mod = $this->getDataGenerator()->create_module('assign', ['course' => $course->id]); 182 $mod2 = $this->getDataGenerator()->create_module('assign', ['course' => $course->id]); 183 $tooldata = [ 184 'cmid' => $mod->cmid, 185 'courseid' => $course->id, 186 ]; 187 $tool = $this->getDataGenerator()->create_lti_tool((object)$tooldata); 188 $tooldata['cmid'] = $mod2->cmid; 189 $tool2 = $this->getDataGenerator()->create_lti_tool((object)$tooldata); 190 191 // Verify the instances are both enabled. 192 $modinstance = helper::get_lti_tool($tool->id); 193 $mod2instance = helper::get_lti_tool($tool2->id); 194 $this->assertEquals(ENROL_INSTANCE_ENABLED, $modinstance->status); 195 $this->assertEquals(ENROL_INSTANCE_ENABLED, $mod2instance->status); 196 197 // Delete a module and verify the associated instance is disabled. 198 course_delete_module($mod->cmid); 199 $modinstance = helper::get_lti_tool($tool->id); 200 $mod2instance = helper::get_lti_tool($tool2->id); 201 $this->assertEquals(ENROL_INSTANCE_DISABLED, $modinstance->status); 202 $this->assertEquals(ENROL_INSTANCE_ENABLED, $mod2instance->status); 203 } 204 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body