MDL-76867 core_admin: Migrate media plugin management to dynamic table

This commit is contained in:
Andrew Nicols 2023-02-20 20:06:32 +08:00
parent 9bc9204a52
commit 6aba049a64
6 changed files with 389 additions and 29 deletions

View File

@ -0,0 +1,71 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
namespace core_admin\table;
use moodle_url;
use stdClass;
/**
* Media plugin admin settings.
*
* @package core_admin
* @copyright 2023 Andrew Lyons <andrew@nicols.co.uk>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class media_management_table extends \core_admin\table\plugin_management_table {
/** @var array The list of used extensions */
protected array $usedextensions = [];
protected function get_plugintype(): string {
return 'media';
}
protected function get_action_url(array $params = []): moodle_url {
return new moodle_url('/admin/media.php', $params);
}
protected function get_column_list(): array {
$columns = parent::get_column_list();
return array_merge(
array_slice($columns, 0, 1, true),
['supports' => get_string('supports', 'core_media')],
array_slice($columns, 1, null, true),
);
}
protected function col_name(stdClass $row): string {
global $OUTPUT, $PAGE;
$name = $row->plugininfo->name;
if ($PAGE->theme->resolve_image_location('icon', 'media_' . $name, false)) {
$icon = $OUTPUT->pix_icon('icon', '', "media_{$name}", ['class' => 'icon pluginicon']);
} else {
$icon = $OUTPUT->pix_icon('spacer', '', 'moodle', ['class' => 'icon pluginicon noicon']);
}
$help = '';
if (get_string_manager()->string_exists('pluginname_help', 'media_' . $name)) {
$help = '&nbsp;' . $OUTPUT->help_icon('pluginname', 'media_' . $name);
}
return $icon . $row->plugininfo->displayname . $help;
}
protected function col_supports(stdClass $row): string {
return $row->plugininfo->supports($this->usedextensions);
}
}

View File

@ -23,14 +23,11 @@
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
define('NO_OUTPUT_BUFFERING', true);
require_once('../config.php');
require_once($CFG->libdir.'/adminlib.php');
require_once("{$CFG->libdir}/adminlib.php");
$action = required_param('action', PARAM_ALPHANUMEXT);
$media = required_param('media', PARAM_PLUGIN);
$confirm = optional_param('confirm', 0, PARAM_BOOL);
$action = required_param('action', PARAM_ALPHANUMEXT);
$plugin = required_param('plugin', PARAM_PLUGIN);
$PAGE->set_url('/admin/media.php');
$PAGE->set_context(context_system::instance());
@ -38,42 +35,38 @@ $PAGE->set_context(context_system::instance());
require_admin();
require_sesskey();
$plugins = core_plugin_manager::instance()->get_plugins_of_type('media');
$sortorder = array_values(\core\plugininfo\media::get_enabled_plugins());
$return = new moodle_url('/admin/settings.php', array('section' => 'managemediaplayers'));
if (!array_key_exists($media, $plugins)) {
redirect($return);
}
$return = new moodle_url('/admin/settings.php', [
'section' => 'managemediaplayers',
]);
$displayname = get_string('pluginname', "media_{$plugin}");
switch ($action) {
case 'disable':
$class = \core_plugin_manager::resolve_plugininfo_class('media');
$class::enable_plugin($media, false);
if ($class::enable_plugin($plugin, false)) {
\core\notification::add(
get_string('plugin_disabled', 'core_admin', $displayname),
\core\notification::SUCCESS
);
}
break;
case 'enable':
$class = \core_plugin_manager::resolve_plugininfo_class('media');
$class::enable_plugin($media, true);
if ($class::enable_plugin($plugin, true)) {
\core\notification::add(
get_string('plugin_enabled', 'core_admin', $displayname),
\core\notification::SUCCESS
);
}
break;
case 'up':
if (($pos = array_search($media, $sortorder)) > 0) {
$tmp = $sortorder[$pos - 1];
$sortorder[$pos - 1] = $sortorder[$pos];
$sortorder[$pos] = $tmp;
\core\plugininfo\media::set_enabled_plugins($sortorder);
}
$class::change_plugin_order($plugin, $class::MOVE_UP);
break;
case 'down':
if ((($pos = array_search($media, $sortorder)) !== false) && ($pos < count($sortorder) - 1)) {
$tmp = $sortorder[$pos + 1];
$sortorder[$pos + 1] = $sortorder[$pos];
$sortorder[$pos] = $tmp;
\core\plugininfo\media::set_enabled_plugins($sortorder);
}
$class::change_plugin_order($plugin, $class::MOVE_DOWN);
break;
}

View File

@ -278,7 +278,12 @@ if ($hassiteconfig) {
$temp = new admin_settingpage('managemediaplayers', new lang_string('managemediaplayers', 'media'));
$temp->add(new admin_setting_heading('mediaformats', get_string('mediaformats', 'core_media'),
format_text(get_string('mediaformats_desc', 'core_media'), FORMAT_MARKDOWN)));
$temp->add(new admin_setting_managemediaplayers());
$temp->add(new \core_admin\admin\admin_setting_plugin_manager(
'media',
\core_admin\table\media_management_table::class,
'managemediaplayers',
new lang_string('managemediaplayers', 'core_media'),
));
$temp->add(new admin_setting_heading('managemediaplayerscommonheading', new lang_string('commonsettings', 'admin'), ''));
$temp->add(new admin_setting_configtext('media_default_width',
new lang_string('defaultwidth', 'core_media'), new lang_string('defaultwidthdesc', 'core_media'),

View File

@ -0,0 +1,30 @@
@core @core_admin
Feature: An administrator can manage Media plugins
In order to alter the user experience
As an admin
I can manage media plugins
@javascript
Scenario: An administrator can control the enabled state of media plugins using JavaScript
Given I am logged in as "admin"
And I navigate to "Plugins > Media players > Manage media players" in site administration
When I click on "Disable the YouTube plugin" "link"
Then I should see "The YouTube plugin has been disabled"
And "Disable the YouTube plugin" "link" should not exist
But "Enable the YouTube plugin" "link" should exist
When I click on "Enable the YouTube plugin" "link"
Then I should see "The YouTube plugin has been enabled"
And "Enable the YouTube plugin" "link" should not exist
But "Disable the YouTube plugin" "link" should exist
Scenario: An administrator can control the enabled state of media plugins without JavaScript
Given I am logged in as "admin"
And I navigate to "Plugins > Media players > Manage media players" in site administration
When I click on "Disable the YouTube plugin" "link"
Then I should see "The YouTube plugin has been disabled"
And "Disable the YouTube plugin" "link" should not exist
But "Enable the YouTube plugin" "link" should exist
When I click on "Enable the YouTube plugin" "link"
Then I should see "The YouTube plugin has been enabled"
And "Enable the YouTube plugin" "link" should not exist
But "Disable the YouTube plugin" "link" should exist

View File

@ -204,4 +204,66 @@ class media extends base {
}
return '';
}
public static function plugintype_supports_ordering(): bool {
return true;
}
// phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
public static function get_sorted_plugins(bool $enabledonly = false): ?array {
$pluginmanager = \core_plugin_manager::instance();
$plugins = $pluginmanager->get_plugins_of_type('media');
$enabledplugins = $pluginmanager->get_enabled_plugins('media');
// Sort plugins so enabled plugins are displayed first and all others are displayed in the end sorted by rank.
\core_collator::asort_objects_by_method($plugins, 'get_rank', \core_collator::SORT_NUMERIC);
$order = array_values($enabledplugins);
if (!$enabledonly) {
$order = array_merge($order, array_diff(array_reverse(array_keys($plugins)), $order));
}
$sortedplugins = [];
foreach ($order as $name) {
$sortedplugins[$name] = $plugins[$name];
}
return $sortedplugins;
}
public static function change_plugin_order(string $pluginname, int $direction): bool {
$activeeditors = array_keys(self::get_sorted_plugins(true));
$key = array_search($pluginname, $activeeditors);
[$media] = explode('_', $pluginname, 2);
if ($key === false) {
return false;
}
$sortorder = array_values(self::get_enabled_plugins());
if ($direction === self::MOVE_DOWN) {
// Move down the list.
if ((($pos = array_search($media, $sortorder)) !== false) && ($pos < count($sortorder) - 1)) {
$tmp = $sortorder[$pos + 1];
$sortorder[$pos + 1] = $sortorder[$pos];
$sortorder[$pos] = $tmp;
self::set_enabled_plugins($sortorder);
return true;
}
} else if ($direction === self::MOVE_UP) {
if (($pos = array_search($media, $sortorder)) > 0) {
$tmp = $sortorder[$pos - 1];
$sortorder[$pos - 1] = $sortorder[$pos];
$sortorder[$pos] = $tmp;
self::set_enabled_plugins($sortorder);
return true;
}
}
return false;
}
}

View File

@ -0,0 +1,199 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
declare(strict_types=1);
namespace core\plugininfo;
use advanced_testcase;
/**
* Unit tests for the media plugininfo class.
*
* @package core
* @covers \core\plugininfo\media
* @copyright 2023 Andrew Lyons <andrew@nicols.co.uk>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class media_test extends advanced_testcase {
/**
* Test the get_enabled_plugins method.
*
* @covers ::get_enabled_plugins
*/
public function test_get_enabled_plugins(): void {
$this->resetAfterTest();
$plugins = media::get_enabled_plugins();
$this->assertArrayHasKey('videojs', $plugins);
$this->assertArrayHasKey('youtube', $plugins);
// Disable a plugin.
media::enable_plugin('youtube', 0);
$plugins = media::get_enabled_plugins();
$this->assertArrayNotHasKey('youtube', $plugins);
$this->assertArrayHasKey('videojs', $plugins);
}
/**
* Test the is_uninstall_allowed method.
*
* @dataProvider is_uninstall_allowed_provider
* @param string $plugin
*/
public function test_is_uninstall_allowed(
string $plugin,
): void {
$pluginmanager = \core_plugin_manager::instance();
$plugininfo = $pluginmanager->get_plugin_info("media_{$plugin}");
$this->assertTrue($plugininfo->is_uninstall_allowed());
}
/**
* Data provider for the is_uninstall_allowed tests.
*
* @return array
*/
public function is_uninstall_allowed_provider(): array {
$plugins = media::get_enabled_plugins();
return array_map(function ($plugin) {
return [
'plugin' => $plugin,
];
}, array_keys($plugins));
}
/**
* Ensure that change_plugin_order() changes the order of the plugins.
*
* @dataProvider change_plugin_order_provider
* @param string $initialorder
* @param string $pluginname
* @param int $direction
* @param array $neworder
*/
public function test_change_plugin_order(
array $initialorder,
string $pluginname,
int $direction,
array $neworder,
): void {
$this->resetAfterTest(true);
media::set_enabled_plugins($initialorder);
media::change_plugin_order($pluginname, $direction);
$this->assertSame(
$neworder,
array_keys(media::get_sorted_plugins()),
);
}
public function change_plugin_order_provider(): array {
$pluginmanager = \core_plugin_manager::instance();
$allplugins = $pluginmanager->get_plugins_of_type('media');
\core_collator::asort_objects_by_method($allplugins, 'get_rank', \core_collator::SORT_NUMERIC);
$getorder = function (array $plugins) use ($allplugins) {
return array_merge(
$plugins,
array_diff(array_reverse(array_keys($allplugins)), array_values($plugins)),
);
};
return [
'Top item down one' => [
'initialorder' => [
'videojs',
'html5audio',
'html5video',
'youtube',
],
'pluginname' => 'videojs',
'direction' => base::MOVE_DOWN,
'expected' => $getorder([
'html5audio',
'videojs',
'html5video',
'youtube',
]),
],
'Bottom item down one' => [
'initialorder' => [
'videojs',
'html5audio',
'html5video',
'youtube',
],
'pluginname' => 'youtube',
'direction' => base::MOVE_DOWN,
'expected' => $getorder([
'videojs',
'html5audio',
'html5video',
'youtube',
]),
],
'Top item up' => [
'initialorder' => [
'videojs',
'html5audio',
'html5video',
'youtube',
],
'pluginname' => 'videojs',
'direction' => base::MOVE_UP,
'expected' => $getorder([
'videojs',
'html5audio',
'html5video',
'youtube',
]),
],
'Disabled plugin' => [
'initialorder' => [
'videojs',
'html5audio',
'html5video',
],
'pluginname' => 'youtube',
'direction' => base::MOVE_UP,
'expected' => $getorder([
'videojs',
'html5audio',
'html5video',
]),
],
'Non-existent plugin' => [
'initialorder' => [
'videojs',
'html5audio',
'html5video',
'youtube',
],
'pluginname' => 'moodletube',
'direction' => base::MOVE_UP,
'expected' => $getorder([
'videojs',
'html5audio',
'html5video',
'youtube',
]),
],
];
}
}