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\entity;
  18  
  19  /**
  20   * Tests for application_registration.
  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\application_registration
  26   */
  27  class application_registration_test extends \advanced_testcase {
  28  
  29      /**
  30       * Test the creation of an application_registration instance.
  31       *
  32       * @dataProvider creation_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_creation(array $args, array $expectations) {
  38          if ($expectations['valid']) {
  39              $reg = application_registration::create(...array_values($args));
  40              $this->assertEquals($args['name'], $reg->get_name());
  41              $this->assertEquals($args['uniqueid'], $reg->get_uniqueid());
  42              $this->assertEquals($args['platformid'], $reg->get_platformid());
  43              $this->assertEquals($args['clientid'], $reg->get_clientid());
  44              $this->assertEquals($args['authrequesturl'], $reg->get_authenticationrequesturl());
  45              $this->assertEquals($args['jwksurl'], $reg->get_jwksurl());
  46              $this->assertEquals($args['accesstokenurl'], $reg->get_accesstokenurl());
  47              $expectedid = $args['id'] ?? null;
  48              $this->assertEquals($expectedid, $reg->get_id());
  49              $this->assertTrue($reg->is_complete());
  50          } else {
  51              $this->expectException($expectations['exception']);
  52              $this->expectExceptionMessage($expectations['exceptionmessage']);
  53              application_registration::create(...array_values($args));
  54          }
  55      }
  56  
  57      /**
  58       * Data provider for testing the creation of application_registration instances.
  59       *
  60       * @return array the data for testing.
  61       */
  62      public function creation_data_provider(): array {
  63          return [
  64              'Valid, only required args provided' => [
  65                  'args' => [
  66                      'name' => 'Platform X',
  67                      'uniqueid' => 'a2c94a2c94',
  68                      'platformid' => new \moodle_url('https://lms.example.com'),
  69                      'clientid' => 'client-id-12345',
  70                      'authrequesturl' => new \moodle_url('https://lms.example.com/auth'),
  71                      'jwksurl' => new \moodle_url('https://lms.example.com/jwks'),
  72                      'accesstokenurl' => new \moodle_url('https://lms.example.com/token'),
  73                  ],
  74                  'expectations' => [
  75                      'valid' => true
  76                  ]
  77              ],
  78              'Valid, all args provided' => [
  79                  'args' => [
  80                      'name' => 'Platform X',
  81                      'uniqueid' => 'a2c94a2c94',
  82                      'platformid' => new \moodle_url('https://lms.example.com'),
  83                      'clientid' => 'client-id-12345',
  84                      'authrequesturl' => new \moodle_url('https://lms.example.com/auth'),
  85                      'jwksurl' => new \moodle_url('https://lms.example.com/jwks'),
  86                      'accesstokenurl' => new \moodle_url('https://lms.example.com/token'),
  87                      'id' => 24
  88                  ],
  89                  'expectations' => [
  90                      'valid' => true
  91                  ]
  92              ],
  93              'Invalid, empty name provided' => [
  94                  'args' => [
  95                      'name' => '',
  96                      'uniqueid' => 'a2c94a2c94',
  97                      'platformid' => new \moodle_url('https://lms.example.com'),
  98                      'clientid' => 'client-id-12345',
  99                      'authrequesturl' => new \moodle_url('https://lms.example.com/auth'),
 100                      'jwksurl' => new \moodle_url('https://lms.example.com/jwks'),
 101                      'accesstokenurl' => new \moodle_url('https://lms.example.com/token'),
 102                  ],
 103                  'expectations' => [
 104                      'valid' => false,
 105                      'exception' => \coding_exception::class,
 106                      'exceptionmessage' => "Invalid 'name' arg. Cannot be an empty string."
 107                  ]
 108              ],
 109              'Invalid, empty uniqueid provided' => [
 110                  'args' => [
 111                      'name' => 'Platform X',
 112                      'uniqueid' => '',
 113                      'platformid' => new \moodle_url('https://lms.example.com'),
 114                      'clientid' => 'client-id-12345',
 115                      'authrequesturl' => new \moodle_url('https://lms.example.com/auth'),
 116                      'jwksurl' => new \moodle_url('https://lms.example.com/jwks'),
 117                      'accesstokenurl' => new \moodle_url('https://lms.example.com/token'),
 118                  ],
 119                  'expectations' => [
 120                      'valid' => false,
 121                      'exception' => \coding_exception::class,
 122                      'exceptionmessage' => "Invalid 'uniqueid' arg. Cannot be an empty string."
 123                  ]
 124              ],
 125              'Invalid, empty clientid provided' => [
 126                  'args' => [
 127                      'name' => 'Platform X',
 128                      'uniqueid' => 'a2c94a2c94',
 129                      'platformid' => new \moodle_url('https://lms.example.com'),
 130                      'clientid' => '',
 131                      'authrequesturl' => new \moodle_url('https://lms.example.com/auth'),
 132                      'jwksurl' => new \moodle_url('https://lms.example.com/jwks'),
 133                      'accesstokenurl' => new \moodle_url('https://lms.example.com/token'),
 134                  ],
 135                  'expectations' => [
 136                      'valid' => false,
 137                      'exception' => \coding_exception::class,
 138                      'exceptionmessage' => "Invalid 'clientid' arg. Cannot be an empty string."
 139                  ]
 140              ]
 141          ];
 142      }
 143  
 144      /**
 145       * Test the creation of a draft application_registration instances.
 146       *
 147       * @dataProvider create_draft_data_provider
 148       * @param array $args the arguments to the creation method.
 149       * @param array $expectations various expectations for the test cases.
 150       * @covers ::create_draft
 151       */
 152      public function test_create_draft(array $args, array $expectations) {
 153          if ($expectations['valid']) {
 154              $reg = application_registration::create_draft(...array_values($args));
 155              $this->assertEquals($args['name'], $reg->get_name());
 156              $this->assertEquals($args['uniqueid'], $reg->get_uniqueid());
 157              $this->assertNull($reg->get_platformid());
 158              $this->assertNull($reg->get_clientid());
 159              $this->assertNull($reg->get_authenticationrequesturl());
 160              $this->assertNull($reg->get_jwksurl());
 161              $this->assertNull($reg->get_accesstokenurl());
 162              $expectedid = $args['id'] ?? null;
 163              $this->assertEquals($expectedid, $reg->get_id());
 164              $this->assertFalse($reg->is_complete());
 165          } else {
 166              $this->expectException($expectations['exception']);
 167              $this->expectExceptionMessage($expectations['exceptionmessage']);
 168              application_registration::create_draft(...array_values($args));
 169          }
 170      }
 171  
 172      /**
 173       * Test that complete registration can transition a pending registration in the correct state to a complete registration.
 174       *
 175       * @covers ::complete_registration
 176       */
 177      public function test_complete_registration() {
 178          // Create a draft registration which should initially be incomplete.
 179          $draft = application_registration::create_draft('Test platform name', '1234bfda');
 180          $this->assertFalse($draft->is_complete());
 181  
 182          // Add information to the draft, but don't complete it. It should still be incomplete.
 183          $draft->set_name('Adjusted platform name');
 184          $draft->set_platformid(new \moodle_url('https://lms.example.com'));
 185          $draft->set_clientid('bcgd23');
 186          $draft->set_authenticationrequesturl(new \moodle_url('https://lms.example.com/auth'));
 187          $draft->set_jwksurl(new \moodle_url('https://lms.example.com/jwks'));
 188          $draft->set_accesstokenurl(new \moodle_url('https://lms.example.com/token'));
 189          $this->assertFalse($draft->is_complete());
 190  
 191          // Complete the registration. It will now be indicated as complete.
 192          $draft->complete_registration();
 193          $this->assertTrue($draft->is_complete());
 194  
 195          // Now, create another draft registration and try to mark it complete. It should throw.
 196          $draft2 = application_registration::create_draft('Another platform', '3434bfafas');
 197          $this->expectException(\coding_exception::class);
 198          $draft2->complete_registration();
 199      }
 200  
 201      /**
 202       * Data provider for testing draft creation.
 203       *
 204       * @return array the test case data.
 205       */
 206      public function create_draft_data_provider(): array {
 207          return [
 208              'Valid, new draft' => [
 209                  'args' => [
 210                      'name' => 'Platform X',
 211                      'uniqueid' => 'a2c94a2c94',
 212                  ],
 213                  'expectations' => [
 214                      'valid' => true
 215                  ]
 216              ],
 217              'Valid, existing draft' => [
 218                  'args' => [
 219                      'name' => 'Platform X',
 220                      'uniqueid' => 'a2c94a2c94',
 221                      'id' => 24
 222                  ],
 223                  'expectations' => [
 224                      'valid' => true
 225                  ]
 226              ],
 227              'Invalid, empty identifier' => [
 228                  'args' => [
 229                      'name' => 'Platform X',
 230                      'uniqueid' => '',
 231                  ],
 232                  'expectations' => [
 233                      'valid' => false,
 234                      'exception' => \coding_exception::class,
 235                      'exceptionmessage' => "Invalid 'uniqueid' arg. Cannot be an empty string."
 236                  ]
 237              ]
 238          ];
 239      }
 240  
 241      /**
 242       * Test the factory method for creating a tool deployment associated with the registration instance.
 243       *
 244       * @dataProvider add_tool_deployment_data_provider
 245       * @param array $args the arguments to the creation method.
 246       * @param array $expectations various expectations for the test cases.
 247       * @covers ::add_tool_deployment
 248       */
 249      public function test_add_tool_deployment(array $args, array $expectations) {
 250  
 251          if ($expectations['valid']) {
 252              $reg = application_registration::create(...array_values($args['registration']));
 253              $deployment = $reg->add_tool_deployment(...array_values($args['deployment']));
 254              $this->assertInstanceOf(deployment::class, $deployment);
 255              $this->assertEquals($args['deployment']['name'], $deployment->get_deploymentname());
 256              $this->assertEquals($args['deployment']['deploymentid'], $deployment->get_deploymentid());
 257              $this->assertEquals($reg->get_id(), $deployment->get_registrationid());
 258          } else {
 259              $reg = application_registration::create(...array_values($args['registration']));
 260              $this->expectException($expectations['exception']);
 261              $this->expectExceptionMessage($expectations['exceptionmessage']);
 262              $reg->add_tool_deployment(...array_values($args['deployment']));
 263          }
 264      }
 265  
 266      /**
 267       * Data provider for testing the add_tool_deployment method.
 268       *
 269       * @return array the array of test data.
 270       */
 271      public function add_tool_deployment_data_provider(): array {
 272          return [
 273              'Valid, contains id on registration and valid deployment data provided' => [
 274                  'args' => [
 275                      'registration' => [
 276                          'name' => 'Platform X',
 277                          'uniqueid' => 'a2c94a2c94',
 278                          'platformid' => new \moodle_url('https://lms.example.com'),
 279                          'clientid' => 'client-id-12345',
 280                          'authrequesturl' => new \moodle_url('https://lms.example.com/auth'),
 281                          'jwksurl' => new \moodle_url('https://lms.example.com/jwks'),
 282                          'accesstokenurl' => new \moodle_url('https://lms.example.com/token'),
 283                          'id' => 24
 284                      ],
 285                      'deployment' => [
 286                          'name' => 'Deployment at site level',
 287                          'deploymentid' => 'id-123abc'
 288                      ]
 289                  ],
 290                  'expectations' => [
 291                      'valid' => true,
 292                  ]
 293              ],
 294              'Invalid, missing id on registration' => [
 295                  'args' => [
 296                      'registration' => [
 297                          'name' => 'Platform X',
 298                          'uniqueid' => 'a2c94a2c94',
 299                          'platformid' => new \moodle_url('https://lms.example.com'),
 300                          'clientid' => 'client-id-12345',
 301                          'authrequesturl' => new \moodle_url('https://lms.example.com/auth'),
 302                          'jwksurl' => new \moodle_url('https://lms.example.com/jwks'),
 303                          'accesstokenurl' => new \moodle_url('https://lms.example.com/token'),
 304                      ],
 305                      'deployment' => [
 306                          'name' => 'Deployment at site level',
 307                          'deploymentid' => 'id-123abc'
 308                      ]
 309                  ],
 310                  'expectations' => [
 311                      'valid' => false,
 312                      'exception' => \coding_exception::class,
 313                      'exceptionmessage' => "Can't add deployment to a resource_link that hasn't first been saved."
 314                  ]
 315              ],
 316              'Invalid, id present on registration but empty deploymentname' => [
 317                  'args' => [
 318                      'registration' => [
 319                          'name' => 'Platform X',
 320                          'uniqueid' => 'a2c94a2c94',
 321                          'platformid' => new \moodle_url('https://lms.example.com'),
 322                          'clientid' => 'client-id-12345',
 323                          'authrequesturl' => new \moodle_url('https://lms.example.com/auth'),
 324                          'jwksurl' => new \moodle_url('https://lms.example.com/jwks'),
 325                          'accesstokenurl' => new \moodle_url('https://lms.example.com/token'),
 326                          'id' => 24
 327                      ],
 328                      'deployment' => [
 329                          'name' => '',
 330                          'deploymentid' => 'id-123abc'
 331                      ]
 332                  ],
 333                  'expectations' => [
 334                      'valid' => false,
 335                      'exception' => \coding_exception::class,
 336                      'exceptionmessage' => "Invalid 'deploymentname' arg. Cannot be an empty string."
 337                  ]
 338              ],
 339              'Invalid, id present on registration but empty deploymentid' => [
 340                  'args' => [
 341                      'registration' => [
 342                          'name' => 'Platform X',
 343                          'uniqueid' => 'a2c94a2c94',
 344                          'platformid' => new \moodle_url('https://lms.example.com'),
 345                          'clientid' => 'client-id-12345',
 346                          'authrequesturl' => new \moodle_url('https://lms.example.com/auth'),
 347                          'jwksurl' => new \moodle_url('https://lms.example.com/jwks'),
 348                          'accesstokenurl' => new \moodle_url('https://lms.example.com/token'),
 349                          'id' => 24
 350                      ],
 351                      'deployment' => [
 352                          'name' => 'Site deployment',
 353                          'deploymentid' => ''
 354                      ]
 355                  ],
 356                  'expectations' => [
 357                      'valid' => false,
 358                      'exception' => \coding_exception::class,
 359                      'exceptionmessage' => "Invalid 'deploymentid' arg. Cannot be an empty string."
 360                  ]
 361              ]
 362          ];
 363      }
 364  }