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.
   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_admin\external;
  20  
  21  defined('MOODLE_INTERNAL') || die();
  22  
  23  global $CFG;
  24  require_once($CFG->dirroot . '/webservice/tests/helpers.php');
  25  
  26  /**
  27   * Unit tests to configure the plugin order.
  28   *
  29   * Note: Not all plugins can be ordered, so this test is limited to those which support it.
  30   *
  31   * @package     core
  32   * @covers      \core_admin\external\set_plugin_state
  33   * @copyright   2023 Andrew Lyons <andrew@nicols.co.uk>
  34   * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  35   */
  36  class set_plugin_order_test extends \externallib_advanced_testcase {
  37      /**
  38       * Text execute method for editor plugins, which support ordering.
  39       *
  40       * @dataProvider execute_editor_provider
  41       * @param string $initialstate The initial state of the plugintype
  42       * @param string $plugin The name of the plugin
  43       * @param int $direction
  44       * @param array $neworder
  45       * @param string $newstate
  46       */
  47      public function test_execute_editors(
  48          string $initialstate,
  49          string $plugin,
  50          int $direction,
  51          array $neworder,
  52          string $newstate,
  53      ): void {
  54          global $CFG;
  55  
  56          $this->resetAfterTest();
  57          $this->setAdminUser();
  58  
  59          $CFG->texteditors = $initialstate;
  60  
  61          set_plugin_order::execute($plugin, $direction);
  62  
  63          $this->assertSame(
  64              $neworder,
  65              array_keys(\core\plugininfo\editor::get_sorted_plugins()),
  66          );
  67          $this->assertSame($newstate, $CFG->texteditors);
  68      }
  69  
  70      /**
  71       * Data provider for base tests of the execute method.
  72       *
  73       * @return array
  74       */
  75      public function execute_editor_provider(): array {
  76          $pluginmanager = \core_plugin_manager::instance();
  77          $allplugins = array_keys($pluginmanager->get_plugins_of_type('editor'));
  78  
  79          // Disabled editors are listed alphabetically at the end.
  80          $getorder = function (array $plugins) use ($allplugins) {
  81              return array_merge(
  82                  $plugins,
  83                  array_diff($allplugins, array_values($plugins)),
  84              );
  85          };
  86          return [
  87              [
  88                  'initialstate' => 'textarea,tiny',
  89                  'pluginname' => 'editor_textarea',
  90                  'direction' => 1, // DOWN.
  91                  'expected' => $getorder([
  92                      'tiny',
  93                      'textarea',
  94                  ]),
  95                  'newtexteditors' => 'tiny,textarea',
  96              ],
  97              [
  98                  'initialstate' => 'textarea,tiny',
  99                  'pluginname' => 'editor_textarea',
 100                  'direction' => -1, // UP.
 101                  'expected' => $getorder([
 102                      'textarea',
 103                      'tiny',
 104                  ]),
 105                  'newtexteditors' => 'textarea,tiny',
 106              ],
 107              [
 108                  'initialstate' => 'textarea,tiny',
 109                  'pluginname' => 'editor_tiny',
 110                  'direction' => 1, // DOWN.
 111                  // Tiny is already at the bottom of the list of enabled plugins.
 112                  'expected' => $getorder([
 113                      'textarea',
 114                      'tiny',
 115                  ]),
 116                  'newtexteditors' => 'textarea,tiny',
 117              ],
 118              [
 119                  'initialstate' => 'textarea,tiny',
 120                  'pluginname' => 'editor_atto',
 121                  'direction' => 1, // DOWN.
 122                  // Atto is not enabled. Disabled editors are listed lexically after enabled editors.
 123                  'expected' => $getorder([
 124                      'textarea',
 125                      'tiny',
 126                  ]),
 127                  'newtexteditors' => 'textarea,tiny',
 128              ],
 129          ];
 130      }
 131  
 132      /**
 133       * Text execute method for plugins which do not support ordering.
 134       *
 135       * @dataProvider execute_non_orderable_provider
 136       * @param string $plugin
 137       */
 138      public function test_execute_editors_non_orderable(string $plugin): void {
 139          $this->resetAfterTest();
 140          $this->setAdminUser();
 141  
 142          $this->assertIsArray(set_plugin_order::execute($plugin, 1));
 143      }
 144  
 145      public function execute_non_orderable_provider(): array {
 146          return [
 147              // Activities do not support ordering.
 148              ['mod_assign'],
 149              // Nor to blocks.
 150              ['block_login'],
 151          ];
 152      }
 153  
 154      /**
 155       * Test execute method with no login.
 156       */
 157      public function test_execute_no_login(): void {
 158          $this->expectException(\require_login_exception::class);
 159          set_plugin_order::execute('editor_tiny', 1);
 160      }
 161  
 162      /**
 163       * Test execute method with no login.
 164       */
 165      public function test_execute_no_capability(): void {
 166          $this->resetAfterTest();
 167          $user = $this->getDataGenerator()->create_user();
 168          $this->setUser($user);
 169          $this->expectException(\required_capability_exception::class);
 170          set_plugin_order::execute('editor_tiny', 1);
 171      }
 172  }