Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.
   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  namespace enrol_lti\local\ltiadvantage\lib;
  18  
  19  use enrol_lti\local\ltiadvantage\entity\application_registration;
  20  use enrol_lti\local\ltiadvantage\repository\application_registration_repository;
  21  use enrol_lti\local\ltiadvantage\repository\deployment_repository;
  22  use Packback\Lti1p3\LtiDeployment;
  23  use Packback\Lti1p3\LtiRegistration;
  24  
  25  /**
  26   * Tests for the issuer_database class.
  27   *
  28   * @package enrol_lti
  29   * @copyright 2021 Jake Dallimore <jrhdallimore@gmail.com>
  30   * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  31   * @coversDefaultClass \enrol_lti\local\ltiadvantage\lib\issuer_database
  32   */
  33  class issuer_database_test extends \advanced_testcase {
  34  
  35      /**
  36       * Test the Moodle implementation of the library database method test_find_registration_by_issuer().
  37       *
  38       * @covers ::findRegistrationByIssuer
  39       */
  40      public function test_find_registration_by_issuer() {
  41          $this->resetAfterTest();
  42          $appregrepo = new application_registration_repository();
  43          $appreg = application_registration::create(
  44              'My platform',
  45              'a2c94a2c94',
  46              new \moodle_url('https://lms.example.com'),
  47              'client-id-123',
  48              new \moodle_url('https://lms.example.com/lti/auth'),
  49              new \moodle_url('https://lms.example.com/lti/jwks'),
  50              new \moodle_url('https://lms.example.com/lti/token')
  51          );
  52          $appregrepo->save($appreg);
  53  
  54          $issuerdb = new issuer_database($appregrepo, new deployment_repository());
  55          $registration = $issuerdb->findRegistrationByIssuer('https://lms.example.com', 'client-id-123');
  56          $this->assertInstanceOf(LtiRegistration::class, $registration);
  57          $this->assertEquals($appreg->get_authenticationrequesturl()->out(false), $registration->getAuthLoginUrl());
  58          $this->assertEquals($appreg->get_jwksurl()->out(false), $registration->getKeySetUrl());
  59          $this->assertEquals($appreg->get_accesstokenurl()->out(false), $registration->getAuthTokenUrl());
  60          $this->assertEquals($appreg->get_clientid(), $registration->getClientId());
  61          $this->assertEquals($appreg->get_platformid()->out(false), $registration->getIssuer());
  62  
  63          $this->assertNull($issuerdb->findRegistrationByIssuer('https://lms.example.com', 'client-id-456'));
  64  
  65          $this->expectException(\coding_exception::class);
  66          $this->expectExceptionMessageMatches('/The param \'clientid\' is required. /');
  67          $issuerdb->findRegistrationByIssuer('https://lms.example.com');
  68      }
  69  
  70      /**
  71       * Test the Moodle implementation of the library database method test_find_deployment().
  72       *
  73       * @covers ::findDeployment
  74       */
  75      public function test_find_deployment() {
  76          $this->resetAfterTest();
  77          $appregrepo = new application_registration_repository();
  78          $appreg = application_registration::create(
  79              'My platform',
  80              'a2c94a2c94',
  81              new \moodle_url('https://lms.example.com'),
  82              'client-id-123',
  83              new \moodle_url('https://lms.example.com/lti/auth'),
  84              new \moodle_url('https://lms.example.com/lti/jwks'),
  85              new \moodle_url('https://lms.example.com/lti/token')
  86          );
  87          $appreg = $appregrepo->save($appreg);
  88          $dep = $appreg->add_tool_deployment('Site wide tool deployment', 'deployment-id-1');
  89          $deploymentrepo = new deployment_repository();
  90          $deploymentrepo->save($dep);
  91  
  92          $issuerdb = new issuer_database($appregrepo, new deployment_repository());
  93          $deployment = $issuerdb->findDeployment('https://lms.example.com', 'deployment-id-1', 'client-id-123');
  94          $this->assertInstanceOf(LtiDeployment::class, $deployment);
  95          $this->assertEquals($dep->get_deploymentid(), $deployment->getDeploymentId());
  96  
  97          $this->assertNull($issuerdb->findDeployment('https://lms.example.com', 'deployment-id-1', 'client-id-456'));
  98          $this->assertNull($issuerdb->findDeployment('https://lms.example.com', 'deployment-id-2', 'client-id-123'));
  99  
 100          $this->expectException(\coding_exception::class);
 101          $this->expectExceptionMessageMatches('/Both issuer and client id are required to identify platform registrations /');
 102          $issuerdb->findDeployment('https://lms.example.com', 'deployment-id-2');
 103      }
 104  }