Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

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

   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('boxnet', 'coursefiles', 'dropbox', 'equella', 'filesystem', 'flickr',
  51              'flickr_public', 'googledocs', 'local', 'nextcloud', 'merlot', 'picasa', '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          $record = new \stdClass();
 106          $record->pluginname = 'Custom Dropbox';
 107          $record->dropbox_key = '12345';
 108          $record->dropbox_secret = '67890';
 109          $record->dropbox_cachelimit = '123';
 110          $dropbox = $this->getDataGenerator()->create_repository_type('dropbox', $record);
 111  
 112          $config = get_config('dropbox');
 113          $record->enableuserinstances = '0';
 114          $record->enablecourseinstances = '0';
 115          $this->assertEquals($record, $config);
 116          $this->assertEquals('Custom Dropbox',
 117              $DB->get_field('repository_instances', 'name', array('typeid' => $dropbox->id), MUST_EXIST));
 118  
 119          // Multiple instances.
 120          $record = new \stdClass();
 121          $record->pluginname = 'Custom WebDAV';
 122          $record->enableuserinstances = '0';
 123          $record->enablecourseinstances = '0';
 124          $webdav = $this->getDataGenerator()->create_repository_type('webdav', $record);
 125  
 126          $config = get_config('webdav');
 127          $this->assertEquals($record, $config);
 128          $this->assertFalse( $DB->record_exists('repository_instances', array('typeid' => $webdav->id)));
 129  
 130          $record = new \stdClass();
 131          $record->pluginname = 'Custom Equella';
 132          $record->enableuserinstances = '1';
 133          $record->enablecourseinstances = '0';
 134          $equella = $this->getDataGenerator()->create_repository_type('equella', $record);
 135  
 136          $config = get_config('equella');
 137          $this->assertEquals($record, $config);
 138          $this->assertFalse( $DB->record_exists('repository_instances', array('typeid' => $equella->id)));
 139      }
 140  
 141      /**
 142       * Covers basic testing of instance creation.
 143       *
 144       * @return void
 145       */
 146      public function test_create_instance() {
 147          global $DB;
 148          $this->resetAfterTest(true);
 149  
 150          $course = $this->getDataGenerator()->create_course();
 151          $user = $this->getDataGenerator()->create_user();
 152          $block = $this->getDataGenerator()->create_block('online_users');
 153  
 154          $type = $this->getDataGenerator()->create_repository_type('webdav');
 155          $record = new \stdClass();
 156          $record->name = 'A WebDAV instance';
 157          $record->webdav_type = '1';
 158          $record->webdav_server = 'localhost';
 159          $record->webdav_port = '12345';
 160          $record->webdav_path = '/nothing';
 161          $record->webdav_user = 'me';
 162          $record->webdav_password = '\o/';
 163          $record->webdav_auth = 'basic';
 164          $instance = $this->getDataGenerator()->create_repository('webdav', $record);
 165  
 166          $this->assertEquals(1, $DB->count_records('repository_instances', array('typeid' => $type->id)));
 167          $this->assertEquals($record->name, $DB->get_field('repository_instances', 'name', array('id' => $instance->id)));
 168          $entries = $DB->get_records('repository_instance_config', array('instanceid' => $instance->id));
 169          $config = new \stdClass();
 170          foreach ($entries as $entry) {
 171              $config->{$entry->name} = $entry->value;
 172          }
 173          unset($record->name);
 174          $this->assertEquals($config, $record);
 175  
 176          // Course context.
 177          $record = new \stdClass();
 178          $record->contextid = \context_course::instance($course->id)->id;
 179          $instance = $this->getDataGenerator()->create_repository('webdav', $record);
 180          $this->assertEquals(2, $DB->count_records('repository_instances', array('typeid' => $type->id)));
 181          $this->assertEquals($record->contextid, $instance->contextid);
 182  
 183          // User context.
 184          $record->contextid = \context_user::instance($user->id)->id;
 185          $instance = $this->getDataGenerator()->create_repository('webdav', $record);
 186          $this->assertEquals(3, $DB->count_records('repository_instances', array('typeid' => $type->id)));
 187          $this->assertEquals($record->contextid, $instance->contextid);
 188  
 189          // Invalid context.
 190          $this->expectException('coding_exception');
 191          $record->contextid = \context_block::instance($block->id)->id;
 192          $instance = $this->getDataGenerator()->create_repository('webdav', $record);
 193      }
 194  
 195  }