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\service;
  18  
  19  use enrol_lti\helper;
  20  use enrol_lti\local\ltiadvantage\entity\application_registration;
  21  use enrol_lti\local\ltiadvantage\entity\deployment;
  22  use enrol_lti\local\ltiadvantage\repository\application_registration_repository;
  23  use enrol_lti\local\ltiadvantage\repository\context_repository;
  24  use enrol_lti\local\ltiadvantage\repository\user_repository;
  25  use enrol_lti\local\ltiadvantage\repository\deployment_repository;
  26  use enrol_lti\local\ltiadvantage\repository\resource_link_repository;
  27  
  28  defined('MOODLE_INTERNAL') || die();
  29  
  30  require_once (__DIR__ . '/../lti_advantage_testcase.php');
  31  
  32  /**
  33   * Tests for the tool_deployment_service.
  34   *
  35   * @package enrol_lti
  36   * @copyright 2021 Jake Dallimore <jrhdallimore@gmail.com>
  37   * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  38   * @coversDefaultClass \enrol_lti\local\ltiadvantage\service\tool_deployment_service
  39   */
  40  class tool_deployment_service_test extends \lti_advantage_testcase {
  41      /**
  42       * Return a pre-existing application_registration object for testing.
  43       *
  44       * @return application_registration
  45       */
  46      protected function generate_application_registration(): application_registration {
  47          $reg = application_registration::create(
  48              'Example LMS application',
  49              'a2c94a2c94',
  50              new \moodle_url('https://lms.example.org'),
  51              '123',
  52              new \moodle_url('https://example.org/authrequesturl'),
  53              new \moodle_url('https://example.org/jwksurl'),
  54              new \moodle_url('https://example.org/accesstokenurl')
  55          );
  56  
  57          $regrepo = new application_registration_repository();
  58          return $regrepo->save($reg);
  59      }
  60  
  61      /**
  62       * Helper to get a tool_deployment_service instance.
  63       *
  64       * @return tool_deployment_service the instance.
  65       */
  66      protected function get_tool_deployment_service(): tool_deployment_service {
  67          return new tool_deployment_service(
  68              new application_registration_repository(),
  69              new deployment_repository(),
  70              new resource_link_repository(),
  71              new context_repository(),
  72              new user_repository()
  73          );
  74      }
  75  
  76      /**
  77       * Test the use case "As an admin, I can register an application as an LTI consumer (platform)".
  78       *
  79       * @covers ::add_tool_deployment
  80       */
  81      public function test_add_tool_deployment() {
  82          $this->resetAfterTest();
  83          $testreg = $this->generate_application_registration();
  84          $deploymentrepo = new deployment_repository();
  85  
  86          $service = $this->get_tool_deployment_service();
  87          $createddeployment = $service->add_tool_deployment(
  88              (object) [
  89                  'registration_id' => $testreg->get_id(),
  90                  'deployment_id' => 'Deploy_ID_123',
  91                  'deployment_name' => "Tool deployment in location x"
  92              ]
  93          );
  94  
  95          $this->assertInstanceOf(deployment::class, $createddeployment);
  96          $this->assertTrue($deploymentrepo->exists($createddeployment->get_id()));
  97      }
  98  
  99      /**
 100       * Test trying to add a tool deployment when a registration identified by the specified id cannot be found.
 101       *
 102       * @covers ::add_tool_deployment
 103       */
 104      public function test_add_tool_deployment_registration_missing() {
 105          $this->resetAfterTest();
 106          $service = $this->get_tool_deployment_service();
 107  
 108          $this->expectException(\coding_exception::class);
 109          $this->expectExceptionMessageMatches('/Cannot add deployment to non-existent application registration/');
 110          $service->add_tool_deployment(
 111              (object) [
 112                  'registration_id' => 1234,
 113                  'deployment_id' => 'Deploy_ID_123',
 114                  'deployment_name' => "Tool deployment in location x"
 115              ]
 116          );
 117      }
 118  
 119      /**
 120       * Test that removal of a deployment removes all associated data.
 121       *
 122       * @covers ::delete_tool_deployment
 123       */
 124      public function test_delete_deployment() {
 125          $this->resetAfterTest();
 126          // Setup.
 127          $registrationrepo = new application_registration_repository();
 128          $deploymentrepo = new deployment_repository();
 129          $contextrepo = new context_repository();
 130          $resourcelinkrepo = new resource_link_repository();
 131          $userrepo = new user_repository();
 132          [$course, $resource] = $this->create_test_environment();
 133  
 134          // Launch the tool for a user.
 135          $mocklaunch = $this->get_mock_launch($resource, $this->get_mock_launch_users_with_ids(['1'])[0]);
 136          $instructoruser = $this->getDataGenerator()->create_user();
 137          $launchservice = $this->get_tool_launch_service();
 138          $launchservice->user_launches_tool($instructoruser, $mocklaunch);
 139  
 140          // Check all the expected data exists for the deployment after setup.
 141          $registrations = $registrationrepo->find_all();
 142          $this->assertCount(1, $registrations);
 143          $registration = array_pop($registrations);
 144  
 145          $deployments = $deploymentrepo->find_all_by_registration($registration->get_id());
 146          $this->assertCount(1, $deployments);
 147          $deployment = array_pop($deployments);
 148  
 149          $resourcelinks = $resourcelinkrepo->find_by_resource($resource->id);
 150          $this->assertCount(1, $resourcelinks);
 151          $resourcelink = array_pop($resourcelinks);
 152  
 153          $context = $contextrepo->find($resourcelink->get_contextid());
 154          $this->assertNotNull($context);
 155  
 156          $users = $userrepo->find_by_resource($resource->id);
 157          $this->assertCount(1, $users);
 158          $user = array_pop($users);
 159  
 160          $enrolledusers = get_enrolled_users(\context_course::instance($course->id));
 161          $this->assertCount(1, $enrolledusers);
 162  
 163          // Now delete the deployment using the service.
 164          $service = $this->get_tool_deployment_service();
 165          $service->delete_tool_deployment($deployment->get_id());
 166  
 167          // Verify that the context, resourcelink, user and deployment instances are all deleted but the registration
 168          // instance remains.
 169          $this->assertTrue($registrationrepo->exists($registration->get_id()));
 170          $this->assertFalse($deploymentrepo->exists($deployment->get_id()));
 171          $this->assertFalse($contextrepo->exists($context->get_id()));
 172          $this->assertFalse($resourcelinkrepo->exists($resourcelink->get_id()));
 173          $this->assertFalse($userrepo->exists($user->get_id()));
 174  
 175          // Verify that all users are unenrolled.
 176          $enrolledusers = get_enrolled_users(\context_course::instance($course->id));
 177          $this->assertCount(0, $enrolledusers);
 178  
 179          // Verify the tool record stays in place (I.e. the published resource is still available).
 180          $this->assertNotEmpty(helper::get_lti_tool($resource->id));
 181      }
 182  }