Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.0.x will end 8 May 2023 (12 months).
  • Bug fixes for security issues in 4.0.x will end 13 November 2023 (18 months).
  • PHP version: minimum PHP 7.3.0 Note: the minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is also supported.
   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  use enrol_lti\local\ltiadvantage\entity\application_registration;
  18  use enrol_lti\local\ltiadvantage\repository\application_registration_repository;
  19  use enrol_lti\local\ltiadvantage\repository\deployment_repository;
  20  
  21  /**
  22   * LTI Enrolment test data generator class.
  23   *
  24   * @package enrol_lti
  25   * @category test
  26   * @copyright 2021 Jake Dallimore <jrhdallimore@gmail.com>
  27   * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  28   */
  29  class enrol_lti_generator extends component_generator_base {
  30  
  31      /**
  32       * Test method to generate an application registration (and optionally a deployment) for a platform.
  33       *
  34       * @param array $data the application registration data, with optional deployment data.
  35       * @return application_registration
  36       */
  37      public function create_application_registration(array $data): application_registration {
  38          $bytes = random_bytes(30);
  39          $uniqueid = bin2hex($bytes);
  40          if (empty($data['platformid']) || empty($data['clientid']) || empty($data['authrequesturl']) || empty($data['jwksurl']) ||
  41                  empty($data['accesstokenurl'])) {
  42              $registration = application_registration::create_draft(
  43                  $data['name'],
  44                  $uniqueid
  45              );
  46          } else {
  47              $registration = application_registration::create(
  48                  $data['name'],
  49                  $uniqueid,
  50                  new moodle_url($data['platformid']),
  51                  $data['clientid'],
  52                  new moodle_url($data['authrequesturl']),
  53                  new moodle_url($data['jwksurl']),
  54                  new moodle_url($data['accesstokenurl'])
  55              );
  56          }
  57  
  58          $appregrepo = new application_registration_repository();
  59          $createdregistration = $appregrepo->save($registration);
  60  
  61          if (isset($data['deploymentname']) && isset($data['deploymentid'])) {
  62              $deployment = $createdregistration->add_tool_deployment($data['deploymentname'], $data['deploymentid']);
  63              $deploymentrepo = new deployment_repository();
  64              $deploymentrepo->save($deployment);
  65          }
  66  
  67          return $createdregistration;
  68      }
  69  
  70      /**
  71       * Test method to generate a published resource for a course.
  72       *
  73       * @param array $data the data required to publish the resource.
  74       * @return stdClass the enrol_lti_tools record, representing the published resource.
  75       */
  76      public function create_published_resource(array $data): stdClass {
  77  
  78          if (!empty($data['ltiversion']) && !in_array($data['ltiversion'], ['LTI-1p3', 'LTI-1p0/LTI-2p0'])) {
  79              throw new coding_exception("The field 'ltiversion' must be either 'LTI-1p3' or 'LTI-1p0/LTI-2p0'.");
  80          }
  81  
  82          $instancedata = (object) [
  83              'name' => $data['name'],
  84              'courseid' => $data['courseid'],
  85              'cmid' => $data['activityid'],
  86              'ltiversion' => $data['ltiversion'] ?? 'LTI-1p3'
  87          ];
  88          $tool = $this->datagenerator->create_lti_tool($instancedata);
  89  
  90          if (empty($data['uuid'])) {
  91              return $tool;
  92          }
  93  
  94          // Allow tests to create predictable uuids.
  95          global $DB;
  96          $DB->set_field('enrol_lti_tools', 'uuid', $data['uuid']);
  97          return enrol_lti\helper::get_lti_tool($tool->id);
  98      }
  99  }