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.
   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  declare(strict_types=1);
  18  
  19  namespace core\plugininfo;
  20  
  21  use advanced_testcase;
  22  
  23  /**
  24   * Unit tests for the dataformat plugininfo class
  25   *
  26   * @package     core
  27   * @covers      \core\plugininfo\dataformat
  28   * @copyright   2022 Paul Holden <paulh@moodle.com>
  29   * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  30   */
  31  class dataformat_test extends advanced_testcase {
  32  
  33      /**
  34       * Helper method, to allow easy filtering of default formats in order to perform assertions without any third-party
  35       * formats affecting expected results
  36       *
  37       * @param string $format
  38       * @return bool
  39       */
  40      private function filter_default_plugins(string $format): bool {
  41          $defaultformats = ['csv', 'excel', 'html', 'json', 'ods', 'pdf'];
  42  
  43          return in_array($format, $defaultformats);
  44      }
  45  
  46      /**
  47       * Test getting enabled plugins
  48       */
  49      public function test_get_enabled_plugins(): void {
  50          $this->resetAfterTest();
  51  
  52          // Check all default formats.
  53          $plugins = array_filter(dataformat::get_enabled_plugins(), [$this, 'filter_default_plugins']);
  54          $this->assertEquals([
  55              'csv' => 'csv',
  56              'excel' => 'excel',
  57              'html' => 'html',
  58              'json' => 'json',
  59              'ods' => 'ods',
  60              'pdf' => 'pdf',
  61          ], $plugins);
  62  
  63          // Disable excel & html.
  64          dataformat::enable_plugin('excel', 0);
  65          dataformat::enable_plugin('html', 0);
  66  
  67          $plugins = array_filter(dataformat::get_enabled_plugins(), [$this, 'filter_default_plugins']);
  68          $this->assertEquals([
  69              'csv' => 'csv',
  70              'json' => 'json',
  71              'ods' => 'ods',
  72              'pdf' => 'pdf',
  73          ], $plugins);
  74      }
  75  
  76      /**
  77       * Test getting enabled plugins obeys configured sortorder
  78       */
  79      public function test_get_enabled_plugins_sorted(): void {
  80          $this->resetAfterTest();
  81  
  82          set_config('dataformat_plugins_sortorder', 'csv,pdf,excel,json,html,ods');
  83  
  84          $plugins = array_filter(dataformat::get_enabled_plugins(), [$this, 'filter_default_plugins']);
  85          $this->assertEquals([
  86              'csv' => 'csv',
  87              'pdf' => 'pdf',
  88              'excel' => 'excel',
  89              'json' => 'json',
  90              'html' => 'html',
  91              'ods' => 'ods',
  92          ], $plugins);
  93      }
  94  }