Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 3.9.x will end* 10 May 2021 (12 months).
  • Bug fixes for security issues in 3.9.x will end* 8 May 2023 (36 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

Differences Between: [Versions 39 and 310] [Versions 39 and 311] [Versions 39 and 400] [Versions 39 and 401] [Versions 39 and 402] [Versions 39 and 403]

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