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  declare(strict_types=1);
  18  
  19  namespace core\plugininfo;
  20  
  21  use advanced_testcase;
  22  
  23  /**
  24   * Unit tests for the media plugininfo class.
  25   *
  26   * @package     core
  27   * @covers      \core\plugininfo\media
  28   * @copyright   2023 Andrew Lyons <andrew@nicols.co.uk>
  29   * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  30   */
  31  class media_test extends advanced_testcase {
  32  
  33      /**
  34       * Test the get_enabled_plugins method.
  35       *
  36       * @covers ::get_enabled_plugins
  37       */
  38      public function test_get_enabled_plugins(): void {
  39          $this->resetAfterTest();
  40  
  41          $plugins = media::get_enabled_plugins();
  42          $this->assertArrayHasKey('videojs', $plugins);
  43          $this->assertArrayHasKey('youtube', $plugins);
  44  
  45          // Disable a plugin.
  46          media::enable_plugin('youtube', 0);
  47  
  48          $plugins = media::get_enabled_plugins();
  49          $this->assertArrayNotHasKey('youtube', $plugins);
  50          $this->assertArrayHasKey('videojs', $plugins);
  51      }
  52  
  53      /**
  54       * Test the is_uninstall_allowed method.
  55       *
  56       * @dataProvider is_uninstall_allowed_provider
  57       * @param string $plugin
  58       */
  59      public function test_is_uninstall_allowed(
  60          string $plugin,
  61      ): void {
  62          $pluginmanager = \core_plugin_manager::instance();
  63          $plugininfo = $pluginmanager->get_plugin_info("media_{$plugin}");
  64          $this->assertTrue($plugininfo->is_uninstall_allowed());
  65      }
  66  
  67      /**
  68       * Data provider for the is_uninstall_allowed tests.
  69       *
  70       * @return array
  71       */
  72      public function is_uninstall_allowed_provider(): array {
  73          $plugins = media::get_enabled_plugins();
  74          return array_map(function ($plugin) {
  75              return [
  76                  'plugin' => $plugin,
  77              ];
  78          }, array_keys($plugins));
  79      }
  80  
  81      /**
  82       * Ensure that change_plugin_order() changes the order of the plugins.
  83       *
  84       * @dataProvider change_plugin_order_provider
  85       * @param string $initialorder
  86       * @param string $pluginname
  87       * @param int $direction
  88       * @param array $neworder
  89       */
  90      public function test_change_plugin_order(
  91          array $initialorder,
  92          string $pluginname,
  93          int $direction,
  94          array $neworder,
  95      ): void {
  96          $this->resetAfterTest(true);
  97  
  98          media::set_enabled_plugins($initialorder);
  99          media::change_plugin_order($pluginname, $direction);
 100  
 101          $this->assertSame(
 102              $neworder,
 103              array_keys(media::get_sorted_plugins()),
 104          );
 105      }
 106  
 107      public function change_plugin_order_provider(): array {
 108          $pluginmanager = \core_plugin_manager::instance();
 109          $allplugins = $pluginmanager->get_plugins_of_type('media');
 110          \core_collator::asort_objects_by_method($allplugins, 'get_rank', \core_collator::SORT_NUMERIC);
 111          $getorder = function (array $plugins) use ($allplugins) {
 112              return array_merge(
 113                  $plugins,
 114                  array_diff(array_reverse(array_keys($allplugins)), array_values($plugins)),
 115              );
 116          };
 117  
 118          return [
 119              'Top item down one' => [
 120                  'initialorder' => [
 121                      'videojs',
 122                      'html5audio',
 123                      'html5video',
 124                      'youtube',
 125                  ],
 126                  'pluginname' => 'videojs',
 127                  'direction' => base::MOVE_DOWN,
 128                  'expected' => $getorder([
 129                      'html5audio',
 130                      'videojs',
 131                      'html5video',
 132                      'youtube',
 133                  ]),
 134              ],
 135              'Bottom item down one' => [
 136                  'initialorder' => [
 137                      'videojs',
 138                      'html5audio',
 139                      'html5video',
 140                      'youtube',
 141                  ],
 142                  'pluginname' => 'youtube',
 143                  'direction' => base::MOVE_DOWN,
 144                  'expected' => $getorder([
 145                      'videojs',
 146                      'html5audio',
 147                      'html5video',
 148                      'youtube',
 149                  ]),
 150              ],
 151              'Top item up' => [
 152                  'initialorder' => [
 153                      'videojs',
 154                      'html5audio',
 155                      'html5video',
 156                      'youtube',
 157                  ],
 158                  'pluginname' => 'videojs',
 159                  'direction' => base::MOVE_UP,
 160                  'expected' => $getorder([
 161                      'videojs',
 162                      'html5audio',
 163                      'html5video',
 164                      'youtube',
 165                  ]),
 166              ],
 167              'Disabled plugin' => [
 168                  'initialorder' => [
 169                      'videojs',
 170                      'html5audio',
 171                      'html5video',
 172                  ],
 173                  'pluginname' => 'youtube',
 174                  'direction' => base::MOVE_UP,
 175                  'expected' => $getorder([
 176                      'videojs',
 177                      'html5audio',
 178                      'html5video',
 179                  ]),
 180              ],
 181              'Non-existent plugin' => [
 182                  'initialorder' => [
 183                      'videojs',
 184                      'html5audio',
 185                      'html5video',
 186                      'youtube',
 187                  ],
 188                  'pluginname' => 'moodletube',
 189                  'direction' => base::MOVE_UP,
 190                  'expected' => $getorder([
 191                      'videojs',
 192                      'html5audio',
 193                      'html5video',
 194                      'youtube',
 195                  ]),
 196              ],
 197          ];
 198      }
 199  }