Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.
   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 tool_moodlenet\local;
  18  
  19  use tool_moodlenet\local\import_handler_registry;
  20  use tool_moodlenet\local\import_processor;
  21  use tool_moodlenet\local\import_strategy_file;
  22  use tool_moodlenet\local\import_strategy_link;
  23  use tool_moodlenet\local\remote_resource;
  24  use tool_moodlenet\local\url;
  25  
  26  /**
  27   * Class tool_moodlenet_import_processor_testcase, providing test cases for the import_processor class.
  28   *
  29   * @package    tool_moodlenet
  30   * @category   test
  31   * @copyright  2020 Jake Dallimore <jrhdallimore@gmail.com>
  32   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  33   */
  34  class import_processor_test extends \advanced_testcase {
  35  
  36      /**
  37       * An integration test, this confirms the ability to construct an import processor and run the import for the current user.
  38       */
  39      public function test_process_valid_resource() {
  40          $this->resetAfterTest();
  41  
  42          // Set up a user as a teacher in a course.
  43          $course = $this->getDataGenerator()->create_course();
  44          $teacher = $this->getDataGenerator()->create_and_enrol($course, 'editingteacher');
  45          $section = 0;
  46          $this->setUser($teacher);
  47  
  48          // Set up the import, using a mod_resource handler for the html extension.
  49          $resourceurl = $this->getExternalTestFileUrl('/test.html');
  50          $remoteresource = new remote_resource(
  51              new \curl(),
  52              new url($resourceurl),
  53              (object) [
  54                  'name' => 'Resource name',
  55                  'description' => 'Resource description'
  56              ]
  57          );
  58          $handlerregistry = new import_handler_registry($course, $teacher);
  59          $handlerinfo = $handlerregistry->get_resource_handler_for_mod_and_strategy($remoteresource, 'resource',
  60              new import_strategy_file());
  61          $importproc = new import_processor($course, $section, $remoteresource, $handlerinfo, $handlerregistry);
  62  
  63          // Import the file.
  64          $importproc->process();
  65  
  66          // Verify there is a new mod_resource created with correct name, description and containing the test.html file.
  67          $modinfo = get_fast_modinfo($course, $teacher->id);
  68          $cms = $modinfo->get_instances();
  69          $this->assertArrayHasKey('resource', $cms);
  70          $cminfo = array_shift($cms['resource']);
  71          $this->assertEquals('Resource name', $cminfo->get_formatted_name());
  72          $cm = get_coursemodule_from_id('', $cminfo->id, 0, false, MUST_EXIST);
  73          list($cm, $context, $module, $data, $cw) = get_moduleinfo_data($cminfo, $course);
  74          $this->assertEquals($remoteresource->get_description(), $data->intro);
  75          $fs = get_file_storage();
  76          $files = $fs->get_area_files(\context_module::instance($cminfo->id)->id, 'mod_resource', 'content', false,
  77              'sortorder DESC, id ASC', false);
  78          $file = reset($files);
  79          $this->assertEquals('test.html', $file->get_filename());
  80          $this->assertEquals('text/html', $file->get_mimetype());
  81      }
  82  
  83      /**
  84       * Test confirming that an exception is thrown when trying to process a resource which does not exist.
  85       */
  86      public function test_process_invalid_resource() {
  87          $this->resetAfterTest();
  88  
  89          // Set up a user as a teacher in a course.
  90          $course = $this->getDataGenerator()->create_course();
  91          $teacher = $this->getDataGenerator()->create_and_enrol($course, 'editingteacher');
  92          $section = 0;
  93          $this->setUser($teacher);
  94  
  95          // Set up the import, using a mod_resource handler for the html extension.
  96          $resourceurl = $this->getExternalTestFileUrl('/test.htmlzz');
  97          $remoteresource = new remote_resource(
  98              new \curl(),
  99              new url($resourceurl),
 100              (object) [
 101                  'name' => 'Resource name',
 102                  'description' => 'Resource description'
 103              ]
 104          );
 105          $handlerregistry = new import_handler_registry($course, $teacher);
 106          $handlerinfo = $handlerregistry->get_resource_handler_for_mod_and_strategy($remoteresource, 'resource',
 107              new import_strategy_file());
 108          $importproc = new import_processor($course, $section, $remoteresource, $handlerinfo, $handlerregistry);
 109  
 110          // Import the file.
 111          $this->expectException(\coding_exception::class);
 112          $importproc->process();
 113      }
 114  
 115      /**
 116       * Test confirming that imports can be completed using alternative import strategies.
 117       */
 118      public function test_process_alternative_import_strategies() {
 119          $this->resetAfterTest();
 120  
 121          // Set up a user as a teacher in a course.
 122          $course = $this->getDataGenerator()->create_course();
 123          $teacher = $this->getDataGenerator()->create_and_enrol($course, 'editingteacher');
 124          $section = 0;
 125          $this->setUser($teacher);
 126  
 127          // Set up the import, using a mod_url handler and the link import strategy.
 128          $remoteresource = new remote_resource(
 129              new \curl(),
 130              new url('http://example.com/cats.pdf'),
 131              (object) [
 132                  'name' => 'Resource name',
 133                  'description' => 'Resource description'
 134              ]
 135          );
 136          $handlerregistry = new import_handler_registry($course, $teacher);
 137          $handlerinfo = $handlerregistry->get_resource_handler_for_mod_and_strategy($remoteresource, 'url',
 138              new import_strategy_link());
 139          $importproc = new import_processor($course, $section, $remoteresource, $handlerinfo, $handlerregistry);
 140  
 141          // Import the resource as a link.
 142          $importproc->process();
 143  
 144          // Verify there is a new mod_url created with name 'cats' and containing the URL of the resource.
 145          $modinfo = get_fast_modinfo($course, $teacher->id);
 146          $cms = $modinfo->get_instances();
 147          $this->assertArrayHasKey('url', $cms);
 148          $cminfo = array_shift($cms['url']);
 149          $this->assertEquals('Resource name', $cminfo->get_formatted_name());
 150      }
 151  }