Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.2.x will end 22 April 2024 (12 months).
  • Bug fixes for security issues in 4.2.x will end 7 October 2024 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.1.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 core_courseformat\external;
  18  
  19  use core_external\external_api;
  20  
  21  defined('MOODLE_INTERNAL') || die();
  22  
  23  global $CFG;
  24  require_once($CFG->dirroot . '/webservice/tests/helpers.php');
  25  
  26  use dndupload_handler;
  27  
  28  /**
  29   * Tests for the file_hanlders class.
  30   *
  31   * @package    core_course
  32   * @category   test
  33   * @copyright  2022 Ferran Recio <ferran@moodle.com>
  34   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  35   * @coversDefaultClass \core_courseformat\external\file_handlers
  36   */
  37  class file_handlers_test extends \externallib_advanced_testcase {
  38  
  39      /**
  40       * Setup to ensure that fixtures are loaded.
  41       */
  42      public static function setupBeforeClass(): void { // phpcs:ignore
  43          global $CFG;
  44          require_once($CFG->dirroot . '/course/lib.php');
  45          require_once($CFG->dirroot . '/course/dnduploadlib.php');
  46      }
  47  
  48      /**
  49       * Test the behaviour of get_state::execute().
  50       *
  51       * @covers ::execute
  52       */
  53      public function test_execute(): void {
  54          $this->resetAfterTest();
  55          $course = $this->getDataGenerator()->create_course(['numsections' => 3, 'format' => 'topics']);
  56          $this->setAdminUser();
  57  
  58          $result = file_handlers::execute($course->id);
  59          $result = external_api::clean_returnvalue(file_handlers::execute_returns(), $result);
  60  
  61          $handlers = new dndupload_handler($course, null);
  62          $expected = $handlers->get_js_data();
  63  
  64          $this->assertCount(count($expected->filehandlers), $result);
  65          foreach ($expected->filehandlers as $key => $handler) {
  66              $tocompare = $result[$key];
  67              $this->assertEquals($handler->extension, $tocompare['extension']);
  68          }
  69      }
  70  
  71      /**
  72       * Test the behaviour of get_state::execute() in a wrong course.
  73       *
  74       * @covers ::execute
  75       */
  76      public function test_execute_wrong_course(): void {
  77          $this->resetAfterTest();
  78          $course = $this->getDataGenerator()->create_course(['numsections' => 3, 'format' => 'topics']);
  79          $this->setAdminUser();
  80  
  81          $this->expectException('dml_missing_record_exception');
  82          $result = file_handlers::execute(-1);
  83          $result = external_api::clean_returnvalue(file_handlers::execute_returns(), $result);
  84      }
  85  }