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 deployment.
  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\deployment
  26   */
  27  class deployment_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_creation(array $args, array $expectations) {
  38          if (!$expectations['valid']) {
  39              $this->expectException($expectations['exception']);
  40              $this->expectExceptionMessage($expectations['exceptionmessage']);
  41              deployment::create(...array_values($args));
  42          } else {
  43              $deployment = deployment::create(...array_values($args));
  44              $this->assertEquals($args['deploymentname'], $deployment->get_deploymentname());
  45              $this->assertEquals($args['deploymentid'], $deployment->get_deploymentid());
  46              $this->assertEquals($args['registrationid'], $deployment->get_registrationid());
  47              $this->assertEquals($args['legacyconsumerkey'], $deployment->get_legacy_consumer_key());
  48              $this->assertEquals($args['id'], $deployment->get_id());
  49          }
  50      }
  51  
  52      /**
  53       * Data provider for testing object instantiation.
  54       * @return array the data for testing.
  55       */
  56      public function instantiation_data_provider(): array {
  57          return [
  58              'Valid deployment creation, no id or legacy consumer key' => [
  59                  'args' => [
  60                      'registrationid' => 99,
  61                      'deploymentid' => 'deployment-abcde',
  62                      'deploymentname' => 'Global platform deployment',
  63                      'id' => null,
  64                      'legacyconsumerkey' => null,
  65                  ],
  66                  'expectations' => [
  67                      'valid' => true,
  68                  ]
  69              ],
  70              'Valid deployment creation, with id, no legacy consumer key' => [
  71                  'args' => [
  72                      'registrationid' => 99,
  73                      'deploymentid' => 'deployment-abcde',
  74                      'deploymentname' => 'Global platform deployment',
  75                      'id' => 45,
  76                      'legacyconsumerkey' => null,
  77                  ],
  78                  'expectations' => [
  79                      'valid' => true,
  80                  ]
  81              ],
  82              'Valid deployment creation, with id and legacy consumer key' => [
  83                  'args' => [
  84                      'registrationid' => 99,
  85                      'deploymentid' => 'deployment-abcde',
  86                      'deploymentname' => 'Global platform deployment',
  87                      'id' => 45,
  88                      'legacyconsumerkey' => '1bcb23dfff',
  89                  ],
  90                  'expectations' => [
  91                      'valid' => true,
  92                  ]
  93              ],
  94              'Invalid deployment creation, invalid id' => [
  95                  'args' => [
  96                      'registrationid' => 99,
  97                      'deploymentid' => 'deployment-abcde',
  98                      'deploymentname' => 'Global platform deployment',
  99                      'id' => 0,
 100                      'legacyconsumerkey' => null,
 101                  ],
 102                  'expectations' => [
 103                      'valid' => false,
 104                      'exception' => \coding_exception::class,
 105                      'exceptionmessage' => 'id must be a positive int'
 106                  ]
 107              ],
 108              'Invalid deployment creation, empty deploymentname' => [
 109                  'args' => [
 110                      'registrationid' => 99,
 111                      'deploymentid' => 'deployment-abcde',
 112                      'deploymentname' => '',
 113                      'id' => null,
 114                      'legacyconsumerkey' => null,
 115                  ],
 116                  'expectations' => [
 117                      'valid' => false,
 118                      'exception' => \coding_exception::class,
 119                      'exceptionmessage' => "Invalid 'deploymentname' arg. Cannot be an empty string."
 120                  ]
 121              ],
 122              'Invalid deployment creation, empty deploymentid' => [
 123                  'args' => [
 124                      'registrationid' => 99,
 125                      'deploymentid' => '',
 126                      'deploymentname' => 'Global platform deployment',
 127                      'id' => null,
 128                      'legacyconsumerkey' => null,
 129                  ],
 130                  'expectations' => [
 131                      'valid' => false,
 132                      'exception' => \coding_exception::class,
 133                      'exceptionmessage' => "Invalid 'deploymentid' arg. Cannot be an empty string."
 134                  ]
 135              ]
 136          ];
 137      }
 138  
 139      /**
 140       * Test verifying that a context can only be created from a deployment that has an id.
 141       *
 142       * @covers ::add_context
 143       */
 144      public function test_add_context() {
 145          $deploymentwithid = deployment::create(123, 'deploymentid123', 'Global tool deployment', 55);
 146          $context = $deploymentwithid->add_context('context-id-123', ['CourseSection']);
 147          $this->assertInstanceOf(context::class, $context);
 148          $this->assertEquals(55, $context->get_deploymentid());
 149  
 150          $deploymentwithoutid = deployment::create(123, 'deploymentid123', 'Global tool deployment');
 151          $this->expectException(\coding_exception::class);
 152          $this->expectExceptionMessage("Can't add context to a deployment that hasn't first been saved");
 153          $deploymentwithoutid->add_context('context-id-345', ['Group']);
 154      }
 155  
 156      /**
 157       * Test verifying that a resource_link can only be created from a deployment that has an id.
 158       *
 159       * @covers ::add_resource_link
 160       */
 161      public function test_add_resource_link() {
 162          $deploymentwithid = deployment::create(123, 'deploymentid123', 'Global tool deployment', 55);
 163          $resourcelink = $deploymentwithid->add_resource_link('res-link-id-123', 45);
 164          $this->assertInstanceOf(resource_link::class, $resourcelink);
 165          $this->assertEquals(55, $resourcelink->get_deploymentid());
 166  
 167          $resourcelink2 = $deploymentwithid->add_resource_link('res-link-id-456', 45, 66);
 168          $this->assertEquals(66, $resourcelink2->get_contextid());
 169  
 170          $deploymentwithoutid = deployment::create(123, 'deploymentid123', 'Global tool deployment');
 171          $this->expectException(\coding_exception::class);
 172          $this->expectExceptionMessage("Can't add resource_link to a deployment that hasn't first been saved");
 173          $deploymentwithoutid->add_resource_link('res-link-id-123', 45);
 174      }
 175  
 176      /**
 177       * Test the setter set_legacy_consumer_key.
 178       *
 179       * @covers ::set_legacy_consumer_key
 180       */
 181      public function test_set_legacy_consumer_key() {
 182          $deployment = deployment::create(12, 'deploy-id-123', 'Global tool deployment');
 183          $deployment->set_legacy_consumer_key(str_repeat('a', 255));
 184          $this->assertEquals(str_repeat('a', 255), $deployment->get_legacy_consumer_key());
 185  
 186          $this->expectException(\coding_exception::class);
 187          $this->expectExceptionMessage('Legacy consumer key too long. Cannot exceed 255 chars.');
 188          $deployment->set_legacy_consumer_key(str_repeat('a', 256));
 189      }
 190  }