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.

Differences Between: [Versions 401 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   * Unit tests for the custom file types.
  19   *
  20   * @package tool_filetypes
  21   * @copyright 2014 The Open University
  22   * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  defined('MOODLE_INTERNAL') || die();
  26  
  27  use tool_filetypes\utils;
  28  
  29  /**
  30   * Unit tests for the custom file types.
  31   *
  32   * @package tool_filetypes
  33   * @copyright 2014 The Open University
  34   * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  35   */
  36  class tool_filetypes_test extends advanced_testcase {
  37      /**
  38       * Tests is_extension_invalid() function.
  39       */
  40      public function test_is_extension_invalid() {
  41          // The pdf file extension already exists in default moodle minetypes.
  42          $this->assertTrue(utils::is_extension_invalid('pdf'));
  43  
  44          // The frog extension does not.
  45          $this->assertFalse(utils::is_extension_invalid('frog'));
  46  
  47          // However you could use the pdf extension when editing the pdf extension.
  48          $this->assertFalse(utils::is_extension_invalid('pdf', 'pdf'));
  49  
  50          // Blank extension is invalid.
  51          $this->assertTrue(utils::is_extension_invalid(''));
  52  
  53          // Extensions with dot are invalid.
  54          $this->assertTrue(utils::is_extension_invalid('.frog'));
  55      }
  56  
  57      /**
  58       * Tests is_defaulticon_allowed() function.
  59       */
  60      public function test_is_defaulticon_allowed() {
  61          // You ARE allowed to set a default icon for a MIME type that hasn't
  62          // been used yet.
  63          $this->assertTrue(utils::is_defaulticon_allowed('application/x-frog'));
  64  
  65          // You AREN'T allowed to set default icon for text/plain as there is
  66          // already a type that has that set.
  67          $this->assertFalse(utils::is_defaulticon_allowed('text/plain'));
  68  
  69          // But you ARE still allowed to set it when actually editing txt, which
  70          // is the default.
  71          $this->assertTrue(utils::is_defaulticon_allowed('text/plain', 'txt'));
  72      }
  73  
  74      /**
  75       * Tests get_icons_from_path() function.
  76       */
  77      public function test_get_icons_from_path() {
  78          // Get icons from the fixtures folder.
  79          $icons = utils::get_icons_from_path(__DIR__ . '/fixtures');
  80  
  81          // The icons are returned alphabetically and with keys === values.
  82          // For the icon with numbers after the name, only the base name is
  83          // returned and only one of it.
  84          $this->assertEquals(array('frog' => 'frog', 'zombie' => 'zombie'), $icons);
  85      }
  86  }