| 1 | 
           efrain | 
           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 IMSGlobal\LTI\ToolProvider\ResourceLink;
  | 
        
        
            | 
            | 
           29 | 
           use IMSGlobal\LTI\ToolProvider\ToolConsumer;
  | 
        
        
            | 
            | 
           30 | 
           use IMSGlobal\LTI\ToolProvider\ToolProvider;
  | 
        
        
            | 
            | 
           31 | 
           use IMSGlobal\LTI\ToolProvider\User;
  | 
        
        
            | 
            | 
           32 | 
              | 
        
        
            | 
            | 
           33 | 
           defined('MOODLE_INTERNAL') || die();
  | 
        
        
            | 
            | 
           34 | 
              | 
        
        
            | 
            | 
           35 | 
           require_once(__DIR__ . '/local/ltiadvantage/lti_advantage_testcase.php');
  | 
        
        
            | 
            | 
           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 | 
            */
  | 
        
        
           | 1441 | 
           ariadna | 
           44 | 
           final class lib_test extends \lti_advantage_testcase {
  | 
        
        
           | 1 | 
           efrain | 
           45 | 
              | 
        
        
            | 
            | 
           46 | 
               /**
  | 
        
        
            | 
            | 
           47 | 
                * Test set up.
  | 
        
        
            | 
            | 
           48 | 
                *
  | 
        
        
            | 
            | 
           49 | 
                * This is executed before running any tests in this file.
  | 
        
        
            | 
            | 
           50 | 
                */
  | 
        
        
            | 
            | 
           51 | 
               public function setUp(): void {
  | 
        
        
           | 1441 | 
           ariadna | 
           52 | 
                   parent::setUp();
  | 
        
        
           | 1 | 
           efrain | 
           53 | 
                   $this->resetAfterTest();
  | 
        
        
            | 
            | 
           54 | 
                   $this->setAdminUser();
  | 
        
        
            | 
            | 
           55 | 
               }
  | 
        
        
            | 
            | 
           56 | 
              | 
        
        
            | 
            | 
           57 | 
               /**
  | 
        
        
            | 
            | 
           58 | 
                * Test for enrol_lti_plugin::delete_instance().
  | 
        
        
            | 
            | 
           59 | 
                */
  | 
        
        
           | 11 | 
           efrain | 
           60 | 
               public function test_delete_instance(): void {
  | 
        
        
           | 1 | 
           efrain | 
           61 | 
                   global $DB;
  | 
        
        
            | 
            | 
           62 | 
              | 
        
        
            | 
            | 
           63 | 
                   // Create tool enrolment instance.
  | 
        
        
            | 
            | 
           64 | 
                   $data = new \stdClass();
  | 
        
        
            | 
            | 
           65 | 
                   $data->enrolstartdate = time();
  | 
        
        
            | 
            | 
           66 | 
                   $data->secret = 'secret';
  | 
        
        
            | 
            | 
           67 | 
                   $tool = $this->getDataGenerator()->create_lti_tool($data);
  | 
        
        
            | 
            | 
           68 | 
              | 
        
        
            | 
            | 
           69 | 
                   // Create consumer and related data.
  | 
        
        
            | 
            | 
           70 | 
                   $dataconnector = new data_connector();
  | 
        
        
            | 
            | 
           71 | 
                   $consumer = new ToolConsumer('testkey', $dataconnector);
  | 
        
        
            | 
            | 
           72 | 
                   $consumer->secret = $tool->secret;
  | 
        
        
            | 
            | 
           73 | 
                   $consumer->ltiVersion = ToolProvider::LTI_VERSION1;
  | 
        
        
            | 
            | 
           74 | 
                   $consumer->name = 'TEST CONSUMER NAME';
  | 
        
        
            | 
            | 
           75 | 
                   $consumer->consumerName = 'TEST CONSUMER INSTANCE NAME';
  | 
        
        
            | 
            | 
           76 | 
                   $consumer->consumerGuid = 'TEST CONSUMER INSTANCE GUID';
  | 
        
        
            | 
            | 
           77 | 
                   $consumer->consumerVersion = 'TEST CONSUMER INFO VERSION';
  | 
        
        
            | 
            | 
           78 | 
                   $consumer->enabled = true;
  | 
        
        
            | 
            | 
           79 | 
                   $consumer->protected = true;
  | 
        
        
            | 
            | 
           80 | 
                   $consumer->save();
  | 
        
        
            | 
            | 
           81 | 
              | 
        
        
            | 
            | 
           82 | 
                   $resourcelink = ResourceLink::fromConsumer($consumer, 'testresourcelinkid');
  | 
        
        
            | 
            | 
           83 | 
                   $resourcelink->save();
  | 
        
        
            | 
            | 
           84 | 
              | 
        
        
            | 
            | 
           85 | 
                   $ltiuser = User::fromResourceLink($resourcelink, '');
  | 
        
        
            | 
            | 
           86 | 
                   $ltiuser->ltiResultSourcedId = 'testLtiResultSourcedId';
  | 
        
        
            | 
            | 
           87 | 
                   $ltiuser->ltiUserId = 'testuserid';
  | 
        
        
            | 
            | 
           88 | 
                   $ltiuser->email = 'user1@example.com';
  | 
        
        
            | 
            | 
           89 | 
                   $ltiuser->save();
  | 
        
        
            | 
            | 
           90 | 
              | 
        
        
            | 
            | 
           91 | 
                   $tp = new tool_provider($tool->id);
  | 
        
        
            | 
            | 
           92 | 
                   $tp->user = $ltiuser;
  | 
        
        
            | 
            | 
           93 | 
                   $tp->resourceLink = $resourcelink;
  | 
        
        
            | 
            | 
           94 | 
                   $tp->consumer = $consumer;
  | 
        
        
            | 
            | 
           95 | 
                   $tp->map_tool_to_consumer();
  | 
        
        
            | 
            | 
           96 | 
              | 
        
        
            | 
            | 
           97 | 
                   $mappingparams = [
  | 
        
        
            | 
            | 
           98 | 
                       'toolid' => $tool->id,
  | 
        
        
            | 
            | 
           99 | 
                       'consumerid' => $tp->consumer->getRecordId()
  | 
        
        
            | 
            | 
           100 | 
                   ];
  | 
        
        
            | 
            | 
           101 | 
              | 
        
        
            | 
            | 
           102 | 
                   // Check first that the related records exist.
  | 
        
        
            | 
            | 
           103 | 
                   $this->assertTrue($DB->record_exists('enrol_lti_tool_consumer_map', $mappingparams));
  | 
        
        
            | 
            | 
           104 | 
                   $this->assertTrue($DB->record_exists('enrol_lti_lti2_consumer', [ 'id' => $consumer->getRecordId() ]));
  | 
        
        
            | 
            | 
           105 | 
                   $this->assertTrue($DB->record_exists('enrol_lti_lti2_resource_link', [ 'id' => $resourcelink->getRecordId() ]));
  | 
        
        
            | 
            | 
           106 | 
                   $this->assertTrue($DB->record_exists('enrol_lti_lti2_user_result', [ 'id' => $ltiuser->getRecordId() ]));
  | 
        
        
            | 
            | 
           107 | 
              | 
        
        
            | 
            | 
           108 | 
                   // Perform deletion.
  | 
        
        
            | 
            | 
           109 | 
                   $enrollti = new enrol_lti_plugin();
  | 
        
        
            | 
            | 
           110 | 
                   $instance = $DB->get_record('enrol', ['id' => $tool->enrolid]);
  | 
        
        
            | 
            | 
           111 | 
                   $enrollti->delete_instance($instance);
  | 
        
        
            | 
            | 
           112 | 
              | 
        
        
            | 
            | 
           113 | 
                   // Check that the related records have been deleted.
  | 
        
        
            | 
            | 
           114 | 
                   $this->assertFalse($DB->record_exists('enrol_lti_tool_consumer_map', $mappingparams));
  | 
        
        
            | 
            | 
           115 | 
                   $this->assertFalse($DB->record_exists('enrol_lti_lti2_consumer', [ 'id' => $consumer->getRecordId() ]));
  | 
        
        
            | 
            | 
           116 | 
                   $this->assertFalse($DB->record_exists('enrol_lti_lti2_resource_link', [ 'id' => $resourcelink->getRecordId() ]));
  | 
        
        
            | 
            | 
           117 | 
                   $this->assertFalse($DB->record_exists('enrol_lti_lti2_user_result', [ 'id' => $ltiuser->getRecordId() ]));
  | 
        
        
            | 
            | 
           118 | 
              | 
        
        
            | 
            | 
           119 | 
                   // Check that the enrolled users and the tool instance has been deleted.
  | 
        
        
            | 
            | 
           120 | 
                   $this->assertFalse($DB->record_exists('enrol_lti_users', [ 'toolid' => $tool->id ]));
  | 
        
        
            | 
            | 
           121 | 
                   $this->assertFalse($DB->record_exists('enrol_lti_tools', [ 'id' => $tool->id ]));
  | 
        
        
            | 
            | 
           122 | 
                   $this->assertFalse($DB->record_exists('enrol', [ 'id' => $instance->id ]));
  | 
        
        
            | 
            | 
           123 | 
               }
  | 
        
        
            | 
            | 
           124 | 
              | 
        
        
            | 
            | 
           125 | 
               /**
  | 
        
        
            | 
            | 
           126 | 
                * Test confirming that relevant data is removed after enrol instance removal.
  | 
        
        
            | 
            | 
           127 | 
                *
  | 
        
        
            | 
            | 
           128 | 
                * @covers \enrol_lti_plugin::delete_instance
  | 
        
        
            | 
            | 
           129 | 
                */
  | 
        
        
           | 11 | 
           efrain | 
           130 | 
               public function test_delete_instance_lti_advantage(): void {
  | 
        
        
           | 1 | 
           efrain | 
           131 | 
                   global $DB;
  | 
        
        
            | 
            | 
           132 | 
                   // Setup.
  | 
        
        
            | 
            | 
           133 | 
                   [
  | 
        
        
            | 
            | 
           134 | 
                       $course,
  | 
        
        
            | 
            | 
           135 | 
                       $modresource,
  | 
        
        
            | 
            | 
           136 | 
                       $modresource2,
  | 
        
        
            | 
            | 
           137 | 
                       $courseresource,
  | 
        
        
            | 
            | 
           138 | 
                       $registration,
  | 
        
        
            | 
            | 
           139 | 
                       $deployment
  | 
        
        
            | 
            | 
           140 | 
                   ] = $this->create_test_environment();
  | 
        
        
            | 
            | 
           141 | 
              | 
        
        
            | 
            | 
           142 | 
                   // Launch the tool.
  | 
        
        
            | 
            | 
           143 | 
                   $mockuser = $this->get_mock_launch_users_with_ids(['1p3_1'])[0];
  | 
        
        
            | 
            | 
           144 | 
                   $mocklaunch = $this->get_mock_launch($modresource, $mockuser);
  | 
        
        
            | 
            | 
           145 | 
                   $instructoruser = $this->getDataGenerator()->create_user();
  | 
        
        
            | 
            | 
           146 | 
                   $launchservice = $this->get_tool_launch_service();
  | 
        
        
            | 
            | 
           147 | 
                   $launchservice->user_launches_tool($instructoruser, $mocklaunch);
  | 
        
        
            | 
            | 
           148 | 
              | 
        
        
            | 
            | 
           149 | 
                   // Verify data exists.
  | 
        
        
            | 
            | 
           150 | 
                   $this->assertEquals(1, $DB->count_records('enrol_lti_user_resource_link'));
  | 
        
        
            | 
            | 
           151 | 
                   $this->assertEquals(1, $DB->count_records('enrol_lti_resource_link'));
  | 
        
        
            | 
            | 
           152 | 
                   $this->assertEquals(1, $DB->count_records('enrol_lti_app_registration'));
  | 
        
        
            | 
            | 
           153 | 
                   $this->assertEquals(1, $DB->count_records('enrol_lti_deployment'));
  | 
        
        
            | 
            | 
           154 | 
                   $this->assertEquals(1, $DB->count_records('enrol_lti_context'));
  | 
        
        
            | 
            | 
           155 | 
                   $this->assertEquals(1, $DB->count_records('enrol_lti_users'));
  | 
        
        
            | 
            | 
           156 | 
              | 
        
        
            | 
            | 
           157 | 
                   // Now delete the enrol instance.
  | 
        
        
            | 
            | 
           158 | 
                   $enrollti = new enrol_lti_plugin();
  | 
        
        
            | 
            | 
           159 | 
                   $instance = $DB->get_record('enrol', ['id' => $modresource->enrolid]);
  | 
        
        
            | 
            | 
           160 | 
                   $enrollti->delete_instance($instance);
  | 
        
        
            | 
            | 
           161 | 
              | 
        
        
            | 
            | 
           162 | 
                   $this->assertEquals(0, $DB->count_records('enrol_lti_user_resource_link'));
  | 
        
        
            | 
            | 
           163 | 
                   $this->assertEquals(0, $DB->count_records('enrol_lti_resource_link'));
  | 
        
        
            | 
            | 
           164 | 
                   $this->assertEquals(0, $DB->count_records('enrol_lti_users'));
  | 
        
        
            | 
            | 
           165 | 
              | 
        
        
            | 
            | 
           166 | 
                   // App registration, Deployment and Context tables are not affected by instance removal.
  | 
        
        
            | 
            | 
           167 | 
                   $this->assertEquals(1, $DB->count_records('enrol_lti_app_registration'));
  | 
        
        
            | 
            | 
           168 | 
                   $this->assertEquals(1, $DB->count_records('enrol_lti_deployment'));
  | 
        
        
            | 
            | 
           169 | 
                   $this->assertEquals(1, $DB->count_records('enrol_lti_context'));
  | 
        
        
            | 
            | 
           170 | 
               }
  | 
        
        
            | 
            | 
           171 | 
              | 
        
        
            | 
            | 
           172 | 
               /**
  | 
        
        
            | 
            | 
           173 | 
                * Test for getting user enrolment actions.
  | 
        
        
            | 
            | 
           174 | 
                */
  | 
        
        
           | 11 | 
           efrain | 
           175 | 
               public function test_get_user_enrolment_actions(): void {
  | 
        
        
           | 1 | 
           efrain | 
           176 | 
                   global $CFG, $DB, $PAGE;
  | 
        
        
            | 
            | 
           177 | 
                   $this->resetAfterTest();
  | 
        
        
            | 
            | 
           178 | 
              | 
        
        
            | 
            | 
           179 | 
                   // Set page URL to prevent debugging messages.
  | 
        
        
            | 
            | 
           180 | 
                   $PAGE->set_url('/enrol/editinstance.php');
  | 
        
        
            | 
            | 
           181 | 
              | 
        
        
            | 
            | 
           182 | 
                   $pluginname = 'lti';
  | 
        
        
            | 
            | 
           183 | 
              | 
        
        
            | 
            | 
           184 | 
                   // Only enable the lti enrol plugin.
  | 
        
        
            | 
            | 
           185 | 
                   $CFG->enrol_plugins_enabled = $pluginname;
  | 
        
        
            | 
            | 
           186 | 
              | 
        
        
            | 
            | 
           187 | 
                   $generator = $this->getDataGenerator();
  | 
        
        
            | 
            | 
           188 | 
              | 
        
        
            | 
            | 
           189 | 
                   // Get the enrol plugin.
  | 
        
        
            | 
            | 
           190 | 
                   $plugin = enrol_get_plugin($pluginname);
  | 
        
        
            | 
            | 
           191 | 
              | 
        
        
            | 
            | 
           192 | 
                   // Create a course.
  | 
        
        
            | 
            | 
           193 | 
                   $course = $generator->create_course();
  | 
        
        
            | 
            | 
           194 | 
                   $context = \context_course::instance($course->id);
  | 
        
        
            | 
            | 
           195 | 
                   $teacherroleid = $DB->get_field('role', 'id', ['shortname' => 'editingteacher'], MUST_EXIST);
  | 
        
        
            | 
            | 
           196 | 
                   $studentroleid = $DB->get_field('role', 'id', ['shortname' => 'student'], MUST_EXIST);
  | 
        
        
            | 
            | 
           197 | 
              | 
        
        
            | 
            | 
           198 | 
                   // Enable this enrol plugin for the course.
  | 
        
        
            | 
            | 
           199 | 
                   $fields = ['contextid' => $context->id, 'roleinstructor' => $teacherroleid, 'rolelearner' => $studentroleid];
  | 
        
        
            | 
            | 
           200 | 
                   $plugin->add_instance($course, $fields);
  | 
        
        
            | 
            | 
           201 | 
              | 
        
        
            | 
            | 
           202 | 
                   // Create a student.
  | 
        
        
            | 
            | 
           203 | 
                   $student = $generator->create_user();
  | 
        
        
            | 
            | 
           204 | 
                   // Enrol the student to the course.
  | 
        
        
            | 
            | 
           205 | 
                   $generator->enrol_user($student->id, $course->id, 'student', $pluginname);
  | 
        
        
            | 
            | 
           206 | 
              | 
        
        
            | 
            | 
           207 | 
                   // Teachers don't have enrol/lti:unenrol capability by default. Login as admin for simplicity.
  | 
        
        
            | 
            | 
           208 | 
                   $this->setAdminUser();
  | 
        
        
            | 
            | 
           209 | 
              | 
        
        
            | 
            | 
           210 | 
                   require_once($CFG->dirroot . '/enrol/locallib.php');
  | 
        
        
            | 
            | 
           211 | 
                   $manager = new course_enrolment_manager($PAGE, $course);
  | 
        
        
            | 
            | 
           212 | 
                   $userenrolments = $manager->get_user_enrolments($student->id);
  | 
        
        
            | 
            | 
           213 | 
                   $this->assertCount(1, $userenrolments);
  | 
        
        
            | 
            | 
           214 | 
              | 
        
        
            | 
            | 
           215 | 
                   $ue = reset($userenrolments);
  | 
        
        
            | 
            | 
           216 | 
                   $actions = $plugin->get_user_enrolment_actions($manager, $ue);
  | 
        
        
            | 
            | 
           217 | 
                   // LTI enrolment has 1 enrol actions for active users -- unenrol.
  | 
        
        
            | 
            | 
           218 | 
                   $this->assertCount(1, $actions);
  | 
        
        
            | 
            | 
           219 | 
               }
  | 
        
        
            | 
            | 
           220 | 
              | 
        
        
            | 
            | 
           221 | 
               /**
  | 
        
        
            | 
            | 
           222 | 
                * Test the behaviour of an enrolment method when the activity to which it provides access is deleted.
  | 
        
        
            | 
            | 
           223 | 
                *
  | 
        
        
            | 
            | 
           224 | 
                * @covers \enrol_lti_pre_course_module_delete
  | 
        
        
            | 
            | 
           225 | 
                */
  | 
        
        
           | 11 | 
           efrain | 
           226 | 
               public function test_course_module_deletion(): void {
  | 
        
        
           | 1 | 
           efrain | 
           227 | 
                   // Create two modules and publish them.
  | 
        
        
            | 
            | 
           228 | 
                   $course = $this->getDataGenerator()->create_course();
  | 
        
        
            | 
            | 
           229 | 
                   $mod = $this->getDataGenerator()->create_module('assign', ['course' => $course->id]);
  | 
        
        
            | 
            | 
           230 | 
                   $mod2 = $this->getDataGenerator()->create_module('assign', ['course' => $course->id]);
  | 
        
        
            | 
            | 
           231 | 
                   $tooldata = [
  | 
        
        
            | 
            | 
           232 | 
                       'cmid' => $mod->cmid,
  | 
        
        
            | 
            | 
           233 | 
                       'courseid' => $course->id,
  | 
        
        
            | 
            | 
           234 | 
                   ];
  | 
        
        
            | 
            | 
           235 | 
                   $tool = $this->getDataGenerator()->create_lti_tool((object)$tooldata);
  | 
        
        
            | 
            | 
           236 | 
                   $tooldata['cmid'] = $mod2->cmid;
  | 
        
        
            | 
            | 
           237 | 
                   $tool2 = $this->getDataGenerator()->create_lti_tool((object)$tooldata);
  | 
        
        
            | 
            | 
           238 | 
              | 
        
        
            | 
            | 
           239 | 
                   // Verify the instances are both enabled.
  | 
        
        
            | 
            | 
           240 | 
                   $modinstance = helper::get_lti_tool($tool->id);
  | 
        
        
            | 
            | 
           241 | 
                   $mod2instance = helper::get_lti_tool($tool2->id);
  | 
        
        
            | 
            | 
           242 | 
                   $this->assertEquals(ENROL_INSTANCE_ENABLED, $modinstance->status);
  | 
        
        
            | 
            | 
           243 | 
                   $this->assertEquals(ENROL_INSTANCE_ENABLED, $mod2instance->status);
  | 
        
        
            | 
            | 
           244 | 
              | 
        
        
            | 
            | 
           245 | 
                   // Delete a module and verify the associated instance is disabled.
  | 
        
        
            | 
            | 
           246 | 
                   course_delete_module($mod->cmid);
  | 
        
        
            | 
            | 
           247 | 
                   $modinstance = helper::get_lti_tool($tool->id);
  | 
        
        
            | 
            | 
           248 | 
                   $mod2instance = helper::get_lti_tool($tool2->id);
  | 
        
        
            | 
            | 
           249 | 
                   $this->assertEquals(ENROL_INSTANCE_DISABLED, $modinstance->status);
  | 
        
        
            | 
            | 
           250 | 
                   $this->assertEquals(ENROL_INSTANCE_ENABLED, $mod2instance->status);
  | 
        
        
            | 
            | 
           251 | 
               }
  | 
        
        
            | 
            | 
           252 | 
           }
  |