Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

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

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