Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 3.9.x will end* 10 May 2021 (12 months).
  • Bug fixes for security issues in 3.9.x will end* 8 May 2023 (36 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.
   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   * Unit tests for the import_handler_registry class.
  19   *
  20   * @package    tool_moodlenet
  21   * @category   test
  22   * @copyright  2020 Jake Dallimore <jrhdallimore@gmail.com>
  23   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  namespace tool_moodlenet\local\tests;
  26  
  27  use tool_moodlenet\local\import_handler_registry;
  28  use tool_moodlenet\local\import_handler_info;
  29  use tool_moodlenet\local\import_strategy_file;
  30  use tool_moodlenet\local\import_strategy_link;
  31  use tool_moodlenet\local\remote_resource;
  32  use tool_moodlenet\local\url;
  33  
  34  defined('MOODLE_INTERNAL') || die();
  35  
  36  /**
  37   * Class tool_moodlenet_import_handler_registry_testcase, providing test cases for the import_handler_registry class.
  38   */
  39  class tool_moodlenet_import_handler_registry_testcase extends \advanced_testcase {
  40  
  41      /**
  42       * Test confirming the behaviour of get_resource_handlers_for_strategy with different params.
  43       */
  44      public function test_get_resource_handlers_for_strategy() {
  45          $this->resetAfterTest();
  46  
  47          $course = $this->getDataGenerator()->create_course();
  48          $teacher = $this->getDataGenerator()->create_and_enrol($course, 'editingteacher');
  49          $ihr = new import_handler_registry($course, $teacher);
  50          $resource = new remote_resource(
  51              new \curl(),
  52              new url('http://example.org'),
  53              (object) [
  54                  'name' => 'Resource name',
  55                  'description' => 'Resource description'
  56              ]
  57          );
  58  
  59          $handlers = $ihr->get_resource_handlers_for_strategy($resource, new import_strategy_file());
  60          $this->assertIsArray($handlers);
  61          foreach ($handlers as $handler) {
  62              $this->assertInstanceOf(import_handler_info::class, $handler);
  63          }
  64      }
  65  
  66      /**
  67       * Test confirming that the results are scoped to the provided user.
  68       */
  69      public function test_get_resource_handlers_for_strategy_user_scoping() {
  70          $this->resetAfterTest();
  71  
  72          $course = $this->getDataGenerator()->create_course();
  73          $student = $this->getDataGenerator()->create_and_enrol($course, 'student');
  74          $teacher = $this->getDataGenerator()->create_and_enrol($course, 'editingteacher');
  75  
  76          $studentihr = new import_handler_registry($course, $student);
  77          $teacherihr = new import_handler_registry($course, $teacher);
  78          $resource = new remote_resource(
  79              new \curl(),
  80              new url('http://example.org'),
  81              (object) [
  82                  'name' => 'Resource name',
  83                  'description' => 'Resource description'
  84              ]
  85          );
  86  
  87          $this->assertEmpty($studentihr->get_resource_handlers_for_strategy($resource, new import_strategy_file()));
  88          $this->assertNotEmpty($teacherihr->get_resource_handlers_for_strategy($resource, new import_strategy_file()));
  89      }
  90  
  91      /**
  92       * Test confirming that we can find a unique handler based on the module and strategy name.
  93       */
  94      public function test_get_resource_handler_for_module_and_strategy() {
  95          $this->resetAfterTest();
  96  
  97          $course = $this->getDataGenerator()->create_course();
  98          $teacher = $this->getDataGenerator()->create_and_enrol($course, 'editingteacher');
  99          $ihr = new import_handler_registry($course, $teacher);
 100          $resource = new remote_resource(
 101              new \curl(),
 102              new url('http://example.org'),
 103              (object) [
 104                  'name' => 'Resource name',
 105                  'description' => 'Resource description'
 106              ]
 107          );
 108  
 109          // Resource handles every file type, so we'll always be able to find that unique handler when looking.
 110          $handler = $ihr->get_resource_handler_for_mod_and_strategy($resource, 'resource', new import_strategy_file());
 111          $this->assertInstanceOf(import_handler_info::class, $handler);
 112  
 113          // URL handles every resource, so we'll always be able to find that unique handler when looking with a link strategy.
 114          $handler = $ihr->get_resource_handler_for_mod_and_strategy($resource, 'url', new import_strategy_link());
 115          $this->assertInstanceOf(import_handler_info::class, $handler);
 116          $this->assertEquals('url', $handler->get_module_name());
 117          $this->assertInstanceOf(import_strategy_link::class, $handler->get_strategy());
 118      }
 119  }