Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.3.x will end 7 October 2024 (12 months).
  • Bug fixes for security issues in 4.3.x will end 21 April 2025 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.2.x is supported too.
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.

/**
 * Tests for the enrol_lti_plugin class.
 *
 * @package enrol_lti
 * @copyright 2016 Jun Pataleta <jun@moodle.com>
 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 */
> namespace enrol_lti;
< use enrol_lti\data_connector; < use enrol_lti\tool_provider;
> use course_enrolment_manager; > use enrol_lti_plugin;
use IMSGlobal\LTI\ToolProvider\ResourceLink; use IMSGlobal\LTI\ToolProvider\ToolConsumer; use IMSGlobal\LTI\ToolProvider\ToolProvider; use IMSGlobal\LTI\ToolProvider\User; defined('MOODLE_INTERNAL') || die();
> require_once(__DIR__ . '/local/ltiadvantage/lti_advantage_testcase.php'); /** >
* Tests for the enrol_lti_plugin class. * * @package enrol_lti * @copyright 2016 Jun Pataleta <jun@moodle.com> * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */
< class enrol_lti_testcase extends advanced_testcase {
> class lib_test extends \lti_advantage_testcase {
/** * Test set up. * * This is executed before running any tests in this file. */ public function setUp(): void { $this->resetAfterTest(); $this->setAdminUser(); } /** * Test for enrol_lti_plugin::delete_instance(). */ public function test_delete_instance() { global $DB; // Create tool enrolment instance.
< $data = new stdClass();
> $data = new \stdClass();
$data->enrolstartdate = time(); $data->secret = 'secret'; $tool = $this->getDataGenerator()->create_lti_tool($data); // Create consumer and related data. $dataconnector = new data_connector(); $consumer = new ToolConsumer('testkey', $dataconnector); $consumer->secret = $tool->secret; $consumer->ltiVersion = ToolProvider::LTI_VERSION1; $consumer->name = 'TEST CONSUMER NAME'; $consumer->consumerName = 'TEST CONSUMER INSTANCE NAME'; $consumer->consumerGuid = 'TEST CONSUMER INSTANCE GUID'; $consumer->consumerVersion = 'TEST CONSUMER INFO VERSION'; $consumer->enabled = true; $consumer->protected = true; $consumer->save(); $resourcelink = ResourceLink::fromConsumer($consumer, 'testresourcelinkid'); $resourcelink->save(); $ltiuser = User::fromResourceLink($resourcelink, ''); $ltiuser->ltiResultSourcedId = 'testLtiResultSourcedId'; $ltiuser->ltiUserId = 'testuserid'; $ltiuser->email = 'user1@example.com'; $ltiuser->save(); $tp = new tool_provider($tool->id); $tp->user = $ltiuser; $tp->resourceLink = $resourcelink; $tp->consumer = $consumer; $tp->map_tool_to_consumer(); $mappingparams = [ 'toolid' => $tool->id, 'consumerid' => $tp->consumer->getRecordId() ]; // Check first that the related records exist. $this->assertTrue($DB->record_exists('enrol_lti_tool_consumer_map', $mappingparams)); $this->assertTrue($DB->record_exists('enrol_lti_lti2_consumer', [ 'id' => $consumer->getRecordId() ])); $this->assertTrue($DB->record_exists('enrol_lti_lti2_resource_link', [ 'id' => $resourcelink->getRecordId() ])); $this->assertTrue($DB->record_exists('enrol_lti_lti2_user_result', [ 'id' => $ltiuser->getRecordId() ])); // Perform deletion. $enrollti = new enrol_lti_plugin(); $instance = $DB->get_record('enrol', ['id' => $tool->enrolid]); $enrollti->delete_instance($instance); // Check that the related records have been deleted. $this->assertFalse($DB->record_exists('enrol_lti_tool_consumer_map', $mappingparams)); $this->assertFalse($DB->record_exists('enrol_lti_lti2_consumer', [ 'id' => $consumer->getRecordId() ])); $this->assertFalse($DB->record_exists('enrol_lti_lti2_resource_link', [ 'id' => $resourcelink->getRecordId() ])); $this->assertFalse($DB->record_exists('enrol_lti_lti2_user_result', [ 'id' => $ltiuser->getRecordId() ])); // Check that the enrolled users and the tool instance has been deleted. $this->assertFalse($DB->record_exists('enrol_lti_users', [ 'toolid' => $tool->id ])); $this->assertFalse($DB->record_exists('enrol_lti_tools', [ 'id' => $tool->id ])); $this->assertFalse($DB->record_exists('enrol', [ 'id' => $instance->id ])); } /**
> * Test confirming that relevant data is removed after enrol instance removal. * Test for getting user enrolment actions. > * */ > * @covers \enrol_lti_plugin::delete_instance public function test_get_user_enrolment_actions() { > */ global $CFG, $DB, $PAGE; > public function test_delete_instance_lti_advantage() { $this->resetAfterTest(); > global $DB; > // Setup. // Set page URL to prevent debugging messages. > [ $PAGE->set_url('/enrol/editinstance.php'); > $course, > $modresource, $pluginname = 'lti'; > $modresource2, > $courseresource, // Only enable the lti enrol plugin. > $registration, $CFG->enrol_plugins_enabled = $pluginname; > $deployment > ] = $this->create_test_environment(); $generator = $this->getDataGenerator(); > > // Launch the tool. // Get the enrol plugin. > $mockuser = $this->get_mock_launch_users_with_ids(['1p3_1'])[0]; $plugin = enrol_get_plugin($pluginname); > $mocklaunch = $this->get_mock_launch($modresource, $mockuser); > $instructoruser = $this->getDataGenerator()->create_user(); // Create a course. > $launchservice = $this->get_tool_launch_service(); $course = $generator->create_course(); > $launchservice->user_launches_tool($instructoruser, $mocklaunch); $context = context_course::instance($course->id); > $teacherroleid = $DB->get_field('role', 'id', ['shortname' => 'editingteacher'], MUST_EXIST); > // Verify data exists. $studentroleid = $DB->get_field('role', 'id', ['shortname' => 'student'], MUST_EXIST); > $this->assertEquals(1, $DB->count_records('enrol_lti_user_resource_link')); > $this->assertEquals(1, $DB->count_records('enrol_lti_resource_link')); // Enable this enrol plugin for the course. > $this->assertEquals(1, $DB->count_records('enrol_lti_app_registration')); $fields = ['contextid' => $context->id, 'roleinstructor' => $teacherroleid, 'rolelearner' => $studentroleid]; > $this->assertEquals(1, $DB->count_records('enrol_lti_deployment')); $plugin->add_instance($course, $fields); > $this->assertEquals(1, $DB->count_records('enrol_lti_context')); > $this->assertEquals(1, $DB->count_records('enrol_lti_users')); // Create a student. > $student = $generator->create_user(); > // Now delete the enrol instance. // Enrol the student to the course. > $enrollti = new enrol_lti_plugin(); $generator->enrol_user($student->id, $course->id, 'student', $pluginname); > $instance = $DB->get_record('enrol', ['id' => $modresource->enrolid]); > $enrollti->delete_instance($instance); // Teachers don't have enrol/lti:unenrol capability by default. Login as admin for simplicity. > $this->setAdminUser(); > $this->assertEquals(0, $DB->count_records('enrol_lti_user_resource_link')); > $this->assertEquals(0, $DB->count_records('enrol_lti_resource_link')); require_once($CFG->dirroot . '/enrol/locallib.php'); > $this->assertEquals(0, $DB->count_records('enrol_lti_users')); $manager = new course_enrolment_manager($PAGE, $course); > $userenrolments = $manager->get_user_enrolments($student->id); > // App registration, Deployment and Context tables are not affected by instance removal. $this->assertCount(1, $userenrolments); > $this->assertEquals(1, $DB->count_records('enrol_lti_app_registration')); > $this->assertEquals(1, $DB->count_records('enrol_lti_deployment')); $ue = reset($userenrolments); > $this->assertEquals(1, $DB->count_records('enrol_lti_context')); $actions = $plugin->get_user_enrolment_actions($manager, $ue); > } // LTI enrolment has 1 enrol actions for active users -- unenrol. > $this->assertCount(1, $actions); > /**
< $context = context_course::instance($course->id);
> $context = \context_course::instance($course->id);
}
> } > > /** > * Test the behaviour of an enrolment method when the activity to which it provides access is deleted. > * > * @covers \enrol_lti_pre_course_module_delete > */ > public function test_course_module_deletion() { > // Create two modules and publish them. > $course = $this->getDataGenerator()->create_course(); > $mod = $this->getDataGenerator()->create_module('assign', ['course' => $course->id]); > $mod2 = $this->getDataGenerator()->create_module('assign', ['course' => $course->id]); > $tooldata = [ > 'cmid' => $mod->cmid, > 'courseid' => $course->id, > ]; > $tool = $this->getDataGenerator()->create_lti_tool((object)$tooldata); > $tooldata['cmid'] = $mod2->cmid; > $tool2 = $this->getDataGenerator()->create_lti_tool((object)$tooldata); > > // Verify the instances are both enabled. > $modinstance = helper::get_lti_tool($tool->id); > $mod2instance = helper::get_lti_tool($tool2->id); > $this->assertEquals(ENROL_INSTANCE_ENABLED, $modinstance->status); > $this->assertEquals(ENROL_INSTANCE_ENABLED, $mod2instance->status); > > // Delete a module and verify the associated instance is disabled. > course_delete_module($mod->cmid); > $modinstance = helper::get_lti_tool($tool->id); > $mod2instance = helper::get_lti_tool($tool2->id); > $this->assertEquals(ENROL_INSTANCE_DISABLED, $modinstance->status); > $this->assertEquals(ENROL_INSTANCE_ENABLED, $mod2instance->status);