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  namespace enrol_lti\local\ltiadvantage\entity;
  18  
  19  /**
  20   * Tests for nrps_info.
  21   *
  22   * @package enrol_lti
  23   * @copyright 2021 Jake Dallimore <jrhdallimore@gmail.com>
  24   * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  25   * @coversDefaultClass \enrol_lti\local\ltiadvantage\entity\nrps_info
  26   */
  27  class nrps_info_test extends \advanced_testcase {
  28  
  29      /**
  30       * Test creation of the object instances.
  31       *
  32       * @dataProvider instantiation_data_provider
  33       * @param array $args the arguments to the creation method.
  34       * @param array $expectations various expectations for the test cases.
  35       * @covers ::create
  36       */
  37      public function test_create(array $args, array $expectations) {
  38          if (!$expectations['valid']) {
  39              $this->expectException($expectations['exception']);
  40              $this->expectExceptionMessage($expectations['exceptionmessage']);
  41              nrps_info::create(...array_values($args));
  42          } else {
  43              $nrpsinfo = nrps_info::create(...array_values($args));
  44              $this->assertEquals($args['contextmembershipsurl'], $nrpsinfo->get_context_memberships_url());
  45              $this->assertEquals($expectations['serviceversions'], $nrpsinfo->get_service_versions());
  46              $this->assertEquals('https://purl.imsglobal.org/spec/lti-nrps/scope/contextmembership.readonly',
  47                  $nrpsinfo->get_service_scope());
  48          }
  49      }
  50  
  51      /**
  52       * Data provider for testing object instantiation.
  53       * @return array the data for testing.
  54       */
  55      public function instantiation_data_provider(): array {
  56          return [
  57              'Valid creation' => [
  58                  'args' => [
  59                      'contextmembershipsurl' => new \moodle_url('https://lms.example.com/45/memberships'),
  60                      'serviceversions' => ['1.0', '2.0'],
  61                  ],
  62                  'expectations' => [
  63                      'valid' => true,
  64                      'serviceversions' => ['1.0', '2.0']
  65                  ]
  66              ],
  67              'Missing service version' => [
  68                  'args' => [
  69                      'contextmembershipsurl' => new \moodle_url('https://lms.example.com/45/memberships'),
  70                      'serviceversions' => [],
  71                  ],
  72                  'expectations' => [
  73                      'valid' => false,
  74                      'exception' => \coding_exception::class,
  75                      'exceptionmessage' => 'Service versions array cannot be empty'
  76                  ]
  77              ],
  78              'Invalid service version' => [
  79                  'args' => [
  80                      'contextmembershipsurl' => new \moodle_url('https://lms.example.com/45/memberships'),
  81                      'serviceversions' => ['1.1'],
  82                  ],
  83                  'expectations' => [
  84                      'valid' => false,
  85                      'exception' => \coding_exception::class,
  86                      'exceptionmessage' => "Invalid Names and Roles service version '1.1'"
  87                  ]
  88              ],
  89              'Duplicate service version' => [
  90                  'args' => [
  91                      'contextmembershipsurl' => new \moodle_url('https://lms.example.com/45/memberships'),
  92                      'serviceversions' => ['1.0', '1.0'],
  93                  ],
  94                  'expectations' => [
  95                      'valid' => true,
  96                      'serviceversions' => ['1.0']
  97                  ]
  98              ]
  99          ];
 100      }
 101  }