Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.3.x will end 7 October 2024 (12 months).
  • Bug fixes for security issues in 4.3.x will end 21 April 2025 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.2.x is supported too.

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