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.

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

   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   * Repository generator tests
  19   *
  20   * @package   core_repository
  21   * @category  test
  22   * @copyright 2013 Frédéric Massart
  23   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  namespace core_repository;
  27  
  28  use repository_exception;
  29  
  30  /**
  31   * Repository generator tests class
  32   *
  33   * @package   repository
  34   * @category  test
  35   * @copyright 2013 Frédéric Massart
  36   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  37   */
  38  class generator_test extends \advanced_testcase {
  39  
  40      /**
  41       * Basic test of creation of repository types.
  42       *
  43       * @return void
  44       */
  45      public function test_create_type() {
  46          global $DB;
  47          $this->resetAfterTest(true);
  48  
  49          // All the repository types.
  50          $all = array('coursefiles', 'dropbox', 'equella', 'filesystem', 'flickr',
  51              'flickr_public', 'googledocs', 'local', 'nextcloud', 'merlot', 'recent', 's3', 'upload', 'url',
  52              'user', 'webdav', 'wikimedia', 'youtube');
  53  
  54          // The ones enabled during installation.
  55          $alreadyenabled = array('local', 'recent', 'upload', 'url', 'user', 'wikimedia');
  56  
  57          // Enable all the repositories which are not enabled yet.
  58          foreach ($all as $type) {
  59              if (in_array($type, $alreadyenabled)) {
  60                  continue;
  61              }
  62              $repotype = $this->getDataGenerator()->create_repository_type($type);
  63              $this->assertEquals($repotype->type, $type, 'Unexpected name after creating repository type ' . $type);
  64              $this->assertTrue($DB->record_exists('repository', array('type' => $type, 'visible' => 1)));
  65          }
  66  
  67          // Check that all the repositories have been enabled.
  68          foreach ($all as $type) {
  69              $caughtexception = false;
  70              try {
  71                  $this->getDataGenerator()->create_repository_type($type);
  72              } catch (repository_exception $e) {
  73                  if ($e->getMessage() === 'This repository already exists') {
  74                      $caughtexception = true;
  75                  }
  76              }
  77              $this->assertTrue($caughtexception, "Repository type '$type' should have already been enabled");
  78          }
  79      }
  80  
  81      /**
  82       * Ensure that the type options are properly saved.
  83       *
  84       * @return void
  85       */
  86      public function test_create_type_custom_options() {
  87          global $DB;
  88          $this->resetAfterTest(true);
  89  
  90          // Single instances.
  91          // Note: for single instances repositories enablecourseinstances and enableuserinstances are forced set to 0.
  92          $record = new \stdClass();
  93          $record->pluginname = 'Custom Flickr';
  94          $record->api_key = '12345';
  95          $record->secret = '67890';
  96          $flickr = $this->getDataGenerator()->create_repository_type('flickr', $record);
  97  
  98          $config = get_config('flickr');
  99          $record->enableuserinstances = '0';
 100          $record->enablecourseinstances = '0';
 101          $this->assertEquals($record, $config);
 102          $this->assertEquals('Custom Flickr',
 103              $DB->get_field('repository_instances', 'name', array('typeid' => $flickr->id), MUST_EXIST));
 104  
 105          // Create a dropbox oauth issuer.
 106          $this->setAdminUser();
 107          $params = [
 108              'name' => 'Dropbox',
 109              'clientid' => 'key',
 110              'clientsecret' => 'secret',
 111              'loginparamsoffline' => 'token_access_type=offline',
 112              'image' => '',
 113              'showonloginpage' => 1,
 114          ];
 115          $issuer = \core\oauth2\api::create_issuer((object)$params);
 116          $record = new \stdClass();
 117          $record->pluginname = 'Custom Dropbox';
 118          $record->dropbox_issuerid = $issuer->get('id');
 119          $record->dropbox_cachelimit = '123';
 120          $dropbox = $this->getDataGenerator()->create_repository_type('dropbox', $record);
 121  
 122          $config = get_config('dropbox');
 123          $record->enableuserinstances = '0';
 124          $record->enablecourseinstances = '0';
 125          $this->assertEquals($record, $config);
 126          $this->assertEquals('Custom Dropbox',
 127              $DB->get_field('repository_instances', 'name', array('typeid' => $dropbox->id), MUST_EXIST));
 128  
 129          // Multiple instances.
 130          $record = new \stdClass();
 131          $record->pluginname = 'Custom WebDAV';
 132          $record->enableuserinstances = '0';
 133          $record->enablecourseinstances = '0';
 134          $webdav = $this->getDataGenerator()->create_repository_type('webdav', $record);
 135  
 136          $config = get_config('webdav');
 137          $this->assertEquals($record, $config);
 138          $this->assertFalse( $DB->record_exists('repository_instances', array('typeid' => $webdav->id)));
 139  
 140          $record = new \stdClass();
 141          $record->pluginname = 'Custom Equella';
 142          $record->enableuserinstances = '1';
 143          $record->enablecourseinstances = '0';
 144          $equella = $this->getDataGenerator()->create_repository_type('equella', $record);
 145  
 146          $config = get_config('equella');
 147          $this->assertEquals($record, $config);
 148          $this->assertFalse( $DB->record_exists('repository_instances', array('typeid' => $equella->id)));
 149      }
 150  
 151      /**
 152       * Covers basic testing of instance creation.
 153       *
 154       * @return void
 155       */
 156      public function test_create_instance() {
 157          global $DB;
 158          $this->resetAfterTest(true);
 159  
 160          $course = $this->getDataGenerator()->create_course();
 161          $user = $this->getDataGenerator()->create_user();
 162          $block = $this->getDataGenerator()->create_block('online_users');
 163  
 164          $type = $this->getDataGenerator()->create_repository_type('webdav');
 165          $record = new \stdClass();
 166          $record->name = 'A WebDAV instance';
 167          $record->webdav_type = '1';
 168          $record->webdav_server = 'localhost';
 169          $record->webdav_port = '12345';
 170          $record->webdav_path = '/nothing';
 171          $record->webdav_user = 'me';
 172          $record->webdav_password = '\o/';
 173          $record->webdav_auth = 'basic';
 174          $instance = $this->getDataGenerator()->create_repository('webdav', $record);
 175  
 176          $this->assertEquals(1, $DB->count_records('repository_instances', array('typeid' => $type->id)));
 177          $this->assertEquals($record->name, $DB->get_field('repository_instances', 'name', array('id' => $instance->id)));
 178          $entries = $DB->get_records('repository_instance_config', array('instanceid' => $instance->id));
 179          $config = new \stdClass();
 180          foreach ($entries as $entry) {
 181              $config->{$entry->name} = $entry->value;
 182          }
 183          unset($record->name);
 184          $this->assertEquals($config, $record);
 185  
 186          // Course context.
 187          $record = new \stdClass();
 188          $record->contextid = \context_course::instance($course->id)->id;
 189          $instance = $this->getDataGenerator()->create_repository('webdav', $record);
 190          $this->assertEquals(2, $DB->count_records('repository_instances', array('typeid' => $type->id)));
 191          $this->assertEquals($record->contextid, $instance->contextid);
 192  
 193          // User context.
 194          $record->contextid = \context_user::instance($user->id)->id;
 195          $instance = $this->getDataGenerator()->create_repository('webdav', $record);
 196          $this->assertEquals(3, $DB->count_records('repository_instances', array('typeid' => $type->id)));
 197          $this->assertEquals($record->contextid, $instance->contextid);
 198  
 199          // Invalid context.
 200          $this->expectException('coding_exception');
 201          $record->contextid = \context_block::instance($block->id)->id;
 202          $instance = $this->getDataGenerator()->create_repository('webdav', $record);
 203      }
 204  
 205  }