Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.2.x will end 22 April 2024 (12 months).
  • Bug fixes for security issues in 4.2.x will end 7 October 2024 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.1.x is supported too.

Differences Between: [Versions 310 and 402] [Versions 311 and 402] [Versions 39 and 402] [Versions 400 and 402]

   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  /**
  18   * Tests for tool_licensemanager manager class.
  19   *
  20   * @package    tool_licensemanager
  21   * @copyright  2020 Tom Dickman <tom.dickman@catalyst-au.net>
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  defined('MOODLE_INTERNAL') || die();
  26  
  27  global $CFG;
  28  require_once($CFG->libdir . '/formslib.php');
  29  require_once($CFG->libdir . '/licenselib.php');
  30  
  31  /**
  32   * Tests for tool_licensemanager manager class.
  33   *
  34   * @package    tool_licensemanager
  35   * @copyright  2020 Tom Dickman <tom.dickman@catalyst-au.net>
  36   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  37   * @group      tool_licensemanager
  38   */
  39  class manager_test extends advanced_testcase {
  40  
  41      /**
  42       * Test editing a license.
  43       */
  44      public function test_edit_existing_license() {
  45          $this->resetAfterTest();
  46  
  47          // Create initial custom license to edit.
  48          $testlicense = new stdClass();
  49          $testlicense->shortname = 'my-lic';
  50          $testlicense->fullname = 'My License';
  51          $testlicense->source = 'https://fakeurl.net';
  52          $testlicense->version = date('Ymd', time()) . '00';
  53          $testlicense->custom = license_manager::CUSTOM_LICENSE;
  54  
  55          license_manager::save($testlicense);
  56          license_manager::enable($testlicense->shortname);
  57  
  58          $manager = new \tool_licensemanager\manager();
  59  
  60          // Attempt to submit form data with altered details.
  61          $formdata = [
  62              'shortname' => 'new-value',
  63              'fullname' => 'New License Name',
  64              'source' => 'https://updatedfakeurl.net',
  65              'version' => time()
  66          ];
  67  
  68          // Attempt to submit form data with an altered shortname.
  69          \tool_licensemanager\form\edit_license::mock_submit($formdata);
  70  
  71          // We're testing a private method, so we need to setup reflector magic.
  72          $method = new ReflectionMethod('\tool_licensemanager\manager', 'edit');
  73          $method->setAccessible(true); // Allow accessing of private method.
  74          $method->invoke($manager, \tool_licensemanager\manager::ACTION_UPDATE, $testlicense->shortname);
  75  
  76          // Should not create a new license when updating an existing license.
  77          $this->assertEmpty(license_manager::get_license_by_shortname($formdata['shortname']));
  78  
  79          $actual = license_manager::get_license_by_shortname('my-lic');
  80          // Should not be able to update the shortname of the license.
  81          $this->assertNotSame($formdata['shortname'], $actual->shortname);
  82          // Should be able to update other details of the license.
  83          $this->assertSame($formdata['fullname'], $actual->fullname);
  84          $this->assertSame($formdata['source'], $actual->source);
  85          $this->assertSame(date('Ymd', $formdata['version']) . '00', $actual->version);
  86      }
  87  
  88      public function test_edit_license_not_exists() {
  89          $manager = new \tool_licensemanager\manager();
  90  
  91          // We're testing a private method, so we need to setup reflector magic.
  92          $method = new ReflectionMethod('\tool_licensemanager\manager', 'edit');
  93          $method->setAccessible(true); // Allow accessing of private method.
  94  
  95          // Attempt to update a license that doesn't exist.
  96          $formdata = [
  97              'shortname' => 'new-value',
  98              'fullname' => 'New License Name',
  99              'source' => 'https://updatedfakeurl.net',
 100              'version' => time()
 101          ];
 102          \tool_licensemanager\form\edit_license::mock_submit($formdata);
 103  
 104          // Should not be able to update a license with a shortname that doesn't exist.
 105          $this->expectException('moodle_exception');
 106          $method->invoke($manager, \tool_licensemanager\manager::ACTION_UPDATE, $formdata['shortname']);
 107      }
 108  
 109      public function test_edit_license_no_shortname() {
 110          $manager = new \tool_licensemanager\manager();
 111  
 112          // We're testing a private method, so we need to setup reflector magic.
 113          $method = new ReflectionMethod('\tool_licensemanager\manager', 'edit');
 114          $method->setAccessible(true); // Allow accessing of private method.
 115  
 116          // Attempt to update a license without passing license shortname.
 117          $formdata = [
 118              'fullname' => 'New License Name',
 119              'source' => 'https://updatedfakeurl.net',
 120              'version' => time()
 121          ];
 122          \tool_licensemanager\form\edit_license::mock_submit($formdata);
 123  
 124          // Should not be able to update empty license shortname.
 125          $this->expectException('moodle_exception');
 126          $method->invoke($manager, \tool_licensemanager\manager::ACTION_UPDATE, '');
 127      }
 128  
 129      /**
 130       * Test creating a new license.
 131       */
 132      public function test_edit_create_license() {
 133          $this->resetAfterTest();
 134  
 135          $licensecount = count(license_manager::get_licenses());
 136  
 137          $manager = new \tool_licensemanager\manager();
 138  
 139          $formdata = [
 140              'shortname' => 'new-value',
 141              'fullname' => 'My License',
 142              'source' => 'https://fakeurl.net',
 143              'version' => time()
 144          ];
 145  
 146          // Attempt to submit form data for a new license.
 147          \tool_licensemanager\form\edit_license::mock_submit($formdata);
 148  
 149          // We're testing a private method, so we need to setup reflector magic.
 150          $method = new ReflectionMethod('\tool_licensemanager\manager', 'edit');
 151          $method->setAccessible(true); // Allow accessing of private method.
 152          $method->invoke($manager, \tool_licensemanager\manager::ACTION_CREATE, $formdata['shortname']);
 153  
 154          // Should create a new license in database.
 155          $this->assertCount($licensecount + 1, license_manager::get_licenses());
 156          $actual = license_manager::get_license_by_shortname($formdata['shortname']);
 157          $this->assertSame($formdata['shortname'], $actual->shortname);
 158          $this->assertSame($formdata['fullname'], $actual->fullname);
 159          $this->assertSame($formdata['source'], $actual->source);
 160          $this->assertSame(date('Ymd', $formdata['version']) . '00', $actual->version);
 161  
 162          // Attempt to submit form data for a duplicate license.
 163          \tool_licensemanager\form\edit_license::mock_submit($formdata);
 164  
 165          // Should not be able to create duplicate licenses.
 166          $this->expectException('moodle_exception');
 167          $method->invoke($manager, \tool_licensemanager\manager::ACTION_CREATE, $formdata['shortname']);
 168      }
 169  
 170      /**
 171       * Test changing the order of licenses.
 172       */
 173      public function test_change_license_order() {
 174          $this->resetAfterTest();
 175  
 176          $licenseorder = array_keys(license_manager::get_licenses());
 177          $initialposition = array_search('cc-nc-4.0', $licenseorder);
 178  
 179          $manager = new tool_licensemanager\manager();
 180  
 181          // We're testing a private method, so we need to setup reflector magic.
 182          $method = new ReflectionMethod('\tool_licensemanager\manager', 'change_license_order');
 183          $method->setAccessible(true); // Allow accessing of private method.
 184          $method->invoke($manager, \tool_licensemanager\manager::ACTION_MOVE_UP, 'cc-nc-4.0');
 185  
 186          $licenseorder = array_keys(license_manager::get_licenses());
 187          $newposition = array_search('cc-nc-4.0', $licenseorder);
 188  
 189          $this->assertLessThan($initialposition, $newposition);
 190  
 191          $initialposition = array_search('allrightsreserved', $licenseorder);
 192          $method->invoke($manager, \tool_licensemanager\manager::ACTION_MOVE_DOWN, 'allrightsreserved');
 193          $licenseorder = array_keys(license_manager::get_licenses());
 194          $newposition = array_search('cc-nc-4.0', $licenseorder);
 195  
 196          $this->assertGreaterThan($initialposition, $newposition);
 197      }
 198  
 199  }