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.
   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_handler_info;
  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_handler_registry_testcase, providing test cases for the import_handler_registry 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_handler_registry_test extends \advanced_testcase {
  35  
  36      /**
  37       * Test confirming the behaviour of get_resource_handlers_for_strategy with different params.
  38       */
  39      public function test_get_resource_handlers_for_strategy() {
  40          $this->resetAfterTest();
  41  
  42          $course = $this->getDataGenerator()->create_course();
  43          $teacher = $this->getDataGenerator()->create_and_enrol($course, 'editingteacher');
  44          $ihr = new import_handler_registry($course, $teacher);
  45          $resource = new remote_resource(
  46              new \curl(),
  47              new url('http://example.org'),
  48              (object) [
  49                  'name' => 'Resource name',
  50                  'description' => 'Resource description'
  51              ]
  52          );
  53  
  54          $handlers = $ihr->get_resource_handlers_for_strategy($resource, new import_strategy_file());
  55          $this->assertIsArray($handlers);
  56          foreach ($handlers as $handler) {
  57              $this->assertInstanceOf(import_handler_info::class, $handler);
  58          }
  59      }
  60  
  61      /**
  62       * Test confirming that the results are scoped to the provided user.
  63       */
  64      public function test_get_resource_handlers_for_strategy_user_scoping() {
  65          $this->resetAfterTest();
  66  
  67          $course = $this->getDataGenerator()->create_course();
  68          $student = $this->getDataGenerator()->create_and_enrol($course, 'student');
  69          $teacher = $this->getDataGenerator()->create_and_enrol($course, 'editingteacher');
  70  
  71          $studentihr = new import_handler_registry($course, $student);
  72          $teacherihr = new import_handler_registry($course, $teacher);
  73          $resource = new remote_resource(
  74              new \curl(),
  75              new url('http://example.org'),
  76              (object) [
  77                  'name' => 'Resource name',
  78                  'description' => 'Resource description'
  79              ]
  80          );
  81  
  82          $this->assertEmpty($studentihr->get_resource_handlers_for_strategy($resource, new import_strategy_file()));
  83          $this->assertNotEmpty($teacherihr->get_resource_handlers_for_strategy($resource, new import_strategy_file()));
  84      }
  85  
  86      /**
  87       * Test confirming that we can find a unique handler based on the module and strategy name.
  88       */
  89      public function test_get_resource_handler_for_module_and_strategy() {
  90          $this->resetAfterTest();
  91  
  92          $course = $this->getDataGenerator()->create_course();
  93          $teacher = $this->getDataGenerator()->create_and_enrol($course, 'editingteacher');
  94          $ihr = new import_handler_registry($course, $teacher);
  95          $resource = new remote_resource(
  96              new \curl(),
  97              new url('http://example.org'),
  98              (object) [
  99                  'name' => 'Resource name',
 100                  'description' => 'Resource description'
 101              ]
 102          );
 103  
 104          // Resource handles every file type, so we'll always be able to find that unique handler when looking.
 105          $handler = $ihr->get_resource_handler_for_mod_and_strategy($resource, 'resource', new import_strategy_file());
 106          $this->assertInstanceOf(import_handler_info::class, $handler);
 107  
 108          // URL handles every resource, so we'll always be able to find that unique handler when looking with a link strategy.
 109          $handler = $ihr->get_resource_handler_for_mod_and_strategy($resource, 'url', new import_strategy_link());
 110          $this->assertInstanceOf(import_handler_info::class, $handler);
 111          $this->assertEquals('url', $handler->get_module_name());
 112          $this->assertInstanceOf(import_strategy_link::class, $handler->get_strategy());
 113      }
 114  }